-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
638 lines (547 loc) · 19 KB
/
Copy pathmainwindow.cpp
File metadata and controls
638 lines (547 loc) · 19 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include <QDebug>
#include <QDir>
#include <QStringList>
#include <QFileDialog>
#include <QMessageBox>
#include <QCloseEvent>
#include "mainwindow.h"
#include "configdialog.h"
#include "randdialog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
initUi();
}
MainWindow::~MainWindow()
{
}
void MainWindow::initUi()
{
// mainwindow
this->setWindowTitle(QString("%1 v%2").arg(APP).arg(VER));
this->setObjectName("MainWindow");
QIcon icon;
icon.addFile(tr(":/images/mouse.png"), QSize(), QIcon::Normal, QIcon::Off);
this->setWindowIcon(icon);
this->resize(800, 600);
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
this->setSizePolicy(sizePolicy);
// actions
toolGroup = new QActionGroup(this);
actionCursor = new QAction(this);
actionCursor->setText("Cursor");
actionCursor->setObjectName("actionCursor");
actionCursor->setShortcut(QKeySequence(Qt::Key_Escape));
QIcon icon0;
icon0.addFile(tr(":/images/cursor.png"), QSize(), QIcon::Normal, QIcon::Off);
actionCursor->setIcon(icon0);
actionCursor->setCheckable(true);
actionCursor->setChecked(true);
toolGroup->addAction(actionCursor);
actionEraser = new QAction(this);
actionEraser->setText("Eraser");
actionEraser->setObjectName("actionEraser");
actionEraser->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0));
QIcon icon1;
icon1.addFile(tr(":/images/eraser.png"), QSize(), QIcon::Normal, QIcon::Off);
actionEraser->setIcon(icon1);
actionEraser->setCheckable(true);
toolGroup->addAction(actionEraser);
actionBlock = new QAction(this);
actionBlock->setText("Block");
actionBlock->setObjectName("actionBlock");
actionBlock->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1));
QIcon icon2;
icon2.addFile(tr(":/images/block.png"), QSize(), QIcon::Normal, QIcon::Off);
actionBlock->setIcon(icon2);
actionBlock->setCheckable(true);
toolGroup->addAction(actionBlock);
actionCheese = new QAction(this);
actionCheese->setText("Cheese");
actionCheese->setObjectName("actionCheese");
actionCheese->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_2));
QIcon icon3;
icon3.addFile(tr(":/images/cheese.png"), QSize(), QIcon::Normal, QIcon::Off);
actionCheese->setIcon(icon3);
actionCheese->setCheckable(true);
toolGroup->addAction(actionCheese);
actionNail = new QAction(this);
actionNail->setText("Nail");
actionNail->setObjectName("actionNail");
actionNail->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_3));
QIcon icon4;
icon4.addFile(tr(":/images/nail.png"), QSize(), QIcon::Normal, QIcon::Off);
actionNail->setIcon(icon4);
actionNail->setCheckable(true);
toolGroup->addAction(actionNail);
actionBomb = new QAction(this);
actionBomb->setText("Bomb");
actionBomb->setObjectName("actionBomb");
actionBomb->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_4));
QIcon iconbomb;
iconbomb.addFile(tr(":/images/bomb.png"), QSize(), QIcon::Normal, QIcon::Off);
actionBomb->setIcon(iconbomb);
actionBomb->setCheckable(true);
toolGroup->addAction(actionBomb);
actionMouse = new QAction(this);
actionMouse->setText("Mouse");
actionMouse->setObjectName("actionMouse");
actionMouse->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_5));
QIcon icon5;
icon5.addFile(tr(":/images/mouse.png"), QSize(), QIcon::Normal, QIcon::Off);
actionMouse->setIcon(icon5);
actionMouse->setCheckable(true);
toolGroup->addAction(actionMouse);
actionCat = new QAction(this);
actionCat->setText("Cat");
actionCat->setObjectName("actionCat");
actionCat->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_6));
QIcon iconCat;
iconCat.addFile(tr(":/images/cat.png"), QSize(), QIcon::Normal, QIcon::Off);
actionCat->setIcon(iconCat);
actionCat->setCheckable(true);
toolGroup->addAction(actionCat);
actionElephant = new QAction(this);
actionElephant->setText("Elephant");
actionElephant->setObjectName("actionElephant");
actionElephant->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_7));
QIcon iconEle;
iconEle.addFile(tr(":/images/elephant.png"), QSize(), QIcon::Normal, QIcon::Off);
actionElephant->setIcon(iconEle);
actionElephant->setCheckable(true);
toolGroup->addAction(actionElephant);
actionPause_Resume = new QAction(this);
actionPause_Resume->setText("Pause/Resume");
actionPause_Resume->setObjectName("actionPause_Resume");
actionPause_Resume->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
QIcon icon6;
icon6.addFile(tr(":/images/resume.png"), QSize(), QIcon::Normal, QIcon::Off);
actionPause_Resume->setIcon(icon6);
actionPause_Resume->setCheckable(true);
actionSpeedUp = new QAction(this);
actionSpeedUp->setText("Speed Up");
actionSpeedUp->setObjectName("actionSpeedUp");
actionSpeedUp->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
QIcon icon7;
icon7.addFile(tr(":/images/speedup.png"), QSize(), QIcon::Normal, QIcon::Off);
actionSpeedUp->setIcon(icon7);
actionSpeedDown = new QAction(this);
actionSpeedDown->setText("Speed Down");
actionSpeedDown->setObjectName("actionSpeedDown");
actionSpeedDown->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
QIcon icon8;
icon8.addFile(tr(":/images/speeddown.png"), QSize(), QIcon::Normal, QIcon::Off);
actionSpeedDown->setIcon(icon8);
// viewer
centralWidget = new QWidget(this);
centralWidget->setObjectName("centralWidget");
this->setCentralWidget(centralWidget);
verticalLayout = new QVBoxLayout(centralWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(tr("verticalLayout"));
viewer = new Viewer(centralWidget);
viewer->setObjectName(tr("viewer"));
verticalLayout->addWidget(viewer);
// scene
scene = new Scene(this);
scene->setObjectName("scene");
connect(scene, SIGNAL(spiritsNumChanged(int)), this, SLOT(spiritsNumChanged(int)));
connect(scene, SIGNAL(paused()), this, SLOT(scenePaused()));
connect(scene, SIGNAL(resumed()), this, SLOT(sceneResumed()));
connect(scene, SIGNAL(currentSpeedLevel(int)), this, SLOT(sceneSpeedLevel(int)));
viewer->setScene(scene); // attach scene
// menubar
// File Menu
menuBar = new QMenuBar(this);
menuBar->setObjectName("menuBar");
menuBar->setGeometry(QRect(0, 0, 800, 25));
menuScene = new QMenu(menuBar);
menuScene->setObjectName("menuScene");
menuScene->setTitle(tr("&Scene"));
// Scene actions
actionNew = new QAction(this);
actionNew->setShortcut(tr("CTRL+N"));
actionNew->setIcon(QIcon(":/images/scene-new.png"));
actionNew->setObjectName(tr("actionNew"));
actionOpen = new QAction(this);
actionOpen->setShortcut(tr("CTRL+O"));
actionOpen->setIcon(QIcon(":/images/scene-open.png"));
actionOpen->setObjectName(tr("actionOpen"));
actionSave = new QAction(this);
actionSave->setShortcut(tr("CTRL+S"));
actionSave->setIcon(QIcon(":/images/scene-save.png"));
actionSave->setObjectName(tr("actionSave"));
actionSave_as = new QAction(this);
actionSave_as->setIcon(QIcon(":/images/scene-save-as.png"));
actionSave_as->setObjectName(tr("actionSave_as"));
actionQuit = new QAction(this);
actionQuit->setShortcut(tr("CTRL+Q"));
actionQuit->setIcon(QIcon(":/images/application-exit.png"));
actionQuit->setObjectName(tr("actionQuit"));
actionNew->setText("&New");
actionOpen->setText(tr("&Open"));
actionSave->setText("&Save");
actionSave_as->setText("Save as");
actionQuit->setText("&Quit");
menuDemo = new QMenu(this);
menuDemo->setTitle(tr("&Demo Scenes"));
for (int i = 0; i < MaxDemoScenes; ++i)
{
demoSceneActs[i] = new QAction(this);
demoSceneActs[i]->setVisible(false);
connect(demoSceneActs[i], SIGNAL(triggered()), this, SLOT(openDemoScene()));
menuDemo->addAction(demoSceneActs[i]);
}
updateDemoSceneActions();
menuScene->addAction(actionNew);
menuScene->addAction(actionOpen);
menuScene->addMenu(menuDemo);
menuScene->addSeparator();
menuScene->addAction(actionSave);
menuScene->addAction(actionSave_as);
menuScene->addSeparator();
menuScene->addAction(actionQuit);
// Options Menu
menuOptions = new QMenu(menuBar);
menuOptions->setObjectName("menuOptions");
menuOptions->setTitle(tr("&Options"));
// Options actions
actionConfigure = menuOptions->addAction(tr("Confi&gure"));
actionConfigure->setShortcut(tr("CTRL+G"));
actionConfigure->setIcon(QIcon(":/images/configure.png"));
actionConfigure->setObjectName("actionConfigure");
// Tools Menu
menuTools = new QMenu(menuBar);
menuTools->setObjectName("menuTools");
menuTools->setTitle(tr("&Tools"));
// Tools actions
actionGenRand = menuTools->addAction(tr("&Random Spirits"));
actionGenRand->setObjectName("actionGenRand");
actionGenRand->setShortcut(tr("CTRL+R"));
// Help Menu
menuHelp = new QMenu(menuBar);
menuHelp->setObjectName("menuHelp");
menuHelp->setTitle(tr("&Help"));
// Help actions
actionHelp_App = new QAction(this);
actionHelp_App->setObjectName(tr("actionHelp_App"));
actionHelp_App->setIcon(QIcon(":/images/help-contents.png"));
actionAbout_App = new QAction(this);
actionAbout_App->setObjectName(tr("actionAbout_App"));
actionAbout_GAMCS = new QAction(this);
actionAbout_GAMCS->setObjectName(tr("actionAbout_GAMCS"));
actionAbout_GAMCS->setIcon(QIcon(":/images/aboutGAMCS.png"));
actionHelp_App->setText(tr("Help"));
actionAbout_App->setText(QString("About %1").arg(APP));
actionAbout_GAMCS->setText("About GAMCS");
menuHelp->addAction(actionHelp_App);
menuHelp->addAction(actionAbout_App);
menuHelp->addAction(actionAbout_GAMCS);
// add menus to menuBar
menuBar->addAction(menuScene->menuAction());
menuBar->addAction(menuOptions->menuAction());
menuBar->addAction(menuTools->menuAction());
menuBar->addAction(menuHelp->menuAction());
this->setMenuBar(menuBar);
// toolbar
toolBar = new QToolBar(this);
toolBar->setObjectName("toolBar");
toolBar->setMovable(true);
toolBar->setFloatable(true);
toolBar->addAction(actionCursor);
toolBar->addAction(actionEraser);
toolBar->addSeparator();
toolBar->addAction(actionBlock);
toolBar->addAction(actionCheese);
toolBar->addAction(actionNail);
toolBar->addAction(actionBomb);
toolBar->addAction(actionMouse);
toolBar->addAction(actionCat);
toolBar->addAction(actionElephant);
toolBar->addSeparator();
toolBar->addAction(actionPause_Resume);
toolBar->addAction(actionSpeedUp);
toolBar->addAction(actionSpeedDown);
this->addToolBar(Qt::RightToolBarArea, toolBar);
// statusBar
statusBar = new QStatusBar(this);
statusBar->setObjectName("statusBar");
this->setStatusBar(statusBar);
// slots
QMetaObject::connectSlotsByName(this);
}
void MainWindow::spiritsNumChanged(int num)
{
QList<SpiritInfo> spirits_info = scene->statistics();
QString msg;
QTextStream(&msg) << "Spirits number: " << num;
for (QList<SpiritInfo>::iterator it = spirits_info.begin(); it != spirits_info.end(); ++it)
{
QTextStream(&msg) << ", " << it->name << ":" << it->num;
}
statusBar->showMessage(msg);
}
void MainWindow::scenePaused()
{
QIcon icon;
icon.addFile(tr(":/images/pause.png"), QSize(), QIcon::Normal, QIcon::Off);
actionPause_Resume->setIcon(icon);
actionPause_Resume->setChecked(true);
}
void MainWindow::sceneResumed()
{
QIcon icon;
icon.addFile(tr(":/images/resume.png"), QSize(), QIcon::Normal, QIcon::Off);
actionPause_Resume->setIcon(icon);
}
void MainWindow::sceneSpeedLevel(int level)
{
if (level == -1) // reach minima
{
actionSpeedDown->setEnabled(false);
}
else if (level == 1) // reach maxima
{
actionSpeedUp->setEnabled(false);
}
else
{
actionSpeedUp->setEnabled(true);
actionSpeedDown->setEnabled(true);
}
}
void MainWindow::on_actionCursor_triggered()
{
scene->setCurTool(Scene::T_NONE);
}
void MainWindow::on_actionEraser_triggered()
{
scene->setCurTool(Scene::T_ERASER);
}
void MainWindow::on_actionBlock_triggered()
{
scene->setCurTool(Scene::T_BLOCK);
}
void MainWindow::on_actionCheese_triggered()
{
scene->setCurTool(Scene::T_CHEESE);
}
void MainWindow::on_actionNail_triggered()
{
scene->setCurTool(Scene::T_NAIL);
}
void MainWindow::on_actionBomb_triggered()
{
scene->setCurTool(Scene::T_BOMB);
}
void MainWindow::on_actionMouse_triggered()
{
scene->setCurTool(Scene::T_MOUSE);
}
void MainWindow::on_actionCat_triggered()
{
scene->setCurTool(Scene::T_CAT);
}
void MainWindow::on_actionElephant_triggered()
{
scene->setCurTool(Scene::T_ELEPHANT);
}
void MainWindow::on_actionPause_Resume_toggled(bool arg1)
{
if (arg1)
{
scene->pause();
}
else
{
scene->resume();
}
}
void MainWindow::on_actionSpeedUp_triggered()
{
scene->speedUp();
}
void MainWindow::on_actionSpeedDown_triggered()
{
scene->speedDown();
}
void MainWindow::on_actionNew_triggered()
{
if (confirmClose())
{
scene->build();
setCurrentFileName(QString());
}
}
void MainWindow::on_actionOpen_triggered()
{
on_actionNew_triggered(); // clear the scene first
QString fn = QFileDialog::getOpenFileName(this, tr("Open Scene..."), QString(),
tr("Scene Files (*.scene);;All Files(*)"));
if (!fn.isEmpty())
{
openScene(fn);
}
}
void MainWindow::openScene(const QString &file)
{
if (confirmClose())
{
int ret = scene->load(file);
if (ret == 0) // load successfully
setCurrentFileName(file);
statusBar->showMessage(tr("scene opened"), 1000);
}
}
void MainWindow::on_actionSave_triggered()
{
if (filename.isEmpty())
on_actionSave_as_triggered();
else
saveScene(filename);
}
void MainWindow::on_actionSave_as_triggered()
{
QString fn = QFileDialog::getSaveFileName(this, tr("Save Scene..."), QString(),
tr("Scene Files (*.scene);;All Files(*)"));
if (!fn.isEmpty())
{
if (!fn.endsWith(".scene", Qt::CaseInsensitive))
fn += ".scene"; // default
saveScene(fn);
}
}
void MainWindow::saveScene(const QString &file)
{
scene->save(file);
if (filename.isEmpty())
filename = file;
statusBar->showMessage(tr("scene saved"), 1000);
}
void MainWindow::on_actionQuit_triggered()
{
close();
}
void MainWindow::on_actionHelp_App_triggered()
{
QFile help_file;
help_file.setFileName(":/help.html");
if (!help_file.open(QIODevice::ReadOnly))
{
qWarning("Failed to open help.html");
return;
}
QTextStream ts(&help_file);
ts.setCodec("UTF-8");
QString text = ts.readAll();
QMessageBox::information(this, "Help", text);
}
void MainWindow::on_actionAbout_App_triggered()
{
QMessageBox::about(this, tr("About"), tr("<p> This application demonstrates the using of GAMCS to create autonomous avatars, "
"providing a yard and several basic spirits for you to experiment with. </p>"
"<p> Just play around and watch the magic things that can happen. </p>"));
}
void MainWindow::on_actionAbout_GAMCS_triggered()
{
QString translatedTextAboutGAMCSCaption;
translatedTextAboutGAMCSCaption = QMessageBox::tr(
"<h3> About GAMCS</h3>"
"<p> The Mouse/Cat/Elephant spirits in this program are powered by GAMCS.");
QString translatedTextAboutGAMCSText;
translatedTextAboutGAMCSText = QMessageBox::tr(
"<p> <i>Generalized Agent Model and Computer Simulation</i>(GAMCS) is a computer implementation of <b>GAM</b>, which is a new, generalized (machine) learning algorithm..</p>"
"<p> GAMCS provides a very easy to use as well as complete interface. It is very fast and stable. </p>"
"<p> GAMCS is licensed under Mozilla Public License (MPL). </p>"
"<p> Please see <a href=\"http://gamcs.andy87.com\">gamcs.andy87.com</a> for more details about GAMCS. </p>"
);
QMessageBox *aboutBox = new QMessageBox(this);
aboutBox->setAttribute(Qt::WA_DeleteOnClose);
aboutBox->setWindowTitle(tr("About GAMCS"));
aboutBox->setText(translatedTextAboutGAMCSCaption);
aboutBox->setInformativeText(translatedTextAboutGAMCSText);
QPixmap logo(tr(":/images/gamcs-logo-small.png"));
aboutBox->setIconPixmap(logo);
aboutBox->setDefaultButton(aboutBox->addButton(QMessageBox::Ok));
aboutBox->show();
aboutBox->exec();
}
bool MainWindow::confirmClose()
{
if (scene->empty())
return true;
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Confirm Close?"),
tr("The Scene and all the Avatar Memories will be lost!"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
{
on_actionSave_triggered();
}
else if (ret == QMessageBox::Cancel)
{
return false;
}
return true; // saved
}
void MainWindow::setCurrentFileName(const QString &file)
{
this->filename = file;
QString showName;
if (filename.isEmpty())
showName = "untitled.scene";
else
showName = QFileInfo(filename).fileName();
setWindowTitle(tr("%1[*] [%2]").arg(showName).arg(tr("%1 v%2").arg(APP).arg(VER)));
setWindowModified(false);
}
void MainWindow::closeEvent(QCloseEvent *e)
{
if (confirmClose())
e->accept();
else
e->ignore();
}
void MainWindow::on_actionGenRand_triggered()
{
RandDialog dia;
dia.exec();
scene->genRandSpirit(dia.getNum(), dia.getTypes());
}
void MainWindow::on_actionConfigure_triggered()
{
ConfigDialog dialog;
dialog.exec();
}
void MainWindow::updateDemoSceneActions()
{
// get file list in the demo directory
QDir demo_dir;
demo_dir.setPath("./demos");
if (!demo_dir.exists())
{
qWarning() << "Demo path" << demo_dir.path() << "not exists!";
return;
}
QStringList filter;
filter << "*.scene" << "*.sce";
QStringList scene_files;
scene_files = demo_dir.entryList(filter, QDir::Files);
int num_demo_scenes = scene_files.size() > MaxDemoScenes ? MaxDemoScenes : scene_files.size();
for (int i = 0; i < num_demo_scenes; ++i)
{
QString text = tr("%1").arg(QFileInfo(scene_files.at(i)).baseName());
demoSceneActs[i]->setText(text);
demoSceneActs[i]->setData(demo_dir.absoluteFilePath(scene_files.at(i)));
demoSceneActs[i]->setVisible(true);
}
}
void MainWindow::openDemoScene()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
openScene(action->data().toString());
}