Browse Source

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 years ago
parent
commit
2be0ce350c

+ 2 - 0
Libs/Widgets/CMakeLists.txt

@@ -80,6 +80,7 @@ SET(KIT_SRCS
   ctkMenuButton.h
   ctkMenuComboBox.cpp
   ctkMenuComboBox.h
+  ctkMenuComboBox_p.h
   ctkModalityWidget.cpp
   ctkModalityWidget.h
   ctkPathLineEdit.cpp
@@ -202,6 +203,7 @@ SET(KIT_MOC_SRCS
   ctkMatrixWidget.h
   ctkMenuButton.h
   ctkMenuComboBox.h
+  ctkMenuComboBox_p.h
   ctkModalityWidget.h
   ctkPathLineEdit.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->setMinimumContentsLength(25);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
-  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnDoubleClick);
+  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnPopup);
   //Menu2->show();
 
 /*

+ 18 - 20
Libs/Widgets/ctkMenuComboBox.cpp

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

+ 1 - 1
Libs/Widgets/ctkMenuComboBox.h

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

+ 11 - 4
Libs/Widgets/ctkMenuComboBox_p.h

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