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