----------------------------------------------------------------------------
| 410 | |
| 411 | //---------------------------------------------------------------------------- |
| 412 | void ctkPythonConsoleCompleter::updateCompletionModel(const QString& completion) |
| 413 | { |
| 414 | Q_D(ctkPythonConsoleCompleter); |
| 415 | // Start by clearing the model |
| 416 | this->setModel(0); |
| 417 | |
| 418 | // Don't try to complete the empty string |
| 419 | if (completion.isEmpty()) |
| 420 | { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | bool appendParenthesis = true; |
| 425 | // Search backward through the string for usable characters |
| 426 | QString textToComplete = ctkPythonConsoleCompleterPrivate::searchUsableCharForCompletion(completion); |
| 427 | |
| 428 | // Split the string at the last dot, if one exists |
| 429 | QString lookup; |
| 430 | QString compareText = textToComplete; |
| 431 | int dot = compareText.lastIndexOf('.'); |
| 432 | if (dot != -1) |
| 433 | { |
| 434 | lookup = compareText.mid(0, dot); |
| 435 | compareText = compareText.mid(dot+1); |
| 436 | } |
| 437 | |
| 438 | // Lookup python names |
| 439 | QStringList attrs; |
| 440 | if (!lookup.isEmpty() || !compareText.isEmpty()) |
| 441 | { |
| 442 | QString module = "__main__"; |
| 443 | attrs = d->PythonManager.pythonAttributes(lookup, module.toLatin1(), appendParenthesis); |
| 444 | module = "__main__.__builtins__"; |
| 445 | attrs << d->PythonManager.pythonAttributes(lookup, module.toLatin1(), |
| 446 | appendParenthesis); |
| 447 | attrs.removeDuplicates(); |
| 448 | #if QT_VERSION >= QT_VERSION_CHECK(5,2,0) |
| 449 | std::sort(attrs.begin(), attrs.end(), ctkPythonConsoleCompleterPrivate::PythonAttributeLessThan); |
| 450 | #else |
| 451 | qSort(attrs.begin(), attrs.end(), ctkPythonConsoleCompleterPrivate::PythonAttributeLessThan); |
| 452 | #endif |
| 453 | } |
| 454 | |
| 455 | // Initialize the completion model |
| 456 | if (!attrs.isEmpty()) |
| 457 | { |
| 458 | this->setCompletionMode(QCompleter::PopupCompletion); |
| 459 | this->setModel(new QStringListModel(attrs, this)); |
| 460 | this->setCaseSensitivity(Qt::CaseInsensitive); |
| 461 | this->setCompletionPrefix(compareText.toLower()); |
| 462 | |
| 463 | // If a dot as been entered and if an item of possible |
| 464 | // choices matches one of the preference list, it will be selected. |
| 465 | QModelIndex preferredIndex = this->completionModel()->index(0, 0); |
| 466 | int dotCount = completion.count('.'); |
| 467 | if (dotCount == 0 || completion.at(completion.count() - 1) == '.') |
| 468 | { |
| 469 | foreach(const QString& pref, this->AutocompletePreferenceList) |
no test coverage detected