File size: 25,062 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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
443
444
445
446
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// SPDX-License-Identifier: LGPL-2.1-or-later

/****************************************************************************
 *   Copyright (c) 2020 Chris Hennes (chennes@pioneerlibrarysystem.org)     *
 *   Copyright (c) 2023 FreeCAD Project Association                         *
 *                                                                          *
 *   This file is part of FreeCAD.                                          *
 *                                                                          *
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
 *   under the terms of the GNU Lesser General Public License as            *
 *   published by the Free Software Foundation, either version 2.1 of the   *
 *   License, or (at your option) any later version.                        *
 *                                                                          *
 *   FreeCAD 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       *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU Lesser General Public       *
 *   License along with FreeCAD. If not, see                                *
 *   <https://www.gnu.org/licenses/>.                                       *
 *                                                                          *
 ***************************************************************************/

#include <QAction>
#include <QCheckBox>
#include <QLabel>
#include <QMenu>
#include <QPushButton>
#include <sstream>

#include <Gui/Application.h>
#include <Gui/Workbench.h>
#include <Gui/WorkbenchManager.h>

#include "DlgSettingsWorkbenchesImp.h"
#include "ui_DlgSettingsWorkbenches.h"


using namespace Gui::Dialog;

namespace Gui::Dialog
{
class wbListItem: public QWidget
{
    Q_OBJECT

public:
    explicit wbListItem(
        const QString& wbName,
        bool enabled,
        bool startupWb,
        bool autoLoad,
        int index,
        QWidget* parent = nullptr
    );
    ~wbListItem() override;

    bool isEnabled();
    bool isAutoLoading();
    void setStartupWb(bool val);

    void setShortcutLabel(int index);

protected Q_SLOTS:
    void onLoadClicked();
    void onWbToggled(bool checked);

Q_SIGNALS:
    void wbToggled(const QString& wbName, bool enabled);

private:
    QCheckBox* enableCheckBox;
    QCheckBox* autoloadCheckBox;
    QLabel* iconLabel;
    QLabel* textLabel;
    QLabel* shortcutLabel;
    QLabel* loadLabel;
    QPushButton* loadButton;
};
}  // namespace Gui::Dialog

wbListItem::wbListItem(
    const QString& wbName,
    bool enabled,
    bool startupWb,
    bool autoLoad,
    int index,
    QWidget* parent
)
    : QWidget(parent)
{
    this->setObjectName(wbName);

    auto wbTooltip = Application::Instance->workbenchToolTip(wbName);
    auto wbDisplayName = Application::Instance->workbenchMenuText(wbName);

    // 1: Enable checkbox
    enableCheckBox = new QCheckBox(this);
    enableCheckBox->setToolTip(
        tr("Toggles the visibility of %1 in the available workbenches").arg(wbDisplayName)
    );
    enableCheckBox->setChecked(enabled);
    if (startupWb) {
        enableCheckBox->setChecked(true);
        enableCheckBox->setEnabled(false);
        enableCheckBox->setToolTip(tr("This is the current startup module, and must be enabled"));
    }
    connect(enableCheckBox, &QCheckBox::toggled, this, [this](bool checked) { onWbToggled(checked); });

    QWidget* subWidget = new QWidget(this);
    // 2: Workbench Icon
    auto wbIcon = Application::Instance->workbenchIcon(wbName);
    iconLabel = new QLabel(wbDisplayName, this);
    iconLabel->setPixmap(wbIcon.scaled(
        QSize(20, 20),
        Qt::AspectRatioMode::KeepAspectRatio,
        Qt::TransformationMode::SmoothTransformation
    ));
    iconLabel->setToolTip(wbTooltip);
    iconLabel->setContentsMargins(5, 0, 0, 5);  // Left, top, right, bottom
    iconLabel->setEnabled(enableCheckBox->isChecked());

    // 3: Workbench Display Name
    textLabel = new QLabel(wbDisplayName, this);
    textLabel->setToolTip(wbTooltip);
    QFont font = textLabel->font();
    font.setBold(true);
    textLabel->setFont(font);
    textLabel->setEnabled(enableCheckBox->isChecked());

    // 4: shortcut
    shortcutLabel = new QLabel(QStringLiteral("(W, %1)").arg(index + 1), this);
    shortcutLabel->setToolTip(tr("Shortcut to activate this workbench"));
    shortcutLabel->setEnabled(enableCheckBox->isChecked());
    shortcutLabel->setVisible(index < 9);

    auto subLayout = new QHBoxLayout(subWidget);
    subLayout->addWidget(iconLabel);
    subLayout->addWidget(textLabel);
    subLayout->addWidget(shortcutLabel);
    subLayout->setAlignment(Qt::AlignLeft);
    subLayout->setContentsMargins(5, 0, 0, 5);
    subWidget->setMinimumSize(250, 0);
    subWidget->setAttribute(Qt::WA_TranslucentBackground);

    // 5: Autoloaded checkBox.
    autoloadCheckBox = new QCheckBox(this);
    autoloadCheckBox->setText(tr("Auto-load"));
    autoloadCheckBox->setToolTip(tr("Loads %1 automatically when FreeCAD starts").arg(wbDisplayName));
    autoloadCheckBox->setEnabled(enableCheckBox->isChecked());

    if (startupWb) {  // Figure out whether to check and/or disable this checkBox:
        autoloadCheckBox->setChecked(true);
        autoloadCheckBox->setEnabled(false);
        autoloadCheckBox->setToolTip(tr("This is the current startup module, and must be autoloaded."));
    }
    else if (autoLoad) {
        autoloadCheckBox->setChecked(true);
    }

    // 6: Load button/loaded indicator
    loadLabel = new QLabel(tr("Loaded"), this);
    loadLabel->setAlignment(Qt::AlignCenter);
    loadLabel->setEnabled(enableCheckBox->isChecked());
    loadButton = new QPushButton(tr("Load"), this);
    loadButton->setToolTip(
        tr("To preserve resources, FreeCAD does not load workbenches until they are used. Loading "
           "them may provide access to additional preferences related to their functionality.")
    );
    loadButton->setEnabled(enableCheckBox->isChecked());
    connect(loadButton, &QPushButton::clicked, this, [this]() { onLoadClicked(); });
    if (WorkbenchManager::instance()->getWorkbench(wbName.toStdString())) {
        loadButton->setVisible(false);
    }
    else {
        loadLabel->setVisible(false);
    }

    auto layout = new QHBoxLayout(this);
    layout->addWidget(enableCheckBox);
    layout->addWidget(subWidget);
    layout->addWidget(autoloadCheckBox);
    layout->addWidget(loadButton);
    layout->addWidget(loadLabel);
    layout->setAlignment(Qt::AlignLeft);
    layout->setContentsMargins(10, 0, 0, 0);
}

wbListItem::~wbListItem() = default;

bool wbListItem::isEnabled()
{
    return enableCheckBox->isChecked();
}

bool wbListItem::isAutoLoading()
{
    return autoloadCheckBox->isChecked();
}

void wbListItem::setStartupWb(bool val)
{
    if (val) {
        autoloadCheckBox->setChecked(true);
    }

    enableCheckBox->setEnabled(!val);
    autoloadCheckBox->setEnabled(!val && textLabel->isEnabled());
}

void wbListItem::setShortcutLabel(int index)
{
    shortcutLabel->setText(QStringLiteral("(W, %1)").arg(index + 1));
    shortcutLabel->setVisible(index < 9);
}

void wbListItem::onLoadClicked()
{
    // activate selected workbench
    Workbench* originalActiveWB = WorkbenchManager::instance()->active();
    Application::Instance->activateWorkbench(objectName().toStdString().c_str());
    Application::Instance->activateWorkbench(originalActiveWB->name().c_str());

    // replace load button with loaded indicator
    loadButton->setVisible(false);
    loadLabel->setVisible(true);
}

void wbListItem::onWbToggled(bool checked)
{
    // activate/deactivate the widgets
    iconLabel->setEnabled(checked);
    textLabel->setEnabled(checked);
    shortcutLabel->setEnabled(checked);
    loadLabel->setEnabled(checked);
    loadButton->setEnabled(checked);
    autoloadCheckBox->setEnabled(checked);

    // Reset the start combo items.
    Q_EMIT wbToggled(objectName(), checked);
}

/* TRANSLATOR Gui::Dialog::DlgSettingsWorkbenchesImp */

/**
 *  Constructs a DlgSettingsWorkbenchesImp
 */
DlgSettingsWorkbenchesImp::DlgSettingsWorkbenchesImp(QWidget* parent)
    : PreferencePage(parent)
    , ui(new Ui_DlgSettingsWorkbenches)
{
    ui->setupUi(this);

    ui->wbList->setDragDropMode(QAbstractItemView::InternalMove);
    ui->wbList->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->wbList->viewport()->setAcceptDrops(true);
    ui->wbList->setDropIndicatorShown(true);
    ui->wbList->setDragEnabled(true);
    ui->wbList->setDefaultDropAction(Qt::MoveAction);

    QAction* sortAction = new QAction(tr("Sort Alphabetically"), this);
    connect(sortAction, &QAction::triggered, this, &DlgSettingsWorkbenchesImp::sortEnabledWorkbenches);

    QMenu* contextMenu = new QMenu(ui->wbList);
    contextMenu->addAction(sortAction);
    ui->wbList->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(
        ui->wbList,
        &QListWidget::customContextMenuRequested,
        this,
        [this, contextMenu](const QPoint& pos) { contextMenu->exec(ui->wbList->mapToGlobal(pos)); }
    );

    connect(
        ui->wbList->model(),
        &QAbstractItemModel::rowsMoved,
        this,
        &DlgSettingsWorkbenchesImp::wbItemMoved
    );
    connect(
        ui->AutoloadModuleCombo,
        qOverload<int>(&QComboBox::activated),
        this,
        &DlgSettingsWorkbenchesImp::onStartWbChanged
    );
    connect(ui->CheckBox_WbByTab, &QCheckBox::toggled, this, &DlgSettingsWorkbenchesImp::onWbByTabToggled);
}

/**
 *  Destroys the object and frees any allocated resources
 */
DlgSettingsWorkbenchesImp::~DlgSettingsWorkbenchesImp() = default;

void DlgSettingsWorkbenchesImp::saveSettings()
{
    std::ostringstream orderedStr, disabledStr, autoloadStr;

    auto addStrToOss = [](std::string wbName, std::ostringstream& oss) {
        if (oss.str().find(wbName) == std::string::npos) {
            if (!oss.str().empty()) {
                oss << ",";
            }
            oss << wbName;
        }
    };

    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (!wbItem) {
            continue;
        }
        std::string wbName = wbItem->objectName().toStdString();

        if (wbItem->isEnabled()) {
            addStrToOss(wbName, orderedStr);
        }
        else {
            addStrToOss(wbName, disabledStr);
        }

        if (wbItem->isAutoLoading()) {
            addStrToOss(wbName, autoloadStr);
        }
    }

    if (orderedStr.str().empty()) {  // make sure that we have at least one enabled workbench. This
                                     // should not be necessary because startup wb cannot be disabled.
        orderedStr << "NoneWorkbench";
    }
    else {
        if (!disabledStr.str().empty()) {
            disabledStr << ",";
        }
        disabledStr << "NoneWorkbench";
    }


    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    hGrp->SetASCII("Ordered", orderedStr.str().c_str());
    hGrp->SetASCII("Disabled", disabledStr.str().c_str());

    // Update the list of workbenches in the WorkbenchGroup and in the WorkbenchComboBox & workbench
    // QMenu
    Application::Instance->signalRefreshWorkbenches();

    App::GetApplication()
        .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
        ->SetASCII("BackgroundAutoloadModules", autoloadStr.str().c_str());

    saveWorkbenchSelector();

    int index = ui->AutoloadModuleCombo->currentIndex();
    QVariant data = ui->AutoloadModuleCombo->itemData(index);
    QString startWbName = data.toString();
    App::GetApplication()
        .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
        ->SetASCII("AutoloadModule", startWbName.toLatin1());

    ui->CheckBox_WbByTab->onSave();
}

void DlgSettingsWorkbenchesImp::loadSettings()
{
    loadWorkbenchSelector();

    // There are two different "autoload" settings: the first, in FreeCAD since 2004,
    // controls the module the user sees first when starting FreeCAD, and defaults to the Start workbench
    std::string start = App::Application::Config()["StartWorkbench"];
    _startupModule = App::GetApplication()
                         .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
                         ->GetASCII("AutoloadModule", start.c_str());

    // The second autoload setting does a background autoload of any number of other modules
    std::string autoloadCSV = App::GetApplication()
                                  .GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
                                  ->GetASCII("BackgroundAutoloadModules", "");

    // Tokenize the comma-separated list
    _backgroundAutoloadedModules.clear();
    std::stringstream stream(autoloadCSV);
    std::string workbench;
    while (std::getline(stream, workbench, ',')) {
        _backgroundAutoloadedModules.push_back(workbench);
    }

    buildWorkbenchList();

    // We set the startup setting after building the list so that we can put only the enabled wb.
    setStartWorkbenchComboItems();

    {
        QSignalBlocker sigblk(ui->CheckBox_WbByTab);
        ui->CheckBox_WbByTab->onRestore();
    }
}

void DlgSettingsWorkbenchesImp::resetSettingsToDefaults()
{
    ParameterGrp::handle hGrp;
    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    hGrp->RemoveASCII("Ordered");
    hGrp->RemoveASCII("Disabled");
    hGrp->RemoveASCII("WorkbenchSelectorType");
    hGrp->RemoveASCII("WorkbenchSelectorItem");

    hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
    hGrp->RemoveASCII("BackgroundAutoloadModules");
    hGrp->RemoveASCII("AutoloadModule");

    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/MainWindow"
    );
    hGrp->RemoveASCII("WSPosition");

    // finally reset all the parameters associated to Gui::Pref* widgets
    PreferencePage::resetSettingsToDefaults();

    hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
    if (ui->CheckBox_WbByTab->isChecked() != hGrp->GetBool("SaveWBbyTab", 0)) {
        requireRestart();
    }
}

/**
Build the list of unloaded workbenches.
*/
void DlgSettingsWorkbenchesImp::buildWorkbenchList()
{
    QSignalBlocker sigblk(ui->wbList);

    ui->wbList->clear();

    QStringList enabledWbs = getEnabledWorkbenches();
    QStringList disabledWbs = getDisabledWorkbenches();

    // First we add the enabled wbs in their saved order.
    for (const auto& wbName : enabledWbs) {
        addWorkbench(wbName, true);
    }
    // Second we add workbenches that are disabled in alphabetical order.
    for (const auto& wbName : disabledWbs) {
        if (wbName.toStdString() != "NoneWorkbench") {
            addWorkbench(wbName, false);
        }
    }
}

void DlgSettingsWorkbenchesImp::addWorkbench(const QString& wbName, bool enabled)
{
    const bool isStartupWb = wbName.toStdString() == _startupModule;
    const bool autoLoad = std::ranges::find(_backgroundAutoloadedModules, wbName.toStdString())
        != _backgroundAutoloadedModules.end();
    const auto widget
        = new wbListItem(wbName, enabled, isStartupWb, autoLoad, ui->wbList->count(), this);
    connect(widget, &wbListItem::wbToggled, this, &DlgSettingsWorkbenchesImp::wbToggled);
    const auto wItem = new QListWidgetItem();
    wItem->setSizeHint(widget->sizeHint());
    ui->wbList->addItem(wItem);
    ui->wbList->setItemWidget(wItem, widget);
}

QStringList DlgSettingsWorkbenchesImp::getEnabledWorkbenches()
{
    QStringList disabled_wbs_list = getDisabledWorkbenches();
    QStringList enabled_wbs_list;
    QStringList wbs_ordered_list;
    QString wbs_ordered;
    ParameterGrp::handle hGrp;

    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    wbs_ordered = QString::fromStdString(hGrp->GetASCII("Ordered", ""));

    wbs_ordered_list = wbs_ordered.split(QLatin1String(","), Qt::SkipEmptyParts);

    QStringList workbenches = Application::Instance->workbenches();
    workbenches.sort();

    // First we add the wb that are ordered.
    for (auto& wbName : wbs_ordered_list) {
        if (workbenches.contains(wbName)
            && !disabled_wbs_list.contains(wbName)) {  // Some wb may have been removed
            enabled_wbs_list.append(wbName);
        }
        else {
            Base::Console().log(
                "Ignoring unknown %s workbench found in user preferences.\n",
                wbName.toStdString().c_str()
            );
        }
    }

    // Then we add the wbs that are not ordered and not disabled in alphabetical order
    for (auto& wbName : workbenches) {
        if (!enabled_wbs_list.contains(wbName) && !disabled_wbs_list.contains(wbName)) {
            enabled_wbs_list.append(wbName);
        }
    }

    return enabled_wbs_list;
}

QStringList DlgSettingsWorkbenchesImp::getDisabledWorkbenches()
{
    QString disabled_wbs;
    QStringList unfiltered_disabled_wbs_list;
    QStringList disabled_wbs_list;
    ParameterGrp::handle hGrp;

    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    disabled_wbs = QString::fromStdString(hGrp->GetASCII(
        "Disabled",
        "NoneWorkbench,TestWorkbench,InspectionWorkbench,RobotWorkbench,OpenSCADWorkbench"
    ));

    unfiltered_disabled_wbs_list = disabled_wbs.split(QLatin1String(","), Qt::SkipEmptyParts);

    QStringList workbenches = Application::Instance->workbenches();

    for (auto& wbName : unfiltered_disabled_wbs_list) {
        if (workbenches.contains(wbName)) {  // Some wb may have been removed
            disabled_wbs_list.append(wbName);
        }
        else {
            Base::Console().log(
                "Ignoring unknown %s workbench found in user preferences.\n",
                wbName.toStdString().c_str()
            );
        }
    }

    disabled_wbs_list.sort();

    return disabled_wbs_list;
}

/**
 * Sets the strings of the subwidgets using the current language.
 */
void DlgSettingsWorkbenchesImp::changeEvent(QEvent* e)
{
    if (e->type() == QEvent::LanguageChange) {
        ui->retranslateUi(this);
        translateWorkbenchSelector();
    }
    else {
        QWidget::changeEvent(e);
    }
}

void DlgSettingsWorkbenchesImp::saveWorkbenchSelector()
{
    // save workbench selector type
    ParameterGrp::handle hGrp;
    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    int prevIndex = hGrp->GetInt("WorkbenchSelectorType", 0);
    int index = ui->WorkbenchSelectorType->currentIndex();
    if (prevIndex != index) {
        hGrp->SetInt("WorkbenchSelectorType", index);
        requireRestart();
    }

    // save workbench selector items style
    prevIndex = hGrp->GetInt("WorkbenchSelectorItem", 0);
    index = ui->WorkbenchSelectorItem->currentIndex();
    if (prevIndex != index) {
        hGrp->SetInt("WorkbenchSelectorItem", index);
        requireRestart();
    }
}

void DlgSettingsWorkbenchesImp::loadWorkbenchSelector()
{
    // workbench selector type setup
    ParameterGrp::handle hGrp;
    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    int widgetTypeIndex = hGrp->GetInt("WorkbenchSelectorType", 0);
    ui->WorkbenchSelectorType->clear();
    ui->WorkbenchSelectorType->addItem(tr("ComboBox"));
    ui->WorkbenchSelectorType->addItem(tr("TabBar"));
    ui->WorkbenchSelectorType->setCurrentIndex(widgetTypeIndex);

    // workbench selector items style
    int itemStyleIndex = hGrp->GetInt("WorkbenchSelectorItem", 0);
    ui->WorkbenchSelectorItem->clear();
    ui->WorkbenchSelectorItem->addItem(tr("Icon and text"));
    ui->WorkbenchSelectorItem->addItem(tr("Icon"));
    ui->WorkbenchSelectorItem->addItem(tr("Text"));
    ui->WorkbenchSelectorItem->setCurrentIndex(itemStyleIndex);
}

void DlgSettingsWorkbenchesImp::translateWorkbenchSelector()
{
    ui->WorkbenchSelectorType->setItemText(0, tr("ComboBox"));
    ui->WorkbenchSelectorType->setItemText(1, tr("TabBar"));

    ui->WorkbenchSelectorItem->setItemText(0, tr("Icon and text"));
    ui->WorkbenchSelectorItem->setItemText(1, tr("Icon"));
    ui->WorkbenchSelectorItem->setItemText(2, tr("Text"));
}

void DlgSettingsWorkbenchesImp::wbToggled(const QString& wbName, bool enabled)
{
    setStartWorkbenchComboItems();

    // reorder the list of items.
    int wbIndex = 0;
    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (wbItem && wbItem->objectName() == wbName) {
            wbIndex = i;
        }
    }

    int destinationIndex = ui->wbList->count();

    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (wbItem && !wbItem->isEnabled()
            && (enabled || ((wbItem->objectName()).toStdString() > wbName.toStdString()))) {
            // If the wb was enabled, then it was in the disabled wbs. So it moves to the row of the
            // currently first disabled wb If the wb was disabled. Then it goes to the disabled wb
            // where it belongs alphabetically.
            destinationIndex = i;
            break;
        }
    }
    ui->wbList->model()->moveRow(QModelIndex(), wbIndex, QModelIndex(), destinationIndex);
}

void DlgSettingsWorkbenchesImp::setStartWorkbenchComboItems()
{
    ui->AutoloadModuleCombo->clear();

    // fills the combo box with activated workbenches.
    QStringList enabledWbs;
    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (wbItem && wbItem->isEnabled()) {
            enabledWbs << wbItem->objectName();
        }
    }

    QMap<QString, QString> menuText;
    for (const auto& it : enabledWbs) {
        QString text = Application::Instance->workbenchMenuText(it);
        menuText[text] = it;
    }

    {  // add special workbench to selection
        QPixmap px = Application::Instance->workbenchIcon(QStringLiteral("NoneWorkbench"));
        QString key = QStringLiteral("<last>");
        QString value = QStringLiteral("$LastModule");
        if (px.isNull()) {
            ui->AutoloadModuleCombo->addItem(key, QVariant(value));
        }
        else {
            ui->AutoloadModuleCombo->addItem(px, key, QVariant(value));
        }
    }

    for (QMap<QString, QString>::Iterator it = menuText.begin(); it != menuText.end(); ++it) {
        QPixmap px = Application::Instance->workbenchIcon(it.value());
        if (px.isNull()) {
            ui->AutoloadModuleCombo->addItem(it.key(), QVariant(it.value()));
        }
        else {
            ui->AutoloadModuleCombo->addItem(px, it.key(), QVariant(it.value()));
        }
    }

    ui->AutoloadModuleCombo->setCurrentIndex(
        ui->AutoloadModuleCombo->findData(QString::fromStdString(_startupModule))
    );
}

void DlgSettingsWorkbenchesImp::wbItemMoved()
{
    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (wbItem) {
            wbItem->setShortcutLabel(i);
        }
    }
}

void DlgSettingsWorkbenchesImp::onStartWbChanged(int index)
{
    // Update _startupModule
    QVariant data = ui->AutoloadModuleCombo->itemData(index);
    QString wbName = data.toString();
    _startupModule = wbName.toStdString();

    // Change wb that user can't deactivate.
    for (int i = 0; i < ui->wbList->count(); i++) {
        wbListItem* wbItem = qobject_cast<wbListItem*>(ui->wbList->itemWidget(ui->wbList->item(i)));
        if (wbItem) {
            wbItem->setStartupWb(wbItem->objectName() == wbName);
        }
    }
}

void DlgSettingsWorkbenchesImp::onWbByTabToggled(bool val)
{
    Q_UNUSED(val);
    requireRestart();
}

void DlgSettingsWorkbenchesImp::sortEnabledWorkbenches()
{
    ParameterGrp::handle hGrp;

    hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Workbenches"
    );
    hGrp->SetASCII("Ordered", "");

    buildWorkbenchList();
}
#include "moc_DlgSettingsWorkbenchesImp.cpp"
#include "DlgSettingsWorkbenchesImp.moc"