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  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 // Open an about dialog
226 void IntelliPhotoGui::slotAboutDialog(){
227  // Window title and text to display
228  QMessageBox::about(this, tr("About Painting"),
229  tr("<p><b>IntelliPhoto</b>Pretty basic editor.</p>"));
230 }
231 
232 // Define menu actions that call functions
233 void IntelliPhotoGui::createActions(){
234  // Get a list of the supported file formats
235  // QImageWriter is used to write images to files
236  foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
237  QString text = tr("%1...").arg(QString(format).toUpper());
238 
239  // Create an action for each file format
240  QAction*action = new QAction(text, this);
241 
242  // Set an action for each file format
243  action->setData(format);
244 
245  // When clicked call IntelliPhotoGui::save()
246  connect(action, SIGNAL(triggered()), this, SLOT(slotSave()));
247 
248  // Attach each file format option menu item to Save As
249  actionSaveAs.append(action);
250  }
251 
252  //set exporter to actions
253  QAction*pngSaveAction = new QAction("PNG-8", this);
254  pngSaveAction->setData("PNG");
255  // When clicked call IntelliPhotoGui::save()
256  connect(pngSaveAction, SIGNAL(triggered()), this, SLOT(slotSave()));
257  // Attach each PNG in save Menu
258  actionSaveAs.append(pngSaveAction);
259 
260  // Create exit action and tie to IntelliPhotoGui::close()
261  actionExit = new QAction(tr("&Exit"), this);
262  actionExit->setShortcuts(QKeySequence::Quit);
263  connect(actionExit, SIGNAL(triggered()), this, SLOT(close()));
264 
265  actionOpen = new QAction(tr("&Open"), this);
266  actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
267  connect(actionOpen, SIGNAL(triggered()), this, SLOT(slotOpen()));
268 
269  // Create New Layer action and tie to IntelliPhotoGui::newLayer()
270  actionCreateNewLayer = new QAction(tr("&New Layer..."), this);
271  actionCreateNewLayer->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
272  connect(actionCreateNewLayer, SIGNAL(triggered()), this, SLOT(slotCreateNewLayer()));
273 
274  // Delete New Layer action and tie to IntelliPhotoGui::deleteLayer()
275  actionDeleteLayer = new QAction(tr("&Delete Layer..."), this);
276  connect(actionDeleteLayer, SIGNAL(triggered()), this, SLOT(slotDeleteLayer()));
277 
278  actionSetActiveLayer = new QAction(tr("&set Active"), this);
279  connect(actionSetActiveLayer, SIGNAL(triggered()), this, SLOT(slotSetActiveLayer()));
280 
281  actionSetActiveAlpha = new QAction(tr("&set Alpha"), this);
282  connect(actionSetActiveAlpha, SIGNAL(triggered()), this, SLOT(slotSetActiveAlpha()));
283 
284  actionMovePositionUp = new QAction(tr("&move Up"), this);
285  actionMovePositionUp->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
286  connect(actionMovePositionUp, SIGNAL(triggered()), this, SLOT(slotPositionMoveUp()));
287 
288  actionMovePositionDown = new QAction(tr("&move Down"), this);
289  actionMovePositionDown->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
290  connect(actionMovePositionDown, SIGNAL(triggered()), this, SLOT(slotPositionMoveDown()));
291 
292  actionMovePositionLeft = new QAction(tr("&move Left"), this);
293  actionMovePositionLeft->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
294  connect(actionMovePositionLeft, SIGNAL(triggered()), this, SLOT(slotPositionMoveLeft()));
295 
296  actionMovePositionRight = new QAction(tr("&move Right"), this);
297  actionMovePositionRight->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
298  connect(actionMovePositionRight, SIGNAL(triggered()), this, SLOT(slotPositionMoveRight()));
299 
300  actionMoveLayerUp = new QAction(tr("&move Layer Up"), this);
301  actionMoveLayerUp->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Up));
302  connect(actionMoveLayerUp, SIGNAL(triggered()), this, SLOT(slotMoveLayerUp()));
303 
304  actionMoveLayerDown= new QAction(tr("&move Layer Down"), this);
305  actionMoveLayerDown->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Down));
306  connect(actionMoveLayerDown, SIGNAL(triggered()), this, SLOT(slotMoveLayerDown()));
307 
308  //Create Color Actions here
309  actionColorPickerFirstColor = new QAction(tr("&Main"), this);
310  connect(actionColorPickerFirstColor, SIGNAL(triggered()), this, SLOT(slotSetFirstColor()));
311 
312  actionColorPickerSecondColor = new QAction(tr("&Secondary"), this);
313  connect(actionColorPickerSecondColor, SIGNAL(triggered()), this, SLOT(slotSetSecondColor()));
314 
315  actionColorSwitch = new QAction(tr("&Switch"), this);
316  actionColorSwitch->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
317  connect(actionColorSwitch, SIGNAL(triggered()), this, SLOT(slotSwitchColor()));
318 
319  //Create Tool actions down here
320  actionCreatePlainTool = new QAction(tr("&Plain"), this);
321  connect(actionCreatePlainTool, SIGNAL(triggered()), this, SLOT(slotCreatePlainTool()));
322 
323  actionCreatePenTool = new QAction(tr("&Pen"),this);
324  connect(actionCreatePenTool, SIGNAL(triggered()), this, SLOT(slotCreatePenTool()));
325 
326  actionCreateLineTool = new QAction(tr("&Line"), this);
327  connect(actionCreateLineTool, SIGNAL(triggered()), this, SLOT(slotCreateLineTool()));
328 
329  // Create about action and tie to IntelliPhotoGui::about()
330  actionAboutDialog = new QAction(tr("&About"), this);
331  connect(actionAboutDialog, SIGNAL(triggered()), this, SLOT(slotAboutDialog()));
332 
333  // Create about Qt action and tie to IntelliPhotoGui::aboutQt()
334  actionAboutQtDialog = new QAction(tr("About &Qt"), this);
335  connect(actionAboutQtDialog, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
336 }
337 
338 // Create the menubar
339 void IntelliPhotoGui::createMenus(){
340  // Create Save As option and the list of file types
341  saveAsMenu = new QMenu(tr("&Save As"), this);
342  foreach (QAction *action, actionSaveAs)
343  saveAsMenu->addAction(action);
344 
345 
346  // Attach all actions to File
347  fileMenu = new QMenu(tr("&File"), this);
348  fileMenu->addAction(actionOpen);
349  fileMenu->addMenu(saveAsMenu);
350  fileMenu->addSeparator();
351  fileMenu->addAction(actionExit);
352 
353  // Attach all actions to Options
354  optionMenu = new QMenu(tr("&Options"), this);
355  optionMenu->addAction(actionSetActiveLayer);
356  optionMenu->addAction(actionSetActiveAlpha);
357  optionMenu->addAction(actionMovePositionUp);
358  optionMenu->addAction(actionMovePositionDown);
359  optionMenu->addAction(actionMovePositionLeft);
360  optionMenu->addAction(actionMovePositionRight);
361  optionMenu->addAction(actionMoveLayerUp);
362  optionMenu->addAction(actionMoveLayerDown);
363 
364  // Attach all actions to Layer
365  layerMenu = new QMenu(tr("&Layer"), this);
366  layerMenu->addAction(actionCreateNewLayer);
367  layerMenu->addAction(actionDeleteLayer);
368 
369  //Attach all Color Options
370  colorMenu = new QMenu(tr("&Color"), this);
371  colorMenu->addAction(actionColorPickerFirstColor);
372  colorMenu->addAction(actionColorPickerSecondColor);
373  colorMenu->addAction(actionColorSwitch);
374 
375  //Attach all Tool Options
376  toolMenu = new QMenu(tr("&Tools"), this);
377  toolMenu->addAction(actionCreatePenTool);
378  toolMenu->addAction(actionCreatePlainTool);
379  toolMenu->addAction(actionCreateLineTool);
380  toolMenu->addSeparator();
381  toolMenu->addMenu(colorMenu);
382 
383  // Attach all actions to Help
384  helpMenu = new QMenu(tr("&Help"), this);
385  helpMenu->addAction(actionAboutDialog);
386  helpMenu->addAction(actionAboutQtDialog);
387 
388  // Add menu items to the menubar
389  menuBar()->addMenu(fileMenu);
390  menuBar()->addMenu(optionMenu);
391  menuBar()->addMenu(layerMenu);
392  menuBar()->addMenu(toolMenu);
393  menuBar()->addMenu(helpMenu);
394 }
395 
396 void IntelliPhotoGui::createGui(){
397  // create a central widget to work on
398  centralGuiWidget = new QWidget(this);
399  setCentralWidget(centralGuiWidget);
400 
401  // create the grid for the layout
402  mainLayout = new QGridLayout(centralGuiWidget);
403  centralGuiWidget->setLayout(mainLayout);
404 
405  // create Gui elements
406  paintingArea = new PaintingArea();
407 
408  // set gui elements
409  mainLayout->addWidget(paintingArea);
410 }
411 
412 void IntelliPhotoGui::setIntelliStyle(){
413  // Set the title
414  setWindowTitle("IntelliPhoto Prototype");
415  // Set style sheet
416  this->setStyleSheet("background-color:rgb(64,64,64)");
417  this->centralGuiWidget->setStyleSheet("color:rgb(255,255,255)");
418  this->menuBar()->setStyleSheet("color:rgb(255,255,255)");
419 }
420 
421 bool IntelliPhotoGui::maybeSave(){
422  // Check for changes since last save
423 
424  // TODO insert variable for modified status here to make an save exit message
425  if (false) {
426  QMessageBox::StandardButton ret;
427 
428  // Painting is the title of the window
429  // Add text and the buttons
430  ret = QMessageBox::warning(this, tr("Painting"),
431  tr("The image has been modified.\n"
432  "Do you want to save your changes?"),
433  QMessageBox::Save | QMessageBox::Discard
434  | QMessageBox::Cancel);
435 
436  // If save button clicked call for file to be saved
437  if (ret == QMessageBox::Save) {
438  return saveFile("png");
439 
440  // If cancel do nothing
441  } else if (ret == QMessageBox::Cancel) {
442  return false;
443  }
444  }
445  return true;
446 }
447 
448 bool IntelliPhotoGui::saveFile(const QByteArray &fileFormat){
449  // Define path, name and default file type
450  QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
451 
452  // Get selected file from dialog
453  // Add the proper file formats and extensions
454  QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
455  initialPath,
456  tr("%1 Files (*.%2);;All Files (*)")
457  .arg(QString::fromLatin1(fileFormat.toUpper()))
458  .arg(QString::fromLatin1(fileFormat)), nullptr, QFileDialog::DontUseNativeDialog);
459 
460  // If no file do nothing
461  if (fileName.isEmpty()) {
462  return false;
463  } else {
464  // Call for the file to be saved
465  return paintingArea->save(fileName, fileFormat.constData());
466  }
467 }
PaintingArea::addLayer
int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type=ImageType::Raster_Image)
Definition: PaintingArea.cpp:58
PaintingArea::open
bool open(const QString &fileName)
Definition: PaintingArea.cpp:104
PaintingArea::setLayerToActive
void setLayerToActive(int index)
Definition: PaintingArea.cpp:91
PaintingArea::floodFill
void floodFill(int r, int g, int b, int a)
Definition: PaintingArea.cpp:140
PaintingArea::save
bool save(const QString &fileName, const char *fileFormat)
Definition: PaintingArea.cpp:116
PaintingArea::createPlainTool
void createPlainTool()
Definition: PaintingArea.cpp:187
IntelliPhotoGui::IntelliPhotoGui
IntelliPhotoGui()
Definition: IntelliPhotoGui.cpp:10
PaintingArea
Definition: PaintingArea.h:25
PaintingArea::deleteLayer
void deleteLayer(int index)
Definition: PaintingArea.cpp:75
PaintingArea::createPenTool
void createPenTool()
Definition: PaintingArea.cpp:182
PaintingArea::createLineTool
void createLineTool()
Definition: PaintingArea.cpp:192
PaintingArea::colorPickerSetSecondColor
void colorPickerSetSecondColor()
Definition: PaintingArea.cpp:173
PaintingArea::colorPickerSetFirstColor
void colorPickerSetFirstColor()
Definition: PaintingArea.cpp:168
PaintingArea::colorPickerSwitchColor
void colorPickerSwitchColor()
Definition: PaintingArea.cpp:178
IntelliPhotoGui.h
IntelliPhotoGui::closeEvent
void closeEvent(QCloseEvent *event) override
Definition: IntelliPhotoGui.cpp:26
PaintingArea::moveActiveLayer
void moveActiveLayer(int idx)
Definition: PaintingArea.cpp:154
PaintingArea.h
PaintingArea::slotActivateLayer
void slotActivateLayer(int a)
Definition: PaintingArea.cpp:162
PaintingArea::setAlphaOfLayer
void setAlphaOfLayer(int index, int alpha)
Definition: PaintingArea.cpp:97
PaintingArea::movePositionActive
void movePositionActive(int x, int y)
Definition: PaintingArea.cpp:149