瀏覽代碼

Add new property to ctkMenuComboBox with a search icon.

Added a search icon next to the menu combobox.
New default behavior : ctkMenuComboBox is not editable with the search
icon visible.
Benjamin Long 13 年之前
父節點
當前提交
3fa0e674b6

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

@@ -64,7 +64,9 @@ int ctkMenuComboBoxTest1(int argc, char * argv [] )
   Menu2->setDefaultText("Search");
   Menu2->setDefaultText("Search");
   Menu2->setAutoFillBackground(true);
   Menu2->setAutoFillBackground(true);
   Menu2->setMinimumContentsLength(25);
   Menu2->setMinimumContentsLength(25);
-  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
+  Menu2->setSearchIconVisible(false);
+  Menu2->setSearchIconVisible(true);
+  //Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnPopup);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnPopup);
   //Menu2->show();
   //Menu2->show();
 
 

+ 50 - 2
Libs/Widgets/ctkMenuComboBox.cpp

@@ -27,6 +27,7 @@
 #include <QLineEdit>
 #include <QLineEdit>
 #include <QStringList>
 #include <QStringList>
 #include <QStringListModel>
 #include <QStringListModel>
+#include <QToolButton>
 
 
 // CTK includes
 // CTK includes
 #include "ctkCompleter.h"
 #include "ctkCompleter.h"
@@ -70,7 +71,7 @@ ctkMenuComboBoxPrivate::ctkMenuComboBoxPrivate(ctkMenuComboBox& object)
 {
 {
   this->MenuComboBox = 0;
   this->MenuComboBox = 0;
   this->SearchCompleter = 0;
   this->SearchCompleter = 0;
-  this->EditBehavior = ctkMenuComboBox::EditableOnPopup;
+  this->EditBehavior = ctkMenuComboBox::NotEditable;
   this->IsDefaultTextCurrent = true;
   this->IsDefaultTextCurrent = true;
   this->IsDefaultIconCurrent = true;
   this->IsDefaultIconCurrent = true;
 }
 }
@@ -84,7 +85,19 @@ void ctkMenuComboBoxPrivate::init()
   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);
-
+  layout->setSpacing(0);
+
+  // SearchButton
+  this->SearchButton = new QToolButton();
+  this->SearchButton->setIcon(QIcon(":/Icons/search.svg"));
+  this->SearchButton->setCheckable(true);
+  this->SearchButton->setAutoRaise(true);
+  this->SearchButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Ignored);
+  layout->addWidget(this->SearchButton);
+  q->connect(this->SearchButton, SIGNAL(toggled(bool)),
+             this, SLOT(setComboBoxEditable(bool)));
+
+  // MenuComboBox
   this->MenuComboBox = new ctkMenuComboBoxInternal();
   this->MenuComboBox = new ctkMenuComboBoxInternal();
   this->MenuComboBox->setMinimumContentsLength(12);
   this->MenuComboBox->setMinimumContentsLength(12);
   layout->addWidget(this->MenuComboBox);
   layout->addWidget(this->MenuComboBox);
@@ -174,6 +187,11 @@ void ctkMenuComboBoxPrivate::setComboBoxEditable(bool edit)
       {
       {
       ctkSearchBox* line = new ctkSearchBox();
       ctkSearchBox* line = new ctkSearchBox();
       this->MenuComboBox->setLineEdit(line);
       this->MenuComboBox->setLineEdit(line);
+      if (q->isSearchIconVisible())
+        {
+        this->MenuComboBox->lineEdit()->selectAll();
+        this->MenuComboBox->setFocus();
+        }
       q->connect(line, SIGNAL(editingFinished()),
       q->connect(line, SIGNAL(editingFinished()),
                  q,SLOT(onEditingFinished()));
                  q,SLOT(onEditingFinished()));
       }
       }
@@ -397,6 +415,20 @@ ctkMenuComboBox::EditableBehavior ctkMenuComboBox::editableBehavior()const
 }
 }
 
 
 // -------------------------------------------------------------------------
 // -------------------------------------------------------------------------
+void ctkMenuComboBox::setSearchIconVisible(bool state)
+{
+  Q_D(ctkMenuComboBox);
+  d->SearchButton->setVisible(state);
+}
+
+// -------------------------------------------------------------------------
+bool ctkMenuComboBox::isSearchIconVisible() const
+{
+  Q_D(const ctkMenuComboBox);
+  return d->SearchButton->isVisibleTo(const_cast<ctkMenuComboBox*>(this));
+}
+
+// -------------------------------------------------------------------------
 void ctkMenuComboBox::setMinimumContentsLength(int characters)
 void ctkMenuComboBox::setMinimumContentsLength(int characters)
 {
 {
   Q_D(ctkMenuComboBox);
   Q_D(ctkMenuComboBox);
@@ -451,6 +483,11 @@ void ctkMenuComboBox::onEditingFinished()
     {
     {
     return;
     return;
     }
     }
+  if (this->isSearchIconVisible())
+    {
+    d->SearchButton->setChecked(false);
+    }
+
   action->trigger();
   action->trigger();
 }
 }
 
 
@@ -489,3 +526,14 @@ bool ctkMenuComboBox::eventFilter(QObject* target, QEvent* event)
     }
     }
   return this->Superclass::eventFilter(target, event);
   return this->Superclass::eventFilter(target, event);
 }
 }
+
+// -------------------------------------------------------------------------
+void ctkMenuComboBox::resizeEvent(QResizeEvent *event)
+{
+  Q_D(ctkMenuComboBox);
+  this->Superclass::resizeEvent(event);
+  if (this->isSearchIconVisible())
+    {
+    d->SearchButton->setFixedWidth(d->MenuComboBox->size().height());
+    }
+}

+ 9 - 1
Libs/Widgets/ctkMenuComboBox.h

@@ -43,8 +43,9 @@ class ctkMenuComboBoxPrivate;
 ///   if it is disabled :
 ///   if it is disabled :
 /// the ctkMenuComboBox has the same behavior as a QPushButton. You can't filter the menu.
 /// the ctkMenuComboBox has the same behavior as a QPushButton. You can't filter the menu.
 
 
-/// By default ctkMenuComboBox is editable on double click.
+/// By default ctkMenuComboBox is not editable with the search icon visible.
 /// See ctkmenuComboBox::setEditableType() to change the default behavior.
 /// See ctkmenuComboBox::setEditableType() to change the default behavior.
+/// and setIconSearchVisible() to show/hide the icon.
 
 
 class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
 class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
 {
 {
@@ -53,6 +54,7 @@ class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
   Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
   Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
   Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
   Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
   Q_PROPERTY(EditableBehavior editBehavior READ editableBehavior WRITE setEditableBehavior)
   Q_PROPERTY(EditableBehavior editBehavior READ editableBehavior WRITE setEditableBehavior)
+  Q_PROPERTY(bool searchIconVisible READ isSearchIconVisible WRITE setSearchIconVisible)
 
 
 public:
 public:
   enum EditableBehavior{
   enum EditableBehavior{
@@ -87,10 +89,16 @@ public:
   void setEditableBehavior(EditableBehavior editBehavior);
   void setEditableBehavior(EditableBehavior editBehavior);
   EditableBehavior editableBehavior()const;
   EditableBehavior editableBehavior()const;
 
 
+  /// set the icon search visible
+  void setSearchIconVisible(bool state);
+  bool isSearchIconVisible() const;
+
   /// See QComboBox::setMinimumContentsLength()
   /// See QComboBox::setMinimumContentsLength()
   void setMinimumContentsLength(int characters);
   void setMinimumContentsLength(int characters);
 
 
+protected:
   virtual bool eventFilter(QObject* target, QEvent* event);
   virtual bool eventFilter(QObject* target, QEvent* event);
+  virtual void resizeEvent(QResizeEvent *event);
 
 
 public slots:
 public slots:
   void clearActiveAction();
   void clearActiveAction();

+ 2 - 0
Libs/Widgets/ctkMenuComboBox_p.h

@@ -28,6 +28,7 @@
 // CTK includes
 // CTK includes
 #include "ctkMenuComboBox.h"
 #include "ctkMenuComboBox.h"
 class ctkCompleter;
 class ctkCompleter;
+class QToolButton;
 
 
 class ctkMenuComboBoxInternal: public QComboBox
 class ctkMenuComboBoxInternal: public QComboBox
 {
 {
@@ -84,6 +85,7 @@ protected:
   ctkMenuComboBoxInternal*    MenuComboBox;
   ctkMenuComboBoxInternal*    MenuComboBox;
   ctkCompleter*               SearchCompleter;
   ctkCompleter*               SearchCompleter;
   QWeakPointer<QMenu>         Menu;
   QWeakPointer<QMenu>         Menu;
+  QToolButton*                SearchButton;
 };
 };
 
 
 #endif
 #endif