-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
260 lines (231 loc) · 6.49 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
/*
| author: Christian Balles
| email: code@ballessay.de
| date: 2009/11/28
| version: 0.1
| copyright 2009 Christian Balles
|
| This file is part of qspeedreader.
| qspeedreader is free software: you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation, either version 2 of the License, or (at your option)
| any later version.
| qspeedreader is distributed in the hope that it will be useful, but
| WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
| or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
| more details.
| You should have received a copy of the GNU General Public License along with
| qspeedreader. If not, see http://www.gnu.org/licenses/.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "colordialog.h"
#include <QClipboard>
#include <QFontDialog>
#include <QMouseEvent>
#include <QFile>
CMainWindow::CMainWindow(QWidget* _pParent)
: QMainWindow(_pParent),
m_pUi(new Ui::CMainWindow),
m_index(0)
{
// setup the stuff created with designer
m_pUi->setupUi(this);
m_pUi->pTextLabel->setFont(m_settings.font());
// get the labels palette
QPalette p(m_pUi->pTextLabel->palette());
// set background and foreground color
p.setColor(QPalette::Background, m_settings.background());
p.setColor(QPalette::Foreground, m_settings.foreground());
// set modified palette
m_pUi->pTextLabel->setPalette(p);
// set words per minute value
m_pUi->pWPMspinBox->setValue(m_settings.wpm());
// set check box value
m_pUi->pRepeatCheckBox->setChecked(m_settings.repeat());
// connect the timer to the local slot
connect(&m_timer, SIGNAL(timeout()),
this, SLOT(displayNext()));
// connect menu actions and extra stuff
connectMenu();
//hide status bar
statusBar()->setVisible(false);
// read stdin
QFile file;
if(file.open(0, QIODevice::ReadOnly) && file.bytesAvailable())
{
m_tokens = QString(file.readAll()).split(" ");
file.close();
}
// quick-n-dirty solution ---------------
// get arguments
QStringList args = qApp->arguments();
for( int i = 0; i < args.size(); ++i)
{
// get i-th argument
QString arg = args.at(i);
// check for option
if(arg == "-f" || arg == "--fullscreen")
{
toggleFullscreen();
}
if(arg == "-h" || arg == "--help")
{
qDebug("./qspeedreader -h || -f");
qApp->quit();
}
}
// if we have tokens at this moment -> start for one run
if(0 < m_tokens.size())
{
on_pStartButton_clicked(true);
connect(this, SIGNAL(finished()),
this, SLOT(close()));
}
}
CMainWindow::~CMainWindow()
{
delete m_pUi;
}
void CMainWindow::on_pStartButton_clicked(bool _checked)
{
if(_checked || !m_timer.isActive())
{
m_timer.setInterval(m_settings.interval());
m_settings.save();
// get text from clipboard and split it to a stringlist
m_tokens = QApplication::clipboard()->text().split(" ");
// init the index
m_index = 0;
// start the timer
m_timer.start();
// change the button text
m_pUi->pStartButton->setText(tr("Stop"));
m_pUi->pStartButton->setChecked(true);
}
else
{
// stop the timer
m_timer.stop();
// change the button text
m_pUi->pStartButton->setText(tr("Start"));
m_pUi->pStartButton->setChecked(false);
}
}
void CMainWindow::on_pFontButton_clicked()
{
// create dialog, set old font, if OK -> set the new font
QFontDialog dialog;
dialog.setCurrentFont(m_settings.font());
if( dialog.exec())
{
// if Ok pressed -> set and save the font
m_pUi->pTextLabel->setFont(dialog.currentFont());
m_settings.setFont(dialog.currentFont());
m_settings.save();
}
}
void CMainWindow::on_pWPMspinBox_valueChanged(int _value)
{
m_settings.setWPM(_value);
// set the new interval on demand
m_timer.setInterval(m_settings.interval());
}
void CMainWindow::on_pColorsButton_clicked()
{
CColorDialog dialog( m_settings.foreground(), m_settings.background());
connect( &dialog, SIGNAL(backgroundChanged(const QColor&)),
this, SLOT(backgroundChanged(const QColor&)));
connect( &dialog, SIGNAL(foregroundChanged(const QColor&)),
this, SLOT(foregroundChanged(const QColor&)));
if(dialog.exec())
{
m_settings.setBackground(dialog.background());
m_settings.setForeground(dialog.foreground());
}
}
void CMainWindow::on_pRepeatCheckBox_clicked(bool _checked)
{
m_settings.setRepeat(_checked);
}
void CMainWindow::displayNext()
{
// display the text at index, then increment index
m_pUi->pTextLabel->setText(m_tokens.at(m_index++));
// if index has reached the last element
if(m_index >= m_tokens.size())
{
// signal that the run is finished
emit finished();
// if repeating is set...
if(m_settings.repeat())
{
//...start from begin
m_index = 0;
}
else
{
// stop the timer
m_timer.stop();
// set the button unchecked
m_pUi->pStartButton->setChecked(false);
// change the text of the button
m_pUi->pStartButton->setText(tr("Start"));
}
}
}
void CMainWindow::backgroundChanged(const QColor& _color)
{
m_settings.setBackground(_color);
QPalette p(m_pUi->pTextLabel->palette());
p.setColor(QPalette::Background, m_settings.background());
m_pUi->pTextLabel->setPalette(p);
}
void CMainWindow::foregroundChanged(const QColor& _color)
{
m_settings.setForeground(_color);
QPalette p(m_pUi->pTextLabel->palette());
p.setColor(QPalette::Foreground, m_settings.foreground());
m_pUi->pTextLabel->setPalette(p);
}
void CMainWindow::toggleFullscreen()
{
if(isFullScreen())
{
// set the last saved state
setWindowState(m_lastState);
}
else
{
// save current state
m_lastState = windowState();
// set fullscreen
setWindowState(Qt::WindowFullScreen);
}
}
void CMainWindow::quitApplication()
{
close();
}
// standard method for retranslation at runtime
void CMainWindow::changeEvent(QEvent* _pEvent)
{
QMainWindow::changeEvent(_pEvent);
switch (_pEvent->type())
{
case QEvent::LanguageChange:
m_pUi->retranslateUi(this);
break;
default:
break;
}
}
void CMainWindow::connectMenu()
{
connect(m_pUi->actionQuit, SIGNAL(triggered()),
this, SLOT(quitApplication()));
connect(m_pUi->actionFullscreen, SIGNAL(triggered()),
this, SLOT(toggleFullscreen()));
connect(m_pUi->actionRun, SIGNAL(triggered(bool)),
this, SLOT(on_pStartButton_clicked(bool)));
}