Support chaging shadow color.
[lxde/compton-conf.git] / maindialog.cpp
CommitLineData
012d3267
HJYP
1/*
2 * <one line to give the library's name and an idea of what it does.>
3 * Copyright (C) 2013 <copyright holder> <email>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21#include "maindialog.h"
22#include "ui_maindialog.h"
23#include <QDebug>
c3b7cf80
HJYP
24#include <QDir>
25#include <QFile>
26#include <QFileInfo>
27#include <QDBusInterface>
28#include <QDBusConnection>
b6646b0a
HJYP
29#include <QDialogButtonBox>
30#include <QColorDialog>
31#include <QTimer>
012d3267 32
c3b7cf80 33// dbus interface of compton
b6646b0a 34#define COMPTON_SERVICE_PREFIX "com.github.chjj.compton."
c3b7cf80
HJYP
35#define COMPTON_PATH "/"
36#define COMPTON_INTERFACE "com.github.chjj.compton"
37
38
39MainDialog::MainDialog(QString userConfigFile) {
012d3267
HJYP
40 ui = new Ui::MainDialog;
41 ui->setupUi(this);
42
c3b7cf80
HJYP
43 if(userConfigFile.isEmpty()) {
44 userConfigFile_ = qgetenv("XDG_CONFIG_HOME");
45 if(userConfigFile_.isEmpty()) {
46 userConfigFile_ = QDir::homePath();
47 userConfigFile_ += "/.config";
48 }
49 // QDir configDir = QDir(userConfigFile);
50 // if(!configDir.exists())
51 userConfigFile_ += "/compton.conf";
52 }
53 else
54 userConfigFile_ = userConfigFile;
55
56 config_init(&config_);
57 if(config_read_file(&config_, userConfigFile_.toLocal8Bit().constData()) == CONFIG_FALSE) {
58 // loading user config file failed
59 // try our default example
60 qDebug() << "load fail, try " << COMPTON_CONF_DATA_DIR << "/compton.conf.example";
61 config_read_file(&config_, COMPTON_CONF_DATA_DIR "/compton.conf.example");
62 }
63
64 // set up signal handlers and initial values of the controls
b6646b0a
HJYP
65 connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onDialogButtonClicked(QAbstractButton*)));
66
67 connect(ui->shadow_color, SIGNAL(clicked(bool)), SLOT(onColorButtonClicked()));
68 double color;
69 shadowColor_.setRedF(config_lookup_float(&config_, "shadow-red", &color) == CONFIG_TRUE ? color : 0.0);
70 shadowColor_.setGreenF(config_lookup_float(&config_, "shadow-green", &color) == CONFIG_TRUE ? color : 0.0);
71 shadowColor_.setBlueF(config_lookup_float(&config_, "shadow-blue", &color) == CONFIG_TRUE ? color : 0.0);
72 updateShadowColorButton();
73
c3b7cf80 74 // objectNames are kept the same as config file key names.
012d3267
HJYP
75 Q_FOREACH(QWidget* child, findChildren<QWidget*>()) {
76 if(!child->isWidgetType() || child->objectName().isEmpty())
77 continue;
c3b7cf80
HJYP
78 // objectName uses _ while config file keys uses - as separator.
79 QByteArray keyName = child->objectName().replace('_', '-').toLatin1(); // generate config key from objectName.
012d3267 80 if(child->inherits("QCheckBox")) {
c3b7cf80
HJYP
81 int val = -1;
82 if(config_lookup_bool(&config_, keyName.constData(), &val) == CONFIG_TRUE)
012d3267
HJYP
83 static_cast<QCheckBox*>(child)->setChecked((bool)val);
84 connect(child, SIGNAL(toggled(bool)), SLOT(onButtonToggled(bool)));
85 }
86 else if(child->inherits("QDoubleSpinBox")) {
87 double val;
c3b7cf80 88 if(config_lookup_float(&config_, keyName.constData(), &val) == CONFIG_TRUE)
012d3267
HJYP
89 static_cast<QDoubleSpinBox*>(child)->setValue(val);
90 connect(child, SIGNAL(valueChanged(double)), SLOT(onSpinValueChanged(double)));
91 }
92 else if(child->inherits("QSpinBox")) {
93 int val;
c3b7cf80 94 if(config_lookup_int(&config_, keyName.constData(), &val) == CONFIG_TRUE)
012d3267
HJYP
95 static_cast<QSpinBox*>(child)->setValue(val);
96 connect(child, SIGNAL(valueChanged(int)), SLOT(onSpinValueChanged(int)));
97 }
98 }
99}
100
101MainDialog::~MainDialog() {
c3b7cf80 102 config_destroy(&config_);
012d3267
HJYP
103 delete ui;
104}
105
106void MainDialog::onButtonToggled(bool checked) {
107 qDebug() << "toggled: " << sender()->objectName();
c3b7cf80
HJYP
108 // generate config key from objectName.
109 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
110 configSetBool(keyName.constData(), checked);
111 // saveConfig();
012d3267
HJYP
112}
113
114void MainDialog::onSpinValueChanged(double d) {
115 qDebug() << "changed: " << sender()->objectName() << ": " << d;
c3b7cf80
HJYP
116 // generate config key from objectName.
117 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
118 configSetFloat(keyName.constData(), d);
119 // saveConfig();
012d3267
HJYP
120}
121
122void MainDialog::onSpinValueChanged(int i) {
123 qDebug() << "changed: " << sender()->objectName() << ": " << i;
c3b7cf80
HJYP
124 // generate config key from objectName.
125 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
126 configSetInt(keyName.constData(), i);
127 // saveConfig();
c3b7cf80
HJYP
128}
129
130void MainDialog::saveConfig() {
131 // ensure the existance of user config dir
132 QString configDir = QFileInfo(userConfigFile_).dir().path();
133 QDir().mkpath(configDir);
134 qDebug() << userConfigFile_;
135 // save the config file
136 config_write_file(&config_, userConfigFile_.toLocal8Bit().constData());
137
138 // ask compton to reload the config
b6646b0a
HJYP
139 QString displayName = qgetenv("DISPLAY");
140 for(int i = 0; i < displayName.length(); ++i) {
141 if(!displayName[i].isNumber()) // replace non-numeric chars with _
142 displayName[i] = '_';
143 }
144 QString comptonServiceName = COMPTON_SERVICE_PREFIX + displayName;
145 QDBusInterface iface(comptonServiceName, COMPTON_PATH, COMPTON_INTERFACE);
146 if(iface.isValid()) {
c3b7cf80 147 iface.call("reset");
b6646b0a
HJYP
148 // raise ourself to the top again
149 QTimer::singleShot(1000, this, SLOT(raise()));
150 }
c3b7cf80
HJYP
151 // FIXME: dbus interface of compton is not always available and reset() creates
152 // much flickers. Maybe we should use internal dbus method set_opts().
153 // Or, we can patch compton to do what we want.
154}
155
156void MainDialog::done(int res) {
157 QDialog::done(res);
012d3267 158}
b6646b0a
HJYP
159
160void MainDialog::onDialogButtonClicked(QAbstractButton* button) {
161 if(ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
162 saveConfig();
163 }
164}
165
166void MainDialog::onColorButtonClicked() {
167 QColorDialog dlg(shadowColor_);
168 dlg.setOption(QColorDialog::ShowAlphaChannel, false);
169 if(dlg.exec() == QDialog::Accepted) {
170 shadowColor_ = dlg.selectedColor();
171 updateShadowColorButton();
172 configSetFloat("shadow-red", shadowColor_.redF());
173 configSetFloat("shadow-green", shadowColor_.greenF());
174 configSetFloat("shadow-blue", shadowColor_.blueF());
175 }
176}
177
178void MainDialog::updateShadowColorButton() {
179 QString qss = QString("QPushButton {"
180 "background-color:%1;"
181 "}").arg(shadowColor_.name());
182 ui->shadow_color->setStyleSheet(qss);
183}
184
185void MainDialog::configSetInt(const char* key, int val) {
186 config_setting_t* setting = config_lookup(&config_, key);
187 if(!setting) { // setting not found
188 // add a new setting for it
189 config_setting_t* root = config_root_setting(&config_);
190 setting = config_setting_add(root, key, CONFIG_TYPE_INT);
191 }
192 config_setting_set_int(setting, val);
193}
194
195void MainDialog::configSetFloat(const char* key, double val) {
196 config_setting_t* setting = config_lookup(&config_, key);
197 if(!setting) { // setting not found
198 // add a new setting for it
199 config_setting_t* root = config_root_setting(&config_);
200 setting = config_setting_add(root, key, CONFIG_TYPE_FLOAT);
201 }
202 config_setting_set_float(setting, val);
203}
204
205void MainDialog::configSetBool(const char* key, bool val) {
206 config_setting_t* setting = config_lookup(&config_, key);
207 if(!setting) { // setting not found
208 // add a new setting for it
209 config_setting_t* root = config_root_setting(&config_);
210 setting = config_setting_add(root, key, CONFIG_TYPE_BOOL);
211 }
212 config_setting_set_bool(setting, val);
213}