refactor: Custom tool settings page

This commit is contained in:
Roy Qu 2024-04-02 16:55:51 +08:00
parent bca4be08b2
commit cbcd958ee3
7 changed files with 162 additions and 38 deletions

View File

@ -3479,7 +3479,17 @@ void MainWindow::loadLastOpens()
void MainWindow::updateTools()
{
QList<QAction*> 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;i<oldToolActions.length();i++) {
delete oldToolActions[i];
}
ui->menuTools->addAction(ui->actionOptions);
if (!mToolsManager->tools().isEmpty()) {
ui->menuTools->addSeparator();

View File

@ -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());

View File

@ -56,6 +56,10 @@ private:
bool mSettingsChanged;
QString mGroup;
QString mName;
// QWidget interface
protected:
void showEvent(QShowEvent *event) override;
};
#endif // SETTINGSWIDGET_H

View File

@ -26,6 +26,7 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QMimeData>
#include <QUuid>
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 &current, 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<ToolItemInputOrigin>(ui->cbInput->currentIndex());
item->outputTarget = static_cast<ToolItemOutputTarget>(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 = "<EXECPATH>"+fileName.mid(appPath.length());
ui->txtProgram->setText(fileName);
}
}
void ToolsGeneralWidget::on_btnEdit_clicked()
{
const QModelIndex& index = ui->lstTools->currentIndex();
if (index.isValid())
editTool(index);
}

View File

@ -40,11 +40,21 @@ public:
private:
QList<PToolItem> 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 &current, 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;

View File

@ -36,6 +36,13 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnEdit">
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnRemove">
<property name="text">
@ -94,6 +101,18 @@
</property>
<item>
<widget class="QListView" name="lstTools">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</property>
@ -340,6 +359,9 @@
<tabstop>txtDirectory</tabstop>
<tabstop>btnBrowseWorkingDirectory</tabstop>
<tabstop>txtParameters</tabstop>
<tabstop>txtDemo</tabstop>
<tabstop>cbInput</tabstop>
<tabstop>cbOutput</tabstop>
<tabstop>btnInsertMacro</tabstop>
<tabstop>cbMacros</tabstop>
<tabstop>btnEditOk</tabstop>

View File

@ -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;
};