Files
create/src/Mod/TechDraw/Gui/QGILeaderLine.cpp
2019-12-26 08:56:21 -05:00

627 lines
20 KiB
C++

/***************************************************************************
* Copyright (c) 2019 WandererFan <wandererfan@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 <BRep_Builder.hxx>
#include <TopoDS_Compound.hxx>
# include <TopoDS_Shape.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS.hxx>
# include <BRepAdaptor_Curve.hxx>
# include <Precision.hxx>
# include <QGraphicsScene>
# include <QGraphicsSceneMouseEvent>
# include <QPainter>
# include <QPaintDevice>
# include <QSvgGenerator>
# include <math.h>
#endif
#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Base/UnitsApi.h>
#include <Gui/Command.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/TechDraw/App/DrawLeaderLine.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include <Mod/TechDraw/App/Geometry.h>
#include <Mod/TechDraw/App/LineGroup.h>
#include "Rez.h"
#include "ZVALUE.h"
#include "QGIArrow.h"
#include "ViewProviderLeader.h"
#include "MDIViewPage.h"
#include "DrawGuiUtil.h"
#include "QGVPage.h"
#include "QGIPrimPath.h"
#include "QGEPath.h"
#include "QGILeaderLine.h"
using namespace TechDraw;
using namespace TechDrawGui;
//**************************************************************
QGILeaderLine::QGILeaderLine(QGraphicsItem* myParent,
TechDraw::DrawLeaderLine* leader) :
m_parentItem(myParent),
m_lineColor(Qt::black),
// m_hasHover(false),
m_blockDraw(false)
{
setHandlesChildEvents(false);
setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
setCacheMode(QGraphicsItem::NoCache);
m_line = new QGIPrimPath();
addToGroup(m_line);
m_line->setNormalColor(getNormalColor());
m_line->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_line->setAcceptHoverEvents(false);
m_line->setPrettyNormal();
m_line->setPos(0.0,0.0);
m_editPath = new QGEPath(this);
m_editPath->setNormalColor(getNormalColor());
addToGroup(m_editPath);
m_editPath->setPos(0.0, 0.0);
m_editPath->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_editPath->setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
m_editPath->setZValue(ZVALUE::DIMENSION);
m_editPath->hide();
m_arrow1 = new QGIArrow();
addToGroup(m_arrow1);
m_arrow1->setNormalColor(getNormalColor());
m_arrow1->setFillColor(getNormalColor());
m_arrow1->setPrettyNormal();
m_arrow1->setPos(0.0,0.0);
m_arrow1->hide();
m_arrow2 = new QGIArrow();
addToGroup(m_arrow2);
m_arrow2->setNormalColor(getNormalColor());
m_arrow2->setFillColor(getNormalColor());
m_arrow2->setPrettyNormal();
m_arrow2->setPos(0.0, 0.0);
m_arrow2->hide();
setParentItem(m_parentItem);
setViewFeature(leader);
setZValue(ZVALUE::DIMENSION);
QObject::connect(
m_editPath, SIGNAL(pointsUpdated(QPointF, std::vector<QPointF>)),
this , SLOT (onLineEditFinished(QPointF, std::vector<QPointF>))
);
}
QVariant QGILeaderLine::itemChange(GraphicsItemChange change, const QVariant &value)
{
// Base::Console().Message("QGILL::itemChange(%d)\n", change);
if (change == ItemSelectedHasChanged && scene()) {
if(isSelected()) {
setPrettySel();
} else {
setPrettyNormal();
}
draw();
} else if(change == ItemSceneChange && scene()) {
// nothing special!
}
return QGIView::itemChange(change, value);
}
//QGILL isn't draggable so skip QGIV::mousePress have event
void QGILeaderLine::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
// Base::Console().Message("QGILL::mousePressEvent() - %s\n",getViewName());
QGraphicsItem::mousePressEvent(event);
}
//void QGILeaderLine::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
//{
// QGraphicsItem::mouseMoveEvent(event);
//}
//QGILL isn't draggable so skip QGIV::mouseRelease
void QGILeaderLine::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
// Base::Console().Message("QGILL::mouseReleaseEvent() - %s\n",getViewName());
QGraphicsItem::mouseReleaseEvent(event);
}
void QGILeaderLine::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
// Base::Console().Message("QGILL::hoverEnter() - selected; %d\n",isSelected());
if (!isSelected()) {
setPrettyPre();
}
QGIView::hoverEnterEvent(event);
}
void QGILeaderLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
// Base::Console().Message("QGILL::hoverLeave() - selected; %d\n",isSelected());
if(!isSelected()) {
setPrettyNormal();
}
QGIView::hoverLeaveEvent(event);
}
void QGILeaderLine::onSourceChange(TechDraw::DrawView* newParent)
{
// Base::Console().Message("QGILL::onSoureChange(%s)\n",newParent->getNameInDocument());
std::string parentName = newParent->getNameInDocument();
QGIView* qgiParent = getQGIVByName(parentName);
if (qgiParent != nullptr) {
m_parentItem = qgiParent;
setParentItem(m_parentItem);
draw();
} else {
Base::Console().Warning("QGILL::onSourceChange - new parent %s has no QGIView\n",parentName.c_str());
}
}
void QGILeaderLine::setPrettyNormal() {
// Base::Console().Message("QGILL::setPrettyNormal()\n");
m_line->setPrettyNormal();
m_arrow1->setPrettyNormal();
m_arrow2->setPrettyNormal();
}
void QGILeaderLine::setPrettyPre() {
// Base::Console().Message("QGILL::setPrettyPre()\n");
m_line->setPrettyPre();
m_arrow1->setPrettyPre();
m_arrow2->setPrettyPre();
}
void QGILeaderLine::setPrettySel() {
// Base::Console().Message("QGILL::setPrettySel()\n");
m_line->setPrettySel();
m_arrow1->setPrettySel();
m_arrow2->setPrettySel();
}
void QGILeaderLine::closeEdit(void)
{
// Base::Console().Message("QGIL::closeEdit()\n");
if (m_editPath != nullptr) {
m_editPath->onEndEdit(); //tell QEPath that edit session ended
}
}
//signaled from QEPath
void QGILeaderLine::onLineEditFinished(QPointF tipDisplace, std::vector<QPointF> points)
{
// Base::Console().Message("QGILL::onLineEditFinished(%s, %d)\n",
// TechDraw::DrawUtil::formatVector(tipDisplace).c_str(),
// points.size());
m_blockDraw = true;
auto featLeader = getFeature();
if (featLeader == nullptr) {
//tarfu
return;
}
double baseScale = featLeader->getBaseScale();
if ( !(TechDraw::DrawUtil::fpCompare(tipDisplace.x(),0.0) &&
TechDraw::DrawUtil::fpCompare(tipDisplace.y(),0.0)) ) {
//tip was moved. need to change AttachPoint
QPointF oldAttach = getAttachFromFeature();
QPointF newAttach = oldAttach + (tipDisplace / baseScale);
featLeader->setPosition(Rez::appX(newAttach.x()),
Rez::appX(- newAttach.y()),
true);
}
std::vector<Base::Vector3d> waypoints;
for (auto& p: points) {
QPointF moved = p - tipDisplace;
Base::Vector3d v(moved.x(),moved.y(),0.0);
waypoints.push_back(v);
}
waypoints.at(0) = Base::Vector3d(0.0, 0.0, 0.0);
featLeader->WayPoints.setValues(waypoints);
if (featLeader->AutoHorizontal.getValue()) {
featLeader->adjustLastSegment();
}
if (m_parentItem == nullptr) {
Base::Console().Warning("QGILL::onLineEditFinished - m_parentItem is NULL\n");
} else {
QGIView* qgiv = dynamic_cast<QGIView*>(m_parentItem);
if (qgiv != nullptr) {
Q_EMIT editComplete(); //to task
}
}
m_blockDraw = false;
m_editPath->hide();
draw();
}
void QGILeaderLine::startPathEdit(void)
{
saveState();
auto featLeader( dynamic_cast<TechDraw::DrawLeaderLine*>(getViewObject()) );
double scale = featLeader->getScale();
m_editPath->setScale(scale);
m_editPath->inEdit(true);
m_editPath->show();
m_editPath->startPathEdit(getWayPointsFromFeature());
}
void QGILeaderLine::saveState(void)
{
// Base::Console().Message("QGILL::saveState()\n");
auto featLeader = getFeature();
if (featLeader != nullptr) {
m_savePoints = featLeader->WayPoints.getValues();
m_saveX = featLeader->X.getValue();
m_saveY = featLeader->Y.getValue();
}
}
void QGILeaderLine::restoreState(void)
{
// Base::Console().Message("QGILL::restoreState()\n");
auto featLeader = getFeature();
if (featLeader != nullptr) {
featLeader->WayPoints.setValues(m_savePoints);
featLeader->X.setValue(m_saveX);
featLeader->Y.setValue(m_saveY);
featLeader->recomputeFeature();
}
}
//******************************************************************************
void QGILeaderLine::updateView(bool update)
{
// Base::Console().Message("QGIL::updateView() %s\n",getViewObject()->getNameInDocument());
Q_UNUSED(update);
auto featLeader( dynamic_cast<TechDraw::DrawLeaderLine*>(getViewObject()) );
if ( featLeader == nullptr ) {
Base::Console().Warning("QGILL::updateView - no feature!\n");
return;
}
auto vp = static_cast<ViewProviderLeader*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
draw();
}
void QGILeaderLine::draw()
{
// Base::Console().Message("QGILL::draw()- %s\n", getViewObject()->getNameInDocument());
if (m_blockDraw) {
// Base::Console().Message("QGIL::draw - block draw\n");
return;
}
if (!isVisible()) {
// Base::Console().Message("QGIL::draw - not visible\n");
return;
}
TechDraw::DrawLeaderLine* featLeader = getFeature();
if((!featLeader) ) {
// Base::Console().Message("QGIL::draw - no feature\n");
return;
}
auto vp = static_cast<ViewProviderLeader*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
// Base::Console().Message("QGIL::draw - no viewprovider\n");
return;
}
TechDraw::DrawView* parent = featLeader->getBaseView();
QGVPage* view = QGIView::getGraphicsView(parent);
if (view == nullptr) {
// Base::Console().Message("QGIL::draw - no graphcisView for parent!! - setup?\n");
return;
}
if (m_editPath->inEdit()) {
// Base::Console().Message("QGIL::draw - m_editPath in edit\n");
return;
}
//********
if (featLeader->isLocked()) {
setFlag(QGraphicsItem::ItemIsMovable, false);
} else {
setFlag(QGraphicsItem::ItemIsMovable, true);
}
m_lineStyle = (Qt::PenStyle) vp->LineStyle.getValue();
double scale = parent->getScale();
double baseScale = featLeader->getBaseScale();
double x = Rez::guiX(featLeader->X.getValue());
double y = - Rez::guiX(featLeader->Y.getValue());
QPointF aPoint(x,y);
aPoint *= baseScale;
setPos(aPoint);
m_line->setFillStyle(Qt::NoBrush);
m_line->setStyle(m_lineStyle);
m_line->setWidth(getLineWidth());
m_line->setNormalColor(getNormalColor());
m_line->setPos(0,0); //make m_line coords == leader coords
std::vector<QPointF> qPoints = getWayPointsFromFeature();
if (featLeader->Scalable.getValue()) {
for (auto& p : qPoints) {
p = p * scale;
}
}
m_line->setPath(makeLeaderPath(qPoints));
m_line->show();
setArrows(qPoints);
}
QPainterPath QGILeaderLine::makeLeaderPath(std::vector<QPointF> qPoints)
{
// Base::Console().Message("QGILeaderLine::makeLeaderPath()\n");
QPainterPath result;
DrawLeaderLine* featLeader = getFeature();
if (featLeader == nullptr) {
Base::Console().Message("QGILL::makeLeaderPath - featLeader is nullptr\n");
return result;
}
QPointF startAdjVec(0.0,0.0);
double startAdjLength(0.0);
QPointF endAdjVec(0.0,0.0);
double endAdjLength(0.0);
if (qPoints.size() > 1) {
//make path adjustment to hide leaderline ends behind arrowheads
if (featLeader->StartSymbol.getValue() > -1) {
startAdjLength = QGIArrow::getOverlapAdjust(featLeader->StartSymbol.getValue(),
QGIArrow::getPrefArrowSize());
}
if (featLeader->EndSymbol.getValue() > -1) {
endAdjLength = QGIArrow::getOverlapAdjust(featLeader->EndSymbol.getValue(),
QGIArrow::getPrefArrowSize());
}
//get adjustment directions
startAdjVec = qPoints.at(1) - qPoints.front();
endAdjVec = (*(qPoints.end() - 2))- qPoints.back();
//get adjustment vectors
QVector2D startTemp(startAdjVec);
QVector2D endTemp(endAdjVec);
startTemp.normalize();
endTemp.normalize();
startAdjVec = startTemp.toPointF() * startAdjLength;
endAdjVec = endTemp.toPointF() * endAdjLength;
qPoints.front() += startAdjVec;
qPoints.back() += endAdjVec;
result.moveTo(qPoints.front());
for (int i = 1; i < (int)qPoints.size(); i++) {
result.lineTo(qPoints.at(i));
}
}
return result;
}
QPointF QGILeaderLine::getAttachFromFeature(void)
{
QPointF result;
TechDraw::DrawLeaderLine* featLeader = getFeature();
if((!featLeader) ) {
Base::Console().Message("QGIL::getAttachFromLeader - no feature\n");
return result;
}
double x = Rez::guiX(featLeader->X.getValue());
double y = - Rez::guiX(featLeader->Y.getValue());
result = QPointF(x,y);
return result;
}
std::vector<QPointF> QGILeaderLine::getWayPointsFromFeature(void)
{
std::vector<QPointF> qPoints;
DrawLeaderLine* featLeader = getFeature();
if (featLeader == nullptr) {
Base::Console().Message("QGILL::getWayPointsFromFeature - featLeader is nullptr\n");
return qPoints;
}
std::vector<Base::Vector3d> vPoints = featLeader->WayPoints.getValues();
for (auto& d: vPoints) {
QPointF temp(d.x, d.y);
qPoints.push_back(temp);
}
if (qPoints.empty()) {
Base::Console().Warning("QGILeaderLine::getWayPointsFromFeature - no points\n");
}
return qPoints;
}
void QGILeaderLine::setArrows(std::vector<QPointF> pathPoints)
{
Base::Vector3d stdX(1.0,0.0,0.0);
TechDraw::DrawLeaderLine* featLeader = getFeature();
double baseScale = featLeader->getBaseScale();
QPointF lastOffset = (pathPoints.back() - pathPoints.front()) * baseScale;
if (featLeader->StartSymbol.getValue() > -1) {
m_arrow1->setStyle(featLeader->StartSymbol.getValue());
m_arrow1->setWidth(getLineWidth());
// TODO: variable size arrow heads
m_arrow1->setSize(QGIArrow::getPrefArrowSize());
m_arrow1->setDirMode(true);
m_arrow1->setDirection(stdX);
m_arrow1->setNormalColor(getNormalColor());
m_arrow1->setFillColor(getNormalColor());
if (pathPoints.size() > 1) {
auto it = pathPoints.begin();
QPointF s = (*it);
QPointF e = (*(it + 1));
QPointF qsVec = s - e;
Base::Vector3d sVec(qsVec.x(),qsVec.y(),0.0);
m_arrow1->setDirection(sVec);
m_arrow1->setPos(0.0,0.0);
}
m_arrow1->draw();
m_arrow1->show();
} else {
m_arrow1->hide();
}
if (featLeader->EndSymbol.getValue() > -1) {
m_arrow2->setStyle(featLeader->EndSymbol.getValue());
m_arrow2->setWidth(getLineWidth());
m_arrow2->setDirMode(true);
m_arrow2->setDirection(-stdX);
m_arrow2->setNormalColor(getNormalColor());
m_arrow2->setFillColor(getNormalColor());
if (pathPoints.size() > 1) {
auto itr = pathPoints.rbegin();
QPointF s = (*itr);
QPointF e = (*(itr + 1));
QPointF qeVec = s - e;
Base::Vector3d eVec(qeVec.x(),qeVec.y(),0.0);
m_arrow2->setDirection(eVec);
m_arrow2->setPos(lastOffset);
}
m_arrow2->draw();
m_arrow2->show();
} else {
m_arrow2->hide();
}
}
void QGILeaderLine::drawBorder()
{
////Leaders have no border!
// QGIView::drawBorder(); //good for debugging
}
//******************************************************************************
void QGILeaderLine::abandonEdit(void)
{
// Base::Console().Message("QGIL::abandonEdit()\n");
m_editPath->clearMarkers();
m_editPath->hide();
restoreState();
}
double QGILeaderLine::getLineWidth(void)
{
auto vp = static_cast<ViewProviderLeader*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return Rez::guiX(LineGroup::getDefaultWidth("Graphic"));
}
return Rez::guiX(vp->LineWidth.getValue());
}
TechDraw::DrawLeaderLine* QGILeaderLine::getFeature(void)
{
TechDraw::DrawLeaderLine* result =
static_cast<TechDraw::DrawLeaderLine*>(getViewObject());
return result;
}
double QGILeaderLine::getEdgeFuzz(void) const
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->
GetGroup("Preferences")->GetGroup("Mod/TechDraw/General");
double result = hGrp->GetFloat("EdgeFuzz",10.0);
return result;
}
QColor QGILeaderLine::getNormalColor()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/LeaderLinens");
App::Color fcColor;
fcColor.setPackedValue(hGrp->GetUnsigned("Color", 0x00000000));
m_colNormal = fcColor.asValue<QColor>();
auto lead( dynamic_cast<TechDraw::DrawLeaderLine*>(getViewObject()) );
if( lead == nullptr )
return m_colNormal;
auto vp = static_cast<ViewProviderLeader*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return m_colNormal;
}
m_colNormal = vp->Color.getValue().asValue<QColor>();
return m_colNormal;
}
QRectF QGILeaderLine::boundingRect() const
{
return childrenBoundingRect();
}
QPainterPath QGILeaderLine::shape() const
{
return QGraphicsItemGroup::shape();
}
void QGILeaderLine::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;
// painter->setPen(Qt::blue);
// painter->drawRect(boundingRect()); //good for debugging
QGIView::paint (painter, &myOption, widget);
}
#include <Mod/TechDraw/Gui/moc_QGILeaderLine.cpp>