@@ -6,24 +6,41 @@ MainWindow::MainWindow(QWidget *parent)
6
6
, m_ui(new Ui::MainWindow)
7
7
, m_calls(0 )
8
8
, m_db()
9
+ , m_lock(false )
9
10
{
10
11
m_ui->setupUi (this );
11
12
12
13
connect (&m_db, &Database::error, this , &MainWindow::error);
14
+ connect (m_ui->actionAbout , &QAction::triggered, this , &MainWindow::about);
13
15
connect (m_ui->newButton , &QPushButton::clicked, this , &MainWindow::newCall);
16
+ connect (m_ui->removeButton , &QPushButton::clicked, this , &MainWindow::removeCall);
14
17
connect (m_ui->saveButton , &QPushButton::clicked, this , &MainWindow::saveCalls);
15
- connect (&m_timer, &QTimer::timeout, this , &MainWindow::setDateTime);
18
+ connect (&m_saverTimer, &QTimer::timeout, this , &MainWindow::saveCalls);
19
+ connect (&m_saverTimer1s, &QTimer::timeout, this , &MainWindow::setRemainingTimeLabel);
20
+ connect (&m_datetimeTimer, &QTimer::timeout, this , &MainWindow::setDateTime);
21
+ connect (m_ui->screenshotButton , &QPushButton::clicked, this , &MainWindow::saveScreenShot);
22
+ connect (&m_lockerTimer, &QTimer::timeout, this , &MainWindow::unlockNewButton);
16
23
17
24
m_username = qgetenv (" USER" );
18
25
if (m_username.isEmpty ())
19
26
m_username = qgetenv (" USERNAME" );
20
- m_ui->usernameLabel ->setText (" Usuario : " + m_username);
27
+ m_ui->usernameLabel ->setText (tr ( " User : " ) + m_username);
21
28
22
29
setDateTime ();
23
30
setTodaysCalls ();
24
31
25
- m_timer.setInterval (1'000 );
26
- m_timer.start ();
32
+ m_lockerTimer.setInterval (5'000 );
33
+ m_saverTimer.setInterval (300'000 ); // 5 minutes.
34
+ m_saverTimer1s.setInterval (1'000 );
35
+ m_datetimeTimer.setInterval (1'000 );
36
+ m_datetimeTimer.start ();
37
+ m_saverTimer.start ();
38
+ m_saverTimer1s.start ();
39
+
40
+ m_ui->newButton ->setShortcut (Qt::CTRL | Qt::Key_A);
41
+ m_ui->removeButton ->setShortcut (Qt::CTRL | Qt::Key_R);
42
+ m_ui->screenshotButton ->setShortcut (Qt::CTRL | Qt::Key_P);
43
+ m_ui->saveButton ->setShortcut (Qt::CTRL | Qt::Key_S);
27
44
}
28
45
29
46
MainWindow::~MainWindow ()
@@ -33,20 +50,29 @@ MainWindow::~MainWindow()
33
50
34
51
void MainWindow::setLabel ()
35
52
{
36
- m_ui->messageLabel ->setText (QString ( " Hoy has registrado %1 %2." )
37
- .arg (QString::number (m_calls), m_calls == 1 ? " llamada " : " llamadas " ));
53
+ m_ui->messageLabel ->setText (tr ( " You've registered %1 %2 today ." )
54
+ .arg (QString::number (m_calls), m_calls == 1 ? tr ( " call " ) : tr ( " calls " ) ));
38
55
}
39
56
40
57
void MainWindow::setDateTime ()
41
58
{
42
59
m_datetime = QDateTime::currentDateTime ().toString (" dd-MM-yyyy" );
43
- m_ui->datetimeLabel ->setText (QString (" Fecha: %1" ).arg (m_datetime));
60
+ auto time = QDateTime::currentDateTime ().toString (" hh:mm:ss" );
61
+ m_ui->datetimeLabel ->setText (tr (" Date: %1 %2" ).arg (m_datetime, time));
44
62
}
45
63
46
64
void MainWindow::setTodaysCalls ()
47
65
{
48
66
if (not m_db.open ())
49
67
return ;
68
+ // shouldUpdate() will help us because it checks if there's at least one record in the database.
69
+ // If there aren't any, we'll get 'not positioned on a valid record' error message, so to avoid this:
70
+ if (not shouldUpdate ()) {
71
+ m_db.close ();
72
+ setLabel ();
73
+ return ;
74
+ }
75
+
50
76
auto statement = QString (" SELECT id, calls FROM Calls WHERE date='%1' ORDER BY id DESC LIMIT 1" ).arg (m_datetime);
51
77
if (not m_db.exec (statement)) {
52
78
m_db.close ();
@@ -82,24 +108,70 @@ bool MainWindow::shouldUpdate()
82
108
return true ;
83
109
}
84
110
111
+ void MainWindow::about (bool checked)
112
+ {
113
+ Q_UNUSED (checked);
114
+ auto message = tr (" <p>Version: %1</p>\
115
+ <p>Author: %2</p>\
116
+ <a href=\" %3\" >This software</a><br>\
117
+ <a href=\" %4\" >Other projects</a>" ).arg (VERSION, AUTHOR, THIS_PROGRAM_URL, OTHER_PROJECTS);
118
+ QMessageBox msgBox (this );
119
+ msgBox.setWindowTitle (PROGRAM_NAME);
120
+ msgBox.setTextFormat (Qt::RichText);
121
+ msgBox.setText (message);
122
+ msgBox.exec ();
123
+ }
124
+
125
+ void MainWindow::unlockNewButton ()
126
+ {
127
+ m_lock = false ;
128
+ }
129
+
85
130
void MainWindow::newCall ()
86
131
{
132
+ if (m_lock) {
133
+ QMessageBox::warning (this , tr (" Warning" ), tr (" You have to wait a couple of secs to register a new call." ));
134
+ return ;
135
+ } else {
136
+ m_lock = true ;
137
+ m_lockerTimer.start ();
138
+ }
139
+
87
140
++m_calls;
88
141
setLabel ();
89
142
}
90
143
144
+ void MainWindow::removeCall ()
145
+ {
146
+ if (m_lock) {
147
+ QMessageBox::warning (this , tr (" Warning" ), tr (" You have to wait a couple of secs to remove a call." ));
148
+ return ;
149
+ } else {
150
+ m_lock = true ;
151
+ m_lockerTimer.start ();
152
+ }
153
+
154
+ if (m_calls == 0 ) {
155
+ error (tr (" Registered calls cannot be below zero." ));
156
+ return ;
157
+ }
158
+
159
+ --m_calls;
160
+ setLabel ();
161
+ }
162
+
91
163
void MainWindow::saveCalls ()
92
164
{
93
165
if (not m_db.open ())
94
166
return ;
95
167
96
- bool update = shouldUpdate ();
168
+ bool update { shouldUpdate () } ;
97
169
QString statement;
98
170
99
171
if (update) {
100
- // I prefer to use IDs in the WHERE clause, but we have no problem since datetime cannot be duplicated.
101
- statement = QString (" UPDATE Calls SET calls='%1' WHERE username ='%2' AND date ='%3'" )
102
- .arg (QString::number (m_calls), m_username, m_datetime );
172
+ // I prefer to use the id in the WHERE clause, but we have no problem since date cannot be duplicated.
173
+ statement = QString (" UPDATE Calls SET calls='%1' WHERE date ='%2' AND username ='%3'" )
174
+ .arg (QString::number (m_calls), m_datetime, m_username );
103
175
} else {
104
176
statement = QString (" INSERT INTO Calls (calls, date, username) VALUES ('%1', '%2', '%3')" )
105
177
.arg (QString::number (m_calls), m_datetime, m_username);
@@ -112,5 +184,59 @@ void MainWindow::saveCalls()
112
184
113
185
m_db.close ();
114
186
115
- QMessageBox::information (this , " Information" , " Calls have been saved." );
187
+ if (sender () == m_ui->saveButton ) {
188
+ QMessageBox::information (this , tr (" Information" ), tr (" Calls have been saved." ));
189
+ }
190
+
191
+ m_saverTimer.start ();
192
+ }
193
+
194
+ QPixmap MainWindow::takeScreenShot ()
195
+ {
196
+ QRect rect { geometry () };
197
+ int x { rect.x () };
198
+ int y { rect.y () };
199
+ int width { rect.width () };
200
+ int height { rect.height () };
201
+ QScreen* s { screen () };
202
+ QPixmap pixmap { s->grabWindow (0 , x, y, width, height) };
203
+ return pixmap;
204
+ }
205
+
206
+ void MainWindow::saveScreenShot ()
207
+ {
208
+ auto filename { QFileDialog::getSaveFileName (this , tr (" Save screenshot" ), tr (" Images (*.png *.jpg *.jpeg)" )) };
209
+ if (filename.isEmpty ()) {
210
+ QMessageBox::warning (this , tr (" Warning" ), tr (" Not saving screenshot." ));
211
+ return ;
212
+ }
213
+
214
+ if (not filename.endsWith (" .png" ) and not filename.endsWith (" .jpg" ) and not filename.endsWith (" .jpeg" ))
215
+ filename += " .png" ;
216
+
217
+ if (QFile file (filename); file.exists ()) {
218
+ auto answer = QMessageBox::question (this , tr (" Confirm" ), tr (" File %1 exists. Do you want to overload it?" ).arg (filename));
219
+ if (answer != QMessageBox::Yes)
220
+ return ;
221
+ }
222
+
223
+ auto pixmap { takeScreenShot () };
224
+ pixmap.save (filename, " PNG" );
225
+ QMessageBox::information (this , tr (" Information" ), tr (" Screenshot saved." ));
226
+ }
227
+
228
+ void MainWindow::setRemainingTimeLabel ()
229
+ {
230
+ int milliseconds { m_saverTimer.remainingTime () };
231
+ QString minutes;
232
+
233
+ {
234
+ int s = milliseconds / 1'000 ;
235
+ int m = s / 60 ;
236
+ minutes = QString::number (m);
237
+ }
238
+
239
+ auto label { tr (" Saving calls on %1 minute%2" ).arg (minutes, (minutes == " 1" ? " ." : " s." )) };
240
+ m_ui->remainingTimeLabel ->setText (label);
241
+ m_saverTimer1s.start ();
116
242
}
0 commit comments