Support chaging shadow color.
authorHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Sat, 9 Nov 2013 05:41:50 +0000 (13:41 +0800)
committerHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Sat, 9 Nov 2013 05:41:50 +0000 (13:41 +0800)
Set correct ranges on spiin buttons.

compton.conf.example
maindialog.cpp
maindialog.h
maindialog.ui

index c444b30..6ea9b69 100644 (file)
@@ -6,10 +6,10 @@ clear-shadow = true;
 shadow-radius = 7;
 shadow-offset-x = -7;
 shadow-offset-y = -7;
-shadow-opacity = 0.7;
-shadow-red = 0.0;
-shadow-green = 0.0;
-shadow-blue = 0.0;
+shadow-opacity = 0.7;
+shadow-red = 0.0;
+shadow-green = 0.0;
+shadow-blue = 0.0;
 shadow-exclude = [ "name = 'Notification'", "class_g = 'Conky'", "class_g ?= 'Notify-osd'", "class_g = 'Cairo-clock'" ];
 # shadow-exclude = "n:e:Notification";
 shadow-ignore-shaped = false;
@@ -19,11 +19,11 @@ shadow-ignore-shaped = false;
 # Opacity
 menu-opacity = 0.8;
 inactive-opacity = 0.8;
-active-opacity = 0.8;
+active-opacity = 0.8;
 frame-opacity = 0.7;
 inactive-opacity-override = false;
 alpha-step = 0.06;
-# inactive-dim = 0.2;
+inactive-dim = 0.0;
 # inactive-dim-fixed = true;
 # blur-background = true;
 # blur-background-frame = true;
index 5896ec3..cbc753d 100644 (file)
 #include <QFileInfo>
 #include <QDBusInterface>
 #include <QDBusConnection>
+#include <QDialogButtonBox>
+#include <QColorDialog>
+#include <QTimer>
 
 // dbus interface of compton
-#define COMPTON_SERVICE    "com.github.chjj.compton._0"
+#define COMPTON_SERVICE_PREFIX    "com.github.chjj.compton."
 #define COMPTON_PATH       "/"
 #define COMPTON_INTERFACE  "com.github.chjj.compton"
 
@@ -59,6 +62,15 @@ MainDialog::MainDialog(QString userConfigFile) {
   }
 
   // set up signal handlers and initial values of the controls
+  connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onDialogButtonClicked(QAbstractButton*)));
+  
+  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())
@@ -95,45 +107,24 @@ void MainDialog::onButtonToggled(bool checked) {
   qDebug() << "toggled: " << sender()->objectName();
   // generate config key from objectName.
   QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
-  config_setting_t* setting = config_lookup(&config_, keyName.constData());
-  if(!setting) { // setting not found
-    // add a new setting for it
-    config_setting_t* root = config_root_setting(&config_);
-    setting = config_setting_add(root, keyName.constData(), CONFIG_TYPE_BOOL);
-  }
-  // set the value
-  config_setting_set_bool(setting, checked);
-  saveConfig();
+  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();
-  config_setting_t* setting = config_lookup(&config_, keyName.constData());
-  if(!setting) { // setting not found
-    // add a new setting for it
-    config_setting_t* root = config_root_setting(&config_);
-    setting = config_setting_add(root, keyName.constData(), CONFIG_TYPE_FLOAT);
-  }
-  // set the value
-  config_setting_set_float(setting, d);
-  saveConfig();
+  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();
-  config_setting_t* setting = config_lookup(&config_, keyName.constData());
-  if(!setting) { // setting not found
-    // add a new setting for it
-    config_setting_t* root = config_root_setting(&config_);
-    setting = config_setting_add(root, keyName.constData(), CONFIG_TYPE_INT);
-  }
-  // set the value
-  config_setting_set_int(setting, i);
-  saveConfig();
+  configSetInt(keyName.constData(), i);
+  // saveConfig();
 }
 
 void MainDialog::saveConfig() {
@@ -145,9 +136,18 @@ void MainDialog::saveConfig() {
   config_write_file(&config_, userConfigFile_.toLocal8Bit().constData());
   
   // ask compton to reload the config
-  QDBusInterface iface(COMPTON_SERVICE, COMPTON_PATH, COMPTON_INTERFACE);
-  if(iface.isValid())
+  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
+    QTimer::singleShot(1000, this, SLOT(raise()));
+  }
   // 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.
@@ -156,3 +156,58 @@ void MainDialog::saveConfig() {
 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::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);
+}
index 17865e6..4c3694e 100644 (file)
@@ -29,6 +29,8 @@ namespace Ui
   class MainDialog;
 }
 
+class QAbstractButton;
+
 class MainDialog : public QDialog
 {
   Q_OBJECT
@@ -40,16 +42,24 @@ public:
 
 private:
   void saveConfig();
+  void updateShadowColorButton();
 
+  void configSetInt(const char* key, int val);
+  void configSetFloat(const char* key, double val);
+  void configSetBool(const char* key, bool val);
+  
 private Q_SLOTS:
   void onButtonToggled(bool checked);
   void onSpinValueChanged(double d);
   void onSpinValueChanged(int i);
+  void onDialogButtonClicked(QAbstractButton* button);
+  void onColorButtonClicked();
 
 private:
   Ui::MainDialog* ui;
   config_t config_;
   QString userConfigFile_;
+  QColor shadowColor_;
 };
 
 #endif // MAINDIALOG_H
index 788c941..5f9fb8f 100644 (file)
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>423</width>
+    <width>493</width>
     <height>397</height>
    </rect>
   </property>
         </widget>
        </item>
        <item row="5" column="1">
-        <widget class="QSpinBox" name="shadow_offset_x"/>
+        <widget class="QSpinBox" name="shadow_offset_x">
+         <property name="minimum">
+          <number>-99</number>
+         </property>
+        </widget>
        </item>
        <item row="6" column="0">
         <widget class="QLabel" name="shadow_offset_y_label">
         </widget>
        </item>
        <item row="6" column="1">
-        <widget class="QSpinBox" name="shadow_offset_y"/>
+        <widget class="QSpinBox" name="shadow_offset_y">
+         <property name="minimum">
+          <number>-99</number>
+         </property>
+        </widget>
        </item>
        <item row="7" column="0">
         <widget class="QLabel" name="shadow_opacity_label">
         </widget>
        </item>
        <item row="7" column="1">
-        <widget class="QDoubleSpinBox" name="shadow_opacity"/>
+        <widget class="QDoubleSpinBox" name="shadow_opacity">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
       </layout>
      </widget>
         </widget>
        </item>
        <item row="2" column="1">
-        <widget class="QDoubleSpinBox" name="active_opacity"/>
+        <widget class="QDoubleSpinBox" name="active_opacity">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="1" column="1">
-        <widget class="QDoubleSpinBox" name="inactive_opacity"/>
+        <widget class="QDoubleSpinBox" name="inactive_opacity">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="0" column="1">
-        <widget class="QDoubleSpinBox" name="menu_opacity"/>
+        <widget class="QDoubleSpinBox" name="menu_opacity">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="3" column="1">
-        <widget class="QDoubleSpinBox" name="frame_opacity"/>
+        <widget class="QDoubleSpinBox" name="frame_opacity">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="4" column="1">
-        <widget class="QDoubleSpinBox" name="alpha_step"/>
+        <widget class="QDoubleSpinBox" name="alpha_step">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.010000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="5" column="1">
-        <widget class="QDoubleSpinBox" name="inactive_dim"/>
+        <widget class="QDoubleSpinBox" name="inactive_dim">
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.050000000000000</double>
+         </property>
+        </widget>
        </item>
       </layout>
      </widget>
          </property>
         </widget>
        </item>
-       <item row="1" column="1">
-        <widget class="QSpinBox" name="fade_delta"/>
-       </item>
        <item row="2" column="1">
-        <widget class="QDoubleSpinBox" name="fade_in_step"/>
+        <widget class="QDoubleSpinBox" name="fade_in_step">
+         <property name="decimals">
+          <number>3</number>
+         </property>
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.010000000000000</double>
+         </property>
+         <property name="value">
+          <double>0.028000000000000</double>
+         </property>
+        </widget>
        </item>
        <item row="3" column="1">
-        <widget class="QDoubleSpinBox" name="fade_out_step"/>
+        <widget class="QDoubleSpinBox" name="fade_out_step">
+         <property name="decimals">
+          <number>3</number>
+         </property>
+         <property name="maximum">
+          <double>1.000000000000000</double>
+         </property>
+         <property name="singleStep">
+          <double>0.010000000000000</double>
+         </property>
+         <property name="value">
+          <double>0.030000000000000</double>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="1">
+        <widget class="QSpinBox" name="fade_delta"/>
        </item>
       </layout>
      </widget>
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="standardButtons">
-      <set>QDialogButtonBox::Close</set>
+      <set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel</set>
      </property>
     </widget>
    </item>