-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaindialog.cpp
187 lines (165 loc) · 6.12 KB
/
maindialog.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
#include "maindialog.h"
#include <DMessageManager>
#include <DTitlebar>
#include <QApplication>
#include <QCloseEvent>
#include <QFormLayout>
#include <QSettings>
#include <QVBoxLayout>
#include <systemd/sd-bus.h>
MainDialog::MainDialog(QList<BackLightInfo> &infos) : buffer(infos) {
auto title = titlebar();
title->setTitle(tr("ScreenLight"));
setWindowFlag(Qt::WindowMaximizeButtonHint, false);
QIcon icon(":/images/logo.svg");
setWindowIcon(icon);
setMinimumWidth(500);
auto w = new QWidget(this);
setCentralWidget(w);
auto layout = new QVBoxLayout(w);
layout->addWidget(new DLabel(tr("SelectToSet"), this), 0, Qt::AlignCenter);
layout->addSpacing(5);
QStringList backlights;
for (auto &item : infos) {
backlights.append(item.name);
}
combox = new DComboBox(this);
combox->addItems(backlights);
combox->setCurrentIndex(0);
layout->addWidget(combox);
layout->addSpacing(10);
svlbl = new DLabel(this);
layout->addWidget(svlbl, 0, Qt::AlignCenter);
slider = new DSlider(Qt::Horizontal, this);
slider->setMaximum(100);
slider->setMinimum(1);
slider->setPageStep(5);
slider->setIconSize(QSize(25, 25));
slider->setLeftIcon(icon);
auto fir = infos[0];
slider->setValue(fir.curBrightness * 100 / fir.maxBrightness);
svlbl->setText(QString::number(slider->value()) + " %");
layout->addWidget(slider);
layout->addSpacing(10);
layout->addWidget(
new DLabel(QString(500 / fontMetrics().horizontalAdvance('-'), '-'),
this),
0, Qt::AlignCenter);
layout->addSpacing(5);
auto flayout = new QFormLayout;
flayout->setMargin(5);
layout->addLayout(flayout);
spin = new DSpinBox(this);
spin->setRange(1, 20);
spin->setSuffix(" %");
spin->setValue(5);
spin->setToolTip(tr("Range1to20"));
flayout->addRow(tr("Interval"), spin);
hkLighter = new QHotkey(this);
seqLighter = new DKeySequenceEdit(this);
flayout->addRow(tr("Lighter"), seqLighter);
connect(seqLighter, &DKeySequenceEdit::keySequenceChanged, this,
[=](const QKeySequence &keySequence) {
if (!hkLighter->setShortcut(keySequence, true)) {
DMessageManager::instance()->sendMessage(
this, icon, tr("HotkeyErr:") + keySequence.toString());
seqLighter->clear();
}
});
connect(hkLighter, &QHotkey::activated, this, [=] {
auto interval = spin->value();
slider->setValue(slider->value() + interval);
});
hkDaker = new QHotkey(this);
seqDarker = new DKeySequenceEdit(this);
flayout->addRow(tr("Darker"), seqDarker);
connect(seqDarker, &DKeySequenceEdit::keySequenceChanged, this,
[=](const QKeySequence &keySequence) {
if (!hkDaker->setShortcut(keySequence, true)) {
DMessageManager::instance()->sendMessage(
this, icon, tr("HotkeyErr:") + keySequence.toString());
seqDarker->clear();
}
});
connect(hkDaker, &QHotkey::activated, this, [=] {
auto interval = spin->value();
slider->setValue(slider->value() - interval);
});
connect(combox, QOverload<int>::of(&DComboBox::currentIndexChanged), this,
[=](int index) {
curindex = index;
auto &fir = buffer[index];
slider->setValue(fir.curBrightness * 100 / fir.maxBrightness);
});
connect(slider, &DSlider::valueChanged, this, [=](int value) {
auto &fir = buffer[curindex];
fir.curBrightness = value * fir.maxBrightness / 100;
if (!this->setBrightness(fir.curBrightness)) {
DMessageManager::instance()->sendMessage(this, icon,
tr("SetBrightnessError"));
}
svlbl->setText(QString::number(value) + " %");
});
auto menu = new DMenu(this);
aLighter = new QAction(tr("EnabledLighter"), menu);
aLighter->setCheckable(true);
connect(aLighter, &QAction::toggled, this,
[=](bool v) { hkLighter->setRegistered(v); });
menu->addAction(aLighter);
aDarker = new QAction(tr("EnabledDarker"), menu);
aDarker->setCheckable(true);
connect(aDarker, &QAction::toggled, this,
[=](bool v) { hkDaker->setRegistered(v); });
menu->addAction(aDarker);
title->setMenu(menu);
loadSettings();
}
bool MainDialog::setBrightness(int value) {
sd_bus *bus = nullptr;
int r = sd_bus_default_system(&bus);
if (r < 0) {
fprintf(stderr, "Can't connect to system bus: %s\n", strerror(-r));
return false;
}
r = sd_bus_call_method(
bus, "org.freedesktop.login1", "/org/freedesktop/login1/session/auto",
"org.freedesktop.login1.Session", "SetBrightness", nullptr, nullptr,
"ssu", "backlight", combox->currentText().toLocal8Bit().constData(),
value);
if (r < 0)
fprintf(stderr, "Failed to set brightness: %s\n", strerror(-r));
sd_bus_unref(bus);
return r >= 0;
}
void MainDialog::loadSettings() {
QSettings settings(QApplication::organizationName(),
QApplication::applicationName());
spin->setValue(settings.value("Interval", 5).toInt());
seqLighter->setKeySequence(
settings
.value("HkLighter", QKeySequence(Qt::ControlModifier |
Qt::AltModifier | Qt::UpArrow))
.value<QKeySequence>());
seqDarker->setKeySequence(
settings
.value("HkDarker", QKeySequence(Qt::ControlModifier |
Qt::AltModifier | Qt::DownArrow))
.value<QKeySequence>());
aLighter->setChecked(settings.value("EnLighter", true).value<bool>());
aDarker->setChecked(settings.value("EnDarker", true).value<bool>());
}
void MainDialog::saveSettings() {
QSettings settings(QApplication::organizationName(),
QApplication::applicationName());
settings.setValue("Interval", spin->value());
settings.setValue("HkLighter", hkLighter->shortcut());
settings.setValue("HkDarker", hkDaker->shortcut());
settings.setValue("EnLighter", aLighter->isChecked());
settings.setValue("EnDarker", aDarker->isChecked());
}
void MainDialog::setScreenBrightness(int percent) { slider->setValue(percent); }
void MainDialog::closeEvent(QCloseEvent *event) {
event->ignore();
hide();
saveSettings();
}