/************************************************************************ ** ** @file vtooloptionspropertybrowser.cpp ** @author Roman Telezhynskyi ** @date 22 8, 2014 ** ** @brief ** @copyright ** This source code is part of the Valentine project, a pattern making ** program, whose allow create and modeling patterns of clothing. ** Copyright (C) 2014 Valentina project ** All Rights Reserved. ** ** Valentina is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 3 of the License, or ** (at your option) any later version. ** ** Valentina 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 General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with Valentina. If not, see . ** *************************************************************************/ #include "vtooloptionspropertybrowser.h" #include "tools/drawTools/drawtools.h" #include "widgets/vapplication.h" #include "widgets/vmaingraphicsview.h" #include "visualization/vgraphicssimpletextitem.h" #include "visualization/vcontrolpointspline.h" #include "../libs/vpropertyexplorer/vproperties.h" #include "vformulaproperty.h" #include "../container/vformula.h" #include #include using namespace VPE; //--------------------------------------------------------------------------------------------------------------------- VToolOptionsPropertyBrowser::VToolOptionsPropertyBrowser(QDockWidget *parent) :QObject(parent), currentItem(nullptr), propertyToId(QMap()), idToProperty(QMap()) { PropertyModel = new VPropertyModel(this); TreeView = new VPropertyFormView(PropertyModel, parent); TreeView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QScrollArea *scroll = new QScrollArea(parent); scroll->setWidgetResizable(true); scroll->setWidget(TreeView); parent->setWidget(scroll); connect(PropertyModel, SIGNAL(onDataChangedByEditor(VProperty*)), this, SLOT(userChangedData(VProperty*))); } //--------------------------------------------------------------------------------------------------------------------- void VToolOptionsPropertyBrowser::itemClicked(QGraphicsItem *item) { if (currentItem == item) { UpdateOptions(); return; } PropertyModel->clear(); propertyToId.clear(); idToProperty.clear(); currentItem = item; if (currentItem == nullptr) { TreeView->setTitle(tr("")); return; } ShowItemOptions(currentItem); } //--------------------------------------------------------------------------------------------------------------------- void VToolOptionsPropertyBrowser::userChangedData(VProperty *property) { VProperty *prop = property; if (!propertyToId.contains(prop)) { if (!propertyToId.contains(prop->getParent()))// Maybe we know parent { return; } else { prop = prop->getParent(); } } if (!currentItem) { return; } QVariant variant = prop->data(VProperty::DPC_Data, Qt::DisplayRole); QString id = propertyToId[prop]; switch (currentItem->type()) { case VToolSinglePoint::Type: { if (id == QLatin1String("name")) { SetPointName(variant.toString()); } else if (id == QLatin1String("position")) { currentItem->setPos(variant.toPointF()); } break; } case VToolEndLine::Type: { VToolEndLine *i = qgraphicsitem_cast(currentItem); if (id == QLatin1String("name")) { SetPointName(variant.toString()); } else if (id == QLatin1String("basePoint")) { i->setBasePointId(variant.toUInt()); } else if (id == QLatin1String("lineType")) { i->setTypeLine(variant.toString()); } else if (id == QLatin1String("formulaLength")) { VFormula formula = variant.value(); if (formula.error() == false) { i->setFormulaLength(variant.value().getFormula(FormulaType::FromUser)); } } else if (id == QLatin1String("formulaAngle")) { VFormula formula = variant.value(); if (formula.error() == false) { i->setFormulaAngle(variant.value().getFormula(FormulaType::FromUser)); } } break; } default: break; } qApp->getSceneView()->update(); } template void VToolOptionsPropertyBrowser::SetPointName(const QString &name) { if (Tool *i = qgraphicsitem_cast(currentItem)) { if (name == i->name()) { return; } if (name.isEmpty()) { idToProperty[QLatin1String("name")]->setValue(i->name()); } else { //TODO check if label name is unique i->setName(name); } } else { qWarning()<<"Can't cast item"; } } //--------------------------------------------------------------------------------------------------------------------- void VToolOptionsPropertyBrowser::UpdateOptions() { if (currentItem == nullptr) { return; } switch (currentItem->type()) { case VToolSinglePoint::Type: { VToolSinglePoint *i = qgraphicsitem_cast(currentItem); idToProperty[QLatin1String("name")]->setValue(i->name()); idToProperty[QLatin1String("position")]->setValue(i->pos()); break; } case VToolEndLine::Type: { VToolEndLine *i = qgraphicsitem_cast(currentItem); idToProperty[QLatin1String("name")]->setValue(i->name()); idToProperty[QLatin1String("basePoint")]->setValue(i->getBasePointId()); QStringList styles = VAbstractTool::Styles(); qint32 index = styles.indexOf(i->getLineType()); idToProperty[QLatin1String("lineType")]->setValue(index); VFormula formula(i->getFormulaLength(), i->getData()); formula.setCheckZero(true); formula.setToolId(i->getId()); formula.setPostfix(VDomDocument::UnitsToStr(qApp->patternUnit())); QVariant valueFormula; valueFormula.setValue(formula); idToProperty[QLatin1String("formulaLength")]->setValue(valueFormula); VFormula formulaAngle(i->getFormulaAngle(), i->getData()); formulaAngle.setCheckZero(false); formulaAngle.setToolId(i->getId()); formulaAngle.setPostfix(QStringLiteral("°")); QVariant valueAngle; valueAngle.setValue(formulaAngle); idToProperty[QLatin1String("formulaAngle")]->setValue(valueAngle); break; } case VGraphicsSimpleTextItem::Type: ShowItemOptions(currentItem->parentItem()); break; case VControlPointSpline::Type: ShowItemOptions(currentItem->parentItem()); break; default: break; } } //--------------------------------------------------------------------------------------------------------------------- void VToolOptionsPropertyBrowser::AddProperty(VProperty *property, const QString &id) { propertyToId[property] = id; idToProperty[id] = property; PropertyModel->addProperty(property, id); } //--------------------------------------------------------------------------------------------------------------------- void VToolOptionsPropertyBrowser::ShowItemOptions(QGraphicsItem *item) { switch (item->type()) { case VToolSinglePoint::Type: { VToolSinglePoint *i = qgraphicsitem_cast(item); TreeView->setTitle(tr("Base point")); VProperty* itemName = new VProperty(tr("Point name")); itemName->setValue(i->name()); AddProperty(itemName, QLatin1String("name")); VPointFProperty* itemPosition = new VPointFProperty(tr("Position")); itemPosition->setValue(i->pos()); AddProperty(itemPosition, QLatin1String("position")); break; } case VToolEndLine::Type: { VToolEndLine *i = qgraphicsitem_cast(item); TreeView->setTitle(tr("Point at distance and angle")); VProperty* itemName = new VProperty(tr("Point name")); itemName->setValue(i->name()); AddProperty(itemName, QLatin1String("name")); VObjectProperty *pointsProperty = new VObjectProperty(tr("Base point")); QMap pointsList = i->PointsList(); pointsProperty->setObjectsList(pointsList); pointsProperty->setValue(i->getBasePointId()); AddProperty(pointsProperty, QLatin1String("basePoint")); VEnumProperty *lineTypeProperty = new VEnumProperty(tr("Line type")); lineTypeProperty->setLiterals(VAbstractTool::Styles()); QStringList styles = VAbstractTool::Styles(); qint32 index = styles.indexOf(i->getLineType()); lineTypeProperty->setValue(index); AddProperty(lineTypeProperty, QLatin1String("lineType")); VFormulaProperty* itemLength = new VFormulaProperty(tr("Length")); VFormula formulaLength(i->getFormulaLength(), i->getData()); formulaLength.setCheckZero(true); formulaLength.setToolId(i->getId()); formulaLength.setPostfix(VDomDocument::UnitsToStr(qApp->patternUnit())); itemLength->setFormula(formulaLength); AddProperty(itemLength, QLatin1String("formulaLength")); VFormulaProperty* itemAngle = new VFormulaProperty(tr("Angle")); VFormula formulaAngle(i->getFormulaAngle(), i->getData()); formulaAngle.setCheckZero(false); formulaAngle.setToolId(i->getId()); formulaAngle.setPostfix(QStringLiteral("°")); itemAngle->setFormula(formulaAngle); AddProperty(itemAngle, QLatin1String("formulaAngle")); break; } case VGraphicsSimpleTextItem::Type: currentItem = item->parentItem(); ShowItemOptions(currentItem); break; case VControlPointSpline::Type: currentItem = item->parentItem(); ShowItemOptions(currentItem); break; default: break; } }