-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwin.cpp
152 lines (119 loc) · 3.7 KB
/
mainwin.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
#include "mainwin.h"
#include <QtDebug>
#include <QTime>
#include <QSystemTrayIcon>
#include <QMessageBox>
#include "helpers.h"
MainWin::MainWin(Settings &settings, QWidget *parent) : QMainWindow(parent), settings_(settings), warning_activity_shown_(false), warning_pause_shown_(false), was_active_before_autopause_(false)
{
setupCentralWidget(settings);
setupIcon();
setWindowTitle("µTimer");
setWindowFlags(windowFlags() &(~Qt::WindowMaximizeButtonHint));
}
void MainWin::setupCentralWidget(Settings &settings)
{
content_widget_ = new ContentWidget(settings, this);
setCentralWidget(content_widget_);
QObject::connect(content_widget_, SIGNAL(pressedButton(Button)), this, SIGNAL(sendButtons(Button)));
QObject::connect(content_widget_, SIGNAL(minToTray()), this, SLOT(minToTray()));
QObject::connect(content_widget_, SIGNAL(toggleAlwaysOnTop()), this, SLOT(toggleAlwaysOnTop()));
}
void MainWin::setupIcon()
{
const QIcon icon(":/clock.png");
setWindowIcon(icon);
tray_icon_ = new QSystemTrayIcon(icon, this);
tray_icon_->setToolTip("Timing Inactive");
tray_icon_->show();
QObject::connect(tray_icon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void MainWin::updateAllTimes(qint64 t_active, qint64 t_pause)
{
content_widget_->setAllTimes(t_active, t_pause);
tray_icon_->setToolTip(content_widget_->getTooltip());
if((content_widget_->isGUIinActivity()) && (settings_.showTooMuchActivityWarning() || settings_.showTooMuchActivityWarning()))
showActivityWarnings(t_active, t_pause);
}
void MainWin::showActivityWarnings(const qint64 &t_active, const qint64 &t_pause)
{
if ((!warning_activity_shown_)
&& (t_active > settings_.getWarnTimeActivityMsec())) {
warning_activity_shown_ = true;
showMsgBox("Total activity time: " + convMSecToTimeStr(t_active));
}
if ((!warning_pause_shown_)
&& (t_active > settings_.getWarnTimeNoPauseMsec())
&& (t_pause < settings_.getPauseTimeForWarnTimeNoPauseMsec())) {
warning_pause_shown_ = true;
showMsgBox("Pause time: " + convMSecToTimeStr(t_pause) + "\nwith activity time: " + convMSecToTimeStr(t_active));
}
}
void MainWin::showMsgBox(const QString &text)
{
showMainWin();
QMessageBox msgBox(this);
msgBox.setWindowTitle("µTimer Warning");
msgBox.setText(text);
msgBox.setIcon(QMessageBox::Warning);
msgBox.exec();
}
void MainWin::reactOnLockState(LockEvent event)
{
if (settings_.isAutopauseEnabled()) {
if (event == LockEvent::LongOngoingLock) {
if (content_widget_->isGUIinActivity()) {
was_active_before_autopause_ = true;
content_widget_->setGUItoPause();
}
else {
was_active_before_autopause_ = false;
}
}
else if (event == LockEvent::Unlock) {
if (was_active_before_autopause_)
content_widget_->setGUItoActivity();
was_active_before_autopause_ = false;
}
}
}
void MainWin::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason != QSystemTrayIcon::DoubleClick)
return;
if (isVisible())
minToTray();
else
showMainWin();
}
void MainWin::showMainWin()
{
activateWindow();
show();
}
void MainWin::minToTray()
{
hide();
}
void MainWin::toggleAlwaysOnTop()
{
toggleAlwaysOnTopFlag();
showMainWin();
}
void MainWin::toggleAlwaysOnTopFlag()
{
setWindowFlags(windowFlags() ^ (Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
}
void MainWin::start()
{
if (settings_.isPinnedStartEnabled())
toggleAlwaysOnTopFlag();
if (settings_.isMinimizedStartEnabled())
minToTray();
else
showMainWin();
if (settings_.isAutostartTimingEnabled())
content_widget_->pressedStartPauseButton();
warning_activity_shown_ = !settings_.showTooMuchActivityWarning();
warning_pause_shown_ = !settings_.showNoPauseWarning();
}