-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
91 lines (80 loc) · 2.54 KB
/
Copy pathmainwindow.cpp
File metadata and controls
91 lines (80 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::initializeApp()
{
//create a new qgraphicsscene
scene = new QGraphicsScene(this);
//generate and display the random image
on_pushButtonGenerate_clicked();
}//initializeApp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initializeApp();
}//MainWindow
MainWindow::~MainWindow()
{
delete ui;
}//MainWindow
void MainWindow::on_pushButtonGenerate_clicked()
{
QRgb value=0;
QPixmap pixMap;
//get width and height of image
int width = ui->lineEditWidth->text().toInt();
int height = ui->lineEditHeight->text().toInt();
//create image of specified size
myImage = QImage(width, height, QImage::Format_RGB32);
if(myImage.isNull())
{
qDebug() << "NULL Image!";
}//if we have a bad image dont do anything
else {
for(int x=0;x<myImage.width();x++){
for(int y=0;y<myImage.height();y++){
if(ui->radioButtonColor->isChecked()){
//get random color
value=getRandomColor();
}//if color
else if(ui->radioButtonGrey->isChecked()){
//get random grey
value=getRandomGreyScale();
}//else if grey
myImage.setPixel(x,y,value);
}//for every y pixel
}//for every x pixel
//create pixmap from image
pixMap = QPixmap::fromImage(myImage);
//add pixmap to scene
scene->addPixmap(pixMap);
//set the scene size to match size
scene->setSceneRect(pixMap.rect());
//display scene in graphicsview
ui->graphicsView->setScene(scene);
}//else we have a valid image
}//on_pushButtonGenerate_clicked
void MainWindow::on_pushButtonSave_clicked()
{
myImage.save(QFileDialog::getSaveFileName(this, tr("Save File"),
"keyFile",
tr("Images (*.png)")), "PNG");
}//on_pushButtonSave_clicked
qint64 MainWindow::genRand(qint64 max)
{
qint64 randValue = ranGen.bounded(max);
return randValue;
}//genRand
QRgb MainWindow::getRandomColor()
{
return QColor::fromRgb(ranGen.generate()).rgb();
}//getRandomColor
QRgb MainWindow::getRandomGreyScale()
{
//grescale is done differently, all rgb values must be the same
int randomInt = genRand(255);
return qRgb(randomInt,
randomInt,
randomInt);
}//getRandomGreyScale