Debian Codegraph @

src/Gui/Dialogs/DlgToolbarsImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
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. *
10 * *
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. *
15 * *
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 *
20 * *
21 ***************************************************************************/
22
23 #include <QHeaderView>
24 #include <QInputDialog>
25 #include <QMenu>
26 #include <QMessageBox>
27 #include <QToolBar>
28 #include <QToolButton>
29
30 #include "Dialogs/DlgToolbarsImp.h"
31 #include "Dialogs/DlgKeyboardImp.h"
32 #include "ui_DlgToolbars.h"
33 #include "Application.h"
34 #include "BitmapFactory.h"
35 #include "Command.h"
36 #include "Action.h"
37 #include "ToolBarManager.h"
38 #include "MainWindow.h"
39 #include "ToolBarManager.h"
40 #include "Widgets.h"
41 #include "Workbench.h"
42 #include "WorkbenchManager.h"
43
44
45 using namespace Gui::Dialog;
46
47 /* TRANSLATOR Gui::Dialog::DlgCustomToolbars */
48
49 /**
50 * Constructs a DlgCustomToolbars which is a child of 'parent', with the
51 * name 'name' and widget flags set to 'f'
52 *
53 * The dialog will by default be modeless, unless you set 'modal' to
54 * true to construct a modal dialog.
55 */
56 DlgCustomToolbars::DlgCustomToolbars(DlgCustomToolbars::Type t, QWidget* parent)
57 : CustomizeActionPage(parent)
58 , ui(new Ui_DlgCustomToolbars)
59 , type(t)
60 {
61 ui->setupUi(this);
62 setupConnections();
63
64 ui->moveActionRightButton->setIcon(BitmapFactory().iconFromTheme("button_right"));
65 ui->moveActionLeftButton->setIcon(BitmapFactory().iconFromTheme("button_left"));
66 ui->moveActionDownButton->setIcon(BitmapFactory().iconFromTheme("button_down"));
67 ui->moveActionUpButton->setIcon(BitmapFactory().iconFromTheme("button_up"));
68
69 auto sepItem = new QTreeWidgetItem;
70 sepItem->setText(1, tr("<Separator>"));
71 sepItem->setData(1, Qt::UserRole, QByteArray("Separator"));
72 sepItem->setSizeHint(0, QSize(32, 32));
73
74 conn = DlgCustomKeyboardImp::initCommandWidgets(
75 ui->commandTreeWidget,
76 sepItem,
77 ui->categoryBox,
78 ui->editCommand
79 );
80
81 // fills the combo box with all available workbenches
82 QStringList workbenches = Application::Instance->workbenches();
83 workbenches.sort();
84 int index = 1;
85 ui->workbenchBox->addItem(QApplication::windowIcon(), tr("Global"));
86 ui->workbenchBox->setItemData(0, QVariant(QStringLiteral("Global")), Qt::UserRole);
87 for (const auto& workbench : workbenches) {
88 QPixmap px = Application::Instance->workbenchIcon(workbench);
89 QString mt = Application::Instance->workbenchMenuText(workbench);
90 if (mt != QLatin1String("<none>")) {
91 if (px.isNull()) {
92 ui->workbenchBox->addItem(mt);
93 }
94 else {
95 ui->workbenchBox->addItem(px, mt);
96 }
97 ui->workbenchBox->setItemData(index, QVariant(workbench), Qt::UserRole);
98 index++;
99 }
100 }
101
102 QStringList labels;
103 labels << tr("Command");
104 ui->toolbarTreeWidget->setHeaderLabels(labels);
105 ui->toolbarTreeWidget->header()->hide();
106
107 Workbench* w = WorkbenchManager::instance()->active();
108 if (w) {
109 QString name = QString::fromLatin1(w->name().c_str());
110 int index = ui->workbenchBox->findData(name);
111 ui->workbenchBox->setCurrentIndex(index);
112 }
113 onWorkbenchBoxActivated(ui->workbenchBox->currentIndex());
114 }
115
116 /** Destroys the object and frees any allocated resources */
117 DlgCustomToolbars::~DlgCustomToolbars() = default;
118
119 void DlgCustomToolbars::setupConnections()
120 {
121 // clang-format off
122 connect(ui->workbenchBox, qOverload<int>(&QComboBox::activated),
123 this, &DlgCustomToolbars::onWorkbenchBoxActivated);
124 connect(ui->moveActionRightButton, &QPushButton::clicked,
125 this, &DlgCustomToolbars::onMoveActionRightButtonClicked);
126 connect(ui->moveActionLeftButton, &QPushButton::clicked,
127 this, &DlgCustomToolbars::onMoveActionLeftButtonClicked);
128 connect(ui->moveActionUpButton, &QPushButton::clicked,
129 this, &DlgCustomToolbars::onMoveActionUpButtonClicked);
130 connect(ui->moveActionDownButton, &QPushButton::clicked,
131 this, &DlgCustomToolbars::onMoveActionDownButtonClicked);
132 connect(ui->newButton, &QPushButton::clicked,
133 this, &DlgCustomToolbars::onNewButtonClicked);
134 connect(ui->renameButton, &QPushButton::clicked,
135 this, &DlgCustomToolbars::onRenameButtonClicked);
136 connect(ui->deleteButton, &QPushButton::clicked,
137 this, &DlgCustomToolbars::onDeleteButtonClicked);
138 // clang-format on
139 }
140
141 void DlgCustomToolbars::addCustomToolbar(const QString&)
142 {}
143
144 void DlgCustomToolbars::removeCustomToolbar(const QString&)
145 {}
146
147 void DlgCustomToolbars::renameCustomToolbar(const QString&, const QString&)
148 {}
149
150 void DlgCustomToolbars::addCustomCommand(const QString&, const QByteArray&)
151 {}
152
153 void DlgCustomToolbars::removeCustomCommand(const QString&, const QByteArray&)
154 {}
155
156 void DlgCustomToolbars::moveUpCustomCommand(const QString&, const QByteArray&)
157 {}
158
159 void DlgCustomToolbars::moveDownCustomCommand(const QString&, const QByteArray&)
160 {}
161
162 void DlgCustomToolbars::hideEvent(QHideEvent* event)
163 {
164 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
165 QString workbench = data.toString();
166 exportCustomToolbars(workbench.toLatin1());
167
168 CustomizeActionPage::hideEvent(event);
169 }
170
171 void DlgCustomToolbars::onActivateCategoryBox()
172 {}
173
174 // called from DlgMacroExecuteImp toolbar walkthrough function
175 void DlgCustomToolbars::activateWorkbenchBox(int index)
176 {
177 onWorkbenchBoxActivated(index);
178 }
179
180 void DlgCustomToolbars::onWorkbenchBoxActivated(int index)
181 {
182 QVariant data = ui->workbenchBox->itemData(index, Qt::UserRole);
183 QString workbench = data.toString();
184 ui->toolbarTreeWidget->clear();
185
186 QByteArray workbenchname = workbench.toLatin1();
187 importCustomToolbars(workbenchname);
188 }
189
190 void DlgCustomToolbars::importCustomToolbars(const QByteArray& name)
191 {
192 ParameterGrp::handle hGrp
193 = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Workbench");
194 const char* subgroup = (type == Toolbar ? "Toolbar" : "Toolboxbar");
195 if (!hGrp->HasGroup(name.constData())) {
196 return;
197 }
198 hGrp = hGrp->GetGroup(name.constData());
199 if (!hGrp->HasGroup(subgroup)) {
200 return;
201 }
202 hGrp = hGrp->GetGroup(subgroup);
203 std::string separator = "Separator";
204
205 std::vector<Base::Reference<ParameterGrp>> hGrps = hGrp->GetGroups();
206 CommandManager& rMgr = Application::Instance->commandManager();
207 for (const auto& hGrp : hGrps) {
208 // create a toplevel item
209 auto toplevel = new QTreeWidgetItem(ui->toolbarTreeWidget);
210 bool active = hGrp->GetBool("Active", true);
211 toplevel->setCheckState(0, (active ? Qt::Checked : Qt::Unchecked));
212
213 // get the elements of the subgroups
214 std::vector<std::pair<std::string, std::string>> items = hGrp->GetASCIIMap();
215 for (const auto& it2 : items) {
216 // since we have stored the separators to the user parameters as (key, pair) we had to
217 // make sure to use a unique key because otherwise we cannot store more than
218 // one.
219 if (it2.first.substr(0, separator.size()) == separator) {
220 auto item = new QTreeWidgetItem(toplevel);
221 item->setText(0, tr("<Separator>"));
222 item->setData(0, Qt::UserRole, QByteArray("Separator"));
223 item->setSizeHint(0, QSize(32, 32));
224 }
225 else if (it2.first == "Name") {
226 QString toolbarName = QString::fromUtf8(it2.second.c_str());
227 toplevel->setText(0, toolbarName);
228 }
229 else {
230 Command* pCmd = rMgr.getCommandByName(it2.first.c_str());
231 if (pCmd) {
232 // command name
233 auto* item = new QTreeWidgetItem(toplevel);
234 item->setText(0, Action::commandMenuText(pCmd));
235 item->setToolTip(0, Action::commandToolTip(pCmd));
236 item->setData(0, Qt::UserRole, QByteArray(it2.first.c_str()));
237 if (pCmd->getPixmap()) {
238 item->setIcon(0, BitmapFactory().iconFromTheme(pCmd->getPixmap()));
239 }
240 item->setSizeHint(0, QSize(32, 32));
241 }
242 else {
243 // If corresponding module is not yet loaded do not lose the entry
244 auto item = new QTreeWidgetItem(toplevel);
245 item->setText(0, tr("%1 module not loaded").arg(QString::fromStdString(it2.second)));
246 item->setData(0, Qt::UserRole, QByteArray(it2.first.c_str()));
247 item->setData(0, Qt::WhatsThisPropertyRole, QByteArray(it2.second.c_str()));
248 item->setSizeHint(0, QSize(32, 32));
249 }
250 }
251 }
252 }
253 }
254
255 void DlgCustomToolbars::exportCustomToolbars(const QByteArray& workbench)
256 {
257 ParameterGrp::handle hGrp
258 = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Workbench");
259 const char* subgroup = (type == Toolbar ? "Toolbar" : "Toolboxbar");
260 hGrp = hGrp->GetGroup(workbench.constData())->GetGroup(subgroup);
261 hGrp->Clear();
262
263 CommandManager& rMgr = Application::Instance->commandManager();
264 for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
265 QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
266 QString groupName = QStringLiteral("Custom_%1").arg(i + 1);
267 QByteArray toolbarName = toplevel->text(0).toUtf8();
268 ParameterGrp::handle hToolGrp = hGrp->GetGroup(groupName.toLatin1());
269 hToolGrp->SetASCII("Name", toolbarName.constData());
270 hToolGrp->SetBool("Active", toplevel->checkState(0) == Qt::Checked);
271
272 // since we store the separators to the user parameters as (key, pair) we must
273 // make sure to use a unique key because otherwise we cannot store more than
274 // one.
275 int suffixSeparator = 1;
276 for (int j = 0; j < toplevel->childCount(); j++) {
277 QTreeWidgetItem* child = toplevel->child(j);
278 QByteArray commandName = child->data(0, Qt::UserRole).toByteArray();
279 if (commandName == "Separator") {
280 QByteArray key = commandName + QByteArray::number(suffixSeparator);
281 suffixSeparator++;
282 hToolGrp->SetASCII(key, commandName);
283 }
284 else {
285 Command* pCmd = rMgr.getCommandByName(commandName);
286 if (pCmd) {
287 hToolGrp->SetASCII(pCmd->getName(), pCmd->getAppModuleName());
288 }
289 else {
290 QByteArray moduleName = child->data(0, Qt::WhatsThisPropertyRole).toByteArray();
291 hToolGrp->SetASCII(commandName, moduleName);
292 }
293 }
294 }
295 }
296 }
297
298 /** Adds a new action */
299 void DlgCustomToolbars::onMoveActionRightButtonClicked()
300 {
301 QTreeWidgetItem* item = ui->commandTreeWidget->currentItem();
302 if (item) {
303 QTreeWidgetItem* current = ui->toolbarTreeWidget->currentItem();
304 if (!current) {
305 current = ui->toolbarTreeWidget->topLevelItem(0);
306 }
307 else if (current->parent()) {
308 current = current->parent();
309 }
310 if (current && !current->parent()) {
311 auto copy = new QTreeWidgetItem(current);
312 copy->setText(0, item->text(1));
313 copy->setIcon(0, item->icon(0));
314 QByteArray data = item->data(1, Qt::UserRole).toByteArray();
315 copy->setData(0, Qt::UserRole, data);
316 copy->setSizeHint(0, QSize(32, 32));
317 addCustomCommand(current->text(0), data);
318 }
319 }
320
321 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
322 QString workbench = data.toString();
323 exportCustomToolbars(workbench.toLatin1());
324 }
325
326 /** Removes an action */
327 void DlgCustomToolbars::onMoveActionLeftButtonClicked()
328 {
329 QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
330 if (item && item->parent() && item->isSelected()) {
331 QTreeWidgetItem* parent = item->parent();
332 int index = parent->indexOfChild(item);
333 parent->takeChild(index);
334
335 // In case a separator should be moved we have to count the separators
336 // which come before this one.
337 // This is needed so that we can distinguish in removeCustomCommand
338 // which separator it is.
339 QByteArray data = item->data(0, Qt::UserRole).toByteArray();
340 if (data == "Separator") {
341 int countSep = 1;
342 for (int i = 0; i < index - 1; i++) {
343 QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
344 if (d == "Separator") {
345 countSep++;
346 }
347 }
348
349 data += QByteArray::number(countSep);
350 }
351 removeCustomCommand(parent->text(0), data);
352 delete item;
353 }
354
355 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
356 QString workbench = data.toString();
357 exportCustomToolbars(workbench.toLatin1());
358 }
359
360 /** Moves up an action */
361 void DlgCustomToolbars::onMoveActionUpButtonClicked()
362 {
363 QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
364 if (item && item->parent() && item->isSelected()) {
365 QTreeWidgetItem* parent = item->parent();
366 int index = parent->indexOfChild(item);
367 if (index > 0) {
368 // In case a separator should be moved we have to count the separators
369 // which come before this one.
370 // This is needed so that we can distinguish in moveUpCustomCommand
371 // which separator it is.
372 QByteArray data = item->data(0, Qt::UserRole).toByteArray();
373 if (data == "Separator") {
374 int countSep = 1;
375 for (int i = 0; i < index; i++) {
376 QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
377 if (d == "Separator") {
378 countSep++;
379 }
380 }
381
382 data += QByteArray::number(countSep);
383 }
384
385 parent->takeChild(index);
386 parent->insertChild(index - 1, item);
387 ui->toolbarTreeWidget->setCurrentItem(item);
388
389 moveUpCustomCommand(parent->text(0), data);
390 }
391 }
392
393 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
394 QString workbench = data.toString();
395 exportCustomToolbars(workbench.toLatin1());
396 }
397
398 /** Moves down an action */
399 void DlgCustomToolbars::onMoveActionDownButtonClicked()
400 {
401 QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
402 if (item && item->parent() && item->isSelected()) {
403 QTreeWidgetItem* parent = item->parent();
404 int index = parent->indexOfChild(item);
405 if (index < parent->childCount() - 1) {
406 // In case a separator should be moved we have to count the separators
407 // which come before this one.
408 // This is needed so that we can distinguish in moveDownCustomCommand
409 // which separator it is.
410 QByteArray data = item->data(0, Qt::UserRole).toByteArray();
411 if (data == "Separator") {
412 int countSep = 1;
413 for (int i = 0; i < index; i++) {
414 QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
415 if (d == "Separator") {
416 countSep++;
417 }
418 }
419
420 data += QByteArray::number(countSep);
421 }
422
423 parent->takeChild(index);
424 parent->insertChild(index + 1, item);
425 ui->toolbarTreeWidget->setCurrentItem(item);
426
427 moveDownCustomCommand(parent->text(0), data);
428 }
429 }
430
431 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
432 QString workbench = data.toString();
433 exportCustomToolbars(workbench.toLatin1());
434 }
435
436 void DlgCustomToolbars::onNewButtonClicked()
437 {
438 bool ok;
439 QString text = QStringLiteral("Custom%1").arg(ui->toolbarTreeWidget->topLevelItemCount() + 1);
440 text = QInputDialog::getText(
441 this,
442 tr("New toolbar"),
443 tr("Toolbar name:"),
444 QLineEdit::Normal,
445 text,
446 &ok,
447 Qt::MSWindowsFixedSizeDialogHint
448 );
449 if (ok) {
450 // Check for duplicated name
451 for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
452 QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
453 QString groupName = toplevel->text(0);
454 if (groupName == text) {
455 QMessageBox::warning(
456 this,
457 tr("Duplicated name"),
458 tr("The toolbar name '%1' is already used").arg(text)
459 );
460 return;
461 }
462 }
463
464 auto item = new QTreeWidgetItem(ui->toolbarTreeWidget);
465 item->setText(0, text);
466 item->setCheckState(0, Qt::Checked);
467 item->setExpanded(true);
468
469 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
470 QString workbench = data.toString();
471 exportCustomToolbars(workbench.toLatin1());
472 addCustomToolbar(text);
473 }
474 }
475
476 void DlgCustomToolbars::onDeleteButtonClicked()
477 {
478 QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
479 if (item && !item->parent() && item->isSelected()) {
480 int index = ui->toolbarTreeWidget->indexOfTopLevelItem(item);
481 ui->toolbarTreeWidget->takeTopLevelItem(index);
482 removeCustomToolbar(item->text(0));
483 delete item;
484 }
485
486 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
487 QString workbench = data.toString();
488 exportCustomToolbars(workbench.toLatin1());
489 }
490
491 void DlgCustomToolbars::onRenameButtonClicked()
492 {
493 bool renamed = false;
494 QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
495 if (item && !item->parent() && item->isSelected()) {
496 bool ok;
497 QString old_text = item->text(0);
498 QString text = QInputDialog::getText(
499 this,
500 tr("Rename toolbar"),
501 tr("Toolbar name:"),
502 QLineEdit::Normal,
503 old_text,
504 &ok,
505 Qt::MSWindowsFixedSizeDialogHint
506 );
507 if (ok && text != old_text) {
508 // Check for duplicated name
509 for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
510 QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
511 QString groupName = toplevel->text(0);
512 if (groupName == text && toplevel != item) {
513 QMessageBox::warning(
514 this,
515 tr("Duplicated name"),
516 tr("The toolbar name '%1' is already used").arg(text)
517 );
518 return;
519 }
520 }
521
522 item->setText(0, text);
523 renameCustomToolbar(old_text, text);
524 renamed = true;
525 }
526 }
527
528 if (renamed) {
529 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
530 QString workbench = data.toString();
531 exportCustomToolbars(workbench.toLatin1());
532 }
533 }
534
535 void DlgCustomToolbars::onAddMacroAction(const QByteArray&)
536 {}
537
538 void DlgCustomToolbars::onRemoveMacroAction(const QByteArray&)
539 {}
540
541 void DlgCustomToolbars::onModifyMacroAction(const QByteArray& macro)
542 {
543 QVariant data = ui->categoryBox->itemData(ui->categoryBox->currentIndex(), Qt::UserRole);
544 QString group = data.toString();
545 if (group == QLatin1String("Macros")) {
546 CommandManager& cCmdMgr = Application::Instance->commandManager();
547 Command* pCmd = cCmdMgr.getCommandByName(macro);
548 // the right side
549 for (int i = 0; i < ui->toolbarTreeWidget->topLevelItemCount(); i++) {
550 QTreeWidgetItem* toplevel = ui->toolbarTreeWidget->topLevelItem(i);
551 for (int j = 0; j < toplevel->childCount(); j++) {
552 QTreeWidgetItem* item = toplevel->child(j);
553 QByteArray command = item->data(0, Qt::UserRole).toByteArray();
554 if (command == macro) {
555 item->setText(0, Action::commandMenuText(pCmd));
556 item->setToolTip(0, Action::commandToolTip(pCmd));
557 if (pCmd->getPixmap()) {
558 item->setIcon(0, BitmapFactory().iconFromTheme(pCmd->getPixmap()));
559 }
560 }
561 }
562 }
563 ui->categoryBox->activated(ui->categoryBox->currentIndex());
564 }
565 }
566
567 void DlgCustomToolbars::changeEvent(QEvent* e)
568 {
569 if (e->type() == QEvent::LanguageChange) {
570 ui->retranslateUi(this);
571 int count = ui->categoryBox->count();
572
573 CommandManager& cCmdMgr = Application::Instance->commandManager();
574 for (int i = 0; i < count; i++) {
575 QVariant data = ui->categoryBox->itemData(i, Qt::UserRole);
576 std::vector<Command*> aCmds = cCmdMgr.getGroupCommands(data.toByteArray());
577 if (!aCmds.empty()) {
578 QString text = aCmds[0]->translatedGroupName();
579 ui->categoryBox->setItemText(i, text);
580 }
581 }
582 ui->categoryBox->activated(ui->categoryBox->currentIndex());
583 }
584 else if (e->type() == QEvent::StyleChange) {
585 ui->categoryBox->activated(ui->categoryBox->currentIndex());
586 }
587
588 QWidget::changeEvent(e);
589 }
590
591 // -------------------------------------------------------------
592
593 /* TRANSLATOR Gui::Dialog::DlgCustomToolbarsImp */
594
595 /**
596 * Constructs a DlgCustomToolbarsImp which is a child of 'parent', with the
597 * name 'name' and widget flags set to 'f'
598 *
599 * The dialog will by default be modeless, unless you set 'modal' to
600 * true to construct a modal dialog.
601 */
602 DlgCustomToolbarsImp::DlgCustomToolbarsImp(QWidget* parent)
603 : DlgCustomToolbars(DlgCustomToolbars::Toolbar, parent)
604 {}
605
606 /** Destroys the object and frees any allocated resources */
607 DlgCustomToolbarsImp::~DlgCustomToolbarsImp() = default;
608
609 void DlgCustomToolbarsImp::addCustomToolbar(const QString& name)
610 {
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 QToolBar* bar = getMainWindow()->addToolBar(name);
615 bar->setObjectName(name);
616 }
617 }
618
619 void DlgCustomToolbarsImp::removeCustomToolbar(const QString& name)
620 {
621 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
622 Workbench* w = WorkbenchManager::instance()->active();
623 if (w && w->name() == std::string((const char*)data.toByteArray())) {
624 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
625 if (bars.size() != 1) {
626 return;
627 }
628
629 QToolBar* tb = bars.front();
630 getMainWindow()->removeToolBar(tb);
631 delete tb;
632 }
633 }
634
635 void DlgCustomToolbarsImp::renameCustomToolbar(const QString& old_name, const QString& new_name)
636 {
637 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
638 Workbench* w = WorkbenchManager::instance()->active();
639 if (w && w->name() == std::string((const char*)data.toByteArray())) {
640 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(old_name);
641 if (bars.size() != 1) {
642 return;
643 }
644
645 QToolBar* tb = bars.front();
646 tb->setObjectName(new_name);
647 tb->setWindowTitle(new_name);
648 }
649 }
650
651 QList<QAction*> DlgCustomToolbarsImp::getActionGroup(QAction* action)
652 {
653 QList<QAction*> group;
654 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
655 QList<QWidget*> widgets = action->associatedWidgets();
656 #else
657 QList<QObject*> widgets = action->associatedObjects();
658 #endif
659 for (const auto& widget : widgets) {
660 auto tb = qobject_cast<QToolButton*>(widget);
661 if (tb) {
662 QMenu* menu = tb->menu();
663 if (menu) {
664 group = menu->actions();
665 break;
666 }
667 }
668 }
669 return group;
670 }
671
672 void DlgCustomToolbarsImp::setActionGroup(QAction* action, const QList<QAction*>& group)
673 {
674 // See also ActionGroup::addTo()
675 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
676 QList<QWidget*> widgets = action->associatedWidgets();
677 #else
678 QList<QObject*> widgets = action->associatedObjects();
679 #endif
680 for (const auto& widget : widgets) {
681 auto tb = qobject_cast<QToolButton*>(widget);
682 if (tb) {
683 QMenu* menu = tb->menu();
684 if (!menu) {
685 tb->setPopupMode(QToolButton::MenuButtonPopup);
686 tb->setObjectName(QStringLiteral("qt_toolbutton_menubutton"));
687 auto menu = new QMenu(tb);
688 menu->addActions(group);
689 tb->setMenu(menu);
690 }
691 }
692 }
693 }
694
695 void DlgCustomToolbarsImp::addCustomCommand(const QString& name, const QByteArray& cmd)
696 {
697 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
698 Workbench* w = WorkbenchManager::instance()->active();
699 if (w && w->name() == std::string((const char*)data.toByteArray())) {
700 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
701 if (bars.size() != 1) {
702 return;
703 }
704
705 if (cmd == "Separator") {
706 QAction* action = bars.front()->addSeparator();
707 action->setData(QByteArray("Separator"));
708 }
709 else {
710 CommandManager& mgr = Application::Instance->commandManager();
711 if (mgr.addTo(cmd, bars.front())) {
712 QAction* action = bars.front()->actions().last();
713 // See ToolBarManager::setup(ToolBarItem* , QToolBar* )
714 // We have to add the user data in order to identify the command in
715 // removeCustomCommand(), moveUpCustomCommand() or moveDownCustomCommand()
716 if (action && action->data().isNull()) {
717 action->setData(cmd);
718 }
719 }
720 }
721 }
722 }
723
724 void DlgCustomToolbarsImp::removeCustomCommand(const QString& name, const QByteArray& userdata)
725 {
726 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
727 Workbench* w = WorkbenchManager::instance()->active();
728 if (w && w->name() == std::string((const char*)data.toByteArray())) {
729 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
730 if (bars.size() != 1) {
731 return;
732 }
733
734 QByteArray cmd = userdata;
735 int numSep = 0, indexSep = 0;
736 if (cmd.startsWith("Separator")) {
737 numSep = cmd.mid(9).toInt();
738 cmd = "Separator";
739 }
740 QList<QAction*> actions = bars.front()->actions();
741 for (const auto& action : actions) {
742 if (action->data().toByteArray() == cmd) {
743 // if we move a separator then make sure to pick up the right one
744 if (numSep > 0) {
745 if (++indexSep < numSep) {
746 continue;
747 }
748 }
749 bars.front()->removeAction(action);
750 break;
751 }
752 }
753 }
754 }
755
756 void DlgCustomToolbarsImp::moveUpCustomCommand(const QString& name, const QByteArray& userdata)
757 {
758 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
759 Workbench* w = WorkbenchManager::instance()->active();
760 if (w && w->name() == std::string((const char*)data.toByteArray())) {
761 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
762 if (bars.size() != 1) {
763 return;
764 }
765
766 QByteArray cmd = userdata;
767 int numSep = 0, indexSep = 0;
768 if (cmd.startsWith("Separator")) {
769 numSep = cmd.mid(9).toInt();
770 cmd = "Separator";
771 }
772 QList<QAction*> actions = bars.front()->actions();
773 QAction* before = nullptr;
774 for (const auto& action : actions) {
775 if (action->data().toByteArray() == cmd) {
776 // if we move a separator then make sure to pick up the right one
777 if (numSep > 0) {
778 if (++indexSep < numSep) {
779 before = action;
780 continue;
781 }
782 }
783 if (before) {
784 QList<QAction*> group = getActionGroup(action);
785 bars.front()->removeAction(action);
786 bars.front()->insertAction(before, action);
787 if (!group.isEmpty()) {
788 setActionGroup(action, group);
789 }
790 break;
791 }
792 }
793
794 before = action;
795 }
796 }
797 }
798
799 void DlgCustomToolbarsImp::moveDownCustomCommand(const QString& name, const QByteArray& userdata)
800 {
801 QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
802 Workbench* w = WorkbenchManager::instance()->active();
803 if (w && w->name() == std::string((const char*)data.toByteArray())) {
804 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
805 if (bars.size() != 1) {
806 return;
807 }
808
809 QByteArray cmd = userdata;
810 int numSep = 0, indexSep = 0;
811 if (cmd.startsWith("Separator")) {
812 numSep = cmd.mid(9).toInt();
813 cmd = "Separator";
814 }
815 QList<QAction*> actions = bars.front()->actions();
816 for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
817 if ((*it)->data().toByteArray() == cmd) {
818 // if we move a separator then make sure to pick up the right one
819 if (numSep > 0) {
820 if (++indexSep < numSep) {
821 continue;
822 }
823 }
824 QAction* act = *it;
825 if (*it == actions.back()) {
826 break; // we're already on the last element
827 }
828 ++it;
829 // second last item
830 if (*it == actions.back()) {
831 QList<QAction*> group = getActionGroup(act);
832 bars.front()->removeAction(act);
833 bars.front()->addAction(act);
834 if (!group.isEmpty()) {
835 setActionGroup(act, group);
836 }
837 break;
838 }
839 ++it;
840 QList<QAction*> group = getActionGroup(act);
841 bars.front()->removeAction(act);
842 bars.front()->insertAction(*it, act);
843 if (!group.isEmpty()) {
844 setActionGroup(act, group);
845 }
846 break;
847 }
848 }
849 }
850 }
851
852 void DlgCustomToolbarsImp::showEvent(QShowEvent* event)
853 {
854 Q_UNUSED(event);
855 // If we did this already in the constructor we wouldn't get the vertical scrollbar if needed.
856 // The problem was noticed with Qt 4.1.4 but may arise with any later version.
857 if (firstShow) {
858 ui->categoryBox->activated(ui->categoryBox->currentIndex());
859 firstShow = false;
860 }
861 }
862
863 void DlgCustomToolbarsImp::changeEvent(QEvent* e)
864 {
865 DlgCustomToolbars::changeEvent(e);
866 }
867
868
869 /* TRANSLATOR Gui::Dialog::DlgCustomToolBoxbarsImp */
870
871 /**
872 * Constructs a DlgCustomToolBoxbarsImp which is a child of 'parent', with the
873 * name 'name' and widget flags set to 'f'
874 *
875 * The dialog will by default be modeless, unless you set 'modal' to
876 * true to construct a modal dialog.
877 */
878 DlgCustomToolBoxbarsImp::DlgCustomToolBoxbarsImp(QWidget* parent)
879 : DlgCustomToolbars(DlgCustomToolbars::Toolboxbar, parent)
880 {
881 setWindowTitle(tr("Toolbox Bars"));
882 }
883
884 /** Destroys the object and frees any allocated resources */
885 DlgCustomToolBoxbarsImp::~DlgCustomToolBoxbarsImp() = default;
886
887 void DlgCustomToolBoxbarsImp::changeEvent(QEvent* e)
888 {
889 if (e->type() == QEvent::LanguageChange) {
890 setWindowTitle(tr("Toolbox Bars"));
891 }
892 DlgCustomToolbars::changeEvent(e);
893 }
894
895 #include "moc_DlgToolbarsImp.cpp"