Selaa lähdekoodia

ctkPythonConsole - Implement AutocompletePreferenceList

If a dot as been entered and if an item of possible
choices matches one of the preference list, it will be selected.

For example, in order to autocomplete the text "qt." with QPusbButton,
the list should contains: "qt.QPushButton"

By adding to the list "QPushButton" instead of "qt.QPushButton", all
module contains "QPushButton" will be autocompleted following
that preference.
Jean-Christophe Fillion-Robin 14 vuotta sitten
vanhempi
commit
e94a185743

+ 4 - 0
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -62,6 +62,10 @@ int main(int argc, char** argv)
 
   console.setProperty("isInteractive", parsedArgs.contains("interactive"));
 
+  QStringList list;
+  list << "qt.QPushButton";
+  console.completer()->setAutocompletePreferenceList(list);
+
   pythonManager.addObjectToPythonMain("_ctkPythonConsoleInstance", &console);
 
   ctkTestWrappedQProperty testWrappedQProperty;

+ 42 - 1
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -141,7 +141,48 @@ public:
       this->setModel(new QStringListModel(attrs, this));
       this->setCaseSensitivity(Qt::CaseInsensitive);
       this->setCompletionPrefix(compareText.toLower());
-      this->popup()->setCurrentIndex(this->completionModel()->index(0, 0));
+
+      // If a dot as been entered and if an item of possible
+      // choices matches one of the preference list, it will be selected.
+      QModelIndex preferredIndex = this->completionModel()->index(0, 0);
+      int dotCount = completion.count('.');
+      if (dotCount == 0 || completion.at(completion.count() - 1) == '.')
+        {
+        foreach(const QString& pref, this->AutocompletePreferenceList)
+          {
+          if (dotCount < pref.count('.'))
+            {
+            continue;
+            }
+          // Extract string before the last dot
+          int lastDot = pref.lastIndexOf('.');
+          QString prefBeforeLastDot;
+          if (lastDot != -1)
+            {
+            prefBeforeLastDot = pref.left(lastDot);
+            }
+          // qDebug() << "prefLookup" << prefBeforeLastDot;
+          if (!prefBeforeLastDot.isEmpty() && QString::compare(prefBeforeLastDot, lookup) != 0)
+            {
+            break;
+            }
+          QString prefAfterLastDot = pref;
+          if (lastDot != -1 )
+            {
+            prefAfterLastDot = pref.right(pref.size() - lastDot - 1);
+            }
+          // qDebug() << "prefAfterLastDot" << prefAfterLastDot;
+          QModelIndexList list = this->completionModel()->match(
+                this->completionModel()->index(0, 0), Qt::DisplayRole, QVariant(prefAfterLastDot));
+          if (list.count() > 0)
+            {
+            preferredIndex = list.first();
+            break;
+            }
+          }
+        }
+
+      this->popup()->setCurrentIndex(preferredIndex);
       }
     }
   ctkAbstractPythonManager& PythonManager;