Newer
Older
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
/***************************************************************************
File : CustomActionDialog.cpp
Project : QtiPlot
--------------------------------------------------------------------
Copyright : (C) 2007 by Ion Vasilief
Email (use @ for *) : ion_vasilief*yahoo.fr
Description : Custom Action dialog
***************************************************************************/
/***************************************************************************
* *
* 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 2 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, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include "CustomActionDialog.h"
#include "ApplicationWindow.h"
#include <QPushButton>
#include <QRadioButton>
#include <QLabel>
#include <QGroupBox>
#include <QComboBox>
#include <QLineEdit>
#include <QAction>
#include <QApplication>
#include <QDir>
#include <QListWidget>
#include <QLayout>
#include <QFileDialog>
#include <QToolBar>
#include <QMenu>
#include <QImageReader>
#include <QShortcut>
#include <QMessageBox>
CustomActionDialog::CustomActionDialog(QWidget* parent, Qt::WFlags fl)
: QDialog(parent, fl)
{
setWindowTitle(tr("MantidPlot") + " - " + tr("Add Custom Action"));
itemsList = new QListWidget();
itemsList->setSelectionMode(QAbstractItemView::SingleSelection);
itemsList->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
itemsList->setSpacing(2);
QGroupBox *gb1 = new QGroupBox();
gb1->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));
QGridLayout *gl1 = new QGridLayout(gb1);
gl1->addWidget(new QLabel(tr("Folder")), 0, 0);
folderBox = new QLineEdit();
gl1->addWidget(folderBox, 0, 1);
folderBtn = new QPushButton(tr("Choose &Folder"));
gl1->addWidget(folderBtn, 0, 2);
gl1->addWidget(new QLabel(tr("Script File")), 1, 0);
fileBox = new QLineEdit();
gl1->addWidget(fileBox, 1, 1);
fileBtn = new QPushButton(tr("Choose &Script"));
gl1->addWidget(fileBtn, 1, 2);
gl1->addWidget(new QLabel(tr("Icon")), 2, 0);
iconBox = new QLineEdit();
gl1->addWidget(iconBox, 2, 1);
iconBtn = new QPushButton(tr("Choose &Icon"));
gl1->addWidget(iconBtn, 2, 2);
gl1->addWidget(new QLabel(tr("Text")), 3, 0);
textBox = new QLineEdit();
gl1->addWidget(textBox, 3, 1);
gl1->addWidget(new QLabel(tr("Tool Tip Text")), 4, 0);
toolTipBox = new QLineEdit();
gl1->addWidget(toolTipBox, 4, 1);
gl1->addWidget(new QLabel(tr("Shortcut")), 5, 0);
shortcutBox = new QLineEdit();
gl1->addWidget(shortcutBox, 5, 1);
menuBtn = new QRadioButton(tr("&Menu"));
gl1->addWidget(menuBtn, 6, 0);
menuBox = new QComboBox();
gl1->addWidget(menuBox, 6, 1);
toolBarBtn = new QRadioButton(tr("&Tool Bar"));
toolBarBtn->setChecked(true);
gl1->addWidget(toolBarBtn, 7, 0);
toolBarBox = new QComboBox();
gl1->addWidget(toolBarBox, 7, 1);
gl1->setRowStretch(8, 1);
gl1->setColumnStretch(1, 10);
QHBoxLayout * bottomButtons = new QHBoxLayout();
bottomButtons->addStretch();
buttonSave = new QPushButton(tr("&Save"));
buttonSave->setAutoDefault( true );
bottomButtons->addWidget(buttonSave);
buttonAdd = new QPushButton(tr("&Add"));
buttonAdd->setAutoDefault( true );
bottomButtons->addWidget(buttonAdd);
buttonRemove = new QPushButton(tr("&Remove"));
buttonRemove->setAutoDefault(true);
bottomButtons->addWidget(buttonRemove);
buttonCancel = new QPushButton(tr("&Close"));
buttonCancel->setAutoDefault( true );
bottomButtons->addWidget( buttonCancel );
QHBoxLayout *vl = new QHBoxLayout();
vl->addWidget(itemsList);
vl->addWidget(gb1);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(vl);
mainLayout->addLayout(bottomButtons);
init();
QShortcut *accelRemove = new QShortcut(QKeySequence(Qt::Key_Delete), this);
connect(accelRemove, SIGNAL(activated()), this, SLOT(removeAction()));
connect(buttonSave, SIGNAL(clicked()), this, SLOT(saveCurrentAction()));
connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addAction()));
connect(buttonRemove, SIGNAL(clicked()), this, SLOT(removeAction()));
connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(iconBtn, SIGNAL(clicked()), this, SLOT(chooseIcon()));
connect(fileBtn, SIGNAL(clicked()), this, SLOT(chooseFile()));
connect(folderBtn, SIGNAL(clicked()), this, SLOT(chooseFolder()));
connect(itemsList, SIGNAL(currentRowChanged(int)), this, SLOT(setCurrentAction(int)));
}
void CustomActionDialog::init()
{
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
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
folderBox->setText(app->customActionsDirPath);
d_menus = app->customizableMenusList();
d_app_toolbars = app->toolBarsList();
QList<QMenu *> d_app_menus = app->menusList();
QStringList toolBars, menus;
foreach (QMenu *m, d_menus){
if (!m->title().isEmpty()){
menus << m->title().remove("&");
}
}
menus.sort();
menuBox->addItems(menus);
//Build the list of shortcut key sequences and keep it to memory to improve speed!
foreach (QMenu *m, d_app_menus){
QList<QAction *> actionsList = m->actions();
foreach (QAction *a, actionsList){
QString shortcut = a->shortcut().toString();
if (!shortcut.isEmpty() && !d_app_shortcut_keys.contains(shortcut))
d_app_shortcut_keys << shortcut;
}
}
foreach (QToolBar *t, d_app_toolbars){
toolBars << t->windowTitle();
QList<QAction *> actionsList = t->actions();
foreach (QAction *a, actionsList){
QString shortcut = a->shortcut().toString();
if (!shortcut.isEmpty() && !d_app_shortcut_keys.contains(shortcut))
d_app_shortcut_keys << shortcut;
}
}
toolBars.sort();
toolBarBox->addItems(toolBars);
updateDisplayList();
}
void CustomActionDialog::updateDisplayList()
{
itemsList->clear();
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
if (!app) {
throw std::logic_error("Parent of CustomActionDialog is not ApplicationWindow as expected.");
}
QList<QAction *> actionsList = app->customActionsList();
foreach(QAction *action, actionsList){//add existing actions to the list widget
QString text = action->text();
QString shortcut = action->shortcut().toString();
if (!shortcut.isEmpty())
text += " (" + shortcut + ")";
QListWidgetItem *it = new QListWidgetItem(text, itemsList);
if (!action->icon().isNull())
it->setIcon(action->icon());
itemsList->addItem(it);
}
itemsList->setCurrentRow(0);
setCurrentAction(0);
}
QAction* CustomActionDialog::addAction()
{
QAction *action = NULL;
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parentWidget());
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
if (!app)
return action;
if (validUserInput()){
action = new QAction(app);
customizeAction(action);
if (toolBarBtn->isChecked()){
foreach (QToolBar *t, d_app_toolbars){
if (t->windowTitle() == toolBarBox->currentText()){
app->addCustomAction(action, t->objectName());
break;
}
}
} else {
foreach (QMenu *m, d_menus){
if (m->title().remove("&") == menuBox->currentText()){
action->setStatusTip(m->objectName());
app->addCustomAction(action, m->objectName());
break;
}
}
}
QString text = action->text();
QString shortcut = action->shortcut().toString();
if (!shortcut.isEmpty())
text += " (" + shortcut + ")";
QListWidgetItem *it = new QListWidgetItem(text, itemsList);
if (!action->icon().isNull())
it->setIcon(action->icon());
itemsList->addItem(it);
itemsList->setCurrentItem(it);
saveAction(action);
}
return action;
}
bool CustomActionDialog::validUserInput()
{
QString folder = folderBox->text();
while (folder.isEmpty() || !QFileInfo(folder).exists() || !QFileInfo(folder).isReadable()){
chooseFolder();
folder = folderBox->text();
}
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
if (!app) {
throw std::logic_error("Parent of CustomActionDialog is not ApplicationWindow as expected.");
}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
QList<QAction *>actions = app->customActionsList();
if (textBox->text().isEmpty()){
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("Please provide a description for your custom action!"));
textBox->setFocus();
return false;
} else if (textBox->text().contains(".")){
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("Dot characters are not allowed in the description text!"));
textBox->setFocus();
textBox->setText(textBox->text().remove(".").simplified());
return false;
}
QString text = textBox->text().remove(".").simplified();
foreach(QAction *action, actions){
if(action->text() == text){
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("You have already defined an action having description: %1 <br>Please provide a different description text!").arg(textBox->text()));
textBox->setFocus();
return false;
}
}
QString file = fileBox->text();
QFileInfo fi(file);
if (file.isEmpty() || !fi.exists()){
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("The file you have specified doesn't exist, please choose a valid script file!"));
fileBox->setFocus();
return false;
}
QString iconPath = iconBox->text();
QFileInfo iconInfo(iconPath);
if (!iconPath.isEmpty() && (!iconInfo.exists() || !iconInfo.isFile() || !iconInfo.isReadable())){
iconPath = QString();
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("The image file you have specified doesn't exist or can't be read, please choose another file!"));
iconBox->setFocus();
return false;
}
QStringList shortcuts = d_app_shortcut_keys;
foreach (QAction *a, actions){
QString shortcut = a->shortcut().toString();
if (!shortcut.isEmpty() && !shortcuts.contains(shortcut))
shortcuts << shortcut;
}
shortcuts.sort();
QString s;
int i = 0, n = shortcuts.count();
while(i < n-5){
s += shortcuts[i] + "\t" + shortcuts[i+1] + "\t" + shortcuts[i+2];
s += "\t" + shortcuts[i+3] + "\t" + shortcuts[i+4] + "\n";
i += 5;
}
if (shortcuts.contains(shortcutBox->text().remove(QRegExp("\\s")))){
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("Error"),
tr("Please provide a different key sequence! The following shortcut key sequences are already assigned:") +
"\n\n" + s);
shortcutBox->setFocus();
return false;
}
return true;
}
void CustomActionDialog::customizeAction(QAction *action)
{
if (!action)
return;
action->setText(textBox->text().remove(".").simplified());
action->setData(QFileInfo(fileBox->text()).absoluteFilePath());
QIcon icon = QIcon();
QString iconPath = iconBox->text();
QFileInfo iconInfo(iconPath);
if (!iconPath.isEmpty() && iconInfo.exists()){
icon = QIcon(iconPath);
action->setIcon(icon);
action->setIconText(iconInfo.absoluteFilePath());
}
if (!toolTipBox->text().isEmpty())
action->setToolTip(toolTipBox->text().simplified());
if (!shortcutBox->text().isEmpty())
action->setShortcut(shortcutBox->text().remove(QRegExp("\\s")));
}
void CustomActionDialog::removeAction()
{
QString s = tr("Are you sure you want to remove this action?");
if (QMessageBox::Yes != QMessageBox::question(this, tr("MantidPlot") + " - " + tr("Remove Action"), s, QMessageBox::Yes, QMessageBox::Cancel))
return;
int row = itemsList->currentRow();
QAction *action = actionAt(row);
if (!action)
return;
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
if (!app) {
throw std::logic_error("Parent of CustomActionDialog is not ApplicationWindow as expected.");
}
QFile f(app->customActionsDirPath + "/" + action->text() + ".qca");
f.remove();
app->removeCustomAction(action);
itemsList->takeItem(row);
if (itemsList->count())
setCurrentAction(0);
}
void CustomActionDialog::saveCurrentAction()
{
int row = itemsList->currentRow();
QAction *action = actionAt(row);
if (!action)
return;
QList<QWidget *> list = action->associatedWidgets();
QWidget *w = list[0];
QString parentName = w->objectName();
if ((toolBarBtn->isChecked() && w->objectName() != toolBarBox->currentText()) ||
(menuBtn->isChecked() && w->objectName() != menuBox->currentText())){
//relocate action: create a new one and delete the old
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
if (!app) {
throw std::logic_error("Parent of CustomActionDialog is not ApplicationWindow as expected.");
}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
QAction *newAction = new QAction(app);
customizeAction(newAction);
if (toolBarBtn->isChecked()){
foreach (QToolBar *t, d_app_toolbars){
if (t->windowTitle() == toolBarBox->currentText()){
app->addCustomAction(newAction, t->objectName(), row);
break;
}
}
} else {
foreach (QMenu *m, d_menus){
if (m->title().remove("&") == menuBox->currentText()){
action->setStatusTip(m->objectName());
app->addCustomAction(newAction, m->objectName(), row);
break;
}
}
}
saveAction(newAction);
delete action;
} else {
customizeAction(action);
saveAction(action);
}
}
void CustomActionDialog::saveAction(QAction *action)
{
if (!action)
return;
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
if (!app) {
throw std::logic_error("Parent of CustomActionDialog is not ApplicationWindow as expected.");
}
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
QString fileName = app->customActionsDirPath + "/" + action->text() + ".qca";
QFile f(fileName);
if (!f.open( QIODevice::WriteOnly)){
QApplication::restoreOverrideCursor();
QMessageBox::critical(app, tr("MantidPlot") + " - " + tr("File Save Error"),
tr("Could not write to file: <br><h4> %1 </h4><p>Please verify that you have the right to write to this location!").arg(fileName));
return;
}
QTextStream out( &f );
out.setCodec("UTF-8");
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<< "<!DOCTYPE action>\n"
<< "<action version=\"1.0\">\n";
out << "<text>" + action->text() + "</text>\n";
out << "<file>" + action->data().toString() + "</file>\n";
out << "<icon>" + action->iconText() + "</icon>\n";
out << "<tooltip>" + action->toolTip() + "</tooltip>\n";
out << "<shortcut>" + action->shortcut().toString() + "</shortcut>\n";
QList<QWidget *> list = action->associatedWidgets();
QWidget *w = list[0];
out << "<location>" + w->objectName() + "</location>\n";
out << "</action>\n";
}
void CustomActionDialog::chooseIcon()
{
QList<QByteArray> list = QImageReader::supportedImageFormats();
QString filter = tr("images") + " (", aux1, aux2;
for (int i=0; i<(int)list.count(); i++){
aux1 = " *." + list[i] + " ";
aux2 += " *." + list[i] + ";;";
filter += aux1;
}
filter+=");;" + aux2;
QString fn = QFileDialog::getOpenFileName(this, tr("MantidPlot - Load icon from file"), iconBox->text(), filter);
if (!fn.isEmpty())
iconBox->setText(fn);
}
void CustomActionDialog::chooseFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Choose script file"), fileBox->text());
if (!fileName.isEmpty())
fileBox->setText(fileName);
}
void CustomActionDialog::chooseFolder()
{
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parentWidget());
QString dir = QFileDialog::getExistingDirectory(this, tr("Choose the custom actions folder"), app->customActionsDirPath);
if (!dir.isEmpty() && QFileInfo(dir).isReadable()){
QList<QAction *> actionsList = app->customActionsList();
foreach (QAction *a, actionsList)
app->removeCustomAction(a);
app->customActionsDirPath = dir;
app->loadCustomActions();
updateDisplayList();
folderBox->setText(dir);
}
}
QAction * CustomActionDialog::actionAt(int row)
{
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent());
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
QList<QAction *>actions = app->customActionsList();
if (actions.isEmpty() || row < 0 || row >= actions.count())
return 0;
return actions.at(row);
}
void CustomActionDialog::setCurrentAction(int row)
{
QAction *action = actionAt(row);
if (!action)
return;
fileBox->setText(action->data().toString());
textBox->setText(action->text());
iconBox->setText(action->iconText());
toolTipBox->setText(action->toolTip());
shortcutBox->setText(action->shortcut().toString());
QList<QWidget *> list = action->associatedWidgets();
QWidget *w = NULL;
if (!list.isEmpty())
w = list[0];
if (!w)
return;
if (w->isA("QToolBar")){
int index = toolBarBox->findText((dynamic_cast<QToolBar*>(w))->windowTitle());
if (index >= 0){
toolBarBox->setCurrentIndex(index);
toolBarBtn->setChecked(true);
}
} else {
int index = menuBox->findText((dynamic_cast<QMenu*>(w))->title().remove("&"));
if (index >= 0){
menuBox->setCurrentIndex(index);
menuBtn->setChecked(true);
}
}
}
/*****************************************************************************
*
* Class CustomActionHandler
*
*****************************************************************************/
CustomActionHandler::CustomActionHandler(QAction *action)
: metFitTag(true),filePath(QString()),d_widget_name(QString()),d_action(action)
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
{
}
bool CustomActionHandler::startElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName,
const QXmlAttributes &attributes)
{
if (!metFitTag && qName != "action") {
errorStr = QObject::tr("The file is not a MantidPlot custom action file.");
return false;
}
if (qName == "action") {
QString version = attributes.value("version");
if (!version.isEmpty() && version != "1.0") {
errorStr = QObject::tr("The file is not an MantidPlot custom action version 1.0 file.");
return false;
}
metFitTag = true;
}
currentText.clear();
return true;
}
bool CustomActionHandler::endElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName)
{
if (qName == "text")
d_action->setText(currentText);
else if (qName == "file")
filePath = currentText;
else if (qName == "icon" && !currentText.isEmpty()){
d_action->setIcon(QIcon(currentText));
d_action->setIconText(currentText);
} else if (qName == "tooltip")
d_action->setToolTip(currentText);
else if (qName == "shortcut")
d_action->setShortcut(currentText);
else if (qName == "location"){
d_widget_name = currentText;
//use status tip to store the name of the destination menu (ugly hack!)
d_action->setStatusTip(currentText);
} else if (qName == "action")
d_action->setData(filePath);
return true;