-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
303 lines (268 loc) · 10.5 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
/****************************************************************************
**
** Copyright (C) 2009 TECHNOGERMA Systems France and/or its subsidiary(-ies).
** Contact: Technogerma Systems France Information ([email protected])
**
** This file is part of the GICS library.
**
** Commercial Usage
** Licensees holding valid GICS Commercial licenses may use this file in
** accordance with the GICS Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and TECHNOGERMA Systems France.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL3.txt included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
****************************************************************************/
/****************************************************************************
**
** Copyright (C) 2009 caiwenfeng at suda dot edu dot cn.
**
****************************************************************************/
#include "mainwindow.hpp"
#include "view.hpp"
#include "inputpanel.hpp"
#include "outputpanel.hpp"
#include <gics/button.hpp>
#include <gics/panel.hpp>
#include <gics/picture.hpp>
#include <QGraphicsLinearLayout>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QFont>
#include <QFile>
#include <QFileDialog>
#include <QDomDocument>
#include <QDomElement>
#include <QDateTime>
#include <QPixmap>
#include <boost/numeric/conversion/converter.hpp>
//-------------------------------------------------------------------------------------------------
MainWindow::MainWindow(QWidget* parent, Qt::WFlags flags)
: QMainWindow(parent, flags | Qt::FramelessWindowHint)
{
// Setup the window
setStyleSheet("background: black");
setWindowTitle(tr("点将机"));
resize(800, 600);
setWindowIcon(QIcon(":/skins/logozzz.png"));
// Create the title
title = new gics::TextLabel(tr("Attention!"));
title->setHorizontalTextAlignment(gics::alignHCenter);
title->setHorizontalSizePolicy(QSizePolicy::MinimumExpanding);
title->setColor(Qt::white);
title->setFont(QFont(tr("Arial Unicode MS"), 30, QFont::Bold));
// Create the input panel
m_input = new InputPanel;
// Create the output panel
m_output = new OutputPanel;
// Create the logo
gics::Picture* logo = new gics::Picture;
logo->setFile(":/skins/logozzz.svg");
logo->setId("zzz");
logo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// Create the "about" button
gics::Button* about = new gics::Button(tr("多此一举"));
about->setButtonType(gics::Button::monostable);
about->setMinimumSize(100, 30);
about->setMaximumSize(100, 30);
about->setDisplaySettings(gics::Button::showText);
about->setHorizontalSizePolicy(QSizePolicy::Fixed);
// Create the "quit" button
gics::Button* quit = new gics::Button(tr("走人"));
quit->setButtonType(gics::Button::monostable);
quit->setMinimumSize(100, 30);
quit->setMaximumSize(100, 30);
quit->setDisplaySettings(gics::Button::showText);
quit->setHorizontalSizePolicy(QSizePolicy::Fixed);
// Create the about title
gics::TextLabel* aboutTitle = new gics::TextLabel(tr("关于点将机"));
aboutTitle->setHorizontalSizePolicy(QSizePolicy::MinimumExpanding);
aboutTitle->setHorizontalTextAlignment(gics::alignHCenter);
aboutTitle->setColor(Qt::white);
QFont font = aboutTitle->font();
aboutTitle->setFont(QFont(tr("Arial Unicode MS"), 30, QFont::Bold));
// Create the about text
gics::TextLabel* aboutText = new gics::TextLabel;
aboutText->setColor(Qt::white);
aboutText->setText(tr("三天不点名,全都跑没影。\n\n点将机 0.2.2.\n\nCopyright 2010 caiwenfeng at suda dot edu dot cn.\n\nBase one the demo app of Tegesoft."));
aboutText->setHorizontalTextAlignment(gics::alignHCenter);
// Create the "close about" button
gics::Button* closeAbout = new gics::Button(tr("闪了"));
closeAbout->setButtonType(gics::Button::monostable);
closeAbout->setMinimumSize(100, 30);
closeAbout->setMaximumSize(100, 30);
closeAbout->setDisplaySettings(gics::Button::showText);
closeAbout->setHorizontalSizePolicy(QSizePolicy::Fixed);
// Create the buttons layout
QGraphicsLinearLayout* buttonsLayout = new QGraphicsLinearLayout(Qt::Horizontal);
buttonsLayout->addItem(logo);
buttonsLayout->addStretch(1);
buttonsLayout->addItem(about);
buttonsLayout->addItem(quit);
buttonsLayout->setStretchFactor(about, 0);
buttonsLayout->setStretchFactor(quit, 0);
buttonsLayout->setAlignment(about, Qt::AlignCenter);
buttonsLayout->setAlignment(quit, Qt::AlignCenter);
// Create the "close about" button layout
QGraphicsLinearLayout* closeButtonLayout = new QGraphicsLinearLayout(Qt::Horizontal);
closeButtonLayout->addStretch(1);
closeButtonLayout->addItem(closeAbout);
closeButtonLayout->addStretch(1);
closeButtonLayout->setStretchFactor(closeAbout, 0);
// Create the front layout
QGraphicsLinearLayout* frontLayout = new QGraphicsLinearLayout(Qt::Vertical);
frontLayout->setSpacing(15);
frontLayout->addItem(title);
frontLayout->addItem(m_input);
frontLayout->addItem(m_output);
frontLayout->addItem(buttonsLayout);
frontLayout->setStretchFactor(title, 0);
frontLayout->setStretchFactor(m_input, 0);
frontLayout->setStretchFactor(m_output, 1);
frontLayout->setStretchFactor(buttonsLayout, 0);
// Create the back layout
QGraphicsLinearLayout* backLayout = new QGraphicsLinearLayout(Qt::Vertical);
backLayout->addItem(aboutTitle);
backLayout->addItem(aboutText);
backLayout->addItem(closeButtonLayout);
backLayout->setStretchFactor(aboutTitle, 0);
backLayout->setStretchFactor(aboutText, 1);
backLayout->setStretchFactor(closeButtonLayout, 0);
// Create the front panel
gics::Panel* front = new gics::Panel;
front->setSubClass("noframe");
front->background().setBorderMode(gics::Picture::fixed);
front->setCentralItem(frontLayout);
// Create the back panel
gics::Panel* back = new gics::Panel;
back->setSubClass("noframe");
back->background().setBorderMode(gics::Picture::fixed);
back->setCentralItem(backLayout);
// Create the view
m_view = new View(this);
m_view->setFrontInstrument(front);
m_view->setBackInstrument(back);
setCentralWidget(m_view);
// Set a default minimum size
setMinimumSize(600, 600);
// Connect signals
connect(about, SIGNAL(clicked()), m_view, SLOT(flip()));
connect(closeAbout, SIGNAL(clicked()), m_view, SLOT(flip()));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
connect(m_input, SIGNAL(valueChanged(double)), m_output, SLOT(setValue(double)));
parserClassRoom();
connect(m_input, SIGNAL(valueChanged(double)), this, SLOT(refreshhot(double)));
connect(m_input, SIGNAL(oneLucky()), this, SLOT(popOne()));
qsrand(QDateTime::currentDateTime().toTime_t());
#ifdef Q_OS_WIN32
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
#endif
}
MainWindow::~MainWindow()
{
#ifdef Q_OS_WIN32
CryptReleaseContext(hCryptProv, 0);
#endif
}
//-------------------------------------------------------------------------------------------------
void MainWindow::mousePressEvent(QMouseEvent* event)
{
// Reset the drag position
if (event->buttons() & Qt::LeftButton)
m_dragPosition = event->globalPos();
}
//-------------------------------------------------------------------------------------------------
void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
// Move the window accordingly
if (event->buttons() & Qt::LeftButton)
{
QPoint offset = event->globalPos() - m_dragPosition;
m_dragPosition = event->globalPos();
if (event->modifiers() & Qt::ControlModifier)
{
// Resize
resize(width() + offset.x(), height() + offset.y());
setMinimumSize(m_view->frontInstrument()->minimumSize().toSize());
}
else
{
// Move
move(x() + offset.x(), y() + offset.y());
}
}
}
//-------------------------------------------------------------------------------------------------
void MainWindow::keyPressEvent(QKeyEvent* event)
{
// Close the window if escape is pressed
if (event->key() == Qt::Key_Escape)
close();
else if (event->key() == Qt::Key_Space)
m_input->onKeySpace();
}
void MainWindow::parserClassRoom()
{
QString classfilename = QFileDialog::getOpenFileName(this,
tr("选择班级文件"), QString(), "XML files (*.xml)");
if (classfilename.isNull())
classfilename = "SampleClassroom.xml";
QFile* classfile = new QFile(classfilename, this);
QDomDocument classdocument;
if (classdocument.setContent(classfile))
{
QDomElement theelem = classdocument.documentElement();
m_classname = theelem.attribute("name");
title->setText("Attention! " + m_classname);
QDomNodeList memlistelm = classdocument.elementsByTagName("member");
int i;
for (i = 0; i < memlistelm.count(); i++)
{
theelem = memlistelm.item(i).toElement();
m_sids.append(theelem.attribute("id"));
m_names.append(theelem.text());
}
}
delete classfile;
}
void MainWindow::refreshhot(double value)
{
using namespace boost::numeric;
typedef converter<int,double,conversion_traits<int,double>,
def_overflow_handler, RoundEven<double>> HZround;
#ifdef Q_OS_WIN32
//--------------------------------------------------------------------
// Declare and initialize variables.
BYTE pbData[2];
CryptGenRandom(hCryptProv, 2, pbData);
quint16 randint = *((quint16 *) pbData) & RAND_MAX;
m_theone = HZround::convert((m_sids.size() - 1.) * randint / RAND_MAX);
#else
m_theone = HZround::convert((m_sids.size() - 1.) * qrand() / RAND_MAX);
#endif
if (m_theone < m_sids.size())
{
m_output->setID(m_sids.at(m_theone));
m_output->setName(m_names.at(m_theone));
}
}
void MainWindow::popOne()
{
if (m_sids.size() > 0)
{
m_output->pumpUpOne();
m_sids.removeAt(m_theone);
m_names.removeAt(m_theone);
}
}