Quellcode durchsuchen

Add ctkColorDialog::insertTab and setCurrentTab

It gives full control on where to add extra tabs.
Static functions also have the new feature.
ctkColorDialog::insertDefaultTab
ctkColorDialog::setDefaultTab
Julien Finet vor 14 Jahren
Ursprung
Commit
9af99587dd
2 geänderte Dateien mit 60 neuen und 27 gelöschten Zeilen
  1. 30 23
      Libs/Widgets/ctkColorDialog.cpp
  2. 30 4
      Libs/Widgets/ctkColorDialog.h

+ 30 - 23
Libs/Widgets/ctkColorDialog.cpp

@@ -23,11 +23,13 @@
 #include <QVBoxLayout>
 #include <QVBoxLayout>
 #include <QHBoxLayout>
 #include <QHBoxLayout>
 #include <QTabWidget>
 #include <QTabWidget>
+#include <QVariant>
 
 
 // CTK includes
 // CTK includes
 #include "ctkColorDialog.h"
 #include "ctkColorDialog.h"
 
 
 QList<QWidget*> ctkColorDialog::DefaultTabs;
 QList<QWidget*> ctkColorDialog::DefaultTabs;
+int ctkColorDialog::DefaultTab = -1;
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 class ctkColorDialogPrivate
 class ctkColorDialogPrivate
@@ -90,40 +92,38 @@ ctkColorDialog::~ctkColorDialog()
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
-void ctkColorDialog::addTab(QWidget* widget, const QString& label)
+void ctkColorDialog::insertTab(int tabIndex, QWidget* widget, const QString& label)
 {
 {
   Q_D(ctkColorDialog);
   Q_D(ctkColorDialog);
-  d->LeftTabWidget->addTab(widget, label);
+  d->LeftTabWidget->insertTab(tabIndex, widget, label);
+}
+
+//------------------------------------------------------------------------------
+void ctkColorDialog::setCurrentTab(int index)
+{
+  Q_D(ctkColorDialog);
+  d->LeftTabWidget->setCurrentIndex(index);
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 void ctkColorDialog::removeTab(int index)
 void ctkColorDialog::removeTab(int index)
 {
 {
   Q_D(ctkColorDialog);
   Q_D(ctkColorDialog);
-  if (index < 0)
-    {
-    return;
-    }
-  d->LeftTabWidget->removeTab(index + 1);
+  d->LeftTabWidget->removeTab(index);
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 int ctkColorDialog::indexOf(QWidget* widget)const
 int ctkColorDialog::indexOf(QWidget* widget)const
 {
 {
   Q_D(const ctkColorDialog);
   Q_D(const ctkColorDialog);
-  int index = d->LeftTabWidget->indexOf(widget);
-  return index >= 0 ? index - 1 : -1;
+  return d->LeftTabWidget->indexOf(widget);
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 QWidget* ctkColorDialog::widget(int index)const
 QWidget* ctkColorDialog::widget(int index)const
 {
 {
   Q_D(const ctkColorDialog);
   Q_D(const ctkColorDialog);
-  if (index < 0)
-    {
-    return 0;
-    }
-  return d->LeftTabWidget->widget(index+1);
+  return d->LeftTabWidget->widget(index);
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
@@ -139,20 +139,21 @@ QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QS
   dlg.setCurrentColor(initial);
   dlg.setCurrentColor(initial);
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
     {
     {
-    dlg.addTab(tab, tab->windowTitle());
-    if (!tab->accessibleDescription().isEmpty())
+    dlg.insertTab(tab->property("tabIndex").toInt(), tab, tab->windowTitle());
+    if (!tab->property("signal").isValid())
       {
       {
-      QObject::connect(tab, tab->accessibleDescription().toLatin1(),
+      QObject::connect(tab, tab->property("signal").toString().toLatin1(),
                        &dlg, SLOT(setColor(QColor)));
                        &dlg, SLOT(setColor(QColor)));
       }
       }
     }
     }
+  dlg.setCurrentTab(ctkColorDialog::DefaultTab);
   dlg.exec();
   dlg.exec();
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
   foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
     {
     {
     dlg.removeTab(dlg.indexOf(tab));
     dlg.removeTab(dlg.indexOf(tab));
-    if (!tab->accessibleDescription().isEmpty())
+    if (tab->property("signal").isValid())
       {
       {
-      QObject::disconnect(tab, tab->accessibleDescription().toLatin1(),
+      QObject::disconnect(tab, tab->property("signal").toString().toLatin1(),
                           &dlg, SLOT(setColor(QColor)));
                           &dlg, SLOT(setColor(QColor)));
       }
       }
     tab->setParent(0);
     tab->setParent(0);
@@ -163,20 +164,26 @@ QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QS
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
-void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label, const char* signal)
+void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal)
 {
 {
-  // I'm a bit lazy here and the label+ signal should probably be stored in a
-  // separate structure
   widget->setWindowTitle(label);
   widget->setWindowTitle(label);
-  widget->setAccessibleDescription(signal);
+  widget->setProperty("signal", signal);
+  widget->setProperty("tabIndex", tabIndex);
 
 
   ctkColorDialog::DefaultTabs << widget;
   ctkColorDialog::DefaultTabs << widget;
   widget->setParent(0);
   widget->setParent(0);
 }
 }
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
+void ctkColorDialog::setDefaultTab(int index)
+{
+  ctkColorDialog::DefaultTab = index;
+}
+
+//------------------------------------------------------------------------------
 void ctkColorDialog::setColor(const QColor& color)
 void ctkColorDialog::setColor(const QColor& color)
 {
 {
   this->QColorDialog::setCurrentColor(color);
   this->QColorDialog::setCurrentColor(color);
 }
 }
 
 
+

+ 30 - 4
Libs/Widgets/ctkColorDialog.h

@@ -49,16 +49,22 @@ public:
   /// The ownership of the widget is taken.
   /// 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)
   /// to ctkColorDialog::setColor(QColor)
-  void addTab(QWidget* widget, const QString& label);
+  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
+  void insertTab(int tabIndex, QWidget* widget, const QString& label);
 
 
   /// The ownership of widget remains the same. The widget is not deleted, 
   /// The ownership of widget remains the same. The widget is not deleted, 
   /// but simply removed from the widget's stacked layout, causing it to be
   /// but simply removed from the widget's stacked layout, causing it to be
   /// hidden.
   /// hidden.
-  /// It is not possible to remove the "Basic Colors" tab
   void removeTab(int index);
   void removeTab(int index);
 
 
+  /// Set the current tab index. 0 ("Basic" tab) by default.
+  void setCurrentTab(int index);
+
   /// Return the extra widget if any
   /// Return the extra widget if any
-  /// It is not possible to retrieave the "Basic colors" tab
+  /// Be careful with the "Basic" tab.
   QWidget* widget(int index)const;
   QWidget* widget(int index)const;
   
   
   /// Returns the index position of the page occupied by the widget w,
   /// Returns the index position of the page occupied by the widget w,
@@ -78,7 +84,14 @@ public:
   /// ctkColorDialog::getColor. \a label is title of the tab and \a signal is the signal fired 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
   /// the widget whenever a QColor is changed, typically: SIGNAL(currentColorChanged(QColor)). It
   /// is internally connected to set the current color of the dialog
   /// is internally connected to set the current color of the dialog
-  static void addDefaultTab(QWidget* widget, const QString& label, const char* signal = 0);
+  static inline void addDefaultTab(QWidget* widget, const QString& label, const char* signal = 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);
+  /// 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 slots:
 public slots:
   /// Slotify QColorDialog::setCurrentColor(QColor)
   /// Slotify QColorDialog::setCurrentColor(QColor)
@@ -88,9 +101,22 @@ protected:
   QScopedPointer<ctkColorDialogPrivate> d_ptr;
   QScopedPointer<ctkColorDialogPrivate> d_ptr;
 
 
   static QList<QWidget*> DefaultTabs;
   static QList<QWidget*> DefaultTabs;
+  static int DefaultTab;
 private:
 private:
   Q_DECLARE_PRIVATE(ctkColorDialog);
   Q_DECLARE_PRIVATE(ctkColorDialog);
   Q_DISABLE_COPY(ctkColorDialog);
   Q_DISABLE_COPY(ctkColorDialog);
 };
 };
 
 
+//------------------------------------------------------------------------------
+void ctkColorDialog::addTab(QWidget* widget, const QString& label)
+{
+  this->insertTab(-1, widget, label);
+}
+
+//------------------------------------------------------------------------------
+void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label, const char* signal)
+{
+  ctkColorDialog::insertDefaultTab(-1, widget, label, signal);
+}
+
 #endif
 #endif