forked from lxqt/compton-conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maindialog.cpp
218 lines (196 loc) · 8.05 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
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
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright (C) 2013 <copyright holder> <email>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "maindialog.h"
#include "ui_maindialog.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDBusInterface>
#include <QDBusConnection>
#include <QDialogButtonBox>
#include <QColorDialog>
#include <QMessageBox>
// dbus interface of compton
#define COMPTON_SERVICE_PREFIX "com.github.chjj.compton."
#define COMPTON_PATH "/"
#define COMPTON_INTERFACE "com.github.chjj.compton"
MainDialog::MainDialog(QString userConfigFile) {
ui = new Ui::MainDialog;
ui->setupUi(this);
if(userConfigFile.isEmpty()) {
userConfigFile_ = qgetenv("XDG_CONFIG_HOME");
if(userConfigFile_.isEmpty()) {
userConfigFile_ = QDir::homePath();
userConfigFile_ += "/.config";
}
// QDir configDir = QDir(userConfigFile);
// if(!configDir.exists())
userConfigFile_ += "/compton.conf";
}
else
userConfigFile_ = userConfigFile;
config_init(&config_);
if(config_read_file(&config_, userConfigFile_.toLocal8Bit().constData()) == CONFIG_FALSE) {
// loading user config file failed
// try our default example
qDebug() << "load fail, try " << COMPTON_CONF_DATA_DIR << "/compton.conf.example";
config_read_file(&config_, COMPTON_CONF_DATA_DIR "/compton.conf.example");
}
// set up signal handlers and initial values of the controls
connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onDialogButtonClicked(QAbstractButton*)));
connect(ui->aboutButton, SIGNAL(clicked(bool)), SLOT(onAboutButtonClicked()));
connect(ui->shadow_color, SIGNAL(clicked(bool)), SLOT(onColorButtonClicked()));
double color;
shadowColor_.setRedF(config_lookup_float(&config_, "shadow-red", &color) == CONFIG_TRUE ? color : 0.0);
shadowColor_.setGreenF(config_lookup_float(&config_, "shadow-green", &color) == CONFIG_TRUE ? color : 0.0);
shadowColor_.setBlueF(config_lookup_float(&config_, "shadow-blue", &color) == CONFIG_TRUE ? color : 0.0);
updateShadowColorButton();
// objectNames are kept the same as config file key names.
Q_FOREACH(QWidget* child, findChildren<QWidget*>()) {
if(!child->isWidgetType() || child->objectName().isEmpty())
continue;
// objectName uses _ while config file keys uses - as separator.
QByteArray keyName = child->objectName().replace('_', '-').toLatin1(); // generate config key from objectName.
if(child->inherits("QCheckBox")) {
int val = -1;
if(config_lookup_bool(&config_, keyName.constData(), &val) == CONFIG_TRUE)
static_cast<QCheckBox*>(child)->setChecked((bool)val);
connect(child, SIGNAL(toggled(bool)), SLOT(onButtonToggled(bool)));
}
else if(child->inherits("QDoubleSpinBox")) {
double val;
if(config_lookup_float(&config_, keyName.constData(), &val) == CONFIG_TRUE)
static_cast<QDoubleSpinBox*>(child)->setValue(val);
connect(child, SIGNAL(valueChanged(double)), SLOT(onSpinValueChanged(double)));
}
else if(child->inherits("QSpinBox")) {
int val;
if(config_lookup_int(&config_, keyName.constData(), &val) == CONFIG_TRUE)
static_cast<QSpinBox*>(child)->setValue(val);
connect(child, SIGNAL(valueChanged(int)), SLOT(onSpinValueChanged(int)));
}
}
}
MainDialog::~MainDialog() {
config_destroy(&config_);
delete ui;
}
void MainDialog::onButtonToggled(bool checked) {
qDebug() << "toggled: " << sender()->objectName();
// generate config key from objectName.
QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
configSetBool(keyName.constData(), checked);
// saveConfig();
}
void MainDialog::onSpinValueChanged(double d) {
qDebug() << "changed: " << sender()->objectName() << ": " << d;
// generate config key from objectName.
QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
configSetFloat(keyName.constData(), d);
// saveConfig();
}
void MainDialog::onSpinValueChanged(int i) {
qDebug() << "changed: " << sender()->objectName() << ": " << i;
// generate config key from objectName.
QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
configSetInt(keyName.constData(), i);
// saveConfig();
}
void MainDialog::saveConfig() {
// ensure the existance of user config dir
QString configDir = QFileInfo(userConfigFile_).dir().path();
QDir().mkpath(configDir);
qDebug() << userConfigFile_;
// save the config file
config_write_file(&config_, userConfigFile_.toLocal8Bit().constData());
// ask compton to reload the config
QString displayName = qgetenv("DISPLAY");
for(int i = 0; i < displayName.length(); ++i) {
if(!displayName[i].isNumber()) // replace non-numeric chars with _
displayName[i] = '_';
}
QString comptonServiceName = COMPTON_SERVICE_PREFIX + displayName;
QDBusInterface iface(comptonServiceName, COMPTON_PATH, COMPTON_INTERFACE);
if(iface.isValid()) {
iface.call("reset");
// raise ourself to the top again (we'll loosing focus after reloading compton)
activateWindow();
}
// FIXME: dbus interface of compton is not always available and reset() creates
// much flickers. Maybe we should use internal dbus method set_opts().
// Or, we can patch compton to do what we want.
}
void MainDialog::done(int res) {
QDialog::done(res);
}
void MainDialog::onDialogButtonClicked(QAbstractButton* button) {
if(ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
saveConfig();
}
}
void MainDialog::onColorButtonClicked() {
QColorDialog dlg(shadowColor_);
dlg.setOption(QColorDialog::ShowAlphaChannel, false);
if(dlg.exec() == QDialog::Accepted) {
shadowColor_ = dlg.selectedColor();
updateShadowColorButton();
configSetFloat("shadow-red", shadowColor_.redF());
configSetFloat("shadow-green", shadowColor_.greenF());
configSetFloat("shadow-blue", shadowColor_.blueF());
}
}
void MainDialog::onAboutButtonClicked() {
QMessageBox::about(this, tr("About ComptonConf"),
tr("ComptonConf - configuration tool for compton\n\nCopyright (C) 2013\nAuthor: Hong Jen Yee (PCMan) <[email protected]>"));
}
void MainDialog::updateShadowColorButton() {
QString qss = QString("QPushButton {"
"background-color:%1;"
"}").arg(shadowColor_.name());
ui->shadow_color->setStyleSheet(qss);
}
void MainDialog::configSetInt(const char* key, int val) {
config_setting_t* setting = config_lookup(&config_, key);
if(!setting) { // setting not found
// add a new setting for it
config_setting_t* root = config_root_setting(&config_);
setting = config_setting_add(root, key, CONFIG_TYPE_INT);
}
config_setting_set_int(setting, val);
}
void MainDialog::configSetFloat(const char* key, double val) {
config_setting_t* setting = config_lookup(&config_, key);
if(!setting) { // setting not found
// add a new setting for it
config_setting_t* root = config_root_setting(&config_);
setting = config_setting_add(root, key, CONFIG_TYPE_FLOAT);
}
config_setting_set_float(setting, val);
}
void MainDialog::configSetBool(const char* key, bool val) {
config_setting_t* setting = config_lookup(&config_, key);
if(!setting) { // setting not found
// add a new setting for it
config_setting_t* root = config_root_setting(&config_);
setting = config_setting_add(root, key, CONFIG_TYPE_BOOL);
}
config_setting_set_bool(setting, val);
}