IntelliPhoto 0.5

IntelliPhotoGui.cpp
Go to the documentation of this file.
1 // ---------- IntelliPhotoGui.cpp ----------
2
3 #include <QtWidgets>
4 #include <QPixmap>
5
6 #include "IntelliPhotoGui.h"
7 #include "Layer/PaintingArea.h"
8
9 // IntelliPhotoGui constructor
10 IntelliPhotoGui::IntelliPhotoGui(){
11 // create Gui elements and lay them out
12 createGui();
13 // Create actions
14 createActions();
15 // create Menus
16 createMenus();
17 // set style of the gui
18 setIntelliStyle();
19 // Size the app
20 resize(600,600);
21 showMaximized();
22
23 }
24
25 // User tried to close the app
26 void IntelliPhotoGui::closeEvent(QCloseEvent*event){
27 // If they try to close maybeSave() returns true
28 // if no changes have been made and the app closes
29 if (maybeSave()) {
30 event->accept();
31 } else {
32 // If there have been changes ignore the event
33 event->ignore();
34 }
35 }
36
37 // Check if the current image has been changed and then
38 // open a dialog to open a file
39 void IntelliPhotoGui::slotOpen(){
40 // Check if changes have been made since last save
41 // maybeSave() returns true if no changes have been made
42 if (maybeSave()) {
43
44 // Get the file to open from a dialog
45 // tr sets the window title to Open File
46 // QDir opens the current dirctory
47 QString fileName = QFileDialog::getOpenFileName(this,
48 tr("Open File"), QDir::currentPath(), nullptr, nullptr, QFileDialog::DontUseNativeDialog);
49
50 // If we have a file name load the image and place
51 // it in the paintingArea
52 if (!fileName.isEmpty())
53 paintingArea->open(fileName);
54 }
55 }
56
57 // Called when the user clicks Save As in the menu
58 void IntelliPhotoGui::slotSave(){
59 // A QAction represents the action of the user clicking
60 QAction*action = qobject_cast<QAction*>(sender());
61
62 // Stores the array of bytes of the users data
63 QByteArray fileFormat = action->data().toByteArray();
64
65 // Pass it to be saved
66 saveFile(fileFormat);
67 }
68
69 // Opens a dialog that allows the user to create a New Layer
70 void IntelliPhotoGui::slotCreateNewLayer(){
71 // Stores button value
72 bool ok1, ok2;
73
74 // "New Layer" is the title of the window
75 // the next tr is the text to display
76 // Define the standard Value, min, max, step and ok button
77 int width = QInputDialog::getInt(this, tr("New Layer"),
78 tr("Width:"),
79 200,1, 500, 1, &ok1);
80 int height = QInputDialog::getInt(this, tr("New Layer"),
81 tr("Height:"),
82 200,1, 500, 1, &ok2);
83 // Create New Layer
84 if (ok1&&ok2)
85 {
86 int layer = paintingArea->addLayer(width,height,0,0);
87 paintingArea->slotActivateLayer(layer);
88 }
89 }
90
91 // Opens a dialog that allows the user to delete a Layer
92 void IntelliPhotoGui::slotDeleteLayer(){
93 // Stores button value
94 bool ok;
95
96 // "delete Layer" is the title of the window
97 // the next tr is the text to display
98 // Define the standard Value, min, max, step and ok button
99 int layerNumber = QInputDialog::getInt(this, tr("delete Layer"),
100 tr("Number:"),
101 1,0, 500, 1, &ok);
102 // Create New Layer
103 if (ok)
104 paintingArea->deleteLayer(layerNumber);
105 }
106
107 void IntelliPhotoGui::slotSetActiveAlpha(){
108 // Stores button value
109 bool ok1, ok2;
110
111 // "Layer to set on" is the title of the window
112 // the next tr is the text to display
113 // Define the standard Value, min, max, step and ok button
114 int layer = QInputDialog::getInt(this, tr("Layer to set on"),
115 tr("Layer:"),
116 -1,-1,100,1, &ok1);
117 // "New Alpha" is the title of the window
118 int alpha = QInputDialog::getInt(this, tr("New Alpha"),
119 tr("Alpha:"),
120 255,0, 255, 1, &ok2);
121 if (ok1&&ok2)
122 {
123 paintingArea->setAlphaOfLayer(layer,alpha);
124 }
125 }
126
127 void IntelliPhotoGui::slotPositionMoveUp(){
128 paintingArea->movePositionActive(0,-20);
129 update();
130 }
131
132 void IntelliPhotoGui::slotPositionMoveDown(){
133 paintingArea->movePositionActive(0,20);
134 update();
135 }
136
137 void IntelliPhotoGui::slotPositionMoveLeft(){
138 paintingArea->movePositionActive(-20,0);
139 update();
140 }
141
142 void IntelliPhotoGui::slotPositionMoveRight(){
143 paintingArea->movePositionActive(20,0);
144 update();
145 }
146
147 void IntelliPhotoGui::slotMoveLayerUp(){
148 paintingArea->moveActiveLayer(1);
149 update();
150 }
151
152 void IntelliPhotoGui::slotMoveLayerDown(){
153 paintingArea->moveActiveLayer(-1);
154 update();
155 }
156
157 void IntelliPhotoGui::slotClearActiveLayer(){
158 // Stores button value
159 bool ok1, ok2, ok3, ok4;
160
161 // "Red Input" is the title of the window
162 // the next tr is the text to display
163 // Define the standard Value, min, max, step and ok button
164 int red = QInputDialog::getInt(this, tr("Red Input"),
165 tr("Red:"),
166 255,0, 255,1, &ok1);
167 // "Green Input" is the title of the window
168 int green = QInputDialog::getInt(this, tr("Green Input"),
169 tr("Green:"),
170 255,0, 255, 1, &ok2);
171 // "Blue Input" is the title of the window
172 int blue = QInputDialog::getInt(this, tr("Blue Input"),
173 tr("Blue:"),
174 255,0, 255, 1, &ok3);
175 // "Alpha Input" is the title of the window
176 int alpha = QInputDialog::getInt(this, tr("Alpha Input"),
177 tr("Alpha:"),
178 255,0, 255, 1, &ok4);
179 if (ok1&&ok2&&ok3&&ok4)
180 {
181 paintingArea->floodFill(red, green, blue, alpha);
182 }
183 }
184
185 void IntelliPhotoGui::slotSetActiveLayer(){
186 // Stores button value
187 bool ok1;
188
189 // "Layer to set on" is the title of the window
190 // the next tr is the text to display
191 // Define the standard Value, min, max, step and ok button
192 int layer = QInputDialog::getInt(this, tr("Layer to set on"),
193 tr("Layer:"),
194 -1,0,255,1, &ok1);
195 if (ok1)
196 {
197 paintingArea->setLayerToActive(layer);
198 }
199 }
200
201 void IntelliPhotoGui::slotSetFirstColor(){
202 paintingArea->colorPickerSetFirstColor();
203 }
204
205 void IntelliPhotoGui::slotSetSecondColor(){
206 paintingArea->colorPickerSetSecondColor();
207 }
208
209 void IntelliPhotoGui::slotSwitchColor(){
210 paintingArea->colorPickerSwitchColor();
211 }
212
213 void IntelliPhotoGui::slotCreatePenTool(){
214 paintingArea->createPenTool();
215 }
216
217 void IntelliPhotoGui::slotCreatePlainTool(){
218 paintingArea->createPlainTool();
219 }
220
221 void IntelliPhotoGui::slotCreateLineTool(){
222 paintingArea->createLineTool();
223 }
224
225 void IntelliPhotoGui::slotCreateRectangleTool(){
226 paintingArea->createRectangleTool();
227 }
228
229 void IntelliPhotoGui::slotCreateCircleTool(){
230 paintingArea->createCircleTool();
231 }
232
233 void IntelliPhotoGui::slotCreatePolygonTool(){
234 paintingArea->createPolygonTool();
235 }
236
237 void IntelliPhotoGui::slotCreateFloodFillTool(){
238 paintingArea->createFloodFillTool();
239 }
240
241 // Open an about dialog
242 void IntelliPhotoGui::slotAboutDialog(){
243 // Window title and text to display
244 QMessageBox::about(this, tr("About Painting"),
245 tr("<p><b>IntelliPhoto</b>Pretty basic editor.</p>"));
246 }
247
248 // Define menu actions that call functions
249 void IntelliPhotoGui::createActions(){
250 // Get a list of the supported file formats
251 // QImageWriter is used to write images to files
252 foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
253 QString text = tr("%1...").arg(QString(format).toUpper());
254
255 // Create an action for each file format
256 QAction*action = new QAction(text, this);
257
258 // Set an action for each file format
259 action->setData(format);
260
261 // When clicked call IntelliPhotoGui::save()
262 connect(action, SIGNAL(triggered()), this, SLOT(slotSave()));
263
264 // Attach each file format option menu item to Save As
265 actionSaveAs.append(action);
266 }
267
268 //set exporter to actions
269 QAction*pngSaveAction = new QAction("PNG-8", this);
270 pngSaveAction->setData("PNG");
271 // When clicked call IntelliPhotoGui::save()
272 connect(pngSaveAction, SIGNAL(triggered()), this, SLOT(slotSave()));
273 // Attach each PNG in save Menu
274 actionSaveAs.append(pngSaveAction);
275
276 // Create exit action and tie to IntelliPhotoGui::close()
277 actionExit = new QAction(tr("&Exit"), this);
278 actionExit->setShortcuts(QKeySequence::Quit);
279 connect(actionExit, SIGNAL(triggered()), this, SLOT(close()));
280
281 actionOpen = new QAction(tr("&Open"), this);
282 actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
283 connect(actionOpen, SIGNAL(triggered()), this, SLOT(slotOpen()));
284
285 // Create New Layer action and tie to IntelliPhotoGui::newLayer()
286 actionCreateNewLayer = new QAction(tr("&New Layer..."), this);
287 actionCreateNewLayer->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
288 connect(actionCreateNewLayer, SIGNAL(triggered()), this, SLOT(slotCreateNewLayer()));
289
290 // Delete New Layer action and tie to IntelliPhotoGui::deleteLayer()
291 actionDeleteLayer = new QAction(tr("&Delete Layer..."), this);
292 connect(actionDeleteLayer, SIGNAL(triggered()), this, SLOT(slotDeleteLayer()));
293
294 actionSetActiveLayer = new QAction(tr("&set Active"), this);
295 connect(actionSetActiveLayer, SIGNAL(triggered()), this, SLOT(slotSetActiveLayer()));
296
297 actionSetActiveAlpha = new QAction(tr("&set Alpha"), this);
298 connect(actionSetActiveAlpha, SIGNAL(triggered()), this, SLOT(slotSetActiveAlpha()));
299
300 actionMovePositionUp = new QAction(tr("&move Up"), this);
301 actionMovePositionUp->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
302 connect(actionMovePositionUp, SIGNAL(triggered()), this, SLOT(slotPositionMoveUp()));
303
304 actionMovePositionDown = new QAction(tr("&move Down"), this);
305 actionMovePositionDown->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
306 connect(actionMovePositionDown, SIGNAL(triggered()), this, SLOT(slotPositionMoveDown()));
307
308 actionMovePositionLeft = new QAction(tr("&move Left"), this);
309 actionMovePositionLeft->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
310 connect(actionMovePositionLeft, SIGNAL(triggered()), this, SLOT(slotPositionMoveLeft()));
311
312 actionMovePositionRight = new QAction(tr("&move Right"), this);
313 actionMovePositionRight->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
314 connect(actionMovePositionRight, SIGNAL(triggered()), this, SLOT(slotPositionMoveRight()));
315
316 actionMoveLayerUp = new QAction(tr("&move Layer Up"), this);
317 actionMoveLayerUp->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Up));
318 connect(actionMoveLayerUp, SIGNAL(triggered()), this, SLOT(slotMoveLayerUp()));
319
320 actionMoveLayerDown= new QAction(tr("&move Layer Down"), this);
321 actionMoveLayerDown->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Down));
322 connect(actionMoveLayerDown, SIGNAL(triggered()), this, SLOT(slotMoveLayerDown()));
323
324 //Create Color Actions here
325 actionColorPickerFirstColor = new QAction(tr("&Main"), this);
326 connect(actionColorPickerFirstColor, SIGNAL(triggered()), this, SLOT(slotSetFirstColor()));
327
328 actionColorPickerSecondColor = new QAction(tr("&Secondary"), this);
329 connect(actionColorPickerSecondColor, SIGNAL(triggered()), this, SLOT(slotSetSecondColor()));
330
331 actionColorSwitch = new QAction(tr("&Switch"), this);
332 actionColorSwitch->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
333 connect(actionColorSwitch, SIGNAL(triggered()), this, SLOT(slotSwitchColor()));
334
335 //Create Tool actions down here
336 actionCreatePlainTool = new QAction(tr("&Plain"), this);
337 connect(actionCreatePlainTool, SIGNAL(triggered()), this, SLOT(slotCreatePlainTool()));
338
339 actionCreatePenTool = new QAction(tr("&Pen"),this);
340 connect(actionCreatePenTool, SIGNAL(triggered()), this, SLOT(slotCreatePenTool()));
341
342 actionCreateLineTool = new QAction(tr("&Line"), this);
343 connect(actionCreateLineTool, SIGNAL(triggered()), this, SLOT(slotCreateLineTool()));
344
345 actionCreateCircleTool = new QAction(tr("&Circle"), this);
346 connect(actionCreateCircleTool, SIGNAL(triggered()), this, SLOT(slotCreateCircleTool()));
347
348 actionCreateRectangleTool = new QAction(tr("&Rectangle"), this);
349 connect(actionCreateRectangleTool, SIGNAL(triggered()), this, SLOT(slotCreateRectangleTool()));
350
351 actionCreatePolygonTool = new QAction(tr("&Polygon"), this);
352 connect(actionCreatePolygonTool, SIGNAL(triggered()), this, SLOT(slotCreatePolygonTool()));
353
354 actionCreateFloodFillTool = new QAction(tr("&FloodFill"), this);
355 connect(actionCreateFloodFillTool, SIGNAL(triggered()), this, SLOT(slotCreateFloodFillTool()));
356
357 // Create about action and tie to IntelliPhotoGui::about()
358 actionAboutDialog = new QAction(tr("&About"), this);
359 connect(actionAboutDialog, SIGNAL(triggered()), this, SLOT(slotAboutDialog()));
360
361 // Create about Qt action and tie to IntelliPhotoGui::aboutQt()
362 actionAboutQtDialog = new QAction(tr("About &Qt"), this);
363 connect(actionAboutQtDialog, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
364 }
365
366 // Create the menubar
367 void IntelliPhotoGui::createMenus(){
368 // Create Save As option and the list of file types
369 saveAsMenu = new QMenu(tr("&Save As"), this);
370 foreach (QAction *action, actionSaveAs)
371 saveAsMenu->addAction(action);
372
373
374 // Attach all actions to File
375 fileMenu = new QMenu(tr("&File"), this);
376 fileMenu->addAction(actionOpen);
377 fileMenu->addMenu(saveAsMenu);
378 fileMenu->addSeparator();
379 fileMenu->addAction(actionExit);
380
381 // Attach all actions to Options
382 optionMenu = new QMenu(tr("&Options"), this);
383 optionMenu->addAction(actionSetActiveLayer);
384 optionMenu->addAction(actionSetActiveAlpha);
385 optionMenu->addAction(actionMovePositionUp);
386 optionMenu->addAction(actionMovePositionDown);
387 optionMenu->addAction(actionMovePositionLeft);
388 optionMenu->addAction(actionMovePositionRight);
389 optionMenu->addAction(actionMoveLayerUp);
390 optionMenu->addAction(actionMoveLayerDown);
391
392 // Attach all actions to Layer
393 layerMenu = new QMenu(tr("&Layer"), this);
394 layerMenu->addAction(actionCreateNewLayer);
395 layerMenu->addAction(actionDeleteLayer);
396
397 //Attach all Color Options
398 colorMenu = new QMenu(tr("&Color"), this);
399 colorMenu->addAction(actionColorPickerFirstColor);
400 colorMenu->addAction(actionColorPickerSecondColor);
401 colorMenu->addAction(actionColorSwitch);
402
403 //Attach all Tool Options
404 toolMenu = new QMenu(tr("&Tools"), this);
405 toolMenu->addAction(actionCreatePenTool);
406 toolMenu->addAction(actionCreatePlainTool);
407 toolMenu->addAction(actionCreateLineTool);
408 toolMenu->addAction(actionCreateRectangleTool);
409 toolMenu->addAction(actionCreateCircleTool);
410 toolMenu->addAction(actionCreatePolygonTool);
411 toolMenu->addAction(actionCreateFloodFillTool);
412 toolMenu->addSeparator();
413 toolMenu->addMenu(colorMenu);
414
415 // Attach all actions to Help
416 helpMenu = new QMenu(tr("&Help"), this);
417 helpMenu->addAction(actionAboutDialog);
418 helpMenu->addAction(actionAboutQtDialog);
419
420 // Add menu items to the menubar
421 menuBar()->addMenu(fileMenu);
422 menuBar()->addMenu(optionMenu);
423 menuBar()->addMenu(layerMenu);
424 menuBar()->addMenu(toolMenu);
425 menuBar()->addMenu(helpMenu);
426 }
427
428 void IntelliPhotoGui::createGui(){
429 // create a central widget to work on
430 centralGuiWidget = new QWidget(this);
431 setCentralWidget(centralGuiWidget);
432
433 // create the grid for the layout
434 mainLayout = new QGridLayout(centralGuiWidget);
435 centralGuiWidget->setLayout(mainLayout);
436
437 // create Gui elements
438 paintingArea = new PaintingArea();
439
440 // set gui elements
441 mainLayout->addWidget(paintingArea);
442 }
443
444 void IntelliPhotoGui::setIntelliStyle(){
445 // Set the title
446 setWindowTitle("IntelliPhoto Prototype");
447 // Set style sheet
448 this->setStyleSheet("background-color:rgb(64,64,64)");
449 this->centralGuiWidget->setStyleSheet("color:rgb(255,255,255)");
450 this->menuBar()->setStyleSheet("color:rgb(255,255,255)");
451 }
452
453 bool IntelliPhotoGui::maybeSave(){
454 // Check for changes since last save
455
456 // TODO insert variable for modified status here to make an save exit message
457 if (false) {
458 QMessageBox::StandardButton ret;
459
460 // Painting is the title of the window
461 // Add text and the buttons
462 ret = QMessageBox::warning(this, tr("Painting"),
463 tr("The image has been modified.\n"
464 "Do you want to save your changes?"),
465 QMessageBox::Save | QMessageBox::Discard
466 | QMessageBox::Cancel);
467
468 // If save button clicked call for file to be saved
469 if (ret == QMessageBox::Save) {
470 return saveFile("png");
471
472 // If cancel do nothing
473 } else if (ret == QMessageBox::Cancel) {
474 return false;
475 }
476 }
477 return true;
478 }
479
480 bool IntelliPhotoGui::saveFile(const QByteArray &fileFormat){
481 // Define path, name and default file type
482 QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
483
484 // Get selected file from dialog
485 // Add the proper file formats and extensions
486 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
487 initialPath,
488 tr("%1 Files (*.%2);;All Files (*)")
489 .arg(QString::fromLatin1(fileFormat.toUpper()))
490 .arg(QString::fromLatin1(fileFormat)), nullptr, QFileDialog::DontUseNativeDialog);
491
492 // If no file do nothing
493 if (fileName.isEmpty()) {
494 return false;
495 } else {
496 // Call for the file to be saved
497 return paintingArea->save(fileName, fileFormat.constData());
498 }
499 }
PaintingArea::createCircleTool
void createCircleTool()
Definition: PaintingArea.cpp:200
PaintingArea::addLayer
int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type=ImageType::Raster_Image)
The addLayer adds a layer to the current project/ painting area.
Definition: PaintingArea.cpp:56
PaintingArea::createRectangleTool
void createRectangleTool()
Definition: PaintingArea.cpp:195
PaintingArea::open
bool open(const QString &fileName)
The open method is used for loading a picture into the current layer.
Definition: PaintingArea.cpp:102
PaintingArea::setLayerToActive
void setLayerToActive(int index)
The setLayerToActive method marks a specific layer as active.
Definition: PaintingArea.cpp:89
PaintingArea::floodFill
void floodFill(int r, int g, int b, int a)
The floodFill method fills a the active layer with a given color.
Definition: PaintingArea.cpp:138
PaintingArea::save
bool save(const QString &fileName, const char *fileFormat)
The save method is used for exporting the current project as one picture.
Definition: PaintingArea.cpp:114
PaintingArea::createPlainTool
void createPlainTool()
Definition: PaintingArea.cpp:185
IntelliPhotoGui::IntelliPhotoGui
IntelliPhotoGui()
The IntelliPhotoGui method is the constructor and is used to create a new instance of the main progra...
Definition: IntelliPhotoGui.cpp:10
PaintingArea
The PaintingArea class manages the methods and stores information about the current painting area,...
Definition: PaintingArea.h:36
PaintingArea::deleteLayer
void deleteLayer(int index)
The deleteLayer method removes a layer at a given index.
Definition: PaintingArea.cpp:73
PaintingArea::createPenTool
void createPenTool()
Definition: PaintingArea.cpp:180
PaintingArea::createLineTool
void createLineTool()
Definition: PaintingArea.cpp:190
PaintingArea::colorPickerSetSecondColor
void colorPickerSetSecondColor()
The colorPickerSetSecondColor calls the QTColorPicker to determine the secondary drawing color.
Definition: PaintingArea.cpp:171
PaintingArea::colorPickerSetFirstColor
void colorPickerSetFirstColor()
The colorPickerSetFirstColor calls the QTColorPicker to determine the primary drawing color.
Definition: PaintingArea.cpp:166
PaintingArea::colorPickerSwitchColor
void colorPickerSwitchColor()
The colorPickerSwitchColor swaps the primary color with the secondary drawing color.
Definition: PaintingArea.cpp:176
IntelliPhotoGui.h
IntelliPhotoGui::closeEvent
void closeEvent(QCloseEvent *event) override
Definition: IntelliPhotoGui.cpp:26
PaintingArea::createPolygonTool
void createPolygonTool()
Definition: PaintingArea.cpp:204
PaintingArea::moveActiveLayer
void moveActiveLayer(int idx)
The moveActiveLayer moves the active layer to a specific position in the layer stack.
Definition: PaintingArea.cpp:152
PaintingArea.h
PaintingArea::createFloodFillTool
void createFloodFillTool()
Definition: PaintingArea.cpp:209
PaintingArea::slotActivateLayer
void slotActivateLayer(int a)
The slotActivateLayer method handles the event of selecting one layer as active.
Definition: PaintingArea.cpp:160
PaintingArea::setAlphaOfLayer
void setAlphaOfLayer(int index, int alpha)
The setAlphaOfLayer method sets the alpha value of a specific layer.
Definition: PaintingArea.cpp:95
PaintingArea::movePositionActive
void movePositionActive(int x, int y)
The movePositionActive method moves the active layer to certain position.
Definition: PaintingArea.cpp:147