-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemtimemodule.cpp
More file actions
203 lines (167 loc) · 6.01 KB
/
systemtimemodule.cpp
File metadata and controls
203 lines (167 loc) · 6.01 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
#include "systemtimemodule.h"
#include "ui_systemtimemodule.h"
#include "mainwindow.h"
#include "stylesheetgenerator.h"
#include <QtConcurrent>
#include <QTime>
#include <QFont>
class MainWindow;
class StopwatchManager;
SystemTimeModule::SystemTimeModule(QWidget *parent, MainWindow *mwr)
: QWidget(parent)
, ui(new Ui::SystemTimeModule)
, mw(mwr)
, isDeconstructing{false}
, enabled{mw->qsm.CheckIfClockEnabled()}
, refreshClockThread(QtConcurrent::run(&SystemTimeModule::RefreshClockThread, this))
, backgroundEnabled(mw->qsm.FetchIsClockBackgroundEnabled())
{
ui->setupUi(this);
connect(this, &SystemTimeModule::signalRefreshClock, this, &SystemTimeModule::updateClock);
rainbowModeIndex = mw->qsm.FetchClockRainbowModeIndex();
connect(&mw->swm,&StopwatchManager::updateClockRainbowBackgroundColor, this, &SystemTimeModule::updateRainbowBackgroundColor);
connect(&mw->swm,&StopwatchManager::updateClockRainbowFontColor, this, &SystemTimeModule::updateRainbowColor);
}
SystemTimeModule::~SystemTimeModule()
{
isDeconstructing = true;
refreshClockThread.cancel();
refreshClockThread.waitForFinished();
delete ui;
}
bool SystemTimeModule::event(QEvent *event)
{
return QWidget::event(event);
}
void SystemTimeModule::showEvent(QShowEvent *event)
{
event->accept();
systemClockFontColor = mw->qsm.FetchClockFontColor();
systemClockBackgroundColor = mw->qsm.FetchClockBackgroundColor();
refreshBackgroundState();
QString style = StylesheetGenerator::NewModuleOutputStylesheet(systemClockFontColor, systemClockBackgroundColor);
ui->SystemTimeLabel->setStyleSheet(style);
UpdateClockFont(mw->qsm.FetchClockFont(), mw->qsm.FetchClockFontSize());
}
void SystemTimeModule::closeEvent(QCloseEvent *event)
{
QString spos = QString("%1,%2").arg(this->pos().x()).arg(this->pos().y());
mw->qsm.setValue(QSettingsManager::LastClockPosition, spos);
mw->qsm.setValue(QSettingsManager::IsClockEnabled, QString("%1").arg(enabled));
event->accept();
}
void SystemTimeModule::mouseMoveEvent(QMouseEvent *event)
{
if (!event->buttons().testFlag(Qt::MouseButton::RightButton)) {
const QPointF delta = event->globalPosition() - oldPosition;
move(x()+delta.x(), y()+delta.y());
oldPosition = event->globalPosition();
}
}
void SystemTimeModule::mousePressEvent(QMouseEvent *event)
{
auto rawEvent = event;
auto button = event->button();
if (button != Qt::MouseButton::RightButton)
oldPosition = event->globalPosition();
else if (button == Qt::MouseButton::RightButton) {
mw->sie->setVisible(true);
}
}
void SystemTimeModule::ResizeClockToFitWindow()
{
ui->SystemTimeLabel->adjustSize();
QSize centralSize = ui->SystemTimeLabel->sizeHint();
// Calculate frame size difference (window decorations)
int frameWidth = this->frameGeometry().width() - this->geometry().width();
int frameHeight = this->frameGeometry().height() - this->geometry().height();
// Resize main window explicitly
this->resize(centralSize.width() + frameWidth,
centralSize.height() + frameHeight);
}
void SystemTimeModule::SetBackgroundEnabled(bool enabled)
{
backgroundEnabled = enabled;
}
void SystemTimeModule::RefreshClockThread()
{
qDebug() << "System Time Module initialized";
while (!isDeconstructing) {
QThread::msleep(1000);
QString currentTimeString = QTime::currentTime().toString("h:mm ap");
emit signalRefreshClock(currentTimeString);
}
}
void SystemTimeModule::UpdateClockFont(QString fontName, int fontSize)
{
ui->SystemTimeLabel->setFont(QFont(fontName, fontSize));
}
void SystemTimeModule::UpdateClockFontColor(QColor color)
{
systemClockFontColor = color;
ui->SystemTimeLabel->setStyleSheet(StylesheetGenerator::NewModuleOutputStylesheet(color));
}
void SystemTimeModule::UpdateClockBackgroundColor(QColor color)
{
systemClockBackgroundColor = color;
ui->SystemTimeLabel->setStyleSheet(StylesheetGenerator::NewModuleOutputStylesheet(systemClockFontColor,color));
}
QFont SystemTimeModule::GetCurrentFont()
{
return ui->SystemTimeLabel->font();
}
bool SystemTimeModule::CheckIfModuleIsEnabled()
{
return enabled;
}
void SystemTimeModule::RefreshModuleState()
{
this->setVisible(enabled);
}
int SystemTimeModule::GetCurrentFontSize()
{
return ui->SystemTimeLabel->font().pointSize();
}
void SystemTimeModule::updateClock(const QString &time)
{
ui->SystemTimeLabel->setText(time);
ResizeClockToFitWindow();
}
void SystemTimeModule::setLoadModule(bool shouldEnable)
{
enabled = shouldEnable;
RefreshModuleState();
}
void SystemTimeModule::refreshColorState(QColor color)
{
ui->SystemTimeLabel->update();
ui->SystemTimeLabel->repaint();
ui->SystemTimeLabel->show();
}
void SystemTimeModule::refreshBackgroundState()
{
if (!backgroundEnabled) {
ui->SystemTimeLabel->setAttribute(Qt::WA_NoSystemBackground);
ui->SystemTimeLabel->setAutoFillBackground(false);
ui->SystemTimeLabel->setStyleSheet(QString("%1%2").arg(ui->SystemTimeLabel->styleSheet()).arg("background-color: rgba(0, 0, 0, 0);"));
}
else {
ui->SystemTimeLabel->setAttribute(Qt::WA_NoSystemBackground, false);
}
ui->SystemTimeLabel->update();
ui->SystemTimeLabel->repaint();
ui->SystemTimeLabel->show();
}
void SystemTimeModule::updateRainbowColor(const QColor &color)
{
ui->SystemTimeLabel->setStyleSheet(StylesheetGenerator::NewModuleOutputStylesheet(color.name(),systemClockBackgroundColor.name()));
}
void SystemTimeModule::updateRainbowBackgroundColor(const QColor &color)
{
ui->SystemTimeLabel->setStyleSheet(StylesheetGenerator::NewModuleOutputStylesheet(systemClockFontColor.name(), color.name()));
}
void SystemTimeModule::UpdateRainbowMode(int index) {
rainbowModeIndex = index;
if (rainbowModeIndex == 0)
ui->SystemTimeLabel->setStyleSheet(StylesheetGenerator::NewModuleOutputStylesheet(systemClockFontColor.name(), systemClockBackgroundColor.name()));
}