FreeCAD

Форк
0
/
QGIHighlight.cpp 
208 строк · 6.3 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 WandererFan <wandererfan@gmail.com>                *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <QPainter>
26
# include <QStyleOptionGraphicsItem>
27
#endif
28

29
#include <Base/Tools.h>
30
#include <Mod/TechDraw/App/DrawUtil.h>
31

32
#include "QGIHighlight.h"
33
#include "PreferencesGui.h"
34
#include "QGIViewPart.h"
35
#include "Rez.h"
36

37

38
using namespace TechDrawGui;
39
using namespace TechDraw;
40

41
QGIHighlight::QGIHighlight() :
42
    m_referenceAngle(0.0)
43
{
44
    m_refSize = 0.0;
45
    setInteractive(false);
46

47
    m_circle = new QGraphicsEllipseItem();
48
    addToGroup(m_circle);
49
    m_circle->setFlag(QGraphicsItem::ItemIsSelectable, false);
50

51
    m_rect = new QGCustomRect();
52
    addToGroup(m_rect);
53
    m_rect->setFlag(QGraphicsItem::ItemIsSelectable, false);
54

55
    m_reference = new QGCustomText();
56
    addToGroup(m_reference);
57
    m_reference->setFlag(QGraphicsItem::ItemIsSelectable, false);
58

59
    setWidth(Rez::guiX(0.75));
60
}
61

62
QGIHighlight::~QGIHighlight()
63
{
64

65
}
66

67
void QGIHighlight::onDragFinished()
68
{
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);
73
    if (qgivp) {
74
        qgivp->highlightMoved(this, pos());
75
    }
76
}
77

78
void QGIHighlight::draw()
79
{
80
    prepareGeometryChange();
81
    makeHighlight();
82
    makeReference();
83
    update();
84
}
85

86
void QGIHighlight::makeHighlight()
87
{
88
    QRectF r(m_start, m_end);
89
    m_circle->setRect(r);
90
    m_rect->setRect(r);
91
    if (getHoleStyle() == 0) {
92
        m_rect->hide();
93
        m_circle->show();
94
    } else {
95
        m_rect->show();
96
        m_circle->hide();
97
    }
98
}
99

100
void QGIHighlight::makeReference()
101
{
102
    prepareGeometryChange();
103
    int fontSize = QGIView::exactFontSize(Base::Tools::toStdString(m_refFont.family()),
104
                                          m_refSize);
105
    m_refFont .setPixelSize(fontSize);
106
    m_reference->setFont(m_refFont);
107
    m_reference->setPlainText(m_refText);
108

109
    double vertOffset = 0.0;
110
    if (m_referenceAngle >= 0.0 &&
111
        m_referenceAngle <= 180.0) {
112
        //above the X axis
113
        //referenceText is positioned by top-left, need to adjust upwards by text height
114
        vertOffset = m_reference->boundingRect().height();
115
    } else {
116
        //below X axis. need to adjust upwards a bit because there is blank space above text
117
        vertOffset = m_reference->tightBoundingAdjust().y();
118
    }
119

120
    double horizOffset = 0.0;
121
    if (m_referenceAngle > 90.0 &&
122
        m_referenceAngle < 270.0) {
123
        //to left of Y axis
124
        horizOffset = -m_reference->boundingRect().width();
125
    }
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);
133

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);
140
    }
141
}
142

143
void QGIHighlight::setInteractive(bool state)
144
{
145
//    setAcceptHoverEvents(state);
146
    setFlag(QGraphicsItem::ItemIsSelectable, state);
147
    setFlag(QGraphicsItem::ItemIsMovable, state);
148
    setFlag(QGraphicsItem::ItemSendsScenePositionChanges, state);
149
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
150
}
151

152
void QGIHighlight::setBounds(double x1, double y1, double x2, double y2)
153
{
154
    m_start = QPointF(Rez::guiX(x1), Rez::guiX(-y1));
155
    m_end = QPointF(Rez::guiX(x2), Rez::guiX(-y2));
156
}
157

158
void QGIHighlight::setReference(const char* ref)
159
{
160
    m_refText = QString::fromUtf8(ref);
161
}
162

163
void QGIHighlight::setFont(QFont f, double fsize)
164
{
165
    m_refFont = f;
166
    m_refSize = fsize;
167
}
168

169

170
//obs?
171
QColor QGIHighlight::getHighlightColor()
172
{
173
    return PreferencesGui::sectionLineQColor();
174
}
175

176
int QGIHighlight::getHoleStyle()
177
{
178
    return TechDraw::Preferences::mattingStyle();
179
}
180

181
void QGIHighlight::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
182
    QStyleOptionGraphicsItem myOption(*option);
183
//    myOption.state &= ~QStyle::State_Selected;
184

185
    setTools();
186
//    painter->drawRect(boundingRect());          //good for debugging
187
    QGIDecoration::paint (painter, &myOption, widget);
188
}
189

190
void QGIHighlight::setTools()
191
{
192
    m_pen.setWidthF(m_width);
193
    m_pen.setColor(m_colCurrent);
194
    m_pen.setStyle(Qt::CustomDashLine);
195

196
    m_brush.setStyle(m_brushCurrent);
197
    m_brush.setColor(m_colCurrent);
198

199
    m_circle->setPen(m_pen);
200
    m_rect->setPen(m_pen);
201

202
    m_reference->setDefaultTextColor(m_colCurrent);
203
}
204

205
void QGIHighlight::setLinePen(QPen isoPen)
206
{
207
    m_pen = isoPen;
208
}
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.