瀏覽代碼

Merge branch 'ctkColorPickerButton-colorname'

* ctkColorPickerButton-colorname:
  Add support for color name in ctkColorDialog and ctkColorPickerButton
Julien Finet 12 年之前
父節點
當前提交
f8d6331190
共有 4 個文件被更改,包括 213 次插入45 次删除
  1. 55 7
      Libs/Widgets/ctkColorDialog.cpp
  2. 40 10
      Libs/Widgets/ctkColorDialog.h
  3. 84 26
      Libs/Widgets/ctkColorPickerButton.cpp
  4. 34 2
      Libs/Widgets/ctkColorPickerButton.h

+ 55 - 7
Libs/Widgets/ctkColorDialog.cpp

@@ -30,6 +30,7 @@
 
 QList<QWidget*> ctkColorDialog::DefaultTabs;
 int ctkColorDialog::DefaultTab = -1;
+QString ctkColorDialog::LastColorName = QString();
 
 //------------------------------------------------------------------------------
 class ctkColorDialogPrivate
@@ -42,6 +43,7 @@ public:
   void init();
   QTabWidget* LeftTabWidget;
   QWidget*    BasicTab;
+  QString ColorName;
 };
 
 //------------------------------------------------------------------------------
@@ -72,6 +74,9 @@ void ctkColorDialogPrivate::init()
   // well.
   q->setSizeGripEnabled(true);
   q->layout()->setSizeConstraint(QLayout::SetDefaultConstraint);
+
+  QObject::connect(q, SIGNAL(currentColorChanged(QColor)),
+                   q, SLOT(resetColorName()));
 }
 
 //------------------------------------------------------------------------------
@@ -146,34 +151,54 @@ QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QS
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
     {
     dlg.insertTab(tab->property("tabIndex").toInt(), tab, tab->windowTitle());
-    if (tab->property("signal").isValid())
+    if (tab->property("colorSignal").isValid())
       {
-      QObject::connect(tab, tab->property("signal").toString().toLatin1(),
+      QObject::connect(tab, tab->property("colorSignal").toString().toLatin1(),
                        &dlg, SLOT(setColor(QColor)));
       }
+    if (tab->property("nameSignal").isValid())
+      {
+      QObject::connect(tab, tab->property("nameSignal").toString().toLatin1(),
+                       &dlg, SLOT(setColorName(QString)));
+      }
     }
   dlg.setCurrentTab(ctkColorDialog::DefaultTab);
   dlg.exec();
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
     {
     dlg.removeTab(dlg.indexOf(tab));
-    if (tab->property("signal").isValid())
+    if (tab->property("colorSignal").isValid())
       {
-      QObject::disconnect(tab, tab->property("signal").toString().toLatin1(),
+      QObject::disconnect(tab, tab->property("colorSignal").toString().toLatin1(),
                           &dlg, SLOT(setColor(QColor)));
       }
+    if (tab->property("nameSignal").isValid())
+      {
+      QObject::disconnect(tab, tab->property("nameSignal").toString().toLatin1(),
+                          &dlg, SLOT(setColorName(QString)));
+      }
     tab->setParent(0);
     tab->hide();
     }
-  
+  ctkColorDialog::LastColorName = dlg.colorName();
   return dlg.selectedColor();
 }
 
 //------------------------------------------------------------------------------
-void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal)
+QString ctkColorDialog::getColorName()
+{
+  return ctkColorDialog::LastColorName;
+}
+
+//------------------------------------------------------------------------------
+void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget,
+                                      const QString& label,
+                                      const char* colorSignal,
+                                      const char* nameSignal)
 {
   widget->setWindowTitle(label);
-  widget->setProperty("signal", signal);
+  widget->setProperty("colorSignal", colorSignal);
+  widget->setProperty("nameSignal", nameSignal);
   widget->setProperty("tabIndex", tabIndex);
 
   ctkColorDialog::DefaultTabs << widget;
@@ -192,4 +217,27 @@ void ctkColorDialog::setColor(const QColor& color)
   this->QColorDialog::setCurrentColor(color);
 }
 
+//------------------------------------------------------------------------------
+void ctkColorDialog::setColorName(const QString& name)
+{
+  Q_D(ctkColorDialog);
+  if (d->ColorName == name)
+    {
+    return;
+    }
+  d->ColorName = name;
+  emit currentColorNameChanged(d->ColorName);
+}
+
+//------------------------------------------------------------------------------
+QString ctkColorDialog::colorName()const
+{
+  Q_D(const ctkColorDialog);
+  return d->ColorName;
+}
 
+//------------------------------------------------------------------------------
+void ctkColorDialog::resetColorName()
+{
+  this->setColorName(QString());
+}

+ 40 - 10
Libs/Widgets/ctkColorDialog.h

@@ -43,14 +43,19 @@ public:
   explicit ctkColorDialog(QWidget* parent = 0);
   explicit ctkColorDialog(const QColor& initial, QWidget* parent = 0);
   virtual ~ctkColorDialog();
-  
+
   /// Add an extra widget under the file format combobox. If a label is
   /// given, it will appear in the first column.
   /// The widget is reparented to ctkColorDialog
   /// The ownership of the widget is taken.
-  /// You must manually connect the color changed signal of the widget 
+  /// You must manually connect the color changed signal of the widget
   /// to ctkColorDialog::setColor(QColor)
+  /// Same apply if you want to specify a color name, you must connect the
+  /// color name changed signal to ctkColorDialog::setColorName(QString) but
+  /// you have to make sure the color name is set after setColor as it always
+  /// resets the color name.
   inline void addTab(QWidget* widget, const QString& label);
+
   /// Same as addTab(), in addition, \a tabIndex control the tab index of the widget.
   /// If index is -1, the tab is appended (same as addDefaultTab). The last
   /// tab added with an index of 0 will be the first tab open
@@ -67,11 +72,14 @@ public:
   /// Return the extra widget if any
   /// Be careful with the "Basic" tab.
   QWidget* widget(int index)const;
-  
+
   /// Returns the index position of the page occupied by the widget w,
   /// or -1 if the widget cannot be found
   int indexOf(QWidget* widget)const;
 
+  /// Return the current color name if any has been set.
+  QString colorName()const;
+
   /// Pops up a modal color dialog with the given window \a title (or "Select Color" if none is
   /// specified), lets the user choose a color, and returns that color. The color is initially set
   /// to \a initial. The dialog is a child of \a parent. It returns an invalid (see
@@ -81,28 +89,48 @@ public:
   /// QColorDialog::DontUseNativeDialog is forced
   static QColor getColor(const QColor &initial, QWidget *parent,
                          const QString &title, ColorDialogOptions options = 0);
-  /// Add a custom widget as an additional tab of the color dialog created by 
-  /// ctkColorDialog::getColor. \a label is title of the tab and \a signal is the signal fired by 
+  /// Return the last selected color name if any. getColorName() call is only
+  /// valid after a getColor() call.
+  /// \sa getColor
+  static QString getColorName();
+
+  /// Add a custom widget as an additional tab of the color dialog created by
+  /// ctkColorDialog::getColor. \a label is title of the tab and \a signal is the signal fired by
   /// the widget whenever a QColor is changed, typically: SIGNAL(currentColorChanged(QColor)). It
   /// is internally connected to set the current color of the dialog
-  static inline void addDefaultTab(QWidget* widget, const QString& label, const char* signal = 0);
+  static inline void addDefaultTab(QWidget* widget, const QString& label,
+                                   const char* colorSignal = 0,
+                                   const char* nameSignal = 0);
   /// Same as addDefaultTab, in addition, \a tabIndex control the tab index of the widget.
   /// If index is -1, the tab is appended (same as addDefaultTab). The last
   /// tab added with an index of 0 will be the first tab open
-  static void insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal = 0);
+  static void insertDefaultTab(int tabIndex, QWidget* widget, const QString& label,
+                               const char* colorSignal = 0,
+                               const char* nameSignal = 0);
   /// Index of the tab to make default (active when getColor is called).
   /// -1 for the "Basic Colors", it's the default behavior
   static void setDefaultTab(int index);
 
 public Q_SLOTS:
-  /// Slotify QColorDialog::setCurrentColor(QColor)
+  /// Slot-ify QColorDialog::setCurrentColor(QColor)
   void setColor(const QColor& color);
 
+  /// Set the color name.
+  /// Note that each time the color is changed the name is reset.
+  void setColorName(const QString& name);
+
+Q_SIGNALS:
+  void currentColorNameChanged(const QString& colorName);
+
+protected Q_SLOTS:
+  void resetColorName();
+
 protected:
   QScopedPointer<ctkColorDialogPrivate> d_ptr;
 
   static QList<QWidget*> DefaultTabs;
   static int DefaultTab;
+  static QString LastColorName;
 private:
   Q_DECLARE_PRIVATE(ctkColorDialog);
   Q_DISABLE_COPY(ctkColorDialog);
@@ -115,9 +143,11 @@ void ctkColorDialog::addTab(QWidget* widget, const QString& label)
 }
 
 //------------------------------------------------------------------------------
-void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label, const char* signal)
+void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label,
+                                   const char* colorSignal,
+                                   const char* nameSignal)
 {
-  ctkColorDialog::insertDefaultTab(-1, widget, label, signal);
+  ctkColorDialog::insertDefaultTab(-1, widget, label, colorSignal, nameSignal);
 }
 
 #endif

+ 84 - 26
Libs/Widgets/ctkColorPickerButton.cpp

@@ -42,9 +42,11 @@ public:
   ctkColorPickerButtonPrivate(ctkColorPickerButton& object);
   void init();
   void computeIcon();
+  QString text()const;
 
   QIcon  Icon;
   QColor Color;
+  QString ColorName;
   bool   DisplayColorName;
   ctkColorPickerButton::ColorDialogOptions DialogOptions;
   mutable QSize CachedSizeHint;
@@ -55,6 +57,7 @@ ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& o
   : q_ptr(&object)
 {
   this->Color = Qt::black;
+  this->ColorName = QString();
   this->DisplayColorName = true;
   this->DialogOptions = 0;
 }
@@ -85,6 +88,24 @@ void ctkColorPickerButtonPrivate::computeIcon()
 }
 
 //-----------------------------------------------------------------------------
+QString ctkColorPickerButtonPrivate::text()const
+{
+  Q_Q(const ctkColorPickerButton);
+  if (!this->DisplayColorName)
+    {
+    return q->text();
+    }
+  if (this->ColorName.isEmpty())
+    {
+    return this->Color.name();
+    }
+  else
+    {
+    return this->ColorName;
+    }
+}
+
+//-----------------------------------------------------------------------------
 ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
   : QPushButton(_parent)
   , d_ptr(new ctkColorPickerButtonPrivate(*this))
@@ -123,25 +144,28 @@ ctkColorPickerButton::~ctkColorPickerButton()
 void ctkColorPickerButton::changeColor()
 {
   Q_D(ctkColorPickerButton);
-  QColor res;
-    QColorDialog::ColorDialogOptions options;
-    options |= QColorDialog::ColorDialogOption(
-      static_cast<int>(d->DialogOptions & ShowAlphaChannel));
-    options |= QColorDialog::ColorDialogOption(
-      static_cast<int>(d->DialogOptions & NoButtons));
-    options |= QColorDialog::ColorDialogOption(
-      static_cast<int>(d->DialogOptions & DontUseNativeDialog));
+  QColor newColor;
+  QString newColorName;
+  QColorDialog::ColorDialogOptions options;
+  options |= QColorDialog::ColorDialogOption(
+    static_cast<int>(d->DialogOptions & ShowAlphaChannel));
+  options |= QColorDialog::ColorDialogOption(
+    static_cast<int>(d->DialogOptions & NoButtons));
+  options |= QColorDialog::ColorDialogOption(
+    static_cast<int>(d->DialogOptions & DontUseNativeDialog));
   if (d->DialogOptions & UseCTKColorDialog)
     {
-    res = ctkColorDialog::getColor(d->Color, this, QString(""),options);
+    newColor = ctkColorDialog::getColor(d->Color, this, QString(""),options);
+    newColorName = ctkColorDialog::getColorName();
     }
   else
     {
-    res = QColorDialog::getColor(d->Color, this, QString(""), options);
+    newColor = QColorDialog::getColor(d->Color, this, QString(""), options);
     }
-  if (res.isValid())
+  if (newColor.isValid())
     {
-    this->setColor(res);
+    this->setColor(newColor);
+    this->setColorName(newColorName);
     }
 }
 
@@ -210,16 +234,36 @@ QColor ctkColorPickerButton::color()const
 }
 
 //-----------------------------------------------------------------------------
+void ctkColorPickerButton::setColorName(const QString& newColorName)
+{
+  Q_D(ctkColorPickerButton);
+  if (newColorName == d->ColorName)
+    {
+    return;
+    }
+
+  d->ColorName = newColorName;
+  d->CachedSizeHint = QSize();
+  this->update();
+  this->updateGeometry();
+  emit colorNameChanged(d->ColorName);
+}
+
+//-----------------------------------------------------------------------------
+QString ctkColorPickerButton::colorName()const
+{
+  Q_D(const ctkColorPickerButton);
+  return d->ColorName;
+}
+
+//-----------------------------------------------------------------------------
 void ctkColorPickerButton::paintEvent(QPaintEvent *)
 {
   Q_D(ctkColorPickerButton);
   QStylePainter p(this);
   QStyleOptionButton option;
   this->initStyleOption(&option);
-  if (d->DisplayColorName)
-    {
-    option.text = d->Color.name();
-    }
+  option.text = d->text();
   option.icon = d->Icon;
   p.drawControl(QStyle::CE_PushButton, option);
 }
@@ -228,7 +272,7 @@ void ctkColorPickerButton::paintEvent(QPaintEvent *)
 QSize ctkColorPickerButton::sizeHint()const
 {
   Q_D(const ctkColorPickerButton);
-  if (d->DisplayColorName || !this->text().isEmpty())
+  if (!d->DisplayColorName && !this->text().isEmpty())
     {
     return this->QPushButton::sizeHint();
     }
@@ -237,17 +281,31 @@ QSize ctkColorPickerButton::sizeHint()const
     return d->CachedSizeHint;
     }
 
+  // If no text, the sizehint is a QToolButton sizeHint
   QStyleOptionButton pushButtonOpt;
   this->initStyleOption(&pushButtonOpt);
-  QStyleOptionToolButton opt;
-  (&opt)->QStyleOption::operator=(pushButtonOpt);
-  opt.arrowType = Qt::NoArrow;
-  opt.icon = d->Icon;
+  pushButtonOpt.text = d->text();
   int iconSize = this->style()->pixelMetric(QStyle::PM_SmallIconSize);
-  opt.iconSize = QSize(iconSize, iconSize);
-  opt.rect.setSize(opt.iconSize); // PM_MenuButtonIndicator depends on the height
-  d->CachedSizeHint = this->style()->sizeFromContents(
-    QStyle::CT_ToolButton, &opt, opt.iconSize, this).
-    expandedTo(QApplication::globalStrut());
+  if (pushButtonOpt.text == QString())
+    {
+    QStyleOptionToolButton opt;
+    (&opt)->QStyleOption::operator=(pushButtonOpt);
+    opt.arrowType = Qt::NoArrow;
+    opt.icon = d->Icon;
+    opt.iconSize = QSize(iconSize, iconSize);
+    opt.rect.setSize(opt.iconSize); // PM_MenuButtonIndicator depends on the height
+    d->CachedSizeHint = this->style()->sizeFromContents(
+      QStyle::CT_ToolButton, &opt, opt.iconSize, this).
+      expandedTo(QApplication::globalStrut());
+    }
+  else
+    {
+    pushButtonOpt.icon = d->Icon;
+    pushButtonOpt.iconSize = QSize(iconSize, iconSize);
+    pushButtonOpt.rect.setSize(pushButtonOpt.iconSize); // PM_MenuButtonIndicator depends on the height
+    d->CachedSizeHint = (style()->sizeFromContents(
+                           QStyle::CT_PushButton, &pushButtonOpt, pushButtonOpt.iconSize, this).
+                         expandedTo(QApplication::globalStrut()));
+    }
   return d->CachedSizeHint;
 }

+ 34 - 2
Libs/Widgets/ctkColorPickerButton.h

@@ -39,8 +39,22 @@ class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
 {
   Q_OBJECT
   Q_FLAGS(ColorDialogOption ColorDialogOptions)
+
+  /// This property controls the name of the color.
+  /// Black (0,0,0) by default.
   Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
+
+  /// This property controls the name of the color.
+  /// If empty (default), the color in the format "#RRGGBB" is displayed in the
+  /// button if \a displayColorName is true, otherwise, the color name is used.
+  Q_PROPERTY(QString colorName READ colorName WRITE setColorName NOTIFY colorNameChanged)
+
+  /// This properties controls whether the name of the color is shown on the
+  /// button if true or the button text instead. True by default.
+  /// \sa colorName, QPushButton::text
   Q_PROPERTY(bool displayColorName READ displayColorName WRITE setDisplayColorName DESIGNABLE true)
+
+  /// This property controls the properties of the dialog used in \a changeColor
   Q_PROPERTY(ColorDialogOptions dialogOptions READ dialogOptions WRITE setDialogOptions)
 public:
   enum ColorDialogOption {
@@ -53,20 +67,33 @@ public:
 
   /// By default, the color is black
   explicit ctkColorPickerButton(QWidget* parent = 0);
+
   /// 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();
 
-  ///
   /// Current selected color
   QColor color()const;
 
+  /// Current selected color name.
+  /// Returns the name of the color in the format "#RRGGBB" or the string set
+  /// by setColorName().
+  /// \sa color(), setColorName()
+  QString colorName()const;
+
+  /// Set the current color name.
+  /// This allows you to give name other than the default "#RRGGBB"
+  /// Set an invalid QString to restore the default color names
+  void setColorName(const QString& name);
+
   ///
   /// Display the color name after color selection
   bool displayColorName()const;
@@ -87,8 +114,10 @@ public Q_SLOTS:
   /// Set a new current color without opening a dialog
   void setColor(const QColor& color);
 
-  ///
   /// Opens a color dialog to select a new current color.
+  /// If the CTK color dialog (\a UseCTKColorDialog) is used, then the color
+  /// name is also set if the user selects a named color.
+  /// \sa ctkColorDialog, color, colorName
   void changeColor();
 
   ///
@@ -101,6 +130,9 @@ Q_SIGNALS:
   /// by the user when choosing a color from the color dialog
   void colorChanged(QColor);
 
+  /// This signaled is fired anytime a new color name is set.
+  void colorNameChanged(QString);
+
 protected Q_SLOTS:
   void onToggled(bool change = true);