This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Ui.cpp
296 lines (245 loc) · 9.38 KB
/
Ui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 athre0z
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Ui.hpp"
#include "Config.hpp"
#include "ImportExport.hpp"
#include <cassert>
#include <QMessageBox>
#include <QFile>
#include <QMenu>
#include <QFileDialog>
#include <QSettings>
// ============================================================================================== //
// [SubstitutionModel] //
// ============================================================================================== //
SubstitutionModel::SubstitutionModel(SubstitutionManager *data, QObject *parent)
: QAbstractItemModel(parent)
, m_substMgr(data)
{
}
int SubstitutionModel::rowCount(const QModelIndex &parent) const
{
return m_substMgr->rules().size();
}
int SubstitutionModel::columnCount(const QModelIndex &/*parent*/) const
{
return 2;
}
QModelIndex SubstitutionModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column, nullptr);
}
QVariant SubstitutionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
assert(static_cast<unsigned>(index.row()) >= m_substMgr->rules().size());
auto sbst = m_substMgr->rules().at(index.row());
return index.column() == 0 ? QString::fromStdString(sbst->regexpPattern)
: QString::fromStdString(sbst->replacement);
}
QVariant SubstitutionModel::headerData(int section,
Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
return QVariant();
switch (section)
{
case 0:
return "Search text";
case 1:
return "Replacement";
default:
return QVariant();
}
}
QModelIndex SubstitutionModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
Qt::ItemFlags SubstitutionModel::flags(const QModelIndex &index) const
{
return Qt::NoItemFlags;
}
const Substitution* SubstitutionModel::substitutionByIndex(const QModelIndex& index)
{
assert(m_substMgr);
assert(m_substMgr->rules().size() > static_cast<unsigned>(index.row()));
return m_substMgr->rules().at(index.row()).get();
}
void SubstitutionModel::update()
{
emit layoutAboutToBeChanged();
emit layoutChanged();
}
// ============================================================================================== //
// [SubstitutionEditor] //
// ============================================================================================== //
SubstitutionEditor::SubstitutionEditor(QWidget* parent)
: QDialog(parent)
{
m_widgets.setupUi(this);
connect(m_widgets.tvSubstitutions,
SIGNAL(customContextMenuRequested(const QPoint&)),
SLOT(displayContextMenu(const QPoint&)));
connect(m_widgets.btnAdd, SIGNAL(clicked(bool)), SLOT(addSubstitution(bool)));
connect(m_widgets.btnImport, SIGNAL(clicked(bool)), SLOT(importRules(bool)));
connect(m_widgets.btnExport, SIGNAL(clicked(bool)), SLOT(exportRules(bool)));
m_widgets.tvSubstitutions->setContextMenuPolicy(Qt::CustomContextMenu);
}
void SubstitutionEditor::setModel(SubstitutionModel* model)
{
assert(model);
m_widgets.tvSubstitutions->setModel(model);
}
SubstitutionModel* SubstitutionEditor::model()
{
return static_cast<SubstitutionModel*>(m_widgets.tvSubstitutions->model());
}
void SubstitutionEditor::addSubstitution(bool)
{
assert(model());
assert(model()->substitutionManager());
auto regexp = m_widgets.leSearchText->text().toStdString();
// Empty?
if (regexp.empty())
{
QMessageBox::warning(qApp->activeWindow(), PLUGIN_NAME, "Search text may not be empty!");
return;
}
// Unique?
const auto& substs = model()->substitutionManager()->rules();
auto it = std::find_if(substs.cbegin(), substs.cend(),
[®exp](const std::shared_ptr<Substitution>& cur) -> bool
{
assert(cur);
return cur->regexpPattern == regexp;
});
if (it != substs.cend())
{
QMessageBox::warning(qApp->activeWindow(), PLUGIN_NAME, "Search texts must be unique!");
return;
}
// Valid?
auto newSubst = std::make_shared<Substitution>();
try
{
newSubst->regexp = std::regex(regexp, std::regex_constants::optimize);
}
catch (const std::regex_error& e)
{
QMessageBox::warning(qApp->activeWindow(), PLUGIN_NAME,
QString("The given regexp does not seem to be valid:\n") + e.what());
return;
}
newSubst->replacement = m_widgets.leReplacement->text().toStdString();
newSubst->regexpPattern = regexp;
// Sane, add to list.
m_widgets.leSearchText->clear();
m_widgets.leReplacement->clear();
model()->substitutionManager()->addRule(std::move(newSubst));
model()->update();
}
void SubstitutionEditor::displayContextMenu(const QPoint& point)
{
QModelIndex index = m_widgets.tvSubstitutions->indexAt(point);
if (!index.isValid())
return;
assert(model());
m_contextMenuSelectedItem = model()->substitutionByIndex(index);
assert(m_contextMenuSelectedItem);
QMenu menu(this);
QAction *deleteAction = menu.addAction(QIcon(":/SubstitutionEditor/delete.png"), "&Delete");
connect(deleteAction, SIGNAL(triggered(bool)), SLOT(deleteSubstitution(bool)));
QAction *editAction = menu.addAction(QIcon(":/SubstitutionEditor/edit.png"), "&Edit");
connect(editAction, SIGNAL(triggered(bool)), SLOT(editSubstitution(bool)));
menu.exec(m_widgets.tvSubstitutions->viewport()->mapToGlobal(point));
}
void SubstitutionEditor::deleteSubstitution(bool)
{
if (QMessageBox::question(qApp->activeWindow(), PLUGIN_NAME,
"Are you sure your want to delete this entry?", QMessageBox::Yes | QMessageBox::No)
!= QMessageBox::Yes)
{
return;
}
assert(model());
assert(model()->substitutionManager());
assert(m_contextMenuSelectedItem);
model()->substitutionManager()->removeRule(m_contextMenuSelectedItem);
m_contextMenuSelectedItem = nullptr;
model()->update();
}
void SubstitutionEditor::importRules(bool)
{
auto fileName = QFileDialog::getOpenFileName(qApp->activeWindow(), "Import rules...",
QString(), "Rule file (*.ini)");
if (fileName.isEmpty())
return;
QSettings settings(fileName, QSettings::IniFormat);
SettingsImporterExporter importer(model()->substitutionManager(), &settings);
importer.importRules();
model()->update();
}
void SubstitutionEditor::exportRules(bool)
{
auto fileName = QFileDialog::getSaveFileName(qApp->activeWindow(), "Export rules...",
QString(), "Rule file (*.ini)");
if (fileName.isEmpty())
return;
QSettings settings(fileName, QSettings::IniFormat);
SettingsImporterExporter exporter(model()->substitutionManager(), &settings);
exporter.exportRules();
}
void SubstitutionEditor::editSubstitution(bool)
{
assert(model());
assert(model()->substitutionManager());
assert(m_contextMenuSelectedItem);
m_widgets.leSearchText->setText(QString::fromStdString(
m_contextMenuSelectedItem->regexpPattern));
m_widgets.leReplacement->setText(QString::fromStdString(
m_contextMenuSelectedItem->replacement));
model()->substitutionManager()->removeRule(m_contextMenuSelectedItem);
m_contextMenuSelectedItem = nullptr;
model()->update();
}
// ============================================================================================== //
// [AboutDialog] //
// ============================================================================================== //
AboutDialog::AboutDialog()
{
m_widgets.setupUi(this);
connect(m_widgets.lblUdis,
SIGNAL(linkActivated(const QString&)),
SLOT(displayUdisLicense(const QString&)));
}
void AboutDialog::displayUdisLicense(const QString& /*link*/)
{
QFile licenseFile(":/Misc/udis_license.txt");
licenseFile.open(QFile::ReadOnly);
QMessageBox::information(qApp->activeWindow(), "udis86 license",
QString::fromAscii(licenseFile.readAll()));
}
// ============================================================================================== //