/************************************************************************ ** ** @file vabstractspline.cpp ** @author Roman Telezhynskyi ** @date 4 3, 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) 2013-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 "vabstractspline.h" #include "../vwidgets/vcontrolpointspline.h" #include "../qmuparser/qmutokenparser.h" #include //--------------------------------------------------------------------------------------------------------------------- VAbstractSpline::VAbstractSpline(VAbstractPattern *doc, VContainer *data, quint32 id, QGraphicsItem *parent) :VDrawTool(doc, data, id), QGraphicsPathItem(parent), controlPoints(QVector()), sceneType(SceneObject::Unknown), isHovered(false), detailsMode(false) { setAcceptHoverEvents(true); } //--------------------------------------------------------------------------------------------------------------------- VAbstractSpline::~VAbstractSpline() {} //--------------------------------------------------------------------------------------------------------------------- QString VAbstractSpline::getTagName() const { return VAbstractPattern::TagSpline; } //--------------------------------------------------------------------------------------------------------------------- /** * @brief FullUpdateFromFile update tool data form file. */ void VAbstractSpline::FullUpdateFromFile() { ReadAttributes(); RefreshGeometry(); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::Disable(bool disable, const QString &namePP) { enabled = !CorrectDisable(disable, namePP); this->setEnabled(enabled); const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); this->setPen(QPen(CorrectColor(curve->GetColor()), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor, Qt::SolidLine, Qt::RoundCap)); emit setEnabledPoint(enabled); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::DetailsMode(bool mode) { detailsMode = mode; RefreshGeometry(); ShowHandles(detailsMode); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::AllowHover(bool enabled) { setAcceptHoverEvents(enabled); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::AllowSelecting(bool enabled) { setFlag(QGraphicsItem::ItemIsSelectable, enabled); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief ShowTool highlight tool. * @param id object id in container * @param enable enable or disable highlight. */ void VAbstractSpline::ShowTool(quint32 id, bool enable) { ShowItem(this, id, enable); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetFactor set current scale factor of scene. * @param factor scene scale factor. */ void VAbstractSpline::SetFactor(qreal factor) { VDrawTool::SetFactor(factor); RefreshGeometry(); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief hoverEnterEvent handle hover enter events. * @param event hover enter event. */ // cppcheck-suppress unusedFunction void VAbstractSpline::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); this->setPen(QPen(CorrectColor(curve->GetColor()), qApp->toPixel(WidthMainLine(*VAbstractTool::data.GetPatternUnit()))/factor, Qt::SolidLine, Qt::RoundCap)); this->setPath(ToolPath(PathDirection::Show)); isHovered = true; QGraphicsPathItem::hoverEnterEvent(event); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief hoverLeaveEvent handle hover leave events. * @param event hover leave event. */ // cppcheck-suppress unusedFunction void VAbstractSpline::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); this->setPen(QPen(CorrectColor(curve->GetColor()), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); if (detailsMode) { this->setPath(ToolPath(PathDirection::Show)); } else { this->setPath(ToolPath()); } isHovered = false; QGraphicsPathItem::hoverLeaveEvent(event); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief itemChange hadle item change. * @param change change. * @param value value. * @return value. */ QVariant VAbstractSpline::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { if (change == QGraphicsItem::ItemSelectedChange) { emit ChangedToolSelection(value.toBool(), id, id); } return QGraphicsPathItem::itemChange(change, value); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief keyReleaseEvent handle key release events. * @param event key release event. */ void VAbstractSpline::keyReleaseEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Delete: try { DeleteTool(); } catch(const VExceptionToolWasDeleted &e) { Q_UNUSED(e); return;//Leave this method immediately!!! } break; default: break; } QGraphicsPathItem::keyReleaseEvent ( event ); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::mousePressEvent(QGraphicsSceneMouseEvent *event) { // Special for not selectable item first need to call standard mousePressEvent then accept event QGraphicsPathItem::mousePressEvent(event); event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event } //--------------------------------------------------------------------------------------------------------------------- /** * @brief mouseReleaseEvent handle mouse release events. * @param event mouse release event. */ void VAbstractSpline::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { emit ChoosedTool(id, sceneType); } QGraphicsPathItem::mouseReleaseEvent(event); } //--------------------------------------------------------------------------------------------------------------------- QPainterPath VAbstractSpline::ToolPath(PathDirection direction) const { const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); QPainterPath path; path.addPath(curve->GetPath(direction)); path.setFillRule( Qt::WindingFill ); return path; } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::ReadToolAttributes(const QDomElement &domElement) { Q_UNUSED(domElement) } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::SaveOptions(QDomElement &tag, QSharedPointer &obj) { VDrawTool::SaveOptions(tag, obj); const QSharedPointer curve = qSharedPointerCast(obj); doc->SetAttribute(tag, AttrColor, curve->GetColor()); } //--------------------------------------------------------------------------------------------------------------------- VSpline VAbstractSpline::CorrectedSpline(const VSpline &spline, const SplinePointPosition &position, const QPointF &pos) const { VSpline spl; if (position == SplinePointPosition::FirstPoint) { QLineF line(spline.GetP1(), pos); qreal newAngle1 = line.angle(); QString newAngle1F = QString().setNum(newAngle1); qreal newLength1 = line.length(); QString newLength1F = QString().setNum(qApp->fromPixel(newLength1)); if (not qmu::QmuTokenParser::IsSingle(spline.GetStartAngleFormula())) { newAngle1 = spline.GetStartAngle(); newAngle1F = spline.GetStartAngleFormula(); } if (not qmu::QmuTokenParser::IsSingle(spline.GetC1LengthFormula())) { newLength1 = spline.GetC1Length(); newLength1F = spline.GetC1LengthFormula(); } spl = VSpline(spline.GetP1(), spline.GetP4(), newAngle1, newAngle1F, spline.GetEndAngle(), spline.GetEndAngleFormula(), newLength1, newLength1F, spline.GetC2Length(), spline.GetC2LengthFormula()); } else { QLineF line(spline.GetP4(), pos); qreal newAngle2 = line.angle(); QString newAngle2F = QString().setNum(newAngle2); qreal newLength2 = line.length(); QString newLength2F = QString().setNum(qApp->fromPixel(newLength2)); if (not qmu::QmuTokenParser::IsSingle(spline.GetEndAngleFormula())) { newAngle2 = spline.GetEndAngle(); newAngle2F = spline.GetEndAngleFormula(); } if (not qmu::QmuTokenParser::IsSingle(spline.GetC2LengthFormula())) { newLength2 = spline.GetC2Length(); newLength2F = spline.GetC2LengthFormula(); } spl = VSpline(spline.GetP1(), spline.GetP4(), spline.GetStartAngle(), spline.GetStartAngleFormula(), newAngle2, newAngle2F, spline.GetC1Length(), spline.GetC1LengthFormula(), newLength2, newLength2F); } return spl; } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::ShowHandles(bool show) { for (int i = 0; i < controlPoints.size(); ++i) { controlPoints.at(i)->setVisible(show); } } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::setEnabled(bool enabled) { QGraphicsPathItem::setEnabled(enabled); if (enabled) { const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); setPen(QPen(QColor(curve->GetColor()), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); } else { setPen(QPen(Qt::gray, qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); } } //--------------------------------------------------------------------------------------------------------------------- QString VAbstractSpline::GetLineColor() const { const QSharedPointer curve = VAbstractTool::data.GeometricObject(id); return curve->GetColor(); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::SetLineColor(const QString &value) { QSharedPointer curve = VAbstractTool::data.GeometricObject(id); curve->SetColor(value); QSharedPointer obj = qSharedPointerCast(curve); SaveOption(obj); } //--------------------------------------------------------------------------------------------------------------------- QString VAbstractSpline::name() const { return ObjectName(id); } //--------------------------------------------------------------------------------------------------------------------- void VAbstractSpline::GroupVisibility(quint32 object, bool visible) { Q_UNUSED(object); setVisible(visible); }