zjes avatar zjes committed 40da1db

Change color select control, add project color

Comments (0)

Files changed (13)

     return static_cast<PynotoApplication*>(QApplication::instance());
 }
 
+void emitPreferencesChanged(const QString& name)
+{
+    app()->emitPreferencesChanged(name);
+}
+
+void emitPreferencesChanged(const QString& name, const QVariant& value)
+{
+    app()->emitPreferencesChanged(name, value);
+}
+
 }
 
 PynotoApplication::PynotoApplication(int &argc, char **argv, int flag):
     delete pluginManager;
 }
 
+void PynotoApplication::emitPreferencesChanged(const QString& name)
+{
+    emit preferencesChanged(name);
+}
 
+void PynotoApplication::emitPreferencesChanged(const QString& name, const QVariant& value)
+{
+    emit preferencesChanged(name, value);
+}
 

src/Core/ColorChooser.cpp

+#include <QDebug>
 #include <QPainter>
 #include <QColorDialog>
+#include <QStyle>
+#include <QStyleOptionButton>
+#include <QMenu>
 #include "ColorChooser.h"
 
 ColorChooser::ColorChooser(QWidget* parent):
-    QWidget(parent),
-    _color(QColor(0, 0, 0))
+    QToolButton(parent)
 {
+    QMenu * menu = new QMenu(this);
+    menu->addAction(tr("Reset"), this, SLOT(colorReset()));
+    setMenu(menu);
+    setPopupMode(QToolButton::MenuButtonPopup);
+    setColor(QColor(0, 0, 0, 0));
+    connect(this, SIGNAL(clicked()), SLOT(showDialog()));
 }
 
 ColorChooser::~ColorChooser()
 {
-
 }
 
 QColor ColorChooser::color()
 void ColorChooser::setColor(QColor color)
 {
     _color = color;
-    update();
+    setText(_color.alpha() == 0 ? tr("Transparent") : _color.name().toUpper());
 }
 
-void ColorChooser::paintEvent(QPaintEvent *)
+bool ColorChooser::isLight()
 {
-    QPainter paint(this);
-    if (isEnabled()){
-        paint.fillRect(rect(), _color);
-        paint.setPen(QColor(255 - _color.red(), 255 - _color.green(), 255 - _color.blue()));
-        paint.drawText(rect(), Qt::AlignCenter, _color.name().toUpper());
-        paint.setPen(Qt::black);
-    } else {
-        QPalette pal = palette();
-        paint.fillRect(rect(), pal.color(QPalette::Window));
-        paint.setPen(Qt::darkGray);
-    }
-    paint.drawRect(rect().adjusted(0, 0, -1, -1));
+    int yiq = ((_color.red()*299)+(_color.green()*587)+(_color.blue()*114))/1000;
+    return yiq >= 128;
 }
 
-void ColorChooser::mousePressEvent(QMouseEvent *)
+void ColorChooser::paintEvent(QPaintEvent *evt)
 {
-    showDialog();
+    QToolButton::paintEvent(evt);
+    if (_color.alpha() != 0){
+        QPainter paint(this);
+        QStyleOptionButton option;
+        option.initFrom(this);
+        option.state = isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
+        option.text = text();
+        option.icon = icon();
+        int menuWidth = style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &option, this);
+        QRect drawRect = style()->subElementRect(QStyle::SE_PushButtonFocusRect, &option, this).adjusted(2, 0, -2 - menuWidth, -0);
+        if (isEnabled()){
+            paint.fillRect(drawRect, _color);
+        }
+        paint.setPen(_color.alpha() == 0 || isLight() ? Qt::black : Qt::white);
+        paint.drawText(drawRect, Qt::AlignCenter, text());
+    }
 }
 
 void ColorChooser::showDialog()
 {
     QColorDialog dlg(_color, this);
     if (dlg.exec() == QDialog::Accepted){
-        _color = dlg.currentColor();
-        update();
+        setColor(dlg.currentColor());
         emit changed(_color);
     }
 }
+
+QSize ColorChooser::sizeHint()
+{
+    return QSize(60, 24);
+}
+
+void ColorChooser::colorReset()
+{
+    setColor(Qt::transparent);
+}

src/Core/ColorChooser.h

 #ifndef _COLORCHOOSER_H_
 #define _COLORCHOOSER_H_
-#include <QWidget>
+#include <QToolButton>
 
-class ColorChooser: public QWidget
+class ColorChooser: public QToolButton
 {
     Q_OBJECT
     Q_PROPERTY(QColor color READ color WRITE setColor)
 
     QColor color();
     void setColor(QColor color);
+public slots:
+    void colorReset();
+    void showDialog();
 signals:
     void changed(const QColor& color);
 protected:
     virtual void paintEvent(QPaintEvent *);
-    virtual void mousePressEvent(QMouseEvent *);
+    virtual QSize sizeHint();
 private:
-    void showDialog();
+    bool isLight();
 private:
     QColor _color;
 };

src/Core/PynotoApplication.h

 public:
     PynotoApplication(int &argc, char **argv, int = ApplicationFlags);
     virtual ~PynotoApplication();
+    void emitPreferencesChanged(const QString& name);
+    void emitPreferencesChanged(const QString& name, const QVariant& value);
 signals:
-    void preferencesChanged();
     void preferencesChanged(const QString& what);
+    void preferencesChanged(const QString& what, const QVariant& value);
 public:
     Core::PluginManager *pluginManager;
     IconProvider::IIconProvider *iconProvider;
 IconProvider::IIconProvider* icons();
 Core::PluginManager * manager();
 PynotoApplication* app();
+void emitPreferencesChanged(const QString& name);
+void emitPreferencesChanged(const QString& name, const QVariant& value);
 
 }
 #endif

src/Main/Preferences/GeneralPrefs.ui

    <rect>
     <x>0</x>
     <y>0</y>
-    <width>687</width>
-    <height>501</height>
+    <width>685</width>
+    <height>499</height>
    </rect>
   </property>
   <property name="windowTitle">
        </widget>
       </item>
       <item>
-       <widget class="ColorChooser" name="defColor" native="true">
+       <widget class="ColorChooser" name="defColor">
         <property name="minimumSize">
          <size>
-          <width>100</width>
+          <width>120</width>
           <height>25</height>
          </size>
         </property>
  <customwidgets>
   <customwidget>
    <class>ColorChooser</class>
-   <extends>QWidget</extends>
+   <extends>QToolButton</extends>
    <header>Core/ColorChooser.h</header>
    <container>1</container>
   </customwidget>

src/Main/PynotoStyle.cpp

     _pbtn  = Aux::icons()->pixmap("pushbutton");
     _pbtnHover = Aux::icons()->pixmap("pushbutton_hover");
     _pbtnPressed = Aux::icons()->pixmap("pushbutton_pressed");
+
+    connect(Aux::app(), SIGNAL(preferencesChanged(QString,QVariant)), SLOT(onPreferencesChanged(QString,QVariant)));
 }
 
 bool PynotoStyle::isPanel(const QWidget* widget) const
             }
             widget->setPalette(pal);
         }
+        QPalette pal = widget->palette();
+        pal.setColor(QPalette::WindowText, isLight() ? Qt::black : Qt::white);
+        pal.setColor(QPalette::ButtonText, isLight() ? Qt::black : Qt::white);
+        widget->setPalette(pal);
     }
 }
 
             if (widget->property("fancy").toBool()){
                 drawFancyToolButton(toolbutton, painter, widget);
             } else {
-            painter->save();
+                painter->save();
                 bool pressed = toolbutton->state & State_Sunken || toolbutton->state & State_On;
                 bool hovered = toolbutton->state & State_Enabled && toolbutton->state & State_MouseOver;
                 drawPrimitive(PE_PanelButtonTool, option, painter, widget);
                 toolbutton->icon.paint(painter, iconRect, Qt::AlignCenter, mode);
                 if (!toolbutton->text.isEmpty()){
                     int shift = toolbutton->icon.isNull() ? 0 : toolbutton->iconSize.width()+5;
-                    painter->setPen(Qt::white);
+                    QPalette pal = widget->palette();
+                    painter->setPen((option->state & State_Enabled) ? pal.color(QPalette::ButtonText) : pal.color(QPalette::ButtonText).lighter());
                     painter->drawText(option->rect.adjusted(shift, 0, 0, 0), toolbutton->text);
                 }
                 painter->restore();
             painter->save();
             QRect editRect = subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, widget);
             QString text = option->fontMetrics.elidedText(cb->currentText, Qt::ElideRight, editRect.width());
+            QPalette pal = widget->palette();
             if ((option->state & State_Enabled)) {
-                painter->setPen(QColor(0, 0, 0, 70));
+                painter->setPen(pal.color(QPalette::WindowText));
                 painter->drawText(editRect.adjusted(1, 0, -1, 0), Qt::AlignLeft | Qt::AlignVCenter, text);
             } else {
                 painter->setOpacity(0.8);
+                painter->setPen(pal.color(QPalette::WindowText).lighter());
+                painter->drawText(editRect.adjusted(1, 0, -1, 0), Qt::AlignLeft | Qt::AlignVCenter, text);
             }
-            painter->setPen(Qt::white);
-            painter->drawText(editRect.adjusted(1, 0, -1, 0), Qt::AlignLeft | Qt::AlignVCenter, text);
             painter->restore();
         }
         break;
     QRect textRect = option->rect.adjusted(2, 0, 0, 2);
     if (!option->icon.isNull())
         textRect = textRect.adjusted(textRect.height(), 0, 0, 0);
+    painter->setPen(Qt::black);
     painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, option->text);
 }
 
-void PynotoStyle::onPreferencesChanged()
+void PynotoStyle::onPreferencesChanged(const QString& name, const QVariant& value)
 {
-    Main::Settings set;
-    if (_panelColor != set.color()){
-        _panelColor = set.color();
+    if (name == "panel-color"){
+        Main::Settings set;
+        QColor toColor = value.value<QColor>();
+        if (toColor.alpha() == 0){
+            _panelColor = set.color();
+        } else {
+            _panelColor = toColor;
+        }
         createPanelImage();
         QTimer::singleShot(10, this, SLOT(resetStyle()));
     }
         w->update();
     }
 }
+
+bool PynotoStyle::isLight()
+{
+    int yiq = ((_panelColor.red()*299)+(_panelColor.green()*587)+(_panelColor.blue()*114))/1000;
+    return yiq >= 128;
+}

src/Main/PynotoStyle.h

     void drawButtonSeparator(QPainter *painter, const QRect &rect, bool reverse) const;
     void drawFancyToolButton(const QStyleOptionToolButton* option, QPainter* painter, const QWidget* widget) const;
     void createPanelImage();
+    bool isLight();
 private slots:
-    void onPreferencesChanged();
+    void onPreferencesChanged(const QString& name, const QVariant& value);
     void resetStyle();
 private:
     QPixmap _panel;

src/Main/PynotoWindow.cpp

     ui->setupUi(this);
     setWindowIcon(QIcon(":/pynoto.svgz"));
     QApplication::setStyle(new PynotoStyle);
-    connect(Aux::app(), SIGNAL(preferencesChanged()), SLOT(onPreferencesChanged()));
 
     setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
 
     QMainWindow::closeEvent(ev);
 }
 
-void PynotoWindow::onPreferencesChanged()
-{
-
-}
-
 }

src/Main/PynotoWindow.h

     void newProject();
     void resetEditor();
     void consoleChanged(int);
-    void onPreferencesChanged();
 private:
     Ui::PynotoWindow *ui;
     IconProvider::IIconProvider * _iconProvider;

src/Preferences/PreferencesPlugin.cpp

 void PreferencesPlugin::editPreferences(QWidget *parent)
 {
     PreferencesWindow wnd(parent);
-    connect(&wnd, SIGNAL(changed()), Aux::app(), SIGNAL(preferencesChanged()));
 
     QMultiMap<int, IPreferencesPage*> map;
     foreach(IPlugin * p, Aux::manager()->plugins()){

src/Project/ProjectFile.cpp

     _loaded = true;
 
     emit loaded();
+    if (QColor(_settings->value("common/color", "transparent").toString()).alpha() != 0){
+        Aux::emitPreferencesChanged("panel-color", QColor(_settings->value("common/color", "transparent").toString()));
+    }
     return _loaded;
 }
 
         _settings->setValue("common/color", "transparent");
     else
         _settings->setValue("common/color", color.name());
+
+    Aux::emitPreferencesChanged("panel-color", color);
 }
 
 }

src/Project/Properties/General.ui

    <rect>
     <x>0</x>
     <y>0</y>
-    <width>531</width>
-    <height>424</height>
+    <width>529</width>
+    <height>422</height>
    </rect>
   </property>
   <property name="windowTitle">
       </widget>
      </item>
      <item row="5" column="1">
-      <widget class="ColorChooser" name="projectColor" native="true">
+      <widget class="ColorChooser" name="projectColor">
        <property name="sizePolicy">
-        <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+        <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
+       <property name="minimumSize">
+        <size>
+         <width>120</width>
+         <height>0</height>
+        </size>
+       </property>
+       <property name="text">
+        <string/>
+       </property>
       </widget>
      </item>
     </layout>
   </customwidget>
   <customwidget>
    <class>ColorChooser</class>
-   <extends>QWidget</extends>
+   <extends>QToolButton</extends>
    <header>Core/ColorChooser.h</header>
    <container>1</container>
   </customwidget>

src/Theme/ThemeEditor.ui

    <rect>
     <x>0</x>
     <y>0</y>
-    <width>652</width>
-    <height>509</height>
+    <width>650</width>
+    <height>507</height>
    </rect>
   </property>
   <property name="windowTitle">
               </widget>
              </item>
              <item row="1" column="1">
-              <widget class="ColorChooser" name="cbackground" native="true">
+              <widget class="ColorChooser" name="cbackground">
+               <property name="sizePolicy">
+                <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+                 <horstretch>0</horstretch>
+                 <verstretch>0</verstretch>
+                </sizepolicy>
+               </property>
                <property name="minimumSize">
                 <size>
-                 <width>60</width>
-                 <height>30</height>
+                 <width>120</width>
+                 <height>0</height>
                 </size>
                </property>
+               <property name="text">
+                <string/>
+               </property>
               </widget>
              </item>
              <item row="0" column="1">
-              <widget class="ColorChooser" name="cforeground" native="true">
+              <widget class="ColorChooser" name="cforeground">
+               <property name="sizePolicy">
+                <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+                 <horstretch>0</horstretch>
+                 <verstretch>0</verstretch>
+                </sizepolicy>
+               </property>
                <property name="minimumSize">
                 <size>
-                 <width>60</width>
-                 <height>30</height>
+                 <width>120</width>
+                 <height>0</height>
                 </size>
                </property>
+               <property name="text">
+                <string/>
+               </property>
               </widget>
              </item>
             </layout>
            <bool>false</bool>
           </property>
           <property name="alternatingRowColors">
-           <bool>false</bool>
+           <bool>true</bool>
           </property>
          </widget>
         </item>
                </property>
               </widget>
              </item>
-             <item row="0" column="1">
-              <widget class="ColorChooser" name="foreground" native="true">
-               <property name="minimumSize">
-                <size>
-                 <width>60</width>
-                 <height>30</height>
-                </size>
+             <item row="1" column="2">
+              <widget class="QCheckBox" name="transparent">
+               <property name="text">
+                <string>Transparent</string>
                </property>
               </widget>
              </item>
              <item row="1" column="1">
-              <widget class="ColorChooser" name="background" native="true">
+              <widget class="ColorChooser" name="background">
+               <property name="sizePolicy">
+                <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+                 <horstretch>0</horstretch>
+                 <verstretch>0</verstretch>
+                </sizepolicy>
+               </property>
                <property name="minimumSize">
                 <size>
-                 <width>60</width>
-                 <height>30</height>
+                 <width>120</width>
+                 <height>0</height>
                 </size>
                </property>
+               <property name="text">
+                <string/>
+               </property>
               </widget>
              </item>
-             <item row="1" column="2">
-              <widget class="QCheckBox" name="transparent">
+             <item row="0" column="1">
+              <widget class="ColorChooser" name="foreground">
+               <property name="sizePolicy">
+                <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+                 <horstretch>0</horstretch>
+                 <verstretch>0</verstretch>
+                </sizepolicy>
+               </property>
+               <property name="minimumSize">
+                <size>
+                 <width>120</width>
+                 <height>0</height>
+                </size>
+               </property>
                <property name="text">
-                <string>Transparent</string>
+                <string/>
                </property>
               </widget>
              </item>
  <customwidgets>
   <customwidget>
    <class>ColorChooser</class>
-   <extends>QWidget</extends>
+   <extends>QToolButton</extends>
    <header>Core/ColorChooser.h</header>
-   <container>1</container>
   </customwidget>
  </customwidgets>
  <resources/>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.