diff --git a/src/plugins/python/python/option/interpreterwidget.cpp b/src/plugins/python/python/option/interpreterwidget.cpp index 8b88ecf1a..357c691a5 100644 --- a/src/plugins/python/python/option/interpreterwidget.cpp +++ b/src/plugins/python/python/option/interpreterwidget.cpp @@ -23,6 +23,7 @@ #include #include #include +#include DWIDGET_USE_NAMESPACE class InterpreterModelPrivate @@ -97,7 +98,7 @@ QVariant InterpreterModel::headerData(int section, Qt::Orientation orientation, } } -void InterpreterModel::setCustomData(QVector> &data) +void InterpreterModel::setCustomData(const QVector> &data) { beginResetModel(); d->packageVector = data; @@ -110,6 +111,8 @@ class InterpreterWidgetPrivate DComboBox *interpreterComboBox = nullptr; DLineEdit *pipSourceEdit = nullptr; + QStackedWidget *packagesWidget = nullptr; + DLabel *msgLabel = nullptr; //todo: modified it later: creating a generic component to manage the toolchain DPushButton *selectCustomInterpreter = nullptr; @@ -164,9 +167,16 @@ void InterpreterWidget::setupUi() mainLayout->addWidget(d->selectCustomInterpreter, 0, 2); mainLayout->addWidget(d->removeCustomInterpreter, 0, 3); - auto tableframe = new DFrame(this); - auto tablelayout = new QVBoxLayout(tableframe); - tableframe->setLayout(tablelayout); + auto pkgInfoFrame = new DFrame(this); + auto pkgLayout = new QVBoxLayout(pkgInfoFrame); + pkgInfoFrame->setLayout(pkgLayout); + + d->packagesWidget = new QStackedWidget(this); + d->packagesWidget->setFixedHeight(180); + d->msgLabel = new DLabel(this); + d->msgLabel->setAlignment(Qt::AlignCenter); + d->msgLabel->setWordWrap(true); + d->msgLabel->setForegroundRole(DPalette::TextWarning); DTableView *tableView = new DTableView(); tableView->setFrameShape(QFrame::NoFrame); @@ -174,17 +184,18 @@ void InterpreterWidget::setupUi() tableView->setShowGrid(false); tableView->horizontalHeader()->setSectionResizeMode(DHeaderView::Stretch); tableView->verticalHeader()->hide(); - tablelayout->addWidget(tableView); DHeaderView *headerView = tableView->horizontalHeader(); headerView->setDefaultAlignment(Qt::AlignLeft); - tableView->setSelectionMode(QAbstractItemView::SingleSelection); - d->model = new InterpreterModel(); tableView->setModel(d->model); - tableView->setFixedHeight(180); - mainLayout->addWidget(tableframe, 1, 0, 1, 4); + + d->packagesWidget->addWidget(tableView); + d->packagesWidget->addWidget(d->msgLabel); + pkgLayout->addWidget(d->packagesWidget); + + mainLayout->addWidget(pkgInfoFrame, 1, 0, 1, 4); d->pipSourceEdit = new DLineEdit(this); mainLayout->addWidget(new DLabel(tr("PIP Source:"), this), 2, 0); @@ -257,19 +268,18 @@ void InterpreterWidget::updatePackageData() { auto param = qvariant_cast(d->interpreterComboBox->currentData(Qt::UserRole + 1)); QString cmd = param.path + " -m pip list"; - - QtConcurrent::run(this, &InterpreterWidget::findPackages, cmd); + + QtConcurrent::run(this, &InterpreterWidget::queryPackages, cmd); } -void InterpreterWidget::findPackages(const QString &cmd) +void InterpreterWidget::queryPackages(const QString &cmd) { QProcess process; connect(&process, static_cast(&QProcess::finished), &process, [&](int exitcode, QProcess::ExitStatus exitStatus) { + QueryInfo info; if (0 == exitcode && exitStatus == QProcess::ExitStatus::NormalExit) { QString output = QString(process.readAllStandardOutput()); - - QVector> dataVector; QStringList list = output.split("\n"); foreach (QString value, list) { value = value.trimmed(); @@ -280,20 +290,34 @@ void InterpreterWidget::findPackages(const QString &cmd) || sublist.at(0).indexOf("----") > -1) { continue; } - dataVector.append(QPair(sublist.at(0).trimmed(), sublist.at(1).trimmed())); + info.packageList.append(QPair(sublist.at(0).trimmed(), sublist.at(1).trimmed())); } } - QMutexLocker lk(&d->mutex); - d->model->setCustomData(dataVector); } else { - qInfo() << "Error" << exitcode << exitStatus; + info.errMsg = process.readAllStandardError(); } + metaObject()->invokeMethod(this, + "applyQueryInfo", + Qt::QueuedConnection, + Q_ARG(QueryInfo, info)); }); process.start(cmd); process.waitForFinished(); } +void InterpreterWidget::applyQueryInfo(const QueryInfo &info) +{ + if (info.errMsg.isEmpty()) { + d->packagesWidget->setCurrentIndex(0); + QMutexLocker lk(&d->mutex); + d->model->setCustomData(info.packageList); + } else { + d->packagesWidget->setCurrentIndex(1); + d->msgLabel->setText(info.errMsg); + } +} + bool InterpreterWidget::getControlValue(QMap &map) { int index = d->interpreterComboBox->currentIndex(); diff --git a/src/plugins/python/python/option/interpreterwidget.h b/src/plugins/python/python/option/interpreterwidget.h index b866ebb6e..90a239c99 100644 --- a/src/plugins/python/python/option/interpreterwidget.h +++ b/src/plugins/python/python/option/interpreterwidget.h @@ -10,17 +10,23 @@ #include -enum ColumnID -{ +enum ColumnID { kPackage, kVersion, _KCount }; -struct InterpreterConfig{ +struct InterpreterConfig +{ ToolChainData::ToolChainParam version; }; +struct QueryInfo +{ + QVector> packageList; + QString errMsg; +}; + class InterpreterModelPrivate; class InterpreterModel : public QAbstractTableModel { @@ -34,7 +40,7 @@ class InterpreterModel : public QAbstractTableModel QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; - void setCustomData(QVector>& data); + void setCustomData(const QVector> &data); signals: @@ -54,10 +60,6 @@ class InterpreterWidget : public PageWidget void setUserConfig(const QMap &map) override; void getUserConfig(QMap &map) override; -signals: - -public slots: - private: void setupUi(); void updateUi(); @@ -69,9 +71,12 @@ public slots: void setControlValue(const QMap &map); void updatePackageData(); - void findPackages(const QString &cmd); + void queryPackages(const QString &cmd); + Q_INVOKABLE void applyQueryInfo(const QueryInfo &info); InterpreterWidgetPrivate *const d; }; -#endif // INTERPRETERWIDGET_H +Q_DECLARE_METATYPE(QueryInfo) + +#endif // INTERPRETERWIDGET_H