diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index cac1031eaf..3676b2ced7 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -206,6 +206,10 @@ void PropertyItem::appendChild(PropertyItem *item) childItems.append(item); } +/*! + * \brief PropertyItem::removeChildren + * Deletes the children in the range of [from, to] + */ void PropertyItem::removeChildren(int from, int to) { int count = to-from+1; @@ -215,6 +219,17 @@ void PropertyItem::removeChildren(int from, int to) } } +/*! + * \brief PropertyItem::takeChild + * Removes the child at index row but doesn't delete it + */ +PropertyItem *PropertyItem::takeChild(int row) +{ + PropertyItem* child = childItems.takeAt(row); + child->setParent(nullptr); + return child; +} + PropertyItem *PropertyItem::child(int row) { return childItems.value(row); diff --git a/src/Gui/propertyeditor/PropertyItem.h b/src/Gui/propertyeditor/PropertyItem.h index 0929cdf735..4ea604c0ea 100644 --- a/src/Gui/propertyeditor/PropertyItem.h +++ b/src/Gui/propertyeditor/PropertyItem.h @@ -136,6 +136,7 @@ public: PropertyItem *parent() const; void appendChild(PropertyItem *child); void removeChildren(int from, int to); + PropertyItem *takeChild(int); void setReadOnly(bool); bool isReadOnly() const; diff --git a/src/Gui/propertyeditor/PropertyModel.cpp b/src/Gui/propertyeditor/PropertyModel.cpp index 597ba8fdea..65b87cdbe9 100644 --- a/src/Gui/propertyeditor/PropertyModel.cpp +++ b/src/Gui/propertyeditor/PropertyModel.cpp @@ -208,11 +208,11 @@ QModelIndex PropertyModel::propertyIndexFromPath(const QStringList& path) const void PropertyModel::buildUp(const PropertyModel::PropertyList& props) { + beginResetModel(); + // fill up the listview with the properties rootItem->reset(); - beginResetModel(); - // sort the properties into their groups std::map > > propGroup; PropertyModel::PropertyList::const_iterator jt; diff --git a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp index dd3c4ed318..5b1fd069f8 100644 --- a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp +++ b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp @@ -124,7 +124,7 @@ void PropertyConstraintListItem::initialize() else { onlyUnnamed = false; if (!unnamed.empty()) { - PropertyConstraintListItem* item = static_cast(PropertyConstraintListItem::create()); + PropertyConstraintListItem* item = static_cast(PropertyConstraintListItem::create()); item->setParent(this); item->setPropertyName(tr("Unnamed")); this->appendChild(item); @@ -137,6 +137,98 @@ void PropertyConstraintListItem::initialize() } } +void PropertyConstraintListItem::assignProperty(const App::Property* prop) +{ + // Hint: When renaming a constraint that was unnamed before then it can happen that + // a constraint appears twice in the property editor, one time in this group and a + // second time inside the Unnamed group + if (!prop->getTypeId().isDerivedFrom(Sketcher::PropertyConstraintList::getClassTypeId())) + return; + + const Sketcher::PropertyConstraintList* list = static_cast(prop); + const std::vector< Sketcher::Constraint * > &vals = list->getValues(); + + // search for the group of unnamed items if available and take it out + int numUnnamed = 0; + PropertyConstraintListItem* unnamed = nullptr; + for (int i = this->childCount() - 1; i >= 0; i--) { + unnamed = qobject_cast(this->child(i)); + if (unnamed) { + numUnnamed = unnamed->childCount(); + this->takeChild(i); + break; + } + } + + int id = 1; + int namedIndex = 0; + int unnamedIndex = 0; + int numNamed = this->childCount(); + this->onlyUnnamed = true; + + for (std::vector< Sketcher::Constraint* >::const_iterator it = vals.begin();it != vals.end(); ++it, ++id) { + if ((*it)->Type == Sketcher::Distance || // Datum constraint + (*it)->Type == Sketcher::DistanceX || + (*it)->Type == Sketcher::DistanceY || + (*it)->Type == Sketcher::Radius || + (*it)->Type == Sketcher::Angle ) { + + PropertyUnitItem* child = nullptr; + if ((*it)->Name.empty()) { + // search inside the group item for unnamed constraints + if (!unnamed) { + unnamed = static_cast(PropertyConstraintListItem::create()); + unnamed->setPropertyName(tr("Unnamed")); + } + + if (unnamedIndex < numUnnamed) { + child = static_cast(unnamed->child(unnamedIndex)); + } + else { + child = static_cast(PropertyUnitItem::create()); + unnamed->appendChild(child); + child->setParent(unnamed); + } + unnamedIndex++; + } + else { + // search inside this item + if (namedIndex < numNamed) + child = dynamic_cast(this->child(namedIndex)); + + if (!child) { + child = static_cast(PropertyUnitItem::create()); + this->appendChild(child); + child->setParent(this); + } + namedIndex++; + this->onlyUnnamed = false; + } + + // Get the name + QString internalName = QString::fromLatin1("Constraint%1").arg(id); + QString name = QString::fromUtf8((*it)->Name.c_str()); + if (name.isEmpty()) { + name = internalName; + } + + if (child->objectName() != internalName) { + child->setPropertyName(name); + child->setObjectName(internalName); + + child->bind(list->createPath(id-1)); + child->setAutoApply(false); + } + } + } + + // at the Unnamed group at very the end + if (unnamed) { + this->appendChild(unnamed); + unnamed->setParent(this); + } +} + QVariant PropertyConstraintListItem::value(const App::Property* prop) const { assert(prop && prop->getTypeId().isDerivedFrom(Sketcher::PropertyConstraintList::getClassTypeId())); @@ -173,9 +265,6 @@ QVariant PropertyConstraintListItem::value(const App::Property* prop) const // Use a 7-bit ASCII string for the internal name. // See also comment in PropertyConstraintListItem::initialize() QString internalName = QString::fromLatin1("Constraint%1").arg(id); - PropertyConstraintListItem* self = const_cast(this); - - self->blockEvent = true; if ((*it)->Name.empty() && !onlyUnnamed) { onlyNamed = false; @@ -193,10 +282,10 @@ QVariant PropertyConstraintListItem::value(const App::Property* prop) const } } else { + self->blockEvent = true; self->setProperty(internalName.toLatin1(), QVariant::fromValue(quant)); + self->blockEvent = false; } - - self->blockEvent = false; } } diff --git a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h index e3ae30d4fd..8afe43ba44 100644 --- a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h +++ b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h @@ -46,6 +46,7 @@ class PropertyConstraintListItem: public Gui::PropertyEditor::PropertyItem PROPERTYITEM_HEADER virtual ~PropertyConstraintListItem(); + virtual void assignProperty(const App::Property* prop); virtual QWidget* createEditor(QWidget* parent, const QObject* receiver, const char* method) const; virtual void setEditorData(QWidget *editor, const QVariant& data) const; virtual QVariant editorData(QWidget *editor) const;