Revamp ColorButton
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 27 Jul 2009 23:34:59 +0000 (01:34 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 6 Aug 2009 18:25:05 +0000 (20:25 +0200)
Rather than requiring parent widgets using this to connect all sorts of signals and show the
color selection dialog themselves, move this into ColorButton itself, making it a
self-sufficient widget. Also, we're now using the Qt property system, and emit a
colorChanged() signal.

Oh, and we inherit from QToolButton now, which seems to be a saner choice on MacOSX and also
allows for nicer layouts on all platforms. Yes, we'll try and improve the looks later on, right
now it's just a crude QPixmap overlaying the button.

src/uisupport/colorbutton.cpp
src/uisupport/colorbutton.h

index 3b52133..15e9de4 100644 (file)
 #include <QStyle>
 #include <QStyleOptionFrame>
 
 #include <QStyle>
 #include <QStyleOptionFrame>
 
-ColorButton::ColorButton(QWidget *parent) : QPushButton(parent) {
-
+#ifdef HAVE_KDE
+#  include <KColorDialog>
+#else
+#  include <QColorDialog>
+#endif
+
+ColorButton::ColorButton(QWidget *parent) : QToolButton(parent) {
+  setText("");
+  connect(this, SIGNAL(clicked()), SLOT(chooseColor()));
 }
 
 void ColorButton::setColor(const QColor &color) {
   _color = color;
 }
 
 void ColorButton::setColor(const QColor &color) {
   _color = color;
-  update();
+  QPixmap pixmap(QSize(32,32));
+  pixmap.fill(color);
+  setIcon(pixmap);
+
+  emit colorChanged(color);
 }
 
 QColor ColorButton::color() const {
   return _color;
 }
 
 }
 
 QColor ColorButton::color() const {
   return _color;
 }
 
-/* This has been heavily inspired by KDE's KColorButton, thanks! */
-void ColorButton::paintEvent(QPaintEvent *) {
-  QPainter painter(this);
-
-  QStyleOptionButton opt;
-  initStyleOption(&opt);
-  opt.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
-  opt.features = QStyleOptionButton::None;
-  if(isDefault())
-    opt.features |= QStyleOptionButton::DefaultButton;
+void ColorButton::chooseColor() {
+#ifdef HAVE_KDE
+  QColor c;
+  KColorDialog::getColor(c, this);
+#else
+  QColor c = QColorDialog::getColor(color(), this);
+#endif
 
 
-  // Draw bevel
-  style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &painter, this);
-
-  // Calc geometry
-  QRect labelRect = style()->subElementRect(QStyle::SE_PushButtonContents, &opt, this);
-  int shift = style()->pixelMetric(QStyle::PM_ButtonMargin);
-  labelRect.adjust(shift, shift, -shift, -shift);
-  int x, y, w, h;
-  labelRect.getRect(&x, &y, &w, &h);
-
-  if(isChecked() || isDown()) {
-    x += style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal);
-    y += style()->pixelMetric(QStyle::PM_ButtonShiftVertical);
+  if(c.isValid()) {
+    setColor(c);
   }
   }
-
-  // Draw color rect
-  QBrush brush = isEnabled() ? color() : palette().color(backgroundRole());
-  qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, &brush);
 }
 }
index 9b941c5..0c9309f 100644 (file)
 #ifndef COLORBUTTON_H_
 #define COLORBUTTON_H_
 
 #ifndef COLORBUTTON_H_
 #define COLORBUTTON_H_
 
-#include <QPushButton>
+#include <QToolButton>
 
 
-class ColorButton : public QPushButton {
+class ColorButton : public QToolButton {
   Q_OBJECT
   Q_PROPERTY(QColor color READ color WRITE setColor USER true)
 
   Q_OBJECT
   Q_PROPERTY(QColor color READ color WRITE setColor USER true)
 
-  public:
-    explicit ColorButton(QWidget *parent = 0);
-    explicit ColorButton(const QColor &c, QWidget *parent = 0);
+public:
+  explicit ColorButton(QWidget *parent = 0);
+  explicit ColorButton(const QColor &c, QWidget *parent = 0);
 
 
-    void setColor(const QColor &color);
-    QColor color() const;
+  void setColor(const QColor &color);
+  QColor color() const;
 
 
-  protected:
-    void paintEvent(QPaintEvent *event);
+signals:
+  void colorChanged(const QColor &);
 
 
-  private:
-    QColor _color;
+protected:
+  //void paintEvent(QPaintEvent *event);
+
+private slots:
+  void chooseColor();
+
+private:
+  QColor _color;
 };
 
 #endif
 };
 
 #endif