Explorar o código

Add static functions to ctkColorDialog

Julien Finet %!s(int64=14) %!d(string=hai) anos
pai
achega
f4639656a1

+ 2 - 0
Libs/Widgets/Testing/Cpp/ctkColorDialogTest1.cpp

@@ -50,6 +50,8 @@ int ctkColorDialogTest1(int argc, char * argv [] )
     {
     return EXIT_FAILURE;
     }
+  ctkColorDialog::addDefaultTab(extraPanel, "Extra");
+  QColor color = ctkColorDialog::getColor(Qt::black,0 , "", 0);
   return EXIT_SUCCESS;
 
 }

+ 28 - 1
Libs/Widgets/ctkColorDialog.cpp

@@ -27,6 +27,8 @@
 // CTK includes
 #include "ctkColorDialog.h"
 
+QList<QWidget*> ctkColorDialog::DefaultTabs;
+
 //------------------------------------------------------------------------------
 class ctkColorDialogPrivate
 {
@@ -64,7 +66,6 @@ void ctkColorDialogPrivate::init()
   this->LeftTabWidget->addTab(this->BasicTab, QObject::tr("Basic"));
 }
 
-
 //------------------------------------------------------------------------------
 ctkColorDialog::ctkColorDialog(QWidget* parent)
   : QColorDialog(parent)
@@ -101,3 +102,29 @@ QWidget* ctkColorDialog::widget(int index)const
   Q_D(const ctkColorDialog);
   return d->LeftTabWidget->widget(index+1);
 }
+
+//------------------------------------------------------------------------------
+QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
+                              ColorDialogOptions options)
+{
+  ctkColorDialog dlg(parent);
+  if (!title.isEmpty())
+    {
+    dlg.setWindowTitle(title);
+    }
+  dlg.setOptions(options | QColorDialog::DontUseNativeDialog);
+  dlg.setCurrentColor(initial);
+  foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
+    {
+    dlg.addTab(tab, tab->accessibleDescription());
+    }
+  dlg.exec();
+  return dlg.selectedColor();
+}
+
+//------------------------------------------------------------------------------
+void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label)
+{
+  widget->setAccessibleDescription(label);
+  ctkColorDialog::DefaultTabs << widget;
+}

+ 11 - 2
Libs/Widgets/ctkColorDialog.h

@@ -51,12 +51,21 @@ public:
   /// Return the extra widget if any
   QWidget* widget(int index)const;
 
-  /// Internally used
-  //bool eventFilter(QObject *obj, QEvent *event);
+  /// 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
+  /// QColor::isValid()) color if the user cancels the dialog.
+  ///
+  /// The \a options argument allows you to customize the dialog;
+  /// QColorDialog::DontUseNativeDialog is forced
+  static QColor getColor(const QColor &initial, QWidget *parent,
+                         const QString &title, ColorDialogOptions options);
+  static void addDefaultTab(QWidget* widget, const QString& label);
 
 protected:
   QScopedPointer<ctkColorDialogPrivate> d_ptr;
 
+  static QList<QWidget*> DefaultTabs;
 private:
   Q_DECLARE_PRIVATE(ctkColorDialog);
   Q_DISABLE_COPY(ctkColorDialog);