-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
QGIRichAnno.cpp
313 lines (265 loc) · 9.6 KB
/
QGIRichAnno.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/***************************************************************************
* 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 <QGraphicsItem>
#include <QPainter>
#include <QPaintDevice>
#include <QSvgGenerator>
#include <QRegExp>
#include <QTextDocument>
#include <QTextFrame>
#include <QTextBlock>
# 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/DrawRichAnno.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include <Mod/TechDraw/App/Geometry.h>
#include "Rez.h"
#include "ZVALUE.h"
#include "QGIArrow.h"
#include "ViewProviderRichAnno.h"
#include "MDIViewPage.h"
#include "DrawGuiUtil.h"
#include "QGVPage.h"
#include "QGIPrimPath.h"
#include "QGEPath.h"
#include "QGMText.h"
#include "QGIView.h"
#include "QGCustomText.h"
#include "QGCustomRect.h"
#include "QGIRichAnno.h"
using namespace TechDraw;
using namespace TechDrawGui;
//**************************************************************
QGIRichAnno::QGIRichAnno(QGraphicsItem* myParent,
TechDraw::DrawRichAnno* anno) :
m_isExporting(false)
{
setHandlesChildEvents(false);
setAcceptHoverEvents(false);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
if (myParent != nullptr) {
setParentItem(myParent);
}
setViewFeature(anno);
m_text = new QGCustomText();
m_text->setTextInteractionFlags(Qt::NoTextInteraction);
addToGroup(m_text);
m_text->setZValue(ZVALUE::DIMENSION);
m_text->centerAt(0.0, 0.0);
m_rect = new QGCustomRect();
addToGroup(m_rect);
m_rect->setZValue(ZVALUE::DIMENSION - 1);
m_rect->centerAt(0.0, 0.0);
setZValue(ZVALUE::DIMENSION);
}
QVariant QGIRichAnno::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemSelectedHasChanged && scene()) {
//There's nothing special for QGIRA to do when selection changes!
} else if(change == ItemSceneChange && scene()) {
// nothing special!
}
return QGIView::itemChange(change, value);
}
//void QGIRichAnno::select(bool state)
//{
// setSelected(state);
// draw();
//}
//void QGIRichAnno::hover(bool state)
//{
// m_hasHover = state;
// draw();
//}
void QGIRichAnno::updateView(bool update)
{
// Base::Console().Message("QGIRA::updateView() - %s\n", getViewName());
Q_UNUSED(update);
auto annoFeat( dynamic_cast<TechDraw::DrawRichAnno*>(getViewObject()) );
if ( annoFeat == nullptr ) {
Base::Console().Log("QGIRA::updateView - no feature!\n");
return;
}
auto vp = static_cast<ViewProviderRichAnno*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
if (annoFeat->X.isTouched() ||
annoFeat->Y.isTouched()) {
float x = Rez::guiX(annoFeat->X.getValue());
float y = Rez::guiX(annoFeat->Y.getValue());
m_text->centerAt(x, -y);
m_rect->centerAt(x, -y);
}
draw();
}
void QGIRichAnno::drawBorder()
{
////Leaders have no border!
// QGIView::drawBorder(); //good for debugging
}
void QGIRichAnno::draw()
{
// Base::Console().Log("QGIRA::draw() - %s - parent: %X\n",getFeature()->getNameInDocument(), parentItem());
if (!isVisible()) {
// Base::Console().Message("QGIRA::draw - not visible\n");
return;
}
TechDraw::DrawRichAnno* annoFeat = getFeature();
if((!annoFeat) ) {
// Base::Console().Message("QGIRA::draw - no feature\n");
return;
}
auto vp = static_cast<ViewProviderRichAnno*>(getViewProvider(getFeature()));
if ( vp == nullptr ) {
// Base::Console().Message("QGIRA::draw - no viewprovider\n");
return;
}
// double appX = Rez::guiX(annoFeat->X.getValue());
// double appY = Rez::guiX(annoFeat->Y.getValue());
QGIView::draw();
setTextItem();
}
void QGIRichAnno::setTextItem()
{
// Base::Console().Message("QGIRA::setTextItem() - %s\n",getViewName());
TechDraw::DrawRichAnno* annoFeat = getFeature();
QString inHtml = QString::fromUtf8(annoFeat->AnnoText.getValue());
//don't do this multiplication if exporting to SVG as other apps interpret
//font sizes differently from QGraphicsTextItem (?)
if (!getExporting()) {
//convert point font sizes to (Rez,mm) font sizes
QRegExp rxFontSize(QString::fromUtf8("font-size:([0-9]*)pt;"));
QString match;
double mmPerPoint = 0.353;
double sizeConvert = Rez::getRezFactor() * mmPerPoint;
int pos = 0;
QStringList findList;
QStringList replList;
while ((pos = rxFontSize.indexIn(inHtml, pos)) != -1) {
QString found = rxFontSize.cap(0);
findList << found;
QString qsOldSize = rxFontSize.cap(1);
QString repl = found;
double newSize = qsOldSize.toDouble();
newSize = newSize * sizeConvert;
QString qsNewSize = QString::number(newSize, 'f', 2);
repl.replace(qsOldSize,qsNewSize);
replList << repl;
pos += rxFontSize.matchedLength();
}
QString outHtml = inHtml;
int iRepl = 0;
//TODO: check list for duplicates?
for ( ; iRepl < findList.size(); iRepl++) {
outHtml = outHtml.replace(findList[iRepl], replList[iRepl]);
}
m_text->setTextWidth(Rez::guiX(annoFeat->MaxWidth.getValue()));
m_text->setHtml(outHtml);
} else {
//TODO: fix line spacing. common solutions (style sheet,
// QTextBlock::setLineHeight(150, QTextBlockFormat::ProportionalHeight)) don't help
double realWidth = m_text->boundingRect().width();
m_text->setTextWidth(realWidth);
m_text->setHtml(inHtml);
}
if (annoFeat->ShowFrame.getValue()) {
QRectF r = m_text->boundingRect().adjusted(1,1,-1,-1);
m_rect->setPen(rectPen());
m_rect->setBrush(Qt::NoBrush);
m_rect->setRect(r);
m_rect->show();
} else {
m_rect->hide();
}
m_text->centerAt(0.0, 0.0);
m_rect->centerAt(0.0, 0.0);
}
//void QGIRichAnno::drawBorder()
//{
//////Leaders have no border!
//// QGIView::drawBorder(); //good for debugging
//}
TechDraw::DrawRichAnno* QGIRichAnno::getFeature(void)
{
TechDraw::DrawRichAnno* result =
static_cast<TechDraw::DrawRichAnno*>(getViewObject());
return result;
}
QRectF QGIRichAnno::boundingRect() const
{
QRectF rect = mapFromItem(m_text,m_text->boundingRect()).boundingRect();
return rect.adjusted(-10.,-10.,10.,10.);
}
QPainterPath QGIRichAnno::shape() const
{
return QGraphicsItemGroup::shape();
}
void QGIRichAnno::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;
// painter->drawRect(boundingRect()); //good for debugging
QGIView::paint (painter, &myOption, widget);
}
QPen QGIRichAnno::rectPen() const
{
QPen pen;
const auto sym( dynamic_cast<TechDraw::DrawRichAnno*>(getViewObject()) );
if( sym == nullptr ) {
return pen;
}
auto vp = static_cast<ViewProviderRichAnno*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return pen;
}
double rectWeight = Rez::guiX(vp->LineWidth.getValue());
Qt::PenStyle rectStyle = (Qt::PenStyle) vp->LineStyle.getValue();
App::Color temp = vp->LineColor.getValue();
QColor rectColor = temp.asValue<QColor>();
pen = QPen(rectStyle);
pen.setWidthF(rectWeight);
pen.setColor(rectColor);
return pen;
}
#include <Mod/TechDraw/Gui/moc_QGIRichAnno.cpp>