1 | /* |
2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | #include "toolsgeneralwidget.h" |
18 | #include "ui_toolsgeneralwidget.h" |
19 | #include "../mainwindow.h" |
20 | #include "../settings.h" |
21 | #include "../iconsmanager.h" |
22 | |
23 | #include <QFileDialog> |
24 | #include <QMessageBox> |
25 | |
26 | ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group, QWidget *parent) : |
27 | SettingsWidget(name,group,parent), |
28 | ui(new Ui::ToolsGeneralWidget) |
29 | { |
30 | ui->setupUi(this); |
31 | ui->cbMacros->setModel(&mMacroInfoModel); |
32 | ui->lstTools->setModel(&mToolsModel); |
33 | mEditType = EditType::None; |
34 | finishEditing(false); |
35 | connect(ui->lstTools->selectionModel(), &QItemSelectionModel::currentRowChanged, |
36 | this,&ToolsGeneralWidget::onToolsCurrentChanged); |
37 | connect(ui->txtProgram,&QLineEdit::textChanged, |
38 | this, &ToolsGeneralWidget::updateDemo); |
39 | connect(ui->txtParameters,&QLineEdit::textChanged, |
40 | this, &ToolsGeneralWidget::updateDemo); |
41 | } |
42 | |
43 | ToolsGeneralWidget::~ToolsGeneralWidget() |
44 | { |
45 | delete ui; |
46 | } |
47 | |
48 | void ToolsGeneralWidget::onToolsCurrentChanged() |
49 | { |
50 | if (mEditType != EditType::None) { |
51 | finishEditing(true); |
52 | } |
53 | QModelIndex index = ui->lstTools->currentIndex(); |
54 | if (!index.isValid()) |
55 | return; |
56 | PToolItem item = mToolsModel.getTool(index.row()); |
57 | if (item) { |
58 | mEditType = EditType::Edit; |
59 | ui->txtDirectory->setText(item->workingDirectory); |
60 | ui->txtParameters->setText(item->parameters); |
61 | ui->txtProgram->setText(item->program); |
62 | ui->txtTitle->setText(item->title); |
63 | ui->chkPauseConsole->setChecked(item->pauseAfterExit); |
64 | ui->panelEdit->setVisible(true); |
65 | } |
66 | } |
67 | |
68 | void ToolsGeneralWidget::finishEditing(bool askSave) |
69 | { |
70 | if (mEditType == EditType::None) { |
71 | ui->panelEdit->setVisible(false); |
72 | return; |
73 | } |
74 | if (askSave && QMessageBox::question(this, |
75 | tr("Save Changes?" ), |
76 | tr("Do you want to save changes to the current tool?" ), |
77 | QMessageBox::Yes | QMessageBox::No, |
78 | QMessageBox::Yes) != QMessageBox::Yes) { |
79 | ui->panelEdit->setVisible(false); |
80 | return; |
81 | } |
82 | ui->panelEdit->setVisible(false); |
83 | if (mEditType == EditType::Add) { |
84 | mEditType = EditType::None; |
85 | PToolItem item = std::make_shared<ToolItem>(); |
86 | item->title = ui->txtTitle->text(); |
87 | item->program = ui->txtProgram->text(); |
88 | item->workingDirectory = ui->txtDirectory->text(); |
89 | item->parameters = ui->txtParameters->text(); |
90 | item->pauseAfterExit = ui->chkPauseConsole->isChecked(); |
91 | mToolsModel.addTool(item); |
92 | } else { |
93 | mEditType = EditType::None; |
94 | QModelIndex index = ui->lstTools->currentIndex(); |
95 | if (!index.isValid()) |
96 | return; |
97 | PToolItem item = mToolsModel.getTool(index.row()); |
98 | item->workingDirectory = ui->txtDirectory->text(); |
99 | item->parameters = ui->txtParameters->text(); |
100 | item->program = ui->txtProgram->text(); |
101 | item->title = ui->txtTitle->text(); |
102 | item->pauseAfterExit = ui->chkPauseConsole->isChecked(); |
103 | } |
104 | } |
105 | |
106 | void ToolsGeneralWidget::prepareEdit() |
107 | { |
108 | ui->txtDirectory->setText("" ); |
109 | ui->txtParameters->setText("" ); |
110 | ui->txtProgram->setText("" ); |
111 | ui->txtTitle->setText("" ); |
112 | ui->chkPauseConsole->setChecked(false); |
113 | ui->panelEdit->setVisible(true); |
114 | } |
115 | |
116 | void ToolsGeneralWidget::updateDemo() |
117 | { |
118 | ui->txtDemo->setText( |
119 | parseMacros(ui->txtProgram->text())+ " " + |
120 | parseMacros(ui->txtParameters->text())); |
121 | } |
122 | |
123 | ToolsModel::ToolsModel(QObject *parent):QAbstractListModel(parent) |
124 | { |
125 | |
126 | } |
127 | |
128 | const QList<PToolItem> &ToolsModel::tools() const |
129 | { |
130 | return mTools; |
131 | } |
132 | |
133 | void ToolsModel::setTools(const QList<PToolItem> &newTools) |
134 | { |
135 | beginResetModel(); |
136 | mTools = newTools; |
137 | endResetModel(); |
138 | } |
139 | |
140 | void ToolsModel::addTool(PToolItem item) |
141 | { |
142 | beginInsertRows(QModelIndex(),mTools.count(),mTools.count()); |
143 | mTools.append(item); |
144 | endInsertRows(); |
145 | } |
146 | |
147 | PToolItem ToolsModel::getTool(int index) |
148 | { |
149 | return mTools[index]; |
150 | } |
151 | |
152 | void ToolsModel::removeTool(int index) |
153 | { |
154 | mTools.removeAt(index); |
155 | } |
156 | |
157 | int ToolsModel::rowCount(const QModelIndex &) const |
158 | { |
159 | return mTools.count(); |
160 | } |
161 | |
162 | QVariant ToolsModel::data(const QModelIndex &index, int role) const |
163 | { |
164 | if (!index.isValid()) |
165 | return QVariant(); |
166 | if (role==Qt::DisplayRole) { |
167 | PToolItem item = mTools[index.row()]; |
168 | return item->title; |
169 | } |
170 | return QVariant(); |
171 | } |
172 | |
173 | void ToolsGeneralWidget::on_btnAdd_clicked() |
174 | { |
175 | ui->lstTools->setCurrentIndex(QModelIndex()); |
176 | prepareEdit(); |
177 | mEditType = EditType::Add; |
178 | } |
179 | |
180 | |
181 | void ToolsGeneralWidget::on_btnEditOk_clicked() |
182 | { |
183 | finishEditing(false); |
184 | } |
185 | |
186 | |
187 | void ToolsGeneralWidget::on_btnEditCancel_clicked() |
188 | { |
189 | mEditType = EditType::None; |
190 | ui->panelEdit->setVisible(false); |
191 | } |
192 | |
193 | void ToolsGeneralWidget::doLoad() |
194 | { |
195 | mToolsModel.setTools(pMainWindow->toolsManager()->tools()); |
196 | } |
197 | |
198 | void ToolsGeneralWidget::doSave() |
199 | { |
200 | finishEditing(true); |
201 | pMainWindow->toolsManager()->setTools(mToolsModel.tools()); |
202 | pMainWindow->toolsManager()->save(); |
203 | pMainWindow->updateTools(); |
204 | } |
205 | |
206 | void ToolsGeneralWidget::updateIcons(const QSize &) |
207 | { |
208 | pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD); |
209 | pIconsManager->setIcon(ui->btnRemove,IconsManager::ACTION_MISC_REMOVE); |
210 | pIconsManager->setIcon(ui->btnBrowseProgram,IconsManager::ACTION_FILE_OPEN_FOLDER); |
211 | pIconsManager->setIcon(ui->btnBrowseWorkingDirectory,IconsManager::ACTION_FILE_OPEN_FOLDER); |
212 | } |
213 | |
214 | |
215 | void ToolsGeneralWidget::on_btnRemove_clicked() |
216 | { |
217 | mEditType = EditType::None; |
218 | finishEditing(false); |
219 | QModelIndex index = ui->lstTools->currentIndex(); |
220 | if (index.isValid()) { |
221 | mToolsModel.removeTool(index.row()); |
222 | } |
223 | } |
224 | |
225 | |
226 | void ToolsGeneralWidget::on_btnInsertMacro_clicked() |
227 | { |
228 | ui->txtParameters->setText( |
229 | ui->txtParameters->text() + |
230 | ui->cbMacros->currentData(Qt::UserRole).toString()); |
231 | } |
232 | |
233 | void ToolsGeneralWidget::on_btnBrowseWorkingDirectory_clicked() |
234 | { |
235 | QString folder = QFileDialog::getExistingDirectory(this,tr("Choose Folder" )); |
236 | if (!folder.isEmpty()) { |
237 | ui->txtDirectory->setText(folder); |
238 | } |
239 | } |
240 | |
241 | |
242 | void ToolsGeneralWidget::on_btnBrowseProgram_clicked() |
243 | { |
244 | QString fileName = QFileDialog::getOpenFileName( |
245 | this, |
246 | tr("Select program" ), |
247 | pSettings->dirs().appDir(), |
248 | tr("Executable files (*.exe)" )); |
249 | if (!fileName.isEmpty() ) { |
250 | ui->txtProgram->setText(fileName); |
251 | } |
252 | } |
253 | |
254 | |