1
/***************************************************************************
2
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
27
#include <QContextMenuEvent>
29
#include <QInputDialog>
35
#include <App/Application.h>
36
#include <Base/Exception.h>
37
#include <Base/Parameter.h>
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"
48
using namespace Gui::Dialog;
50
/* TRANSLATOR Gui::Dialog::DlgParameterImp */
53
* Constructs a DlgParameterImp which is a child of 'parent', with the
54
* name 'name' and widget flags set to 'f'
56
* The dialog will by default be modeless, unless you set 'modal' to
57
* true to construct a modal dialog.
59
DlgParameterImp::DlgParameterImp(QWidget* parent, Qt::WindowFlags fl)
60
: QDialog(parent, fl | Qt::WindowMinMaxButtonsHint)
61
, ui(new Ui_DlgParameter)
66
ui->checkSort->setVisible(false); // for testing
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));
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));
87
QSizePolicy policy = paramValue->sizePolicy();
88
policy.setHorizontalStretch(3);
89
paramValue->setSizePolicy(policy);
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" );
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())));
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();
111
connect(ui->parameterSet,
112
qOverload<int>(&QComboBox::activated),
114
&DlgParameterImp::onChangeParameterSet);
115
connect(paramGroup, &QTreeWidget::currentItemChanged, this, &DlgParameterImp::onGroupSelected);
116
onGroupSelected(paramGroup->currentItem());
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);
126
ui->findGroupLE->setPlaceholderText(tr("Search Group"));
130
* Destroys the object and frees any allocated resources
132
DlgParameterImp::~DlgParameterImp()
134
// no need to delete child widgets, Qt does it all for us
138
void DlgParameterImp::setupConnections()
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);
154
void DlgParameterImp::onButtonFindClicked()
156
if (finder.isNull()) {
157
finder = new DlgParameterFind(this);
162
void DlgParameterImp::onFindGroupTtextChanged(const QString& SearchStr)
164
// search for group tree items and highlight found results
166
QTreeWidgetItem* ExpandItem;
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);
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()) {
181
ExpandItem->setExpanded(false);
182
ExpandItem = ExpandItem->parent();
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);
192
// don't perform a search if the string is empty
193
if (SearchStr.isEmpty()) {
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());
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
211
if (!ExpandItem->parent()) {
215
ExpandItem->setExpanded(true);
216
ExpandItem = ExpandItem->parent();
219
// if there is only one item found, scroll to it
220
if (foundList.size() == 1) {
221
paramGroup->scrollToItem(foundList[0], QAbstractItemView::PositionAtCenter);
226
// Set red background to indicate no matching
227
QString styleSheet = QString::fromLatin1(" QLineEdit {\n"
228
" background-color: rgb(221,144,161);\n"
230
ui->findGroupLE->setStyleSheet(styleSheet);
235
* Sets the strings of the subwidgets using the current
238
void DlgParameterImp::changeEvent(QEvent* e)
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"));
248
QDialog::changeEvent(e);
252
void DlgParameterImp::onCheckSortToggled(bool on)
254
paramGroup->setSortingEnabled(on);
255
paramGroup->sortByColumn(0, Qt::AscendingOrder);
256
paramGroup->header()->setProperty("showSortIndicator", QVariant(on));
258
paramValue->setSortingEnabled(on);
259
paramValue->sortByColumn(0, Qt::AscendingOrder);
260
paramValue->header()->setProperty("showSortIndicator", QVariant(on));
263
void DlgParameterImp::onCloseButtonClicked()
268
void DlgParameterImp::accept()
273
void DlgParameterImp::reject()
278
void DlgParameterImp::showEvent(QShowEvent*)
280
ParameterGrp::handle hGrp =
281
App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
282
hGrp = hGrp->GetGroup("ParameterEditor");
283
std::string buf = hGrp->GetASCII("Geometry", "");
287
std::stringstream str(buf);
288
str >> sep >> x1 >> sep >> y1 >> sep >> x2 >> sep >> y2;
290
rect.setCoords(x1, y1, x2, y2);
291
this->setGeometry(rect);
295
void DlgParameterImp::closeEvent(QCloseEvent*)
297
ParameterGrp::handle hGrp =
298
App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
299
hGrp = hGrp->GetGroup("ParameterEditor");
300
QTreeWidgetItem* current = paramGroup->currentItem();
303
paths << current->text(0);
304
QTreeWidgetItem* parent = current->parent();
306
paths.push_front(parent->text(0));
307
parent = parent->parent();
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());
320
void DlgParameterImp::onGroupSelected(QTreeWidgetItem* item)
322
if (item && item->type() == QTreeWidgetItem::UserType + 1) {
323
bool sortingEnabled = paramValue->isSortingEnabled();
325
Base::Reference<ParameterGrp> _hcGrp = static_cast<ParameterGroupItem*>(item)->_hcGrp;
326
static_cast<ParameterValue*>(paramValue)->setCurrentGroup(_hcGrp);
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()),
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()),
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()),
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()),
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()),
372
paramValue->setSortingEnabled(sortingEnabled);
376
/** Switches the type of parameters with name @a config. */
377
void DlgParameterImp::activateParameterSet(const char* config)
379
int index = ui->parameterSet->findData(QByteArray(config));
381
ui->parameterSet->setCurrentIndex(index);
382
onChangeParameterSet(index);
386
/** Switches the type of parameters either to user or system parameters. */
387
void DlgParameterImp::onChangeParameterSet(int itemPos)
389
ParameterManager* rcParMngr =
390
App::GetApplication().GetParameterSet(ui->parameterSet->itemData(itemPos).toByteArray());
395
rcParMngr->CheckDocument();
396
ui->buttonSaveToDisk->setEnabled(rcParMngr->HasSerializer());
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));
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);
418
QStringList paths = path.split(QLatin1String("."), QString::SkipEmptyParts);
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()) {
430
while (parent && !paths.empty()) {
431
parent->setExpanded(true);
432
QTreeWidgetItem* item = parent;
434
for (int index = 0; index < item->childCount(); index++) {
435
QTreeWidgetItem* child = item->child(index);
436
if (child->text(0) == paths.front()) {
445
paramGroup->setCurrentItem(parent);
447
else if (paramGroup->topLevelItemCount() > 0) {
448
paramGroup->setCurrentItem(paramGroup->topLevelItem(0));
452
void DlgParameterImp::onButtonSaveToDiskClicked()
454
int index = ui->parameterSet->currentIndex();
455
ParameterManager* parmgr =
456
App::GetApplication().GetParameterSet(ui->parameterSet->itemData(index).toByteArray());
461
parmgr->SaveDocument();
466
bool validateInput(QWidget* parent, const QString& input)
468
if (input.isEmpty()) {
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));
487
// --------------------------------------------------------------------
489
/* TRANSLATOR Gui::Dialog::ParameterGroup */
491
ParameterGroup::ParameterGroup(QWidget* parent)
492
: QTreeWidget(parent)
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);
499
menuEdit->addAction(tr("Remove group"), this, &ParameterGroup::onDeleteSelectedItem);
501
menuEdit->addAction(tr("Rename group"), this, &ParameterGroup::onRenameSelectedItem);
502
menuEdit->addSeparator();
503
exportAct = menuEdit->addAction(tr("Export parameter"), this, &ParameterGroup::onExportToFile);
505
menuEdit->addAction(tr("Import parameter"), this, &ParameterGroup::onImportFromFile);
506
menuEdit->setDefaultAction(expandAct);
509
ParameterGroup::~ParameterGroup() = default;
511
void ParameterGroup::contextMenuEvent(QContextMenuEvent* event)
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);
519
if (item->isExpanded()) {
520
expandAct->setText(tr("Collapse"));
523
expandAct->setText(tr("Expand"));
525
menuEdit->popup(event->globalPos());
529
void ParameterGroup::keyPressEvent(QKeyEvent* event)
531
if (event->matches(QKeySequence::Delete)) {
532
onDeleteSelectedItem();
535
QTreeWidget::keyPressEvent(event);
539
void ParameterGroup::onDeleteSelectedItem()
541
QTreeWidgetItem* sel = currentItem();
542
if (sel && sel->isSelected() && sel->parent()) {
543
if (QMessageBox::question(this,
545
tr("Do you really want to remove this parameter group?"),
546
QMessageBox::Yes | QMessageBox::No,
548
== QMessageBox::Yes) {
549
QTreeWidgetItem* parent = sel->parent();
550
int index = parent->indexOfChild(sel);
551
parent->takeChild(index);
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
558
auto para = static_cast<ParameterGroupItem*>(parent);
559
para->_hcGrp->RemoveGrp(groupName.c_str());
564
void ParameterGroup::onToggleSelectedItem()
566
QTreeWidgetItem* sel = currentItem();
567
if (sel && sel->isSelected()) {
568
if (sel->isExpanded()) {
569
sel->setExpanded(false);
571
else if (sel->childCount() > 0) {
572
sel->setExpanded(true);
577
void ParameterGroup::onCreateSubgroup()
580
QString name = QInputDialog::getText(this,
581
QObject::tr("New sub-group"),
582
QObject::tr("Enter the name:"),
586
Qt::MSWindowsFixedSizeDialogHint);
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;
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));
601
hGrp = hGrp->GetGroup(name.toLatin1());
602
(void)new ParameterGroupItem(para, hGrp);
608
void ParameterGroup::onExportToFile()
610
QString file = FileDialog::getSaveFileName(this,
611
tr("Export parameter to file"),
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());
624
void ParameterGroup::onImportFromFile()
626
QString file = FileDialog::getOpenFileName(this,
627
tr("Import parameter from file"),
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;
636
// remove the items and internal parameter values
637
QList<QTreeWidgetItem*> childs = para->takeChildren();
638
for (auto& child : childs) {
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);
649
para->setExpanded(para->childCount());
651
catch (const Base::Exception&) {
652
QMessageBox::critical(this,
654
tr("Reading from '%1' failed.").arg(file));
660
void ParameterGroup::onRenameSelectedItem()
662
QTreeWidgetItem* sel = currentItem();
663
if (sel && sel->isSelected()) {
668
void ParameterGroup::changeEvent(QEvent* e)
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"));
679
QTreeWidget::changeEvent(e);
683
// --------------------------------------------------------------------
685
/* TRANSLATOR Gui::Dialog::ParameterValue */
687
ParameterValue::ParameterValue(QWidget* parent)
688
: QTreeWidget(parent)
690
menuEdit = new QMenu(this);
691
changeAct = menuEdit->addAction(tr("Change value"),
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);
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);
705
menuNew->addAction(tr("New unsigned item"), this, &ParameterValue::onCreateUIntItem);
706
newBlnAct = menuNew->addAction(tr("New Boolean item"), this, &ParameterValue::onCreateBoolItem);
709
&ParameterValue::itemDoubleClicked,
711
qOverload<QTreeWidgetItem*, int>(&ParameterValue::onChangeSelectedItem));
714
ParameterValue::~ParameterValue() = default;
716
void ParameterValue::setCurrentGroup(const Base::Reference<ParameterGrp>& hGrp)
721
Base::Reference<ParameterGrp> ParameterValue::currentGroup() const
726
bool ParameterValue::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event)
728
if (index.column() > 0) {
731
return QTreeWidget::edit(index, trigger, event);
734
void ParameterValue::contextMenuEvent(QContextMenuEvent* event)
736
QTreeWidgetItem* item = currentItem();
737
if (item && item->isSelected()) {
738
menuEdit->popup(event->globalPos());
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.
745
// menuNew->popup(event->globalPos());
746
menuNew->exec(event->globalPos());
750
void ParameterValue::keyPressEvent(QKeyEvent* event)
752
if (event->matches(QKeySequence::Delete)) {
753
onDeleteSelectedItem();
756
QTreeWidget::keyPressEvent(event);
760
void ParameterValue::resizeEvent(QResizeEvent* event)
762
QHeaderView* hv = header();
763
hv->setSectionResizeMode(QHeaderView::Stretch);
765
QTreeWidget::resizeEvent(event);
767
hv->setSectionResizeMode(QHeaderView::Interactive);
770
void ParameterValue::onChangeSelectedItem(QTreeWidgetItem* item, int col)
772
if (item->isSelected() && col > 0) {
773
static_cast<ParameterValueItem*>(item)->changeValue();
777
void ParameterValue::onChangeSelectedItem()
779
onChangeSelectedItem(currentItem(), 1);
782
void ParameterValue::onDeleteSelectedItem()
784
QTreeWidgetItem* sel = currentItem();
785
if (sel && sel->isSelected()) {
786
takeTopLevelItem(indexOfTopLevelItem(sel));
787
static_cast<ParameterValueItem*>(sel)->removeFromGroup();
792
void ParameterValue::onRenameSelectedItem()
794
QTreeWidgetItem* sel = currentItem();
795
if (sel && sel->isSelected()) {
800
void ParameterValue::onCreateTextItem()
803
QString name = QInputDialog::getText(this,
804
QObject::tr("New text item"),
805
QObject::tr("Enter the name:"),
809
Qt::MSWindowsFixedSizeDialogHint);
811
if (!ok || !Gui::validateInput(this, name)) {
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,
820
tr("The item '%1' already exists.").arg(name));
825
QString val = QInputDialog::getText(this,
826
QObject::tr("New text item"),
827
QObject::tr("Enter your text:"),
831
Qt::MSWindowsFixedSizeDialogHint);
832
if (ok && !val.isEmpty()) {
833
ParameterValueItem* pcItem;
834
pcItem = new ParameterText(this, name, val.toUtf8(), _hcGrp);
835
pcItem->appendToGroup();
839
void ParameterValue::onCreateIntItem()
842
QString name = QInputDialog::getText(this,
843
QObject::tr("New integer item"),
844
QObject::tr("Enter the name:"),
848
Qt::MSWindowsFixedSizeDialogHint);
850
if (!ok || !Gui::validateInput(this, name)) {
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,
859
tr("The item '%1' already exists.").arg(name));
864
int val = QInputDialog::getInt(this,
865
QObject::tr("New integer item"),
866
QObject::tr("Enter your number:"),
872
Qt::MSWindowsFixedSizeDialogHint);
875
ParameterValueItem* pcItem;
876
pcItem = new ParameterInt(this, name, (long)val, _hcGrp);
877
pcItem->appendToGroup();
881
void ParameterValue::onCreateUIntItem()
884
QString name = QInputDialog::getText(this,
885
QObject::tr("New unsigned item"),
886
QObject::tr("Enter the name:"),
890
Qt::MSWindowsFixedSizeDialogHint);
892
if (!ok || !Gui::validateInput(this, name)) {
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,
901
tr("The item '%1' already exists.").arg(name));
906
DlgInputDialogImp dlg(QObject::tr("Enter your number:"),
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);
918
ParameterValueItem* pcItem;
919
pcItem = new ParameterUInt(this, name, val, _hcGrp);
920
pcItem->appendToGroup();
925
void ParameterValue::onCreateFloatItem()
928
QString name = QInputDialog::getText(this,
929
QObject::tr("New float item"),
930
QObject::tr("Enter the name:"),
934
Qt::MSWindowsFixedSizeDialogHint);
936
if (!ok || !Gui::validateInput(this, name)) {
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,
945
tr("The item '%1' already exists.").arg(name));
950
double val = QInputDialog::getDouble(this,
951
QObject::tr("New float item"),
952
QObject::tr("Enter your number:"),
958
Qt::MSWindowsFixedSizeDialogHint);
960
ParameterValueItem* pcItem;
961
pcItem = new ParameterFloat(this, name, val, _hcGrp);
962
pcItem->appendToGroup();
966
void ParameterValue::onCreateBoolItem()
969
QString name = QInputDialog::getText(this,
970
QObject::tr("New Boolean item"),
971
QObject::tr("Enter the name:"),
975
Qt::MSWindowsFixedSizeDialogHint);
977
if (!ok || !Gui::validateInput(this, name)) {
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,
986
tr("The item '%1' already exists.").arg(name));
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:"),
1000
Qt::MSWindowsFixedSizeDialogHint);
1002
ParameterValueItem* pcItem;
1003
pcItem = new ParameterBool(this, name, (val == list[0] ? true : false), _hcGrp);
1004
pcItem->appendToGroup();
1008
// ---------------------------------------------------------------------------
1010
ParameterGroupItem::ParameterGroupItem(ParameterGroupItem* parent,
1011
const Base::Reference<ParameterGrp>& hcGrp)
1012
: QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1)
1015
setFlags(flags() | Qt::ItemIsEditable);
1019
ParameterGroupItem::ParameterGroupItem(QTreeWidget* parent,
1020
const Base::Reference<ParameterGrp>& hcGrp)
1021
: QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1)
1024
setFlags(flags() | Qt::ItemIsEditable);
1028
ParameterGroupItem::~ParameterGroupItem()
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();
1037
void ParameterGroupItem::fillUp()
1039
// filling up groups
1040
std::vector<Base::Reference<ParameterGrp>> vhcParamGrp = _hcGrp->GetGroups();
1042
setText(0, QString::fromUtf8(_hcGrp->GetGroupName()));
1043
for (const auto& It : vhcParamGrp) {
1044
(void)new ParameterGroupItem(this, It);
1048
void ParameterGroupItem::setData(int column, int role, const QVariant& value)
1050
if (role == Qt::EditRole) {
1051
QString oldName = text(0);
1052
QString newName = value.toString();
1053
if (newName.isEmpty() || oldName == newName) {
1057
if (!Gui::validateInput(treeWidget(), newName)) {
1061
// first check if there is already a group with name "newName"
1062
auto item = static_cast<ParameterGroupItem*>(parent());
1064
QMessageBox::critical(treeWidget(),
1065
QObject::tr("Rename group"),
1066
QObject::tr("The group '%1' cannot be renamed.").arg(oldName));
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));
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())) {
1083
QTreeWidgetItem::setData(column, role, value);
1086
QVariant ParameterGroupItem::data(int column, int role) const
1088
if (role == Qt::DecorationRole) {
1089
// The root item should keep its special pixmap
1091
return this->isExpanded()
1092
? QApplication::style()->standardPixmap(QStyle::SP_DirOpenIcon)
1093
: QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon);
1097
return QTreeWidgetItem::data(column, role);
1100
// --------------------------------------------------------------------
1102
ParameterValueItem::ParameterValueItem(QTreeWidget* parent,
1103
const Base::Reference<ParameterGrp>& hcGrp)
1104
: QTreeWidgetItem(parent)
1107
setFlags(flags() | Qt::ItemIsEditable);
1110
ParameterValueItem::~ParameterValueItem() = default;
1112
void ParameterValueItem::setData(int column, int role, const QVariant& value)
1114
if (role == Qt::EditRole) {
1115
QString oldName = text(0);
1116
QString newName = value.toString();
1117
if (newName.isEmpty() || oldName == newName) {
1121
if (!Gui::validateInput(treeWidget(), newName)) {
1125
replace(oldName, newName);
1128
QTreeWidgetItem::setData(column, role, value);
1131
// --------------------------------------------------------------------
1133
ParameterText::ParameterText(QTreeWidget* parent,
1136
const Base::Reference<ParameterGrp>& hcGrp)
1137
: ParameterValueItem(parent, hcGrp)
1139
setIcon(0, BitmapFactory().iconFromTheme("Param_Text"));
1141
setText(1, QString::fromLatin1("Text"));
1142
setText(2, QString::fromUtf8(value));
1145
ParameterText::~ParameterText() = default;
1147
void ParameterText::changeValue()
1150
QString txt = QInputDialog::getText(treeWidget(),
1151
QObject::tr("Change value"),
1152
QObject::tr("Enter your text:"),
1156
Qt::MSWindowsFixedSizeDialogHint);
1159
_hcGrp->SetASCII(text(0).toLatin1(), txt.toUtf8());
1163
void ParameterText::removeFromGroup()
1165
_hcGrp->RemoveASCII(text(0).toLatin1());
1168
void ParameterText::replace(const QString& oldName, const QString& newName)
1170
std::string val = _hcGrp->GetASCII(oldName.toLatin1());
1171
_hcGrp->RemoveASCII(oldName.toLatin1());
1172
_hcGrp->SetASCII(newName.toLatin1(), val.c_str());
1175
void ParameterText::appendToGroup()
1177
_hcGrp->SetASCII(text(0).toLatin1(), text(2).toUtf8());
1180
// --------------------------------------------------------------------
1182
ParameterInt::ParameterInt(QTreeWidget* parent,
1185
const Base::Reference<ParameterGrp>& hcGrp)
1186
: ParameterValueItem(parent, hcGrp)
1188
setIcon(0, BitmapFactory().iconFromTheme("Param_Int"));
1190
setText(1, QString::fromLatin1("Integer"));
1191
setText(2, QString::fromLatin1("%1").arg(value));
1194
ParameterInt::~ParameterInt() = default;
1196
void ParameterInt::changeValue()
1199
int num = QInputDialog::getInt(treeWidget(),
1200
QObject::tr("Change value"),
1201
QObject::tr("Enter your number:"),
1207
Qt::MSWindowsFixedSizeDialogHint);
1209
setText(2, QString::fromLatin1("%1").arg(num));
1210
_hcGrp->SetInt(text(0).toLatin1(), (long)num);
1214
void ParameterInt::removeFromGroup()
1216
_hcGrp->RemoveInt(text(0).toLatin1());
1219
void ParameterInt::replace(const QString& oldName, const QString& newName)
1221
long val = _hcGrp->GetInt(oldName.toLatin1());
1222
_hcGrp->RemoveInt(oldName.toLatin1());
1223
_hcGrp->SetInt(newName.toLatin1(), val);
1226
void ParameterInt::appendToGroup()
1228
_hcGrp->SetInt(text(0).toLatin1(), text(2).toLong());
1231
// --------------------------------------------------------------------
1233
ParameterUInt::ParameterUInt(QTreeWidget* parent,
1235
unsigned long value,
1236
const Base::Reference<ParameterGrp>& hcGrp)
1237
: ParameterValueItem(parent, hcGrp)
1239
setIcon(0, BitmapFactory().iconFromTheme("Param_UInt"));
1241
setText(1, QString::fromLatin1("Unsigned"));
1242
setText(2, QString::fromLatin1("%1").arg(value));
1245
ParameterUInt::~ParameterUInt() = default;
1247
void ParameterUInt::changeValue()
1250
DlgInputDialogImp dlg(QObject::tr("Enter your number:"),
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);
1263
setText(2, QString::fromLatin1("%1").arg(num));
1264
_hcGrp->SetUnsigned(text(0).toLatin1(), (unsigned long)num);
1269
void ParameterUInt::removeFromGroup()
1271
_hcGrp->RemoveUnsigned(text(0).toLatin1());
1274
void ParameterUInt::replace(const QString& oldName, const QString& newName)
1276
unsigned long val = _hcGrp->GetUnsigned(oldName.toLatin1());
1277
_hcGrp->RemoveUnsigned(oldName.toLatin1());
1278
_hcGrp->SetUnsigned(newName.toLatin1(), val);
1281
void ParameterUInt::appendToGroup()
1283
_hcGrp->SetUnsigned(text(0).toLatin1(), text(2).toULong());
1286
// --------------------------------------------------------------------
1288
ParameterFloat::ParameterFloat(QTreeWidget* parent,
1291
const Base::Reference<ParameterGrp>& hcGrp)
1292
: ParameterValueItem(parent, hcGrp)
1294
setIcon(0, BitmapFactory().iconFromTheme("Param_Float"));
1296
setText(1, QString::fromLatin1("Float"));
1297
setText(2, QString::fromLatin1("%1").arg(value));
1300
ParameterFloat::~ParameterFloat() = default;
1302
void ParameterFloat::changeValue()
1305
double num = QInputDialog::getDouble(treeWidget(),
1306
QObject::tr("Change value"),
1307
QObject::tr("Enter your number:"),
1313
Qt::MSWindowsFixedSizeDialogHint);
1315
setText(2, QString::fromLatin1("%1").arg(num));
1316
_hcGrp->SetFloat(text(0).toLatin1(), num);
1320
void ParameterFloat::removeFromGroup()
1322
_hcGrp->RemoveFloat(text(0).toLatin1());
1325
void ParameterFloat::replace(const QString& oldName, const QString& newName)
1327
double val = _hcGrp->GetFloat(oldName.toLatin1());
1328
_hcGrp->RemoveFloat(oldName.toLatin1());
1329
_hcGrp->SetFloat(newName.toLatin1(), val);
1332
void ParameterFloat::appendToGroup()
1334
_hcGrp->SetFloat(text(0).toLatin1(), text(2).toDouble());
1337
// --------------------------------------------------------------------
1339
ParameterBool::ParameterBool(QTreeWidget* parent,
1342
const Base::Reference<ParameterGrp>& hcGrp)
1343
: ParameterValueItem(parent, hcGrp)
1345
setIcon(0, BitmapFactory().iconFromTheme("Param_Bool"));
1347
setText(1, QString::fromLatin1("Boolean"));
1348
setText(2, QString::fromLatin1((value ? "true" : "false")));
1351
ParameterBool::~ParameterBool() = default;
1353
void ParameterBool::changeValue()
1357
list << QString::fromLatin1("true") << QString::fromLatin1("false");
1358
int pos = (text(2) == list[0] ? 0 : 1);
1360
QString txt = QInputDialog::getItem(treeWidget(),
1361
QObject::tr("Change value"),
1362
QObject::tr("Choose an item:"),
1367
Qt::MSWindowsFixedSizeDialogHint);
1370
_hcGrp->SetBool(text(0).toLatin1(), (txt == list[0] ? true : false));
1374
void ParameterBool::removeFromGroup()
1376
_hcGrp->RemoveBool(text(0).toLatin1());
1379
void ParameterBool::replace(const QString& oldName, const QString& newName)
1381
bool val = _hcGrp->GetBool(oldName.toLatin1());
1382
_hcGrp->RemoveBool(oldName.toLatin1());
1383
_hcGrp->SetBool(newName.toLatin1(), val);
1386
void ParameterBool::appendToGroup()
1388
bool val = (text(2) == QLatin1String("true") ? true : false);
1389
_hcGrp->SetBool(text(0).toLatin1(), val);
1392
#include "moc_DlgParameterImp.cpp"