-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_window.cpp
More file actions
434 lines (372 loc) · 12.9 KB
/
main_window.cpp
File metadata and controls
434 lines (372 loc) · 12.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <iostream>
using namespace std;
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QFileDialog>
#include <QColorDialog>
#include <QSignalMapper>
#include <QMenuBar>
#include <QMenu>
#include "main_window.h"
#include "commands.h"
#include "draw_area.h"
/**
* @brief MainWindow::MainWindow - the main window, parent to every other
* widget.
*/
MainWindow::MainWindow(QWidget* parent, const char* name)
:QMainWindow(parent)
{
// create the DrawArea, which will receive the draw mouse events
drawArea = new DrawArea(this);
drawArea->setStyleSheet("background-color:transparent");
// get default tool
currentTool = drawArea->getCurrentTool();
// create the menu and toolbar
createMenuAndToolBar();
// init dialog pointers to 0
penDialog = 0;
lineDialog = 0;
eraserDialog = 0;
rectDialog = 0;
// adjust window size, name, & stop context menu
setWindowTitle(name);
resize(QDesktopWidget().availableGeometry(this).size()*.6);
setContextMenuPolicy(Qt::PreventContextMenu);
setCentralWidget(drawArea);
}
MainWindow::~MainWindow()
{
imageActions.clear();
toolActions.clear();
}
/**
* @brief MainWindow::mousePressEvent - On mouse right click, open dialog menu.
*
*/
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton) {
openToolDialog();
}
}
/**
* @brief MainWindow::OnNewImage - Open a NewCanvasDialogue prompting user to
* enterthe dimensions for a new image.
*/
void MainWindow::OnNewImage()
{
CanvasSizeDialog* newCanvas = new CanvasSizeDialog(this, "New Canvas");
newCanvas->exec();
// if user hit 'OK' button, create new image
if (newCanvas->result())
{
QSize size = QSize(newCanvas->getWidthValue(),
newCanvas->getHeightValue());
drawArea->createNewImage(size);
}
// done with the dialog, free it
delete newCanvas;
}
/**
* @brief MainWindow::OnLoadImage - Open a QFileDialogue prompting user to
* browse for a file to open.
*/
void MainWindow::OnLoadImage()
{
QString s = QFileDialog::getOpenFileName(this, tr("Open File"),
".",
tr("BMP image (*.bmp)"));
if (! s.isNull())
{
drawArea->loadImage(s);
}
}
/**
* @brief MainWindow::OnSaveImage - Open a QFileDialogue prompting user to
* enter a filename and save location.
*/
void MainWindow::OnSaveImage()
{
if(drawArea->getImage()->isNull())
return;
// use custom dialog settings for appending suffixes
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setAcceptMode(QFileDialog::AcceptSave);
fileDialog->setDirectory(".");
fileDialog->setNameFilter("BMP image (*.bmp)");
fileDialog->setDefaultSuffix("bmp");
fileDialog->exec();
// if user hit 'OK' button, save file
if (fileDialog->result())
{
QString s = fileDialog->selectedFiles().first();
if (! s.isNull())
{
drawArea->saveImage(s);
}
}
// done with the dialog, free it
delete fileDialog;
}
/**
* @brief MainWindow::OnResizeImage - Change the dimensions of the image.
*
*/
void MainWindow::OnResizeImage()
{
QPixmap *image = drawArea->getImage();
if(image->isNull())
return;
CanvasSizeDialog* newCanvas = new CanvasSizeDialog(this, "Resize Image",
image->width(),
image->height());
newCanvas->exec();
// if user hit 'OK' button, create new image
if (newCanvas->result())
{
drawArea->resizeImage(QSize(newCanvas->getWidthValue(),
newCanvas->getHeightValue()));
}
// done with the dialog, free it
delete newCanvas;
}
/**
* @brief MainWindow::OnPickColor - Open a QColorDialog prompting the user to
* select a color.
*
*/
void MainWindow::OnPickColor(int which)
{
QColorDialog* colorDialog = new QColorDialog(this);
QColor foregroundColor = drawArea->getForegroundColor();
QColor backgroundColor = drawArea->getBackgroundColor();
QColor color = colorDialog->getColor(which == foreground ? foregroundColor
: backgroundColor,
this,
which == foreground ? "Foreground Color"
: "Background Color",
QColorDialog::DontUseNativeDialog);
// if user hit 'OK' button, change the color
if (color.isValid())
drawArea->updateColorConfig(color, which);
// done with the dialog, free it
delete colorDialog;
}
/**
* @brief MainWindow::OnChangeTool - Sets the current tool based on argument.
*
*/
void MainWindow::OnChangeTool(int newTool)
{
currentTool = drawArea->setCurrentTool(newTool); // notify observer
}
/**
* @brief MainWindow::OnPenDialog - Open a PenDialog prompting the user to
* change pen settings.
*
*/
void MainWindow::OnPenDialog()
{
if(!penDialog)
penDialog = new PenDialog(this, drawArea);
if(penDialog && penDialog->isVisible())
return;
penDialog->show();
}
/**
* @brief MainWindow::OnLineDialog - Open a LineDialog prompting the user to
* change line tool settings.
*
*/
void MainWindow::OnLineDialog()
{
if(!lineDialog)
lineDialog = new LineDialog(this, drawArea);
if(lineDialog && lineDialog->isVisible())
return;
lineDialog->show();
}
/**
* @brief MainWindow::OnEraserDialog - Open a EraserDialog prompting the user
* to change eraser settings.
*
*/
void MainWindow::OnEraserDialog()
{
if (!eraserDialog)
eraserDialog = new EraserDialog(this, drawArea);
if(eraserDialog->isVisible())
return;
eraserDialog->show();
}
/**
* @brief MainWindow::OnRectangleDialog - Open a RectDialog prompting the user
* to change rect tool settings.
*
*/
void MainWindow::OnRectangleDialog()
{
if (!rectDialog)
rectDialog = new RectDialog(this, drawArea);
if(rectDialog->isVisible())
return;
rectDialog->show();
}
/**
* @brief MainWindow::openToolDialog - call the appropriate dialog function
* based on the current tool.
*
*/
void MainWindow::openToolDialog()
{
switch(currentTool->getType())
{
case pen: OnPenDialog(); break;
case line: OnLineDialog(); break;
case eraser: OnEraserDialog(); break;
case rect_tool: OnRectangleDialog(); break;
}
}
/**
* @brief ToolBar::createMenuAndToolBar() - ensure that everything gets
* created in the correct order
*
*/
void MainWindow::createMenuAndToolBar()
{
// create actions and add them to the menu
createMenuActions();
// create the toolbar
toolbar = new ToolBar(this, imageActions, toolActions);
addToolBar(toolbar);
// add a toolbar toggle action to the menu
createToolBarToggle();
}
/**
* @brief MainWindow::createMenu - create the actions that appear in the menu
*
*/
void MainWindow::createMenuActions()
{
// load button icons from files
QIcon newIcon(":/icons/newIcon");
QIcon openIcon(":/icons/openIcon");
QIcon saveIcon(":/icons/saveIcon");
QIcon undoIcon(":/icons/undoIcon");
QIcon redoIcon(":/icons/redoIcon");
QIcon clearIcon(":/icons/clearAllIcon");
QIcon resizeIcon(":/icons/resizeIcon");
QIcon fColorIcon(":/icons/fColorIcon");
QIcon bColorIcon(":/icons/bColorIcon");
QIcon penIcon(":/icons/penIcon");
QIcon lineIcon(":/icons/lineIcon");
QIcon eraserIcon(":/icons/eraserIcon");
QIcon rectIcon(":/icons/rectIcon");
// File
QMenu* file = new QMenu(tr("File"), this);
QAction* newAction = file->addAction(newIcon, tr("New image..."),
this, SLOT(OnNewImage()), tr("Ctrl+N"));
QAction* openAction = file->addAction(openIcon, tr("Load image..."),
this, SLOT(OnLoadImage()), tr("Ctrl+O"));
QAction* saveAction = file->addAction(saveIcon, tr("Save image..."),
this, SLOT(OnSaveImage()), tr("Ctrl+S"));
file->addAction("Quit", this, SLOT(close()), tr("Ctrl+Q"));
// Edit
QMenu* edit = new QMenu(tr("Edit"), this);
QAction* undoAction = edit->addAction(undoIcon, tr("Undo"),
drawArea, SLOT(OnUndo()), tr("Ctrl+Z"));
QAction* redoAction = edit->addAction(redoIcon, tr("Redo"),
drawArea, SLOT(OnRedo()), tr("Ctrl+Y"));
QAction* clearAction = edit->addAction(clearIcon, tr("Clear Canvas"),
drawArea, SLOT(OnClearAll()), tr("Ctrl+C"));
QAction* resizeAction = edit->addAction(resizeIcon, tr("Resize Image..."),
this, SLOT(OnResizeImage()), tr("Ctrl+R"));
// color pickers (still under >Edit)
QSignalMapper *signalMapper = new QSignalMapper(this);
QAction* fColorAction = new QAction(fColorIcon, tr("Foreground Color..."), this);
connect(fColorAction, SIGNAL(triggered()),
signalMapper, SLOT(map()));
fColorAction->setShortcut(tr("Ctrl+F"));
QAction* bColorAction = new QAction(bColorIcon, tr("Background Color..."), this);
connect(bColorAction, SIGNAL(triggered()),
signalMapper, SLOT(map()));
bColorAction->setShortcut(tr("Ctrl+B"));
signalMapper->setMapping(fColorAction, foreground);
signalMapper->setMapping(bColorAction, background);
connect(signalMapper, SIGNAL(mapped(int)),
this, SLOT(OnPickColor(int)));
edit->addAction(fColorAction);
edit->addAction(bColorAction);
// Tool pickers
QSignalMapper *signalMapperT = new QSignalMapper(this);
QAction* penAction = new QAction(penIcon, tr("Pen Tool"), this);
connect(penAction, SIGNAL(triggered()),
signalMapperT, SLOT(map()));
penAction->setShortcut(tr("W"));
QAction* lineAction = new QAction(lineIcon, tr("Line Tool"), this);
connect(lineAction, SIGNAL(triggered()),
signalMapperT, SLOT(map()));
lineAction->setShortcut(tr("F"));
QAction* eraserAction = new QAction(eraserIcon, tr("Eraser"), this);
connect(eraserAction, SIGNAL(triggered()),
signalMapperT, SLOT(map()));
eraserAction->setShortcut(tr("E"));
QAction* rectAction = new QAction(rectIcon, tr("Rectangle Tool"), this);
connect(rectAction, SIGNAL(triggered()),
signalMapperT, SLOT(map()));
rectAction->setShortcut(tr("R"));
signalMapperT->setMapping(penAction, pen);
signalMapperT->setMapping(lineAction, line);
signalMapperT->setMapping(eraserAction, eraser);
signalMapperT->setMapping(rectAction, rect_tool);
connect(signalMapperT, SIGNAL(mapped(int)), this, SLOT(OnChangeTool(int)));
// Tool dialogs
QMenu* tools = new QMenu(tr("Tools"), this);
tools->addAction(penAction);
tools->addAction(lineAction);
tools->addAction(eraserAction);
tools->addAction(rectAction);
tools->addAction(tr("Pen Properties..."),
this, SLOT(OnPenDialog()));
tools->addAction(tr("Line Properties..."),
this, SLOT(OnLineDialog()));
tools->addAction(tr("Eraser Properties..."),
this, SLOT(OnEraserDialog()));
tools->addAction(tr("Rectangle Properties..."),
this, SLOT(OnRectangleDialog()));
// store the actions in QLists for convenience
imageActions.append(newAction);
imageActions.append(openAction);
imageActions.append(saveAction);
imageActions.append(undoAction);
imageActions.append(redoAction);
imageActions.append(clearAction);
imageActions.append(resizeAction);
imageActions.append(fColorAction);
imageActions.append(bColorAction);
toolActions.append(penAction);
toolActions.append(lineAction);
toolActions.append(eraserAction);
toolActions.append(rectAction);
// populate the menubar with menu items
menuBar()->addMenu(file);
menuBar()->addMenu(edit);
menuBar()->addMenu(tools);
menuBar()->setNativeMenuBar(false);
}
/**
* @brief ToolBar::createBarToggle - Create the toggle toolbar action and add
* it to the menu under "View"
*
*/
void MainWindow::createToolBarToggle()
{
// View
QMenu* view = new QMenu(tr("View"), this);
QAction *toggleToolbar = toolbar->toggleViewAction();
toggleToolbar->setText(tr("Show &Toolbar"));
toggleToolbar->setShortcut(tr("Ctrl+T"));
view->addAction(toggleToolbar);
menuBar()->addMenu(view);
}