Selaa lähdekoodia

ENH: Added the property displayColorName.

By default, after color selection the button text shows the color name as
 hexcode.  The new methods allow to disable this behavior to keep the button
 text after a color update.
Daniel Haehn 14 vuotta sitten
vanhempi
commit
624df02647
2 muutettua tiedostoa jossa 34 lisäystä ja 2 poistoa
  1. 22 1
      Libs/Widgets/ctkColorPickerButton.cpp
  2. 12 1
      Libs/Widgets/ctkColorPickerButton.h

+ 22 - 1
Libs/Widgets/ctkColorPickerButton.cpp

@@ -36,6 +36,7 @@ ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
   this->setColor(Qt::black);
   this->setColor(Qt::black);
 
 
   this->setCheckable(true);
   this->setCheckable(true);
+  this->setDisplayColorName(true);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -47,6 +48,7 @@ ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _paren
 
 
   // Customize
   // Customize
   this->setCheckable(true);
   this->setCheckable(true);
+  this->setDisplayColorName(true);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -60,6 +62,7 @@ ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
 
 
   // Customize
   // Customize
   this->setCheckable(true);
   this->setCheckable(true);
+  this->setDisplayColorName(true);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -84,6 +87,12 @@ void ctkColorPickerButton::onToggled(bool change)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
+void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
+{
+  this->DisplayColorName = displayColorName;
+}
+
+//-----------------------------------------------------------------------------
 void ctkColorPickerButton::setColor(const QColor& newColor)
 void ctkColorPickerButton::setColor(const QColor& newColor)
 {
 {
   if (newColor == this->Color)
   if (newColor == this->Color)
@@ -100,7 +109,12 @@ void ctkColorPickerButton::setColor(const QColor& newColor)
   p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
   p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
 
 
   this->setIcon(QIcon(pix));
   this->setIcon(QIcon(pix));
-  this->setText(newColor.name());
+  
+  // Update the button text to the color name, if selected
+  if (this->DisplayColorName)
+    {
+    this->setText(newColor.name());
+    }
   
   
   this->Color = newColor;
   this->Color = newColor;
   emit colorChanged(this->Color);
   emit colorChanged(this->Color);
@@ -111,3 +125,10 @@ QColor ctkColorPickerButton::color()const
 {
 {
   return this->Color;
   return this->Color;
 }
 }
+
+//-----------------------------------------------------------------------------
+bool ctkColorPickerButton::displayColorName()const
+{
+  return this->DisplayColorName;
+}
+

+ 12 - 1
Libs/Widgets/ctkColorPickerButton.h

@@ -37,6 +37,7 @@ class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
 {
 {
   Q_OBJECT
   Q_OBJECT
   Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
   Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
+  Q_PROPERTY(bool displayColorName READ displayColorName WRITE setDisplayColorName DESIGNABLE true)
 public:
 public:
   /// By default, the color is black
   /// By default, the color is black
   explicit ctkColorPickerButton(QWidget* parent = 0);
   explicit ctkColorPickerButton(QWidget* parent = 0);
@@ -44,11 +45,15 @@ public:
   explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
   explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
   explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
   explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
   virtual ~ctkColorPickerButton();
   virtual ~ctkColorPickerButton();
-  
+ 
   ///
   ///
   /// Current selected color
   /// Current selected color
   QColor color()const;
   QColor color()const;
 
 
+  ///
+  /// Display the color name after color selection
+  bool displayColorName()const;
+
 public slots:
 public slots:
   ///
   ///
   /// Set a new current color without opening a dialog
   /// Set a new current color without opening a dialog
@@ -58,6 +63,11 @@ public slots:
   /// Opens a color dialog to select a new current color.
   /// Opens a color dialog to select a new current color.
   void changeColor();
   void changeColor();
 
 
+  ///
+  /// Toggle the display of the color name after color selection.
+  /// By default, this is activated.
+  void setDisplayColorName(bool displayColorName);
+
 signals:
 signals:
   /// colorChanged is fired anytime a new color is set. Programatically or
   /// colorChanged is fired anytime a new color is set. Programatically or
   /// by the user when choosing a color from the color dialog
   /// by the user when choosing a color from the color dialog
@@ -68,6 +78,7 @@ protected slots:
 
 
 protected:
 protected:
   QColor Color;
   QColor Color;
+  bool DisplayColorName;
 };
 };
 
 
 #endif
 #endif