Просмотр исходного кода

Cleanup ctkColorPickerButton to use pimpl

Add property showAlpha
Don't overwrite ever QPushButton::text nor QPushButton::icon
Julien Finet лет назад: 14
Родитель
Сommit
61dfc054a4
2 измененных файлов с 146 добавлено и 49 удалено
  1. 126 46
      Libs/Widgets/ctkColorPickerButton.cpp
  2. 20 3
      Libs/Widgets/ctkColorPickerButton.h

+ 126 - 46
Libs/Widgets/ctkColorPickerButton.cpp

@@ -20,49 +20,95 @@
 
 // Qt includes
 #include <QColorDialog>
+#include <QDebug>
 #include <QIcon>
 #include <QPainter>
 #include <QPixmap>
 #include <QStyle>
+#include <QStyleOptionButton>
+#include <QStylePainter>
 
 // CTK includes
 #include "ctkColorPickerButton.h"
 
+class ctkColorPickerButtonPrivate
+{
+  Q_DECLARE_PUBLIC(ctkColorPickerButton);
+protected:
+  ctkColorPickerButton* const q_ptr;
+public:
+  ctkColorPickerButtonPrivate(ctkColorPickerButton& object);
+  void init();
+  void computeIcon();
+
+  QIcon  Icon;
+  QColor Color;
+  bool   DisplayColorName;
+  bool   ShowAlpha;
+};
+
 //-----------------------------------------------------------------------------
-ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
-  :QPushButton(_parent)
+ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& object)
+  : q_ptr(&object)
 {
-  connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
-  this->setColor(Qt::black);
+  this->Color = Qt::black;
+  this->DisplayColorName = true;
+  this->ShowAlpha = false;
+}
 
-  this->setCheckable(true);
-  this->setDisplayColorName(true);
+//-----------------------------------------------------------------------------
+void ctkColorPickerButtonPrivate::init()
+{
+  Q_Q(ctkColorPickerButton);
+  q->setCheckable(true);
+  QObject::connect(q, SIGNAL(toggled(bool)),
+                   q, SLOT(onToggled(bool)));
+  this->computeIcon();
 }
 
 //-----------------------------------------------------------------------------
-ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
-  :QPushButton(_text, _parent)
+void ctkColorPickerButtonPrivate::computeIcon()
+{
+  Q_Q(ctkColorPickerButton);
+  int _iconSize = q->style()->pixelMetric(QStyle::PM_SmallIconSize);
+  QPixmap pix(_iconSize, _iconSize);
+  pix.fill(q->palette().button().color());
+  QPainter p(&pix);
+  p.setPen(QPen(Qt::gray));
+  p.setBrush(this->Color);
+  p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
+
+  this->Icon = QIcon(pix);
+}
+
+//-----------------------------------------------------------------------------
+ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
+  : QPushButton(_parent)
+  , d_ptr(new ctkColorPickerButtonPrivate(*this))
 {
-  connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
-  this->setColor(Qt::black);
+  Q_D(ctkColorPickerButton);
+  d->init();
+}
 
-  // Customize
-  this->setCheckable(true);
-  this->setDisplayColorName(true);
+//-----------------------------------------------------------------------------
+ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
+  : QPushButton(_text, _parent)
+  , d_ptr(new ctkColorPickerButtonPrivate(*this))
+{
+  Q_D(ctkColorPickerButton);
+  d->init();
 }
 
 //-----------------------------------------------------------------------------
 ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
-                                             const QString& _text,
-                                             QWidget* _parent)
-  :QPushButton(_text, _parent)
+                                           const QString& _text,
+                                           QWidget* _parent)
+  : QPushButton(_text, _parent)
+  , d_ptr(new ctkColorPickerButtonPrivate(*this))
 {
-  connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
+  Q_D(ctkColorPickerButton);
+  d->init();
   this->setColor(_color);
-
-  // Customize
-  this->setCheckable(true);
-  this->setDisplayColorName(true);
 }
 
 //-----------------------------------------------------------------------------
@@ -73,7 +119,21 @@ ctkColorPickerButton::~ctkColorPickerButton()
 //-----------------------------------------------------------------------------
 void ctkColorPickerButton::changeColor()
 {
-  this->setColor(QColorDialog::getColor(this->Color));
+  Q_D(ctkColorPickerButton);
+  QColor res;
+  if (d->ShowAlpha)
+    {
+    res = QColorDialog::getColor(
+      d->Color, this, QString(""), QColorDialog::ShowAlphaChannel );
+    }
+  else
+    {
+    res = QColorDialog::getColor(d->Color);
+    }
+  if (res.isValid())
+    {
+    this->setColor(res);
+    }
 }
 
 //-----------------------------------------------------------------------------
@@ -89,46 +149,66 @@ void ctkColorPickerButton::onToggled(bool change)
 //-----------------------------------------------------------------------------
 void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
 {
-  this->DisplayColorName = displayColorName;
+  Q_D(ctkColorPickerButton);
+  d->DisplayColorName = displayColorName;
+  this->update();
 }
 
 //-----------------------------------------------------------------------------
-void ctkColorPickerButton::setColor(const QColor& newColor)
+bool ctkColorPickerButton::displayColorName()const
 {
-  if (newColor == this->Color)
-    {
-    return;
-    }
+  Q_D(const ctkColorPickerButton);
+  return d->DisplayColorName;
+}
 
-  int _iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
-  QPixmap pix(_iconSize, _iconSize);
-  pix.fill(palette().button().color());
-  QPainter p(&pix);
-  p.setPen(QPen(Qt::gray));
-  p.setBrush(newColor);
-  p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
+//-----------------------------------------------------------------------------
+void ctkColorPickerButton::setShowAlpha(bool show)
+{
+  Q_D(ctkColorPickerButton);
+  d->ShowAlpha = show;
+}
 
-  this->setIcon(QIcon(pix));
-  
-  // Update the button text to the color name, if selected
-  if (this->DisplayColorName)
+//-----------------------------------------------------------------------------
+bool ctkColorPickerButton::showAlpha()const
+{
+  Q_D(const ctkColorPickerButton);
+  return d->ShowAlpha;
+}
+
+//-----------------------------------------------------------------------------
+void ctkColorPickerButton::setColor(const QColor& newColor)
+{
+  Q_D(ctkColorPickerButton);
+  if (newColor == d->Color)
     {
-    this->setText(newColor.name());
+    return;
     }
   
-  this->Color = newColor;
-  emit colorChanged(this->Color);
+  d->Color = newColor;
+  d->computeIcon();
+
+  this->update();
+  emit colorChanged(d->Color);
 }
 
 //-----------------------------------------------------------------------------
 QColor ctkColorPickerButton::color()const
 {
-  return this->Color;
+  Q_D(const ctkColorPickerButton);
+  return d->Color;
 }
 
 //-----------------------------------------------------------------------------
-bool ctkColorPickerButton::displayColorName()const
+void ctkColorPickerButton::paintEvent(QPaintEvent *)
 {
-  return this->DisplayColorName;
+  Q_D(ctkColorPickerButton);
+  QStylePainter p(this);
+  QStyleOptionButton option;
+  initStyleOption(&option);
+  if (d->DisplayColorName)
+    {
+    option.text = d->Color.name();
+    }
+  option.icon = d->Icon;
+  p.drawControl(QStyle::CE_PushButton, option);
 }
-

+ 20 - 3
Libs/Widgets/ctkColorPickerButton.h

@@ -27,6 +27,7 @@
 
 // CTK includes
 #include "ctkWidgetsExport.h"
+class ctkColorPickerButtonPrivate;
 
 ///
 /// ctkColorPickerButton is a QPushButton that refers to a color. The color 
@@ -38,11 +39,17 @@ class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
   Q_OBJECT
   Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
   Q_PROPERTY(bool displayColorName READ displayColorName WRITE setDisplayColorName DESIGNABLE true)
+  Q_PROPERTY(bool showAlpha READ showAlpha WRITE setShowAlpha DESIGNABLE true)
 public:
   /// By default, the color is black
   explicit ctkColorPickerButton(QWidget* parent = 0);
-  /// By default, the color is black
+  /// By default, the color is black. The text will be shown on the button if
+  /// displayColorName is false, otherwise the color name is shown.
+  /// \sa QPushButton::setText
   explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
+  /// The text will be shown on the button if
+  /// displayColorName is false, otherwise the color name is shown.
+  /// \sa setColor, QPushButton::setText 
   explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
   virtual ~ctkColorPickerButton();
  
@@ -53,6 +60,12 @@ public:
   ///
   /// Display the color name after color selection
   bool displayColorName()const;
+  
+  ///
+  /// Returns true if alpha is shown on the color dialog
+  /// \sa QColorDialog::ShowAlphaChannel
+  void setShowAlpha(bool show);
+  bool showAlpha()const;
 
 public slots:
   ///
@@ -77,8 +90,12 @@ protected slots:
   void onToggled(bool change = true);
 
 protected:
-  QColor Color;
-  bool DisplayColorName;
+  virtual void paintEvent(QPaintEvent* event);
+
+  QScopedPointer<ctkColorPickerButtonPrivate> d_ptr;
+private :
+  Q_DECLARE_PRIVATE(ctkColorPickerButton);
+  Q_DISABLE_COPY(ctkColorPickerButton);
 };
 
 #endif