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