-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
221 lines (194 loc) · 5.91 KB
/
Copy pathmainwindow.cpp
File metadata and controls
221 lines (194 loc) · 5.91 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
void CenterWindow(QMainWindow *w)
{
QScreen* s= QGuiApplication::screens()[0];
const QRect sr = s->availableGeometry();
const QRect wr({}, w->frameSize().boundedTo(sr.size()));
w->move(sr.center() - wr.center());
}
void MainWindow::CreateMenus()
{
// create a menu item for system tray icon
auto exitAction = new QAction(tr("&Exit"), this);
connect(exitAction, &QAction::triggered, [this]()
{
closing = true;
close();
});
DndAction = new QAction(tr("&DND"), this);
DndAction->setCheckable(true);
connect(DndAction, &QAction::triggered, [this]()
{
dnd = !dnd;
DndAction->setChecked(dnd);
});
MuteAction = new QAction(tr("&No sound"), this);
MuteAction->setCheckable(true);
connect(MuteAction, &QAction::triggered, [this]()
{
mute = !mute;
MuteAction->setChecked(mute);
});
CustomEventAction = new QAction(tr("&Add custom event"), this);
connect(CustomEventAction, &QAction::triggered, [this]()
{
DisplayCustomEventWindow();
});
// create a system tray menu and add the items to the menu
QMenu *trayIconMenu = CreateSystemTrayMenu();
trayIconMenu->addAction(DndAction);
trayIconMenu->addAction(MuteAction);
trayIconMenu->addSeparator();
// custom events
trayIconMenu->addAction(CustomEventAction);
customEventsMenu = trayIconMenu->addMenu("Custom events");
customEventsMenu->setEnabled(false);
// now we can add exit menu as the last item
trayIconMenu->addSeparator();
trayIconMenu->addAction(exitAction);
}
void MainWindow::DisplayCustomEventWindow()
{
bool wasHidden = false;
if (!this->isVisible())
{
wasHidden = true;
this->show();
}
AddCustomEvent *addCustomEventWindow = new AddCustomEvent(this) ;
auto result = addCustomEventWindow->exec();
if (result == QDialog::Accepted)
{
auto time = addCustomEventWindow->GetTime();
auto message = addCustomEventWindow->GetMessage();
auto caption = QString("%1->%2").arg(time, message);
QAction *addThis = new QAction(caption, this);
addThis->setStatusTip(message);
customEventsMenu->addAction(addThis);
customEventsMenu->setEnabled(true);
customEventsList.append(QPair<QString, QString>(time, message));
}
if (wasHidden)
this->hide();
}
QMenu *MainWindow::CreateSystemTrayMenu()
{
// read icon from resource
auto icon = QIcon(":/clock.ico");
// create system tray main menu
auto trayIconMenu = new QMenu(this);
// create system tray icon and add menu to it
sysTrayIcon = new QSystemTrayIcon(this);
sysTrayIcon->setContextMenu(trayIconMenu);
sysTrayIcon->setIcon(icon);
sysTrayIcon->setToolTip("Pomidoro timer");
sysTrayIcon->show();
// system tray icon click responder
connect(sysTrayIcon, &QSystemTrayIcon::activated, [this](auto reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
if (!isVisible())
{
show();
sysTrayIcon->hide();
CenterWindow(this);
activateWindow();
}
if (isMinimized())
{
showNormal();
}
}
});
return trayIconMenu;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// remove window title and make it stay-on-top
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
CreateMenus();
// center window and update clock
CenterWindow(this);
UpdateClock();
// create a timer to check current time
timer = new QTimer(this);
closing = false;
dnd = false;
connect(timer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::UpdateClock));
timer->start(1000);
firstWidth = this->size().width();
firstHeight = this->size().height();
int currentHour = QDateTime::currentDateTime().time().hour();
if (currentHour < 12)
ui->lblMessage->setText("Good Morning!");
else if (currentHour < 17)
ui->lblMessage->setText("Good Afternoon!");
else
ui->lblMessage->setText("Good Evening!");
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(closing) // check to see if we clicked on the Exit menu
{
event->accept();
}
else // otherwise just hide the window
{
sysTrayIcon->show();
this->hide();
event->ignore();
}
}
void MainWindow::UpdateClock()
{
// update time information on screen
QDateTime current = QDateTime::currentDateTime();
QString s = current.toString("HH:mm:ss");
ui->lblClock->setText(s);
bool newHour = s.endsWith("00:00");
bool isInCustomEvents = false;
QString message = "";
foreach(auto item, customEventsList)
{
if (item.first == s)
{
isInCustomEvents = true;
message = item.second;
break;
}
}
// check if it is the beginning of the new hour and the window is not visible
// also check to see if we are in DND mode
if (!dnd && (newHour || isInCustomEvents) && !isVisible())
{
ui->lblMessage->setText(message);
CenterWindow(this);
show();
sysTrayIcon->hide();
// load sound from resource
if (!mute)
{
QSoundEffect player(this);
player.setSource(QUrl("qrc:/shwup.wav"));
player.play();
// do not leave this method until the sound finishes
// if we leave the method before it finishes QSoundEffect will be destroyed and the sound will not be played
while (player.isPlaying())
QApplication::processEvents();
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_closeButton_clicked()
{
sysTrayIcon->show();
hide();
}