Bläddra i källkod

ENH: ctkColorPickerButton can use internally ctkColorDialog

Julien Finet 14 år sedan
förälder
incheckning
9d1d133ca5

+ 5 - 4
Libs/Widgets/Testing/Cpp/ctkColorPickerButtonTest1.cpp

@@ -84,11 +84,12 @@ int ctkColorPickerButtonTest1(int argc, char * argv [] )
               << std::endl;
     return EXIT_FAILURE;
     }
-    
-  colorPicker2.setShowAlpha(true);
-  if (!colorPicker2.showAlpha())
+
+  colorPicker2.setDialogOptions(ctkColorPickerButton::ShowAlphaChannel | ctkColorPickerButton::UseCTKColorDialog);
+  if (!colorPicker2.dialogOptions().testFlag(ctkColorPickerButton::ShowAlphaChannel) ||
+      !colorPicker2.dialogOptions().testFlag(ctkColorPickerButton::UseCTKColorDialog))
     {
-    std::cerr << "ctkColorPickerButton::setShowAlpha failed" << std::endl;
+    std::cerr << "ctkColorPickerButton::setDialogOptions failed" << std::endl;
     return EXIT_FAILURE;
     }
 

+ 17 - 10
Libs/Widgets/ctkColorPickerButton.cpp

@@ -29,6 +29,7 @@
 #include <QStylePainter>
 
 // CTK includes
+#include "ctkColorDialog.h"
 #include "ctkColorPickerButton.h"
 
 class ctkColorPickerButtonPrivate
@@ -44,7 +45,7 @@ public:
   QIcon  Icon;
   QColor Color;
   bool   DisplayColorName;
-  bool   ShowAlpha;
+  ctkColorPickerButton::ColorDialogOptions DialogOptions;
 };
 
 //-----------------------------------------------------------------------------
@@ -53,7 +54,7 @@ ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& o
 {
   this->Color = Qt::black;
   this->DisplayColorName = true;
-  this->ShowAlpha = false;
+  this->DialogOptions = 0;
 }
 
 //-----------------------------------------------------------------------------
@@ -121,14 +122,20 @@ void ctkColorPickerButton::changeColor()
 {
   Q_D(ctkColorPickerButton);
   QColor res;
-  if (d->ShowAlpha)
+    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 = QColorDialog::getColor(
-      d->Color, this, QString(""), QColorDialog::ShowAlphaChannel );
+    res = ctkColorDialog::getColor(d->Color, this, QString(""),options);
     }
   else
     {
-    res = QColorDialog::getColor(d->Color);
+    res = QColorDialog::getColor(d->Color, this, QString(""), options);
     }
   if (res.isValid())
     {
@@ -162,17 +169,17 @@ bool ctkColorPickerButton::displayColorName()const
 }
 
 //-----------------------------------------------------------------------------
-void ctkColorPickerButton::setShowAlpha(bool show)
+void ctkColorPickerButton::setDialogOptions(ColorDialogOptions options)
 {
   Q_D(ctkColorPickerButton);
-  d->ShowAlpha = show;
+  d->DialogOptions = options;
 }
 
 //-----------------------------------------------------------------------------
-bool ctkColorPickerButton::showAlpha()const
+ctkColorPickerButton::ColorDialogOptions ctkColorPickerButton::dialogOptions()const
 {
   Q_D(const ctkColorPickerButton);
-  return d->ShowAlpha;
+  return d->DialogOptions;
 }
 
 //-----------------------------------------------------------------------------

+ 18 - 6
Libs/Widgets/ctkColorPickerButton.h

@@ -37,10 +37,19 @@ class ctkColorPickerButtonPrivate;
 class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
 {
   Q_OBJECT
+  Q_ENUMS(ColorDialogOption)
   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)
+  Q_PROPERTY(ColorDialogOptions dialogOptions READ dialogOptions WRITE setDialogOptions DESIGNABLE true)
 public:
+  enum ColorDialogOption {
+    ShowAlphaChannel    = 0x00000001,
+    NoButtons           = 0x00000002,
+    DontUseNativeDialog = 0x00000004,
+    UseCTKColorDialog   = 0x0000000C
+  };
+  Q_DECLARE_FLAGS(ColorDialogOptions, ColorDialogOption)
+
   /// 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
@@ -62,17 +71,18 @@ public:
   bool displayColorName()const;
   
   ///
-  /// Returns true if alpha is shown on the color dialog
-  /// \sa QColorDialog::ShowAlphaChannel
-  void setShowAlpha(bool show);
-  bool showAlpha()const;
+  /// Set the color dialog options to configure the color dialog.
+  /// \sa QColorDialog::setOptions QColorDialog::ColorDialogOption
+  void setDialogOptions(ColorDialogOptions options);
+  ColorDialogOptions dialogOptions() const;
+
 
 public 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.
   void changeColor();
 
@@ -98,4 +108,6 @@ private :
   Q_DISABLE_COPY(ctkColorPickerButton);
 };
 
+Q_DECLARE_OPERATORS_FOR_FLAGS(ctkColorPickerButton::ColorDialogOptions)
+
 #endif