-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
188 lines (149 loc) · 5.65 KB
/
Copy pathmainwindow.cpp
File metadata and controls
188 lines (149 loc) · 5.65 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_scribblearea = new ScribbleArea(this);
setCentralWidget(m_scribblearea);
// createActions();
// createMenus();
setWindowTitle(tr("Scribble"));
m_subMenu = new SubMenu(*m_scribblearea);
resize(500, 500);
}
MainWindow::~MainWindow()
{
delete ui;
delete m_subMenu;
}
void MainWindow::keyPressEvent(QKeyEvent *event) {
Q_UNUSED(event);
}
void MainWindow::closeEvent(QCloseEvent *event) {
if(maybeSave()) {
event->accept();
} else {
event->ignore();
}
}
void MainWindow::open() {
if(maybeSave()) {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if(!fileName.isEmpty()) {
m_scribblearea->openImage(fileName);
}
}
}
void MainWindow::save() {
QAction *action = qobject_cast<QAction *>(sender());
QByteArray fileFormat = action->data().toByteArray();
saveFile(fileFormat);
}
void MainWindow::penColor() {
QColor newColor = QColorDialog::getColor(m_scribblearea->penColor());
if(newColor.isValid()) {
m_scribblearea->setPenColor(newColor);
}
}
void MainWindow::fillEasel() {
m_scribblearea->setEasel(m_scribblearea->penColor());
}
void MainWindow::penWidth() {
bool ok;
int newWidth = QInputDialog::getInt(this, tr("Scribble"),
tr("Select pen width : "),
m_scribblearea->penWidth(),
1, 50, 1, &ok);
if(ok) {
m_scribblearea->setPenWidth(newWidth);
}
}
void MainWindow::about() {
QMessageBox::about(this, tr("About Scribble"),
tr("<p>The <b>Scribble</b> example is awesome</p>"));
}
void MainWindow::createActions() {
m_openAction = new QAction(tr("&Open"), this);
m_openAction->setShortcuts(QKeySequence::Open);
connect(m_openAction, SIGNAL(triggered(bool)), this, SLOT(open()));
foreach(QByteArray format, QImageWriter::supportedImageFormats()) {
QString text = tr("%1...").arg(QString(format).toUpper());
QAction *action = new QAction(text, this);
action->setData(format);
connect(action, SIGNAL(triggered()), this, SLOT(save()));
m_saveAsActs.append(action);
}
m_printAct = new QAction(tr("&Print..."), this);
connect(m_printAct, SIGNAL(triggered()), m_scribblearea, SLOT(print()));
m_exitAction = new QAction(tr("&Exit"), this);
m_exitAction->setShortcuts(QKeySequence::Quit);
connect(m_exitAction, SIGNAL(triggered()), this, SLOT(close()));
m_penColorAct = new QAction(tr("&Pen color..."), this);
connect(m_penColorAct, SIGNAL(triggered()), this, SLOT(penColor()));
m_penWidthAct = new QAction(tr("Pen &Width..."), this);
connect(m_penColorAct, SIGNAL(triggered()), this, SLOT(penWidth()));
m_clearScreenAct = new QAction(tr("&Clear Screen..."), this);
m_clearScreenAct->setShortcut(tr("Ctrl+L"));
connect(m_clearScreenAct, SIGNAL(triggered()), m_scribblearea, SLOT(clearImage()));
m_aboutAct = new QAction(tr("&About..."), this);
connect(m_aboutAct, SIGNAL(triggered()), m_scribblearea, SLOT(about()));
m_aboutQtAct = new QAction(tr("About &Qt..."), this);
connect(m_aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus() {
m_saveAsMenu = new QMenu(tr("&Save As"), this);
foreach(QAction *action, m_saveAsActs) {
m_saveAsMenu->addAction(action);
}
m_fileMenu = new QMenu(tr("&File"), this);
m_fileMenu->addAction(m_openAct);
m_fileMenu->addMenu(m_saveAsMenu);
m_fileMenu->addAction(m_printAct);
m_fileMenu->addSeparator();
m_fileMenu->addAction(m_exitAction);
m_optionMenu = new QMenu(tr("&Options"), this);
m_optionMenu->addAction(m_penColorAct);
m_optionMenu->addAction(m_penWidthAct);
m_optionMenu->addSeparator();
m_optionMenu->addAction(m_clearScreenAct);
m_helpMenu = new QMenu(tr("&Help"), this);
m_helpMenu->addAction(m_aboutAct);
m_helpMenu->addAction(m_aboutQtAct);
menuBar()->addMenu(m_fileMenu);
menuBar()->addMenu(m_optionMenu);
menuBar()->addMenu(m_helpMenu);
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
return true;
}
bool MainWindow::maybeSave() {
if(m_scribblearea->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Scribble"),
tr("The image has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if(ret == QMessageBox::Save) {
return saveFile("png");
} else if(ret == QMessageBox::Cancel) {
return false;
}
}
return true;
}
bool MainWindow::saveFile(const QByteArray &fileFormat) {
QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
initialPath,
tr("%1 Files (*.%2);; All Files(*)")
.arg(QString::fromLatin1(fileFormat.toUpper()))
.arg(QString::fromLatin1(fileFormat)));
if(fileName.isEmpty()) {
return false;
} else {
return m_scribblearea->saveImage(fileName, fileFormat.constData());
}
}