-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
179 lines (154 loc) · 6.63 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
#include "mainwindow.h"
#include "aboutus.h"
#include "choicebutton.h"
#include "gameview.h"
#include "settings.h"
#include "welcome.h"
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QGraphicsScene>
#include <QMenu>
#include <QMenuBar>
#include <QScreen>
#include <QSettings>
#include <QSlider>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
creatActions();
// delete the MaximizeButton
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
setCentralWidget(control.view);
// move the MainWindow to screen Centre
const QRect availableGeometry = screen()->availableGeometry();
adjustSize();
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
setFixedSize(width(), height());
QObject::connect(control.settings->fullscreen, &ChoiceButton::toggled,
[this](bool toggled) {
if (!toggled)
return;
else
showFullScreen();
});
QObject::connect(control.settings->normal, &ChoiceButton::toggled,
[this](bool toggled) {
if (!toggled)
return;
else
showNormal();
});
readSettings();
}
MainWindow::~MainWindow() {}
void MainWindow::readSettings() {
if (!QFile::exists("./settings.ini")) {
control.settings->remark =
new QSettings("./settings.ini", QSettings::IniFormat);
control.settings->remark->setValue("isFullscreen", false);
control.settings->remark->setValue("volume", 80);
control.settings->remark->sync();
control.setVolume(80);
} else {
control.settings->remark =
new QSettings("./settings.ini", QSettings::IniFormat);
if (control.settings->remark->value("isFullscreen").toBool()) {
control.settings->fullscreen->setChecked(true);
control.settings->normal->setChecked(false);
emit control.settings->fullscreen->toggled(true);
}
if (control.settings->remark->value("showShape").toBool()) {
control.settings->showShape->setChecked(true);
control.settings->noShowShape->setChecked(false);
emit control.settings->showShape->toggled(true);
}
int vol = control.settings->remark->value("volume").toInt();
control.settings->volume->setValue(vol);
emit control.settings->volume->valueChanged(vol);
}
// first read settings to set the flag
control.settings->hasChanged = false;
control.settings->setVisible(false);
control.welcome->setVisible(true);
emit control.notInSettings(true);
}
void MainWindow::creatActions() {
QMenu* fileMenu = menuBar()->addMenu(tr("文件(&F)"));
QAction* new_gameAct =
fileMenu->addAction(tr("&New game"), &control, &GameControl::newGame);
new_gameAct->setShortcut(QKeySequence::New);
QObject::connect(&control, &GameControl::notInSettings, new_gameAct,
&QAction::setEnabled);
QAction* re_to_title = fileMenu->addAction(
tr("返回至标题画面(&R)"), &control, &GameControl::reToTitle);
re_to_title->setEnabled(false);
QObject::connect(&control, &GameControl::inGame, re_to_title,
&QAction::setEnabled);
fileMenu->addSeparator();
QAction* exit = fileMenu->addAction(tr("退出游戏(&E)"));
QObject::connect(exit, &QAction::triggered, &QApplication::exit);
exit->setShortcut(QKeySequence::Quit);
QMenu* setMenu = menuBar()->addMenu(tr("设置(&S)"));
setMenu->addAction(tr("显示设置面板(&P)"), &control,
&GameControl::showSettings);
setMenu->addSeparator();
QActionGroup* windowGroup = new QActionGroup(this);
windowGroup->setExclusive(true);
QAction* window = windowGroup->addAction(tr("窗口(&W)"));
QAction* fullscreen = windowGroup->addAction(tr("全屏(&F)"));
window->setCheckable(true);
fullscreen->setCheckable(true);
window->setChecked(true);
setMenu->addActions(windowGroup->actions());
QObject::connect(window, &QAction::toggled, [this](bool toggeled) {
if (!toggeled) return;
control.showSettings();
control.settings->normal->setChecked(true);
control.settings->fullscreen->setChecked(false);
emit control.settings->normal->toggled(true);
});
QObject::connect(fullscreen, &QAction::toggled, [this](bool toggeled) {
if (!toggeled) return;
control.showSettings();
control.settings->fullscreen->setChecked(true);
control.settings->normal->setChecked(false);
emit control.settings->fullscreen->toggled(true);
});
QObject::connect(control.settings->normal, &ChoiceButton::toggled, window,
&QAction::setChecked);
QObject::connect(control.settings->fullscreen, &ChoiceButton::toggled,
fullscreen, &QAction::setChecked);
// setMenu->addSeparator();
/*When the Action is checked, segmentation faults occurs. Why is it?*/
// QAction* showShape = setMenu->addAction(tr("显示判定区域"));
// showShape->setCheckable(true);
// showShape->setChecked(false);
// QObject::connect(showShape, &QAction::toggled, [this](bool toggled) {
// control.showSettings();
// control.settings->noShowShape->setChecked(!toggled);
// control.settings->showShape->setChecked(toggled);
// if (toggled)
// emit control.settings->showShape->toggled(true);
// else
// emit control.settings->noShowShape->toggled(true);
// });
// QObject::connect(control.settings->showShape, &ChoiceButton::toggled,
// showShape, &QAction::setChecked);
// QObject::connect(control.settings->noShowShape,
// &ChoiceButton::toggled,
// [showShape]() { showShape->setChecked(false); });
QMenu* aboutMenu = menuBar()->addMenu(tr("帮助(&H)"));
aboutMenu->addAction(tr("关于我们(&A)"), this, &MainWindow::setAboutus);
}
void MainWindow::setAboutus() {
aboutus = new Aboutus(nullptr);
aboutus->setFixedSize(800, 600);
aboutus->adjustSize();
const QRect availableGeometry = screen()->availableGeometry();
aboutus->move((availableGeometry.width() - aboutus->width()) / 2,
(availableGeometry.height() - aboutus->height()) / 2);
aboutus->setAttribute(Qt::WA_DeleteOnClose);
aboutus->show();
}