From a9fef8412face470e7576a70954aa6cfae2e2cba Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Fri, 8 Aug 2014 14:47:10 +0200 Subject: [PATCH] New feature: Edit Sketch datum constraints from data Property view This feature shows in the data properties associated to a TreeView element, the datum constraints, i.e. those with an associated value. --- src/Mod/Sketcher/App/PropertyConstraintList.h | 4 + src/Mod/Sketcher/Gui/AppSketcherGui.cpp | 3 + src/Mod/Sketcher/Gui/CMakeLists.txt | 3 + .../Gui/PropertyConstraintListItem.cpp | 228 ++++++++++++++++++ .../Sketcher/Gui/PropertyConstraintListItem.h | 70 ++++++ 5 files changed, 308 insertions(+) create mode 100644 src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp create mode 100644 src/Mod/Sketcher/Gui/PropertyConstraintListItem.h diff --git a/src/Mod/Sketcher/App/PropertyConstraintList.h b/src/Mod/Sketcher/App/PropertyConstraintList.h index ec6888285..df99b4b3f 100644 --- a/src/Mod/Sketcher/App/PropertyConstraintList.h +++ b/src/Mod/Sketcher/App/PropertyConstraintList.h @@ -60,6 +60,10 @@ public: virtual void setSize(int newSize); virtual int getSize(void) const; + + const char* getEditorName(void) const { + return "SketcherGui::PropertyConstraintListItem"; + } /** Sets the property */ diff --git a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp index 2a2a26e25..504289f46 100644 --- a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp +++ b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp @@ -37,6 +37,8 @@ #include "SoDatumLabel.h" #include "SoZoomTranslation.h" #include "SketcherSettings.h" +#include "PropertyConstraintListItem.h" + // create the commands void CreateSketcherCommands(void); @@ -92,6 +94,7 @@ void SketcherGuiExport initSketcherGui() SketcherGui::ViewProviderCustomPython ::init(); SketcherGui::SoDatumLabel ::initClass(); SketcherGui::SoZoomTranslation ::initClass(); + SketcherGui::PropertyConstraintListItem ::init(); (void)new Gui::PrefPageProducer ( QT_TRANSLATE_NOOP("QObject","Display") ); diff --git a/src/Mod/Sketcher/Gui/CMakeLists.txt b/src/Mod/Sketcher/Gui/CMakeLists.txt index 7bab80e82..bfb2a240c 100644 --- a/src/Mod/Sketcher/Gui/CMakeLists.txt +++ b/src/Mod/Sketcher/Gui/CMakeLists.txt @@ -37,6 +37,7 @@ set(SketcherGui_MOC_HDRS TaskDlgEditSketch.h SketchOrientationDialog.h SketcherSettings.h + PropertyConstraintListItem.h ) fc_wrap_cpp(SketcherGui_MOC_SRCS ${SketcherGui_MOC_HDRS}) SOURCE_GROUP("Moc" FILES ${SketcherGui_MOC_SRCS}) @@ -72,6 +73,8 @@ SET(SketcherGui_SRCS SoZoomTranslation.h SoDatumLabel.cpp SoDatumLabel.h + PropertyConstraintListItem.h + PropertyConstraintListItem.cpp TaskSketcherConstrains.ui TaskSketcherConstrains.cpp TaskSketcherConstrains.h diff --git a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp new file mode 100644 index 000000000..875b2a86a --- /dev/null +++ b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp @@ -0,0 +1,228 @@ +/*************************************************************************** +* Copyright (c) 2014 Abdullah Tahiri * +* * +* This file is part of the FreeCAD CAx development system. * +* * +* This library is free software; you can redistribute it and/or * +* modify it under the terms of the GNU Library General Public * +* License as published by the Free Software Foundation; either * +* version 2 of the License, or (at your option) any later version. * +* * +* This library is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU Library General Public License for more details. * +* * +* You should have received a copy of the GNU Library General Public * +* License along with this library; see the file COPYING.LIB. If not, * +* write to the Free Software Foundation, Inc., 59 Temple Place, * +* Suite 330, Boston, MA 02111-1307, USA * +* * +***************************************************************************/ + +#include "PreCompiled.h" + +#ifndef _PreComp_ +# include +# include +# include +# include +# include +# include +# include +# include +# include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "../App/PropertyConstraintList.h" + +#include "PropertyConstraintListItem.h" + + +using namespace SketcherGui; +using namespace Gui::PropertyEditor; + +TYPESYSTEM_SOURCE(SketcherGui::PropertyConstraintListItem, Gui::PropertyEditor::PropertyItem); + +PropertyConstraintListItem::PropertyConstraintListItem() +{ + +} + +QVariant PropertyConstraintListItem::toString(const QVariant& prop) const +{ + const QList& value = prop.value< QList >(); + QString str; + QTextStream out(&str); + out << "["; + for (QList::const_iterator it = value.begin(); it != value.end(); ++it) { + if (it != value.begin()) + out << ";"; + out << it->getUserString(); + } + out << "]"; + return QVariant(str); +} + +QVariant PropertyConstraintListItem::value(const App::Property* prop) const +{ + assert(prop && prop->getTypeId().isDerivedFrom(Sketcher::PropertyConstraintList::getClassTypeId())); + + PropertyConstraintListItem* self = const_cast(this); + + int id = 1; + + QList quantities; + const std::vector< Sketcher::Constraint * > &vals = static_cast(prop)->getValues(); + 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) { + + Base::Quantity quant; + if ((*it)->Type == Sketcher::Angle) { + double datum = Base::toDegrees((*it)->Value); + quant.setUnit(Base::Unit::Angle); + quant.setValue(datum); + } + else { + quant.setUnit(Base::Unit::Length); + quant.setValue((*it)->Value); + } + + quantities.append(quant); + + QString name = QString::fromStdString((*it)->Name); + if (name.isEmpty()) + name = QString::fromLatin1("Constraint%1").arg(id); + + PropertyConstraintListItem* self = const_cast(this); + self->blockEvent=true; + self->setProperty(name.toLatin1(), QVariant::fromValue(quant)); + self->blockEvent=false; + + } + } + + + return QVariant::fromValue< QList >(quantities); + +} + +void PropertyConstraintListItem::setValue(const QVariant& value) +{ + // see PropertyConstraintListItem::event +} + +bool PropertyConstraintListItem::event (QEvent* ev) +{ + if (ev->type() == QEvent::DynamicPropertyChange) { + if(!blockEvent) { + QDynamicPropertyChangeEvent* ce = static_cast(ev); + QVariant prop = property(ce->propertyName()); + QString propName = QString::fromLatin1(ce->propertyName()); + Base::Quantity quant = prop.value(); + + int id = 0; + Sketcher::PropertyConstraintList* item = static_cast(getFirstProperty()); + + const std::vector< Sketcher::Constraint * > &vals = item->getValues(); + 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) { + + + // Get the name + QString name = QString::fromStdString((*it)->Name); + if (name.isEmpty()) + name = QString::fromLatin1("Constraint%1").arg(id+1); + if (name == propName) { + double datum = quant.getValue(); + if ((*it)->Type == Sketcher::Angle) + datum = Base::toRadians(datum); + const_cast((*it))->Value = datum; + item->set1Value(id,(*it)); + break; + } + } + } + } + } + return PropertyItem::event(ev); +} + +QWidget* PropertyConstraintListItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const +{ + QLineEdit *le = new QLineEdit(parent); + le->setFrame(false); + le->setReadOnly(true); + return le; +} + +void PropertyConstraintListItem::setEditorData(QWidget *editor, const QVariant& data) const +{ + QLineEdit* le = qobject_cast(editor); + le->setText(toString(data).toString()); +} + +QVariant PropertyConstraintListItem::editorData(QWidget *editor) const +{ + QLineEdit *le = qobject_cast(editor); + return QVariant(le->text()); +} + +void PropertyConstraintListItem::initialize() +{ + const Sketcher::PropertyConstraintList* item=static_cast(getPropertyData()[0]); + + const std::vector< Sketcher::Constraint * > &vals = item->getValues(); + + int id = 1; + + 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) { + + + // Get the name + + QString name = QString::fromStdString((*it)->Name); + if (name.isEmpty()) + name = QString::fromLatin1("Constraint%1").arg(id); + PropertyUnitItem* item = static_cast(PropertyUnitItem::create()); + item->setParent(this); + item->setPropertyName(name); + this->appendChild(item); + } + } + +} + +#include "moc_PropertyConstraintListItem.cpp" \ No newline at end of file diff --git a/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h new file mode 100644 index 000000000..ed60d8c42 --- /dev/null +++ b/src/Mod/Sketcher/Gui/PropertyConstraintListItem.h @@ -0,0 +1,70 @@ + +/*************************************************************************** +* Copyright (c) 2014 Abdullah Tahiri * +* * +* This file is part of the FreeCAD CAx development system. * +* * +* This library is free software; you can redistribute it and/or * +* modify it under the terms of the GNU Library General Public * +* License as published by the Free Software Foundation; either * +* version 2 of the License, or (at your option) any later version. * +* * +* This library is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU Library General Public License for more details. * +* * +* You should have received a copy of the GNU Library General Public * +* License along with this library; see the file COPYING.LIB. If not, * +* write to the Free Software Foundation, Inc., 59 Temple Place, * +* Suite 330, Boston, MA 02111-1307, USA * +* * +***************************************************************************/ +#ifndef PROPERTYCONSTRAINTLISTITEM_H +#define PROPERTYCONSTRAINTLISTITEM_H + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + + +namespace SketcherGui { + +class GuiExport PropertyConstraintListItem: public Gui::PropertyEditor::PropertyItem +{ + Q_OBJECT + TYPESYSTEM_HEADER(); + + 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; + +protected: + virtual QVariant toString(const QVariant&) const; + virtual QVariant value(const App::Property*) const; + virtual void setValue(const QVariant&); + virtual bool event (QEvent* ev); + + virtual void initialize(); + + +protected: + PropertyConstraintListItem(); + bool blockEvent; + +}; + +} //namespace SketcherGui + + +#endif \ No newline at end of file