| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 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 <limits> |
| 24 |
#include <sstream> |
| 25 |
|
| 26 |
#include <QByteArray> |
| 27 |
#include <QContextMenuEvent> |
| 28 |
#include <QHeaderView> |
| 29 |
#include <QInputDialog> |
| 30 |
#include <QMessageBox> |
| 31 |
#include <QMenu> |
| 32 |
#include <QTreeWidget> |
| 33 |
|
| 34 |
#include <App/Application.h> |
| 35 |
#include <Base/Exception.h> |
| 36 |
#include <Base/Parameter.h> |
| 37 |
|
| 38 |
#include "Dialogs/DlgParameterImp.h" |
| 39 |
#include "ui_DlgParameter.h" |
| 40 |
#include "BitmapFactory.h" |
| 41 |
#include "Dialogs/DlgParameterFind.h" |
| 42 |
#include "Dialogs/DlgInputDialogImp.h" |
| 43 |
#include "FileDialog.h" |
| 44 |
#include "SpinBox.h" |
| 45 |
|
| 46 |
|
| 47 |
using namespace Gui::Dialog; |
| 48 |
|
| 49 |
/* TRANSLATOR Gui::Dialog::DlgParameterImp */ |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructs a DlgParameterImp which is a child of 'parent', with the |
| 53 |
* name 'name' and widget flags set to 'f' |
| 54 |
* |
| 55 |
* The dialog will by default be modeless, unless you set 'modal' to |
| 56 |
* true to construct a modal dialog. |
| 57 |
*/ |
| 58 |
DlgParameterImp::DlgParameterImp(QWidget* parent, Qt::WindowFlags fl) |
| 59 |
: QDialog(parent, fl | Qt::WindowMinMaxButtonsHint) |
| 60 |
, ui(new Ui_DlgParameter) |
| 61 |
{ |
| 62 |
ui->setupUi(this); |
| 63 |
setupConnections(); |
| 64 |
|
| 65 |
ui->checkSort->setVisible(false); // for testing |
| 66 |
|
| 67 |
QStringList groupLabels; |
| 68 |
groupLabels << tr("Group"); |
| 69 |
paramGroup = new ParameterGroup(ui->splitter3); |
| 70 |
paramGroup->setHeaderLabels(groupLabels); |
| 71 |
paramGroup->setRootIsDecorated(false); |
| 72 |
paramGroup->setSortingEnabled(true); |
| 73 |
paramGroup->sortByColumn(0, Qt::AscendingOrder); |
| 74 |
paramGroup->header()->setProperty("showSortIndicator", QVariant(true)); |
| 75 |
|
| 76 |
QStringList valueLabels; |
| 77 |
valueLabels << tr("Name") << tr("Type") << tr("Value"); |
| 78 |
paramValue = new ParameterValue(ui->splitter3); |
| 79 |
paramValue->setHeaderLabels(valueLabels); |
| 80 |
paramValue->setRootIsDecorated(false); |
| 81 |
paramValue->header()->setSectionResizeMode(0, QHeaderView::Stretch); |
| 82 |
paramValue->setSortingEnabled(true); |
| 83 |
paramValue->sortByColumn(0, Qt::AscendingOrder); |
| 84 |
paramValue->header()->setProperty("showSortIndicator", QVariant(true)); |
| 85 |
|
| 86 |
QSizePolicy policy = paramValue->sizePolicy(); |
| 87 |
policy.setHorizontalStretch(3); |
| 88 |
paramValue->setSizePolicy(policy); |
| 89 |
|
| 90 |
#if 0 // This is needed for Qt's lupdate |
| 91 |
qApp->translate( "Gui::Dialog::DlgParameterImp", "System parameter" ); |
| 92 |
qApp->translate( "Gui::Dialog::DlgParameterImp", "User parameter" ); |
| 93 |
#endif |
| 94 |
|
| 95 |
ParameterManager* sys = App::GetApplication().GetParameterSet("System parameter"); |
| 96 |
const auto& rcList = App::GetApplication().GetParameterSetList(); |
| 97 |
for (const auto& it : rcList) { |
| 98 |
if (it.second != sys) { // for now ignore system parameters because they are nowhere used |
| 99 |
ui->parameterSet->addItem(tr(it.first.c_str()), QVariant(QByteArray(it.first.c_str()))); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
QByteArray cStr("User parameter"); |
| 104 |
ui->parameterSet->setCurrentIndex(ui->parameterSet->findData(cStr)); |
| 105 |
onChangeParameterSet(ui->parameterSet->currentIndex()); |
| 106 |
if (ui->parameterSet->count() < 2) { |
| 107 |
ui->parameterSet->hide(); |
| 108 |
} |
| 109 |
|
| 110 |
connect( |
| 111 |
ui->parameterSet, |
| 112 |
qOverload<int>(&QComboBox::activated), |
| 113 |
this, |
| 114 |
&DlgParameterImp::onChangeParameterSet |
| 115 |
); |
| 116 |
connect(paramGroup, &QTreeWidget::currentItemChanged, this, &DlgParameterImp::onGroupSelected); |
| 117 |
onGroupSelected(paramGroup->currentItem()); |
| 118 |
|
| 119 |
// setup for function on_findGroup_changed: |
| 120 |
// store the current font properties because |
| 121 |
// we don't know what style sheet the user uses for FC |
| 122 |
defaultFont = paramGroup->font(); |
| 123 |
boldFont = defaultFont; |
| 124 |
boldFont.setBold(true); |
| 125 |
defaultColor = paramGroup->topLevelItem(0)->foreground(0); |
| 126 |
|
| 127 |
ui->findGroupLE->setPlaceholderText(tr("Search group")); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Destroys the object and frees any allocated resources |
| 132 |
*/ |
| 133 |
DlgParameterImp::~DlgParameterImp() |
| 134 |
{ |
| 135 |
// no need to delete child widgets, Qt does it all for us |
| 136 |
delete ui; |
| 137 |
} |
| 138 |
|
| 139 |
void DlgParameterImp::setupConnections() |
| 140 |
{ |
| 141 |
// clang-format off |
| 142 |
connect(ui->buttonFind, &QPushButton::clicked, |
| 143 |
this, &DlgParameterImp::onButtonFindClicked); |
| 144 |
connect(ui->findGroupLE, &QLineEdit::textChanged, |
| 145 |
this, &DlgParameterImp::onFindGroupTtextChanged); |
| 146 |
connect(ui->buttonSaveToDisk, &QPushButton::clicked, |
| 147 |
this, &DlgParameterImp::onButtonSaveToDiskClicked); |
| 148 |
connect(ui->closeButton, &QPushButton::clicked, |
| 149 |
this, &DlgParameterImp::onCloseButtonClicked); |
| 150 |
connect(ui->checkSort, &QCheckBox::toggled, |
| 151 |
this, &DlgParameterImp::onCheckSortToggled); |
| 152 |
// clang-format on |
| 153 |
} |
| 154 |
|
| 155 |
void DlgParameterImp::onButtonFindClicked() |
| 156 |
{ |
| 157 |
if (finder.isNull()) { |
| 158 |
finder = new DlgParameterFind(this); |
| 159 |
} |
| 160 |
finder->show(); |
| 161 |
} |
| 162 |
|
| 163 |
void DlgParameterImp::onFindGroupTtextChanged(const QString& SearchStr) |
| 164 |
{ |
| 165 |
// search for group tree items and highlight found results |
| 166 |
|
| 167 |
QTreeWidgetItem* ExpandItem; |
| 168 |
|
| 169 |
// at first reset all items to the default font and expand state |
| 170 |
if (!foundList.empty()) { |
| 171 |
for (QTreeWidgetItem* item : std::as_const(foundList)) { |
| 172 |
item->setFont(0, defaultFont); |
| 173 |
item->setForeground(0, defaultColor); |
| 174 |
ExpandItem = item; |
| 175 |
// a group can be nested down to several levels |
| 176 |
// do not collapse if the search string is empty |
| 177 |
while (!SearchStr.isEmpty()) { |
| 178 |
if (!ExpandItem->parent()) { |
| 179 |
break; |
| 180 |
} |
| 181 |
else { |
| 182 |
ExpandItem->setExpanded(false); |
| 183 |
ExpandItem = ExpandItem->parent(); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
// expand the top level entries to get the initial tree state |
| 189 |
for (int i = 0; i < paramGroup->topLevelItemCount(); ++i) { |
| 190 |
paramGroup->topLevelItem(i)->setExpanded(true); |
| 191 |
} |
| 192 |
|
| 193 |
// don't perform a search if the string is empty |
| 194 |
if (SearchStr.isEmpty()) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
// search the tree widget |
| 199 |
foundList = paramGroup->findItems(SearchStr, Qt::MatchContains | Qt::MatchRecursive); |
| 200 |
if (!foundList.empty()) { |
| 201 |
// reset background style sheet |
| 202 |
if (!ui->findGroupLE->styleSheet().isEmpty()) { |
| 203 |
ui->findGroupLE->setStyleSheet(QString()); |
| 204 |
} |
| 205 |
for (QTreeWidgetItem* item : std::as_const(foundList)) { |
| 206 |
item->setFont(0, boldFont); |
| 207 |
item->setForeground(0, Qt::red); |
| 208 |
// expand its parent to see the item |
| 209 |
// a group can be nested down to several levels |
| 210 |
ExpandItem = item; |
| 211 |
while (true) { |
| 212 |
if (!ExpandItem->parent()) { |
| 213 |
break; |
| 214 |
} |
| 215 |
else { |
| 216 |
ExpandItem->setExpanded(true); |
| 217 |
ExpandItem = ExpandItem->parent(); |
| 218 |
} |
| 219 |
} |
| 220 |
// if there is only one item found, scroll to it |
| 221 |
if (foundList.size() == 1) { |
| 222 |
paramGroup->scrollToItem(foundList[0], QAbstractItemView::PositionAtCenter); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
else { |
| 227 |
// Set red background to indicate no matching |
| 228 |
QString styleSheet = QStringLiteral( |
| 229 |
" QLineEdit {\n" |
| 230 |
" background-color: rgb(221,144,161);\n" |
| 231 |
" }\n" |
| 232 |
); |
| 233 |
ui->findGroupLE->setStyleSheet(styleSheet); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Sets the strings of the subwidgets using the current |
| 239 |
* language. |
| 240 |
*/ |
| 241 |
void DlgParameterImp::changeEvent(QEvent* e) |
| 242 |
{ |
| 243 |
if (e->type() == QEvent::LanguageChange) { |
| 244 |
ui->retranslateUi(this); |
| 245 |
paramGroup->headerItem()->setText(0, tr("Group")); |
| 246 |
paramValue->headerItem()->setText(0, tr("Name")); |
| 247 |
paramValue->headerItem()->setText(1, tr("Type")); |
| 248 |
paramValue->headerItem()->setText(2, tr("Value")); |
| 249 |
} |
| 250 |
else { |
| 251 |
QDialog::changeEvent(e); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void DlgParameterImp::onCheckSortToggled(bool on) |
| 256 |
{ |
| 257 |
paramGroup->setSortingEnabled(on); |
| 258 |
paramGroup->sortByColumn(0, Qt::AscendingOrder); |
| 259 |
paramGroup->header()->setProperty("showSortIndicator", QVariant(on)); |
| 260 |
|
| 261 |
paramValue->setSortingEnabled(on); |
| 262 |
paramValue->sortByColumn(0, Qt::AscendingOrder); |
| 263 |
paramValue->header()->setProperty("showSortIndicator", QVariant(on)); |
| 264 |
} |
| 265 |
|
| 266 |
void DlgParameterImp::onCloseButtonClicked() |
| 267 |
{ |
| 268 |
close(); |
| 269 |
} |
| 270 |
|
| 271 |
void DlgParameterImp::accept() |
| 272 |
{ |
| 273 |
close(); |
| 274 |
} |
| 275 |
|
| 276 |
void DlgParameterImp::reject() |
| 277 |
{ |
| 278 |
close(); |
| 279 |
} |
| 280 |
|
| 281 |
void DlgParameterImp::showEvent(QShowEvent*) |
| 282 |
{ |
| 283 |
ParameterGrp::handle hGrp |
| 284 |
= App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences"); |
| 285 |
hGrp = hGrp->GetGroup("ParameterEditor"); |
| 286 |
std::string buf = hGrp->GetASCII("Geometry", ""); |
| 287 |
if (!buf.empty()) { |
| 288 |
int x1, y1, x2, y2; |
| 289 |
char sep; |
| 290 |
std::stringstream str(buf); |
| 291 |
str >> sep >> x1 >> sep >> y1 >> sep >> x2 >> sep >> y2; |
| 292 |
QRect rect; |
| 293 |
rect.setCoords(x1, y1, x2, y2); |
| 294 |
this->setGeometry(rect); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void DlgParameterImp::closeEvent(QCloseEvent*) |
| 299 |
{ |
| 300 |
ParameterGrp::handle hGrp |
| 301 |
= App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences"); |
| 302 |
hGrp = hGrp->GetGroup("ParameterEditor"); |
| 303 |
QTreeWidgetItem* current = paramGroup->currentItem(); |
| 304 |
if (current) { |
| 305 |
QStringList paths; |
| 306 |
paths << current->text(0); |
| 307 |
QTreeWidgetItem* parent = current->parent(); |
| 308 |
while (parent) { |
| 309 |
paths.push_front(parent->text(0)); |
| 310 |
parent = parent->parent(); |
| 311 |
} |
| 312 |
|
| 313 |
QString path = paths.join(QLatin1String(".")); |
| 314 |
hGrp->SetASCII("LastParameterGroup", (const char*)path.toUtf8()); |
| 315 |
// save geometry of window |
| 316 |
const QRect& r = this->geometry(); |
| 317 |
std::stringstream str; |
| 318 |
str << "(" << r.left() << "," << r.top() << "," << r.right() << "," << r.bottom() << ")"; |
| 319 |
hGrp->SetASCII("Geometry", str.str().c_str()); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
void DlgParameterImp::onGroupSelected(QTreeWidgetItem* item) |
| 324 |
{ |
| 325 |
if (item && item->type() == QTreeWidgetItem::UserType + 1) { |
| 326 |
bool sortingEnabled = paramValue->isSortingEnabled(); |
| 327 |
paramValue->clear(); |
| 328 |
Base::Reference<ParameterGrp> _hcGrp = static_cast<ParameterGroupItem*>(item)->_hcGrp; |
| 329 |
static_cast<ParameterValue*>(paramValue)->setCurrentGroup(_hcGrp); |
| 330 |
|
| 331 |
// filling up Text nodes |
| 332 |
std::vector<std::pair<std::string, std::string>> mcTextMap = _hcGrp->GetASCIIMap(); |
| 333 |
for (const auto& It2 : mcTextMap) { |
| 334 |
(void)new ParameterText( |
| 335 |
paramValue, |
| 336 |
QString::fromUtf8(It2.first.c_str()), |
| 337 |
It2.second.c_str(), |
| 338 |
_hcGrp |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
// filling up Int nodes |
| 343 |
std::vector<std::pair<std::string, long>> mcIntMap = _hcGrp->GetIntMap(); |
| 344 |
for (const auto& It3 : mcIntMap) { |
| 345 |
( |
| 346 |
void |
| 347 |
)new ParameterInt(paramValue, QString::fromUtf8(It3.first.c_str()), It3.second, _hcGrp); |
| 348 |
} |
| 349 |
|
| 350 |
// filling up Float nodes |
| 351 |
std::vector<std::pair<std::string, double>> mcFloatMap = _hcGrp->GetFloatMap(); |
| 352 |
for (const auto& It4 : mcFloatMap) { |
| 353 |
( |
| 354 |
void |
| 355 |
)new ParameterFloat(paramValue, QString::fromUtf8(It4.first.c_str()), It4.second, _hcGrp); |
| 356 |
} |
| 357 |
|
| 358 |
// filling up bool nodes |
| 359 |
std::vector<std::pair<std::string, bool>> mcBoolMap = _hcGrp->GetBoolMap(); |
| 360 |
for (const auto& It5 : mcBoolMap) { |
| 361 |
( |
| 362 |
void |
| 363 |
)new ParameterBool(paramValue, QString::fromUtf8(It5.first.c_str()), It5.second, _hcGrp); |
| 364 |
} |
| 365 |
|
| 366 |
// filling up UInt nodes |
| 367 |
std::vector<std::pair<std::string, unsigned long>> mcUIntMap = _hcGrp->GetUnsignedMap(); |
| 368 |
for (const auto& It6 : mcUIntMap) { |
| 369 |
( |
| 370 |
void |
| 371 |
)new ParameterUInt(paramValue, QString::fromUtf8(It6.first.c_str()), It6.second, _hcGrp); |
| 372 |
} |
| 373 |
paramValue->setSortingEnabled(sortingEnabled); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/** Switches the type of parameters with name @a config. */ |
| 378 |
void DlgParameterImp::activateParameterSet(const char* config) |
| 379 |
{ |
| 380 |
int index = ui->parameterSet->findData(QByteArray(config)); |
| 381 |
if (index != -1) { |
| 382 |
ui->parameterSet->setCurrentIndex(index); |
| 383 |
onChangeParameterSet(index); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/** Switches the type of parameters either to user or system parameters. */ |
| 388 |
void DlgParameterImp::onChangeParameterSet(int itemPos) |
| 389 |
{ |
| 390 |
ParameterManager* rcParMngr = App::GetApplication().GetParameterSet( |
| 391 |
ui->parameterSet->itemData(itemPos).toByteArray() |
| 392 |
); |
| 393 |
if (!rcParMngr) { |
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
rcParMngr->CheckDocument(); |
| 398 |
ui->buttonSaveToDisk->setEnabled(rcParMngr->HasSerializer()); |
| 399 |
|
| 400 |
// remove all labels |
| 401 |
paramGroup->clear(); |
| 402 |
paramValue->clear(); |
| 403 |
|
| 404 |
// root labels |
| 405 |
std::vector<Base::Reference<ParameterGrp>> grps = rcParMngr->GetGroups(); |
| 406 |
for (const auto& grp : grps) { |
| 407 |
auto item = new ParameterGroupItem(paramGroup, grp); |
| 408 |
paramGroup->expandItem(item); |
| 409 |
item->setIcon(0, QApplication::style()->standardPixmap(QStyle::SP_ComputerIcon)); |
| 410 |
} |
| 411 |
|
| 412 |
// get the path of the last selected group in the editor |
| 413 |
ParameterGrp::handle hGrp |
| 414 |
= App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences"); |
| 415 |
hGrp = hGrp->GetGroup("ParameterEditor"); |
| 416 |
QString path = QString::fromUtf8(hGrp->GetASCII("LastParameterGroup").c_str()); |
| 417 |
QStringList paths = path.split(QLatin1String("."), Qt::SkipEmptyParts); |
| 418 |
|
| 419 |
QTreeWidgetItem* parent = nullptr; |
| 420 |
for (int index = 0; index < paramGroup->topLevelItemCount() && !paths.empty(); index++) { |
| 421 |
QTreeWidgetItem* child = paramGroup->topLevelItem(index); |
| 422 |
if (child->text(0) == paths.front()) { |
| 423 |
paths.pop_front(); |
| 424 |
parent = child; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
while (parent && !paths.empty()) { |
| 429 |
parent->setExpanded(true); |
| 430 |
QTreeWidgetItem* item = parent; |
| 431 |
parent = nullptr; |
| 432 |
for (int index = 0; index < item->childCount(); index++) { |
| 433 |
QTreeWidgetItem* child = item->child(index); |
| 434 |
if (child->text(0) == paths.front()) { |
| 435 |
paths.pop_front(); |
| 436 |
parent = child; |
| 437 |
break; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if (parent) { |
| 443 |
paramGroup->setCurrentItem(parent); |
| 444 |
} |
| 445 |
else if (paramGroup->topLevelItemCount() > 0) { |
| 446 |
paramGroup->setCurrentItem(paramGroup->topLevelItem(0)); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
void DlgParameterImp::onButtonSaveToDiskClicked() |
| 451 |
{ |
| 452 |
int index = ui->parameterSet->currentIndex(); |
| 453 |
ParameterManager* parmgr = App::GetApplication().GetParameterSet( |
| 454 |
ui->parameterSet->itemData(index).toByteArray() |
| 455 |
); |
| 456 |
if (!parmgr) { |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
parmgr->SaveDocument(); |
| 461 |
} |
| 462 |
|
| 463 |
namespace Gui |
| 464 |
{ |
| 465 |
bool validateInput(QWidget* parent, const QString& input) |
| 466 |
{ |
| 467 |
if (input.isEmpty()) { |
| 468 |
return false; |
| 469 |
} |
| 470 |
for (int i = 0; i < input.size(); i++) { |
| 471 |
const char c = input.at(i).toLatin1(); |
| 472 |
if ((c < '0' || c > '9') && // Numbers |
| 473 |
(c < 'A' || c > 'Z') && // Uppercase letters |
| 474 |
(c < 'a' || c > 'z') && // Lowercase letters |
| 475 |
(c != ' ')) { // Space |
| 476 |
QMessageBox::warning( |
| 477 |
parent, |
| 478 |
DlgParameterImp::tr("Invalid input"), |
| 479 |
DlgParameterImp::tr("Invalid key name '%1'").arg(input) |
| 480 |
); |
| 481 |
return false; |
| 482 |
} |
| 483 |
} |
| 484 |
return true; |
| 485 |
} |
| 486 |
} // namespace Gui |
| 487 |
|
| 488 |
// -------------------------------------------------------------------- |
| 489 |
|
| 490 |
/* TRANSLATOR Gui::Dialog::ParameterGroup */ |
| 491 |
|
| 492 |
ParameterGroup::ParameterGroup(QWidget* parent) |
| 493 |
: QTreeWidget(parent) |
| 494 |
{ |
| 495 |
menuEdit = new QMenu(this); |
| 496 |
expandAct = menuEdit->addAction(tr("Expand"), this, &ParameterGroup::onToggleSelectedItem); |
| 497 |
menuEdit->addSeparator(); |
| 498 |
subGrpAct = menuEdit->addAction(tr("Add Sub-Group"), this, &ParameterGroup::onCreateSubgroup); |
| 499 |
removeAct = menuEdit->addAction(tr("Remove Group"), this, &ParameterGroup::onDeleteSelectedItem); |
| 500 |
renameAct = menuEdit->addAction(tr("Rename Group"), this, &ParameterGroup::onRenameSelectedItem); |
| 501 |
menuEdit->addSeparator(); |
| 502 |
exportAct = menuEdit->addAction(tr("Export Parameter"), this, &ParameterGroup::onExportToFile); |
| 503 |
importAct = menuEdit->addAction(tr("Import Parameter"), this, &ParameterGroup::onImportFromFile); |
| 504 |
menuEdit->setDefaultAction(expandAct); |
| 505 |
} |
| 506 |
|
| 507 |
ParameterGroup::~ParameterGroup() = default; |
| 508 |
|
| 509 |
void ParameterGroup::contextMenuEvent(QContextMenuEvent* event) |
| 510 |
{ |
| 511 |
QTreeWidgetItem* item = currentItem(); |
| 512 |
if (item && item->isSelected()) { |
| 513 |
expandAct->setEnabled(item->childCount() > 0); |
| 514 |
// do not allow one to import parameters from a non-empty parameter group |
| 515 |
importAct->setEnabled(item->childCount() == 0); |
| 516 |
|
| 517 |
if (item->isExpanded()) { |
| 518 |
expandAct->setText(tr("Collapse")); |
| 519 |
} |
| 520 |
else { |
| 521 |
expandAct->setText(tr("Expand")); |
| 522 |
} |
| 523 |
menuEdit->popup(event->globalPos()); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void ParameterGroup::keyPressEvent(QKeyEvent* event) |
| 528 |
{ |
| 529 |
if (event->matches(QKeySequence::Delete)) { |
| 530 |
onDeleteSelectedItem(); |
| 531 |
} |
| 532 |
else { |
| 533 |
QTreeWidget::keyPressEvent(event); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
void ParameterGroup::onDeleteSelectedItem() |
| 538 |
{ |
| 539 |
QTreeWidgetItem* sel = currentItem(); |
| 540 |
if (sel && sel->isSelected() && sel->parent()) { |
| 541 |
if (QMessageBox::question( |
| 542 |
this, |
| 543 |
tr("Remove group"), |
| 544 |
tr("Remove this parameter group?"), |
| 545 |
QMessageBox::Yes | QMessageBox::No, |
| 546 |
QMessageBox::No |
| 547 |
) |
| 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( |
| 581 |
this, |
| 582 |
QObject::tr("New sub-group"), |
| 583 |
QObject::tr("Enter the name:"), |
| 584 |
QLineEdit::Normal, |
| 585 |
QString(), |
| 586 |
&ok, |
| 587 |
Qt::MSWindowsFixedSizeDialogHint |
| 588 |
); |
| 589 |
|
| 590 |
if (ok && Gui::validateInput(this, name)) { |
| 591 |
QTreeWidgetItem* item = currentItem(); |
| 592 |
if (item && item->isSelected()) { |
| 593 |
auto para = static_cast<ParameterGroupItem*>(item); |
| 594 |
Base::Reference<ParameterGrp> hGrp = para->_hcGrp; |
| 595 |
|
| 596 |
if (hGrp->HasGroup(name.toLatin1())) { |
| 597 |
QMessageBox::critical( |
| 598 |
this, |
| 599 |
tr("Existing sub-group"), |
| 600 |
tr("The sub-group '%1' already exists.").arg(name) |
| 601 |
); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
hGrp = hGrp->GetGroup(name.toLatin1()); |
| 606 |
(void)new ParameterGroupItem(para, hGrp); |
| 607 |
expandItem(para); |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
void ParameterGroup::onExportToFile() |
| 613 |
{ |
| 614 |
QString file = FileDialog::getSaveFileName( |
| 615 |
this, |
| 616 |
tr("Export parameter to file"), |
| 617 |
QString(), |
| 618 |
QStringLiteral("XML (*.FCParam)") |
| 619 |
); |
| 620 |
if (!file.isEmpty()) { |
| 621 |
QTreeWidgetItem* item = currentItem(); |
| 622 |
if (item && item->isSelected()) { |
| 623 |
auto para = static_cast<ParameterGroupItem*>(item); |
| 624 |
Base::Reference<ParameterGrp> hGrp = para->_hcGrp; |
| 625 |
hGrp->exportTo(file.toUtf8()); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void ParameterGroup::onImportFromFile() |
| 631 |
{ |
| 632 |
QString file = FileDialog::getOpenFileName( |
| 633 |
this, |
| 634 |
tr("Import parameter from file"), |
| 635 |
QString(), |
| 636 |
QStringLiteral("XML (*.FCParam)") |
| 637 |
); |
| 638 |
if (!file.isEmpty()) { |
| 639 |
QTreeWidgetItem* item = currentItem(); |
| 640 |
if (item && item->isSelected()) { |
| 641 |
auto para = static_cast<ParameterGroupItem*>(item); |
| 642 |
Base::Reference<ParameterGrp> hGrp = para->_hcGrp; |
| 643 |
|
| 644 |
// remove the items and internal parameter values |
| 645 |
QList<QTreeWidgetItem*> childs = para->takeChildren(); |
| 646 |
for (auto& child : childs) { |
| 647 |
delete child; |
| 648 |
} |
| 649 |
|
| 650 |
try { |
| 651 |
hGrp->importFrom(file.toUtf8()); |
| 652 |
std::vector<Base::Reference<ParameterGrp>> cSubGrps = hGrp->GetGroups(); |
| 653 |
for (const auto& cSubGrp : cSubGrps) { |
| 654 |
new ParameterGroupItem(para, cSubGrp); |
| 655 |
} |
| 656 |
|
| 657 |
para->setExpanded(para->childCount()); |
| 658 |
} |
| 659 |
catch (const Base::Exception&) { |
| 660 |
QMessageBox::critical( |
| 661 |
this, |
| 662 |
tr("Import error"), |
| 663 |
tr("Reading from '%1' failed.").arg(file) |
| 664 |
); |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
void ParameterGroup::onRenameSelectedItem() |
| 671 |
{ |
| 672 |
QTreeWidgetItem* sel = currentItem(); |
| 673 |
if (sel && sel->isSelected()) { |
| 674 |
editItem(sel, 0); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
void ParameterGroup::changeEvent(QEvent* e) |
| 679 |
{ |
| 680 |
if (e->type() == QEvent::LanguageChange) { |
| 681 |
expandAct->setText(tr("Expand")); |
| 682 |
subGrpAct->setText(tr("Add sub-group")); |
| 683 |
removeAct->setText(tr("Remove group")); |
| 684 |
renameAct->setText(tr("Rename group")); |
| 685 |
exportAct->setText(tr("Export parameter")); |
| 686 |
importAct->setText(tr("Import parameter")); |
| 687 |
} |
| 688 |
else { |
| 689 |
QTreeWidget::changeEvent(e); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
// -------------------------------------------------------------------- |
| 694 |
|
| 695 |
/* TRANSLATOR Gui::Dialog::ParameterValue */ |
| 696 |
|
| 697 |
ParameterValue::ParameterValue(QWidget* parent) |
| 698 |
: QTreeWidget(parent) |
| 699 |
{ |
| 700 |
menuEdit = new QMenu(this); |
| 701 |
changeAct = menuEdit->addAction( |
| 702 |
tr("Change Value"), |
| 703 |
this, |
| 704 |
qOverload<>(&ParameterValue::onChangeSelectedItem) |
| 705 |
); |
| 706 |
menuEdit->addSeparator(); |
| 707 |
removeAct = menuEdit->addAction(tr("Remove Key"), this, &ParameterValue::onDeleteSelectedItem); |
| 708 |
renameAct = menuEdit->addAction(tr("Rename Key"), this, &ParameterValue::onRenameSelectedItem); |
| 709 |
menuEdit->setDefaultAction(changeAct); |
| 710 |
|
| 711 |
menuEdit->addSeparator(); |
| 712 |
menuNew = menuEdit->addMenu(tr("New")); |
| 713 |
newStrAct = menuNew->addAction(tr("New String Item"), this, &ParameterValue::onCreateTextItem); |
| 714 |
newFltAct = menuNew->addAction(tr("New Float Item"), this, &ParameterValue::onCreateFloatItem); |
| 715 |
newIntAct = menuNew->addAction(tr("New Integer Item"), this, &ParameterValue::onCreateIntItem); |
| 716 |
newUlgAct = menuNew->addAction(tr("New Unsigned Item"), this, &ParameterValue::onCreateUIntItem); |
| 717 |
newBlnAct = menuNew->addAction(tr("New Boolean Item"), this, &ParameterValue::onCreateBoolItem); |
| 718 |
|
| 719 |
connect( |
| 720 |
this, |
| 721 |
&ParameterValue::itemDoubleClicked, |
| 722 |
this, |
| 723 |
qOverload<QTreeWidgetItem*, int>(&ParameterValue::onChangeSelectedItem) |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
ParameterValue::~ParameterValue() = default; |
| 728 |
|
| 729 |
void ParameterValue::setCurrentGroup(const Base::Reference<ParameterGrp>& hGrp) |
| 730 |
{ |
| 731 |
_hcGrp = hGrp; |
| 732 |
} |
| 733 |
|
| 734 |
Base::Reference<ParameterGrp> ParameterValue::currentGroup() const |
| 735 |
{ |
| 736 |
return _hcGrp; |
| 737 |
} |
| 738 |
|
| 739 |
bool ParameterValue::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event) |
| 740 |
{ |
| 741 |
if (index.column() > 0) { |
| 742 |
return false; |
| 743 |
} |
| 744 |
return QTreeWidget::edit(index, trigger, event); |
| 745 |
} |
| 746 |
|
| 747 |
void ParameterValue::contextMenuEvent(QContextMenuEvent* event) |
| 748 |
{ |
| 749 |
QTreeWidgetItem* item = currentItem(); |
| 750 |
if (item && item->isSelected()) { |
| 751 |
menuEdit->popup(event->globalPos()); |
| 752 |
} |
| 753 |
else { |
| 754 |
// There is a regression in Qt 5.12.9 where it isn't checked that a sub-menu (here menuNew) |
| 755 |
// can be popped up without its parent menu (menuEdit) and thus causes a crash. |
| 756 |
// A workaround is to simply call exec() instead. |
| 757 |
// |
| 758 |
// menuNew->popup(event->globalPos()); |
| 759 |
menuNew->exec(event->globalPos()); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
void ParameterValue::keyPressEvent(QKeyEvent* event) |
| 764 |
{ |
| 765 |
if (event->matches(QKeySequence::Delete)) { |
| 766 |
onDeleteSelectedItem(); |
| 767 |
} |
| 768 |
else { |
| 769 |
QTreeWidget::keyPressEvent(event); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
void ParameterValue::resizeEvent(QResizeEvent* event) |
| 774 |
{ |
| 775 |
QHeaderView* hv = header(); |
| 776 |
hv->setSectionResizeMode(QHeaderView::Stretch); |
| 777 |
|
| 778 |
QTreeWidget::resizeEvent(event); |
| 779 |
|
| 780 |
hv->setSectionResizeMode(QHeaderView::Interactive); |
| 781 |
} |
| 782 |
|
| 783 |
void ParameterValue::onChangeSelectedItem(QTreeWidgetItem* item, int col) |
| 784 |
{ |
| 785 |
if (item->isSelected() && col > 0) { |
| 786 |
static_cast<ParameterValueItem*>(item)->changeValue(); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
void ParameterValue::onChangeSelectedItem() |
| 791 |
{ |
| 792 |
onChangeSelectedItem(currentItem(), 1); |
| 793 |
} |
| 794 |
|
| 795 |
void ParameterValue::onDeleteSelectedItem() |
| 796 |
{ |
| 797 |
QTreeWidgetItem* sel = currentItem(); |
| 798 |
if (sel && sel->isSelected()) { |
| 799 |
takeTopLevelItem(indexOfTopLevelItem(sel)); |
| 800 |
static_cast<ParameterValueItem*>(sel)->removeFromGroup(); |
| 801 |
delete sel; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
void ParameterValue::onRenameSelectedItem() |
| 806 |
{ |
| 807 |
QTreeWidgetItem* sel = currentItem(); |
| 808 |
if (sel && sel->isSelected()) { |
| 809 |
editItem(sel, 0); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
void ParameterValue::onCreateTextItem() |
| 814 |
{ |
| 815 |
bool ok; |
| 816 |
QString name = QInputDialog::getText( |
| 817 |
this, |
| 818 |
QObject::tr("New text item"), |
| 819 |
QObject::tr("Enter the name:"), |
| 820 |
QLineEdit::Normal, |
| 821 |
QString(), |
| 822 |
&ok, |
| 823 |
Qt::MSWindowsFixedSizeDialogHint |
| 824 |
); |
| 825 |
|
| 826 |
if (!ok || !Gui::validateInput(this, name)) { |
| 827 |
return; |
| 828 |
} |
| 829 |
|
| 830 |
std::vector<std::pair<std::string, std::string>> smap = _hcGrp->GetASCIIMap(); |
| 831 |
for (const auto& it : smap) { |
| 832 |
if (name == QLatin1String(it.first.c_str())) { |
| 833 |
QMessageBox::critical( |
| 834 |
this, |
| 835 |
tr("Existing item"), |
| 836 |
tr("The item '%1' already exists.").arg(name) |
| 837 |
); |
| 838 |
return; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
QString val = QInputDialog::getText( |
| 843 |
this, |
| 844 |
QObject::tr("New text item"), |
| 845 |
QObject::tr("Enter text:"), |
| 846 |
QLineEdit::Normal, |
| 847 |
QString(), |
| 848 |
&ok, |
| 849 |
Qt::MSWindowsFixedSizeDialogHint |
| 850 |
); |
| 851 |
if (ok && !val.isEmpty()) { |
| 852 |
ParameterValueItem* pcItem; |
| 853 |
pcItem = new ParameterText(this, name, val.toUtf8(), _hcGrp); |
| 854 |
pcItem->appendToGroup(); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
void ParameterValue::onCreateIntItem() |
| 859 |
{ |
| 860 |
bool ok; |
| 861 |
QString name = QInputDialog::getText( |
| 862 |
this, |
| 863 |
QObject::tr("New integer item"), |
| 864 |
QObject::tr("Enter the name:"), |
| 865 |
QLineEdit::Normal, |
| 866 |
QString(), |
| 867 |
&ok, |
| 868 |
Qt::MSWindowsFixedSizeDialogHint |
| 869 |
); |
| 870 |
|
| 871 |
if (!ok || !Gui::validateInput(this, name)) { |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
std::vector<std::pair<std::string, long>> lmap = _hcGrp->GetIntMap(); |
| 876 |
for (const auto& it : lmap) { |
| 877 |
if (name == QLatin1String(it.first.c_str())) { |
| 878 |
QMessageBox::critical( |
| 879 |
this, |
| 880 |
tr("Existing item"), |
| 881 |
tr("The item '%1' already exists.").arg(name) |
| 882 |
); |
| 883 |
return; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
int val = QInputDialog::getInt( |
| 888 |
this, |
| 889 |
QObject::tr("New integer item"), |
| 890 |
QObject::tr("Enter number:"), |
| 891 |
0, |
| 892 |
-2147483647, |
| 893 |
2147483647, |
| 894 |
1, |
| 895 |
&ok, |
| 896 |
Qt::MSWindowsFixedSizeDialogHint |
| 897 |
); |
| 898 |
|
| 899 |
if (ok) { |
| 900 |
ParameterValueItem* pcItem; |
| 901 |
pcItem = new ParameterInt(this, name, (long)val, _hcGrp); |
| 902 |
pcItem->appendToGroup(); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
void ParameterValue::onCreateUIntItem() |
| 907 |
{ |
| 908 |
bool ok; |
| 909 |
QString name = QInputDialog::getText( |
| 910 |
this, |
| 911 |
QObject::tr("New unsigned item"), |
| 912 |
QObject::tr("Enter the name:"), |
| 913 |
QLineEdit::Normal, |
| 914 |
QString(), |
| 915 |
&ok, |
| 916 |
Qt::MSWindowsFixedSizeDialogHint |
| 917 |
); |
| 918 |
|
| 919 |
if (!ok || !Gui::validateInput(this, name)) { |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
std::vector<std::pair<std::string, unsigned long>> lmap = _hcGrp->GetUnsignedMap(); |
| 924 |
for (const auto& it : lmap) { |
| 925 |
if (name == QLatin1String(it.first.c_str())) { |
| 926 |
QMessageBox::critical( |
| 927 |
this, |
| 928 |
tr("Existing item"), |
| 929 |
tr("The item '%1' already exists.").arg(name) |
| 930 |
); |
| 931 |
return; |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
DlgInputDialogImp dlg(QObject::tr("Enter number:"), this, true, DlgInputDialogImp::UIntBox); |
| 936 |
dlg.setWindowTitle(QObject::tr("New Unsigned Item")); |
| 937 |
UIntSpinBox* edit = dlg.getUIntBox(); |
| 938 |
edit->setRange(0, std::numeric_limits<unsigned>::max()); |
| 939 |
if (dlg.exec() == QDialog::Accepted) { |
| 940 |
QString value = edit->text(); |
| 941 |
unsigned long val = value.toULong(&ok); |
| 942 |
|
| 943 |
if (ok) { |
| 944 |
ParameterValueItem* pcItem; |
| 945 |
pcItem = new ParameterUInt(this, name, val, _hcGrp); |
| 946 |
pcItem->appendToGroup(); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
void ParameterValue::onCreateFloatItem() |
| 952 |
{ |
| 953 |
bool ok; |
| 954 |
QString name = QInputDialog::getText( |
| 955 |
this, |
| 956 |
QObject::tr("New float item"), |
| 957 |
QObject::tr("Enter the name:"), |
| 958 |
QLineEdit::Normal, |
| 959 |
QString(), |
| 960 |
&ok, |
| 961 |
Qt::MSWindowsFixedSizeDialogHint |
| 962 |
); |
| 963 |
|
| 964 |
if (!ok || !Gui::validateInput(this, name)) { |
| 965 |
return; |
| 966 |
} |
| 967 |
|
| 968 |
std::vector<std::pair<std::string, double>> fmap = _hcGrp->GetFloatMap(); |
| 969 |
for (const auto& it : fmap) { |
| 970 |
if (name == QLatin1String(it.first.c_str())) { |
| 971 |
QMessageBox::critical( |
| 972 |
this, |
| 973 |
tr("Existing item"), |
| 974 |
tr("The item '%1' already exists.").arg(name) |
| 975 |
); |
| 976 |
return; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
double val = QInputDialog::getDouble( |
| 981 |
this, |
| 982 |
QObject::tr("New float item"), |
| 983 |
QObject::tr("Enter number:"), |
| 984 |
0, |
| 985 |
-2147483647, |
| 986 |
2147483647, |
| 987 |
12, |
| 988 |
&ok, |
| 989 |
Qt::MSWindowsFixedSizeDialogHint |
| 990 |
); |
| 991 |
if (ok) { |
| 992 |
ParameterValueItem* pcItem; |
| 993 |
pcItem = new ParameterFloat(this, name, val, _hcGrp); |
| 994 |
pcItem->appendToGroup(); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
void ParameterValue::onCreateBoolItem() |
| 999 |
{ |
| 1000 |
bool ok; |
| 1001 |
QString name = QInputDialog::getText( |
| 1002 |
this, |
| 1003 |
QObject::tr("New boolean item"), |
| 1004 |
QObject::tr("Enter the name:"), |
| 1005 |
QLineEdit::Normal, |
| 1006 |
QString(), |
| 1007 |
&ok, |
| 1008 |
Qt::MSWindowsFixedSizeDialogHint |
| 1009 |
); |
| 1010 |
|
| 1011 |
if (!ok || !Gui::validateInput(this, name)) { |
| 1012 |
return; |
| 1013 |
} |
| 1014 |
|
| 1015 |
std::vector<std::pair<std::string, bool>> bmap = _hcGrp->GetBoolMap(); |
| 1016 |
for (const auto& it : bmap) { |
| 1017 |
if (name == QLatin1String(it.first.c_str())) { |
| 1018 |
QMessageBox::critical( |
| 1019 |
this, |
| 1020 |
tr("Existing item"), |
| 1021 |
tr("The item '%1' already exists.").arg(name) |
| 1022 |
); |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
QStringList list; |
| 1028 |
list << QStringLiteral("true") << QStringLiteral("false"); |
| 1029 |
QString val = QInputDialog::getItem( |
| 1030 |
this, |
| 1031 |
QObject::tr("New boolean item"), |
| 1032 |
QObject::tr("Choose an item:"), |
| 1033 |
list, |
| 1034 |
0, |
| 1035 |
false, |
| 1036 |
&ok, |
| 1037 |
Qt::MSWindowsFixedSizeDialogHint |
| 1038 |
); |
| 1039 |
if (ok) { |
| 1040 |
ParameterValueItem* pcItem; |
| 1041 |
pcItem = new ParameterBool(this, name, (val == list[0] ? true : false), _hcGrp); |
| 1042 |
pcItem->appendToGroup(); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
// --------------------------------------------------------------------------- |
| 1047 |
|
| 1048 |
ParameterGroupItem::ParameterGroupItem( |
| 1049 |
ParameterGroupItem* parent, |
| 1050 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1051 |
) |
| 1052 |
: QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1) |
| 1053 |
, _hcGrp(hcGrp) |
| 1054 |
{ |
| 1055 |
setFlags(flags() | Qt::ItemIsEditable); |
| 1056 |
fillUp(); |
| 1057 |
} |
| 1058 |
|
| 1059 |
ParameterGroupItem::ParameterGroupItem(QTreeWidget* parent, const Base::Reference<ParameterGrp>& hcGrp) |
| 1060 |
: QTreeWidgetItem(parent, QTreeWidgetItem::UserType + 1) |
| 1061 |
, _hcGrp(hcGrp) |
| 1062 |
{ |
| 1063 |
setFlags(flags() | Qt::ItemIsEditable); |
| 1064 |
fillUp(); |
| 1065 |
} |
| 1066 |
|
| 1067 |
ParameterGroupItem::~ParameterGroupItem() |
| 1068 |
{ |
| 1069 |
// if the group has already been removed from the parameters then clear the observer list |
| 1070 |
// as we cannot notify the attached observers here |
| 1071 |
if (_hcGrp.getRefCount() == 1) { |
| 1072 |
_hcGrp->ClearObserver(); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
void ParameterGroupItem::fillUp() |
| 1077 |
{ |
| 1078 |
// filling up groups |
| 1079 |
std::vector<Base::Reference<ParameterGrp>> vhcParamGrp = _hcGrp->GetGroups(); |
| 1080 |
|
| 1081 |
setText(0, QString::fromUtf8(_hcGrp->GetGroupName())); |
| 1082 |
for (const auto& It : vhcParamGrp) { |
| 1083 |
(void)new ParameterGroupItem(this, It); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
void ParameterGroupItem::setData(int column, int role, const QVariant& value) |
| 1088 |
{ |
| 1089 |
if (role == Qt::EditRole) { |
| 1090 |
QString oldName = text(0); |
| 1091 |
QString newName = value.toString(); |
| 1092 |
if (newName.isEmpty() || oldName == newName) { |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (!Gui::validateInput(treeWidget(), newName)) { |
| 1097 |
return; |
| 1098 |
} |
| 1099 |
|
| 1100 |
// first check if there is already a group with name "newName" |
| 1101 |
auto item = static_cast<ParameterGroupItem*>(parent()); |
| 1102 |
if (!item) { |
| 1103 |
QMessageBox::critical( |
| 1104 |
treeWidget(), |
| 1105 |
QObject::tr("Rename group"), |
| 1106 |
QObject::tr("The group '%1' cannot be renamed.").arg(oldName) |
| 1107 |
); |
| 1108 |
return; |
| 1109 |
} |
| 1110 |
if (item->_hcGrp->HasGroup(newName.toLatin1())) { |
| 1111 |
QMessageBox::critical( |
| 1112 |
treeWidget(), |
| 1113 |
QObject::tr("Existing group"), |
| 1114 |
QObject::tr("The group '%1' already exists.").arg(newName) |
| 1115 |
); |
| 1116 |
return; |
| 1117 |
} |
| 1118 |
else { |
| 1119 |
// rename the group by adding a new group, copy the content and remove the old group |
| 1120 |
if (!item->_hcGrp->RenameGrp(oldName.toLatin1(), newName.toLatin1())) { |
| 1121 |
return; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
QTreeWidgetItem::setData(column, role, value); |
| 1127 |
} |
| 1128 |
|
| 1129 |
QVariant ParameterGroupItem::data(int column, int role) const |
| 1130 |
{ |
| 1131 |
if (role == Qt::DecorationRole) { |
| 1132 |
// The root item should keep its special pixmap |
| 1133 |
if (parent()) { |
| 1134 |
return this->isExpanded() |
| 1135 |
? QApplication::style()->standardPixmap(QStyle::SP_DirOpenIcon) |
| 1136 |
: QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
return QTreeWidgetItem::data(column, role); |
| 1141 |
} |
| 1142 |
|
| 1143 |
// -------------------------------------------------------------------- |
| 1144 |
|
| 1145 |
ParameterValueItem::ParameterValueItem(QTreeWidget* parent, const Base::Reference<ParameterGrp>& hcGrp) |
| 1146 |
: QTreeWidgetItem(parent) |
| 1147 |
, _hcGrp(hcGrp) |
| 1148 |
{ |
| 1149 |
setFlags(flags() | Qt::ItemIsEditable); |
| 1150 |
} |
| 1151 |
|
| 1152 |
ParameterValueItem::~ParameterValueItem() = default; |
| 1153 |
|
| 1154 |
void ParameterValueItem::setData(int column, int role, const QVariant& value) |
| 1155 |
{ |
| 1156 |
if (role == Qt::EditRole) { |
| 1157 |
QString oldName = text(0); |
| 1158 |
QString newName = value.toString(); |
| 1159 |
if (newName.isEmpty() || oldName == newName) { |
| 1160 |
return; |
| 1161 |
} |
| 1162 |
|
| 1163 |
if (!Gui::validateInput(treeWidget(), newName)) { |
| 1164 |
return; |
| 1165 |
} |
| 1166 |
|
| 1167 |
replace(oldName, newName); |
| 1168 |
} |
| 1169 |
|
| 1170 |
QTreeWidgetItem::setData(column, role, value); |
| 1171 |
} |
| 1172 |
|
| 1173 |
// -------------------------------------------------------------------- |
| 1174 |
|
| 1175 |
ParameterText::ParameterText( |
| 1176 |
QTreeWidget* parent, |
| 1177 |
QString label, |
| 1178 |
const char* value, |
| 1179 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1180 |
) |
| 1181 |
: ParameterValueItem(parent, hcGrp) |
| 1182 |
{ |
| 1183 |
setIcon(0, BitmapFactory().iconFromTheme("Param_Text")); |
| 1184 |
setText(0, label); |
| 1185 |
setText(1, QStringLiteral("Text")); |
| 1186 |
setText(2, QString::fromUtf8(value)); |
| 1187 |
} |
| 1188 |
|
| 1189 |
ParameterText::~ParameterText() = default; |
| 1190 |
|
| 1191 |
void ParameterText::changeValue() |
| 1192 |
{ |
| 1193 |
bool ok; |
| 1194 |
QString txt = QInputDialog::getText( |
| 1195 |
treeWidget(), |
| 1196 |
QObject::tr("Change value"), |
| 1197 |
QObject::tr("Enter text:"), |
| 1198 |
QLineEdit::Normal, |
| 1199 |
text(2), |
| 1200 |
&ok, |
| 1201 |
Qt::MSWindowsFixedSizeDialogHint |
| 1202 |
); |
| 1203 |
if (ok) { |
| 1204 |
setText(2, txt); |
| 1205 |
_hcGrp->SetASCII(text(0).toLatin1(), txt.toUtf8()); |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
void ParameterText::removeFromGroup() |
| 1210 |
{ |
| 1211 |
_hcGrp->RemoveASCII(text(0).toLatin1()); |
| 1212 |
} |
| 1213 |
|
| 1214 |
void ParameterText::replace(const QString& oldName, const QString& newName) |
| 1215 |
{ |
| 1216 |
std::string val = _hcGrp->GetASCII(oldName.toLatin1()); |
| 1217 |
_hcGrp->RemoveASCII(oldName.toLatin1()); |
| 1218 |
_hcGrp->SetASCII(newName.toLatin1(), val.c_str()); |
| 1219 |
} |
| 1220 |
|
| 1221 |
void ParameterText::appendToGroup() |
| 1222 |
{ |
| 1223 |
_hcGrp->SetASCII(text(0).toLatin1(), text(2).toUtf8()); |
| 1224 |
} |
| 1225 |
|
| 1226 |
// -------------------------------------------------------------------- |
| 1227 |
|
| 1228 |
ParameterInt::ParameterInt( |
| 1229 |
QTreeWidget* parent, |
| 1230 |
QString label, |
| 1231 |
long value, |
| 1232 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1233 |
) |
| 1234 |
: ParameterValueItem(parent, hcGrp) |
| 1235 |
{ |
| 1236 |
setIcon(0, BitmapFactory().iconFromTheme("Param_Int")); |
| 1237 |
setText(0, label); |
| 1238 |
setText(1, QStringLiteral("Integer")); |
| 1239 |
setText(2, QStringLiteral("%1").arg(value)); |
| 1240 |
} |
| 1241 |
|
| 1242 |
ParameterInt::~ParameterInt() = default; |
| 1243 |
|
| 1244 |
void ParameterInt::changeValue() |
| 1245 |
{ |
| 1246 |
bool ok; |
| 1247 |
int num = QInputDialog::getInt( |
| 1248 |
treeWidget(), |
| 1249 |
QObject::tr("Change value"), |
| 1250 |
QObject::tr("Enter number:"), |
| 1251 |
text(2).toInt(), |
| 1252 |
-2147483647, |
| 1253 |
2147483647, |
| 1254 |
1, |
| 1255 |
&ok, |
| 1256 |
Qt::MSWindowsFixedSizeDialogHint |
| 1257 |
); |
| 1258 |
if (ok) { |
| 1259 |
setText(2, QStringLiteral("%1").arg(num)); |
| 1260 |
_hcGrp->SetInt(text(0).toLatin1(), (long)num); |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
void ParameterInt::removeFromGroup() |
| 1265 |
{ |
| 1266 |
_hcGrp->RemoveInt(text(0).toLatin1()); |
| 1267 |
} |
| 1268 |
|
| 1269 |
void ParameterInt::replace(const QString& oldName, const QString& newName) |
| 1270 |
{ |
| 1271 |
long val = _hcGrp->GetInt(oldName.toLatin1()); |
| 1272 |
_hcGrp->RemoveInt(oldName.toLatin1()); |
| 1273 |
_hcGrp->SetInt(newName.toLatin1(), val); |
| 1274 |
} |
| 1275 |
|
| 1276 |
void ParameterInt::appendToGroup() |
| 1277 |
{ |
| 1278 |
_hcGrp->SetInt(text(0).toLatin1(), text(2).toLong()); |
| 1279 |
} |
| 1280 |
|
| 1281 |
// -------------------------------------------------------------------- |
| 1282 |
|
| 1283 |
ParameterUInt::ParameterUInt( |
| 1284 |
QTreeWidget* parent, |
| 1285 |
QString label, |
| 1286 |
unsigned long value, |
| 1287 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1288 |
) |
| 1289 |
: ParameterValueItem(parent, hcGrp) |
| 1290 |
{ |
| 1291 |
setIcon(0, BitmapFactory().iconFromTheme("Param_UInt")); |
| 1292 |
setText(0, label); |
| 1293 |
setText(1, QStringLiteral("Unsigned")); |
| 1294 |
setText(2, QStringLiteral("%1").arg(value)); |
| 1295 |
} |
| 1296 |
|
| 1297 |
ParameterUInt::~ParameterUInt() = default; |
| 1298 |
|
| 1299 |
void ParameterUInt::changeValue() |
| 1300 |
{ |
| 1301 |
bool ok; |
| 1302 |
DlgInputDialogImp dlg(QObject::tr("Enter number:"), treeWidget(), true, DlgInputDialogImp::UIntBox); |
| 1303 |
dlg.setWindowTitle(QObject::tr("Change Value")); |
| 1304 |
UIntSpinBox* edit = dlg.getUIntBox(); |
| 1305 |
edit->setRange(0, std::numeric_limits<unsigned>::max()); |
| 1306 |
edit->setValue(text(2).toULong()); |
| 1307 |
if (dlg.exec() == QDialog::Accepted) { |
| 1308 |
QString value = edit->text(); |
| 1309 |
unsigned long num = value.toULong(&ok); |
| 1310 |
|
| 1311 |
if (ok) { |
| 1312 |
setText(2, QStringLiteral("%1").arg(num)); |
| 1313 |
_hcGrp->SetUnsigned(text(0).toLatin1(), (unsigned long)num); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
void ParameterUInt::removeFromGroup() |
| 1319 |
{ |
| 1320 |
_hcGrp->RemoveUnsigned(text(0).toLatin1()); |
| 1321 |
} |
| 1322 |
|
| 1323 |
void ParameterUInt::replace(const QString& oldName, const QString& newName) |
| 1324 |
{ |
| 1325 |
unsigned long val = _hcGrp->GetUnsigned(oldName.toLatin1()); |
| 1326 |
_hcGrp->RemoveUnsigned(oldName.toLatin1()); |
| 1327 |
_hcGrp->SetUnsigned(newName.toLatin1(), val); |
| 1328 |
} |
| 1329 |
|
| 1330 |
void ParameterUInt::appendToGroup() |
| 1331 |
{ |
| 1332 |
_hcGrp->SetUnsigned(text(0).toLatin1(), text(2).toULong()); |
| 1333 |
} |
| 1334 |
|
| 1335 |
// -------------------------------------------------------------------- |
| 1336 |
|
| 1337 |
ParameterFloat::ParameterFloat( |
| 1338 |
QTreeWidget* parent, |
| 1339 |
QString label, |
| 1340 |
double value, |
| 1341 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1342 |
) |
| 1343 |
: ParameterValueItem(parent, hcGrp) |
| 1344 |
{ |
| 1345 |
setIcon(0, BitmapFactory().iconFromTheme("Param_Float")); |
| 1346 |
setText(0, label); |
| 1347 |
setText(1, QStringLiteral("Float")); |
| 1348 |
setText(2, QStringLiteral("%1").arg(value)); |
| 1349 |
} |
| 1350 |
|
| 1351 |
ParameterFloat::~ParameterFloat() = default; |
| 1352 |
|
| 1353 |
void ParameterFloat::changeValue() |
| 1354 |
{ |
| 1355 |
bool ok; |
| 1356 |
double num = QInputDialog::getDouble( |
| 1357 |
treeWidget(), |
| 1358 |
QObject::tr("Change value"), |
| 1359 |
QObject::tr("Enter number:"), |
| 1360 |
text(2).toDouble(), |
| 1361 |
-2147483647, |
| 1362 |
2147483647, |
| 1363 |
12, |
| 1364 |
&ok, |
| 1365 |
Qt::MSWindowsFixedSizeDialogHint |
| 1366 |
); |
| 1367 |
if (ok) { |
| 1368 |
setText(2, QStringLiteral("%1").arg(num)); |
| 1369 |
_hcGrp->SetFloat(text(0).toLatin1(), num); |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
void ParameterFloat::removeFromGroup() |
| 1374 |
{ |
| 1375 |
_hcGrp->RemoveFloat(text(0).toLatin1()); |
| 1376 |
} |
| 1377 |
|
| 1378 |
void ParameterFloat::replace(const QString& oldName, const QString& newName) |
| 1379 |
{ |
| 1380 |
double val = _hcGrp->GetFloat(oldName.toLatin1()); |
| 1381 |
_hcGrp->RemoveFloat(oldName.toLatin1()); |
| 1382 |
_hcGrp->SetFloat(newName.toLatin1(), val); |
| 1383 |
} |
| 1384 |
|
| 1385 |
void ParameterFloat::appendToGroup() |
| 1386 |
{ |
| 1387 |
_hcGrp->SetFloat(text(0).toLatin1(), text(2).toDouble()); |
| 1388 |
} |
| 1389 |
|
| 1390 |
// -------------------------------------------------------------------- |
| 1391 |
|
| 1392 |
ParameterBool::ParameterBool( |
| 1393 |
QTreeWidget* parent, |
| 1394 |
QString label, |
| 1395 |
bool value, |
| 1396 |
const Base::Reference<ParameterGrp>& hcGrp |
| 1397 |
) |
| 1398 |
: ParameterValueItem(parent, hcGrp) |
| 1399 |
{ |
| 1400 |
setIcon(0, BitmapFactory().iconFromTheme("Param_Bool")); |
| 1401 |
setText(0, label); |
| 1402 |
setText(1, QStringLiteral("Boolean")); |
| 1403 |
setText(2, QString::fromLatin1((value ? "true" : "false"))); |
| 1404 |
} |
| 1405 |
|
| 1406 |
ParameterBool::~ParameterBool() = default; |
| 1407 |
|
| 1408 |
void ParameterBool::changeValue() |
| 1409 |
{ |
| 1410 |
bool ok; |
| 1411 |
QStringList list; |
| 1412 |
list << QStringLiteral("true") << QStringLiteral("false"); |
| 1413 |
int pos = (text(2) == list[0] ? 0 : 1); |
| 1414 |
|
| 1415 |
QString txt = QInputDialog::getItem( |
| 1416 |
treeWidget(), |
| 1417 |
QObject::tr("Change value"), |
| 1418 |
QObject::tr("Choose an item:"), |
| 1419 |
list, |
| 1420 |
pos, |
| 1421 |
false, |
| 1422 |
&ok, |
| 1423 |
Qt::MSWindowsFixedSizeDialogHint |
| 1424 |
); |
| 1425 |
if (ok) { |
| 1426 |
setText(2, txt); |
| 1427 |
_hcGrp->SetBool(text(0).toLatin1(), (txt == list[0] ? true : false)); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
void ParameterBool::removeFromGroup() |
| 1432 |
{ |
| 1433 |
_hcGrp->RemoveBool(text(0).toLatin1()); |
| 1434 |
} |
| 1435 |
|
| 1436 |
void ParameterBool::replace(const QString& oldName, const QString& newName) |
| 1437 |
{ |
| 1438 |
bool val = _hcGrp->GetBool(oldName.toLatin1()); |
| 1439 |
_hcGrp->RemoveBool(oldName.toLatin1()); |
| 1440 |
_hcGrp->SetBool(newName.toLatin1(), val); |
| 1441 |
} |
| 1442 |
|
| 1443 |
void ParameterBool::appendToGroup() |
| 1444 |
{ |
| 1445 |
bool val = (text(2) == QLatin1String("true") ? true : false); |
| 1446 |
_hcGrp->SetBool(text(0).toLatin1(), val); |
| 1447 |
} |
| 1448 |
|
| 1449 |
#include "moc_DlgParameterImp.cpp" |