FreeCAD

Форк
0
/
DlgToolbarsImp.cpp 
885 строк · 34.4 Кб
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 "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <QHeaderView>
26
#include <QInputDialog>
27
#include <QMenu>
28
#include <QMessageBox>
29
#include <QToolBar>
30
#include <QToolButton>
31
#endif
32

33
#include "DlgToolbarsImp.h"
34
#include "DlgKeyboardImp.h"
35
#include "ui_DlgToolbars.h"
36
#include "Application.h"
37
#include "BitmapFactory.h"
38
#include "Command.h"
39
#include "Action.h"
40
#include "ToolBarManager.h"
41
#include "MainWindow.h"
42
#include "ToolBarManager.h"
43
#include "Widgets.h"
44
#include "Workbench.h"
45
#include "WorkbenchManager.h"
46

47

48
using namespace Gui::Dialog;
49

50
/* TRANSLATOR Gui::Dialog::DlgCustomToolbars */
51

52
/**
53
 *  Constructs a DlgCustomToolbars which is a child of 'parent', with the
54
 *  name 'name' and widget flags set to 'f'
55
 *
56
 *  The dialog will by default be modeless, unless you set 'modal' to
57
 *  true to construct a modal dialog.
58
 */
59
DlgCustomToolbars::DlgCustomToolbars(DlgCustomToolbars::Type t, QWidget* parent)
60
    : CustomizeActionPage(parent)
61
    , ui(new Ui_DlgCustomToolbars)
62
    , type(t)
63
{
64
    ui->setupUi(this);
65
    setupConnections();
66

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"));
71

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));
76

77
    conn = DlgCustomKeyboardImp::initCommandWidgets(ui->commandTreeWidget,
78
                                                    sepItem,
79
                                                    ui->categoryBox,
80
                                                    ui->editCommand);
81

82
    // fills the combo box with all available workbenches
83
    QStringList workbenches = Application::Instance->workbenches();
84
    workbenches.sort();
85
    int index = 1;
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>")) {
92
            if (px.isNull()) {
93
                ui->workbenchBox->addItem(mt);
94
            }
95
            else {
96
                ui->workbenchBox->addItem(px, mt);
97
            }
98
            ui->workbenchBox->setItemData(index, QVariant(workbench), Qt::UserRole);
99
            index++;
100
        }
101
    }
102

103
    QStringList labels;
104
    labels << tr("Command");
105
    ui->toolbarTreeWidget->setHeaderLabels(labels);
106
    ui->toolbarTreeWidget->header()->hide();
107

108
    Workbench* w = WorkbenchManager::instance()->active();
109
    if (w) {
110
        QString name = QString::fromLatin1(w->name().c_str());
111
        int index = ui->workbenchBox->findData(name);
112
        ui->workbenchBox->setCurrentIndex(index);
113
    }
114
    onWorkbenchBoxActivated(ui->workbenchBox->currentIndex());
115
}
116

117
/** Destroys the object and frees any allocated resources */
118
DlgCustomToolbars::~DlgCustomToolbars() = default;
119

120
void DlgCustomToolbars::setupConnections()
121
{
122
    // clang-format off
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);
139
    // clang-format on
140
}
141

142
void DlgCustomToolbars::addCustomToolbar(const QString&)
143
{}
144

145
void DlgCustomToolbars::removeCustomToolbar(const QString&)
146
{}
147

148
void DlgCustomToolbars::renameCustomToolbar(const QString&, const QString&)
149
{}
150

151
void DlgCustomToolbars::addCustomCommand(const QString&, const QByteArray&)
152
{}
153

154
void DlgCustomToolbars::removeCustomCommand(const QString&, const QByteArray&)
155
{}
156

157
void DlgCustomToolbars::moveUpCustomCommand(const QString&, const QByteArray&)
158
{}
159

160
void DlgCustomToolbars::moveDownCustomCommand(const QString&, const QByteArray&)
161
{}
162

163
void DlgCustomToolbars::hideEvent(QHideEvent* event)
164
{
165
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
166
    QString workbench = data.toString();
167
    exportCustomToolbars(workbench.toLatin1());
168

169
    CustomizeActionPage::hideEvent(event);
170
}
171

172
void DlgCustomToolbars::onActivateCategoryBox()
173
{}
174

175
void DlgCustomToolbars::onWorkbenchBoxActivated(int index)
176
{
177
    QVariant data = ui->workbenchBox->itemData(index, Qt::UserRole);
178
    QString workbench = data.toString();
179
    ui->toolbarTreeWidget->clear();
180

181
    QByteArray workbenchname = workbench.toLatin1();
182
    importCustomToolbars(workbenchname);
183
}
184

185
void DlgCustomToolbars::importCustomToolbars(const QByteArray& name)
186
{
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())) {
191
        return;
192
    }
193
    hGrp = hGrp->GetGroup(name.constData());
194
    if (!hGrp->HasGroup(subgroup)) {
195
        return;
196
    }
197
    hGrp = hGrp->GetGroup(subgroup);
198
    std::string separator = "Separator";
199

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));
207

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
213
            // one.
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));
219
            }
220
            else if (it2.first == "Name") {
221
                QString toolbarName = QString::fromUtf8(it2.second.c_str());
222
                toplevel->setText(0, toolbarName);
223
            }
224
            else {
225
                Command* pCmd = rMgr.getCommandByName(it2.first.c_str());
226
                if (pCmd) {
227
                    // command name
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()));
234
                    }
235
                    item->setSizeHint(0, QSize(32, 32));
236
                }
237
                else {
238
                    // If corresponding module is not yet loaded do not lose the entry
239
                    auto item = new QTreeWidgetItem(toplevel);
240
                    item->setText(
241
                        0,
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));
246
                }
247
            }
248
        }
249
    }
250
}
251

252
void DlgCustomToolbars::exportCustomToolbars(const QByteArray& workbench)
253
{
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);
258
    hGrp->Clear();
259

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);
268

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
271
        // one.
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);
278
                suffixSeparator++;
279
                hToolGrp->SetASCII(key, commandName);
280
            }
281
            else {
282
                Command* pCmd = rMgr.getCommandByName(commandName);
283
                if (pCmd) {
284
                    hToolGrp->SetASCII(pCmd->getName(), pCmd->getAppModuleName());
285
                }
286
                else {
287
                    QByteArray moduleName = child->data(0, Qt::WhatsThisPropertyRole).toByteArray();
288
                    hToolGrp->SetASCII(commandName, moduleName);
289
                }
290
            }
291
        }
292
    }
293
}
294

295
/** Adds a new action */
296
void DlgCustomToolbars::onMoveActionRightButtonClicked()
297
{
298
    QTreeWidgetItem* item = ui->commandTreeWidget->currentItem();
299
    if (item) {
300
        QTreeWidgetItem* current = ui->toolbarTreeWidget->currentItem();
301
        if (!current) {
302
            current = ui->toolbarTreeWidget->topLevelItem(0);
303
        }
304
        else if (current->parent()) {
305
            current = current->parent();
306
        }
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);
315
        }
316
    }
317

318
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
319
    QString workbench = data.toString();
320
    exportCustomToolbars(workbench.toLatin1());
321
}
322

323
/** Removes an action */
324
void DlgCustomToolbars::onMoveActionLeftButtonClicked()
325
{
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);
331

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") {
338
            int countSep = 1;
339
            for (int i = 0; i < index - 1; i++) {
340
                QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
341
                if (d == "Separator") {
342
                    countSep++;
343
                }
344
            }
345

346
            data += QByteArray::number(countSep);
347
        }
348
        removeCustomCommand(parent->text(0), data);
349
        delete item;
350
    }
351

352
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
353
    QString workbench = data.toString();
354
    exportCustomToolbars(workbench.toLatin1());
355
}
356

357
/** Moves up an action */
358
void DlgCustomToolbars::onMoveActionUpButtonClicked()
359
{
360
    QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
361
    if (item && item->parent() && item->isSelected()) {
362
        QTreeWidgetItem* parent = item->parent();
363
        int index = parent->indexOfChild(item);
364
        if (index > 0) {
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") {
371
                int countSep = 1;
372
                for (int i = 0; i < index; i++) {
373
                    QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
374
                    if (d == "Separator") {
375
                        countSep++;
376
                    }
377
                }
378

379
                data += QByteArray::number(countSep);
380
            }
381

382
            parent->takeChild(index);
383
            parent->insertChild(index - 1, item);
384
            ui->toolbarTreeWidget->setCurrentItem(item);
385

386
            moveUpCustomCommand(parent->text(0), data);
387
        }
388
    }
389

390
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
391
    QString workbench = data.toString();
392
    exportCustomToolbars(workbench.toLatin1());
393
}
394

395
/** Moves down an action */
396
void DlgCustomToolbars::onMoveActionDownButtonClicked()
397
{
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") {
409
                int countSep = 1;
410
                for (int i = 0; i < index; i++) {
411
                    QByteArray d = parent->child(i)->data(0, Qt::UserRole).toByteArray();
412
                    if (d == "Separator") {
413
                        countSep++;
414
                    }
415
                }
416

417
                data += QByteArray::number(countSep);
418
            }
419

420
            parent->takeChild(index);
421
            parent->insertChild(index + 1, item);
422
            ui->toolbarTreeWidget->setCurrentItem(item);
423

424
            moveDownCustomCommand(parent->text(0), data);
425
        }
426
    }
427

428
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
429
    QString workbench = data.toString();
430
    exportCustomToolbars(workbench.toLatin1());
431
}
432

433
void DlgCustomToolbars::onNewButtonClicked()
434
{
435
    bool ok;
436
    QString text =
437
        QString::fromLatin1("Custom%1").arg(ui->toolbarTreeWidget->topLevelItemCount() + 1);
438
    text = QInputDialog::getText(this,
439
                                 tr("New toolbar"),
440
                                 tr("Toolbar name:"),
441
                                 QLineEdit::Normal,
442
                                 text,
443
                                 &ok,
444
                                 Qt::MSWindowsFixedSizeDialogHint);
445
    if (ok) {
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));
454
                return;
455
            }
456
        }
457

458
        auto item = new QTreeWidgetItem(ui->toolbarTreeWidget);
459
        item->setText(0, text);
460
        item->setCheckState(0, Qt::Checked);
461
        item->setExpanded(true);
462

463
        QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
464
        QString workbench = data.toString();
465
        exportCustomToolbars(workbench.toLatin1());
466
        addCustomToolbar(text);
467
    }
468
}
469

470
void DlgCustomToolbars::onDeleteButtonClicked()
471
{
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));
477
        delete item;
478
    }
479

480
    QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
481
    QString workbench = data.toString();
482
    exportCustomToolbars(workbench.toLatin1());
483
}
484

485
void DlgCustomToolbars::onRenameButtonClicked()
486
{
487
    bool renamed = false;
488
    QTreeWidgetItem* item = ui->toolbarTreeWidget->currentItem();
489
    if (item && !item->parent() && item->isSelected()) {
490
        bool ok;
491
        QString old_text = item->text(0);
492
        QString text = QInputDialog::getText(this,
493
                                             tr("Rename toolbar"),
494
                                             tr("Toolbar name:"),
495
                                             QLineEdit::Normal,
496
                                             old_text,
497
                                             &ok,
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));
508
                    return;
509
                }
510
            }
511

512
            item->setText(0, text);
513
            renameCustomToolbar(old_text, text);
514
            renamed = true;
515
        }
516
    }
517

518
    if (renamed) {
519
        QVariant data = ui->workbenchBox->itemData(ui->workbenchBox->currentIndex(), Qt::UserRole);
520
        QString workbench = data.toString();
521
        exportCustomToolbars(workbench.toLatin1());
522
    }
523
}
524

525
void DlgCustomToolbars::onAddMacroAction(const QByteArray&)
526
{}
527

528
void DlgCustomToolbars::onRemoveMacroAction(const QByteArray&)
529
{}
530

531
void DlgCustomToolbars::onModifyMacroAction(const QByteArray& macro)
532
{
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);
538
        // the right side
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()));
549
                    }
550
                }
551
            }
552
        }
553
        ui->categoryBox->activated(ui->categoryBox->currentIndex());
554
    }
555
}
556

557
void DlgCustomToolbars::changeEvent(QEvent* e)
558
{
559
    if (e->type() == QEvent::LanguageChange) {
560
        ui->retranslateUi(this);
561
        int count = ui->categoryBox->count();
562

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);
570
            }
571
        }
572
        ui->categoryBox->activated(ui->categoryBox->currentIndex());
573
    }
574
    else if (e->type() == QEvent::StyleChange) {
575
        ui->categoryBox->activated(ui->categoryBox->currentIndex());
576
    }
577

578
    QWidget::changeEvent(e);
579
}
580

581
// -------------------------------------------------------------
582

583
/* TRANSLATOR Gui::Dialog::DlgCustomToolbarsImp */
584

585
/**
586
 *  Constructs a DlgCustomToolbarsImp which is a child of 'parent', with the
587
 *  name 'name' and widget flags set to 'f'
588
 *
589
 *  The dialog will by default be modeless, unless you set 'modal' to
590
 *  true to construct a modal dialog.
591
 */
592
DlgCustomToolbarsImp::DlgCustomToolbarsImp(QWidget* parent)
593
    : DlgCustomToolbars(DlgCustomToolbars::Toolbar, parent)
594
{}
595

596
/** Destroys the object and frees any allocated resources */
597
DlgCustomToolbarsImp::~DlgCustomToolbarsImp() = default;
598

599
void DlgCustomToolbarsImp::addCustomToolbar(const QString& name)
600
{
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);
606
    }
607
}
608

609
void DlgCustomToolbarsImp::removeCustomToolbar(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
        QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>(name);
615
        if (bars.size() != 1) {
616
            return;
617
        }
618

619
        QToolBar* tb = bars.front();
620
        getMainWindow()->removeToolBar(tb);
621
        delete tb;
622
    }
623
}
624

625
void DlgCustomToolbarsImp::renameCustomToolbar(const QString& old_name, const QString& new_name)
626
{
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) {
632
            return;
633
        }
634

635
        QToolBar* tb = bars.front();
636
        tb->setObjectName(new_name);
637
        tb->setWindowTitle(new_name);
638
    }
639
}
640

641
QList<QAction*> DlgCustomToolbarsImp::getActionGroup(QAction* action)
642
{
643
    QList<QAction*> group;
644
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
645
    QList<QWidget*> widgets = action->associatedWidgets();
646
#else
647
    QList<QObject*> widgets = action->associatedObjects();
648
#endif
649
    for (const auto& widget : widgets) {
650
        auto tb = qobject_cast<QToolButton*>(widget);
651
        if (tb) {
652
            QMenu* menu = tb->menu();
653
            if (menu) {
654
                group = menu->actions();
655
                break;
656
            }
657
        }
658
    }
659
    return group;
660
}
661

662
void DlgCustomToolbarsImp::setActionGroup(QAction* action, const QList<QAction*>& group)
663
{
664
    // See also ActionGroup::addTo()
665
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
666
    QList<QWidget*> widgets = action->associatedWidgets();
667
#else
668
    QList<QObject*> widgets = action->associatedObjects();
669
#endif
670
    for (const auto& widget : widgets) {
671
        auto tb = qobject_cast<QToolButton*>(widget);
672
        if (tb) {
673
            QMenu* menu = tb->menu();
674
            if (!menu) {
675
                tb->setPopupMode(QToolButton::MenuButtonPopup);
676
                tb->setObjectName(QString::fromLatin1("qt_toolbutton_menubutton"));
677
                auto menu = new QMenu(tb);
678
                menu->addActions(group);
679
                tb->setMenu(menu);
680
            }
681
        }
682
    }
683
}
684

685
void DlgCustomToolbarsImp::addCustomCommand(const QString& name, const QByteArray& cmd)
686
{
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) {
692
            return;
693
        }
694

695
        if (cmd == "Separator") {
696
            QAction* action = bars.front()->addSeparator();
697
            action->setData(QByteArray("Separator"));
698
        }
699
        else {
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);
708
                }
709
            }
710
        }
711
    }
712
}
713

714
void DlgCustomToolbarsImp::removeCustomCommand(const QString& name, const QByteArray& userdata)
715
{
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) {
721
            return;
722
        }
723

724
        QByteArray cmd = userdata;
725
        int numSep = 0, indexSep = 0;
726
        if (cmd.startsWith("Separator")) {
727
            numSep = cmd.mid(9).toInt();
728
            cmd = "Separator";
729
        }
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
734
                if (numSep > 0) {
735
                    if (++indexSep < numSep) {
736
                        continue;
737
                    }
738
                }
739
                bars.front()->removeAction(action);
740
                break;
741
            }
742
        }
743
    }
744
}
745

746
void DlgCustomToolbarsImp::moveUpCustomCommand(const QString& name, const QByteArray& userdata)
747
{
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) {
753
            return;
754
        }
755

756
        QByteArray cmd = userdata;
757
        int numSep = 0, indexSep = 0;
758
        if (cmd.startsWith("Separator")) {
759
            numSep = cmd.mid(9).toInt();
760
            cmd = "Separator";
761
        }
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
767
                if (numSep > 0) {
768
                    if (++indexSep < numSep) {
769
                        before = action;
770
                        continue;
771
                    }
772
                }
773
                if (before) {
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);
779
                    }
780
                    break;
781
                }
782
            }
783

784
            before = action;
785
        }
786
    }
787
}
788

789
void DlgCustomToolbarsImp::moveDownCustomCommand(const QString& name, const QByteArray& userdata)
790
{
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) {
796
            return;
797
        }
798

799
        QByteArray cmd = userdata;
800
        int numSep = 0, indexSep = 0;
801
        if (cmd.startsWith("Separator")) {
802
            numSep = cmd.mid(9).toInt();
803
            cmd = "Separator";
804
        }
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
809
                if (numSep > 0) {
810
                    if (++indexSep < numSep) {
811
                        continue;
812
                    }
813
                }
814
                QAction* act = *it;
815
                if (*it == actions.back()) {
816
                    break;  // we're already on the last element
817
                }
818
                ++it;
819
                // second last item
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);
826
                    }
827
                    break;
828
                }
829
                ++it;
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);
835
                }
836
                break;
837
            }
838
        }
839
    }
840
}
841

842
void DlgCustomToolbarsImp::showEvent(QShowEvent* event)
843
{
844
    Q_UNUSED(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.
847
    if (firstShow) {
848
        ui->categoryBox->activated(ui->categoryBox->currentIndex());
849
        firstShow = false;
850
    }
851
}
852

853
void DlgCustomToolbarsImp::changeEvent(QEvent* e)
854
{
855
    DlgCustomToolbars::changeEvent(e);
856
}
857

858

859
/* TRANSLATOR Gui::Dialog::DlgCustomToolBoxbarsImp */
860

861
/**
862
 *  Constructs a DlgCustomToolBoxbarsImp which is a child of 'parent', with the
863
 *  name 'name' and widget flags set to 'f'
864
 *
865
 *  The dialog will by default be modeless, unless you set 'modal' to
866
 *  true to construct a modal dialog.
867
 */
868
DlgCustomToolBoxbarsImp::DlgCustomToolBoxbarsImp(QWidget* parent)
869
    : DlgCustomToolbars(DlgCustomToolbars::Toolboxbar, parent)
870
{
871
    setWindowTitle(tr("Toolbox bars"));
872
}
873

874
/** Destroys the object and frees any allocated resources */
875
DlgCustomToolBoxbarsImp::~DlgCustomToolBoxbarsImp() = default;
876

877
void DlgCustomToolBoxbarsImp::changeEvent(QEvent* e)
878
{
879
    if (e->type() == QEvent::LanguageChange) {
880
        setWindowTitle(tr("Toolbox bars"));
881
    }
882
    DlgCustomToolbars::changeEvent(e);
883
}
884

885
#include "moc_DlgToolbarsImp.cpp"
886

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.