1
/***************************************************************************
2
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
#include <QInputDialog>
33
#include "DlgToolbarsImp.h"
34
#include "DlgKeyboardImp.h"
35
#include "ui_DlgToolbars.h"
36
#include "Application.h"
37
#include "BitmapFactory.h"
40
#include "ToolBarManager.h"
41
#include "MainWindow.h"
42
#include "ToolBarManager.h"
45
#include "WorkbenchManager.h"
48
using namespace Gui::Dialog;
50
/* TRANSLATOR Gui::Dialog::DlgCustomToolbars */
53
* Constructs a DlgCustomToolbars which is a child of 'parent', with the
54
* name 'name' and widget flags set to 'f'
56
* The dialog will by default be modeless, unless you set 'modal' to
57
* true to construct a modal dialog.
59
DlgCustomToolbars::DlgCustomToolbars(DlgCustomToolbars::Type t, QWidget* parent)
60
: CustomizeActionPage(parent)
61
, ui(new Ui_DlgCustomToolbars)
67
ui->moveActionRightButton->setIcon(BitmapFactory().iconFromTheme("button_right"));
68
ui->moveActionLeftButton->setIcon(BitmapFactory().iconFromTheme("button_left"));
69
ui->moveActionDownButton->setIcon(BitmapFactory().iconFromTheme("button_down"));
70
ui->moveActionUpButton->setIcon(BitmapFactory().iconFromTheme("button_up"));
72
auto sepItem = new QTreeWidgetItem;
73
sepItem->setText(1, tr("<Separator>"));
74
sepItem->setData(1, Qt::UserRole, QByteArray("Separator"));
75
sepItem->setSizeHint(0, QSize(32, 32));
77
conn = DlgCustomKeyboardImp::initCommandWidgets(ui->commandTreeWidget,
82
// fills the combo box with all available workbenches
83
QStringList workbenches = Application::Instance->workbenches();
86
ui->workbenchBox->addItem(QApplication::windowIcon(), tr("Global"));
87
ui->workbenchBox->setItemData(0, QVariant(QString::fromLatin1("Global")), Qt::UserRole);
88
for (const auto& workbench : workbenches) {
89
QPixmap px = Application::Instance->workbenchIcon(workbench);
90
QString mt = Application::Instance->workbenchMenuText(workbench);
91
if (mt != QLatin1String("<none>")) {
93
ui->workbenchBox->addItem(mt);
96
ui->workbenchBox->addItem(px, mt);
98
ui->workbenchBox->setItemData(index, QVariant(workbench), Qt::UserRole);
104
labels << tr("Command");
105
ui->toolbarTreeWidget->setHeaderLabels(labels);
106
ui->toolbarTreeWidget->header()->hide();
108
Workbench* w = WorkbenchManager::instance()->active();
110
QString name = QString::fromLatin1(w->name().c_str());
111
int index = ui->workbenchBox->findData(name);
112
ui->workbenchBox->setCurrentIndex(index);
114
onWorkbenchBoxActivated(ui->workbenchBox->currentIndex());
117
/** Destroys the object and frees any allocated resources */
118
DlgCustomToolbars::~DlgCustomToolbars() = default;
120
void DlgCustomToolbars::setupConnections()
123
connect(ui->workbenchBox, qOverload<int>(&QComboBox::activated),
124
this, &DlgCustomToolbars::onWorkbenchBoxActivated);
125
connect(ui->moveActionRightButton, &QPushButton::clicked,
126
this, &DlgCustomToolbars::onMoveActionRightButtonClicked);
127
connect(ui->moveActionLeftButton, &QPushButton::clicked,
128
this, &DlgCustomToolbars::onMoveActionLeftButtonClicked);
129
connect(ui->moveActionUpButton, &QPushButton::clicked,
130
this, &DlgCustomToolbars::onMoveActionUpButtonClicked);
131
connect(ui->moveActionDownButton, &QPushButton::clicked,
132
this, &DlgCustomToolbars::onMoveActionDownButtonClicked);
133
connect(ui->newButton, &QPushButton::clicked,
134
this, &DlgCustomToolbars::onNewButtonClicked);
135
connect(ui->renameButton, &QPushButton::clicked,
136
this, &DlgCustomToolbars::onRenameButtonClicked);
137
connect(ui->deleteButton, &QPushButton::clicked,
138
this, &DlgCustomToolbars::onDeleteButtonClicked);
142
void DlgCustomToolbars::addCustomToolbar(const QString&)
145
void DlgCustomToolbars::removeCustomToolbar(const QString&)
148
void DlgCustomToolbars::renameCustomToolbar(const QString&, const QString&)
151
void DlgCustomToolbars::addCustomCommand(const QString&, const QByteArray&)
154
void DlgCustomToolbars::removeCustomCommand(const QString&, const QByteArray&)
157
void DlgCustomToolbars::moveUpCustomCommand(const QString&, const QByteArray&)
160
void DlgCustomToolbars::moveDownCustomCommand(const QString&, const QByteArray&)
163
void DlgCustomToolbars::hideEvent(QHideEvent* event)
165
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
166
QString workbench = data.toString();
167
exportCustomToolbars(workbench.toLatin1());
169
CustomizeActionPage::hideEvent(event);
172
void DlgCustomToolbars::onActivateCategoryBox()
175
void DlgCustomToolbars::onWorkbenchBoxActivated(int index)
177
QVariant data = ui->workbenchBox->itemData(index, Qt::UserRole);
178
QString workbench = data.toString();
179
ui->toolbarTreeWidget->clear();
181
QByteArray workbenchname = workbench.toLatin1();
182
importCustomToolbars(workbenchname);
185
void DlgCustomToolbars::importCustomToolbars(const QByteArray& name)
187
ParameterGrp::handle hGrp =
188
App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Workbench");
189
const char* subgroup = (type == Toolbar ? "Toolbar" : "Toolboxbar");
190
if (!hGrp->HasGroup(name.constData())) {
193
hGrp = hGrp->GetGroup(name.constData());
194
if (!hGrp->HasGroup(subgroup)) {
197
hGrp = hGrp->GetGroup(subgroup);
198
std::string separator = "Separator";
200
std::vector<Base::Reference<ParameterGrp>> hGrps = hGrp->GetGroups();
201
CommandManager& rMgr = Application::Instance->commandManager();
202
for (const auto& hGrp : hGrps) {
203
// create a toplevel item
204
auto toplevel = new QTreeWidgetItem(ui->toolbarTreeWidget);
205
bool active = hGrp->GetBool("Active", true);
206
toplevel->setCheckState(0, (active ? Qt::Checked : Qt::Unchecked));
208
// get the elements of the subgroups
209
std::vector<std::pair<std::string, std::string>> items = hGrp->GetASCIIMap();
210
for (const auto& it2 : items) {
211
// since we have stored the separators to the user parameters as (key, pair) we had to
212
// make sure to use a unique key because otherwise we cannot store more than
214
if (it2.first.substr(0, separator.size()) == separator) {
215
auto item = new QTreeWidgetItem(toplevel);
216
item->setText(0, tr("<Separator>"));
217
item->setData(0, Qt::UserRole, QByteArray("Separator"));
218
item->setSizeHint(0, QSize(32, 32));
220
else if (it2.first == "Name") {
221
QString toolbarName = QString::fromUtf8(it2.second.c_str());
222
toplevel->setText(0, toolbarName);
225
Command* pCmd = rMgr.getCommandByName(it2.first.c_str());
228
auto* item = new QTreeWidgetItem(toplevel);
229
item->setText(0, Action::commandMenuText(pCmd));
230
item->setToolTip(0, Action::commandToolTip(pCmd));
231
item->setData(0, Qt::UserRole, QByteArray(it2.first.c_str()));
232
if (pCmd->getPixmap()) {
233
item->setIcon(0, BitmapFactory().iconFromTheme(pCmd->getPixmap()));
235
item->setSizeHint(0, QSize(32, 32));
238
// If corresponding module is not yet loaded do not lose the entry
239
auto item = new QTreeWidgetItem(toplevel);
242
tr("%1 module not loaded").arg(QString::fromStdString(it2.second)));
243
item->setData(0, Qt::UserRole, QByteArray(it2.first.c_str()));
244
item->setData(0, Qt::WhatsThisPropertyRole, QByteArray(it2.second.c_str()));
245
item->setSizeHint(0, QSize(32, 32));
252
void DlgCustomToolbars::exportCustomToolbars(const QByteArray& workbench)
254
ParameterGrp::handle hGrp =
255
App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Workbench");
256
const char* subgroup = (type == Toolbar ? "Toolbar" : "Toolboxbar");
257
hGrp = hGrp->GetGroup(workbench.constData())->GetGroup(subgroup);
260
CommandManager& rMgr = Application::Instance->commandManager();
261
for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
262
QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
263
QString groupName = QString::fromLatin1("Custom_%1").arg(i + 1);
264
QByteArray toolbarName = toplevel->text(0).toUtf8();
265
ParameterGrp::handle hToolGrp = hGrp->GetGroup(groupName.toLatin1());
266
hToolGrp->SetASCII("Name", toolbarName.constData());
267
hToolGrp->SetBool("Active", toplevel->checkState(0) == Qt::Checked);
269
// since we store the separators to the user parameters as (key, pair) we must
270
// make sure to use a unique key because otherwise we cannot store more than
272
int suffixSeparator = 1;
273
for (int j = 0; j < toplevel->childCount(); j++) {
274
QTreeWidgetItem* child = toplevel->child(j);
275
QByteArray commandName = child->data(0, Qt::UserRole).toByteArray();
276
if (commandName == "Separator") {
277
QByteArray key = commandName + QByteArray::number(suffixSeparator);
279
hToolGrp->SetASCII(key, commandName);
282
Command* pCmd = rMgr.getCommandByName(commandName);
284
hToolGrp->SetASCII(pCmd->getName(), pCmd->getAppModuleName());
287
QByteArray moduleName = child->data(0, Qt::WhatsThisPropertyRole).toByteArray();
288
hToolGrp->SetASCII(commandName, moduleName);
295
/** Adds a new action */
296
void DlgCustomToolbars::onMoveActionRightButtonClicked()
298
QTreeWidgetItem* item = ui->commandTreeWidget->currentItem();
300
QTreeWidgetItem* current = ui->toolbarTreeWidget->currentItem();
302
current = ui->toolbarTreeWidget->topLevelItem(0);
304
else if (current->parent()) {
305
current = current->parent();
307
if (current && !current->parent()) {
308
auto copy = new QTreeWidgetItem(current);
309
copy->setText(0, item->text(1));
310
copy->setIcon(0, item->icon(0));
311
QByteArray data = item->data(1, Qt::UserRole).toByteArray();
312
copy->setData(0, Qt::UserRole, data);
313
copy->setSizeHint(0, QSize(32, 32));
314
addCustomCommand(current->text(0), data);
318
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
319
QString workbench = data.toString();
320
exportCustomToolbars(workbench.toLatin1());
323
/** Removes an action */
324
void DlgCustomToolbars::onMoveActionLeftButtonClicked()
326
QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
327
if (item && item->parent() && item->isSelected()) {
328
QTreeWidgetItem* parent = item->parent();
329
int index = parent->indexOfChild(item);
330
parent->takeChild(index);
332
// In case a separator should be moved we have to count the separators
333
// which come before this one.
334
// This is needed so that we can distinguish in removeCustomCommand
335
// which separator it is.
336
QByteArray data = item->data(0, Qt::UserRole).toByteArray();
337
if (data == "Separator") {
339
for (int i = 0; i < index - 1; i++) {
340
QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
341
if (d == "Separator") {
346
data += QByteArray::number(countSep);
348
removeCustomCommand(parent->text(0), data);
352
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
353
QString workbench = data.toString();
354
exportCustomToolbars(workbench.toLatin1());
357
/** Moves up an action */
358
void DlgCustomToolbars::onMoveActionUpButtonClicked()
360
QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
361
if (item && item->parent() && item->isSelected()) {
362
QTreeWidgetItem* parent = item->parent();
363
int index = parent->indexOfChild(item);
365
// In case a separator should be moved we have to count the separators
366
// which come before this one.
367
// This is needed so that we can distinguish in moveUpCustomCommand
368
// which separator it is.
369
QByteArray data = item->data(0, Qt::UserRole).toByteArray();
370
if (data == "Separator") {
372
for (int i = 0; i < index; i++) {
373
QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
374
if (d == "Separator") {
379
data += QByteArray::number(countSep);
382
parent->takeChild(index);
383
parent->insertChild(index - 1, item);
384
ui->toolbarTreeWidget->setCurrentItem(item);
386
moveUpCustomCommand(parent->text(0), data);
390
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
391
QString workbench = data.toString();
392
exportCustomToolbars(workbench.toLatin1());
395
/** Moves down an action */
396
void DlgCustomToolbars::onMoveActionDownButtonClicked()
398
QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
399
if (item && item->parent() && item->isSelected()) {
400
QTreeWidgetItem* parent = item->parent();
401
int index = parent->indexOfChild(item);
402
if (index < parent->childCount() - 1) {
403
// In case a separator should be moved we have to count the separators
404
// which come before this one.
405
// This is needed so that we can distinguish in moveDownCustomCommand
406
// which separator it is.
407
QByteArray data = item->data(0, Qt::UserRole).toByteArray();
408
if (data == "Separator") {
410
for (int i = 0; i < index; i++) {
411
QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
412
if (d == "Separator") {
417
data += QByteArray::number(countSep);
420
parent->takeChild(index);
421
parent->insertChild(index + 1, item);
422
ui->toolbarTreeWidget->setCurrentItem(item);
424
moveDownCustomCommand(parent->text(0), data);
428
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
429
QString workbench = data.toString();
430
exportCustomToolbars(workbench.toLatin1());
433
void DlgCustomToolbars::onNewButtonClicked()
437
QString::fromLatin1("Custom%1").arg(ui->toolbarTreeWidget->topLevelItemCount() + 1);
438
text = QInputDialog::getText(this,
444
Qt::MSWindowsFixedSizeDialogHint);
446
// Check for duplicated name
447
for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
448
QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
449
QString groupName = toplevel->text(0);
450
if (groupName == text) {
451
QMessageBox::warning(this,
452
tr("Duplicated name"),
453
tr("The toolbar name '%1' is already used").arg(text));
458
auto item = new QTreeWidgetItem(ui->toolbarTreeWidget);
459
item->setText(0, text);
460
item->setCheckState(0, Qt::Checked);
461
item->setExpanded(true);
463
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
464
QString workbench = data.toString();
465
exportCustomToolbars(workbench.toLatin1());
466
addCustomToolbar(text);
470
void DlgCustomToolbars::onDeleteButtonClicked()
472
QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
473
if (item && !item->parent() && item->isSelected()) {
474
int index = ui->toolbarTreeWidget->indexOfTopLevelItem(item);
475
ui->toolbarTreeWidget->takeTopLevelItem(index);
476
removeCustomToolbar(item->text(0));
480
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
481
QString workbench = data.toString();
482
exportCustomToolbars(workbench.toLatin1());
485
void DlgCustomToolbars::onRenameButtonClicked()
487
bool renamed = false;
488
QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
489
if (item && !item->parent() && item->isSelected()) {
491
QString old_text = item->text(0);
492
QString text = QInputDialog::getText(this,
493
tr("Rename toolbar"),
498
Qt::MSWindowsFixedSizeDialogHint);
499
if (ok && text != old_text) {
500
// Check for duplicated name
501
for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
502
QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
503
QString groupName = toplevel->text(0);
504
if (groupName == text && toplevel != item) {
505
QMessageBox::warning(this,
506
tr("Duplicated name"),
507
tr("The toolbar name '%1' is already used").arg(text));
512
item->setText(0, text);
513
renameCustomToolbar(old_text, text);
519
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
520
QString workbench = data.toString();
521
exportCustomToolbars(workbench.toLatin1());
525
void DlgCustomToolbars::onAddMacroAction(const QByteArray&)
528
void DlgCustomToolbars::onRemoveMacroAction(const QByteArray&)
531
void DlgCustomToolbars::onModifyMacroAction(const QByteArray& macro)
533
QVariant data = ui->categoryBox->itemData(ui->categoryBox->currentIndex(), Qt::UserRole);
534
QString group = data.toString();
535
if (group == QLatin1String("Macros")) {
536
CommandManager& cCmdMgr = Application::Instance->commandManager();
537
Command* pCmd = cCmdMgr.getCommandByName(macro);
539
for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
540
QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
541
for (int j = 0; j < toplevel->childCount(); j++) {
542
QTreeWidgetItem* item = toplevel->child(j);
543
QByteArray command = item->data(0, Qt::UserRole).toByteArray();
544
if (command == macro) {
545
item->setText(0, Action::commandMenuText(pCmd));
546
item->setToolTip(0, Action::commandToolTip(pCmd));
547
if (pCmd->getPixmap()) {
548
item->setIcon(0, BitmapFactory().iconFromTheme(pCmd->getPixmap()));
553
ui->categoryBox->activated(ui->categoryBox->currentIndex());
557
void DlgCustomToolbars::changeEvent(QEvent* e)
559
if (e->type() == QEvent::LanguageChange) {
560
ui->retranslateUi(this);
561
int count = ui->categoryBox->count();
563
CommandManager& cCmdMgr = Application::Instance->commandManager();
564
for (int i = 0; i < count; i++) {
565
QVariant data = ui->categoryBox->itemData(i, Qt::UserRole);
566
std::vector<Command*> aCmds = cCmdMgr.getGroupCommands(data.toByteArray());
567
if (!aCmds.empty()) {
568
QString text = aCmds[0]->translatedGroupName();
569
ui->categoryBox->setItemText(i, text);
572
ui->categoryBox->activated(ui->categoryBox->currentIndex());
574
else if (e->type() == QEvent::StyleChange) {
575
ui->categoryBox->activated(ui->categoryBox->currentIndex());
578
QWidget::changeEvent(e);
581
// -------------------------------------------------------------
583
/* TRANSLATOR Gui::Dialog::DlgCustomToolbarsImp */
586
* Constructs a DlgCustomToolbarsImp which is a child of 'parent', with the
587
* name 'name' and widget flags set to 'f'
589
* The dialog will by default be modeless, unless you set 'modal' to
590
* true to construct a modal dialog.
592
DlgCustomToolbarsImp::DlgCustomToolbarsImp(QWidget* parent)
593
: DlgCustomToolbars(DlgCustomToolbars::Toolbar, parent)
596
/** Destroys the object and frees any allocated resources */
597
DlgCustomToolbarsImp::~DlgCustomToolbarsImp() = default;
599
void DlgCustomToolbarsImp::addCustomToolbar(const QString& name)
601
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
602
Workbench* w = WorkbenchManager::instance()->active();
603
if (w && w->name() == std::string((const char*)data.toByteArray())) {
604
QToolBar* bar = getMainWindow()->addToolBar(name);
605
bar->setObjectName(name);
609
void DlgCustomToolbarsImp::removeCustomToolbar(const QString& name)
611
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
612
Workbench* w = WorkbenchManager::instance()->active();
613
if (w && w->name() == std::string((const char*)data.toByteArray())) {
614
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
615
if (bars.size() != 1) {
619
QToolBar* tb = bars.front();
620
getMainWindow()->removeToolBar(tb);
625
void DlgCustomToolbarsImp::renameCustomToolbar(const QString& old_name, const QString& new_name)
627
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
628
Workbench* w = WorkbenchManager::instance()->active();
629
if (w && w->name() == std::string((const char*)data.toByteArray())) {
630
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(old_name);
631
if (bars.size() != 1) {
635
QToolBar* tb = bars.front();
636
tb->setObjectName(new_name);
637
tb->setWindowTitle(new_name);
641
QList<QAction*> DlgCustomToolbarsImp::getActionGroup(QAction* action)
643
QList<QAction*> group;
644
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
645
QList<QWidget*> widgets = action->associatedWidgets();
647
QList<QObject*> widgets = action->associatedObjects();
649
for (const auto& widget : widgets) {
650
auto tb = qobject_cast<QToolButton*>(widget);
652
QMenu* menu = tb->menu();
654
group = menu->actions();
662
void DlgCustomToolbarsImp::setActionGroup(QAction* action, const QList<QAction*>& group)
664
// See also ActionGroup::addTo()
665
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
666
QList<QWidget*> widgets = action->associatedWidgets();
668
QList<QObject*> widgets = action->associatedObjects();
670
for (const auto& widget : widgets) {
671
auto tb = qobject_cast<QToolButton*>(widget);
673
QMenu* menu = tb->menu();
675
tb->setPopupMode(QToolButton::MenuButtonPopup);
676
tb->setObjectName(QString::fromLatin1("qt_toolbutton_menubutton"));
677
auto menu = new QMenu(tb);
678
menu->addActions(group);
685
void DlgCustomToolbarsImp::addCustomCommand(const QString& name, const QByteArray& cmd)
687
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
688
Workbench* w = WorkbenchManager::instance()->active();
689
if (w && w->name() == std::string((const char*)data.toByteArray())) {
690
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
691
if (bars.size() != 1) {
695
if (cmd == "Separator") {
696
QAction* action = bars.front()->addSeparator();
697
action->setData(QByteArray("Separator"));
700
CommandManager& mgr = Application::Instance->commandManager();
701
if (mgr.addTo(cmd, bars.front())) {
702
QAction* action = bars.front()->actions().last();
703
// See ToolBarManager::setup(ToolBarItem* , QToolBar* )
704
// We have to add the user data in order to identify the command in
705
// removeCustomCommand(), moveUpCustomCommand() or moveDownCustomCommand()
706
if (action && action->data().isNull()) {
707
action->setData(cmd);
714
void DlgCustomToolbarsImp::removeCustomCommand(const QString& name, const QByteArray& userdata)
716
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
717
Workbench* w = WorkbenchManager::instance()->active();
718
if (w && w->name() == std::string((const char*)data.toByteArray())) {
719
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
720
if (bars.size() != 1) {
724
QByteArray cmd = userdata;
725
int numSep = 0, indexSep = 0;
726
if (cmd.startsWith("Separator")) {
727
numSep = cmd.mid(9).toInt();
730
QList<QAction*> actions = bars.front()->actions();
731
for (const auto& action : actions) {
732
if (action->data().toByteArray() == cmd) {
733
// if we move a separator then make sure to pick up the right one
735
if (++indexSep < numSep) {
739
bars.front()->removeAction(action);
746
void DlgCustomToolbarsImp::moveUpCustomCommand(const QString& name, const QByteArray& userdata)
748
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
749
Workbench* w = WorkbenchManager::instance()->active();
750
if (w && w->name() == std::string((const char*)data.toByteArray())) {
751
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
752
if (bars.size() != 1) {
756
QByteArray cmd = userdata;
757
int numSep = 0, indexSep = 0;
758
if (cmd.startsWith("Separator")) {
759
numSep = cmd.mid(9).toInt();
762
QList<QAction*> actions = bars.front()->actions();
763
QAction* before = nullptr;
764
for (const auto& action : actions) {
765
if (action->data().toByteArray() == cmd) {
766
// if we move a separator then make sure to pick up the right one
768
if (++indexSep < numSep) {
774
QList<QAction*> group = getActionGroup(action);
775
bars.front()->removeAction(action);
776
bars.front()->insertAction(before, action);
777
if (!group.isEmpty()) {
778
setActionGroup(action, group);
789
void DlgCustomToolbarsImp::moveDownCustomCommand(const QString& name, const QByteArray& userdata)
791
QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
792
Workbench* w = WorkbenchManager::instance()->active();
793
if (w && w->name() == std::string((const char*)data.toByteArray())) {
794
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
795
if (bars.size() != 1) {
799
QByteArray cmd = userdata;
800
int numSep = 0, indexSep = 0;
801
if (cmd.startsWith("Separator")) {
802
numSep = cmd.mid(9).toInt();
805
QList<QAction*> actions = bars.front()->actions();
806
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
807
if ((*it)->data().toByteArray() == cmd) {
808
// if we move a separator then make sure to pick up the right one
810
if (++indexSep < numSep) {
815
if (*it == actions.back()) {
816
break; // we're already on the last element
820
if (*it == actions.back()) {
821
QList<QAction*> group = getActionGroup(act);
822
bars.front()->removeAction(act);
823
bars.front()->addAction(act);
824
if (!group.isEmpty()) {
825
setActionGroup(act, group);
830
QList<QAction*> group = getActionGroup(act);
831
bars.front()->removeAction(act);
832
bars.front()->insertAction(*it, act);
833
if (!group.isEmpty()) {
834
setActionGroup(act, group);
842
void DlgCustomToolbarsImp::showEvent(QShowEvent* event)
845
// If we did this already in the constructor we wouldn't get the vertical scrollbar if needed.
846
// The problem was noticed with Qt 4.1.4 but may arise with any later version.
848
ui->categoryBox->activated(ui->categoryBox->currentIndex());
853
void DlgCustomToolbarsImp::changeEvent(QEvent* e)
855
DlgCustomToolbars::changeEvent(e);
859
/* TRANSLATOR Gui::Dialog::DlgCustomToolBoxbarsImp */
862
* Constructs a DlgCustomToolBoxbarsImp which is a child of 'parent', with the
863
* name 'name' and widget flags set to 'f'
865
* The dialog will by default be modeless, unless you set 'modal' to
866
* true to construct a modal dialog.
868
DlgCustomToolBoxbarsImp::DlgCustomToolBoxbarsImp(QWidget* parent)
869
: DlgCustomToolbars(DlgCustomToolbars::Toolboxbar, parent)
871
setWindowTitle(tr("Toolbox bars"));
874
/** Destroys the object and frees any allocated resources */
875
DlgCustomToolBoxbarsImp::~DlgCustomToolBoxbarsImp() = default;
877
void DlgCustomToolBoxbarsImp::changeEvent(QEvent* e)
879
if (e->type() == QEvent::LanguageChange) {
880
setWindowTitle(tr("Toolbox bars"));
882
DlgCustomToolbars::changeEvent(e);
885
#include "moc_DlgToolbarsImp.cpp"