| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2024 Ondsel <[email protected]> * |
| 5 |
* Copyright (c) 2025 Pieter Hijma <[email protected]> * |
| 6 |
* * |
| 7 |
* This file is part of FreeCAD. * |
| 8 |
* * |
| 9 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 10 |
* under the terms of the GNU Lesser General Public License as * |
| 11 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 12 |
* License, or (at your option) any later version. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 17 |
* Lesser General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Lesser General Public * |
| 20 |
* License along with FreeCAD. If not, see * |
| 21 |
* <https://www.gnu.org/licenses/>. * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <QMessageBox> |
| 26 |
#include <QString> |
| 27 |
#include <QCompleter> |
| 28 |
#include <algorithm> |
| 29 |
#include <memory> |
| 30 |
#include <array> |
| 31 |
|
| 32 |
#include <App/Application.h> |
| 33 |
#include <App/Document.h> |
| 34 |
#include <App/DocumentObject.h> |
| 35 |
#include <App/ExpressionParser.h> |
| 36 |
#include <App/PropertyUnits.h> |
| 37 |
#include <App/PropertyFile.h> |
| 38 |
#include <App/PropertyGeo.h> |
| 39 |
#include <App/PropertyPythonObject.h> |
| 40 |
#include <Base/Tools.h> |
| 41 |
|
| 42 |
#include "Dialogs/DlgAddProperty.h" |
| 43 |
#include "Application.h" |
| 44 |
#include "Macro.h" |
| 45 |
#include "ui_DlgAddProperty.h" |
| 46 |
#include "ViewProviderVarSet.h" |
| 47 |
#include "propertyeditor/PropertyItem.h" |
| 48 |
|
| 49 |
FC_LOG_LEVEL_INIT("DlgAddProperty", true, true) |
| 50 |
|
| 51 |
using namespace Gui; |
| 52 |
using namespace Gui::Dialog; |
| 53 |
using namespace Gui::PropertyEditor; |
| 54 |
|
| 55 |
const std::string DlgAddProperty::GroupBase = "Base"; |
| 56 |
|
| 57 |
/* |
| 58 |
* This dialog has quite complex logic, so we will document it here. |
| 59 |
* |
| 60 |
* The design goals of this dialog are: |
| 61 |
* - support transactions (undo/redo), |
| 62 |
* - provide a value field as soon as possible (see #16189), |
| 63 |
* - keep the value if the name of the property is changed, |
| 64 |
* - support units (see #15557), |
| 65 |
* - support enumerations (see #15553), |
| 66 |
* - make OK available as soon as there is a valid property (see #17474), |
| 67 |
* - useful Python console commands (see #23760), |
| 68 |
* - support expressions (see #19716). |
| 69 |
* |
| 70 |
* Especially supporting expressions in the value field makes the logic |
| 71 |
* complex. Editors for value fields are created from PropertyItems. An |
| 72 |
* editor has two modes: One without the possibility to add an expression and |
| 73 |
* one with the possibility to add an expression. This depends on whether the |
| 74 |
* PropertyItem is bound. A PropertyItem can only be bound if a valid property |
| 75 |
* exists, which means the name of the property and the type should be known. |
| 76 |
* |
| 77 |
* As one of the goals of this dialog is to show an editor as soon as possible, |
| 78 |
* so also when there is no property name known yet, this means that the editor |
| 79 |
* won't have the expression button at first. |
| 80 |
* |
| 81 |
* To show the expression button as soon as possible, we create the property as |
| 82 |
* soon as a valid type and name are known. This allows us to bind the |
| 83 |
* PropertyItem which results in having the expression button. |
| 84 |
* |
| 85 |
* However, since we also want to support transactions, this means that we need |
| 86 |
* to open a transaction as well. This means that this dialog juggles the |
| 87 |
* following things: |
| 88 |
* |
| 89 |
* Given a valid property name and a property type we open a transaction and |
| 90 |
* create the property. As soon as the property name or type is changed, we |
| 91 |
* abort the transaction and start a new transaction with a recreated property |
| 92 |
* with the new name or type. |
| 93 |
* |
| 94 |
* If the property name or type is invalid, we clear the transaction and as |
| 95 |
* soon as the name or type become valid again, we start a new transaction. |
| 96 |
* |
| 97 |
* If the type is changed, we need to clear the current expression and value to |
| 98 |
* the default value. If only the name is changed, we keep the value as much |
| 99 |
* as possible with two exceptions: having a value based on an expression or |
| 100 |
* being a value for a property link. |
| 101 |
* |
| 102 |
* Expressions and links are bound to the property and changing the name of a |
| 103 |
* property prompts us to remove the old property and create a new one. This |
| 104 |
* to make sure that the transaction for adding a property does not keep a |
| 105 |
* history of old property name changes. So, because we want to have a new |
| 106 |
* transaction and expressions and links are bound to a property, the |
| 107 |
* expression or link becomes invalid when changing the property name. |
| 108 |
* |
| 109 |
* For expressions there are two choices: 1) we leave the outcome of the |
| 110 |
* expression in the value field (which is possible) but without it being based |
| 111 |
* on an expression or 2) we reset the value to the default value of the type. |
| 112 |
* |
| 113 |
* I chose the latter option because it is easy to miss that on a property name |
| 114 |
* change, the expression is invalidated, so the user may think the value is |
| 115 |
* the result of an expression, whereas in reality, the expression is lost. |
| 116 |
* |
| 117 |
* All in all, this leads to the following entities that need to be managed: |
| 118 |
* - transaction |
| 119 |
* - property item |
| 120 |
* - property |
| 121 |
* - editor |
| 122 |
* - value of the editor |
| 123 |
* |
| 124 |
* We have to react on a name change and on a type change. For each of these |
| 125 |
* changes, we need to take three cases into account: |
| 126 |
* - the name and type are valid |
| 127 |
* - only the type is valid |
| 128 |
* - neither the name nor the type is valid |
| 129 |
* |
| 130 |
* This has been encoded in the code below as onNameFieldChanged() and |
| 131 |
* onTypeChanged() and it works in two phases: clearing the transaction, |
| 132 |
* property item, and related, and building it up again depending on the |
| 133 |
* situation. |
| 134 |
*/ |
| 135 |
|
| 136 |
DlgAddProperty::DlgAddProperty(QWidget* parent, App::PropertyContainer* container) |
| 137 |
: DlgAddProperty(parent, container, nullptr) |
| 138 |
{} |
| 139 |
|
| 140 |
|
| 141 |
DlgAddProperty::DlgAddProperty(QWidget* parent, ViewProviderVarSet* viewProvider) |
| 142 |
: DlgAddProperty( |
| 143 |
parent, |
| 144 |
viewProvider ? viewProvider->getObject<App::PropertyContainer>() : nullptr, |
| 145 |
viewProvider |
| 146 |
) |
| 147 |
{} |
| 148 |
|
| 149 |
DlgAddProperty::DlgAddProperty( |
| 150 |
QWidget* parent, |
| 151 |
App::PropertyContainer* container, |
| 152 |
ViewProviderVarSet* viewProvider |
| 153 |
) |
| 154 |
: QDialog(parent) |
| 155 |
, container(container) |
| 156 |
, ui(new Ui_DlgAddProperty) |
| 157 |
, comboBoxGroup(this) |
| 158 |
, completerType(this) |
| 159 |
, editor(nullptr) |
| 160 |
, transactionID(0) |
| 161 |
{ |
| 162 |
ui->setupUi(this); |
| 163 |
setupMacroRedirector(); |
| 164 |
initializeWidgets(viewProvider); |
| 165 |
} |
| 166 |
|
| 167 |
DlgAddProperty::~DlgAddProperty() = default; |
| 168 |
|
| 169 |
void DlgAddProperty::setupMacroRedirector() |
| 170 |
{ |
| 171 |
setValueRedirector = std::make_unique<MacroManager::MacroRedirector>( |
| 172 |
[this](MacroManager::LineType /*type*/, const char* line) { this->setValueCommand = line; } |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
int DlgAddProperty::findLabelRow(const char* labelName, QFormLayout* layout) |
| 177 |
{ |
| 178 |
for (int row = 0; row < layout->rowCount(); ++row) { |
| 179 |
QLayoutItem* labelItem = layout->itemAt(row, QFormLayout::LabelRole); |
| 180 |
if (labelItem == nullptr) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
if (auto label = qobject_cast<QLabel*>(labelItem->widget())) { |
| 185 |
if (label->objectName() == QString::fromLatin1(labelName)) { |
| 186 |
return row; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
void DlgAddProperty::removeExistingWidget(QFormLayout* formLayout, int labelRow) |
| 194 |
{ |
| 195 |
if (QLayoutItem* existingItem = formLayout->itemAt(labelRow, QFormLayout::FieldRole)) { |
| 196 |
if (QWidget* existingWidget = existingItem->widget()) { |
| 197 |
formLayout->removeWidget(existingWidget); |
| 198 |
existingWidget->deleteLater(); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void DlgAddProperty::setWidgetForLabel(const char* labelName, QWidget* widget, QLayout* layout) |
| 205 |
{ |
| 206 |
auto formLayout = qobject_cast<QFormLayout*>(layout); |
| 207 |
if (formLayout == nullptr) { |
| 208 |
FC_ERR("Form layout not found"); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
int labelRow = findLabelRow(labelName, formLayout); |
| 213 |
if (labelRow < 0) { |
| 214 |
FC_ERR("Could not find row for '" << labelName << "'"); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
removeExistingWidget(formLayout, labelRow); |
| 219 |
formLayout->setWidget(labelRow, QFormLayout::FieldRole, widget); |
| 220 |
} |
| 221 |
|
| 222 |
void DlgAddProperty::populateGroup(EditFinishedComboBox& comboBox, const App::PropertyContainer* container) |
| 223 |
{ |
| 224 |
auto paramGroup = App::GetApplication().GetParameterGroupByPath( |
| 225 |
"User parameter:BaseApp/Preferences/PropertyView" |
| 226 |
); |
| 227 |
std::string lastGroup = paramGroup->GetASCII("NewPropertyGroup"); |
| 228 |
|
| 229 |
std::vector<App::Property*> properties; |
| 230 |
container->getPropertyList(properties); |
| 231 |
|
| 232 |
std::unordered_set<std::string> groupNames; |
| 233 |
for (const auto* prop : properties) { |
| 234 |
const char* groupName = container->getPropertyGroup(prop); |
| 235 |
groupNames.insert(groupName ? groupName : GroupBase); |
| 236 |
} |
| 237 |
|
| 238 |
std::vector<std::string> groupNamesSorted(groupNames.begin(), groupNames.end()); |
| 239 |
std::ranges::sort(groupNamesSorted, [](const std::string& a, const std::string& b) { |
| 240 |
// prefer anything else other than Base, so move it to the back |
| 241 |
if (a == GroupBase) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
if (b == GroupBase) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
return a < b; |
| 248 |
}); |
| 249 |
|
| 250 |
for (const auto& groupName : groupNamesSorted) { |
| 251 |
comboBox.addItem(QString::fromStdString(groupName)); |
| 252 |
} |
| 253 |
|
| 254 |
if (!lastGroup.empty() && std::ranges::find(groupNames, lastGroup) != groupNames.end()) { |
| 255 |
comboBox.setEditText(QString::fromStdString(lastGroup)); |
| 256 |
} |
| 257 |
else { |
| 258 |
comboBox.setEditText(QString::fromStdString(groupNamesSorted[0])); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void DlgAddProperty::initializeGroup() |
| 263 |
{ |
| 264 |
comboBoxGroup.setObjectName(QStringLiteral("comboBoxGroup")); |
| 265 |
comboBoxGroup.setInsertPolicy(QComboBox::InsertAtTop); |
| 266 |
comboBoxGroup.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 267 |
|
| 268 |
setWidgetForLabel("labelGroup", &comboBoxGroup, layout()); |
| 269 |
populateGroup(comboBoxGroup, container); |
| 270 |
|
| 271 |
connComboBoxGroup = connect( |
| 272 |
&comboBoxGroup, |
| 273 |
&EditFinishedComboBox::editFinished, |
| 274 |
this, |
| 275 |
&DlgAddProperty::onGroupFinished |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
DlgAddProperty::SupportedTypes DlgAddProperty::getSupportedTypes() |
| 280 |
{ |
| 281 |
std::vector<Base::Type> commonTypes = { |
| 282 |
App::PropertyLength::getClassTypeId(), |
| 283 |
App::PropertyAngle::getClassTypeId(), |
| 284 |
App::PropertyFloat::getClassTypeId(), |
| 285 |
App::PropertyInteger::getClassTypeId(), |
| 286 |
App::PropertyBool::getClassTypeId(), |
| 287 |
App::PropertyString::getClassTypeId(), |
| 288 |
App::PropertyEnumeration::getClassTypeId(), |
| 289 |
}; |
| 290 |
|
| 291 |
std::vector<Base::Type> otherTypes; |
| 292 |
std::vector<Base::Type> allTypes; |
| 293 |
Base::Type::getAllDerivedFrom(Base::Type::fromName("App::Property"), allTypes); |
| 294 |
|
| 295 |
const auto isCommonType = [&commonTypes](const Base::Type& type) { |
| 296 |
return std::ranges::find(commonTypes, type) != commonTypes.end(); |
| 297 |
}; |
| 298 |
|
| 299 |
std::ranges::copy_if(allTypes, std::back_inserter(otherTypes), [&](const Base::Type& type) { |
| 300 |
return type.canInstantiate() && !isExcluded(type) && !isCommonType(type); |
| 301 |
}); |
| 302 |
|
| 303 |
std::ranges::sort(otherTypes, [](Base::Type a, Base::Type b) { |
| 304 |
return strcmp(a.getName(), b.getName()) < 0; |
| 305 |
}); |
| 306 |
|
| 307 |
return {.commonTypes = std::move(commonTypes), .otherTypes = std::move(otherTypes)}; |
| 308 |
} |
| 309 |
|
| 310 |
void DlgAddProperty::initializeTypes() |
| 311 |
{ |
| 312 |
auto* model = new TypeItemModel(this); |
| 313 |
ui->comboBoxType->setModel(model); |
| 314 |
|
| 315 |
auto paramGroup = App::GetApplication().GetParameterGroupByPath( |
| 316 |
"User parameter:BaseApp/Preferences/PropertyView" |
| 317 |
); |
| 318 |
auto lastType = Base::Type::fromName( |
| 319 |
paramGroup->GetASCII("NewPropertyType", "App::PropertyLength").c_str() |
| 320 |
); |
| 321 |
if (lastType.isBad()) { |
| 322 |
lastType = App::PropertyLength::getClassTypeId(); |
| 323 |
} |
| 324 |
|
| 325 |
const auto [commonTypes, otherTypes] = getSupportedTypes(); |
| 326 |
|
| 327 |
const auto addTypes = [this, &lastType](const std::vector<Base::Type>& types) { |
| 328 |
for (const auto& type : types) { |
| 329 |
ui->comboBoxType->addItem(QString::fromLatin1(type.getName())); |
| 330 |
if (type == lastType) { |
| 331 |
ui->comboBoxType->setCurrentIndex(ui->comboBoxType->count() - 1); |
| 332 |
} |
| 333 |
} |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
const auto addSeparator = [this]() { |
| 338 |
ui->comboBoxType->addItem(QStringLiteral("──────────────────────")); |
| 339 |
const int idx = ui->comboBoxType->count() - 1; |
| 340 |
ui->comboBoxType->setItemData(idx, true, TypeItemModel::SeparatorRole); |
| 341 |
}; |
| 342 |
|
| 343 |
addTypes(commonTypes); |
| 344 |
addSeparator(); |
| 345 |
addTypes(otherTypes); |
| 346 |
|
| 347 |
completerType.setModel(ui->comboBoxType->model()); |
| 348 |
completerType.setCaseSensitivity(Qt::CaseInsensitive); |
| 349 |
completerType.setFilterMode(Qt::MatchContains); |
| 350 |
ui->comboBoxType->setCompleter(&completerType); |
| 351 |
ui->comboBoxType->setInsertPolicy(QComboBox::NoInsert); |
| 352 |
|
| 353 |
connComboBoxType = connect( |
| 354 |
ui->comboBoxType, |
| 355 |
&QComboBox::currentTextChanged, |
| 356 |
this, |
| 357 |
&DlgAddProperty::onTypeChanged |
| 358 |
); |
| 359 |
} |
| 360 |
|
| 361 |
void DlgAddProperty::removeSelectionEditor() |
| 362 |
{ |
| 363 |
// If the editor has a lineedit, then Qt selects the string inside it when |
| 364 |
// the editor is created. This interferes with the editor getting focus. |
| 365 |
// For example, units will then be selected as well, whereas this is not |
| 366 |
// the behavior we want. We therefore deselect the text in the lineedit. |
| 367 |
if (auto lineEdit = editor->findChild<QLineEdit*>()) { |
| 368 |
lineEdit->deselect(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void DlgAddProperty::addEnumEditor(PropertyItem* propertyItem) |
| 373 |
{ |
| 374 |
auto* values = static_cast<PropertyStringListItem*>(PropertyStringListItem::create()); |
| 375 |
values->setParent(propertyItem); |
| 376 |
values->setPropertyName(QLatin1String(QT_TRANSLATE_NOOP("App::Property", "Enum"))); |
| 377 |
if (propertyItem->childCount() > 0) { |
| 378 |
auto* child = propertyItem->takeChild(0); |
| 379 |
delete child; |
| 380 |
} |
| 381 |
propertyItem->appendChild(values); |
| 382 |
editor.reset( |
| 383 |
values->createEditor(this, [this]() { this->valueChangedEnum(); }, FrameOption::WithFrame) |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
void DlgAddProperty::addNormalEditor(PropertyItem* propertyItem) |
| 388 |
{ |
| 389 |
editor.reset( |
| 390 |
propertyItem->createEditor(this, [this]() { this->valueChanged(); }, FrameOption::WithFrame) |
| 391 |
); |
| 392 |
} |
| 393 |
|
| 394 |
void DlgAddProperty::addEditor(PropertyItem* propertyItem) |
| 395 |
{ |
| 396 |
if (!isTypeWithEditor(propertyItem)) { |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
if (isEnumPropertyItem()) { |
| 401 |
addEnumEditor(propertyItem); |
| 402 |
} |
| 403 |
else { |
| 404 |
addNormalEditor(propertyItem); |
| 405 |
} |
| 406 |
if (editor == nullptr) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
// Make sure that the editor has the same height as the |
| 411 |
// other widgets in the dialog. |
| 412 |
editor->setMinimumHeight(comboBoxGroup.height()); |
| 413 |
|
| 414 |
QSignalBlocker blockSignals(editor.get()); |
| 415 |
|
| 416 |
// To set the data in the editor, we need to set the data in the |
| 417 |
// propertyItem. The propertyItem needs to have a property set to make |
| 418 |
// sure that we get a correct value and the unit. |
| 419 |
setEditorData(propertyItem->data(PropertyItem::ValueColumn, Qt::EditRole)); |
| 420 |
editor->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 421 |
editor->setObjectName(QStringLiteral("editor")); |
| 422 |
|
| 423 |
setWidgetForLabel("labelValue", editor.get(), layout()); |
| 424 |
|
| 425 |
QWidget::setTabOrder(ui->comboBoxType, editor.get()); |
| 426 |
QWidget::setTabOrder(editor.get(), ui->lineEditToolTip); |
| 427 |
|
| 428 |
removeSelectionEditor(); |
| 429 |
} |
| 430 |
|
| 431 |
bool DlgAddProperty::isExcluded(const Base::Type& type) const |
| 432 |
{ |
| 433 |
// These properties are excluded because you cannot give them a value in |
| 434 |
// the property view. |
| 435 |
static const std::initializer_list<Base::Type> excludedTypes = { |
| 436 |
App::PropertyBoolList::getClassTypeId(), |
| 437 |
App::PropertyColorList::getClassTypeId(), |
| 438 |
App::PropertyExpressionEngine::getClassTypeId(), |
| 439 |
App::PropertyIntegerSet::getClassTypeId(), |
| 440 |
App::PropertyMap::getClassTypeId(), |
| 441 |
App::PropertyMaterial::getClassTypeId(), |
| 442 |
App::PropertyPlacementList::getClassTypeId(), |
| 443 |
App::PropertyPythonObject::getClassTypeId(), |
| 444 |
App::PropertyUUID::getClassTypeId() |
| 445 |
}; |
| 446 |
|
| 447 |
std::string_view name(type.getName()); |
| 448 |
return !name.starts_with("App::Property") |
| 449 |
|| std::ranges::find(excludedTypes, type) != excludedTypes.end(); |
| 450 |
} |
| 451 |
|
| 452 |
bool DlgAddProperty::isTypeWithEditor(PropertyItem* propertyItem) const |
| 453 |
{ |
| 454 |
if (propertyItem == nullptr) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
App::Property* prop = propertyItem->getFirstProperty(); |
| 459 |
if (prop == nullptr) { |
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
const Base::Type type = prop->getTypeId(); |
| 464 |
return isTypeWithEditor(type); |
| 465 |
} |
| 466 |
|
| 467 |
bool DlgAddProperty::isTypeWithEditor(const Base::Type& type) const |
| 468 |
{ |
| 469 |
static const std::initializer_list<Base::Type> subTypesWithEditor = { |
| 470 |
// These types and their subtypes have editors. |
| 471 |
App::PropertyBool::getClassTypeId(), |
| 472 |
App::PropertyColor::getClassTypeId(), |
| 473 |
App::PropertyFileIncluded::getClassTypeId(), |
| 474 |
App::PropertyFloat::getClassTypeId(), |
| 475 |
App::PropertyInteger::getClassTypeId() |
| 476 |
}; |
| 477 |
|
| 478 |
static const std::initializer_list<Base::Type> typesWithEditor = { |
| 479 |
// These types have editors but not necessarily their subtypes. |
| 480 |
|
| 481 |
// Although sublink properties have editors, they need the 3D view to |
| 482 |
// select an object. Because the dialog is modal, it is not possible |
| 483 |
// to make use of the 3D view, hence we do not provide an editor for |
| 484 |
// sublinks and their lists. It is possible to create a property of |
| 485 |
// this type though and the property can be set in the property view |
| 486 |
// later which does give access to the 3D view. |
| 487 |
App::PropertyEnumeration::getClassTypeId(), |
| 488 |
App::PropertyFile::getClassTypeId(), |
| 489 |
App::PropertyFloatList::getClassTypeId(), |
| 490 |
App::PropertyFont::getClassTypeId(), |
| 491 |
App::PropertyIntegerList::getClassTypeId(), |
| 492 |
App::PropertyLink::getClassTypeId(), |
| 493 |
App::PropertyLinkList::getClassTypeId(), |
| 494 |
App::PropertyXLink::getClassTypeId(), |
| 495 |
App::PropertyXLinkList::getClassTypeId(), |
| 496 |
App::PropertyMaterialList::getClassTypeId(), |
| 497 |
App::PropertyPath::getClassTypeId(), |
| 498 |
App::PropertyString::getClassTypeId(), |
| 499 |
App::PropertyStringList::getClassTypeId(), |
| 500 |
App::PropertyVectorList::getClassTypeId() |
| 501 |
}; |
| 502 |
|
| 503 |
const auto isDerivedFromType = [&type](const Base::Type& t) { |
| 504 |
return type.isDerivedFrom(t); |
| 505 |
}; |
| 506 |
|
| 507 |
return std::ranges::find(typesWithEditor, type) != typesWithEditor.end() |
| 508 |
|| std::ranges::any_of(subTypesWithEditor, isDerivedFromType); |
| 509 |
} |
| 510 |
|
| 511 |
bool DlgAddProperty::isTypeWithEditor(const std::string& type) const |
| 512 |
{ |
| 513 |
Base::Type propType |
| 514 |
= Base::Type::getTypeIfDerivedFrom(type.c_str(), App::Property::getClassTypeId(), true); |
| 515 |
return isTypeWithEditor(propType); |
| 516 |
} |
| 517 |
|
| 518 |
static PropertyItem* createPropertyItem(App::Property* prop) |
| 519 |
{ |
| 520 |
const char* editor = prop->getEditorName(); |
| 521 |
if (Base::Tools::isNullOrEmpty(editor)) { |
| 522 |
return nullptr; |
| 523 |
} |
| 524 |
return PropertyItemFactory::instance().createPropertyItem(editor); |
| 525 |
} |
| 526 |
|
| 527 |
void DlgAddProperty::createSupportDataForType(const Base::Type& type) |
| 528 |
{ |
| 529 |
// Temporarily create a property for two reasons: |
| 530 |
// - to acquire the editor name from the instance, and |
| 531 |
// - to acquire an initial value from the instance possibly with the correct unit. |
| 532 |
void* propInstance = type.createInstance(); |
| 533 |
if (!propInstance) { |
| 534 |
FC_THROWM(Base::RuntimeError, "Failed to create a property of type " << type.getName()); |
| 535 |
} |
| 536 |
|
| 537 |
// When prop goes out of scope, it can be deleted because we obtained the |
| 538 |
// propertyItem (if applicable) and we initialized the editor with the data |
| 539 |
// from the property. |
| 540 |
std::unique_ptr<App::Property, void (*)(App::Property*)> prop( |
| 541 |
static_cast<App::Property*>(propInstance), |
| 542 |
[](App::Property* p) { delete p; } |
| 543 |
); |
| 544 |
prop->setContainer(container); |
| 545 |
|
| 546 |
propertyItem.reset(createPropertyItem(prop.get())); |
| 547 |
|
| 548 |
if (propertyItem && isTypeWithEditor(type)) { |
| 549 |
propertyItem->setPropertyData({prop.get()}); |
| 550 |
addEditor(propertyItem.get()); |
| 551 |
propertyItem->removeProperty(prop.get()); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
void DlgAddProperty::initializeValue() |
| 556 |
{ |
| 557 |
std::string type = ui->comboBoxType->currentText().toStdString(); |
| 558 |
|
| 559 |
Base::Type propType |
| 560 |
= Base::Type::getTypeIfDerivedFrom(type.c_str(), App::Property::getClassTypeId(), true); |
| 561 |
if (propType.isBad()) { |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
createSupportDataForType(propType); |
| 566 |
if (!isTypeWithEditor(propType)) { |
| 567 |
// remove the editor from a previous add |
| 568 |
removeEditor(); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
void DlgAddProperty::setTitle() |
| 573 |
{ |
| 574 |
setWindowTitle(tr("Add Property")); |
| 575 |
} |
| 576 |
|
| 577 |
void DlgAddProperty::setAddEnabled(bool enabled) |
| 578 |
{ |
| 579 |
QPushButton* addButton = ui->buttonBox->button(QDialogButtonBox::Ok); |
| 580 |
QPushButton* closeButton = ui->buttonBox->button(QDialogButtonBox::Close); |
| 581 |
closeButton->setDefault(!enabled); |
| 582 |
addButton->setDefault(enabled); |
| 583 |
addButton->setEnabled(enabled); |
| 584 |
} |
| 585 |
|
| 586 |
void DlgAddProperty::initializeWidgets(ViewProviderVarSet* viewProvider) |
| 587 |
{ |
| 588 |
initializeGroup(); |
| 589 |
initializeTypes(); |
| 590 |
initializeValue(); |
| 591 |
|
| 592 |
if (viewProvider) { |
| 593 |
connect(this, &QDialog::finished, this, [viewProvider](int result) { |
| 594 |
viewProvider->onFinished(result); |
| 595 |
}); |
| 596 |
} |
| 597 |
connLineEditNameTextChanged |
| 598 |
= connect(ui->lineEditName, &QLineEdit::textChanged, this, &DlgAddProperty::onNameChanged); |
| 599 |
|
| 600 |
setTitle(); |
| 601 |
QPushButton* addButton = ui->buttonBox->button(QDialogButtonBox::Ok); |
| 602 |
addButton->setText(tr("Add")); |
| 603 |
setAddEnabled(false); |
| 604 |
|
| 605 |
ui->lineEditName->setFocus(); |
| 606 |
|
| 607 |
QWidget::setTabOrder(ui->lineEditName, &comboBoxGroup); |
| 608 |
QWidget::setTabOrder(&comboBoxGroup, ui->comboBoxType); |
| 609 |
|
| 610 |
adjustSize(); |
| 611 |
} |
| 612 |
|
| 613 |
bool DlgAddProperty::propertyExists(const std::string& name) |
| 614 |
{ |
| 615 |
App::Property* prop = container->getPropertyByName(name.c_str()); |
| 616 |
return prop && prop->getContainer() == container |
| 617 |
&& !(propertyItem && propertyItem->getFirstProperty() == prop); |
| 618 |
} |
| 619 |
|
| 620 |
bool DlgAddProperty::isNameValid() |
| 621 |
{ |
| 622 |
std::string name = ui->lineEditName->text().toStdString(); |
| 623 |
|
| 624 |
return !name.empty() && name == Base::Tools::getIdentifier(name) |
| 625 |
&& !App::ExpressionParser::isTokenAConstant(name) |
| 626 |
&& !App::ExpressionParser::isTokenAUnit(name) && !propertyExists(name); |
| 627 |
} |
| 628 |
|
| 629 |
bool DlgAddProperty::isGroupValid() |
| 630 |
{ |
| 631 |
std::string group = comboBoxGroup.currentText().toStdString(); |
| 632 |
return !group.empty() && group == Base::Tools::getIdentifier(group); |
| 633 |
} |
| 634 |
|
| 635 |
bool DlgAddProperty::isTypeValid() |
| 636 |
{ |
| 637 |
std::string type = ui->comboBoxType->currentText().toStdString(); |
| 638 |
return Base::Type::fromName(type.c_str()).isDerivedFrom(App::Property::getClassTypeId()) |
| 639 |
&& type != "App::Property"; |
| 640 |
} |
| 641 |
|
| 642 |
bool DlgAddProperty::isDocument() const |
| 643 |
{ |
| 644 |
return container->isDerivedFrom<App::Document>(); |
| 645 |
} |
| 646 |
|
| 647 |
bool DlgAddProperty::isDocumentObject() const |
| 648 |
{ |
| 649 |
return container->isDerivedFrom<App::DocumentObject>(); |
| 650 |
} |
| 651 |
|
| 652 |
bool DlgAddProperty::areFieldsValid() |
| 653 |
{ |
| 654 |
return isNameValid() && isGroupValid() && isTypeValid(); |
| 655 |
} |
| 656 |
|
| 657 |
void DlgAddProperty::showStatusMessage() |
| 658 |
{ |
| 659 |
QString error; |
| 660 |
QString text = ui->lineEditName->text(); |
| 661 |
std::string name = text.toStdString(); |
| 662 |
|
| 663 |
if (!isGroupValid()) { |
| 664 |
error = tr("Invalid group name"); |
| 665 |
} |
| 666 |
else if (!isTypeValid()) { |
| 667 |
error = tr("Invalid type name"); |
| 668 |
} |
| 669 |
else if (name.empty()) { |
| 670 |
error.clear(); |
| 671 |
} |
| 672 |
else if (name != Base::Tools::getIdentifier(name)) { |
| 673 |
error = tr("Invalid property name '%1'").arg(text); |
| 674 |
} |
| 675 |
else if (propertyExists(name)) { |
| 676 |
error = tr("Property '%1' already exists").arg(text); |
| 677 |
} |
| 678 |
else if (App::ExpressionParser::isTokenAConstant(name)) { |
| 679 |
error = tr("'%1' is a constant").arg(text); |
| 680 |
} |
| 681 |
else if (App::ExpressionParser::isTokenAUnit(name)) { |
| 682 |
error = tr("'%1' is a unit").arg(text); |
| 683 |
} |
| 684 |
|
| 685 |
ui->labelError->setText(error); |
| 686 |
} |
| 687 |
|
| 688 |
void DlgAddProperty::removeEditor() |
| 689 |
{ |
| 690 |
if (editor == nullptr) { |
| 691 |
return; |
| 692 |
} |
| 693 |
|
| 694 |
// Create a placeholder widget to keep the layout intact. |
| 695 |
auto* placeholder = new QWidget(this); |
| 696 |
placeholder->setObjectName(QStringLiteral("placeholder")); |
| 697 |
placeholder->setMinimumHeight(comboBoxGroup.height()); |
| 698 |
setWidgetForLabel("labelValue", placeholder, layout()); |
| 699 |
|
| 700 |
QWidget::setTabOrder(ui->comboBoxType, ui->lineEditToolTip); |
| 701 |
editor = nullptr; |
| 702 |
} |
| 703 |
|
| 704 |
bool DlgAddProperty::isEnumPropertyItem() const |
| 705 |
{ |
| 706 |
return ui->comboBoxType->currentText() |
| 707 |
== QString::fromLatin1(App::PropertyEnumeration::getClassTypeId().getName()); |
| 708 |
} |
| 709 |
|
| 710 |
QVariant DlgAddProperty::getEditorData() const |
| 711 |
{ |
| 712 |
if (isEnumPropertyItem()) { |
| 713 |
PropertyItem* child = propertyItem->child(0); |
| 714 |
if (child == nullptr) { |
| 715 |
return {}; |
| 716 |
} |
| 717 |
return child->editorData(editor.get()); |
| 718 |
} |
| 719 |
|
| 720 |
return propertyItem->editorData(editor.get()); |
| 721 |
} |
| 722 |
|
| 723 |
void DlgAddProperty::setEditorData(const QVariant& data) |
| 724 |
{ |
| 725 |
if (isEnumPropertyItem()) { |
| 726 |
PropertyItem* child = propertyItem->child(0); |
| 727 |
if (child == nullptr) { |
| 728 |
return; |
| 729 |
} |
| 730 |
child->setEditorData(editor.get(), data); |
| 731 |
} |
| 732 |
else { |
| 733 |
propertyItem->setEditorData(editor.get(), data); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
void DlgAddProperty::setEditor(bool valueNeedsReset) |
| 738 |
{ |
| 739 |
if (editor && !valueNeedsReset) { |
| 740 |
QVariant data = getEditorData(); |
| 741 |
addEditor(propertyItem.get()); |
| 742 |
if (editor == nullptr) { |
| 743 |
return; |
| 744 |
} |
| 745 |
setEditorData(data); |
| 746 |
removeSelectionEditor(); |
| 747 |
} |
| 748 |
else if (propertyItem) { |
| 749 |
addEditor(propertyItem.get()); |
| 750 |
} |
| 751 |
else { |
| 752 |
initializeValue(); |
| 753 |
} |
| 754 |
|
| 755 |
if (editor) { |
| 756 |
QVariant data = propertyItem->editorData(editor.get()); |
| 757 |
propertyItem->setData(data); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
void DlgAddProperty::setPropertyItem(App::Property* prop, bool supportsExpressions) |
| 762 |
{ |
| 763 |
if (prop == nullptr) { |
| 764 |
return; |
| 765 |
} |
| 766 |
|
| 767 |
if (propertyItem == nullptr) { |
| 768 |
propertyItem.reset(createPropertyItem(prop)); |
| 769 |
} |
| 770 |
|
| 771 |
if (propertyItem == nullptr) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
propertyItem->setAutoApply(true); |
| 776 |
propertyItem->setPropertyData({prop}); |
| 777 |
if (supportsExpressions) { |
| 778 |
objectIdentifier = std::make_unique<App::ObjectIdentifier>(*prop); |
| 779 |
propertyItem->bind(*objectIdentifier); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
void DlgAddProperty::buildForUnbound(bool valueNeedsReset) |
| 784 |
{ |
| 785 |
setEditor(valueNeedsReset); |
| 786 |
} |
| 787 |
|
| 788 |
void DlgAddProperty::buildForBound(bool valueNeedsReset, bool supportsExpressions) |
| 789 |
{ |
| 790 |
openTransaction(); |
| 791 |
App::Property* prop = createProperty(); |
| 792 |
setPropertyItem(prop, supportsExpressions); |
| 793 |
setEditor(valueNeedsReset); |
| 794 |
} |
| 795 |
|
| 796 |
bool DlgAddProperty::clearBoundProperty() |
| 797 |
{ |
| 798 |
// Both a property link and an expression are bound to a property and as a |
| 799 |
// result need the value to be reset. |
| 800 |
bool isPropertyLinkItem = qobject_cast<PropertyLinkItem*>(propertyItem.get()) != nullptr; |
| 801 |
bool valueNeedsReset = isPropertyLinkItem || propertyItem->hasExpression(); |
| 802 |
|
| 803 |
if (App::Property* prop = propertyItem->getFirstProperty()) { |
| 804 |
propertyItem->unbind(); |
| 805 |
if (auto* eb = dynamic_cast<ExpressionBinding*>(editor.get())) { |
| 806 |
eb->unbind(); |
| 807 |
} |
| 808 |
propertyItem->removeProperty(prop); |
| 809 |
container->removeDynamicProperty(prop->getName()); |
| 810 |
closeTransaction(TransactionOption::Abort); |
| 811 |
} |
| 812 |
return valueNeedsReset; |
| 813 |
} |
| 814 |
|
| 815 |
bool DlgAddProperty::clear(FieldChange fieldChange) |
| 816 |
{ |
| 817 |
if (propertyItem == nullptr) { |
| 818 |
return true; |
| 819 |
} |
| 820 |
|
| 821 |
bool valueNeedsReset = clearBoundProperty(); |
| 822 |
|
| 823 |
if (fieldChange == FieldChange::Type) { |
| 824 |
valueNeedsReset = true; |
| 825 |
removeEditor(); |
| 826 |
propertyItem = nullptr; |
| 827 |
} |
| 828 |
return valueNeedsReset; |
| 829 |
} |
| 830 |
|
| 831 |
void DlgAddProperty::onNameChanged([[maybe_unused]] const QString& text) |
| 832 |
{ |
| 833 |
bool valueNeedsReset = clear(FieldChange::Name); |
| 834 |
if (isNameValid() && isTypeValid()) { |
| 835 |
buildForBound(valueNeedsReset, isDocumentObject()); |
| 836 |
} |
| 837 |
else if (isTypeValid()) { |
| 838 |
buildForUnbound(valueNeedsReset); |
| 839 |
} |
| 840 |
else { |
| 841 |
removeEditor(); |
| 842 |
propertyItem = nullptr; |
| 843 |
} |
| 844 |
|
| 845 |
setAddEnabled(areFieldsValid()); |
| 846 |
showStatusMessage(); |
| 847 |
} |
| 848 |
|
| 849 |
void DlgAddProperty::onGroupFinished() |
| 850 |
{ |
| 851 |
if (isGroupValid() && propertyItem) { |
| 852 |
std::string group = comboBoxGroup.currentText().toStdString(); |
| 853 |
std::string doc = ui->lineEditToolTip->text().toStdString(); |
| 854 |
if (App::Property* prop = propertyItem->getFirstProperty(); |
| 855 |
prop && prop->getGroup() != group) { |
| 856 |
container->changeDynamicProperty(prop, group.c_str(), doc.c_str()); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
setAddEnabled(areFieldsValid()); |
| 861 |
showStatusMessage(); |
| 862 |
} |
| 863 |
|
| 864 |
void DlgAddProperty::onTypeChanged([[maybe_unused]] const QString& text) |
| 865 |
{ |
| 866 |
bool valueNeedsReset = clear(FieldChange::Type); |
| 867 |
if (isNameValid() && isTypeValid()) { |
| 868 |
buildForBound(valueNeedsReset, isDocumentObject()); |
| 869 |
} |
| 870 |
else if (isTypeValid()) { |
| 871 |
buildForUnbound(valueNeedsReset); |
| 872 |
} |
| 873 |
// nothing if both name and type are invalid |
| 874 |
|
| 875 |
setAddEnabled(areFieldsValid()); |
| 876 |
showStatusMessage(); |
| 877 |
} |
| 878 |
|
| 879 |
void DlgAddProperty::changeEvent(QEvent* e) |
| 880 |
{ |
| 881 |
if (e->type() == QEvent::LanguageChange) { |
| 882 |
ui->retranslateUi(this); |
| 883 |
setTitle(); |
| 884 |
} |
| 885 |
QDialog::changeEvent(e); |
| 886 |
} |
| 887 |
|
| 888 |
void DlgAddProperty::valueChangedEnum() |
| 889 |
{ |
| 890 |
auto* propEnum = static_cast<App::PropertyEnumeration*>(propertyItem->getFirstProperty()); |
| 891 |
if (propEnum == nullptr || propertyItem->childCount() == 0) { |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
auto* values = static_cast<PropertyStringListItem*>(propertyItem->child(0)); |
| 896 |
QVariant data = values->editorData(editor.get()); |
| 897 |
QStringList enumValues = data.toStringList(); |
| 898 |
// convert to std::vector<std::string> |
| 899 |
std::vector<std::string> enumValuesVec; |
| 900 |
std::ranges::transform(enumValues, std::back_inserter(enumValuesVec), [](const QString& value) { |
| 901 |
return value.toStdString(); |
| 902 |
}); |
| 903 |
propEnum->setEnums(enumValuesVec); |
| 904 |
} |
| 905 |
|
| 906 |
void DlgAddProperty::valueChanged() |
| 907 |
{ |
| 908 |
QVariant data = propertyItem->editorData(editor.get()); |
| 909 |
propertyItem->setData(data); |
| 910 |
} |
| 911 |
|
| 912 |
/* We use these functions rather than the functions provided by App::Document |
| 913 |
* because this dialog may be opened when another transaction is in progress. |
| 914 |
* An example is opening a sketch. If this dialog uses the functions provided |
| 915 |
* by App::Document, a reject of the dialog would close that transaction. By |
| 916 |
* checking whether the transaction ID is "our" transaction ID, we prevent this |
| 917 |
* behavior. |
| 918 |
*/ |
| 919 |
void DlgAddProperty::openTransaction() |
| 920 |
{ |
| 921 |
transactionID = App::GetApplication().setActiveTransaction("Add property"); |
| 922 |
} |
| 923 |
|
| 924 |
void DlgAddProperty::critical(const QString& title, const QString& text) |
| 925 |
{ |
| 926 |
static bool criticalDialogShown = false; |
| 927 |
if (!criticalDialogShown) { |
| 928 |
Base::StateLocker locker(criticalDialogShown); |
| 929 |
QMessageBox::critical(this, title, text); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
void DlgAddProperty::recordMacroAdd( |
| 934 |
const App::PropertyContainer* container, |
| 935 |
const std::string& type, |
| 936 |
const std::string& name, |
| 937 |
const std::string& group, |
| 938 |
const std::string& doc |
| 939 |
) const |
| 940 |
{ |
| 941 |
std::ostringstream command; |
| 942 |
command << "App.getDocument('"; |
| 943 |
const App::Document* document = freecad_cast<App::Document*>(container); |
| 944 |
const App::DocumentObject* object = freecad_cast<App::DocumentObject*>(container); |
| 945 |
if (document) { |
| 946 |
command << document->getName() << "')"; |
| 947 |
} |
| 948 |
else if (object) { |
| 949 |
command << object->getDocument()->getName() << "')." << object->getNameInDocument(); |
| 950 |
} |
| 951 |
else { |
| 952 |
FC_ERR("Cannot record macro for container of type " << container->getTypeId().getName()); |
| 953 |
return; |
| 954 |
} |
| 955 |
command << ".addProperty('" << type << "', '" << name << "', '" << group << "', '" << doc + "')"; |
| 956 |
Application::Instance->macroManager()->addLine(Gui::MacroManager::App, command.str().c_str()); |
| 957 |
} |
| 958 |
|
| 959 |
App::Property* DlgAddProperty::createProperty() |
| 960 |
{ |
| 961 |
std::string name = ui->lineEditName->text().toStdString(); |
| 962 |
std::string group = comboBoxGroup.currentText().toStdString(); |
| 963 |
std::string type = ui->comboBoxType->currentText().toStdString(); |
| 964 |
std::string doc = ui->lineEditToolTip->text().toStdString(); |
| 965 |
|
| 966 |
auto recordAddCommand = [this](MacroManager::LineType, const char* line) { |
| 967 |
this->addCommand = line; |
| 968 |
}; |
| 969 |
|
| 970 |
try { |
| 971 |
App::Property* prop |
| 972 |
= container->addDynamicProperty(type.c_str(), name.c_str(), group.c_str(), doc.c_str()); |
| 973 |
MacroManager::MacroRedirector redirector(recordAddCommand); |
| 974 |
recordMacroAdd(container, type, name, group, doc); |
| 975 |
return prop; |
| 976 |
} |
| 977 |
catch (Base::Exception& e) { |
| 978 |
e.reportException(); |
| 979 |
critical( |
| 980 |
QObject::tr("Add property"), |
| 981 |
QObject::tr("Failed to add property to '%1': %2") |
| 982 |
.arg(QString::fromLatin1(container->getFullName().c_str()), QString::fromUtf8(e.what())) |
| 983 |
); |
| 984 |
return nullptr; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
void DlgAddProperty::closeTransaction(TransactionOption option) |
| 989 |
{ |
| 990 |
if (transactionID == 0) { |
| 991 |
return; |
| 992 |
} |
| 993 |
|
| 994 |
App::GetApplication().closeActiveTransaction(static_cast<bool>(option), transactionID); |
| 995 |
transactionID = 0; |
| 996 |
} |
| 997 |
|
| 998 |
void DlgAddProperty::clearFields() |
| 999 |
{ |
| 1000 |
{ |
| 1001 |
QSignalBlocker blocker(ui->lineEditName); |
| 1002 |
ui->lineEditName->clear(); |
| 1003 |
} |
| 1004 |
ui->lineEditToolTip->clear(); |
| 1005 |
initializeValue(); |
| 1006 |
setAddEnabled(false); |
| 1007 |
} |
| 1008 |
|
| 1009 |
void DlgAddProperty::addDocumentation() |
| 1010 |
{ |
| 1011 |
/* Since there is no check on documentation (we accept any string), there |
| 1012 |
* is no signal handler for the documentation field. This method updates |
| 1013 |
* the property that is being added with the text inserted as |
| 1014 |
* documentation/tooltip. |
| 1015 |
*/ |
| 1016 |
|
| 1017 |
std::string group = comboBoxGroup.currentText().toStdString(); |
| 1018 |
std::string doc = ui->lineEditToolTip->text().toStdString(); |
| 1019 |
|
| 1020 |
if (propertyItem == nullptr) { |
| 1021 |
// If there is no property item, we cannot add documentation. |
| 1022 |
return; |
| 1023 |
} |
| 1024 |
|
| 1025 |
App::Property* prop = propertyItem->getFirstProperty(); |
| 1026 |
if (prop == nullptr) { |
| 1027 |
return; |
| 1028 |
} |
| 1029 |
|
| 1030 |
container->changeDynamicProperty(prop, group.c_str(), doc.c_str()); |
| 1031 |
} |
| 1032 |
|
| 1033 |
void DlgAddProperty::accept() |
| 1034 |
{ |
| 1035 |
addDocumentation(); |
| 1036 |
auto* object = freecad_cast<App::DocumentObject*>(container); |
| 1037 |
if (object) { |
| 1038 |
object->ExpressionEngine.execute(); |
| 1039 |
} |
| 1040 |
closeTransaction(TransactionOption::Commit); |
| 1041 |
|
| 1042 |
setValueRedirector = nullptr; |
| 1043 |
Application::Instance->macroManager()->addLine(MacroManager::LineType::App, addCommand.c_str()); |
| 1044 |
Application::Instance->macroManager()->addLine(MacroManager::LineType::App, setValueCommand.c_str()); |
| 1045 |
setupMacroRedirector(); |
| 1046 |
|
| 1047 |
std::string group = comboBoxGroup.currentText().toStdString(); |
| 1048 |
std::string type = ui->comboBoxType->currentText().toStdString(); |
| 1049 |
auto paramGroup = App::GetApplication().GetParameterGroupByPath( |
| 1050 |
"User parameter:BaseApp/Preferences/PropertyView" |
| 1051 |
); |
| 1052 |
paramGroup->SetASCII("NewPropertyType", type.c_str()); |
| 1053 |
paramGroup->SetASCII("NewPropertyGroup", group.c_str()); |
| 1054 |
|
| 1055 |
clearFields(); |
| 1056 |
ui->lineEditName->setFocus(); |
| 1057 |
|
| 1058 |
// Note that we don't call QDialog::accept() here to keep the dialog |
| 1059 |
// open for adding more properties. |
| 1060 |
} |
| 1061 |
|
| 1062 |
void DlgAddProperty::reject() |
| 1063 |
{ |
| 1064 |
if (propertyItem) { |
| 1065 |
if (App::Property* prop = propertyItem->getFirstProperty()) { |
| 1066 |
App::PropertyContainer* container = prop->getContainer(); |
| 1067 |
container->removeDynamicProperty(prop->getName()); |
| 1068 |
closeTransaction(TransactionOption::Abort); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
disconnect(connComboBoxGroup); |
| 1072 |
disconnect(connComboBoxType); |
| 1073 |
disconnect(connLineEditNameTextChanged); |
| 1074 |
|
| 1075 |
QDialog::reject(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
#include "moc_DlgAddProperty.cpp" |