Browse Source

ctkMenuComboBox now uses a more flexible completer

it apply the filter on the beginning of words in space separated strings
That way, typing "ta" will not filter out "toto tata tutu", but "oto"
would
Julien Finet 14 years ago
parent
commit
9eb2194597

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

@@ -87,7 +87,7 @@ int ctkMenuComboBoxTest1(int argc, char * argv [] )
 
 
   file->addAction(plus, "Add ...");
   file->addAction(plus, "Add ...");
   file->addAction("Saveeeeeeeeeeeeeeeeeeeeeee ...");
   file->addAction("Saveeeeeeeeeeeeeeeeeeeeeee ...");
-  wizards->addAction("tutu");
+  wizards->addAction("tutu toto tata tonton");
   wizards->addMenu(informatics);
   wizards->addMenu(informatics);
   informatics->addAction("ddd");
   informatics->addAction("ddd");
 
 

+ 17 - 14
Libs/Widgets/ctkMenuComboBox.cpp

@@ -29,6 +29,7 @@
 #include <QStringListModel>
 #include <QStringListModel>
 
 
 // CTK includes
 // CTK includes
+#include "ctkCompleter.h"
 #include "ctkSearchBox.h"
 #include "ctkSearchBox.h"
 #include "ctkMenuComboBox_p.h"
 #include "ctkMenuComboBox_p.h"
 
 
@@ -91,8 +92,9 @@ void ctkMenuComboBoxPrivate::init()
   this->MenuComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
   this->MenuComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
   this->MenuComboBox->addItem(this->DefaultIcon, this->DefaultText);
   this->MenuComboBox->addItem(this->DefaultIcon, this->DefaultText);
 
 
-  this->SearchCompleter = new QCompleter(QStringList(), q);
+  this->SearchCompleter = new ctkCompleter(QStringList(), q);
   this->SearchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
   this->SearchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
+  this->SearchCompleter->setModelFiltering(ctkCompleter::FilterWordStartsWith);
   q->connect(this->SearchCompleter, SIGNAL(activated(const QString&)),
   q->connect(this->SearchCompleter, SIGNAL(activated(const QString&)),
              q, SLOT(onEditingFinished()));
              q, SLOT(onEditingFinished()));
 
 
@@ -220,25 +222,27 @@ void ctkMenuComboBoxPrivate::addMenuToCompleter(QMenu* menu)
 // -------------------------------------------------------------------------
 // -------------------------------------------------------------------------
 void ctkMenuComboBoxPrivate::addActionToCompleter(QAction *action)
 void ctkMenuComboBoxPrivate::addActionToCompleter(QAction *action)
 {
 {
-  Q_ASSERT(qobject_cast<QStringListModel* >(this->SearchCompleter->model()));
-  QModelIndex start = this->SearchCompleter->model()->index(0,0);
-  QModelIndexList indexList = this->SearchCompleter->model()->match(
-    start, 0, action->text());
+  QStringListModel* model = qobject_cast<QStringListModel* >(
+    this->SearchCompleter->sourceModel());
+  Q_ASSERT(model);
+  QModelIndex start = model->index(0,0);
+  QModelIndexList indexList = model->match(start, 0, action->text());
   if (indexList.count())
   if (indexList.count())
     {
     {
     return;
     return;
     }
     }
 
 
-  int actionCount = this->SearchCompleter->model()->rowCount();
-  this->SearchCompleter->model()->insertRow(actionCount);
-  QModelIndex index = this->SearchCompleter->model()->index(actionCount, 0);
-  this->SearchCompleter->model()->setData(index, action->text());
+  int actionCount = model->rowCount();
+  model->insertRow(actionCount);
+  QModelIndex index = model->index(actionCount, 0);
+  model->setData(index, action->text());
 }
 }
 
 
 //  ------------------------------------------------------------------------
 //  ------------------------------------------------------------------------
 void ctkMenuComboBoxPrivate::removeActionToCompleter(QAction *action)
 void ctkMenuComboBoxPrivate::removeActionToCompleter(QAction *action)
 {
 {
-  QStringListModel* model = qobject_cast<QStringListModel* >(this->SearchCompleter->model());
+  QStringListModel* model = qobject_cast<QStringListModel* >(
+    this->SearchCompleter->sourceModel());
   Q_ASSERT(model);
   Q_ASSERT(model);
   if (!model->stringList().contains(action->text()) )
   if (!model->stringList().contains(action->text()) )
     {
     {
@@ -253,14 +257,13 @@ void ctkMenuComboBoxPrivate::removeActionToCompleter(QAction *action)
     return;
     return;
     }
     }
 
 
-  QModelIndex start = this->SearchCompleter->model()->index(0,0);
-  QModelIndexList indexList = this->SearchCompleter->model()->match(
-      start, 0, action->text());
+  QModelIndex start = model->index(0,0);
+  QModelIndexList indexList = model->match(start, 0, action->text());
   Q_ASSERT(indexList.count() == 1);
   Q_ASSERT(indexList.count() == 1);
   foreach (QModelIndex index, indexList)
   foreach (QModelIndex index, indexList)
     {
     {
     // Search completer model is a flat list
     // Search completer model is a flat list
-    this->SearchCompleter->model()->removeRow(index.row());
+    model->removeRow(index.row());
     }
     }
 }
 }
 
 

+ 2 - 2
Libs/Widgets/ctkMenuComboBox_p.h

@@ -27,7 +27,7 @@
 
 
 // CTK includes
 // CTK includes
 #include "ctkMenuComboBox.h"
 #include "ctkMenuComboBox.h"
-#include "ctkSearchBox.h"
+class ctkCompleter;
 
 
 class ctkMenuComboBoxInternal: public QComboBox
 class ctkMenuComboBoxInternal: public QComboBox
 {
 {
@@ -82,7 +82,7 @@ protected:
   ctkMenuComboBox::EditableBehavior EditBehavior;
   ctkMenuComboBox::EditableBehavior EditBehavior;
 
 
   ctkMenuComboBoxInternal*    MenuComboBox;
   ctkMenuComboBoxInternal*    MenuComboBox;
-  QCompleter*                 SearchCompleter;
+  ctkCompleter*               SearchCompleter;
   QWeakPointer<QMenu>         Menu;
   QWeakPointer<QMenu>         Menu;
 };
 };