2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-07 08:34:10 +08:00
|
|
|
#include "toolsgeneralwidget.h"
|
|
|
|
#include "ui_toolsgeneralwidget.h"
|
2021-10-08 00:06:41 +08:00
|
|
|
#include "../mainwindow.h"
|
|
|
|
#include "../settings.h"
|
2021-12-23 09:11:58 +08:00
|
|
|
#include "../iconsmanager.h"
|
2021-10-07 08:34:10 +08:00
|
|
|
|
2021-10-08 00:06:41 +08:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group, QWidget *parent) :
|
|
|
|
SettingsWidget(name,group,parent),
|
2021-10-07 08:34:10 +08:00
|
|
|
ui(new Ui::ToolsGeneralWidget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2021-10-08 00:06:41 +08:00
|
|
|
ui->cbMacros->setModel(&mMacroInfoModel);
|
2022-10-10 18:05:18 +08:00
|
|
|
QItemSelectionModel *m=ui->lstTools->selectionModel();
|
2021-10-08 00:06:41 +08:00
|
|
|
ui->lstTools->setModel(&mToolsModel);
|
2022-10-10 18:05:18 +08:00
|
|
|
delete m;
|
2021-10-08 00:06:41 +08:00
|
|
|
mEditType = EditType::None;
|
2023-01-24 09:17:27 +08:00
|
|
|
showEditPanel(false);
|
2021-10-08 00:06:41 +08:00
|
|
|
connect(ui->lstTools->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
|
|
|
this,&ToolsGeneralWidget::onToolsCurrentChanged);
|
2021-12-09 11:22:28 +08:00
|
|
|
connect(ui->txtProgram,&QLineEdit::textChanged,
|
2021-10-08 00:06:41 +08:00
|
|
|
this, &ToolsGeneralWidget::updateDemo);
|
|
|
|
connect(ui->txtParameters,&QLineEdit::textChanged,
|
|
|
|
this, &ToolsGeneralWidget::updateDemo);
|
2023-01-24 09:17:27 +08:00
|
|
|
|
|
|
|
connect(ui->txtTitle,&QLineEdit::textChanged,
|
|
|
|
this, &ToolsGeneralWidget::onEdited);
|
|
|
|
connect(ui->txtProgram,&QLineEdit::textChanged,
|
|
|
|
this, &ToolsGeneralWidget::onEdited);
|
|
|
|
connect(ui->txtParameters,&QLineEdit::textChanged,
|
|
|
|
this, &ToolsGeneralWidget::onEdited);
|
|
|
|
connect(ui->txtDirectory,&QLineEdit::textChanged,
|
|
|
|
this, &ToolsGeneralWidget::onEdited);
|
|
|
|
connect(ui->chkPauseConsole,&QCheckBox::stateChanged,
|
|
|
|
this, &ToolsGeneralWidget::onEdited);
|
2021-10-07 08:34:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolsGeneralWidget::~ToolsGeneralWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2021-10-08 00:06:41 +08:00
|
|
|
|
2023-01-24 09:17:27 +08:00
|
|
|
void ToolsGeneralWidget::onToolsCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
2021-10-08 00:06:41 +08:00
|
|
|
{
|
2023-01-24 09:17:27 +08:00
|
|
|
finishEditing(true,previous);
|
|
|
|
QModelIndex index = current;
|
2021-10-08 00:06:41 +08:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
PToolItem item = mToolsModel.getTool(index.row());
|
|
|
|
if (item) {
|
2023-01-24 09:17:27 +08:00
|
|
|
prepareEdit(item);
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:17:27 +08:00
|
|
|
void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemIndex)
|
2021-10-08 00:06:41 +08:00
|
|
|
{
|
2023-01-24 09:17:27 +08:00
|
|
|
auto action = finally([this]{
|
|
|
|
showEditPanel(false);
|
|
|
|
});
|
|
|
|
if (mEditType == EditType::None)
|
|
|
|
return;
|
|
|
|
if (!mEdited)
|
2021-10-08 00:06:41 +08:00
|
|
|
return;
|
|
|
|
if (askSave && QMessageBox::question(this,
|
|
|
|
tr("Save Changes?"),
|
2023-01-24 09:17:27 +08:00
|
|
|
tr("Do you want to save changes to \"%1\"?").arg(ui->txtTitle->text()),
|
2021-10-08 00:06:41 +08:00
|
|
|
QMessageBox::Yes | QMessageBox::No,
|
|
|
|
QMessageBox::Yes) != QMessageBox::Yes) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-24 09:17:27 +08:00
|
|
|
if (ui->txtTitle->text().isEmpty()) {
|
|
|
|
QMessageBox::critical(this,
|
|
|
|
tr("Error"),
|
|
|
|
tr("Title shouldn't be empty!"));
|
|
|
|
return;
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
2023-01-24 09:17:27 +08:00
|
|
|
mEditType = EditType::None;
|
|
|
|
QModelIndex index=itemIndex.isValid()?itemIndex:ui->lstTools->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PToolItem item = mToolsModel.getTool(index.row());
|
|
|
|
item->workingDirectory = ui->txtDirectory->text();
|
|
|
|
item->parameters = ui->txtParameters->text();
|
|
|
|
item->program = ui->txtProgram->text();
|
|
|
|
item->title = ui->txtTitle->text();
|
|
|
|
item->pauseAfterExit = ui->chkPauseConsole->isChecked();
|
|
|
|
mEdited=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::prepareEdit(const PToolItem& item)
|
|
|
|
{
|
|
|
|
mEditType = EditType::Edit;
|
|
|
|
ui->txtDirectory->setText(item->workingDirectory);
|
|
|
|
ui->txtParameters->setText(item->parameters);
|
|
|
|
ui->txtProgram->setText(item->program);
|
|
|
|
ui->txtTitle->setText(item->title);
|
|
|
|
ui->chkPauseConsole->setChecked(item->pauseAfterExit);
|
|
|
|
showEditPanel(true);
|
|
|
|
ui->txtTitle->setFocus();
|
|
|
|
mEdited = false;
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:17:27 +08:00
|
|
|
void ToolsGeneralWidget::showEditPanel(bool isShow)
|
2021-10-08 00:06:41 +08:00
|
|
|
{
|
2023-01-24 09:17:27 +08:00
|
|
|
ui->panelEdit->setVisible(isShow);
|
|
|
|
ui->panelEditButtons->setVisible(!isShow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::onEdited()
|
|
|
|
{
|
|
|
|
mEdited=true;
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::updateDemo()
|
|
|
|
{
|
|
|
|
ui->txtDemo->setText(
|
|
|
|
parseMacros(ui->txtProgram->text())+ " " +
|
|
|
|
parseMacros(ui->txtParameters->text()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolsModel::ToolsModel(QObject *parent):QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<PToolItem> &ToolsModel::tools() const
|
|
|
|
{
|
|
|
|
return mTools;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsModel::setTools(const QList<PToolItem> &newTools)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
mTools = newTools;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsModel::addTool(PToolItem item)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(),mTools.count(),mTools.count());
|
|
|
|
mTools.append(item);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
PToolItem ToolsModel::getTool(int index)
|
|
|
|
{
|
|
|
|
return mTools[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsModel::removeTool(int index)
|
|
|
|
{
|
|
|
|
mTools.removeAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ToolsModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return mTools.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ToolsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
if (role==Qt::DisplayRole) {
|
|
|
|
PToolItem item = mTools[index.row()];
|
|
|
|
return item->title;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnAdd_clicked()
|
|
|
|
{
|
|
|
|
ui->lstTools->setCurrentIndex(QModelIndex());
|
2023-01-24 09:17:27 +08:00
|
|
|
PToolItem item = std::make_shared<ToolItem>();
|
|
|
|
item->title = tr("untitled");
|
|
|
|
item->pauseAfterExit = false;
|
|
|
|
mToolsModel.addTool(item);
|
|
|
|
QModelIndex index=mToolsModel.index(mToolsModel.tools().count()-1);
|
|
|
|
ui->lstTools->setCurrentIndex(index);
|
|
|
|
prepareEdit(item);
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnEditOk_clicked()
|
|
|
|
{
|
|
|
|
finishEditing(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnEditCancel_clicked()
|
|
|
|
{
|
|
|
|
mEditType = EditType::None;
|
2023-01-24 09:17:27 +08:00
|
|
|
showEditPanel(false);
|
2021-10-08 00:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::doLoad()
|
|
|
|
{
|
|
|
|
mToolsModel.setTools(pMainWindow->toolsManager()->tools());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::doSave()
|
|
|
|
{
|
|
|
|
finishEditing(true);
|
|
|
|
pMainWindow->toolsManager()->setTools(mToolsModel.tools());
|
|
|
|
pMainWindow->toolsManager()->save();
|
|
|
|
pMainWindow->updateTools();
|
|
|
|
}
|
2021-12-23 09:11:58 +08:00
|
|
|
|
|
|
|
void ToolsGeneralWidget::updateIcons(const QSize &)
|
|
|
|
{
|
|
|
|
pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD);
|
|
|
|
pIconsManager->setIcon(ui->btnRemove,IconsManager::ACTION_MISC_REMOVE);
|
|
|
|
pIconsManager->setIcon(ui->btnBrowseProgram,IconsManager::ACTION_FILE_OPEN_FOLDER);
|
|
|
|
pIconsManager->setIcon(ui->btnBrowseWorkingDirectory,IconsManager::ACTION_FILE_OPEN_FOLDER);
|
|
|
|
}
|
2021-10-08 00:06:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnRemove_clicked()
|
|
|
|
{
|
|
|
|
mEditType = EditType::None;
|
|
|
|
finishEditing(false);
|
|
|
|
QModelIndex index = ui->lstTools->currentIndex();
|
|
|
|
if (index.isValid()) {
|
|
|
|
mToolsModel.removeTool(index.row());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnInsertMacro_clicked()
|
|
|
|
{
|
|
|
|
ui->txtParameters->setText(
|
|
|
|
ui->txtParameters->text() +
|
|
|
|
ui->cbMacros->currentData(Qt::UserRole).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnBrowseWorkingDirectory_clicked()
|
|
|
|
{
|
|
|
|
QString folder = QFileDialog::getExistingDirectory(this,tr("Choose Folder"));
|
|
|
|
if (!folder.isEmpty()) {
|
|
|
|
ui->txtDirectory->setText(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ToolsGeneralWidget::on_btnBrowseProgram_clicked()
|
|
|
|
{
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(
|
|
|
|
this,
|
|
|
|
tr("Select program"),
|
2021-12-26 15:08:54 +08:00
|
|
|
pSettings->dirs().appDir(),
|
2021-10-08 00:06:41 +08:00
|
|
|
tr("Executable files (*.exe)"));
|
|
|
|
if (!fileName.isEmpty() ) {
|
|
|
|
ui->txtProgram->setText(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:17:27 +08:00
|
|
|
|