Skip to content

Commit

Permalink
New feature: Edit Sketch datum constraints from data Property view
Browse files Browse the repository at this point in the history
This feature shows in the data properties associated to a TreeView element,
the datum constraints, i.e. those with an associated value.
  • Loading branch information
abdullahtahiriyo authored and wwmayer committed Aug 19, 2014
1 parent e2f54c7 commit a9fef84
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Mod/Sketcher/App/PropertyConstraintList.h
Expand Up @@ -60,6 +60,10 @@ class SketcherExport PropertyConstraintList : public App::PropertyLists

virtual void setSize(int newSize);
virtual int getSize(void) const;

const char* getEditorName(void) const {
return "SketcherGui::PropertyConstraintListItem";
}

/** Sets the property
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Sketcher/Gui/AppSketcherGui.cpp
Expand Up @@ -37,6 +37,8 @@
#include "SoDatumLabel.h"
#include "SoZoomTranslation.h"
#include "SketcherSettings.h"
#include "PropertyConstraintListItem.h"


// create the commands
void CreateSketcherCommands(void);
Expand Down Expand Up @@ -92,6 +94,7 @@ void SketcherGuiExport initSketcherGui()
SketcherGui::ViewProviderCustomPython ::init();
SketcherGui::SoDatumLabel ::initClass();
SketcherGui::SoZoomTranslation ::initClass();
SketcherGui::PropertyConstraintListItem ::init();

(void)new Gui::PrefPageProducer<SketcherGui::SketcherSettings> ( QT_TRANSLATE_NOOP("QObject","Display") );

Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Sketcher/Gui/CMakeLists.txt
Expand Up @@ -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})
Expand Down Expand Up @@ -72,6 +73,8 @@ SET(SketcherGui_SRCS
SoZoomTranslation.h
SoDatumLabel.cpp
SoDatumLabel.h
PropertyConstraintListItem.h
PropertyConstraintListItem.cpp
TaskSketcherConstrains.ui
TaskSketcherConstrains.cpp
TaskSketcherConstrains.h
Expand Down
228 changes: 228 additions & 0 deletions src/Mod/Sketcher/Gui/PropertyConstraintListItem.cpp
@@ -0,0 +1,228 @@
/***************************************************************************
* Copyright (c) 2014 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
* *
* 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 <algorithm>
# include <QComboBox>
# include <QFontDatabase>
# include <QLayout>
# include <QLocale>
# include <QPixmap>
# include <QSpinBox>
# include <QTextStream>
# include <QTimer>
#endif

#include <Base/Tools.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/PropertyGeo.h>
#include <App/PropertyFile.h>
#include <App/PropertyUnits.h>
#include <Gui/Application.h>
#include <Gui/Control.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/ViewProviderDocumentObject.h>
#include <Gui/Placement.h>
#include <Gui/FileDialog.h>
#include <Gui/DlgPropertyLink.h>
#include <Gui/QuantitySpinBox.h>

#include <Gui/propertyeditor/PropertyItem.h>

#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<Base::Quantity>& value = prop.value< QList<Base::Quantity> >();
QString str;
QTextStream out(&str);
out << "[";
for (QList<Base::Quantity>::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<PropertyConstraintListItem*>(this);

int id = 1;

QList<Base::Quantity> quantities;
const std::vector< Sketcher::Constraint * > &vals = static_cast<const Sketcher::PropertyConstraintList*>(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<double>((*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<PropertyConstraintListItem*>(this);
self->blockEvent=true;
self->setProperty(name.toLatin1(), QVariant::fromValue<Base::Quantity>(quant));
self->blockEvent=false;

}
}


return QVariant::fromValue< QList<Base::Quantity> >(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<QDynamicPropertyChangeEvent*>(ev);
QVariant prop = property(ce->propertyName());
QString propName = QString::fromLatin1(ce->propertyName());
Base::Quantity quant = prop.value<Base::Quantity>();

int id = 0;
Sketcher::PropertyConstraintList* item = static_cast<Sketcher::PropertyConstraintList*>(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<double>(datum);
const_cast<Sketcher::Constraint *>((*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<QLineEdit*>(editor);
le->setText(toString(data).toString());
}

QVariant PropertyConstraintListItem::editorData(QWidget *editor) const
{
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
return QVariant(le->text());
}

void PropertyConstraintListItem::initialize()
{
const Sketcher::PropertyConstraintList* item=static_cast<const Sketcher::PropertyConstraintList*>(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*>(PropertyUnitItem::create());
item->setParent(this);
item->setPropertyName(name);
this->appendChild(item);
}
}

}

#include "moc_PropertyConstraintListItem.cpp"
70 changes: 70 additions & 0 deletions src/Mod/Sketcher/Gui/PropertyConstraintListItem.h
@@ -0,0 +1,70 @@

/***************************************************************************
* Copyright (c) 2014 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
* *
* 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 <QObject>
#include <QPointer>
#include <QItemEditorFactory>
#include <vector>
#include <QList>

#include <Base/Type.h>
#include <Base/Quantity.h>
#include <Base/UnitsApi.h>
#include <App/PropertyStandard.h>
#include <Gui/Widgets.h>

#include <Gui/propertyeditor/PropertyItem.h>


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

0 comments on commit a9fef84

Please sign in to comment.