From cbcd958ee388cb0064afe6b3f6ede31ac591bb85 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Tue, 2 Apr 2024 16:55:51 +0800 Subject: [PATCH] refactor: Custom tool settings page --- RedPandaIDE/mainwindow.cpp | 10 ++ RedPandaIDE/settingsdialog/settingswidget.cpp | 7 +- RedPandaIDE/settingsdialog/settingswidget.h | 4 + .../settingsdialog/toolsgeneralwidget.cpp | 130 ++++++++++++++---- .../settingsdialog/toolsgeneralwidget.h | 19 ++- .../settingsdialog/toolsgeneralwidget.ui | 22 +++ RedPandaIDE/widgets/ojproblemsetmodel.h | 8 +- 7 files changed, 162 insertions(+), 38 deletions(-) diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index b601df08..8a067217 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -3479,7 +3479,17 @@ void MainWindow::loadLastOpens() void MainWindow::updateTools() { + QList oldToolActions; + //save old custom tools actions. + foreach(QAction* action, ui->menuTools->actions()) { + if (action->objectName().startsWith("tool-")) + oldToolActions.append(action); + } ui->menuTools->clear(); + //delete old custom tools actions; + for(int i=0;imenuTools->addAction(ui->actionOptions); if (!mToolsManager->tools().isEmpty()) { ui->menuTools->addSeparator(); diff --git a/RedPandaIDE/settingsdialog/settingswidget.cpp b/RedPandaIDE/settingsdialog/settingswidget.cpp index 1b6976ce..691903af 100644 --- a/RedPandaIDE/settingsdialog/settingswidget.cpp +++ b/RedPandaIDE/settingsdialog/settingswidget.cpp @@ -44,7 +44,7 @@ void SettingsWidget::init() connect(pIconsManager,&IconsManager::actionIconsUpdated, this, &SettingsWidget::onUpdateIcons); onUpdateIcons(); - load(); + //load(); connectInputs(); } @@ -201,6 +201,11 @@ void SettingsWidget::clearSettingsChanged() emit settingsChanged(false); } +void SettingsWidget::showEvent(QShowEvent *event) +{ + load(); +} + void SettingsWidget::onUpdateIcons() { updateIcons(pIconsManager->actionIconSize()); diff --git a/RedPandaIDE/settingsdialog/settingswidget.h b/RedPandaIDE/settingsdialog/settingswidget.h index 2f1f0366..185f09eb 100644 --- a/RedPandaIDE/settingsdialog/settingswidget.h +++ b/RedPandaIDE/settingsdialog/settingswidget.h @@ -56,6 +56,10 @@ private: bool mSettingsChanged; QString mGroup; QString mName; + + // QWidget interface +protected: + void showEvent(QShowEvent *event) override; }; #endif // SETTINGSWIDGET_H diff --git a/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp b/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp index 61ebee51..ebf475ce 100644 --- a/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp +++ b/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp @@ -26,6 +26,7 @@ #include #include +#include #include ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group, QWidget *parent) : @@ -53,8 +54,8 @@ ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group delete m; mCurrentEditingRow = -1; showEditPanel(false); - connect(ui->lstTools->selectionModel(), &QItemSelectionModel::currentRowChanged, - this,&ToolsGeneralWidget::onToolsCurrentChanged); + connect(ui->lstTools, &QAbstractItemView::doubleClicked, + this, &ToolsGeneralWidget::editTool); connect(ui->txtProgram,&QLineEdit::textChanged, this, &ToolsGeneralWidget::updateDemo); connect(ui->txtParameters,&QLineEdit::textChanged, @@ -79,29 +80,30 @@ ToolsGeneralWidget::~ToolsGeneralWidget() delete ui; } -void ToolsGeneralWidget::onToolsCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous) +void ToolsGeneralWidget::editTool(const QModelIndex &index) { - finishEditing(true,previous); - QModelIndex index = current; + if (mCurrentEditingRow>=-1) + return; if (!index.isValid()) return; - PToolItem item = mToolsModel.getTool(index.row()); - if (item) { - prepareEdit(item); - } + prepareEdit(index.row()); } -void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemIndex) +void ToolsGeneralWidget::finishEditing(bool askSave) { auto action = finally([this]{ - mEdited = false; - mCurrentEditingRow = -1; - showEditPanel(false); + cleanEditor(); }); if (mCurrentEditingRow == -1) return; if (!mEdited) return; + if (ui->txtTitle->text().isEmpty()) { + QMessageBox::critical(this, + tr("Error"), + tr("Title shouldn't be empty!")); + return; + } if (askSave && QMessageBox::question(this, tr("Save Changes?"), tr("Do you want to save changes to \"%1\"?").arg(ui->txtTitle->text()), @@ -109,12 +111,6 @@ void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemInde QMessageBox::Yes) != QMessageBox::Yes) { return; } - if (ui->txtTitle->text().isEmpty()) { - QMessageBox::critical(this, - tr("Error"), - tr("Title shouldn't be empty!")); - return; - } PToolItem item = mToolsModel.getTool(mCurrentEditingRow); item->workingDirectory = ui->txtDirectory->text(); item->parameters = ui->txtParameters->text(); @@ -122,11 +118,20 @@ void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemInde item->title = ui->txtTitle->text(); item->inputOrigin = static_cast(ui->cbInput->currentIndex()); item->outputTarget = static_cast(ui->cbOutput->currentIndex()); + mToolsModel.updateTool(mCurrentEditingRow, item); } -void ToolsGeneralWidget::prepareEdit(const PToolItem& item) +void ToolsGeneralWidget::cleanEditor() { - mEditType = EditType::Edit; + mEdited = false; + mCurrentEditingRow = -1; + showEditPanel(false); +} + +void ToolsGeneralWidget::prepareEdit(int row) +{ + mCurrentEditingRow = row; + PToolItem item = mToolsModel.getTool(row); ui->txtDirectory->setText(item->workingDirectory); ui->txtParameters->setText(item->parameters); ui->txtProgram->setText(item->program); @@ -142,6 +147,7 @@ void ToolsGeneralWidget::showEditPanel(bool isShow) { ui->panelEdit->setVisible(isShow); ui->panelEditButtons->setVisible(!isShow); + ui->lstTools->setEnabled(!isShow); } void ToolsGeneralWidget::onEdited() @@ -217,6 +223,65 @@ QVariant ToolsModel::data(const QModelIndex &index, int role) const return QVariant(); } +Qt::ItemFlags ToolsModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flags = Qt::NoItemFlags; + if (index.isValid()) { + flags = Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled; + } else if (index.row() == -1) { + // -1 means it's a drop target? + flags = Qt::ItemIsDropEnabled; + } + return flags ; +} + +Qt::DropActions ToolsModel::supportedDropActions() const +{ + return Qt::MoveAction; +} + +bool ToolsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) +{ + Q_UNUSED(column); + mMoveTargetRow=row; + if (mMoveTargetRow==-1) + mMoveTargetRow=mTools.length(); + return QAbstractListModel::dropMimeData(data,action,row,0,parent); +} + +bool ToolsModel::insertRows(int /* row */, int /*count*/, const QModelIndex &/*parent*/) +{ + return true; +} + +bool ToolsModel::removeRows(int row, int count, const QModelIndex &/*parent*/) +{ + int sourceRow = row; + int destinationChild = mMoveTargetRow; + mMoveTargetRow=-1; + if (sourceRow < 0 + || sourceRow + count - 1 >= mTools.count() + || destinationChild < 0 + || destinationChild > mTools.count() + || sourceRow == destinationChild + || count <= 0) { + return false; + } + if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild)) + return false; + + int fromRow = sourceRow; + if (destinationChild < sourceRow) + fromRow += count - 1; + else + destinationChild--; + while (count--) + mTools.move(fromRow, destinationChild); + endMoveRows(); + return true; +} + + void ToolsGeneralWidget::on_btnAdd_clicked() { ui->lstTools->setCurrentIndex(QModelIndex()); @@ -226,9 +291,10 @@ void ToolsGeneralWidget::on_btnAdd_clicked() item->inputOrigin = ToolItemInputOrigin::None; item->outputTarget = ToolItemOutputTarget::RedirectToToolsOutputPanel; mToolsModel.addTool(item); - QModelIndex index=mToolsModel.index(mToolsModel.tools().count()-1); + int row = mToolsModel.tools().count() - 1; + QModelIndex index=mToolsModel.index(row); ui->lstTools->setCurrentIndex(index); - prepareEdit(item); + prepareEdit(row); } @@ -240,8 +306,7 @@ void ToolsGeneralWidget::on_btnEditOk_clicked() void ToolsGeneralWidget::on_btnEditCancel_clicked() { - mEditType = EditType::None; - showEditPanel(false); + cleanEditor(); } void ToolsGeneralWidget::doLoad() @@ -260,6 +325,7 @@ void ToolsGeneralWidget::doSave() void ToolsGeneralWidget::updateIcons(const QSize &) { pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD); + pIconsManager->setIcon(ui->btnEdit, IconsManager::ACTION_MISC_RENAME); pIconsManager->setIcon(ui->btnRemove,IconsManager::ACTION_MISC_REMOVE); pIconsManager->setIcon(ui->btnBrowseProgram,IconsManager::ACTION_FILE_LOCATE); pIconsManager->setIcon(ui->btnBrowseWorkingDirectory,IconsManager::ACTION_FILE_OPEN_FOLDER); @@ -268,8 +334,7 @@ void ToolsGeneralWidget::updateIcons(const QSize &) void ToolsGeneralWidget::on_btnRemove_clicked() { - mEditType = EditType::None; - finishEditing(false); + cleanEditor(); QModelIndex index = ui->lstTools->currentIndex(); if (index.isValid()) { mToolsModel.removeTool(index.row()); @@ -301,8 +366,19 @@ void ToolsGeneralWidget::on_btnBrowseProgram_clicked() pSettings->dirs().appDir(), pSystemConsts->executableFileFilter()); if (!fileName.isEmpty() ) { + QString appPath = includeTrailingPathDelimiter(pSettings->dirs().appDir()); + if (fileName.startsWith(appPath)) + fileName = ""+fileName.mid(appPath.length()); ui->txtProgram->setText(fileName); } } + +void ToolsGeneralWidget::on_btnEdit_clicked() +{ + const QModelIndex& index = ui->lstTools->currentIndex(); + if (index.isValid()) + editTool(index); +} + diff --git a/RedPandaIDE/settingsdialog/toolsgeneralwidget.h b/RedPandaIDE/settingsdialog/toolsgeneralwidget.h index 57dc2c11..e87d94d4 100644 --- a/RedPandaIDE/settingsdialog/toolsgeneralwidget.h +++ b/RedPandaIDE/settingsdialog/toolsgeneralwidget.h @@ -40,11 +40,21 @@ public: private: QList mTools; + int mMoveTargetRow; // QAbstractItemModel interface public: int rowCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; + + // QAbstractItemModel interface +public: + Qt::ItemFlags flags(const QModelIndex &index) const override; + Qt::DropActions supportedDropActions() const override; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; + bool insertRows(int row, int count, const QModelIndex &parent) override; + bool removeRows(int row, int count, const QModelIndex &parent) override; + }; @@ -55,12 +65,13 @@ public: explicit ToolsGeneralWidget(const QString& name, const QString& group, QWidget *parent = nullptr); ~ToolsGeneralWidget(); private: - void finishEditing(bool askSave, const QModelIndex& itemIndex=QModelIndex()); - void prepareEdit(const PToolItem& PToolItem); + void finishEditing(bool askSave); + void cleanEditor(); + void prepareEdit(int row); void showEditPanel(bool isShow); private slots: void onEdited(); - void onToolsCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous); + void editTool(const QModelIndex &index); void updateDemo(); void on_btnAdd_clicked(); @@ -75,6 +86,8 @@ private slots: void on_btnBrowseWorkingDirectory_clicked(); void on_btnBrowseProgram_clicked(); + void on_btnEdit_clicked(); + private: Ui::ToolsGeneralWidget *ui; MacroInfoModel mMacroInfoModel; diff --git a/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui b/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui index 68cf2def..7b4a8c2c 100644 --- a/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui +++ b/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui @@ -36,6 +36,13 @@ + + + + Edit + + + @@ -94,6 +101,18 @@ + + QAbstractItemView::NoEditTriggers + + + true + + + QAbstractItemView::DragDrop + + + Qt::MoveAction + false @@ -340,6 +359,9 @@ txtDirectory btnBrowseWorkingDirectory txtParameters + txtDemo + cbInput + cbOutput btnInsertMacro cbMacros btnEditOk diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.h b/RedPandaIDE/widgets/ojproblemsetmodel.h index 3fee0379..d19ade68 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.h +++ b/RedPandaIDE/widgets/ojproblemsetmodel.h @@ -48,7 +48,6 @@ public: int rowCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; - Qt::ItemFlags flags(const QModelIndex &index) const override; // QAbstractItemModel interface public: @@ -57,14 +56,9 @@ public: // QAbstractItemModel interface public: + Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::DropActions supportedDropActions() const override; - - // QAbstractItemModel interface -public: bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; - - // QAbstractItemModel interface -public: bool insertRows(int row, int count, const QModelIndex &parent) override; bool removeRows(int row, int count, const QModelIndex &parent) override; };