Selaa lähdekoodia

Add ctkMenuComboBox::EditableOnPopup

the double click editable behavior didn't work well and ended being not
user friendly.
EditableOnPopup turns the combobox editable when the popup is shown.
Julien Finet 14 vuotta sitten
vanhempi
commit
2be0ce350c

+ 2 - 0
Libs/Widgets/CMakeLists.txt

@@ -80,6 +80,7 @@ SET(KIT_SRCS
   ctkMenuButton.h
   ctkMenuButton.h
   ctkMenuComboBox.cpp
   ctkMenuComboBox.cpp
   ctkMenuComboBox.h
   ctkMenuComboBox.h
+  ctkMenuComboBox_p.h
   ctkModalityWidget.cpp
   ctkModalityWidget.cpp
   ctkModalityWidget.h
   ctkModalityWidget.h
   ctkPathLineEdit.cpp
   ctkPathLineEdit.cpp
@@ -202,6 +203,7 @@ SET(KIT_MOC_SRCS
   ctkMatrixWidget.h
   ctkMatrixWidget.h
   ctkMenuButton.h
   ctkMenuButton.h
   ctkMenuComboBox.h
   ctkMenuComboBox.h
+  ctkMenuComboBox_p.h
   ctkModalityWidget.h
   ctkModalityWidget.h
   ctkPathLineEdit.h
   ctkPathLineEdit.h
   ctkPopupWidget.h
   ctkPopupWidget.h

+ 1 - 1
Libs/Widgets/Testing/Cpp/ctkMenuComboBoxTest1.cpp

@@ -65,7 +65,7 @@ int ctkMenuComboBoxTest1(int argc, char * argv [] )
   Menu2->setAutoFillBackground(true);
   Menu2->setAutoFillBackground(true);
   Menu2->setMinimumContentsLength(25);
   Menu2->setMinimumContentsLength(25);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
-  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnDoubleClick);
+  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnPopup);
   //Menu2->show();
   //Menu2->show();
 
 
 /*
 /*

+ 18 - 20
Libs/Widgets/ctkMenuComboBox.cpp

@@ -20,7 +20,6 @@
 
 
 // Qt includes
 // Qt includes
 #include <QActionEvent>
 #include <QActionEvent>
-#include <QComboBox>
 #include <QCompleter>
 #include <QCompleter>
 #include <QDebug>
 #include <QDebug>
 #include <QEvent>
 #include <QEvent>
@@ -53,6 +52,7 @@ void ctkMenuComboBoxInternal::showPopup()
   menu->popup(this->mapToGlobal(this->rect().bottomLeft()));
   menu->popup(this->mapToGlobal(this->rect().bottomLeft()));
   static int minWidth = menu->sizeHint().width();
   static int minWidth = menu->sizeHint().width();
   menu->setFixedWidth(qMax(this->width(), minWidth));
   menu->setFixedWidth(qMax(this->width(), minWidth));
+  emit popupShown();
 }
 }
 
 
 // -------------------------------------------------------------------------
 // -------------------------------------------------------------------------
@@ -69,7 +69,7 @@ ctkMenuComboBoxPrivate::ctkMenuComboBoxPrivate(ctkMenuComboBox& object)
 {
 {
   this->MenuComboBox = 0;
   this->MenuComboBox = 0;
   this->SearchCompleter = 0;
   this->SearchCompleter = 0;
-  this->EditBehavior = ctkMenuComboBox::EditableOnDoubleClick;
+  this->EditBehavior = ctkMenuComboBox::EditableOnPopup;
   this->IsDefaultTextCurrent = true;
   this->IsDefaultTextCurrent = true;
   this->IsDefaultIconCurrent = true;
   this->IsDefaultIconCurrent = true;
 }
 }
@@ -78,6 +78,8 @@ ctkMenuComboBoxPrivate::ctkMenuComboBoxPrivate(ctkMenuComboBox& object)
 void ctkMenuComboBoxPrivate::init()
 void ctkMenuComboBoxPrivate::init()
 {
 {
   Q_Q(ctkMenuComboBox);
   Q_Q(ctkMenuComboBox);
+  this->setParent(q);
+
   QHBoxLayout* layout = new QHBoxLayout(q);
   QHBoxLayout* layout = new QHBoxLayout(q);
   layout->setContentsMargins(0,0,0,0);
   layout->setContentsMargins(0,0,0,0);
   layout->setSizeConstraint(QLayout::SetMinimumSize);
   layout->setSizeConstraint(QLayout::SetMinimumSize);
@@ -351,6 +353,8 @@ void ctkMenuComboBox::setEditableBehavior(ctkMenuComboBox::EditableBehavior edit
 {
 {
   Q_D(ctkMenuComboBox);
   Q_D(ctkMenuComboBox);
   d->EditBehavior = edit;
   d->EditBehavior = edit;
+      this->disconnect(d->MenuComboBox, SIGNAL(popupShown()),
+                    d, SLOT(setComboBoxEditable()));
   switch (edit)
   switch (edit)
   {
   {
     case ctkMenuComboBox::Editable:
     case ctkMenuComboBox::Editable:
@@ -369,8 +373,10 @@ void ctkMenuComboBox::setEditableBehavior(ctkMenuComboBox::EditableBehavior edit
       // which call a popup, because the focus is losted.
       // which call a popup, because the focus is losted.
       d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
       d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
       break;
       break;
-    case ctkMenuComboBox::EditableOnDoubleClick:
+    case ctkMenuComboBox::EditableOnPopup:
       d->setComboBoxEditable(false);
       d->setComboBoxEditable(false);
+      this->connect(d->MenuComboBox, SIGNAL(popupShown()),
+                    d, SLOT(setComboBoxEditable()));
       // Same reason.
       // Same reason.
       d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
       d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
       break;
       break;
@@ -445,27 +451,20 @@ bool ctkMenuComboBox::eventFilter(QObject* target, QEvent* event)
 
 
   if (target == d->MenuComboBox)
   if (target == d->MenuComboBox)
     {
     {
-    if (event->type() == QEvent::MouseButtonDblClick &&
-        d->EditBehavior == ctkMenuComboBox::EditableOnDoubleClick)
-      {
-      d->setComboBoxEditable(true);
-      }
     if (event->type() == QEvent::Resize)
     if (event->type() == QEvent::Resize)
       {
       {
       this->layout()->invalidate();
       this->layout()->invalidate();
       }
       }
-    if(d->EditBehavior == ctkMenuComboBox::EditableOnFocus ||
-       d->EditBehavior == ctkMenuComboBox::EditableOnDoubleClick)
+    if (event->type() == QEvent::FocusIn &&
+        d->EditBehavior == ctkMenuComboBox::EditableOnFocus)
       {
       {
-      if (event->type() == QEvent::FocusIn &&
-          d->EditBehavior == ctkMenuComboBox::EditableOnFocus)
-        {
-        d->setComboBoxEditable(true);
-        }
-      if (event->type() == QEvent::FocusOut)
-        {
-        d->setComboBoxEditable(false);
-        }
+      d->setComboBoxEditable(true);
+      }
+    if (event->type() == QEvent::FocusOut &&
+        (d->EditBehavior == ctkMenuComboBox::EditableOnFocus ||
+         d->EditBehavior == ctkMenuComboBox::EditableOnPopup))
+      {
+      d->setComboBoxEditable(false);
       }
       }
     }
     }
   else if (event->type() == QEvent::ActionAdded)
   else if (event->type() == QEvent::ActionAdded)
@@ -480,4 +479,3 @@ bool ctkMenuComboBox::eventFilter(QObject* target, QEvent* event)
     }
     }
   return this->Superclass::eventFilter(target, event);
   return this->Superclass::eventFilter(target, event);
 }
 }
-

+ 1 - 1
Libs/Widgets/ctkMenuComboBox.h

@@ -59,7 +59,7 @@ public:
     NotEditable = 0,
     NotEditable = 0,
     Editable,
     Editable,
     EditableOnFocus,
     EditableOnFocus,
-    EditableOnDoubleClick
+    EditableOnPopup
   };
   };
 
 
   /// Superclass typedef
   /// Superclass typedef

+ 11 - 4
Libs/Widgets/ctkMenuComboBox_p.h

@@ -22,6 +22,7 @@
 #define __ctkMenuComboBox_p_h
 #define __ctkMenuComboBox_p_h
 
 
 // Qt includes
 // Qt includes
+#include <QComboBox>
 #include <QWeakPointer>
 #include <QWeakPointer>
 
 
 // CTK includes
 // CTK includes
@@ -30,6 +31,7 @@
 
 
 class ctkMenuComboBoxInternal: public QComboBox
 class ctkMenuComboBoxInternal: public QComboBox
 {
 {
+  Q_OBJECT
 public:
 public:
   /// Superclass typedef
   /// Superclass typedef
   typedef QComboBox Superclass;
   typedef QComboBox Superclass;
@@ -39,14 +41,17 @@ public:
   virtual void showPopup();
   virtual void showPopup();
 
 
   virtual QSize minimumSizeHint()const;
   virtual QSize minimumSizeHint()const;
+signals:
+  void popupShown();
+public:
   QWeakPointer<QMenu>  Menu;
   QWeakPointer<QMenu>  Menu;
 };
 };
 
 
 // -------------------------------------------------------------------------
 // -------------------------------------------------------------------------
-class ctkMenuComboBoxPrivate
+class ctkMenuComboBoxPrivate: public QObject
 {
 {
+  Q_OBJECT
   Q_DECLARE_PUBLIC(ctkMenuComboBox);
   Q_DECLARE_PUBLIC(ctkMenuComboBox);
-
 protected:
 protected:
   ctkMenuComboBox* const q_ptr;
   ctkMenuComboBox* const q_ptr;
 public:
 public:
@@ -59,14 +64,16 @@ public:
   void setCurrentIcon(const QIcon& newCurrentIcon);
   void setCurrentIcon(const QIcon& newCurrentIcon);
   QIcon currentIcon()const;
   QIcon currentIcon()const;
 
 
-  void setComboBoxEditable(bool);
-
   void addAction(QAction* action);
   void addAction(QAction* action);
   void addMenuToCompleter(QMenu* menu);
   void addMenuToCompleter(QMenu* menu);
   void addActionToCompleter(QAction* action);
   void addActionToCompleter(QAction* action);
 
 
   void removeActionToCompleter(QAction* action);
   void removeActionToCompleter(QAction* action);
 
 
+public slots:
+  void setComboBoxEditable(bool editable = true);
+
+protected:
   QIcon         DefaultIcon;
   QIcon         DefaultIcon;
   QString       DefaultText;
   QString       DefaultText;
   bool          IsDefaultTextCurrent;
   bool          IsDefaultTextCurrent;