Python: Avoid flashing a separate widget when opening settings

Change-Id: Id0518c222ffa91bc8791cfa71edb9b715ce65cde
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2022-11-17 14:58:11 +01:00
parent 4db30d9b60
commit dc620e987d

View File

@@ -80,8 +80,9 @@ class InterpreterDetailsWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
InterpreterDetailsWidget() InterpreterDetailsWidget(QWidget *parent)
: m_name(new QLineEdit) : QWidget(parent)
, m_name(new QLineEdit)
, m_executable(new PathChooser()) , m_executable(new PathChooser())
{ {
m_executable->setExpectedKind(PathChooser::ExistingCommand); m_executable->setExpectedKind(PathChooser::ExistingCommand);
@@ -131,7 +132,7 @@ public:
QList<Interpreter> interpreterFrom(const QString &detectionSource) const; QList<Interpreter> interpreterFrom(const QString &detectionSource) const;
private: private:
QTreeView m_view; QTreeView *m_view = nullptr;
ListModel<Interpreter> m_model; ListModel<Interpreter> m_model;
InterpreterDetailsWidget *m_detailsWidget = nullptr; InterpreterDetailsWidget *m_detailsWidget = nullptr;
QPushButton *m_deleteButton = nullptr; QPushButton *m_deleteButton = nullptr;
@@ -149,7 +150,7 @@ private:
}; };
InterpreterOptionsWidget::InterpreterOptionsWidget() InterpreterOptionsWidget::InterpreterOptionsWidget()
: m_detailsWidget(new InterpreterDetailsWidget()) : m_detailsWidget(new InterpreterDetailsWidget(this))
, m_defaultId(PythonSettings::defaultInterpreter().id) , m_defaultId(PythonSettings::defaultInterpreter().id)
{ {
m_model.setDataAccessor([this](const Interpreter &interpreter, int column, int role) -> QVariant { m_model.setDataAccessor([this](const Interpreter &interpreter, int column, int role) -> QVariant {
@@ -180,23 +181,17 @@ InterpreterOptionsWidget::InterpreterOptionsWidget()
}); });
m_model.setAllData(PythonSettings::interpreters()); m_model.setAllData(PythonSettings::interpreters());
m_view.setModel(&m_model); auto addButton = new QPushButton(Tr::tr("&Add"), this);
m_view.setHeaderHidden(true);
m_view.setSelectionMode(QAbstractItemView::SingleSelection);
m_view.setSelectionBehavior(QAbstractItemView::SelectItems);
auto addButton = new QPushButton(Tr::tr("&Add"));
m_deleteButton = new QPushButton(Tr::tr("&Delete")); m_deleteButton = new QPushButton(Tr::tr("&Delete"), this);
m_deleteButton->setEnabled(false); m_deleteButton->setEnabled(false);
m_makeDefaultButton = new QPushButton(Tr::tr("&Make Default")); m_makeDefaultButton = new QPushButton(Tr::tr("&Make Default"));
m_makeDefaultButton->setEnabled(false); m_makeDefaultButton->setEnabled(false);
m_cleanButton = new QPushButton(Tr::tr("&Clean Up")); m_cleanButton = new QPushButton(Tr::tr("&Clean Up"), this);
m_cleanButton->setToolTip(Tr::tr("Remove all Python interpreters without a valid executable.")); m_cleanButton->setToolTip(Tr::tr("Remove all Python interpreters without a valid executable."));
updateCleanButton(); m_view = new QTreeView(this);
m_detailsWidget->hide();
Column buttons { Column buttons {
addButton, addButton,
@@ -207,10 +202,19 @@ InterpreterOptionsWidget::InterpreterOptionsWidget()
}; };
Column { Column {
Row { &m_view, buttons }, Row { m_view, buttons },
m_detailsWidget m_detailsWidget
}.attachTo(this); }.attachTo(this);
updateCleanButton();
m_detailsWidget->hide();
m_view->setModel(&m_model);
m_view->setHeaderHidden(true);
m_view->setSelectionMode(QAbstractItemView::SingleSelection);
m_view->setSelectionBehavior(QAbstractItemView::SelectItems);
connect(addButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::addItem); connect(addButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::addItem);
connect(m_deleteButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::deleteItem); connect(m_deleteButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::deleteItem);
connect(m_makeDefaultButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::makeDefault); connect(m_makeDefaultButton, &QPushButton::pressed, this, &InterpreterOptionsWidget::makeDefault);
@@ -218,7 +222,7 @@ InterpreterOptionsWidget::InterpreterOptionsWidget()
connect(m_detailsWidget, &InterpreterDetailsWidget::changed, connect(m_detailsWidget, &InterpreterDetailsWidget::changed,
this, &InterpreterOptionsWidget::detailsChanged); this, &InterpreterOptionsWidget::detailsChanged);
connect(m_view.selectionModel(), &QItemSelectionModel::currentChanged, connect(m_view->selectionModel(), &QItemSelectionModel::currentChanged,
this, &InterpreterOptionsWidget::currentChanged); this, &InterpreterOptionsWidget::currentChanged);
} }
@@ -268,7 +272,7 @@ void InterpreterOptionsWidget::currentChanged(const QModelIndex &index, const QM
void InterpreterOptionsWidget::detailsChanged() void InterpreterOptionsWidget::detailsChanged()
{ {
const QModelIndex &index = m_view.currentIndex(); const QModelIndex &index = m_view->currentIndex();
if (index.isValid()) { if (index.isValid()) {
m_model.itemAt(index.row())->itemData = m_detailsWidget->toInterpreter(); m_model.itemAt(index.row())->itemData = m_detailsWidget->toInterpreter();
emit m_model.dataChanged(index, index); emit m_model.dataChanged(index, index);
@@ -288,13 +292,13 @@ void InterpreterOptionsWidget::addItem()
const QModelIndex &index = m_model.indexForItem( const QModelIndex &index = m_model.indexForItem(
m_model.appendItem({QUuid::createUuid().toString(), QString("Python"), FilePath(), false})); m_model.appendItem({QUuid::createUuid().toString(), QString("Python"), FilePath(), false}));
QTC_ASSERT(index.isValid(), return); QTC_ASSERT(index.isValid(), return);
m_view.setCurrentIndex(index); m_view->setCurrentIndex(index);
updateCleanButton(); updateCleanButton();
} }
void InterpreterOptionsWidget::deleteItem() void InterpreterOptionsWidget::deleteItem()
{ {
const QModelIndex &index = m_view.currentIndex(); const QModelIndex &index = m_view->currentIndex();
if (index.isValid()) if (index.isValid())
m_model.destroyItem(m_model.itemAt(index.row())); m_model.destroyItem(m_model.itemAt(index.row()));
updateCleanButton(); updateCleanButton();
@@ -411,18 +415,10 @@ public:
mainGroupLayout->addWidget(m_advancedLabel); mainGroupLayout->addWidget(m_advancedLabel);
mainGroupLayout->addWidget(m_editor->editorWidget(), 1); mainGroupLayout->addWidget(m_editor->editorWidget(), 1);
setAdvanced(false);
mainGroupLayout->addStretch(); mainGroupLayout->addStretch();
auto advanced = new QCheckBox(Tr::tr("Advanced")); auto advanced = new QCheckBox(Tr::tr("Advanced"));
advanced->setChecked(false); advanced->setChecked(false);
connect(advanced,
&QCheckBox::toggled,
this,
&PyLSConfigureWidget::setAdvanced);
mainGroupLayout->addWidget(advanced); mainGroupLayout->addWidget(advanced);
m_mainGroup->setLayout(mainGroupLayout); m_mainGroup->setLayout(mainGroupLayout);
@@ -434,6 +430,14 @@ public:
m_editor->textDocument()->setPlainText(PythonSettings::pylsConfiguration()); m_editor->textDocument()->setPlainText(PythonSettings::pylsConfiguration());
m_mainGroup->setChecked(PythonSettings::pylsEnabled()); m_mainGroup->setChecked(PythonSettings::pylsEnabled());
updateCheckboxes(); updateCheckboxes();
setAdvanced(false);
connect(advanced,
&QCheckBox::toggled,
this,
&PyLSConfigureWidget::setAdvanced);
} }
void apply() override void apply() override
@@ -518,7 +522,7 @@ static PyLSOptionsPage &pylspOptionsPage()
void InterpreterOptionsWidget::makeDefault() void InterpreterOptionsWidget::makeDefault()
{ {
const QModelIndex &index = m_view.currentIndex(); const QModelIndex &index = m_view->currentIndex();
if (index.isValid()) { if (index.isValid()) {
QModelIndex defaultIndex = m_model.findIndex([this](const Interpreter &interpreter) { QModelIndex defaultIndex = m_model.findIndex([this](const Interpreter &interpreter) {
return interpreter.id == m_defaultId; return interpreter.id == m_defaultId;