-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
734 lines (657 loc) · 25.4 KB
/
mainwindow.cpp
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmath.h>
#include <QColor>
#include <QColorDialog>
#include <QTextStream>
#include <QFileDialog>
#include <QChar>
#include <QList>
#include <QValidator>
#include <QInputDialog>
#include <QStandardItemModel>
#include <QDebug>
//=====================================================================================~~Constructor~~=====================================================================================
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
state(false),
ui(new Ui::MainWindow),
currentColor(QColor("#000")), //Cells are black by default.
game(new GameWidget(this)), //Create custom widget instance.
reg8(QRegExp("[0-8]{0,9}")), //For 8 neighbours rules.
reg4(QRegExp("[0-4]{0,5}")), //For 4 neighbours rules.
gridRatio(10),
infoDialog(new InfoDialog(this)),
intervalMin(25),
intervalMax(1000)
{
ui->setupUi(this);
readDefaults();
readRulesets();
//Make a color square to show current color.
QPixmap icon(12, 12);
icon.fill(currentColor);
ui->colorButton->setIcon( QIcon(icon) );
connect(ui->StartStopBut, SIGNAL(clicked()), this,SLOT(startStopGame()));
connect(game,SIGNAL(gameStops(bool)), this,SLOT(startStopGame()));
connect(ui->saveBut, SIGNAL(clicked()), this,SLOT(saveGame()));
connect(ui->loadBut, SIGNAL(clicked()), this,SLOT(loadGame()));
connect(ui->rootBut, SIGNAL(clicked()), this,SLOT(setTreeRoot()));
connect(ui->ClearBut, SIGNAL(clicked()), game,SLOT(clear()));
connect(ui->stepBut, SIGNAL(clicked()), game, SLOT(step()));
connect(ui->infoBut, SIGNAL(clicked()), this, SLOT(showInfo()));
connect(ui->intervalSlider, SIGNAL(valueChanged(int)), this, SLOT(setInterval(int)));
connect(ui->heightControl, SIGNAL(valueChanged(int)), game, SLOT(setUniverseHeight(int)));
connect(ui->widthControl, SIGNAL(valueChanged(int)), game, SLOT(setUniverseWidth(int)));
connect(ui->heightControl, SIGNAL(valueChanged(int)), this, SLOT(gridResize()));
connect(ui->widthControl, SIGNAL(valueChanged(int)), this, SLOT(gridResize()));
connect(ui->modeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setNeighMode(int)));
connect(ui->edgeRadio, SIGNAL(toggled(bool)), this, SLOT(setEdgeMode(bool)));
connect(ui->Bstates, SIGNAL(textChanged(QString)), this, SLOT(setBStates(QString)));
connect(ui->Sstates, SIGNAL(textChanged(QString)), this, SLOT(setSStates(QString)));
connect(game,SIGNAL(info(QString)), ui->labelInfo, SLOT(setText(QString)));
connect(ui->rulesetsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRuleset(int)));
connect(ui->addBut, SIGNAL(clicked()), this, SLOT(addRuleset()));
connect(ui->removeBut, SIGNAL(clicked()), this, SLOT(removeRuleset()));
connect(ui->invBut, SIGNAL(clicked()), game, SLOT(invert()));
connect(game, SIGNAL(sendGen(int)), ui->lcdG, SLOT(display(int)));
connect(game, SIGNAL(sendPop(int)), ui->lcdP, SLOT(display(int)));
connect(game, SIGNAL(sendXY(int,int)), this, SLOT(showCoord(int, int)));
connect(ui->colorButton, SIGNAL(clicked()), this, SLOT(selectMasterColor()));
connect(ui->zoomInBut, SIGNAL(clicked()), this, SLOT(zoomIn()));
connect(ui->zoomOutBut, SIGNAL(clicked()), this, SLOT(zoomOut()));
connect(game, SIGNAL(crwheelup()), this, SLOT(zoomIn()));
connect(game, SIGNAL(crwheeldw()), this, SLOT(zoomOut()));
connect(game, SIGNAL(wheelup()), this, SLOT(scrollUp()));
connect(game, SIGNAL(wheeldw()), this, SLOT(scrollDw()));
connect(game, SIGNAL(stwheelup()), this, SLOT(scrollRt()));
connect(game, SIGNAL(stwheeldw()), this, SLOT(scrollLt()));
connect(ui->SaveMenu, SIGNAL(triggered()), this, SLOT(saveGame()));
connect(ui->LoadMenu, SIGNAL(triggered()), this, SLOT(loadGame()));
game->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->GameArea->setBackgroundRole(QPalette::Dark);
ui->GameArea->setWidget(game); //Custom widget, has to be added manually.
ui->GameArea->setWidgetResizable(false);
ui->GameArea->setAlignment(Qt::AlignHCenter);
ui->GameArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->GameArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
game->setBackgroundRole(QPalette::Base);
game->resize(game->getUniverseWidth() * gridRatio, game->getUniverseHeight() * gridRatio);
game->setToolTip("¤ Left click to draw \n¤ Right click to erase");
treeModel = new QFileSystemModel(this);
ui->treeView->setModel(treeModel);
ui->treeView->setRootIndex(treeModel->setRootPath(treeRoot));
ui->treeView->hideColumn(1);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
ui->Bstates->setValidator( new QRegExpValidator(reg8, this) );
ui->Sstates->setValidator( new QRegExpValidator(reg8, this) );
ui->lcdG->setPalette(Qt::green);
ui->lcdP->setPalette(Qt::green);
ui->lcdX->setPalette(Qt::red);
ui->lcdY->setPalette(Qt::red);
ui->labelInfo->setText("...");
}
//Destructor:
MainWindow::~MainWindow()
{
delete ui;
}
//====================================================================================~~File_Handling~~====================================================================================
void MainWindow::readDefaults()
{
//Reads .ini default files and initializes UI variables.
// Root Path:
QFile file_r(".." + QString(QDir::separator()) + "rootpath.ini");
if(!file_r.open(QIODevice::ReadOnly)){
//if no default.ini, write it.
file_r.open(QIODevice::WriteOnly | QIODevice::Truncate);
treeRoot = ".." + QString(QDir::separator()) + "patterns";
file_r.write(treeRoot.toUtf8());
file_r.close();
} else {
// .ini found, read path
QTextStream in_r(&file_r);
in_r >> treeRoot;
treeRoot.replace("/", QDir::separator());
treeRoot.replace("\\", QDir::separator());
if(QDir(treeRoot).exists()){
curPath = treeRoot;
file_r.close();
} else {
//Revert to default if the path doesn't exist.
//"./pattern" is the go-to default but if it would be removed, the treeModel would go to the system root until a new valid path is added using setTreeRoot().
file_r.close();
file_r.open(QIODevice::WriteOnly | QIODevice::Truncate);
treeRoot = "." + QString(QDir::separator()) + "patterns";
file_r.write(treeRoot.toUtf8());
file_r.close();
} }
// Startup game settings:
QFile file_d(".." + QString(QDir::separator())+ "default.ini");
if(!file_d.open(QIODevice::ReadOnly)){
//if no default.ini, write it.
file_d.open(QIODevice::WriteOnly | QIODevice::Truncate);
QString def = "#grid:\nheight:50\nwidth:50\n\n#game:\ninterval:100\nmode:m\nruleB:3\nruleS:23\n\n#color:\nr:0\ng:0\nb:0";
file_d.write(def.toUtf8());
defBstates = "3";
defSstates = "23";
}
//read:
int r = 0; int g = 0; int b = 0;
QTextStream in_d(&file_d);
QString buf;
while( ! in_d.atEnd()){
in_d >> buf;
if(buf != "" || buf.startsWith("#")){
//ignore comments and empty lines.
QList<QString> var = buf.split(":");
//Look for identifiers:
if(var[0] == "height"){
ui->heightControl->setValue(var[1].toInt());
game->setUniverseHeight(var[1].toInt());
} else if(var[0] == "width"){
ui->widthControl->setValue(var[1].toInt());
game->setUniverseWidth(var[1].toInt());
} else if(var[0] == "interval"){
ui->intervalSlider->setValue(var[1].toInt());
setInterval(var[1].toInt());
} else if(var[0] == "mode"){
if(var[1] == "m"){
ui->modeBox->setCurrentIndex(0);
game->setNeighMode('m');
} else {
ui->modeBox->setCurrentIndex(1);
game->setNeighMode('v');
}
} else if(var[0] == "ruleB") {
ui->Bstates->setText(var[1]);
defBstates = var[1];
setBStates(var[1]);
} else if(var[0] == "ruleS"){
ui->Sstates->setText(var[1]);
defSstates = var[1];
setSStates(var[1]);
} else if(var[0] == "r"){
r = var[1].toInt();
} else if(var[0] == "g"){
g = var[1].toInt();
} else if(var[0] == "b"){
b = var[1].toInt();
}
}
}
//Set Color
currentColor = QColor(r,g,b);
game->setMasterColor(currentColor); // sets color of the cells
QPixmap icon(12, 12); // icon on the button
icon.fill(currentColor); // fill with new color
ui->colorButton->setIcon( QIcon(icon) );
file_d.close();
}
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
//Open double clicked file.
QString filename = treeModel->fileInfo(index).filePath();
readGame(filename);
}
void MainWindow::readRulesets(){
//Reads ruleset codes and add them to the list.
QFile file(".." + QString(QDir::separator()) + "rulesets.ini");
disconnect(ui->rulesetsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRuleset(int))); //Needs to be disconected to clear or causes crash (index error).
ui->rulesetsBox->clear();
connect(ui->rulesetsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRuleset(int)));
rulesets.clear();
if(!file.open(QIODevice::ReadOnly)){
//If no file, write it.
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QString r = "3|23|m|p|Life"; //Let's at leat have Conway's game.
file.write(r.toUtf8());
QList<QString> token;
rulesets.append(token);
rulesets[0].append("3");
rulesets[0].append("23");
rulesets[0].append("m");
rulesets[0].append("Life");
ui->rulesetsBox->addItem(rulesets[0][3]);
}
QTextStream in(&file);
QString buf;
int i = 0;
//Read rulesets:
while( !in.atEnd()){
in >> buf;
QList<QString> tmp = buf.split("|");
if(tmp.size() >= 4){
rulesets.append(tmp);
ui->rulesetsBox->addItem(rulesets[i][3]);
i++;
}
}
file.close();
ui->rulesetsBox->addItem(" "); //Placeholder Item.
qobject_cast<QStandardItemModel *>(ui->rulesetsBox->model())->item( ui->rulesetsBox->count() -1 )->setEnabled( false );
}
void MainWindow::writeRulesets(){
//Rulesets save to file.
QFile file(".." + QString(QDir::separator()) + "rulesets.ini");
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{return;}
QString s;
for( int i = 0; i < rulesets.size(); i++)
{
s = "";
for(int j=0; j<rulesets[i].size(); j++)
{
s += rulesets[i][j] + "|";
}
s.chop(1); //No "|" after last element.
s += "\n";
file.write(s.toUtf8());
}
file.close();
}
void MainWindow::saveGame()
//Game dump handler to file. Saves on prompted path.
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Save current game"),
curPath,
tr("Life automaton Files (*.laut)"));
if(filename.length() < 1){
return;}
if(!filename.endsWith(".laut")){ filename += ".laut"; }
//extension used to filter files. Files without it will not be loaded.
QFile file(filename);
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{return;}
curPath = QFileInfo(file).absolutePath(); //Save path for next time.
QString n;
if(ui->modeBox->currentIndex() == 0){ n = 'm';} else { n = 'v';}
QString s = ui->Bstates->text() + "|" + ui->Sstates->text() + "|" + n + "\n"; //Ruleset for the pattern.
file.write(s.toUtf8());
s = QString::number(game->getUniverseHeight()) + "|" + QString::number(game->getUniverseWidth()) +"\n"; //Grid dimensions.
file.write(s.toUtf8());
if(ui->edgeRadio->isChecked()){ s = "t\n"; } else { s = "p\n"; } //connected edges?
file.write(s.toUtf8());
file.write(game->dump().toUtf8()); //Game "o"/"*" dump.
QColor color = game->masterColor();
s = QString::number(color.red())+" "+ //RGB setting for cells.
QString::number(color.green())+" "+
QString::number(color.blue())+"\n";
file.write(s.toUtf8());
s = QString::number(ui->intervalSlider->value())+"\n"; //Timer setting.
file.write(s.toUtf8());
file.close();
ui->labelInfo->setText("Pattern saved: " + QFileInfo(file).fileName());
}
void MainWindow::loadGame()
//Prompt for file loading.
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open saved game"),
curPath,
tr("Life automaton Files (*.laut)"));
if(filename.length() < 1)
return;
readGame(filename);
}
void MainWindow::readGame(QString filename)
{
//Game dump handler from file. Can be called from prompt or from the treeview.
game->clear();
gridRatio = 10; //Ratio reset is necessary because apparently loading a small pattern when zoomed far out crashes the widget.
scaleGame(1.0);
QFile file(filename);
if(!file.open(QIODevice::ReadOnly) || !filename.endsWith(".laut")){
//extension used to filter files. Files without it will not be loaded.
return;}
curPath = QFileInfo(file).absolutePath(); //Save path for next time.
QTextStream in(&file);
//Setup ruleset:
QString tmp;
in >> tmp;
QList<QString> tmpl = tmp.split('|');
ui->Bstates->setText(tmpl[0]);
ui->Sstates->setText(tmpl[1]);
if(tmpl[2] == "m"){
ui->modeBox->setCurrentIndex(0);
}else{
ui->modeBox->setCurrentIndex(1);
}
//Setup grid:
in >> tmp;
tmpl = tmp.split('|');
int h = tmpl[0].toInt();
int w = tmpl[1].toInt();
ui->heightControl->setValue(h);
ui->widthControl->setValue(w);
//Edge mode:
in >> tmp;
if( tmp == "t"){ ui->edgeRadio->setChecked(true);}
else { ui->edgeRadio->setChecked(false);}
//Load dump:
QString dump="";
for(int k=0; k != h ; k++) {
QString t;
in >> t;
dump.append(t+"\n");
}
game->setDump(dump);
//Setup RGB setting.
int r,g,b;
in >> r >> g >> b;
currentColor = QColor(r,g,b);
game->setMasterColor(currentColor); // sets color of the cells
QPixmap icon(12, 12); // icon on the button
icon.fill(currentColor); // fill with new color
ui->colorButton->setIcon( QIcon(icon) ); // set icon for button
in >> r; // r will be interval number
//Setup speed increment:
ui->intervalSlider->setValue(r);
setInterval(r);
//End:
file.close();
ui->labelInfo->setText("Pattern loaded: " + QFileInfo(file).fileName());
game->update();
}
//====================================================================================~~Action-Slots~~====================================================================================
void MainWindow::startStopGame()
{
//Handler for start/pause button and other interruptions.
if(state == false)
{
state = true;
game->startGame();
ui->StartStopBut->setIcon(QIcon(":/icons/icons/pause.png"));
ui->StartStopBut->setToolTip("Pause game (space)");
} else {
state = false;
game->stopGame();
ui->StartStopBut->setIcon(QIcon(":/icons/icons/play.png"));
ui->StartStopBut->setToolTip("Start game (space)");
}
}
void MainWindow::setInterval(int ms){
game->setInterval(ms);
ui->intervalSlider->setToolTip("<html>Turn interval (" + QString::number(ms) + "ms<sup>-1</sup>)</html>");
}
void MainWindow::setTreeRoot()
//Prompt for the tree root button. Saves path in ini file.
{
QString tmp = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
treeRoot,
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(tmp != ""){
tmp = QDir("." + QString(QDir::separator())).relativeFilePath(tmp);
treeRoot = tmp;
ui->treeView->setRootIndex(treeModel->setRootPath(treeRoot));
QFile file(".." + QString(QDir::separator()) + "rootpath.ini");
if(!file.open(QIODevice::WriteOnly| QIODevice::Truncate)){
return;}
file.write(treeRoot.toUtf8());
file.close();
}
}
void MainWindow::selectRuleset(int index)
{
//Handler for the ruleset comboBox.
QList<QString> rule = rulesets[index];
if(rule[2] == "v"){
ui->modeBox->setCurrentIndex(1);
setNeighMode(1);
} else {
ui->modeBox->setCurrentIndex(0);
setNeighMode(0);
}
ui->Bstates->setText(rule[0]);
setBStates(rule[0]);
ui->Sstates->setText(rule[1]);
setSStates(rule[1]);
ui->labelInfo->setText("Ruleset \"" + rule[3] + "\"(" + "B" + rule[0] + "/S" + rule[1] + ") selected.");
}
void MainWindow::addRuleset()
{
//Handler for add button. Adds ruleset with prompted name. Overwrites duplicates.
QList<QString> rule =getRuleSet();
QString name = QInputDialog::getText(this ,"Name your ruleset.",
"Ruleset name:", QLineEdit::Normal);
name.replace(" ", "_");
if(name == ""){
return;}
int r = rulesetExists(rule); //Detect if that ruleset is already present
if(r > -1){
//This rule already exists, we just change the name.
rulesets[r][3] = name;
ui->labelInfo->setText("Ruleset (B" + rule[0] + "/S" + rule[1] + ") renamed to \"" + name + "\"." );
} else {
rule[3] = name;
rulesets.append(rule);
ui->labelInfo->setText("Ruleset \"" + rule[3] + "\"(" + "B" + rule[0] + "/S" + rule[1] + ") added.");
}
writeRulesets();
readRulesets();
setBStates(rule[0]);
setSStates(rule[1]);
}
QList<QString> MainWindow::getRuleSet()
{
//Method to retrieve the current settings in the same format as ruleset elements.
QList<QString> rule;
for(int i = 0; i <= 3; i++){ rule.append(""); }
rule[0] = ui->Bstates->text();
rule[1] = ui->Sstates->text();
if(ui->modeBox->currentIndex() == 0){
rule[2] = "m";
} else {
rule[2] = "v";
}
rule[3].append("");
return rule;
}
int MainWindow::rulesetExists(QList<QString> rule){
//Compare a ruleset to saved rulesets, ignores name.
for(int r = 0; r < rulesets.size(); r++){
if(rulesets[r][0] == rule[0] && rulesets[r][1] == rule[1] && rulesets[r][2] == rule[2])
{ return r; }
}
return -1;
}
void MainWindow::removeRuleset()
{
//deletes selected ruleset.
rulesets.removeAt(ui->rulesetsBox->currentIndex());
writeRulesets();
readRulesets();
ruleSwich();
}
void MainWindow::ruleSwich()
{
//Detects if input ruleset already exists and switches combobox to it or placeholder.
disconnect(ui->rulesetsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRuleset(int)));
//Disconect before switching to avoid endless feedback loop.
int r = rulesetExists(getRuleSet());
if( r > -1) {
ui->rulesetsBox->setCurrentIndex(r); //Place on corresponding item;
ui->removeBut->setEnabled(true); //Rule can be deleted.
} else {
ui->rulesetsBox->setCurrentIndex(ui->rulesetsBox->count() - 1); //Or put on placeholder.
ui->removeBut->setDisabled(true); //Nothing to delete.
}
connect(ui->rulesetsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRuleset(int))); //reconnect
}
void MainWindow::selectMasterColor()
{
//Color dialog handler for the button.
QColor color = QColorDialog::getColor(currentColor, this, tr("Select color of figures"));
if(!color.isValid())
return;
currentColor = color;
game->setMasterColor(color);
QPixmap icon(12, 12);
icon.fill(color);
ui->colorButton->setIcon( QIcon(icon) );
}
void MainWindow::setNeighMode(int index)
//Swiches neighbourhood mode in the game instance.
// 'm' mode = Moore -> 8 neighbours considered.
// 'm' mode = Von Neumann -> Only the 4 direct cardinal neighbours are considered.
{
if(state)
{
startStopGame();
}
if(index == 0){
game->setNeighMode('m');
//Filter characters from regex:
ui->Bstates->setValidator( new QRegExpValidator(reg8, this) );
ui->Sstates->setValidator( new QRegExpValidator(reg8, this) );
}
if(index == 1){
game->setNeighMode('v');
//Filter characters from regex:
ui->Bstates->setValidator( new QRegExpValidator(reg4, this) );
ui->Sstates->setValidator( new QRegExpValidator(reg4, this) );
}
//reset states to avoid conflict:
ui->Bstates->setText(defBstates);
ui->Sstates->setText(defSstates);
ruleSwich(); //Compare new ruleset.
}
void MainWindow::setEdgeMode(bool state)
{
if(state){game->setEdgeMode('t');}
else{game->setEdgeMode('p');}
}
void MainWindow::setBStates(QString b)
//handler for rule lineedit input. Converts string to a sorted number array without duplicates.
{
if(state)
{
startStopGame();
}
//Read Regex validated string.
QList<int> array;
foreach(QChar c, b){
int n = c.digitValue();
if(!array.contains(n)){
array.append(n);
}
}
// Change Game rules.
game->setBirthStates(array);
// Don't allow duplicates + sort
qSort(array);
QString nText;
for(int i=0; i<array.size(); i++)
{
nText += QString::number(array[i]);
}
ui->Bstates->setText(nText);
ruleSwich(); //Compare new ruleset.
}
void MainWindow::setSStates(QString s)
//handler for rule lineedit input. Converts string to a sorted number array without duplicates.
{
if(state)
{
startStopGame();
}
game->stopGame();
//Read Regex validated string.
QList<int> array;
foreach(QChar c, s){
int n = c.digitValue();
if(!array.contains(n)){
array.append(n);
}
}
// Change Game rules.
game->setSurvStates(array);
// Don't allow duplicates + sort
qSort(array);
QString nText;
for(int i=0; i<array.size(); i++)
{
nText += QString::number(array[i]);
}
ui->Sstates->setText(nText);
ruleSwich(); //Compare new ruleset.
}
void MainWindow::showInfo()
{
//Reciever for various status update messages.
infoDialog.show();
infoDialog.activateWindow();
}
//====================================================================================~~Graphical-slots~~====================================================================================
void MainWindow::scaleGame(double factor)
{
//Grid scaler for zoom actions.
gridRatio = ( factor * gridRatio); //Modify ratio.
//Limits for resizing:
if( gridRatio <= 1.5){
gridRatio = 1.5;
factor = 1;
}
if(gridRatio > 300){
gridRatio = 300;
factor = 1;
}
//Apply new ratio:
game->resize(game->getUniverseWidth() * gridRatio, game->getUniverseHeight() * gridRatio);
// Relative (-0.8:0.8) offsets to center axes of the game area:
//0.8 gives a softer transition.
float y = 0.8 * ((ui->GameArea->mapFromGlobal(QCursor::pos()).y() - ((float)ui->GameArea->height() / 2) ) / (float)ui->GameArea->height());
float x = 0.8 * ((ui->GameArea->mapFromGlobal(QCursor::pos()).x() - ((float)ui->GameArea->width() / 2) ) / (float)ui->GameArea->width());
//go to cursor position:
adjustScrollBar(ui->GameArea->verticalScrollBar(), factor, y);
adjustScrollBar(ui->GameArea->horizontalScrollBar(), factor, x);
}
void MainWindow::showCoord(int x, int y)
{
//Coordinates reciever for display.
ui->lcdX->display(x);
ui->lcdY->display(y);
}
void MainWindow::gridResize()
{
//Used only for resize when the grid dimensions change.
if( gridRatio <= 1.5){
gridRatio = 1.5;
}
if(gridRatio > 300){
gridRatio = 300;
}
game->clear();
game->resize(game->getUniverseWidth() * gridRatio, game->getUniverseHeight() * gridRatio);
}
void MainWindow::adjustScrollBar(QScrollBar *scrollBar, double factor, double offset)
{
//Allows to Zoom on the point under the cursor.
scrollBar->setValue(int(factor * scrollBar->value()
+ ((factor - 1) * scrollBar->pageStep()/2) //focus on center of the grid.
+ (offset * scrollBar->maximum() ))); //Offset towards the cursor.
}
void MainWindow::zoomIn(){
//Zoom in act in response to ctrl+wheel signal.
scaleGame(1.10);
}
void MainWindow::zoomOut(){
//Zoom out act in response to ctrl+wheel signal.
scaleGame(0.9);
}
//Scrolling navigation:
void MainWindow::scrollUp(){
ui->GameArea->verticalScrollBar()->setValue(ui->GameArea->verticalScrollBar()->value() - (game->getUniverseHeight()/10 * gridRatio) );
}
void MainWindow::scrollDw(){
ui->GameArea->verticalScrollBar()->setValue(ui->GameArea->verticalScrollBar()->value() + (game->getUniverseHeight()/10 * gridRatio));
}
void MainWindow::scrollRt(){
ui->GameArea->horizontalScrollBar()->setValue(ui->GameArea->horizontalScrollBar()->value() + (game->getUniverseWidth()/10 * gridRatio));
}
void MainWindow::scrollLt(){
ui->GameArea->horizontalScrollBar()->setValue(ui->GameArea->horizontalScrollBar()->value() - (game->getUniverseWidth()/10 * gridRatio));
}