FreeCAD

Форк
0
/
DlgParameterImp.cpp 
1392 строки · 48.4 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
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 <sstream>
26
#include <QByteArray>
27
#include <QContextMenuEvent>
28
#include <QHeaderView>
29
#include <QInputDialog>
30
#include <QMessageBox>
31
#include <QMenu>
32
#include <QTreeWidget>
33
#endif
34

35
#include <App/Application.h>
36
#include <Base/Exception.h>
37
#include <Base/Parameter.h>
38

39
#include "DlgParameterImp.h"
40
#include "ui_DlgParameter.h"
41
#include "BitmapFactory.h"
42
#include "DlgParameterFind.h"
43
#include "DlgInputDialogImp.h"
44
#include "FileDialog.h"
45
#include "SpinBox.h"
46

47

48
using namespace Gui::Dialog;
49

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

52
/**
53
 *  Constructs a DlgParameterImp 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
DlgParameterImp::DlgParameterImp(QWidget* parent, Qt::WindowFlags fl)
60
    : QDialog(parent, fl | Qt::WindowMinMaxButtonsHint)
61
    , ui(new Ui_DlgParameter)
62
{
63
    ui->setupUi(this);
64
    setupConnections();
65

66
    ui->checkSort->setVisible(false);  // for testing
67

68
    QStringList groupLabels;
69
    groupLabels << tr("Group");
70
    paramGroup = new ParameterGroup(ui->splitter3);
71
    paramGroup->setHeaderLabels(groupLabels);
72
    paramGroup->setRootIsDecorated(false);
73
    paramGroup->setSortingEnabled(true);
74
    paramGroup->sortByColumn(0, Qt::AscendingOrder);
75
    paramGroup->header()->setProperty("showSortIndicator", QVariant(true));
76

77
    QStringList valueLabels;
78
    valueLabels << tr("Name") << tr("Type") << tr("Value");
79
    paramValue = new ParameterValue(ui->splitter3);
80
    paramValue->setHeaderLabels(valueLabels);
81
    paramValue->setRootIsDecorated(false);
82
    paramValue->header()->setSectionResizeMode(0, QHeaderView::Stretch);
83
    paramValue->setSortingEnabled(true);
84
    paramValue->sortByColumn(0, Qt::AscendingOrder);
85
    paramValue->header()->setProperty("showSortIndicator", QVariant(true));
86

87
    QSizePolicy policy = paramValue->sizePolicy();
88
    policy.setHorizontalStretch(3);
89
    paramValue->setSizePolicy(policy);
90

91
#if 0  // This is needed for Qt's lupdate
92
    qApp->translate( "Gui::Dialog::DlgParameterImp", "System parameter" );
93
    qApp->translate( "Gui::Dialog::DlgParameterImp", "User parameter" );
94
#endif
95

96
    ParameterManager* sys = App::GetApplication().GetParameterSet("System parameter");
97
    const auto& rcList = App::GetApplication().GetParameterSetList();
98
    for (const auto& it : rcList) {
99
        if (it.second != sys) {  // for now ignore system parameters because they are nowhere used
100
            ui->parameterSet->addItem(tr(it.first.c_str()), QVariant(QByteArray(it.first.c_str())));
101
        }
102
    }
103

104
    QByteArray cStr("User parameter");
105
    ui->parameterSet->setCurrentIndex(ui->parameterSet->findData(cStr));
106
    onChangeParameterSet(ui->parameterSet->currentIndex());
107
    if (ui->parameterSet->count() < 2) {
108
        ui->parameterSet->hide();
109
    }
110

111
    connect(ui->parameterSet,
112
            qOverload<int>(&QComboBox::activated),
113
            this,
114
            &DlgParameterImp::onChangeParameterSet);
115
    connect(paramGroup, &QTreeWidget::currentItemChanged, this, &DlgParameterImp::onGroupSelected);
116
    onGroupSelected(paramGroup->currentItem());
117

118
    // setup for function on_findGroup_changed:
119
    // store the current font properties because
120
    // we don't know what style sheet the user uses for FC
121
    defaultFont = paramGroup->font();
122
    boldFont = defaultFont;
123
    boldFont.setBold(true);
124
    defaultColor = paramGroup->topLevelItem(0)->foreground(0);
125

126
    ui->findGroupLE->setPlaceholderText(tr("Search Group"));
127
}
128

129
/**
130
 *  Destroys the object and frees any allocated resources
131
 */
132
DlgParameterImp::~DlgParameterImp()
133
{
134
    // no need to delete child widgets, Qt does it all for us
135
    delete ui;
136
}
137

138
void DlgParameterImp::setupConnections()
139
{
140
    // clang-format off
141
    connect(ui->buttonFind, &QPushButton::clicked,
142
            this, &DlgParameterImp::onButtonFindClicked);
143
    connect(ui->findGroupLE, &QLineEdit::textChanged,
144
            this, &DlgParameterImp::onFindGroupTtextChanged);
145
    connect(ui->buttonSaveToDisk, &QPushButton::clicked,
146
            this, &DlgParameterImp::onButtonSaveToDiskClicked);
147
    connect(ui->closeButton, &QPushButton::clicked,
148
            this, &DlgParameterImp::onCloseButtonClicked);
149
    connect(ui->checkSort, &QCheckBox::toggled,
150
            this, &DlgParameterImp::onCheckSortToggled);
151
    // clang-format on
152
}
153

154
void DlgParameterImp::onButtonFindClicked()
155
{
156
    if (finder.isNull()) {
157
        finder = new DlgParameterFind(this);
158
    }
159
    finder->show();
160
}
161

162
void DlgParameterImp::onFindGroupTtextChanged(const QString& SearchStr)
163
{
164
    // search for group tree items and highlight found results
165

166
    QTreeWidgetItem* ExpandItem;
167

168
    // at first reset all items to the default font and expand state
169
    if (!foundList.empty()) {
170
        for (QTreeWidgetItem* item : std::as_const(foundList)) {
171
            item->setFont(0, defaultFont);
172
            item->setForeground(0, defaultColor);
173
            ExpandItem = item;
174
            // a group can be nested down to several levels
175
            // do not collapse if the search string is empty
176
            while (!SearchStr.isEmpty()) {
177
                if (!ExpandItem->parent()) {
178
                    break;
179
                }
180
                else {
181
                    ExpandItem->setExpanded(false);
182
                    ExpandItem = ExpandItem->parent();
183
                }
184
            }
185
        }
186
    }
187
    // expand the top level entries to get the initial tree state
188
    for (int i = 0; i < paramGroup->topLevelItemCount(); ++i) {
189
        paramGroup->topLevelItem(i)->setExpanded(true);
190
    }
191

192
    // don't perform a search if the string is empty
193
    if (SearchStr.isEmpty()) {
194
        return;
195
    }
196

197
    // search the tree widget
198
    foundList = paramGroup->findItems(SearchStr, Qt::MatchContains | Qt::MatchRecursive);
199
    if (!foundList.empty()) {
200
        // reset background style sheet
201
        if (!ui->findGroupLE->styleSheet().isEmpty()) {
202
            ui->findGroupLE->setStyleSheet(QString());
203
        }
204
        for (QTreeWidgetItem* item : std::as_const(foundList)) {
205
            item->setFont(0, boldFont);
206
            item->setForeground(0, Qt::red);
207
            // expand its parent to see the item
208
            // a group can be nested down to several levels
209
            ExpandItem = item;
210
            while (true) {
211
                if (!ExpandItem->parent()) {
212
                    break;
213
                }
214
                else {
215
                    ExpandItem->setExpanded(true);
216
                    ExpandItem = ExpandItem->parent();
217
                }
218
            }
219
            // if there is only one item found, scroll to it
220
            if (foundList.size() == 1) {
221
                paramGroup->scrollToItem(foundList[0], QAbstractItemView::PositionAtCenter);
222
            }
223
        }
224
    }
225
    else {
226
        // Set red background to indicate no matching
227
        QString styleSheet = QString::fromLatin1(" QLineEdit {\n"
228
                                                 "     background-color: rgb(221,144,161);\n"
229
                                                 " }\n");
230
        ui->findGroupLE->setStyleSheet(styleSheet);
231
    }
232
}
233

234
/**
235
 *  Sets the strings of the subwidgets using the current
236
 *  language.
237
 */
238
void DlgParameterImp::changeEvent(QEvent* e)
239
{
240
    if (e->type() == QEvent::LanguageChange) {
241
        ui->retranslateUi(this);
242
        paramGroup->headerItem()->setText(0, tr("Group"));
243
        paramValue->headerItem()->setText(0, tr("Name"));
244
        paramValue->headerItem()->setText(1, tr("Type"));
245
        paramValue->headerItem()->setText(2, tr("Value"));
246
    }
247
    else {
248
        QDialog::changeEvent(e);
249
    }
250
}
251

252
void DlgParameterImp::onCheckSortToggled(bool on)
253
{
254
    paramGroup->setSortingEnabled(on);
255
    paramGroup->sortByColumn(0, Qt::AscendingOrder);
256
    paramGroup->header()->setProperty("showSortIndicator", QVariant(on));
257

258
    paramValue->setSortingEnabled(on);
259
    paramValue->sortByColumn(0, Qt::AscendingOrder);
260
    paramValue->header()->setProperty("showSortIndicator", QVariant(on));
261
}
262

263
void DlgParameterImp::onCloseButtonClicked()
264
{
265
    close();
266
}
267

268
void DlgParameterImp::accept()
269
{
270
    close();
271
}
272

273
void DlgParameterImp::reject()
274
{
275
    close();
276
}
277

278
void DlgParameterImp::showEvent(QShowEvent*)
279
{
280
    ParameterGrp::handle hGrp =
281
        App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
282
    hGrp = hGrp->GetGroup("ParameterEditor");
283
    std::string buf = hGrp->GetASCII("Geometry", "");
284
    if (!buf.empty()) {
285
        int x1, y1, x2, y2;
286
        char sep;
287
        std::stringstream str(buf);
288
        str >> sep >> x1 >> sep >> y1 >> sep >> x2 >> sep >> y2;
289
        QRect rect;
290
        rect.setCoords(x1, y1, x2, y2);
291
        this->setGeometry(rect);
292
    }
293
}
294

295
void DlgParameterImp::closeEvent(QCloseEvent*)
296
{
297
    ParameterGrp::handle hGrp =
298
        App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
299
    hGrp = hGrp->GetGroup("ParameterEditor");
300
    QTreeWidgetItem* current = paramGroup->currentItem();
301
    if (current) {
302
        QStringList paths;
303
        paths << current->text(0);
304
        QTreeWidgetItem* parent = current->parent();
305
        while (parent) {
306
            paths.push_front(parent->text(0));
307
            parent = parent->parent();
308
        }
309

310
        QString path = paths.join(QLatin1String("."));
311
        hGrp->SetASCII("LastParameterGroup", (const char*)path.toUtf8());
312
        // save geometry of window
313
        const QRect& r = this->geometry();
314
        std::stringstream str;
315
        str << "(" << r.left() << "," << r.top() << "," << r.right() << "," << r.bottom() << ")";
316
        hGrp->SetASCII("Geometry", str.str().c_str());
317
    }
318
}
319

320
void DlgParameterImp::onGroupSelected(QTreeWidgetItem* item)
321
{
322
    if (item && item->type() == QTreeWidgetItem::UserType + 1) {
323
        bool sortingEnabled = paramValue->isSortingEnabled();
324
        paramValue->clear();
325
        Base::Reference<ParameterGrp> _hcGrp = static_cast<ParameterGroupItem*>(item)->_hcGrp;
326
        static_cast<ParameterValue*>(paramValue)->setCurrentGroup(_hcGrp);
327

328
        // filling up Text nodes
329
        std::vector<std::pair<std::string, std::string>> mcTextMap = _hcGrp->GetASCIIMap();
330
        for (const auto& It2 : mcTextMap) {
331
            (void)new ParameterText(paramValue,
332
                                    QString::fromUtf8(It2.first.c_str()),
333
                                    It2.second.c_str(),
334
                                    _hcGrp);
335
        }
336

337
        // filling up Int nodes
338
        std::vector<std::pair<std::string, long>> mcIntMap = _hcGrp->GetIntMap();
339
        for (const auto& It3 : mcIntMap) {
340
            (void)new ParameterInt(paramValue,
341
                                   QString::fromUtf8(It3.first.c_str()),
342
                                   It3.second,
343
                                   _hcGrp);
344
        }
345

346
        // filling up Float nodes
347
        std::vector<std::pair<std::string, double>> mcFloatMap = _hcGrp->GetFloatMap();
348
        for (const auto& It4 : mcFloatMap) {
349
            (void)new ParameterFloat(paramValue,
350
                                     QString::fromUtf8(It4.first.c_str()),
351
                                     It4.second,
352
                                     _hcGrp);
353
        }
354

355
        // filling up bool nodes
356
        std::vector<std::pair<std::string, bool>> mcBoolMap = _hcGrp->GetBoolMap();
357
        for (const auto& It5 : mcBoolMap) {
358
            (void)new ParameterBool(paramValue,
359
                                    QString::fromUtf8(It5.first.c_str()),
360
                                    It5.second,
361
                                    _hcGrp);
362
        }
363

364
        // filling up UInt nodes
365
        std::vector<std::pair<std::string, unsigned long>> mcUIntMap = _hcGrp->GetUnsignedMap();
366
        for (const auto& It6 : mcUIntMap) {
367
            (void)new ParameterUInt(paramValue,
368
                                    QString::fromUtf8(It6.first.c_str()),
369
                                    It6.second,
370
                                    _hcGrp);
371
        }
372
        paramValue->setSortingEnabled(sortingEnabled);
373
    }
374
}
375

376
/** Switches the type of parameters with name @a config. */
377
void DlgParameterImp::activateParameterSet(const char* config)
378
{
379
    int index = ui->parameterSet->findData(QByteArray(config));
380
    if (index != -1) {
381
        ui->parameterSet->setCurrentIndex(index);
382
        onChangeParameterSet(index);
383
    }
384
}
385

386
/** Switches the type of parameters either to user or system parameters. */
387
void DlgParameterImp::onChangeParameterSet(int itemPos)
388
{
389
    ParameterManager* rcParMngr =
390
        App::GetApplication().GetParameterSet(ui->parameterSet->itemData(itemPos).toByteArray());
391
    if (!rcParMngr) {
392
        return;
393
    }
394

395
    rcParMngr->CheckDocument();
396
    ui->buttonSaveToDisk->setEnabled(rcParMngr->HasSerializer());
397

398
    // remove all labels
399
    paramGroup->clear();
400
    paramValue->clear();
401

402
    // root labels
403
    std::vector<Base::Reference<ParameterGrp>> grps = rcParMngr->GetGroups();
404
    for (const auto& grp : grps) {
405
        auto item = new ParameterGroupItem(paramGroup, grp);
406
        paramGroup->expandItem(item);
407
        item->setIcon(0, QApplication::style()->standardPixmap(QStyle::SP_ComputerIcon));
408
    }
409

410
    // get the path of the last selected group in the editor
411
    ParameterGrp::handle hGrp =
412
        App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
413
    hGrp = hGrp->GetGroup("ParameterEditor");
414
    QString path = QString::fromUtf8(hGrp->GetASCII("LastParameterGroup").c_str());
415
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
416
    QStringList paths = path.split(QLatin1String("."), Qt::SkipEmptyParts);
417
#else
418
    QStringList paths = path.split(QLatin1String("."), QString::SkipEmptyParts);
419
#endif
420

421
    QTreeWidgetItem* parent = nullptr;
422
    for (int index = 0; index < paramGroup->topLevelItemCount() && !paths.empty(); index++) {
423
        QTreeWidgetItem* child = paramGroup->topLevelItem(index);
424
        if (child->text(0) == paths.front()) {
425
            paths.pop_front();
426
            parent = child;
427
        }
428
    }
429

430
    while (parent && !paths.empty()) {
431
        parent->setExpanded(true);
432
        QTreeWidgetItem* item = parent;
433
        parent = nullptr;
434
        for (int index = 0; index < item->childCount(); index++) {
435
            QTreeWidgetItem* child = item->child(index);
436
            if (child->text(0) == paths.front()) {
437
                paths.pop_front();
438
                parent = child;
439
                break;
440
            }
441
        }
442
    }
443

444
    if (parent) {
445
        paramGroup->setCurrentItem(parent);
446
    }
447
    else if (paramGroup->topLevelItemCount() > 0) {
448
        paramGroup->setCurrentItem(paramGroup->topLevelItem(0));
449
    }
450
}
451

452
void DlgParameterImp::onButtonSaveToDiskClicked()
453
{
454
    int index = ui->parameterSet->currentIndex();
455
    ParameterManager* parmgr =
456
        App::GetApplication().GetParameterSet(ui->parameterSet->itemData(index).toByteArray());
457
    if (!parmgr) {
458
        return;
459
    }
460

461
    parmgr->SaveDocument();
462
}
463

464
namespace Gui
465
{
466
bool validateInput(QWidget* parent, const QString& input)
467
{
468
    if (input.isEmpty()) {
469
        return false;
470
    }
471
    for (int i = 0; i < input.size(); i++) {
472
        const char c = input.at(i).toLatin1();
473
        if ((c < '0' || c > '9') &&  // Numbers
474
            (c < 'A' || c > 'Z') &&  // Uppercase letters
475
            (c < 'a' || c > 'z') &&  // Lowercase letters
476
            (c != ' ')) {            // Space
477
            QMessageBox::warning(parent,
478
                                 DlgParameterImp::tr("Invalid input"),
479
                                 DlgParameterImp::tr("Invalid key name '%1'").arg(input));
480
            return false;
481
        }
482
    }
483
    return true;
484
}
485
}  // namespace Gui
486

487
// --------------------------------------------------------------------
488

489
/* TRANSLATOR Gui::Dialog::ParameterGroup */
490

491
ParameterGroup::ParameterGroup(QWidget* parent)
492
    : QTreeWidget(parent)
493
{
494
    menuEdit = new QMenu(this);
495
    expandAct = menuEdit->addAction(tr("Expand"), this, &ParameterGroup::onToggleSelectedItem);
496
    menuEdit->addSeparator();
497
    subGrpAct = menuEdit->addAction(tr("Add sub-group"), this, &ParameterGroup::onCreateSubgroup);
498
    removeAct =
499
        menuEdit->addAction(tr("Remove group"), this, &ParameterGroup::onDeleteSelectedItem);
500
    renameAct =
501
        menuEdit->addAction(tr("Rename group"), this, &ParameterGroup::onRenameSelectedItem);
502
    menuEdit->addSeparator();
503
    exportAct = menuEdit->addAction(tr("Export parameter"), this, &ParameterGroup::onExportToFile);
504
    importAct =
505
        menuEdit->addAction(tr("Import parameter"), this, &ParameterGroup::onImportFromFile);
506
    menuEdit->setDefaultAction(expandAct);
507
}
508

509
ParameterGroup::~ParameterGroup() = default;
510

511
void ParameterGroup::contextMenuEvent(QContextMenuEvent* event)
512
{
513
    QTreeWidgetItem* item = currentItem();
514
    if (item && item->isSelected()) {
515
        expandAct->setEnabled(item->childCount() > 0);
516
        // do not allow to import parameters from a non-empty parameter group
517
        importAct->setEnabled(item->childCount() == 0);
518

519
        if (item->isExpanded()) {
520
            expandAct->setText(tr("Collapse"));
521
        }
522
        else {
523
            expandAct->setText(tr("Expand"));
524
        }
525
        menuEdit->popup(event->globalPos());
526
    }
527
}
528

529
void ParameterGroup::keyPressEvent(QKeyEvent* event)
530
{
531
    if (event->matches(QKeySequence::Delete)) {
532
        onDeleteSelectedItem();
533
    }
534
    else {
535
        QTreeWidget::keyPressEvent(event);
536
    }
537
}
538

539
void ParameterGroup::onDeleteSelectedItem()
540
{
541
    QTreeWidgetItem* sel = currentItem();
542
    if (sel && sel->isSelected() && sel->parent()) {
543
        if (QMessageBox::question(this,
544
                                  tr("Remove group"),
545
                                  tr("Do you really want to remove this parameter group?"),
546
                                  QMessageBox::Yes | QMessageBox::No,
547
                                  QMessageBox::No)
548
            == QMessageBox::Yes) {
549
            QTreeWidgetItem* parent = sel->parent();
550
            int index = parent->indexOfChild(sel);
551
            parent->takeChild(index);
552

553
            std::string groupName = sel->text(0).toStdString();
554
            // must delete the tree item here because it and its children still
555
            // hold a reference to the parameter group
556
            delete sel;
557

558
            auto para = static_cast<ParameterGroupItem*>(parent);
559
            para->_hcGrp->RemoveGrp(groupName.c_str());
560
        }
561
    }
562
}
563

564
void ParameterGroup::onToggleSelectedItem()
565
{
566
    QTreeWidgetItem* sel = currentItem();
567
    if (sel && sel->isSelected()) {
568
        if (sel->isExpanded()) {
569
            sel->setExpanded(false);
570
        }
571
        else if (sel->childCount() > 0) {
572
            sel->setExpanded(true);
573
        }
574
    }
575
}
576

577
void ParameterGroup::onCreateSubgroup()
578
{
579
    bool ok;
580
    QString name = QInputDialog::getText(this,
581
                                         QObject::tr("New sub-group"),
582
                                         QObject::tr("Enter the name:"),
583
                                         QLineEdit::Normal,
584
                                         QString(),
585
                                         &ok,
586
                                         Qt::MSWindowsFixedSizeDialogHint);
587

588
    if (ok && Gui::validateInput(this, name)) {
589
        QTreeWidgetItem* item = currentItem();
590
        if (item && item->isSelected()) {
591
            auto para = static_cast<ParameterGroupItem*>(item);
592
            Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
593

594
            if (hGrp->HasGroup(name.toLatin1())) {
595
                QMessageBox::critical(this,
596
                                      tr("Existing sub-group"),
597
                                      tr("The sub-group '%1' already exists.").arg(name));
598
                return;
599
            }
600

601
            hGrp = hGrp->GetGroup(name.toLatin1());
602
            (void)new ParameterGroupItem(para, hGrp);
603
            expandItem(para);
604
        }
605
    }
606
}
607

608
void ParameterGroup::onExportToFile()
609
{
610
    QString file = FileDialog::getSaveFileName(this,
611
                                               tr("Export parameter to file"),
612
                                               QString(),
613
                                               QString::fromLatin1("XML (*.FCParam)"));
614
    if (!file.isEmpty()) {
615
        QTreeWidgetItem* item = currentItem();
616
        if (item && item->isSelected()) {
617
            auto para = static_cast<ParameterGroupItem*>(item);
618
            Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
619
            hGrp->exportTo(file.toUtf8());
620
        }
621
    }
622
}
623

624
void ParameterGroup::onImportFromFile()
625
{
626
    QString file = FileDialog::getOpenFileName(this,
627
                                               tr("Import parameter from file"),
628
                                               QString(),
629
                                               QString::fromLatin1("XML (*.FCParam)"));
630
    if (!file.isEmpty()) {
631
        QTreeWidgetItem* item = currentItem();
632
        if (item && item->isSelected()) {
633
            auto para = static_cast<ParameterGroupItem*>(item);
634
            Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
635

636
            // remove the items and internal parameter values
637
            QList<QTreeWidgetItem*> childs = para->takeChildren();
638
            for (auto& child : childs) {
639
                delete child;
640
            }
641

642
            try {
643
                hGrp->importFrom(file.toUtf8());
644
                std::vector<Base::Reference<ParameterGrp>> cSubGrps = hGrp->GetGroups();
645
                for (const auto& cSubGrp : cSubGrps) {
646
                    new ParameterGroupItem(para, cSubGrp);
647
                }
648

649
                para->setExpanded(para->childCount());
650
            }
651
            catch (const Base::Exception&) {
652
                QMessageBox::critical(this,
653
                                      tr("Import Error"),
654
                                      tr("Reading from '%1' failed.").arg(file));
655
            }
656
        }
657
    }
658
}
659

660
void ParameterGroup::onRenameSelectedItem()
661
{
662
    QTreeWidgetItem* sel = currentItem();
663
    if (sel && sel->isSelected()) {
664
        editItem(sel, 0);
665
    }
666
}
667

668
void ParameterGroup::changeEvent(QEvent* e)
669
{
670
    if (e->type() == QEvent::LanguageChange) {
671
        expandAct->setText(tr("Expand"));
672
        subGrpAct->setText(tr("Add sub-group"));
673
        removeAct->setText(tr("Remove group"));
674
        renameAct->setText(tr("Rename group"));
675
        exportAct->setText(tr("Export parameter"));
676
        importAct->setText(tr("Import parameter"));
677
    }
678
    else {
679
        QTreeWidget::changeEvent(e);
680
    }
681
}
682

683
// --------------------------------------------------------------------
684

685
/* TRANSLATOR Gui::Dialog::ParameterValue */
686

687
ParameterValue::ParameterValue(QWidget* parent)
688
    : QTreeWidget(parent)
689
{
690
    menuEdit = new QMenu(this);
691
    changeAct = menuEdit->addAction(tr("Change value"),
692
                                    this,
693
                                    qOverload<>(&ParameterValue::onChangeSelectedItem));
694
    menuEdit->addSeparator();
695
    removeAct = menuEdit->addAction(tr("Remove key"), this, &ParameterValue::onDeleteSelectedItem);
696
    renameAct = menuEdit->addAction(tr("Rename key"), this, &ParameterValue::onRenameSelectedItem);
697
    menuEdit->setDefaultAction(changeAct);
698

699
    menuEdit->addSeparator();
700
    menuNew = menuEdit->addMenu(tr("New"));
701
    newStrAct = menuNew->addAction(tr("New string item"), this, &ParameterValue::onCreateTextItem);
702
    newFltAct = menuNew->addAction(tr("New float item"), this, &ParameterValue::onCreateFloatItem);
703
    newIntAct = menuNew->addAction(tr("New integer item"), this, &ParameterValue::onCreateIntItem);
704
    newUlgAct =
705
        menuNew->addAction(tr("New unsigned item"), this, &ParameterValue::onCreateUIntItem);
706
    newBlnAct = menuNew->addAction(tr("New Boolean item"), this, &ParameterValue::onCreateBoolItem);
707

708
    connect(this,
709
            &ParameterValue::itemDoubleClicked,
710
            this,
711
            qOverload<QTreeWidgetItem*, int>(&ParameterValue::onChangeSelectedItem));
712
}
713

714
ParameterValue::~ParameterValue() = default;
715

716
void ParameterValue::setCurrentGroup(const Base::Reference<ParameterGrp>& hGrp)
717
{
718
    _hcGrp = hGrp;
719
}
720

721
Base::Reference<ParameterGrp> ParameterValue::currentGroup() const
722
{
723
    return _hcGrp;
724
}
725

726
bool ParameterValue::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event)
727
{
728
    if (index.column() > 0) {
729
        return false;
730
    }
731
    return QTreeWidget::edit(index, trigger, event);
732
}
733

734
void ParameterValue::contextMenuEvent(QContextMenuEvent* event)
735
{
736
    QTreeWidgetItem* item = currentItem();
737
    if (item && item->isSelected()) {
738
        menuEdit->popup(event->globalPos());
739
    }
740
    else {
741
        // There is a regression in Qt 5.12.9 where it isn't checked that a sub-menu (here menuNew)
742
        // can be popped up without its parent menu (menuEdit) and thus causes a crash.
743
        // A workaround is to simply call exec() instead.
744
        //
745
        // menuNew->popup(event->globalPos());
746
        menuNew->exec(event->globalPos());
747
    }
748
}
749

750
void ParameterValue::keyPressEvent(QKeyEvent* event)
751
{
752
    if (event->matches(QKeySequence::Delete)) {
753
        onDeleteSelectedItem();
754
    }
755
    else {
756
        QTreeWidget::keyPressEvent(event);
757
    }
758
}
759

760
void ParameterValue::resizeEvent(QResizeEvent* event)
761
{
762
    QHeaderView* hv = header();
763
    hv->setSectionResizeMode(QHeaderView::Stretch);
764

765
    QTreeWidget::resizeEvent(event);
766

767
    hv->setSectionResizeMode(QHeaderView::Interactive);
768
}
769

770
void ParameterValue::onChangeSelectedItem(QTreeWidgetItem* item, int col)
771
{
772
    if (item->isSelected() && col > 0) {
773
        static_cast<ParameterValueItem*>(item)->changeValue();
774
    }
775
}
776

777
void ParameterValue::onChangeSelectedItem()
778
{
779
    onChangeSelectedItem(currentItem(), 1);
780
}
781

782
void ParameterValue::onDeleteSelectedItem()
783
{
784
    QTreeWidgetItem* sel = currentItem();
785
    if (sel && sel->isSelected()) {
786
        takeTopLevelItem(indexOfTopLevelItem(sel));
787
        static_cast<ParameterValueItem*>(sel)->removeFromGroup();
788
        delete sel;
789
    }
790
}
791

792
void ParameterValue::onRenameSelectedItem()
793
{
794
    QTreeWidgetItem* sel = currentItem();
795
    if (sel && sel->isSelected()) {
796
        editItem(sel, 0);
797
    }
798
}
799

800
void ParameterValue::onCreateTextItem()
801
{
802
    bool ok;
803
    QString name = QInputDialog::getText(this,
804
                                         QObject::tr("New text item"),
805
                                         QObject::tr("Enter the name:"),
806
                                         QLineEdit::Normal,
807
                                         QString(),
808
                                         &ok,
809
                                         Qt::MSWindowsFixedSizeDialogHint);
810

811
    if (!ok || !Gui::validateInput(this, name)) {
812
        return;
813
    }
814

815
    std::vector<std::pair<std::string, std::string>> smap = _hcGrp->GetASCIIMap();
816
    for (const auto& it : smap) {
817
        if (name == QLatin1String(it.first.c_str())) {
818
            QMessageBox::critical(this,
819
                                  tr("Existing item"),
820
                                  tr("The item '%1' already exists.").arg(name));
821
            return;
822
        }
823
    }
824

825
    QString val = QInputDialog::getText(this,
826
                                        QObject::tr("New text item"),
827
                                        QObject::tr("Enter your text:"),
828
                                        QLineEdit::Normal,
829
                                        QString(),
830
                                        &ok,
831
                                        Qt::MSWindowsFixedSizeDialogHint);
832
    if (ok && !val.isEmpty()) {
833
        ParameterValueItem* pcItem;
834
        pcItem = new ParameterText(this, name, val.toUtf8(), _hcGrp);
835
        pcItem->appendToGroup();
836
    }
837
}
838

839
void ParameterValue::onCreateIntItem()
840
{
841
    bool ok;
842
    QString name = QInputDialog::getText(this,
843
                                         QObject::tr("New integer item"),
844
                                         QObject::tr("Enter the name:"),
845
                                         QLineEdit::Normal,
846
                                         QString(),
847
                                         &ok,
848
                                         Qt::MSWindowsFixedSizeDialogHint);
849

850
    if (!ok || !Gui::validateInput(this, name)) {
851
        return;
852
    }
853

854
    std::vector<std::pair<std::string, long>> lmap = _hcGrp->GetIntMap();
855
    for (const auto& it : lmap) {
856
        if (name == QLatin1String(it.first.c_str())) {
857
            QMessageBox::critical(this,
858
                                  tr("Existing item"),
859
                                  tr("The item '%1' already exists.").arg(name));
860
            return;
861
        }
862
    }
863

864
    int val = QInputDialog::getInt(this,
865
                                   QObject::tr("New integer item"),
866
                                   QObject::tr("Enter your number:"),
867
                                   0,
868
                                   -2147483647,
869
                                   2147483647,
870
                                   1,
871
                                   &ok,
872
                                   Qt::MSWindowsFixedSizeDialogHint);
873

874
    if (ok) {
875
        ParameterValueItem* pcItem;
876
        pcItem = new ParameterInt(this, name, (long)val, _hcGrp);
877
        pcItem->appendToGroup();
878
    }
879
}
880

881
void ParameterValue::onCreateUIntItem()
882
{
883
    bool ok;
884
    QString name = QInputDialog::getText(this,
885
                                         QObject::tr("New unsigned item"),
886
                                         QObject::tr("Enter the name:"),
887
                                         QLineEdit::Normal,
888
                                         QString(),
889
                                         &ok,
890
                                         Qt::MSWindowsFixedSizeDialogHint);
891

892
    if (!ok || !Gui::validateInput(this, name)) {
893
        return;
894
    }
895

896
    std::vector<std::pair<std::string, unsigned long>> lmap = _hcGrp->GetUnsignedMap();
897
    for (const auto& it : lmap) {
898
        if (name == QLatin1String(it.first.c_str())) {
899
            QMessageBox::critical(this,
900
                                  tr("Existing item"),
901
                                  tr("The item '%1' already exists.").arg(name));
902
            return;
903
        }
904
    }
905

906
    DlgInputDialogImp dlg(QObject::tr("Enter your number:"),
907
                          this,
908
                          true,
909
                          DlgInputDialogImp::UIntBox);
910
    dlg.setWindowTitle(QObject::tr("New unsigned item"));
911
    UIntSpinBox* edit = dlg.getUIntBox();
912
    edit->setRange(0, UINT_MAX);
913
    if (dlg.exec() == QDialog::Accepted) {
914
        QString value = edit->text();
915
        unsigned long val = value.toULong(&ok);
916

917
        if (ok) {
918
            ParameterValueItem* pcItem;
919
            pcItem = new ParameterUInt(this, name, val, _hcGrp);
920
            pcItem->appendToGroup();
921
        }
922
    }
923
}
924

925
void ParameterValue::onCreateFloatItem()
926
{
927
    bool ok;
928
    QString name = QInputDialog::getText(this,
929
                                         QObject::tr("New float item"),
930
                                         QObject::tr("Enter the name:"),
931
                                         QLineEdit::Normal,
932
                                         QString(),
933
                                         &ok,
934
                                         Qt::MSWindowsFixedSizeDialogHint);
935

936
    if (!ok || !Gui::validateInput(this, name)) {
937
        return;
938
    }
939

940
    std::vector<std::pair<std::string, double>> fmap = _hcGrp->GetFloatMap();
941
    for (const auto& it : fmap) {
942
        if (name == QLatin1String(it.first.c_str())) {
943
            QMessageBox::critical(this,
944
                                  tr("Existing item"),
945
                                  tr("The item '%1' already exists.").arg(name));
946
            return;
947
        }
948
    }
949

950
    double val = QInputDialog::getDouble(this,
951
                                         QObject::tr("New float item"),
952
                                         QObject::tr("Enter your number:"),
953
                                         0,
954
                                         -2147483647,
955
                                         2147483647,
956
                                         12,
957
                                         &ok,
958
                                         Qt::MSWindowsFixedSizeDialogHint);
959
    if (ok) {
960
        ParameterValueItem* pcItem;
961
        pcItem = new ParameterFloat(this, name, val, _hcGrp);
962
        pcItem->appendToGroup();
963
    }
964
}
965

966
void ParameterValue::onCreateBoolItem()
967
{
968
    bool ok;
969
    QString name = QInputDialog::getText(this,
970
                                         QObject::tr("New Boolean item"),
971
                                         QObject::tr("Enter the name:"),
972
                                         QLineEdit::Normal,
973
                                         QString(),
974
                                         &ok,
975
                                         Qt::MSWindowsFixedSizeDialogHint);
976

977
    if (!ok || !Gui::validateInput(this, name)) {
978
        return;
979
    }
980

981
    std::vector<std::pair<std::string, bool>> bmap = _hcGrp->GetBoolMap();
982
    for (const auto& it : bmap) {
983
        if (name == QLatin1String(it.first.c_str())) {
984
            QMessageBox::critical(this,
985
                                  tr("Existing item"),
986
                                  tr("The item '%1' already exists.").arg(name));
987
            return;
988
        }
989
    }
990

991
    QStringList list;
992
    list << QString::fromLatin1("true") << QString::fromLatin1("false");
993
    QString val = QInputDialog::getItem(this,
994
                                        QObject::tr("New boolean item"),
995
                                        QObject::tr("Choose an item:"),
996
                                        list,
997
                                        0,
998
                                        false,
999
                                        &ok,
1000
                                        Qt::MSWindowsFixedSizeDialogHint);
1001
    if (ok) {
1002
        ParameterValueItem* pcItem;
1003
        pcItem = new ParameterBool(this, name, (val == list[0] ? true : false), _hcGrp);
1004
        pcItem->appendToGroup();
1005
    }
1006
}
1007

1008
// ---------------------------------------------------------------------------
1009

1010
ParameterGroupItem::ParameterGroupItem(ParameterGroupItem* parent,
1011
                                       const Base::Reference<ParameterGrp>& hcGrp)
1012
    : QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1)
1013
    , _hcGrp(hcGrp)
1014
{
1015
    setFlags(flags() | Qt::ItemIsEditable);
1016
    fillUp();
1017
}
1018

1019
ParameterGroupItem::ParameterGroupItem(QTreeWidget* parent,
1020
                                       const Base::Reference<ParameterGrp>& hcGrp)
1021
    : QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1)
1022
    , _hcGrp(hcGrp)
1023
{
1024
    setFlags(flags() | Qt::ItemIsEditable);
1025
    fillUp();
1026
}
1027

1028
ParameterGroupItem::~ParameterGroupItem()
1029
{
1030
    // if the group has already been removed from the parameters then clear the observer list
1031
    // as we cannot notify the attached observers here
1032
    if (_hcGrp.getRefCount() == 1) {
1033
        _hcGrp->ClearObserver();
1034
    }
1035
}
1036

1037
void ParameterGroupItem::fillUp()
1038
{
1039
    // filling up groups
1040
    std::vector<Base::Reference<ParameterGrp>> vhcParamGrp = _hcGrp->GetGroups();
1041

1042
    setText(0, QString::fromUtf8(_hcGrp->GetGroupName()));
1043
    for (const auto& It : vhcParamGrp) {
1044
        (void)new ParameterGroupItem(this, It);
1045
    }
1046
}
1047

1048
void ParameterGroupItem::setData(int column, int role, const QVariant& value)
1049
{
1050
    if (role == Qt::EditRole) {
1051
        QString oldName = text(0);
1052
        QString newName = value.toString();
1053
        if (newName.isEmpty() || oldName == newName) {
1054
            return;
1055
        }
1056

1057
        if (!Gui::validateInput(treeWidget(), newName)) {
1058
            return;
1059
        }
1060

1061
        // first check if there is already a group with name "newName"
1062
        auto item = static_cast<ParameterGroupItem*>(parent());
1063
        if (!item) {
1064
            QMessageBox::critical(treeWidget(),
1065
                                  QObject::tr("Rename group"),
1066
                                  QObject::tr("The group '%1' cannot be renamed.").arg(oldName));
1067
            return;
1068
        }
1069
        if (item->_hcGrp->HasGroup(newName.toLatin1())) {
1070
            QMessageBox::critical(treeWidget(),
1071
                                  QObject::tr("Existing group"),
1072
                                  QObject::tr("The group '%1' already exists.").arg(newName));
1073
            return;
1074
        }
1075
        else {
1076
            // rename the group by adding a new group, copy the content and remove the old group
1077
            if (!item->_hcGrp->RenameGrp(oldName.toLatin1(), newName.toLatin1())) {
1078
                return;
1079
            }
1080
        }
1081
    }
1082

1083
    QTreeWidgetItem::setData(column, role, value);
1084
}
1085

1086
QVariant ParameterGroupItem::data(int column, int role) const
1087
{
1088
    if (role == Qt::DecorationRole) {
1089
        // The root item should keep its special pixmap
1090
        if (parent()) {
1091
            return this->isExpanded()
1092
                ? QApplication::style()->standardPixmap(QStyle::SP_DirOpenIcon)
1093
                : QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon);
1094
        }
1095
    }
1096

1097
    return QTreeWidgetItem::data(column, role);
1098
}
1099

1100
// --------------------------------------------------------------------
1101

1102
ParameterValueItem::ParameterValueItem(QTreeWidget* parent,
1103
                                       const Base::Reference<ParameterGrp>& hcGrp)
1104
    : QTreeWidgetItem(parent)
1105
    , _hcGrp(hcGrp)
1106
{
1107
    setFlags(flags() | Qt::ItemIsEditable);
1108
}
1109

1110
ParameterValueItem::~ParameterValueItem() = default;
1111

1112
void ParameterValueItem::setData(int column, int role, const QVariant& value)
1113
{
1114
    if (role == Qt::EditRole) {
1115
        QString oldName = text(0);
1116
        QString newName = value.toString();
1117
        if (newName.isEmpty() || oldName == newName) {
1118
            return;
1119
        }
1120

1121
        if (!Gui::validateInput(treeWidget(), newName)) {
1122
            return;
1123
        }
1124

1125
        replace(oldName, newName);
1126
    }
1127

1128
    QTreeWidgetItem::setData(column, role, value);
1129
}
1130

1131
// --------------------------------------------------------------------
1132

1133
ParameterText::ParameterText(QTreeWidget* parent,
1134
                             QString label,
1135
                             const char* value,
1136
                             const Base::Reference<ParameterGrp>& hcGrp)
1137
    : ParameterValueItem(parent, hcGrp)
1138
{
1139
    setIcon(0, BitmapFactory().iconFromTheme("Param_Text"));
1140
    setText(0, label);
1141
    setText(1, QString::fromLatin1("Text"));
1142
    setText(2, QString::fromUtf8(value));
1143
}
1144

1145
ParameterText::~ParameterText() = default;
1146

1147
void ParameterText::changeValue()
1148
{
1149
    bool ok;
1150
    QString txt = QInputDialog::getText(treeWidget(),
1151
                                        QObject::tr("Change value"),
1152
                                        QObject::tr("Enter your text:"),
1153
                                        QLineEdit::Normal,
1154
                                        text(2),
1155
                                        &ok,
1156
                                        Qt::MSWindowsFixedSizeDialogHint);
1157
    if (ok) {
1158
        setText(2, txt);
1159
        _hcGrp->SetASCII(text(0).toLatin1(), txt.toUtf8());
1160
    }
1161
}
1162

1163
void ParameterText::removeFromGroup()
1164
{
1165
    _hcGrp->RemoveASCII(text(0).toLatin1());
1166
}
1167

1168
void ParameterText::replace(const QString& oldName, const QString& newName)
1169
{
1170
    std::string val = _hcGrp->GetASCII(oldName.toLatin1());
1171
    _hcGrp->RemoveASCII(oldName.toLatin1());
1172
    _hcGrp->SetASCII(newName.toLatin1(), val.c_str());
1173
}
1174

1175
void ParameterText::appendToGroup()
1176
{
1177
    _hcGrp->SetASCII(text(0).toLatin1(), text(2).toUtf8());
1178
}
1179

1180
// --------------------------------------------------------------------
1181

1182
ParameterInt::ParameterInt(QTreeWidget* parent,
1183
                           QString label,
1184
                           long value,
1185
                           const Base::Reference<ParameterGrp>& hcGrp)
1186
    : ParameterValueItem(parent, hcGrp)
1187
{
1188
    setIcon(0, BitmapFactory().iconFromTheme("Param_Int"));
1189
    setText(0, label);
1190
    setText(1, QString::fromLatin1("Integer"));
1191
    setText(2, QString::fromLatin1("%1").arg(value));
1192
}
1193

1194
ParameterInt::~ParameterInt() = default;
1195

1196
void ParameterInt::changeValue()
1197
{
1198
    bool ok;
1199
    int num = QInputDialog::getInt(treeWidget(),
1200
                                   QObject::tr("Change value"),
1201
                                   QObject::tr("Enter your number:"),
1202
                                   text(2).toInt(),
1203
                                   -2147483647,
1204
                                   2147483647,
1205
                                   1,
1206
                                   &ok,
1207
                                   Qt::MSWindowsFixedSizeDialogHint);
1208
    if (ok) {
1209
        setText(2, QString::fromLatin1("%1").arg(num));
1210
        _hcGrp->SetInt(text(0).toLatin1(), (long)num);
1211
    }
1212
}
1213

1214
void ParameterInt::removeFromGroup()
1215
{
1216
    _hcGrp->RemoveInt(text(0).toLatin1());
1217
}
1218

1219
void ParameterInt::replace(const QString& oldName, const QString& newName)
1220
{
1221
    long val = _hcGrp->GetInt(oldName.toLatin1());
1222
    _hcGrp->RemoveInt(oldName.toLatin1());
1223
    _hcGrp->SetInt(newName.toLatin1(), val);
1224
}
1225

1226
void ParameterInt::appendToGroup()
1227
{
1228
    _hcGrp->SetInt(text(0).toLatin1(), text(2).toLong());
1229
}
1230

1231
// --------------------------------------------------------------------
1232

1233
ParameterUInt::ParameterUInt(QTreeWidget* parent,
1234
                             QString label,
1235
                             unsigned long value,
1236
                             const Base::Reference<ParameterGrp>& hcGrp)
1237
    : ParameterValueItem(parent, hcGrp)
1238
{
1239
    setIcon(0, BitmapFactory().iconFromTheme("Param_UInt"));
1240
    setText(0, label);
1241
    setText(1, QString::fromLatin1("Unsigned"));
1242
    setText(2, QString::fromLatin1("%1").arg(value));
1243
}
1244

1245
ParameterUInt::~ParameterUInt() = default;
1246

1247
void ParameterUInt::changeValue()
1248
{
1249
    bool ok;
1250
    DlgInputDialogImp dlg(QObject::tr("Enter your number:"),
1251
                          treeWidget(),
1252
                          true,
1253
                          DlgInputDialogImp::UIntBox);
1254
    dlg.setWindowTitle(QObject::tr("Change value"));
1255
    UIntSpinBox* edit = dlg.getUIntBox();
1256
    edit->setRange(0, UINT_MAX);
1257
    edit->setValue(text(2).toULong());
1258
    if (dlg.exec() == QDialog::Accepted) {
1259
        QString value = edit->text();
1260
        unsigned long num = value.toULong(&ok);
1261

1262
        if (ok) {
1263
            setText(2, QString::fromLatin1("%1").arg(num));
1264
            _hcGrp->SetUnsigned(text(0).toLatin1(), (unsigned long)num);
1265
        }
1266
    }
1267
}
1268

1269
void ParameterUInt::removeFromGroup()
1270
{
1271
    _hcGrp->RemoveUnsigned(text(0).toLatin1());
1272
}
1273

1274
void ParameterUInt::replace(const QString& oldName, const QString& newName)
1275
{
1276
    unsigned long val = _hcGrp->GetUnsigned(oldName.toLatin1());
1277
    _hcGrp->RemoveUnsigned(oldName.toLatin1());
1278
    _hcGrp->SetUnsigned(newName.toLatin1(), val);
1279
}
1280

1281
void ParameterUInt::appendToGroup()
1282
{
1283
    _hcGrp->SetUnsigned(text(0).toLatin1(), text(2).toULong());
1284
}
1285

1286
// --------------------------------------------------------------------
1287

1288
ParameterFloat::ParameterFloat(QTreeWidget* parent,
1289
                               QString label,
1290
                               double value,
1291
                               const Base::Reference<ParameterGrp>& hcGrp)
1292
    : ParameterValueItem(parent, hcGrp)
1293
{
1294
    setIcon(0, BitmapFactory().iconFromTheme("Param_Float"));
1295
    setText(0, label);
1296
    setText(1, QString::fromLatin1("Float"));
1297
    setText(2, QString::fromLatin1("%1").arg(value));
1298
}
1299

1300
ParameterFloat::~ParameterFloat() = default;
1301

1302
void ParameterFloat::changeValue()
1303
{
1304
    bool ok;
1305
    double num = QInputDialog::getDouble(treeWidget(),
1306
                                         QObject::tr("Change value"),
1307
                                         QObject::tr("Enter your number:"),
1308
                                         text(2).toDouble(),
1309
                                         -2147483647,
1310
                                         2147483647,
1311
                                         12,
1312
                                         &ok,
1313
                                         Qt::MSWindowsFixedSizeDialogHint);
1314
    if (ok) {
1315
        setText(2, QString::fromLatin1("%1").arg(num));
1316
        _hcGrp->SetFloat(text(0).toLatin1(), num);
1317
    }
1318
}
1319

1320
void ParameterFloat::removeFromGroup()
1321
{
1322
    _hcGrp->RemoveFloat(text(0).toLatin1());
1323
}
1324

1325
void ParameterFloat::replace(const QString& oldName, const QString& newName)
1326
{
1327
    double val = _hcGrp->GetFloat(oldName.toLatin1());
1328
    _hcGrp->RemoveFloat(oldName.toLatin1());
1329
    _hcGrp->SetFloat(newName.toLatin1(), val);
1330
}
1331

1332
void ParameterFloat::appendToGroup()
1333
{
1334
    _hcGrp->SetFloat(text(0).toLatin1(), text(2).toDouble());
1335
}
1336

1337
// --------------------------------------------------------------------
1338

1339
ParameterBool::ParameterBool(QTreeWidget* parent,
1340
                             QString label,
1341
                             bool value,
1342
                             const Base::Reference<ParameterGrp>& hcGrp)
1343
    : ParameterValueItem(parent, hcGrp)
1344
{
1345
    setIcon(0, BitmapFactory().iconFromTheme("Param_Bool"));
1346
    setText(0, label);
1347
    setText(1, QString::fromLatin1("Boolean"));
1348
    setText(2, QString::fromLatin1((value ? "true" : "false")));
1349
}
1350

1351
ParameterBool::~ParameterBool() = default;
1352

1353
void ParameterBool::changeValue()
1354
{
1355
    bool ok;
1356
    QStringList list;
1357
    list << QString::fromLatin1("true") << QString::fromLatin1("false");
1358
    int pos = (text(2) == list[0] ? 0 : 1);
1359

1360
    QString txt = QInputDialog::getItem(treeWidget(),
1361
                                        QObject::tr("Change value"),
1362
                                        QObject::tr("Choose an item:"),
1363
                                        list,
1364
                                        pos,
1365
                                        false,
1366
                                        &ok,
1367
                                        Qt::MSWindowsFixedSizeDialogHint);
1368
    if (ok) {
1369
        setText(2, txt);
1370
        _hcGrp->SetBool(text(0).toLatin1(), (txt == list[0] ? true : false));
1371
    }
1372
}
1373

1374
void ParameterBool::removeFromGroup()
1375
{
1376
    _hcGrp->RemoveBool(text(0).toLatin1());
1377
}
1378

1379
void ParameterBool::replace(const QString& oldName, const QString& newName)
1380
{
1381
    bool val = _hcGrp->GetBool(oldName.toLatin1());
1382
    _hcGrp->RemoveBool(oldName.toLatin1());
1383
    _hcGrp->SetBool(newName.toLatin1(), val);
1384
}
1385

1386
void ParameterBool::appendToGroup()
1387
{
1388
    bool val = (text(2) == QLatin1String("true") ? true : false);
1389
    _hcGrp->SetBool(text(0).toLatin1(), val);
1390
}
1391

1392
#include "moc_DlgParameterImp.cpp"
1393

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

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

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

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