aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonkitaspect.cpp
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2024-10-02 16:57:44 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2024-10-09 09:15:04 +0000
commit79774519ccaa19fbee58c8a191281f286eb83be2 (patch)
tree35a8ce94247c7cbe126148e0cff981c3841fe791 /src/plugins/python/pythonkitaspect.cpp
parent5a09ce5d53357f312df741beb731cd2d2a04dc5b (diff)
Python: Use model in aspect combo box
For icons and tooltips consistent with the settings page. Task-number: QTCREATORBUG-31574 Change-Id: Ib567f43e0b29fa058770eefb4b16e8610fd28adf Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/python/pythonkitaspect.cpp')
-rw-r--r--src/plugins/python/pythonkitaspect.cpp61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/plugins/python/pythonkitaspect.cpp b/src/plugins/python/pythonkitaspect.cpp
index 8bdc557d946..c555caff657 100644
--- a/src/plugins/python/pythonkitaspect.cpp
+++ b/src/plugins/python/pythonkitaspect.cpp
@@ -20,8 +20,49 @@ using namespace ProjectExplorer;
using namespace Utils;
namespace Python {
+namespace Internal {
-using namespace Internal;
+class PythonAspectSortModel : public SortModel
+{
+public:
+ PythonAspectSortModel(QObject *parent) : SortModel(parent) {}
+
+ void reset()
+ {
+ beginResetModel();
+ if (QAbstractItemModel * const model = sourceModel())
+ model->deleteLater();
+ ListModel<Interpreter> * const model = createInterpreterModel(this);
+ model->setAllData(model->allData() << Interpreter("none", {}, {}));
+ setSourceModel(model);
+ endResetModel();
+ sort(0);
+ }
+
+private:
+ bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override
+ {
+ const auto source = static_cast<ListModel<Interpreter> *>(sourceModel());
+ const auto item1 = static_cast<ListItem<Interpreter> *>(source->itemForIndex(source_left));
+ const auto item2 = static_cast<ListItem<Interpreter> *>(source->itemForIndex(source_right));
+ QTC_ASSERT(item1 && item2, return false);
+
+ // Criterion 1: "None" comes last
+ if (item1->itemData.id == "none")
+ return false;
+ if (item2->itemData.id == "none")
+ return true;
+
+ // Criterion 2: Valid interpreters come before invalid ones.
+ const bool item1Valid = PythonSettings::interpreterIsValid(item1->itemData);
+ const bool item2Valid = PythonSettings::interpreterIsValid(item2->itemData);
+ if (item1Valid != item2Valid)
+ return item1Valid;
+
+ // Criterion 3: Name.
+ return SortModel::lessThan(source_left, source_right);
+ }
+};
class PythonKitAspectImpl final : public KitAspect
{
@@ -33,6 +74,7 @@ public:
m_comboBox = createSubWidget<QComboBox>();
m_comboBox->setSizePolicy(QSizePolicy::Ignored, m_comboBox->sizePolicy().verticalPolicy());
+ m_comboBox->setModel(new PythonAspectSortModel(this));
refresh();
m_comboBox->setToolTip(kitInfo->description());
@@ -56,19 +98,18 @@ public:
void refresh() override
{
const GuardLocker locker(m_ignoreChanges);
- m_comboBox->clear();
- m_comboBox->addItem(Tr::tr("None"), QString());
-
- for (const Interpreter &interpreter : PythonSettings::interpreters())
- m_comboBox->addItem(interpreter.name, interpreter.id);
-
+ static_cast<PythonAspectSortModel *>(m_comboBox->model())->reset();
updateComboBox(PythonKitAspect::python(kit()));
emit changed(); // we need to emit changed here to update changes in the macro expander
}
void updateComboBox(const std::optional<Interpreter> &python)
{
- const int index = python ? std::max(m_comboBox->findData(python->id), 0) : 0;
+ int index = m_comboBox->count() - 1;
+ if (python) {
+ if (const int idx = m_comboBox->findData(python->id); idx != -1)
+ index = idx;
+ }
m_comboBox->setCurrentIndex(index);
}
@@ -175,6 +216,10 @@ public:
}
};
+} // Internal
+
+using namespace Internal;
+
std::optional<Interpreter> PythonKitAspect::python(const Kit *kit)
{
QTC_ASSERT(kit, return {});