1
/***************************************************************************
2
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <QStyleOptionGraphicsItem>
29
#include <Base/Tools.h>
30
#include <Mod/TechDraw/App/DrawUtil.h>
32
#include "QGIHighlight.h"
33
#include "PreferencesGui.h"
34
#include "QGIViewPart.h"
38
using namespace TechDrawGui;
39
using namespace TechDraw;
41
QGIHighlight::QGIHighlight() :
45
setInteractive(false);
47
m_circle = new QGraphicsEllipseItem();
49
m_circle->setFlag(QGraphicsItem::ItemIsSelectable, false);
51
m_rect = new QGCustomRect();
53
m_rect->setFlag(QGraphicsItem::ItemIsSelectable, false);
55
m_reference = new QGCustomText();
56
addToGroup(m_reference);
57
m_reference->setFlag(QGraphicsItem::ItemIsSelectable, false);
59
setWidth(Rez::guiX(0.75));
62
QGIHighlight::~QGIHighlight()
67
void QGIHighlight::onDragFinished()
69
// Base::Console().Message("QGIH::onDragFinished - pos: %s\n",
70
// DrawUtil::formatVector(pos()).c_str());
71
QGraphicsItem* parent = parentItem();
72
auto qgivp = dynamic_cast<QGIViewPart*>(parent);
74
qgivp->highlightMoved(this, pos());
78
void QGIHighlight::draw()
80
prepareGeometryChange();
86
void QGIHighlight::makeHighlight()
88
QRectF r(m_start, m_end);
91
if (getHoleStyle() == 0) {
100
void QGIHighlight::makeReference()
102
prepareGeometryChange();
103
int fontSize = QGIView::exactFontSize(Base::Tools::toStdString(m_refFont.family()),
105
m_refFont .setPixelSize(fontSize);
106
m_reference->setFont(m_refFont);
107
m_reference->setPlainText(m_refText);
109
double vertOffset = 0.0;
110
if (m_referenceAngle >= 0.0 &&
111
m_referenceAngle <= 180.0) {
113
//referenceText is positioned by top-left, need to adjust upwards by text height
114
vertOffset = m_reference->boundingRect().height();
116
//below X axis. need to adjust upwards a bit because there is blank space above text
117
vertOffset = m_reference->tightBoundingAdjust().y();
120
double horizOffset = 0.0;
121
if (m_referenceAngle > 90.0 &&
122
m_referenceAngle < 270.0) {
124
horizOffset = -m_reference->boundingRect().width();
126
QRectF r(m_start, m_end);
127
double radius = r.width() / 2.0;
128
QPointF center = r.center();
129
double angleRad = m_referenceAngle * M_PI / 180.0;
130
double posX = center.x() + cos(angleRad) * radius + horizOffset;
131
double posY = center.y() - sin(angleRad) * radius - vertOffset;
132
m_reference->setPos(posX, posY);
134
double highRot = rotation();
135
if (!TechDraw::DrawUtil::fpCompare(highRot, 0.0)) {
136
QRectF refBR = m_reference->boundingRect();
137
QPointF refCenter = refBR.center();
138
m_reference->setTransformOriginPoint(refCenter);
139
m_reference->setRotation(-highRot);
143
void QGIHighlight::setInteractive(bool state)
145
// setAcceptHoverEvents(state);
146
setFlag(QGraphicsItem::ItemIsSelectable, state);
147
setFlag(QGraphicsItem::ItemIsMovable, state);
148
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, state);
149
setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
152
void QGIHighlight::setBounds(double x1, double y1, double x2, double y2)
154
m_start = QPointF(Rez::guiX(x1), Rez::guiX(-y1));
155
m_end = QPointF(Rez::guiX(x2), Rez::guiX(-y2));
158
void QGIHighlight::setReference(const char* ref)
160
m_refText = QString::fromUtf8(ref);
163
void QGIHighlight::setFont(QFont f, double fsize)
171
QColor QGIHighlight::getHighlightColor()
173
return PreferencesGui::sectionLineQColor();
176
int QGIHighlight::getHoleStyle()
178
return TechDraw::Preferences::mattingStyle();
181
void QGIHighlight::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
182
QStyleOptionGraphicsItem myOption(*option);
183
// myOption.state &= ~QStyle::State_Selected;
186
// painter->drawRect(boundingRect()); //good for debugging
187
QGIDecoration::paint (painter, &myOption, widget);
190
void QGIHighlight::setTools()
192
m_pen.setWidthF(m_width);
193
m_pen.setColor(m_colCurrent);
194
m_pen.setStyle(Qt::CustomDashLine);
196
m_brush.setStyle(m_brushCurrent);
197
m_brush.setColor(m_colCurrent);
199
m_circle->setPen(m_pen);
200
m_rect->setPen(m_pen);
202
m_reference->setDefaultTextColor(m_colCurrent);
205
void QGIHighlight::setLinePen(QPen isoPen)