Debian Codegraph @

src/Gui/PreferencePages/DlgSettingsWorkbenchesImp.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /****************************************************************************
4 * Copyright (c) 2020 Chris Hennes ([email protected]) *
5 * Copyright (c) 2023 FreeCAD Project Association *
6 * *
7 * This file is part of FreeCAD. *
8 * *
9 * FreeCAD is free software: you can redistribute it and/or modify it *
10 * under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 2.1 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with FreeCAD. If not, see *
21 * <https://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25 #include <QAction>
26 #include <QCheckBox>
27 #include <QLabel>
28 #include <QMenu>
29 #include <QPushButton>
30 #include <sstream>
31
32 #include <Gui/Application.h>
33 #include <Gui/Workbench.h>
34 #include <Gui/WorkbenchManager.h>
35
36 #include "DlgSettingsWorkbenchesImp.h"
37 #include "ui_DlgSettingsWorkbenches.h"
38
39
40 using namespace Gui::Dialog;
41
42 namespace Gui::Dialog
43 {
44 class wbListItem: public QWidget
45 {
46 Q_OBJECT
47
48 public:
49 explicit wbListItem(
50 const QString& wbName,
51 bool enabled,
52 bool startupWb,
53 bool autoLoad,
54 int index,
55 QWidget* parent = nullptr
56 );
57 ~wbListItem() override;
58
59 bool isEnabled();
60 bool isAutoLoading();
61 void setStartupWb(bool val);
62
63 void setShortcutLabel(int index);
64
65 protected Q_SLOTS:
66 void onLoadClicked();
67 void onWbToggled(bool checked);
68
69 Q_SIGNALS:
70 void wbToggled(const QString& wbName, bool enabled);
71
72 private:
73 QCheckBox* enableCheckBox;
74 QCheckBox* autoloadCheckBox;
75 QLabel* iconLabel;
76 QLabel* textLabel;
77 QLabel* shortcutLabel;
78 QLabel* loadLabel;
79 QPushButton* loadButton;
80 };
81 } // namespace Gui::Dialog
82
83 wbListItem::wbListItem(
84 const QString& wbName,
85 bool enabled,
86 bool startupWb,
87 bool autoLoad,
88 int index,
89 QWidget* parent
90 )
91 : QWidget(parent)
92 {
93 this->setObjectName(wbName);
94
95 auto wbTooltip = Application::Instance->workbenchToolTip(wbName);
96 auto wbDisplayName = Application::Instance->workbenchMenuText(wbName);
97
98 // 1: Enable checkbox
99 enableCheckBox = new QCheckBox(this);
100 enableCheckBox->setToolTip(
101 tr("Toggles the visibility of %1 in the available workbenches").arg(wbDisplayName)
102 );
103 enableCheckBox->setChecked(enabled);
104 if (startupWb) {
105 enableCheckBox->setChecked(true);
106 enableCheckBox->setEnabled(false);
107 enableCheckBox->setToolTip(tr("This is the current startup module, and must be enabled"));
108 }
109 connect(enableCheckBox, &QCheckBox::toggled, this, [this](bool checked) { onWbToggled(checked); });
110
111 QWidget* subWidget = new QWidget(this);
112 // 2: Workbench Icon
113 auto wbIcon = Application::Instance->workbenchIcon(wbName);
114 iconLabel = new QLabel(wbDisplayName, this);
115 iconLabel->setPixmap(wbIcon.scaled(
116 QSize(20, 20),
117 Qt::AspectRatioMode::KeepAspectRatio,
118 Qt::TransformationMode::SmoothTransformation
119 ));
120 iconLabel->setToolTip(wbTooltip);
121 iconLabel->setContentsMargins(5, 0, 0, 5); // Left, top, right, bottom
122 iconLabel->setEnabled(enableCheckBox->isChecked());
123
124 // 3: Workbench Display Name
125 textLabel = new QLabel(wbDisplayName, this);
126 textLabel->setToolTip(wbTooltip);
127 QFont font = textLabel->font();
128 font.setBold(true);
129 textLabel->setFont(font);
130 textLabel->setEnabled(enableCheckBox->isChecked());
131
132 // 4: shortcut
133 shortcutLabel = new QLabel(QStringLiteral("(W, %1)").arg(index + 1), this);
134 shortcutLabel->setToolTip(tr("Shortcut to activate this workbench"));
135 shortcutLabel->setEnabled(enableCheckBox->isChecked());
136 shortcutLabel->setVisible(index < 9);
137
138 auto subLayout = new QHBoxLayout(subWidget);
139 subLayout->addWidget(iconLabel);
140 subLayout->addWidget(textLabel);
141 subLayout->addWidget(shortcutLabel);
142 subLayout->setAlignment(Qt::AlignLeft);
143 subLayout->setContentsMargins(5, 0, 0, 5);
144 subWidget->setMinimumSize(250, 0);
145 subWidget->setAttribute(Qt::WA_TranslucentBackground);
146
147 // 5: Autoloaded checkBox.
148 autoloadCheckBox = new QCheckBox(this);
149 autoloadCheckBox->setText(tr("Auto-load"));
150 autoloadCheckBox->setToolTip(tr("Loads %1 automatically when FreeCAD starts").arg(wbDisplayName));
151 autoloadCheckBox->setEnabled(enableCheckBox->isChecked());
152
153 if (startupWb) { // Figure out whether to check and/or disable this checkBox:
154 autoloadCheckBox->setChecked(true);
155 autoloadCheckBox->setEnabled(false);
156 autoloadCheckBox->setToolTip(tr("This is the current startup module, and must be autoloaded."));
157 }
158 else if (autoLoad) {
159 autoloadCheckBox->setChecked(true);
160 }
161
162 // 6: Load button/loaded indicator
163 loadLabel = new QLabel(tr("Loaded"), this);
164 loadLabel->setAlignment(Qt::AlignCenter);
165 loadLabel->setEnabled(enableCheckBox->isChecked());
166 loadButton = new QPushButton(tr("Load"), this);
167 loadButton->setToolTip(
168 tr("To preserve resources, FreeCAD does not load workbenches until they are used. Loading "
169 "them may provide access to additional preferences related to their functionality.")
170 );
171 loadButton->setEnabled(enableCheckBox->isChecked());
172 connect(loadButton, &QPushButton::clicked, this, [this]() { onLoadClicked(); });
173 if (WorkbenchManager::instance()->getWorkbench(wbName.toStdString())) {
174 loadButton->setVisible(false);
175 }
176 else {
177 loadLabel->setVisible(false);
178 }
179
180 auto layout = new QHBoxLayout(this);
181 layout->addWidget(enableCheckBox);
182 layout->addWidget(subWidget);
183 layout->addWidget(autoloadCheckBox);
184 layout->addWidget(loadButton);
185 layout->addWidget(loadLabel);
186 layout->setAlignment(Qt::AlignLeft);
187 layout->setContentsMargins(10, 0, 0, 0);
188 }
189
190 wbListItem::~wbListItem() = default;
191
192 bool wbListItem::isEnabled()
193 {
194 return enableCheckBox->isChecked();
195 }
196
197 bool wbListItem::isAutoLoading()
198 {
199 return autoloadCheckBox->isChecked();
200 }
201
202 void wbListItem::setStartupWb(bool val)
203 {
204 if (val) {
205 autoloadCheckBox->setChecked(true);
206 }
207
208 enableCheckBox->setEnabled(!val);
209 autoloadCheckBox->setEnabled(!val && textLabel->isEnabled());
210 }
211
212 void wbListItem::setShortcutLabel(int index)
213 {
214 shortcutLabel->setText(QStringLiteral("(W, %1)").arg(index + 1));
215 shortcutLabel->setVisible(index < 9);
216 }
217
218 void wbListItem::onLoadClicked()
219 {
220 // activate selected workbench
221 Workbench* originalActiveWB = WorkbenchManager::instance()->active();
222 Application::Instance->activateWorkbench(objectName().toStdString().c_str());
223 Application::Instance->activateWorkbench(originalActiveWB->name().c_str());
224
225 // replace load button with loaded indicator
226 loadButton->setVisible(false);
227 loadLabel->setVisible(true);
228 }
229
230 void wbListItem::onWbToggled(bool checked)
231 {
232 // activate/deactivate the widgets
233 iconLabel->setEnabled(checked);
234 textLabel->setEnabled(checked);
235 shortcutLabel->setEnabled(checked);
236 loadLabel->setEnabled(checked);
237 loadButton->setEnabled(checked);
238 autoloadCheckBox->setEnabled(checked);
239
240 // Reset the start combo items.
241 Q_EMIT wbToggled(objectName(), checked);
242 }
243
244 /* TRANSLATOR Gui::Dialog::DlgSettingsWorkbenchesImp */
245
246 /**
247 * Constructs a DlgSettingsWorkbenchesImp
248 */
249 DlgSettingsWorkbenchesImp::DlgSettingsWorkbenchesImp(QWidget* parent)
250 : PreferencePage(parent)
251 , ui(new Ui_DlgSettingsWorkbenches)
252 {
253 ui->setupUi(this);
254
255 ui->wbList->setDragDropMode(QAbstractItemView::InternalMove);
256 ui->wbList->setSelectionMode(QAbstractItemView::SingleSelection);
257 ui->wbList->viewport()->setAcceptDrops(true);
258 ui->wbList->setDropIndicatorShown(true);
259 ui->wbList->setDragEnabled(true);
260 ui->wbList->setDefaultDropAction(Qt::MoveAction);
261
262 QAction* sortAction = new QAction(tr("Sort Alphabetically"), this);
263 connect(sortAction, &QAction::triggered, this, &DlgSettingsWorkbenchesImp::sortEnabledWorkbenches);
264
265 QMenu* contextMenu = new QMenu(ui->wbList);
266 contextMenu->addAction(sortAction);
267 ui->wbList->setContextMenuPolicy(Qt::CustomContextMenu);
268 connect(
269 ui->wbList,
270 &QListWidget::customContextMenuRequested,
271 this,
272 [this, contextMenu](const QPoint& pos) { contextMenu->exec(ui->wbList->mapToGlobal(pos)); }
273 );
274
275 connect(
276 ui->wbList->model(),
277 &QAbstractItemModel::rowsMoved,
278 this,
279 &DlgSettingsWorkbenchesImp::wbItemMoved
280 );
281 connect(
282 ui->AutoloadModuleCombo,
283 qOverload<int>(&QComboBox::activated),
284 this,
285 &DlgSettingsWorkbenchesImp::onStartWbChanged
286 );
287 connect(ui->CheckBox_WbByTab, &QCheckBox::toggled, this, &DlgSettingsWorkbenchesImp::onWbByTabToggled);
288 }
289
290 /**
291 * Destroys the object and frees any allocated resources
292 */
293 DlgSettingsWorkbenchesImp::~DlgSettingsWorkbenchesImp() = default;
294
295 void DlgSettingsWorkbenchesImp::saveSettings()
296 {
297 std::ostringstream orderedStr, disabledStr, autoloadStr;
298
299 auto addStrToOss = [](std::string wbName, std::ostringstream& oss) {
300 if (oss.str().find(wbName) == std::string::npos) {
301 if (!oss.str().empty()) {
302 oss << ",";
303 }
304 oss << wbName;
305 }
306 };
307
308 for (int i = 0; i < ui->wbList->count(); i++) {
309 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
310 if (!wbItem) {
311 continue;
312 }
313 std::string wbName = wbItem->objectName().toStdString();
314
315 if (wbItem->isEnabled()) {
316 addStrToOss(wbName, orderedStr);
317 }
318 else {
319 addStrToOss(wbName, disabledStr);
320 }
321
322 if (wbItem->isAutoLoading()) {
323 addStrToOss(wbName, autoloadStr);
324 }
325 }
326
327 if (orderedStr.str().empty()) { // make sure that we have at least one enabled workbench. This
328 // should not be necessary because startup wb cannot be disabled.
329 orderedStr << "NoneWorkbench";
330 }
331 else {
332 if (!disabledStr.str().empty()) {
333 disabledStr << ",";
334 }
335 disabledStr << "NoneWorkbench";
336 }
337
338
339 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
340 "User parameter:BaseApp/Preferences/Workbenches"
341 );
342 hGrp->SetASCII("Ordered", orderedStr.str().c_str());
343 hGrp->SetASCII("Disabled", disabledStr.str().c_str());
344
345 // Update the list of workbenches in the WorkbenchGroup and in the WorkbenchComboBox & workbench
346 // QMenu
347 Application::Instance->signalRefreshWorkbenches();
348
349 App::GetApplication()
350 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
351 ->SetASCII("BackgroundAutoloadModules", autoloadStr.str().c_str());
352
353 saveWorkbenchSelector();
354
355 int index = ui->AutoloadModuleCombo->currentIndex();
356 QVariant data = ui->AutoloadModuleCombo->itemData(index);
357 QString startWbName = data.toString();
358 App::GetApplication()
359 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
360 ->SetASCII("AutoloadModule", startWbName.toLatin1());
361
362 ui->CheckBox_WbByTab->onSave();
363 }
364
365 void DlgSettingsWorkbenchesImp::loadSettings()
366 {
367 loadWorkbenchSelector();
368
369 // There are two different "autoload" settings: the first, in FreeCAD since 2004,
370 // controls the module the user sees first when starting FreeCAD, and defaults to the Start workbench
371 std::string start = App::Application::Config()["StartWorkbench"];
372 _startupModule = App::GetApplication()
373 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
374 ->GetASCII("AutoloadModule", start.c_str());
375
376 // The second autoload setting does a background autoload of any number of other modules
377 std::string autoloadCSV = App::GetApplication()
378 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
379 ->GetASCII("BackgroundAutoloadModules", "");
380
381 // Tokenize the comma-separated list
382 _backgroundAutoloadedModules.clear();
383 std::stringstream stream(autoloadCSV);
384 std::string workbench;
385 while (std::getline(stream, workbench, ',')) {
386 _backgroundAutoloadedModules.push_back(workbench);
387 }
388
389 buildWorkbenchList();
390
391 // We set the startup setting after building the list so that we can put only the enabled wb.
392 setStartWorkbenchComboItems();
393
394 {
395 QSignalBlocker sigblk(ui->CheckBox_WbByTab);
396 ui->CheckBox_WbByTab->onRestore();
397 }
398 }
399
400 void DlgSettingsWorkbenchesImp::resetSettingsToDefaults()
401 {
402 ParameterGrp::handle hGrp;
403 hGrp = App::GetApplication().GetParameterGroupByPath(
404 "User parameter:BaseApp/Preferences/Workbenches"
405 );
406 hGrp->RemoveASCII("Ordered");
407 hGrp->RemoveASCII("Disabled");
408 hGrp->RemoveASCII("WorkbenchSelectorType");
409 hGrp->RemoveASCII("WorkbenchSelectorItem");
410
411 hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
412 hGrp->RemoveASCII("BackgroundAutoloadModules");
413 hGrp->RemoveASCII("AutoloadModule");
414
415 hGrp = App::GetApplication().GetParameterGroupByPath(
416 "User parameter:BaseApp/Preferences/MainWindow"
417 );
418 hGrp->RemoveASCII("WSPosition");
419
420 // finally reset all the parameters associated to Gui::Pref* widgets
421 PreferencePage::resetSettingsToDefaults();
422
423 hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
424 if (ui->CheckBox_WbByTab->isChecked() != hGrp->GetBool("SaveWBbyTab", 0)) {
425 requireRestart();
426 }
427 }
428
429 /**
430 Build the list of unloaded workbenches.
431 */
432 void DlgSettingsWorkbenchesImp::buildWorkbenchList()
433 {
434 QSignalBlocker sigblk(ui->wbList);
435
436 ui->wbList->clear();
437
438 QStringList enabledWbs = getEnabledWorkbenches();
439 QStringList disabledWbs = getDisabledWorkbenches();
440
441 // First we add the enabled wbs in their saved order.
442 for (const auto& wbName : enabledWbs) {
443 addWorkbench(wbName, true);
444 }
445 // Second we add workbenches that are disabled in alphabetical order.
446 for (const auto& wbName : disabledWbs) {
447 if (wbName.toStdString() != "NoneWorkbench") {
448 addWorkbench(wbName, false);
449 }
450 }
451 }
452
453 void DlgSettingsWorkbenchesImp::addWorkbench(const QString& wbName, bool enabled)
454 {
455 const bool isStartupWb = wbName.toStdString() == _startupModule;
456 const bool autoLoad = std::ranges::find(_backgroundAutoloadedModules, wbName.toStdString())
457 != _backgroundAutoloadedModules.end();
458 const auto widget
459 = new wbListItem(wbName, enabled, isStartupWb, autoLoad, ui->wbList->count(), this);
460 connect(widget, &wbListItem::wbToggled, this, &DlgSettingsWorkbenchesImp::wbToggled);
461 const auto wItem = new QListWidgetItem();
462 wItem->setSizeHint(widget->sizeHint());
463 ui->wbList->addItem(wItem);
464 ui->wbList->setItemWidget(wItem, widget);
465 }
466
467 QStringList DlgSettingsWorkbenchesImp::getEnabledWorkbenches()
468 {
469 QStringList disabled_wbs_list = getDisabledWorkbenches();
470 QStringList enabled_wbs_list;
471 QStringList wbs_ordered_list;
472 QString wbs_ordered;
473 ParameterGrp::handle hGrp;
474
475 hGrp = App::GetApplication().GetParameterGroupByPath(
476 "User parameter:BaseApp/Preferences/Workbenches"
477 );
478 wbs_ordered = QString::fromStdString(hGrp->GetASCII("Ordered", ""));
479
480 wbs_ordered_list = wbs_ordered.split(QLatin1String(","), Qt::SkipEmptyParts);
481
482 QStringList workbenches = Application::Instance->workbenches();
483 workbenches.sort();
484
485 // First we add the wb that are ordered.
486 for (auto& wbName : wbs_ordered_list) {
487 if (workbenches.contains(wbName)
488 && !disabled_wbs_list.contains(wbName)) { // Some wb may have been removed
489 enabled_wbs_list.append(wbName);
490 }
491 else {
492 Base::Console().log(
493 "Ignoring unknown %s workbench found in user preferences.\n",
494 wbName.toStdString().c_str()
495 );
496 }
497 }
498
499 // Then we add the wbs that are not ordered and not disabled in alphabetical order
500 for (auto& wbName : workbenches) {
501 if (!enabled_wbs_list.contains(wbName) && !disabled_wbs_list.contains(wbName)) {
502 enabled_wbs_list.append(wbName);
503 }
504 }
505
506 return enabled_wbs_list;
507 }
508
509 QStringList DlgSettingsWorkbenchesImp::getDisabledWorkbenches()
510 {
511 QString disabled_wbs;
512 QStringList unfiltered_disabled_wbs_list;
513 QStringList disabled_wbs_list;
514 ParameterGrp::handle hGrp;
515
516 hGrp = App::GetApplication().GetParameterGroupByPath(
517 "User parameter:BaseApp/Preferences/Workbenches"
518 );
519 disabled_wbs = QString::fromStdString(hGrp->GetASCII(
520 "Disabled",
521 "NoneWorkbench,TestWorkbench,InspectionWorkbench,RobotWorkbench,OpenSCADWorkbench"
522 ));
523
524 unfiltered_disabled_wbs_list = disabled_wbs.split(QLatin1String(","), Qt::SkipEmptyParts);
525
526 QStringList workbenches = Application::Instance->workbenches();
527
528 for (auto& wbName : unfiltered_disabled_wbs_list) {
529 if (workbenches.contains(wbName)) { // Some wb may have been removed
530 disabled_wbs_list.append(wbName);
531 }
532 else {
533 Base::Console().log(
534 "Ignoring unknown %s workbench found in user preferences.\n",
535 wbName.toStdString().c_str()
536 );
537 }
538 }
539
540 disabled_wbs_list.sort();
541
542 return disabled_wbs_list;
543 }
544
545 /**
546 * Sets the strings of the subwidgets using the current language.
547 */
548 void DlgSettingsWorkbenchesImp::changeEvent(QEvent* e)
549 {
550 if (e->type() == QEvent::LanguageChange) {
551 ui->retranslateUi(this);
552 translateWorkbenchSelector();
553 }
554 else {
555 QWidget::changeEvent(e);
556 }
557 }
558
559 void DlgSettingsWorkbenchesImp::saveWorkbenchSelector()
560 {
561 // save workbench selector type
562 ParameterGrp::handle hGrp;
563 hGrp = App::GetApplication().GetParameterGroupByPath(
564 "User parameter:BaseApp/Preferences/Workbenches"
565 );
566 int prevIndex = hGrp->GetInt("WorkbenchSelectorType", 0);
567 int index = ui->WorkbenchSelectorType->currentIndex();
568 if (prevIndex != index) {
569 hGrp->SetInt("WorkbenchSelectorType", index);
570 requireRestart();
571 }
572
573 // save workbench selector items style
574 prevIndex = hGrp->GetInt("WorkbenchSelectorItem", 0);
575 index = ui->WorkbenchSelectorItem->currentIndex();
576 if (prevIndex != index) {
577 hGrp->SetInt("WorkbenchSelectorItem", index);
578 requireRestart();
579 }
580 }
581
582 void DlgSettingsWorkbenchesImp::loadWorkbenchSelector()
583 {
584 // workbench selector type setup
585 ParameterGrp::handle hGrp;
586 hGrp = App::GetApplication().GetParameterGroupByPath(
587 "User parameter:BaseApp/Preferences/Workbenches"
588 );
589 int widgetTypeIndex = hGrp->GetInt("WorkbenchSelectorType", 0);
590 ui->WorkbenchSelectorType->clear();
591 ui->WorkbenchSelectorType->addItem(tr("ComboBox"));
592 ui->WorkbenchSelectorType->addItem(tr("TabBar"));
593 ui->WorkbenchSelectorType->setCurrentIndex(widgetTypeIndex);
594
595 // workbench selector items style
596 int itemStyleIndex = hGrp->GetInt("WorkbenchSelectorItem", 0);
597 ui->WorkbenchSelectorItem->clear();
598 ui->WorkbenchSelectorItem->addItem(tr("Icon and text"));
599 ui->WorkbenchSelectorItem->addItem(tr("Icon"));
600 ui->WorkbenchSelectorItem->addItem(tr("Text"));
601 ui->WorkbenchSelectorItem->setCurrentIndex(itemStyleIndex);
602 }
603
604 void DlgSettingsWorkbenchesImp::translateWorkbenchSelector()
605 {
606 ui->WorkbenchSelectorType->setItemText(0, tr("ComboBox"));
607 ui->WorkbenchSelectorType->setItemText(1, tr("TabBar"));
608
609 ui->WorkbenchSelectorItem->setItemText(0, tr("Icon and text"));
610 ui->WorkbenchSelectorItem->setItemText(1, tr("Icon"));
611 ui->WorkbenchSelectorItem->setItemText(2, tr("Text"));
612 }
613
614 void DlgSettingsWorkbenchesImp::wbToggled(const QString& wbName, bool enabled)
615 {
616 setStartWorkbenchComboItems();
617
618 // reorder the list of items.
619 int wbIndex = 0;
620 for (int i = 0; i < ui->wbList->count(); i++) {
621 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
622 if (wbItem && wbItem->objectName() == wbName) {
623 wbIndex = i;
624 }
625 }
626
627 int destinationIndex = ui->wbList->count();
628
629 for (int i = 0; i < ui->wbList->count(); i++) {
630 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
631 if (wbItem && !wbItem->isEnabled()
632 && (enabled || ((wbItem->objectName()).toStdString() > wbName.toStdString()))) {
633 // If the wb was enabled, then it was in the disabled wbs. So it moves to the row of the
634 // currently first disabled wb If the wb was disabled. Then it goes to the disabled wb
635 // where it belongs alphabetically.
636 destinationIndex = i;
637 break;
638 }
639 }
640 ui->wbList->model()->moveRow(QModelIndex(), wbIndex, QModelIndex(), destinationIndex);
641 }
642
643 void DlgSettingsWorkbenchesImp::setStartWorkbenchComboItems()
644 {
645 ui->AutoloadModuleCombo->clear();
646
647 // fills the combo box with activated workbenches.
648 QStringList enabledWbs;
649 for (int i = 0; i < ui->wbList->count(); i++) {
650 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
651 if (wbItem && wbItem->isEnabled()) {
652 enabledWbs << wbItem->objectName();
653 }
654 }
655
656 QMap<QString, QString> menuText;
657 for (const auto& it : enabledWbs) {
658 QString text = Application::Instance->workbenchMenuText(it);
659 menuText[text] = it;
660 }
661
662 { // add special workbench to selection
663 QPixmap px = Application::Instance->workbenchIcon(QStringLiteral("NoneWorkbench"));
664 QString key = QStringLiteral("<last>");
665 QString value = QStringLiteral("$LastModule");
666 if (px.isNull()) {
667 ui->AutoloadModuleCombo->addItem(key, QVariant(value));
668 }
669 else {
670 ui->AutoloadModuleCombo->addItem(px, key, QVariant(value));
671 }
672 }
673
674 for (QMap<QString, QString>::Iterator it = menuText.begin(); it != menuText.end(); ++it) {
675 QPixmap px = Application::Instance->workbenchIcon(it.value());
676 if (px.isNull()) {
677 ui->AutoloadModuleCombo->addItem(it.key(), QVariant(it.value()));
678 }
679 else {
680 ui->AutoloadModuleCombo->addItem(px, it.key(), QVariant(it.value()));
681 }
682 }
683
684 ui->AutoloadModuleCombo->setCurrentIndex(
685 ui->AutoloadModuleCombo->findData(QString::fromStdString(_startupModule))
686 );
687 }
688
689 void DlgSettingsWorkbenchesImp::wbItemMoved()
690 {
691 for (int i = 0; i < ui->wbList->count(); i++) {
692 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
693 if (wbItem) {
694 wbItem->setShortcutLabel(i);
695 }
696 }
697 }
698
699 void DlgSettingsWorkbenchesImp::onStartWbChanged(int index)
700 {
701 // Update _startupModule
702 QVariant data = ui->AutoloadModuleCombo->itemData(index);
703 QString wbName = data.toString();
704 _startupModule = wbName.toStdString();
705
706 // Change wb that user can't deactivate.
707 for (int i = 0; i < ui->wbList->count(); i++) {
708 wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
709 if (wbItem) {
710 wbItem->setStartupWb(wbItem->objectName() == wbName);
711 }
712 }
713 }
714
715 void DlgSettingsWorkbenchesImp::onWbByTabToggled(bool val)
716 {
717 Q_UNUSED(val);
718 requireRestart();
719 }
720
721 void DlgSettingsWorkbenchesImp::sortEnabledWorkbenches()
722 {
723 ParameterGrp::handle hGrp;
724
725 hGrp = App::GetApplication().GetParameterGroupByPath(
726 "User parameter:BaseApp/Preferences/Workbenches"
727 );
728 hGrp->SetASCII("Ordered", "");
729
730 buildWorkbenchList();
731 }
732 #include "moc_DlgSettingsWorkbenchesImp.cpp"
733 #include "DlgSettingsWorkbenchesImp.moc"