-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircuitsWindow.cpp
More file actions
439 lines (409 loc) · 14.6 KB
/
circuitsWindow.cpp
File metadata and controls
439 lines (409 loc) · 14.6 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
435
436
437
438
439
#include <circuitsWindow.h>
CircuitsWindow::CircuitsWindow(QWidget *parent) : QMainWindow(parent) {
widget = new CircuitsWidget();
QPalette pal(palette());
pal.setColor(QPalette::Background, Qt::white);
widget->setAutoFillBackground(true);
widget->setPalette(pal);
setCentralWidget(widget);
createActions();
createMenus();
QString message = tr("A context menu is available by right-click");
statusBar()->showMessage(message);
setWindowTitle("Circuits (Alpha Release) v1.3");
setMinimumSize(160, 160);
resize(780, 520);
saved = true;
}
void CircuitsWindow::keyPressEvent(QKeyEvent *event) {
switch(event->key()){
case(Qt::Key_Left):{
widget->scroll(-10,0);
break;
}
case(Qt::Key_Right):{
widget->scroll(10,0);
break;
}
case(Qt::Key_Up):{
widget->scroll(0,-10);
break;
}
case(Qt::Key_Down):{
widget->scroll(0,10);
break;
}
}
saved = false;
}
void CircuitsWindow::createActions() {
// n.b. QAction.setShortcuts(QKeySequence key) binds automatically key event to the current action
// this is the "new" action
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new program"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
// this is the "open" action
openAct = new QAction(tr("&Open"), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Load an existing program"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
// this is the "save" action
saveAct = new QAction(tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the current program"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
// this is the "exit" action
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
// this is the "undo" action
undoAct = new QAction(tr("&Undo"), this);
undoAct->setShortcuts(QKeySequence::Undo);
undoAct->setStatusTip(tr("Undo the last operation"));
connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
// this is the "redo" action
redoAct = new QAction(tr("&Redo"), this);
redoAct->setShortcuts(QKeySequence::Redo);
redoAct->setStatusTip(tr("Redo the last operation"));
connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
// this is the "copy" action
copyAct = new QAction(tr("&Copy"), this);
copyAct->setShortcuts(QKeySequence::Copy);
copyAct->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
// this is the "paste" action
pasteAct = new QAction(tr("&Paste"), this);
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
// this is the "adder" action
adderAct = new QAction(tr("&Adder"), this);
adderAct->setShortcut(tr("Ctrl+A"));
adderAct->setStatusTip("Draw a draggable adder");
connect(adderAct, SIGNAL(triggered()), this, SLOT(adder()));
// this is the "ander" action
anderAct = new QAction(tr("&AND Port"), this);
anderAct->setShortcut(tr("Ctrl+E"));
anderAct->setStatusTip("Draw a draggable AND port");
connect(anderAct, SIGNAL(triggered()), this, SLOT(ander()));
// this is the "orer" action
orerAct = new QAction(tr("&OR Port"), this);
orerAct->setShortcut(tr("Ctrl+W"));
orerAct->setStatusTip("Draw a draggable OR port");
connect(orerAct, SIGNAL(triggered()), this, SLOT(orer()));
// this is the "noter" action
noterAct = new QAction(tr("&NOT Port"), this);
noterAct->setShortcut(tr("Ctrl+G"));
noterAct->setStatusTip("Draw a draggable NOT port");
connect(noterAct, SIGNAL(triggered()), this, SLOT(noter()));
// this is the "diver" action
diverAct = new QAction(tr("&Divider"), this);
diverAct->setShortcut(tr("Ctrl+J"));
diverAct->setStatusTip("Draw a draggable divider");
connect(diverAct, SIGNAL(triggered()), this, SLOT(diver()));
// this is the "muler" action
mulerAct = new QAction(tr("&Multiplier"), this);
mulerAct->setShortcut(tr("Ctrl+X"));
mulerAct->setStatusTip("Draw a draggable multiplier");
connect(mulerAct, SIGNAL(triggered()), this, SLOT(muler()));
// this is the "variable" action
varAct = new QAction(tr("&Variable"), this);
varAct->setShortcut((tr("Ctrl+K")));
varAct->setStatusTip("Draw a draggable text label");
connect(varAct, SIGNAL(triggered()), this, SLOT(variable()));
// this is the "collector" action
colAct = new QAction(tr("&Collector"), this);
colAct->setShortcut((tr("Ctrl+M")));
colAct->setStatusTip("Draw a draggable collector label");
connect(colAct, SIGNAL(triggered()), this, SLOT(collector()));
// this is the "clear" action
clearAct = new QAction(tr("&Clear all"), this);
clearAct->setShortcut((tr("Ctrl+Y")));
clearAct->setStatusTip("Clear the canvas");
connect(clearAct, SIGNAL(triggered()), this, SLOT(clearAll()));
// this is the "scrollUp" action
scrollUpAct = new QAction(tr("&Scroll up"), this);
scrollUpAct->setStatusTip("Scroll up the view");
connect(scrollUpAct, SIGNAL(triggered()), this, SLOT(scrollUp()));
// this is the "scrollDown" action
scrollDownAct = new QAction(tr("&Scroll down"), this);
scrollDownAct->setStatusTip("Scroll down the view");
connect(scrollDownAct, SIGNAL(triggered()), this, SLOT(scrollDown()));
// this is the "scrollLeft" action
scrollLeftAct = new QAction(tr("&Scroll left"), this);
scrollLeftAct->setStatusTip("Scroll left the view");
connect(scrollLeftAct, SIGNAL(triggered()), this, SLOT(scrollLeft()));
// this is the "scrollRight" action
scrollRightAct = new QAction(tr("&Scroll right"), this);
scrollRightAct->setStatusTip("Scroll right the view");
connect(scrollRightAct, SIGNAL(triggered()), this, SLOT(scrollRight()));
// this is the "zoomIn" action
zoomInAct = new QAction(tr("&Zoom in"), this);
zoomInAct->setStatusTip("Zoom in the view");
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
// this is the "zoomOut" action
zoomOutAct = new QAction(tr("&Zoom out"), this);
zoomOutAct->setStatusTip("Zoom out the view");
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
}
void CircuitsWindow::createMenus() {
// file menu
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
// edit menu
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(clearAct);
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
editMenu->addSeparator();
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
// view menu
viewMenu = menuBar()->addMenu(tr("&View"));
viewMenu->addAction(scrollUpAct);
viewMenu->addAction(scrollDownAct);
viewMenu->addAction(scrollLeftAct);
viewMenu->addAction(scrollRightAct);
viewMenu->addSeparator();
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
// BETA ONLY: These actions will be active on the Beta Release
undoAct->setEnabled(false);
redoAct->setEnabled(false);
copyAct->setEnabled(false);
pasteAct->setEnabled(false);
// END BETA ONLY
// math menu
mathMenu = menuBar()->addMenu(tr("&Mathematic"));
mathMenu->addAction(adderAct);
mathMenu->addAction(diverAct);
mathMenu->addAction(mulerAct);
// logic menu
logicMenu = menuBar()->addMenu(tr("&Logic"));
logicMenu->addAction(anderAct);
logicMenu->addAction(orerAct);
logicMenu->addAction(noterAct);
// variables menu
varMenu = menuBar()->addMenu(tr("&Variables"));
varMenu->addAction(varAct);
varMenu->addAction(colAct);
}
void CircuitsWindow::contextMenuEvent(QContextMenuEvent *event) {
QMenu menu(this);
menu.addAction(copyAct);
menu.addAction(pasteAct);
menu.exec(event->globalPos());
}
void CircuitsWindow::newFile(){
statusBar()->showMessage("Creating new program");
clearAll();
statusBar()->showMessage("Done");
}
void CircuitsWindow::open(){
statusBar()->showMessage("Opening existing program");
QString fileName = QFileDialog::getOpenFileName(this, tr("Load program"), "", "");
widget->deserialize(fileName);
statusBar()->showMessage("Program opened");
saved = true;
}
void CircuitsWindow::save(){
statusBar()->showMessage("Saving current program");
QString fileName = QFileDialog::getSaveFileName(this, tr("Save program"), "", "");
widget->serialize(fileName);
statusBar()->showMessage("Program saved");
saved = true;
}
// These are for the beta release
void CircuitsWindow::undo(){
statusBar()->showMessage("Undoing last action");
saved = false;
}
void CircuitsWindow::redo(){
statusBar()->showMessage("Redoing last action");
saved = false;
}
void CircuitsWindow::copy(){
statusBar()->showMessage("Copying");
saved = false;
}
void CircuitsWindow::paste(){
statusBar()->showMessage("Pasting");
saved = false;
}
// end beta
void CircuitsWindow::adder() {
statusBar()->showMessage("Drawing an adder");
Adder *a = new Adder(widget);
widget->drawComponent(a);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::orer() {
statusBar()->showMessage("Drawing an OR port");
Orer *o = new Orer(widget);
widget->drawComponent(o);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::ander() {
statusBar()->showMessage("Drawing an AND port");
Ander *a = new Ander(widget);
widget->drawComponent(a);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::noter() {
statusBar()->showMessage("Drawing a NOT port");
Noter *n = new Noter(widget);
widget->drawComponent(n);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::diver() {
statusBar()->showMessage("Drawing a divider");
Diver *d = new Diver(widget);
widget->drawComponent(d);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::muler() {
statusBar()->showMessage("Drawing a multiplier");
Muler *m = new Muler(widget);
widget->drawComponent(m);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::variable() {
statusBar()->showMessage("Drawing a text label");
Variable *v = new Variable(widget);
widget->drawComponent(v);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::collector() {
statusBar()->showMessage("Drawing a collector");
Collector *c = new Collector(widget);
widget->drawComponent(c);
statusBar()->showMessage("Done");
saved = false;
}
void CircuitsWindow::clearAll() {
statusBar()->showMessage("Clearing canvas");
if(!saved){
QMessageBox msgBox;
msgBox.setText("The program has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:{
// Save was clicked
QString fileName = QFileDialog::getSaveFileName(this, tr("Save program"), "", "");
widget->serialize(fileName);
widget->clearAll();
break;
}
case QMessageBox::Discard:{
// Don't Save was clicked
widget->clearAll();
break;
}
case QMessageBox::Cancel:{
// Cancel was clicked, do nothing
break;
}
default:{
// should never be reached
break;
}
}
}
else {
widget->clearAll();
}
statusBar()->showMessage("Done");
}
void CircuitsWindow::closeEvent(QCloseEvent *event) {
if(!saved){
QMessageBox msgBox;
msgBox.setText("The program has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:{
// Save was clicked
QString fileName = QFileDialog::getSaveFileName(this, tr("Save program"), "", "");
widget->serialize(fileName);
widget->clearAll();
//close();
break;
}
case QMessageBox::Discard:{
// Don't Save was clicked
widget->clearAll();
//close();
break;
}
case QMessageBox::Cancel:{
// Ignore the event
event->ignore();
break;
}
default:{
// should never be reached
break;
}
}
}
}
void CircuitsWindow::scrollUp() {
widget->scroll(0,-10);
saved = false;
}
void CircuitsWindow::scrollDown() {
widget->scroll(0,10);
saved = false;
}
void CircuitsWindow::scrollLeft() {
widget->scroll(-10,0);
saved = false;
}
void CircuitsWindow::scrollRight() {
widget->scroll(10,0);
saved = false;
}
void CircuitsWindow::zoomIn() {
int incPx = 1;
for(int i = 0; i < widget->componentList.size(); i++){
int w = widget->componentList[i]->width() + incPx;
int h = widget->componentList[i]->height() + incPx;
widget->componentList[i]->resize(w,h);
if(widget->componentList[i]->getComponentType()==3){
Variable *v = (Variable*) widget->componentList[i].data();
v->var->resize(v->var->width() + incPx, v->var->height() + incPx);
}
}
saved = false;
}
void CircuitsWindow::zoomOut() {
int incPx = 1;
for(int i = 0; i < widget->componentList.size(); i++){
int w = widget->componentList[i]->width() + incPx;
int h = widget->componentList[i]->height() + incPx;
widget->componentList[i]->resize(w,h);
if(widget->componentList[i]->getComponentType()==3){
Variable *v = (Variable*) widget->componentList[i].data();
v->var->resize(v->var->width() - incPx, v->var->height() + incPx);
}
}
saved = false;
}