diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index 4e57c9518..c473e0165 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -45,7 +45,8 @@ enum class Source : char { FromGui, FromFile, FromTool }; enum class Tool : unsigned char { Arrow, - Point, + SinglePoint, + DoublePoint, LinePoint, AbstractSpline, Cut, @@ -79,14 +80,15 @@ enum class Tool : unsigned char PointOfIntersection, PointFromCircleAndTangent, PointFromArcAndTangent, - UnionDetails // 35 + UnionDetails // 36 }; enum class Vis : unsigned char { - ControlPointSpline = 36, // increase this value if need more positions in Tool enum + ControlPointSpline = 37, // increase this value if need more positions in Tool enum GraphicsSimpleTextItem, SimpleSplinePath, + SimplePoint, Line, Path, ToolAlongLine, diff --git a/src/libs/vpatterndb/vcontainer.cpp b/src/libs/vpatterndb/vcontainer.cpp index 14d90b9ef..cd4d14b68 100644 --- a/src/libs/vpatterndb/vcontainer.cpp +++ b/src/libs/vpatterndb/vcontainer.cpp @@ -95,6 +95,15 @@ const QSharedPointer VContainer::GetGObject(quint32 id)const return GetObject(d->gObjects, id); } +//--------------------------------------------------------------------------------------------------------------------- +const QSharedPointer VContainer::GetFakeGObject(quint32 id) const +{ + VGObject *obj = new VGObject(); + obj->setId(id); + QSharedPointer pointer(obj); + return pointer; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetObject return object from container diff --git a/src/libs/vpatterndb/vcontainer.h b/src/libs/vpatterndb/vcontainer.h index 3e5260afc..78e4aa2f3 100644 --- a/src/libs/vpatterndb/vcontainer.h +++ b/src/libs/vpatterndb/vcontainer.h @@ -103,6 +103,7 @@ public: template const QSharedPointer GeometricObject(const quint32 &id) const; const QSharedPointer GetGObject(quint32 id) const; + const QSharedPointer GetFakeGObject(quint32 id) const; const VDetail GetDetail(quint32 id) const; qreal GetTableValue(const QString& name, MeasurementsType patternType) const; template diff --git a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp new file mode 100644 index 000000000..0e9c868d1 --- /dev/null +++ b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp @@ -0,0 +1,305 @@ +/************************************************************************ + ** + ** @file vtooldoublepoint.cpp + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 "vtooldoublepoint.h" +#include "../vwidgets/vsimplepoint.h" +#include "../vgeometry/vpointf.h" +#include "../../../../undocommands/movelabel.h" + +//--------------------------------------------------------------------------------------------------------------------- +VToolDoublePoint::VToolDoublePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 p1id, quint32 p2id, + QGraphicsItem *parent) + :VAbstractPoint(doc, data, id), QGraphicsPathItem(parent), firstPoint(nullptr), secondPoint(nullptr), p1id(p1id), + p2id(p2id) +{ + firstPoint = new VSimplePoint(p1id, QColor(baseColor), *data->GetPatternUnit(), &factor); + firstPoint->setParentItem(this); + connect(firstPoint, &VSimplePoint::Choosed, this, &VToolDoublePoint::Point1Choosed); + connect(firstPoint, &VSimplePoint::ShowContextMenu, this, &VToolDoublePoint::contextMenuEvent); + connect(firstPoint, &VSimplePoint::Delete, this, &VToolDoublePoint::DeleteFromLabel); + connect(firstPoint, &VSimplePoint::NameChangedPosition, this, &VToolDoublePoint::Label1ChangePosition); + + secondPoint = new VSimplePoint(p2id, QColor(baseColor), *data->GetPatternUnit(), &factor); + secondPoint->setParentItem(this); + connect(secondPoint, &VSimplePoint::Choosed, this, &VToolDoublePoint::Point2Choosed); + connect(firstPoint, &VSimplePoint::ShowContextMenu, this, &VToolDoublePoint::contextMenuEvent); + connect(firstPoint, &VSimplePoint::Delete, this, &VToolDoublePoint::DeleteFromLabel); + connect(firstPoint, &VSimplePoint::NameChangedPosition, this, &VToolDoublePoint::Label2ChangePosition); +} + +//--------------------------------------------------------------------------------------------------------------------- +VToolDoublePoint::~VToolDoublePoint() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + /* From question on StackOverflow + * https://stackoverflow.com/questions/10985028/how-to-remove-border-around-qgraphicsitem-when-selected + * + * There's no interface to disable the drawing of the selection border for the build-in QGraphicsItems. The only way + * I can think of is derive your own items from the build-in ones and override the paint() function:*/ + QStyleOptionGraphicsItem myOption(*option); + myOption.state &= ~QStyle::State_Selected; + QGraphicsPathItem::paint(painter, &myOption, widget); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VToolDoublePoint::nameP1() const +{ + return PointName(p1id); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::setNameP1(const QString &name) +{ + SetPointName(p1id, name); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VToolDoublePoint::nameP2() const +{ + return PointName(p2id); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::setNameP2(const QString &name) +{ + SetPointName(p2id, name); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::SetEnabled(bool enabled) +{ + SetToolEnabled(this, enabled); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::Label1ChangePosition(const QPointF &pos) +{ + ChangePosition(this, p1id, pos); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::Label2ChangePosition(const QPointF &pos) +{ + ChangePosition(this, p2id, pos); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::SetFactor(qreal factor) +{ + VDrawTool::SetFactor(factor); + firstPoint->RefreshGeometry(*VAbstractTool::data.GeometricObject(p1id)); + secondPoint->RefreshGeometry(*VAbstractTool::data.GeometricObject(p2id)); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::Disable(bool disable, const QString &namePP) +{ + enabled = !CorrectDisable(disable, namePP); + this->SetEnabled(enabled); + firstPoint->setEnabled(enabled); + secondPoint->setEnabled(enabled); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::EnableToolMove(bool move) +{ + firstPoint->EnableToolMove(move); + secondPoint->EnableToolMove(move); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::Point1Choosed() +{ + emit ChoosedTool(p1id, SceneObject::Point); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::Point2Choosed() +{ + emit ChoosedTool(p2id, SceneObject::Point); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::FullUpdateFromFile() +{ + ReadAttributes(); + firstPoint->RefreshGeometry(*VAbstractTool::data.GeometricObject(p1id)); + secondPoint->RefreshGeometry(*VAbstractTool::data.GeometricObject(p2id)); + SetVisualization(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::UpdateNamePosition() +{ + qApp->getUndoStack()->beginMacro("move labels"); + + VPointF *p1 = VAbstractTool::data.GeometricObject(p1id).data(); + MoveLabel *moveLabel1 = new MoveLabel(doc, p1->mx(), p1->my(), p1id, this->scene()); + connect(moveLabel1, &MoveLabel::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); + qApp->getUndoStack()->push(moveLabel1); + + VPointF *p2 = VAbstractTool::data.GeometricObject(p2id).data(); + MoveLabel *moveLabel2 = new MoveLabel(doc, p2->mx(), p2->my(), p2id, this->scene()); + connect(moveLabel2, &MoveLabel::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); + qApp->getUndoStack()->push(moveLabel2); + + qApp->getUndoStack()->endMacro(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::RefreshLine() +{ + firstPoint->RefreshLine(); + secondPoint->RefreshLine(); +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief itemChange hadle item change. + * @param change change. + * @param value value. + * @return value. + */ +QVariant VToolDoublePoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +{ + if (change == QGraphicsItem::ItemSelectedChange) + { + if (value == true) + { + // do stuff if selected + this->setFocus(); + } + else + { + // do stuff if not selected + } + } + + return QGraphicsItem::itemChange(change, value); +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief keyReleaseEvent handle key release events. + * @param event key release event. + */ +void VToolDoublePoint::keyReleaseEvent(QKeyEvent *event) +{ + switch (event->key()) + { + case Qt::Key_Delete: + DeleteTool(); + return; //Leave this method immediately after call!!! + default: + break; + } + QGraphicsPathItem::keyReleaseEvent ( event ); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + Q_UNUSED(event) +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::SaveOptions(QDomElement &tag, QSharedPointer &obj) +{ + VDrawTool::SaveOptions(tag, obj); + + if (obj->id() == p1id) + { + QSharedPointer point = qSharedPointerDynamicCast(obj); + SCASSERT(point.isNull() == false); + + doc->SetAttribute(tag, AttrName1, point->name()); + doc->SetAttribute(tag, AttrMx1, qApp->fromPixel(point->mx())); + doc->SetAttribute(tag, AttrMy1, qApp->fromPixel(point->my())); + + VPointF *p = VAbstractTool::data.GeometricObject(p2id).data(); + + doc->SetAttribute(tag, AttrName2, p->name()); + doc->SetAttribute(tag, AttrMx2, qApp->fromPixel(p->mx())); + doc->SetAttribute(tag, AttrMy2, qApp->fromPixel(p->my())); + } + else if (obj->id() == p2id) + { + QSharedPointer point = qSharedPointerDynamicCast(obj); + SCASSERT(point.isNull() == false); + + doc->SetAttribute(tag, AttrName2, point->name()); + doc->SetAttribute(tag, AttrMx2, qApp->fromPixel(point->mx())); + doc->SetAttribute(tag, AttrMy2, qApp->fromPixel(point->my())); + + VPointF *p = VAbstractTool::data.GeometricObject(p1id).data(); + + doc->SetAttribute(tag, AttrName1, p->name()); + doc->SetAttribute(tag, AttrMx1, qApp->fromPixel(p->mx())); + doc->SetAttribute(tag, AttrMy1, qApp->fromPixel(p->my())); + } + else + { + VPointF *p1 = VAbstractTool::data.GeometricObject(p1id).data(); + VPointF *p2 = VAbstractTool::data.GeometricObject(p1id).data(); + + doc->SetAttribute(tag, AttrName1, p1->name()); + doc->SetAttribute(tag, AttrMx1, qApp->fromPixel(p1->mx())); + doc->SetAttribute(tag, AttrMy1, qApp->fromPixel(p1->my())); + + doc->SetAttribute(tag, AttrName2, p2->name()); + doc->SetAttribute(tag, AttrMx2, qApp->fromPixel(p2->mx())); + doc->SetAttribute(tag, AttrMy2, qApp->fromPixel(p2->my())); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::AddToFile() +{ + QDomElement domElement = doc->createElement(getTagName()); + QSharedPointer obj = VAbstractTool::data.GetFakeGObject(id); + SaveOptions(domElement, obj); + AddToCalculation(domElement); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::RefreshDataInFile() +{ + QDomElement domElement = doc->elementById(id); + if (domElement.isElement()) + { + QSharedPointer obj = VAbstractTool::data.GetFakeGObject(id); + SaveOptions(domElement, obj); + } + else + { + qCDebug(vTool, "Can't find tool with id = %u", id); + } +} diff --git a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h new file mode 100644 index 000000000..bbee41cf7 --- /dev/null +++ b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h @@ -0,0 +1,91 @@ +/************************************************************************ + ** + ** @file vtooldoublepoint.h + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 . + ** + *************************************************************************/ + +#ifndef VTOOLDOUBLEPOINT_H +#define VTOOLDOUBLEPOINT_H + +#include "../vabstractpoint.h" + +#include + +class VPointF; +class VGraphicsSimpleTextItem; +class VSimplePoint; + +class VToolDoublePoint: public VAbstractPoint, public QGraphicsPathItem +{ + Q_OBJECT +public: + VToolDoublePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 p1id, quint32 p2id, + QGraphicsItem * parent = nullptr); + virtual ~VToolDoublePoint(); + + virtual int type() const {return Type;} + enum { Type = UserType + static_cast(Tool::DoublePoint)}; + + virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0); + + QString nameP1() const; + void setNameP1(const QString &name); + + QString nameP2() const; + void setNameP2(const QString &name); + + void SetEnabled(bool enabled); + +public slots: + void Label1ChangePosition(const QPointF &pos); + void Label2ChangePosition(const QPointF &pos); + virtual void SetFactor(qreal factor); + virtual void Disable(bool disable, const QString &namePP); + virtual void EnableToolMove(bool move); + void Point1Choosed(); + void Point2Choosed(); + virtual void FullUpdateFromFile(); + +protected: + VSimplePoint *firstPoint; + VSimplePoint *secondPoint; + + quint32 p1id; + quint32 p2id; + + virtual void UpdateNamePosition(); + virtual void RefreshLine(); + virtual QVariant itemChange ( GraphicsItemChange change, const QVariant &value ); + virtual void keyReleaseEvent(QKeyEvent * event); + virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ); + virtual void SaveOptions(QDomElement &tag, QSharedPointer &obj); + virtual void AddToFile(); + virtual void RefreshDataInFile(); + +private: + Q_DISABLE_COPY(VToolDoublePoint) +}; + +#endif // VTOOLDOUBLEPOINT_H diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp index 91708f352..46d1d79ac 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp @@ -36,8 +36,6 @@ Q_LOGGING_CATEGORY(vToolSinglePoint, "v.toolSinglePoint") -const QString VToolSinglePoint::TagName = QStringLiteral("point"); - //--------------------------------------------------------------------------------------------------------------------- /** * @brief VToolSinglePoint constructor. @@ -47,16 +45,15 @@ const QString VToolSinglePoint::TagName = QStringLiteral("point"); * @param parent parent object. */ VToolSinglePoint::VToolSinglePoint(VAbstractPattern *doc, VContainer *data, quint32 id, QGraphicsItem *parent) - :VAbstractPoint(doc, data, id), QGraphicsEllipseItem(parent), radius(DefPointRadius), namePoint(nullptr), - lineName(nullptr) + :VAbstractPoint(doc, data, id), QGraphicsEllipseItem(parent), radius(ToPixel(DefPointRadius/*mm*/, Unit::Mm)), + namePoint(nullptr), lineName(nullptr) { - radius = ToPixel(DefPointRadius/*mm*/, Unit::Mm); namePoint = new VGraphicsSimpleTextItem(this); connect(namePoint, &VGraphicsSimpleTextItem::ShowContextMenu, this, &VToolSinglePoint::contextMenuEvent); connect(namePoint, &VGraphicsSimpleTextItem::DeleteTool, this, &VToolSinglePoint::DeleteFromLabel); connect(namePoint, &VGraphicsSimpleTextItem::PointChoosed, this, &VToolSinglePoint::PointChoosed); - lineName = new QGraphicsLineItem(this); connect(namePoint, &VGraphicsSimpleTextItem::NameChangePosition, this, &VToolSinglePoint::NameChangePosition); + lineName = new QGraphicsLineItem(this); this->setBrush(QBrush(Qt::NoBrush)); this->setFlag(QGraphicsItem::ItemIsSelectable, true); this->setFlag(QGraphicsItem::ItemIsFocusable, true); @@ -84,31 +81,19 @@ void VToolSinglePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem * //--------------------------------------------------------------------------------------------------------------------- QString VToolSinglePoint::name() const { - try - { - return VAbstractTool::data.GeometricObject(id)->name(); - } - catch (const VExceptionBadId &e) - { - qCDebug(vToolSinglePoint, "Error! Couldn't get point name. %s %s", e.ErrorMessage().toUtf8().constData(), - e.DetailedInformation().toUtf8().constData()); - return QString("");// Return empty string for property browser - } + return PointName(id); } //--------------------------------------------------------------------------------------------------------------------- void VToolSinglePoint::setName(const QString &name) { - // Don't know if need check name here. - QSharedPointer obj = VAbstractTool::data.GetGObject(id); - obj->setName(name); - SaveOption(obj); + SetPointName(id, name); } //--------------------------------------------------------------------------------------------------------------------- -QString VToolSinglePoint::getTagName() const +void VToolSinglePoint::SetEnabled(bool enabled) { - return VToolSinglePoint::TagName; + SetToolEnabled(this, enabled); } //--------------------------------------------------------------------------------------------------------------------- @@ -118,13 +103,7 @@ QString VToolSinglePoint::getTagName() const */ void VToolSinglePoint::NameChangePosition(const QPointF &pos) { - VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(id)); - QPointF p = pos - this->pos(); - point->setMx(p.x()); - point->setMy(p.y()); - RefreshLine(); - UpdateNamePosition(point->mx(), point->my()); - VAbstractTool::data.UpdateGObject(id, point); + ChangePosition(this, id, pos); } //--------------------------------------------------------------------------------------------------------------------- @@ -133,24 +112,14 @@ void VToolSinglePoint::NameChangePosition(const QPointF &pos) * @param mx label bias x axis. * @param my label bias y axis. */ -void VToolSinglePoint::UpdateNamePosition(qreal mx, qreal my) +void VToolSinglePoint::UpdateNamePosition() { - MoveLabel *moveLabel = new MoveLabel(doc, mx, my, id, this->scene()); + VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(id)); + MoveLabel *moveLabel = new MoveLabel(doc, point->mx(), point->my(), id, this->scene()); connect(moveLabel, &MoveLabel::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); qApp->getUndoStack()->push(moveLabel); } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief ShowTool highlight tool. - * @param id object id in container. - * @param enable enable or disable highlight. - */ -void VToolSinglePoint::ShowTool(quint32 id, bool enable) -{ - ShowItem(this, id, enable); -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetFactor set current scale factor of scene. @@ -166,16 +135,10 @@ void VToolSinglePoint::SetFactor(qreal factor) void VToolSinglePoint::Disable(bool disable, const QString &namePP) { enabled = !CorrectDisable(disable, namePP); - this->setEnabled(enabled); + this->SetEnabled(enabled); namePoint->setEnabled(enabled); } -//--------------------------------------------------------------------------------------------------------------------- -void VToolSinglePoint::DeleteFromLabel() -{ - DeleteTool(); //Leave this method immediately after call!!! -} - //--------------------------------------------------------------------------------------------------------------------- void VToolSinglePoint::EnableToolMove(bool move) { @@ -275,10 +238,10 @@ void VToolSinglePoint::RefreshLine() nRec.translate(- scenePos()); if (this->rect().intersects(nRec) == false) { - QRectF nameRec = namePoint->sceneBoundingRect(); + const QRectF nameRec = namePoint->sceneBoundingRect(); QPointF p1, p2; VGObject::LineIntersectCircle(QPointF(), radius, QLineF(QPointF(), nameRec.center() - scenePos()), p1, p2); - QPointF pRec = VGObject::LineIntersectRect(nameRec, QLineF(scenePos(), nameRec.center())); + const QPointF pRec = VGObject::LineIntersectRect(nameRec, QLineF(scenePos(), nameRec.center())); lineName->setLine(QLineF(p1, pRec - scenePos())); lineName->setPen(QPen(CorrectColor(Qt::black), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); @@ -359,17 +322,3 @@ void VToolSinglePoint::SaveOptions(QDomElement &tag, QSharedPointer &o doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx())); doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my())); } - -//--------------------------------------------------------------------------------------------------------------------- -void VToolSinglePoint::setEnabled(bool enabled) -{ - QGraphicsEllipseItem::setEnabled(enabled); - if (enabled) - { - setPen(QPen(QColor(baseColor), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); - } - else - { - setPen(QPen(Qt::gray, qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); - } -} diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h index 42847e66c..b3ae98ecc 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h @@ -44,19 +44,20 @@ class VToolSinglePoint: public VAbstractPoint, public QGraphicsEllipseItem public: VToolSinglePoint(VAbstractPattern *doc, VContainer *data, quint32 id, QGraphicsItem * parent = nullptr); virtual ~VToolSinglePoint(); + + virtual int type() const {return Type;} + enum { Type = UserType + static_cast(Tool::SinglePoint)}; + virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0); QString name() const; void setName(const QString &name); - virtual QString getTagName() const; - static const QString TagName; - void setEnabled(bool enabled); + void SetEnabled(bool enabled); + public slots: void NameChangePosition(const QPointF &pos); - virtual void ShowTool(quint32 id, bool enable); virtual void SetFactor(qreal factor); virtual void Disable(bool disable, const QString &namePP); - void DeleteFromLabel(); virtual void EnableToolMove(bool move); void PointChoosed(); virtual void FullUpdateFromFile(); @@ -70,41 +71,16 @@ protected: /** @brief lineName line what we see if label moved too away from point. */ QGraphicsLineItem *lineName; - virtual void UpdateNamePosition(qreal mx, qreal my); + virtual void UpdateNamePosition(); virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ); virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ); virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ); virtual void RefreshPointGeometry(const VPointF &point); - void RefreshLine(); + virtual void RefreshLine(); virtual QVariant itemChange ( GraphicsItemChange change, const QVariant &value ); virtual void keyReleaseEvent(QKeyEvent * event); virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ); virtual void SaveOptions(QDomElement &tag, QSharedPointer &obj); - - template - void ShowToolVisualization(bool show) - { - if (show) - { - if (vis == nullptr) - { - AddVisualization(); - SetVisualization(); - } - else - { - if (T *visual = qobject_cast(vis)) - { - visual->show(); - } - } - } - else - { - delete vis; - vis = nullptr; - } - } private: Q_DISABLE_COPY(VToolSinglePoint) }; diff --git a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.cpp index 4b026f209..464d4989e 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.cpp @@ -27,6 +27,9 @@ *************************************************************************/ #include "vabstractpoint.h" +#include "../vgeometry/vpointf.h" + +const QString VAbstractPoint::TagName = QStringLiteral("point"); //--------------------------------------------------------------------------------------------------------------------- VAbstractPoint::VAbstractPoint(VAbstractPattern *doc, VContainer *data, quint32 id) @@ -36,3 +39,50 @@ VAbstractPoint::VAbstractPoint(VAbstractPattern *doc, VContainer *data, quint32 //--------------------------------------------------------------------------------------------------------------------- VAbstractPoint::~VAbstractPoint() {} + +//--------------------------------------------------------------------------------------------------------------------- +QString VAbstractPoint::getTagName() const +{ + return VAbstractPoint::TagName; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ShowTool highlight tool. + * @param id object id in container. + * @param enable enable or disable highlight. + */ +void VAbstractPoint::ShowTool(quint32 id, bool enable) +{ + ShowItem(this, id, enable); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPoint::DeleteFromLabel() +{ + DeleteTool(); //Leave this method immediately after call!!! +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VAbstractPoint::PointName(quint32 id) const +{ + try + { + return VAbstractTool::data.GeometricObject(id)->name(); + } + catch (const VExceptionBadId &e) + { + qCDebug(vTool, "Error! Couldn't get point name. %s %s", e.ErrorMessage().toUtf8().constData(), + e.DetailedInformation().toUtf8().constData()); + return QString("");// Return empty string for property browser + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPoint::SetPointName(quint32 id, const QString &name) +{ + // Don't know if need check name here. + QSharedPointer obj = VAbstractTool::data.GetGObject(id); + obj->setName(name); + SaveOption(obj); +} diff --git a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h index d787610a6..2fe636043 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h +++ b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h @@ -30,12 +30,95 @@ #define VABSTRACTPOINT_H #include "../vdrawtool.h" +#include "../vgeometry/vpointf.h" class VAbstractPoint: public VDrawTool { + Q_OBJECT public: VAbstractPoint(VAbstractPattern *doc, VContainer *data, quint32 id); virtual ~VAbstractPoint(); + + virtual QString getTagName() const; + static const QString TagName; + + template + void ShowToolVisualization(bool show); + +public slots: + virtual void ShowTool(quint32 id, bool enable); + void DeleteFromLabel(); + +protected: + QString PointName(quint32 id) const; + void SetPointName(quint32 id, const QString &name); + + template + void ChangePosition(T *item, quint32 id, const QPointF &pos); + + virtual void UpdateNamePosition()=0; + virtual void RefreshLine()=0; + + template + void SetToolEnabled(T *item, bool enabled); + +private: + Q_DISABLE_COPY(VAbstractPoint) }; +//--------------------------------------------------------------------------------------------------------------------- +template +void VAbstractPoint::ShowToolVisualization(bool show) +{ + if (show) + { + if (vis == nullptr) + { + AddVisualization(); + SetVisualization(); + } + else + { + if (T *visual = qobject_cast(vis)) + { + visual->show(); + } + } + } + else + { + delete vis; + vis = nullptr; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +template +void VAbstractPoint::SetToolEnabled(T *item, bool enabled) +{ + item->setEnabled(enabled); + if (enabled) + { + item->setPen(QPen(QColor(baseColor), + qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); + } + else + { + item->setPen(QPen(Qt::gray, qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +template +void VAbstractPoint::ChangePosition(T *item, quint32 id, const QPointF &pos) +{ + VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(id)); + const QPointF p = pos - item->pos(); + point->setMx(p.x()); + point->setMy(p.y()); + RefreshLine(); + VAbstractTool::data.UpdateGObject(id, point); + UpdateNamePosition(); +} + #endif // VABSTRACTPOINT_H diff --git a/src/libs/vtools/tools/drawTools/vdrawtool.h b/src/libs/vtools/tools/drawTools/vdrawtool.h index 1057af0f7..5da42ccc6 100644 --- a/src/libs/vtools/tools/drawTools/vdrawtool.h +++ b/src/libs/vtools/tools/drawTools/vdrawtool.h @@ -105,74 +105,83 @@ protected: virtual void ReadToolAttributes(const QDomElement &domElement)=0; template - /** - * @brief ContextMenu show context menu for tool. - * @param tool tool. - * @param event context menu event. - * @param showRemove true - tool have option delete. - */ - void ContextMenu(Tool *tool, QGraphicsSceneContextMenuEvent *event, bool showRemove = true) - { - SCASSERT(tool != nullptr); - SCASSERT(event != nullptr); + void ContextMenu(Tool *tool, QGraphicsSceneContextMenuEvent *event, bool showRemove = true); - QMenu menu; - QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Options")); - QAction *actionRemove = nullptr; - actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete")); - if (showRemove) - { - if (_referens > 1) - { - actionRemove->setEnabled(false); - } - else - { - actionRemove->setEnabled(true); - } - } - else - { - actionRemove->setEnabled(false); - } - - QAction *selectedAction = menu.exec(event->screenPos()); - if (selectedAction == actionOption) - { - qApp->getSceneView()->itemClicked(nullptr); - dialog = new Dialog(getData(), id, qApp->getMainWindow()); - dialog->setModal(true); - - connect(dialog, &DialogTool::DialogClosed, tool, &Tool::FullUpdateFromGuiOk); - connect(dialog, &DialogTool::DialogApplied, tool, &Tool::FullUpdateFromGuiApply); - - tool->setDialog(); - - dialog->show(); - } - if (selectedAction == actionRemove) - { - DeleteTool(); - return; //Leave this method immediately after call!!! - } - } template - /** - * @brief ShowItem highlight tool. - * @param item tool. - * @param id object id in container. - * @param enable enable or disable highlight. - */ - void ShowItem(Item *item, quint32 id, bool enable) - { - SCASSERT(item != nullptr); - if (id == item->id) - { - ShowVisualization(enable); - } - } + void ShowItem(Item *item, quint32 id, bool enable); private: Q_DISABLE_COPY(VDrawTool) }; +//--------------------------------------------------------------------------------------------------------------------- +template +/** + * @brief ContextMenu show context menu for tool. + * @param tool tool. + * @param event context menu event. + * @param showRemove true - tool have option delete. + */ +void VDrawTool::ContextMenu(Tool *tool, QGraphicsSceneContextMenuEvent *event, bool showRemove) +{ + SCASSERT(tool != nullptr); + SCASSERT(event != nullptr); + + QMenu menu; + QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Options")); + QAction *actionRemove = nullptr; + actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete")); + if (showRemove) + { + if (_referens > 1) + { + actionRemove->setEnabled(false); + } + else + { + actionRemove->setEnabled(true); + } + } + else + { + actionRemove->setEnabled(false); + } + + QAction *selectedAction = menu.exec(event->screenPos()); + if (selectedAction == actionOption) + { + qApp->getSceneView()->itemClicked(nullptr); + dialog = new Dialog(getData(), id, qApp->getMainWindow()); + dialog->setModal(true); + + connect(dialog, &DialogTool::DialogClosed, tool, &Tool::FullUpdateFromGuiOk); + connect(dialog, &DialogTool::DialogApplied, tool, &Tool::FullUpdateFromGuiApply); + + tool->setDialog(); + + dialog->show(); + } + if (selectedAction == actionRemove) + { + DeleteTool(); + return; //Leave this method immediately after call!!! + } +} + +//--------------------------------------------------------------------------------------------------------------------- +template +/** + * @brief ShowItem highlight tool. + * @param item tool. + * @param id object id in container. + * @param enable enable or disable highlight. + */ +void VDrawTool::ShowItem(Item *item, quint32 id, bool enable) +{ + SCASSERT(item != nullptr); + if (id == item->id) + { + ShowVisualization(enable); + } +} + #endif // VDRAWTOOL_H diff --git a/src/libs/vtools/tools/tools.pri b/src/libs/vtools/tools/tools.pri index 68912f576..334d53bf0 100644 --- a/src/libs/vtools/tools/tools.pri +++ b/src/libs/vtools/tools/tools.pri @@ -44,7 +44,8 @@ HEADERS += \ $$PWD/drawTools/toolpoint/toolsinglepoint/vtoolpointfromcircleandtangent.h \ $$PWD/drawTools/toolpoint/toolsinglepoint/vtoolpointfromarcandtangent.h \ $$PWD/drawTools/toolcurve/vtoolarcwithlength.h \ - $$PWD/drawTools/toolpoint/vabstractpoint.h + $$PWD/drawTools/toolpoint/vabstractpoint.h \ + $$PWD/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h SOURCES += \ $$PWD/vtooldetail.cpp \ @@ -86,4 +87,5 @@ SOURCES += \ $$PWD/drawTools/toolpoint/toolsinglepoint/vtoolpointfromcircleandtangent.cpp \ $$PWD/drawTools/toolpoint/toolsinglepoint/vtoolpointfromarcandtangent.cpp \ $$PWD/drawTools/toolcurve/vtoolarcwithlength.cpp \ - $$PWD/drawTools/toolpoint/vabstractpoint.cpp + $$PWD/drawTools/toolpoint/vabstractpoint.cpp \ + $$PWD/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp diff --git a/src/libs/vtools/tools/vabstracttool.cpp b/src/libs/vtools/tools/vabstracttool.cpp index 036bc2b40..493c3f9a6 100644 --- a/src/libs/vtools/tools/vabstracttool.cpp +++ b/src/libs/vtools/tools/vabstracttool.cpp @@ -44,6 +44,12 @@ const QString VAbstractTool::AttrType = QStringLiteral("type"); const QString VAbstractTool::AttrMx = QStringLiteral("mx"); const QString VAbstractTool::AttrMy = QStringLiteral("my"); const QString VAbstractTool::AttrName = QStringLiteral("name"); +const QString VAbstractTool::AttrMx1 = QStringLiteral("mx1"); +const QString VAbstractTool::AttrMy1 = QStringLiteral("my1"); +const QString VAbstractTool::AttrName1 = QStringLiteral("name1"); +const QString VAbstractTool::AttrMx2 = QStringLiteral("mx2"); +const QString VAbstractTool::AttrMy2 = QStringLiteral("my2"); +const QString VAbstractTool::AttrName2 = QStringLiteral("name2"); const QString VAbstractTool::AttrX = QStringLiteral("x"); const QString VAbstractTool::AttrY = QStringLiteral("y"); const QString VAbstractTool::AttrTypeLine = QStringLiteral("typeLine"); diff --git a/src/libs/vtools/tools/vabstracttool.h b/src/libs/vtools/tools/vabstracttool.h index 52ce8fd1b..a619a3ec9 100644 --- a/src/libs/vtools/tools/vabstracttool.h +++ b/src/libs/vtools/tools/vabstracttool.h @@ -58,6 +58,12 @@ public: static const QString AttrMx; static const QString AttrMy; static const QString AttrName; + static const QString AttrMx1; + static const QString AttrMy1; + static const QString AttrName1; + static const QString AttrMx2; + static const QString AttrMy2; + static const QString AttrName2; static const QString AttrX; static const QString AttrY; static const QString AttrTypeLine; diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index 255bd8719..bb14dd605 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -20,7 +20,6 @@ HEADERS += \ $$PWD/renamepp.h \ $$PWD/movelabel.h - SOURCES += \ $$PWD/addtocalc.cpp \ $$PWD/addpatternpiece.cpp \ @@ -39,4 +38,3 @@ SOURCES += \ $$PWD/vundocommand.cpp \ $$PWD/renamepp.cpp \ $$PWD/movelabel.cpp - diff --git a/src/libs/vwidgets/vabstractsimple.cpp b/src/libs/vwidgets/vabstractsimple.cpp new file mode 100644 index 000000000..1649df7f6 --- /dev/null +++ b/src/libs/vwidgets/vabstractsimple.cpp @@ -0,0 +1,52 @@ +/************************************************************************ + ** + ** @file vabstractsimple.cpp + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 "vabstractsimple.h" + +//--------------------------------------------------------------------------------------------------------------------- +VAbstractSimple::VAbstractSimple(quint32 id, const QColor ¤tColor, Unit patternUnit, qreal *factor, + QObject *parent) + :QObject(parent), id (id), factor(factor), currentColor(currentColor), enabled(true), patternUnit(patternUnit) +{} + +//--------------------------------------------------------------------------------------------------------------------- +VAbstractSimple::~VAbstractSimple() +{} + +//--------------------------------------------------------------------------------------------------------------------- +QColor VAbstractSimple::CorrectColor(const QColor &color) const +{ + if (enabled) + { + return color; + } + else + { + return Qt::gray; + } +} diff --git a/src/libs/vwidgets/vabstractsimple.h b/src/libs/vwidgets/vabstractsimple.h new file mode 100644 index 000000000..79e3bb221 --- /dev/null +++ b/src/libs/vwidgets/vabstractsimple.h @@ -0,0 +1,82 @@ +/************************************************************************ + ** + ** @file vabstractsimple.h + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 . + ** + *************************************************************************/ + +#ifndef VABSTRACTSIMPLE_H +#define VABSTRACTSIMPLE_H + +#include +#include "../vmisc/def.h" + +class VAbstractSimple : public QObject +{ + Q_OBJECT +public: + VAbstractSimple(quint32 id, const QColor ¤tColor, Unit patternUnit, qreal *factor = nullptr, + QObject *parent = 0); + virtual ~VAbstractSimple(); + + virtual void ChangedActivDraw(const bool &flag)=0; + +protected: + /** @brief id spline id. */ + quint32 id; + + /** @brief factor scale factor. */ + qreal *factor; + + /** @brief currentColor current color. */ + QColor currentColor; + + bool enabled; + + Unit patternUnit; + + QColor CorrectColor(const QColor &color) const; + + template + void SetPen(T *item, const QColor &color, qreal width); + +private: + Q_DISABLE_COPY(VAbstractSimple) +}; + +//--------------------------------------------------------------------------------------------------------------------- +template +void VAbstractSimple::SetPen(T *item, const QColor &color, qreal width) +{ + if (factor == nullptr) + { + item->setPen(QPen(CorrectColor(color), ToPixel(width, patternUnit))); + } + else + { + item->setPen(QPen(CorrectColor(color), ToPixel(width, patternUnit)/ *factor)); + } +} + +#endif // VABSTRACTSIMPLE_H diff --git a/src/libs/vwidgets/vsimplecurve.cpp b/src/libs/vwidgets/vsimplecurve.cpp index aecd703c5..e63ce5ce7 100644 --- a/src/libs/vwidgets/vsimplecurve.cpp +++ b/src/libs/vwidgets/vsimplecurve.cpp @@ -39,19 +39,11 @@ * @param currentColor current color. * @param parent parent object. */ -VSimpleCurve::VSimpleCurve(quint32 id, QColor currentColor, SimpleCurvePoint pointPosition, Unit patternUnit, +VSimpleCurve::VSimpleCurve(quint32 id, const QColor ¤tColor, SimpleCurvePoint pointPosition, Unit patternUnit, qreal *factor, QObject *parent) - :QObject(parent), QGraphicsPathItem(), id (id), factor(factor), currentColor(currentColor), - curvePosition(pointPosition), enabled(true), patternUnit(patternUnit) + :VAbstractSimple(id, currentColor, patternUnit, factor, parent), QGraphicsPathItem(), curvePosition(pointPosition) { - if (factor == nullptr) - { - setPen(QPen(currentColor, ToPixel(WidthHairLine(patternUnit), patternUnit))); - } - else - { - setPen(QPen(currentColor, ToPixel(WidthHairLine(patternUnit), patternUnit)/ *factor)); - } + SetPen(this, currentColor, WidthHairLine(patternUnit)); setFlag(QGraphicsItem::ItemIsSelectable, true); setAcceptHoverEvents(true); } @@ -61,7 +53,7 @@ void VSimpleCurve::ChangedActivDraw(const bool &flag) { enabled = flag; setEnabled(enabled); - setPen(QPen(CorrectColor(currentColor), ToPixel(WidthHairLine(patternUnit), patternUnit)/ *factor)); + SetPen(this, currentColor, WidthHairLine(patternUnit)); } //--------------------------------------------------------------------------------------------------------------------- @@ -93,20 +85,13 @@ void VSimpleCurve::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) //--------------------------------------------------------------------------------------------------------------------- /** - * @brief hoverMoveEvent handle hover move events. + * @brief hoverEnterEvent handle hover enter events. * @param event hover move event. */ -void VSimpleCurve::hoverMoveEvent(QGraphicsSceneHoverEvent *event) +void VSimpleCurve::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); - if (factor == nullptr) - { - this->setPen(QPen(CorrectColor(currentColor), ToPixel(WidthMainLine(patternUnit), patternUnit))); - } - else - { - this->setPen(QPen(CorrectColor(currentColor), ToPixel(WidthMainLine(patternUnit), patternUnit)/ *factor)); - } + SetPen(this, currentColor, WidthMainLine(patternUnit)); emit HoverPath(id, curvePosition, PathDirection::Show); } @@ -118,18 +103,16 @@ void VSimpleCurve::hoverMoveEvent(QGraphicsSceneHoverEvent *event) void VSimpleCurve::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); - if (factor == nullptr) - { - this->setPen(QPen(CorrectColor(currentColor), ToPixel(WidthHairLine(patternUnit), patternUnit))); - } - else - { - this->setPen(QPen(CorrectColor(currentColor), ToPixel(WidthHairLine(patternUnit), patternUnit)/ *factor)); - } - + SetPen(this, currentColor, WidthHairLine(patternUnit)); emit HoverPath(id, curvePosition, PathDirection::Hide); } +//--------------------------------------------------------------------------------------------------------------------- +void VSimpleCurve::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + event->ignore(); +} + //--------------------------------------------------------------------------------------------------------------------- // cppcheck-suppress unusedFunction QColor VSimpleCurve::GetCurrentColor() const @@ -141,24 +124,5 @@ QColor VSimpleCurve::GetCurrentColor() const void VSimpleCurve::SetCurrentColor(const QColor &value) { currentColor = value; - setPen(QPen(CorrectColor(currentColor), pen().widthF())); -} - -//--------------------------------------------------------------------------------------------------------------------- -void VSimpleCurve::mousePressEvent(QGraphicsSceneMouseEvent *event) -{ - event->ignore(); -} - -//--------------------------------------------------------------------------------------------------------------------- -QColor VSimpleCurve::CorrectColor(const QColor &color) const -{ - if (enabled) - { - return color; - } - else - { - return Qt::gray; - } + SetPen(this, CorrectColor(currentColor), pen().widthF()); } diff --git a/src/libs/vwidgets/vsimplecurve.h b/src/libs/vwidgets/vsimplecurve.h index 4f31eba73..c75cb655a 100644 --- a/src/libs/vwidgets/vsimplecurve.h +++ b/src/libs/vwidgets/vsimplecurve.h @@ -31,27 +31,27 @@ #include #include "../vgeometry/vabstractcurve.h" -#include "../vmisc/def.h" +#include "vabstractsimple.h" enum class SimpleCurvePoint : char { FirstPoint, ForthPoint }; /** * @brief The VSimpleSpline class for simple spline. This object used when we cut spline and want show peaces. */ -class VSimpleCurve : public QObject, public QGraphicsPathItem +class VSimpleCurve : public VAbstractSimple, public QGraphicsPathItem { Q_OBJECT public: - VSimpleCurve(quint32 id, QColor currentColor, SimpleCurvePoint curvePosition, Unit patternUnit, + VSimpleCurve(quint32 id, const QColor ¤tColor, SimpleCurvePoint curvePosition, Unit patternUnit, qreal *factor = nullptr, QObject *parent = 0); - void ChangedActivDraw(const bool &flag); + virtual void ChangedActivDraw(const bool &flag); virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0); virtual int type() const {return Type;} enum { Type = UserType + static_cast(Vis::SimpleSplinePath)}; QColor GetCurrentColor() const; - void SetCurrentColor(const QColor &value); + void SetCurrentColor(const QColor &value); signals: /** @@ -63,26 +63,15 @@ signals: protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent * event); virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ); - virtual void hoverMoveEvent ( QGraphicsSceneHoverEvent * event ); + virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ); virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ); private: Q_DISABLE_COPY(VSimpleCurve) - /** @brief id spline id. */ - quint32 id; - /** @brief factor scale factor. */ - qreal *factor; - - /** @brief currentColor current color. */ - QColor currentColor; SimpleCurvePoint curvePosition; - bool enabled; - Unit patternUnit; - - QColor CorrectColor(const QColor &color) const; }; #endif // VSIMPLECURVE_H diff --git a/src/libs/vwidgets/vsimplepoint.cpp b/src/libs/vwidgets/vsimplepoint.cpp new file mode 100644 index 000000000..ff4a731ff --- /dev/null +++ b/src/libs/vwidgets/vsimplepoint.cpp @@ -0,0 +1,210 @@ +/************************************************************************ + ** + ** @file vsimplepoint.cpp + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 "vsimplepoint.h" +#include "vgraphicssimpletextitem.h" +#include "../ifc/ifcdef.h" +#include "../vgeometry/vgobject.h" +#include "../vgeometry/vpointf.h" + +#include +#include + +//--------------------------------------------------------------------------------------------------------------------- +VSimplePoint::VSimplePoint(quint32 id, const QColor ¤tColor, Unit patternUnit, qreal *factor, QObject *parent) + :VAbstractSimple(id, currentColor, patternUnit, factor, parent), QGraphicsEllipseItem(), + radius(ToPixel(DefPointRadius/*mm*/, Unit::Mm)), namePoint(nullptr), lineName(nullptr) +{ + namePoint = new VGraphicsSimpleTextItem(this); + connect(namePoint, &VGraphicsSimpleTextItem::ShowContextMenu, this, &VSimplePoint::ContextMenu); + connect(namePoint, &VGraphicsSimpleTextItem::DeleteTool, this, &VSimplePoint::DeleteFromLabel); + connect(namePoint, &VGraphicsSimpleTextItem::PointChoosed, this, &VSimplePoint::PointChoosed); + connect(namePoint, &VGraphicsSimpleTextItem::NameChangePosition, this, &VSimplePoint::ChangedPosition); + lineName = new QGraphicsLineItem(this); + this->setBrush(QBrush(Qt::NoBrush)); + SetPen(this, currentColor, WidthHairLine(patternUnit)); + this->setFlag(QGraphicsItem::ItemIsSelectable, true); + this->setFlag(QGraphicsItem::ItemIsFocusable, true); + this->setAcceptHoverEvents(true); +} + +//--------------------------------------------------------------------------------------------------------------------- +VSimplePoint::~VSimplePoint() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::ChangedActivDraw(const bool &flag) +{ + enabled = flag; + setEnabled(enabled); + SetPen(this, currentColor, WidthHairLine(patternUnit)); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + /* From question on StackOverflow + * https://stackoverflow.com/questions/10985028/how-to-remove-border-around-qgraphicsitem-when-selected + * + * There's no interface to disable the drawing of the selection border for the build-in QGraphicsItems. The only way + * I can think of is derive your own items from the build-in ones and override the paint() function:*/ + QStyleOptionGraphicsItem myOption(*option); + myOption.state &= ~QStyle::State_Selected; + QGraphicsEllipseItem::paint(painter, &myOption, widget); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::RefreshLine() +{ + QRectF nRec = namePoint->sceneBoundingRect(); + nRec.translate(- scenePos()); + if (this->rect().intersects(nRec) == false) + { + const QRectF nameRec = namePoint->sceneBoundingRect(); + QPointF p1, p2; + VGObject::LineIntersectCircle(QPointF(), radius, QLineF(QPointF(), nameRec.center() - scenePos()), p1, p2); + const QPointF pRec = VGObject::LineIntersectRect(nameRec, QLineF(scenePos(), nameRec.center())); + lineName->setLine(QLineF(p1, pRec - scenePos())); + SetPen(lineName, QColor(Qt::black), WidthHairLine(patternUnit)); + + if (QLineF(p1, pRec - scenePos()).length() <= ToPixel(4, Unit::Mm)) + { + lineName->setVisible(false); + } + else + { + lineName->setVisible(true); + } + } + else + { + lineName->setVisible(false); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::RefreshGeometry(const VPointF &point) +{ + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); + SetPen(this, currentColor, WidthHairLine(patternUnit)); + QRectF rec = QRectF(0, 0, radius*2, radius*2); + rec.translate(-rec.center().x(), -rec.center().y()); + this->setRect(rec); + this->setPos(point.toQPointF()); + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); + namePoint->blockSignals(true); + QFont font = namePoint->font(); + if (factor == nullptr) + { + font.setPointSize(static_cast(namePoint->FontSize())); + } + else + { + font.setPointSize(static_cast(namePoint->FontSize()/ *factor)); + } + namePoint->setFont(font); + namePoint->setText(point.name()); + namePoint->setPos(QPointF(point.mx(), point.my())); + namePoint->blockSignals(false); + RefreshLine(); + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::SetEnabled(bool enabled) +{ + namePoint->setEnabled(enabled); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::EnableToolMove(bool move) +{ + namePoint->setFlag(QGraphicsItem::ItemIsMovable, move); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::DeleteFromLabel() +{ + emit Delete(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::PointChoosed() +{ + emit Choosed(id); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::ChangedPosition(const QPointF &pos) +{ + emit NameChangedPosition(pos); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::ContextMenu(QGraphicsSceneContextMenuEvent *event) +{ + emit ShowContextMenu(event); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) + { + emit Choosed(id); + } + QGraphicsEllipseItem::mouseReleaseEvent(event); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event) +{ + SetPen(this, currentColor, WidthMainLine(patternUnit)); + QGraphicsEllipseItem::hoverEnterEvent(event); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) +{ + SetPen(this, currentColor, WidthHairLine(patternUnit)); + QGraphicsEllipseItem::hoverLeaveEvent(event); +} + +//--------------------------------------------------------------------------------------------------------------------- +// cppcheck-suppress unusedFunction +QColor VSimplePoint::GetCurrentColor() const +{ + return currentColor; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VSimplePoint::SetCurrentColor(const QColor &value) +{ + currentColor = value; + SetPen(this, CorrectColor(currentColor), pen().widthF()); +} diff --git a/src/libs/vwidgets/vsimplepoint.h b/src/libs/vwidgets/vsimplepoint.h new file mode 100644 index 000000000..6ec014b9a --- /dev/null +++ b/src/libs/vwidgets/vsimplepoint.h @@ -0,0 +1,95 @@ +/************************************************************************ + ** + ** @file vsimplepoint.h + ** @author Roman Telezhynskyi + ** @date 20 6, 2015 + ** + ** @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) 2015 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 . + ** + *************************************************************************/ + +#ifndef VSIMPLEPOINT_H +#define VSIMPLEPOINT_H + +#include + +#include "vabstractsimple.h" + +class VPointF; +class VGraphicsSimpleTextItem; + +class VSimplePoint : public VAbstractSimple, public QGraphicsEllipseItem +{ + Q_OBJECT +public: + VSimplePoint(quint32 id, const QColor ¤tColor, Unit patternUnit, qreal *factor = nullptr, + QObject *parent = nullptr); + virtual ~VSimplePoint(); + + virtual void ChangedActivDraw(const bool &flag); + virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0); + + virtual int type() const {return Type;} + enum { Type = UserType + static_cast(Vis::SimplePoint)}; + + void RefreshLine(); + void RefreshGeometry(const VPointF &point); + void SetEnabled(bool enabled); + void EnableToolMove(bool move); + + QColor GetCurrentColor() const; + void SetCurrentColor(const QColor &value); +signals: + /** + * @brief Choosed send id when clicked. + * @param id point id. + */ + void Choosed(quint32 id); + void ShowContextMenu(QGraphicsSceneContextMenuEvent * event); + void Delete(); + void NameChangedPosition(const QPointF &pos); + +public slots: + void DeleteFromLabel(); + void PointChoosed(); + void ChangedPosition(const QPointF &pos); + void ContextMenu(QGraphicsSceneContextMenuEvent * event); + +protected: + virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ); + virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ); + virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ); + +private: + Q_DISABLE_COPY(VSimplePoint) + + /** @brief radius radius circle. */ + qreal radius; + + /** @brief namePoint point label. */ + VGraphicsSimpleTextItem *namePoint; + + /** @brief lineName line what we see if label moved too away from point. */ + QGraphicsLineItem *lineName; + +}; + +#endif // VSIMPLEPOINT_H diff --git a/src/libs/vwidgets/vwidgets.pri b/src/libs/vwidgets/vwidgets.pri index d750ea70f..8d2c768f8 100644 --- a/src/libs/vwidgets/vwidgets.pri +++ b/src/libs/vwidgets/vwidgets.pri @@ -10,7 +10,9 @@ SOURCES += \ $$PWD/vsimplecurve.cpp \ $$PWD/vwidgetpopup.cpp \ $$PWD/vcontrolpointspline.cpp \ - $$PWD/vgraphicssimpletextitem.cpp + $$PWD/vgraphicssimpletextitem.cpp \ + $$PWD/vsimplepoint.cpp \ + $$PWD/vabstractsimple.cpp HEADERS += \ $$PWD/stable.h \ @@ -21,4 +23,6 @@ HEADERS += \ $$PWD/vsimplecurve.h \ $$PWD/vwidgetpopup.h \ $$PWD/vcontrolpointspline.h \ - $$PWD/vgraphicssimpletextitem.h + $$PWD/vgraphicssimpletextitem.h \ + $$PWD/vsimplepoint.h \ + $$PWD/vabstractsimple.h