1
/***************************************************************************
2
* Copyright (c) 2019 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 <QGraphicsScene>
27
# include <QPainterPath>
30
#include <Base/Console.h>
31
#include <Base/Tools.h>
33
#include <Mod/TechDraw/App/DrawLeaderLine.h>
34
#include <Mod/TechDraw/App/DrawTile.h>
35
#include <Mod/TechDraw/App/DrawTileWeld.h>
36
#include <Mod/TechDraw/App/DrawWeldSymbol.h>
38
#include "QGIWeldSymbol.h"
39
#include "PreferencesGui.h"
40
#include "QGCustomText.h"
41
#include "QGILeaderLine.h"
42
#include "QGIPrimPath.h"
46
#include "ViewProviderWeld.h"
50
using namespace TechDraw;
51
using namespace TechDrawGui;
54
//**************************************************************
55
QGIWeldSymbol::QGIWeldSymbol() :
63
setFiltersChildEvents(true); //qt5
64
setFlag(QGraphicsItem::ItemIsMovable, false);
66
setCacheMode(QGraphicsItem::NoCache);
67
setZValue(ZVALUE::DIMENSION);
69
m_tailText = new QGCustomText();
70
m_tailText->setPlainText(
71
QString::fromUtf8(" "));
72
addToGroup(m_tailText);
74
m_tailText->setPos(0.0, 0.0); //avoid bRect issues
76
m_allAround = new QGIVertex(-1);
77
addToGroup(m_allAround);
78
m_allAround->setPos(0.0, 0.0);
79
m_allAround->setAcceptHoverEvents(false);
80
m_allAround->setFlag(QGraphicsItem::ItemIsSelectable, false);
81
m_allAround->setFlag(QGraphicsItem::ItemIsMovable, false);
82
m_allAround->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
83
m_allAround->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
84
m_allAround->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
86
m_fieldFlag = new QGIPrimPath();
87
addToGroup(m_fieldFlag);
88
m_fieldFlag->setPos(0.0, 0.0);
89
m_fieldFlag->setAcceptHoverEvents(false);
90
m_fieldFlag->setFlag(QGraphicsItem::ItemIsSelectable, false);
91
m_fieldFlag->setFlag(QGraphicsItem::ItemIsMovable, false);
92
m_fieldFlag->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
93
m_fieldFlag->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
94
m_fieldFlag->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
95
m_fieldFlag->setFill(prefNormalColor(), Qt::SolidPattern);
97
setNormalColor(prefNormalColor());
98
setCurrentColor(getNormalColor());
99
setSettingColor(getNormalColor());
104
QVariant QGIWeldSymbol::itemChange(GraphicsItemChange change, const QVariant &value)
106
// Base::Console().Message("QGIWS::itemChange(%d)\n", change);
107
if (change == ItemSelectedHasChanged && scene()) {
113
} else if(change == ItemSceneChange && scene()) {
116
return QGIView::itemChange(change, value);
119
void QGIWeldSymbol::updateView(bool update)
121
// Base::Console().Message("QGIWS::updateView()\n");
124
TechDraw::DrawWeldSymbol *feature = getFeature();
126
Base::Console().Warning("QGIWS::updateView - no feature!\n");
130
if (feature->isRestoring() || !getLeader()) {
137
void QGIWeldSymbol::draw()
139
// Base::Console().Message("QGIWS::draw()- %s\n", getFeature()->getNameInDocument());
148
drawTile(m_arrowFeat);
152
drawTile(m_otherFeat);
162
void QGIWeldSymbol::drawTile(TechDraw::DrawTileWeld* tileFeat)
164
// Base::Console().Message("QGIWS::drawTile() - tileFeat: %X\n", tileFeat);
166
Base::Console().Message("QGIWS::drawTile - tile is null\n");
170
const auto sym = getFeature();
173
auto vp = dynamic_cast<ViewProviderWeld *>(getViewProvider(sym));
176
std::string fontName = vp->Font.getValue();
177
int fontSize = QGIView::exactFontSize(vp->Font.getValue(),
178
vp->TileFontSize.getValue());
180
double featScale = getLeader()->getScale();
182
std::string tileTextL = tileFeat->LeftText.getValue();
183
std::string tileTextR = tileFeat->RightText.getValue();
184
std::string tileTextC = tileFeat->CenterText.getValue();
185
// std::string symbolFile = tileFeat->SymbolFile.getValue();
186
int row = tileFeat->TileRow.getValue();
187
int col = tileFeat->TileColumn.getValue();
189
QGITile* tile = new QGITile(tileFeat);
192
QPointF org = getTileOrigin();
193
tile->setTilePosition(org, row, col);
194
tile->setFont(fontName, fontSize);
195
tile->setColor(getCurrentColor());
196
tile->setTileTextLeft(tileTextL);
197
tile->setTileTextRight(tileTextR);
198
tile->setTileTextCenter(tileTextC);
199
// tile->setSymbolFile(symbolFile);
200
tile->setZValue(ZVALUE::DIMENSION);
201
tile->setTileScale(featScale);
202
tile->setTailRight(getFeature()->isTailRightSide());
203
tile->setAltWeld(getFeature()->AlternatingWeld.getValue());
208
void QGIWeldSymbol::drawAllAround()
210
// Base::Console().Message("QGIWS::drawAllAround()\n");
211
QPointF allAroundPos = getKinkPoint();
212
m_allAround->setPos(allAroundPos);
214
if (getFeature()->AllAround.getValue()) {
222
m_allAround->setNormalColor(getCurrentColor());
224
m_allAround->setFill(Qt::NoBrush);
225
// m_allAround->setRadius(calculateFontPixelSize(getDimFontSize()));
226
m_allAround->setRadius(PreferencesGui::dimFontSizePX());
228
auto qgiLead = dynamic_cast<QGILeaderLine *>(getQGIVByName(getLeader()->getNameInDocument()));
230
m_allAround->setWidth(qgiLead->getLineWidth());
232
m_allAround->setZValue(ZVALUE::DIMENSION);
235
void QGIWeldSymbol::drawTailText()
237
// Base::Console().Message("QGIWS::drawTailText()\n");
238
QPointF textPos = getTailPoint();
239
m_tailText->setPos(textPos); //avoid messing up brect with empty item at 0, 0 !!!
240
std::string tText = getFeature()->TailText.getValue();
247
const auto sym = getFeature();
250
auto vp = dynamic_cast<ViewProviderWeld *>(getViewProvider(sym));
254
QString qFontName = Base::Tools::fromStdString(vp->Font.getValue());
255
int fontSize = QGIView::exactFontSize(vp->Font.getValue(),
256
vp->FontSize.getValue());
258
m_font.setFamily(qFontName);
259
m_font.setPixelSize(fontSize);
261
m_tailText->setFont(m_font);
262
m_tailText->setPlainText(
263
QString::fromUtf8(tText.c_str()));
264
m_tailText->setColor(getCurrentColor());
265
m_tailText->setZValue(ZVALUE::DIMENSION);
267
double textWidth = m_tailText->boundingRect().width();
268
double charWidth = textWidth / tText.size();
269
double hMargin = charWidth + prefArrowSize();
271
double textHeight = m_tailText->boundingRect().width();
272
double vFudge = textHeight * 0.1;
274
if (getFeature()->isTailRightSide()) {
275
m_tailText->justifyLeftAt(textPos.x() + hMargin, textPos.y() - vFudge, true);
277
m_tailText->justifyRightAt(textPos.x() - hMargin, textPos.y() - vFudge, true);
281
void QGIWeldSymbol::drawFieldFlag()
283
// Base::Console().Message("QGIWS::drawFieldFlag()\n");
284
QPointF fieldFlagPos = getKinkPoint();
285
m_fieldFlag->setPos(fieldFlagPos);
287
if (getFeature()->FieldWeld.getValue()) {
293
std::vector<QPointF> flagPoints = { QPointF(0.0, 0.0),
296
QPointF(0.0, -2.0) };
297
//flag sb about 2x text?
298
// double scale = calculateFontPixelSize(getDimFontSize()) / 2.0;
299
double scale = PreferencesGui::dimFontSizePX() / 2.0;
301
path.moveTo(flagPoints.at(0) * scale);
303
int stop = flagPoints.size();
304
for ( ; i < stop; i++) {
305
path.lineTo(flagPoints.at(i) * scale);
308
auto qgiLead = dynamic_cast<QGILeaderLine *>(getQGIVByName(getLeader()->getNameInDocument()));
310
m_fieldFlag->setWidth(qgiLead->getLineWidth());
312
m_fieldFlag->setZValue(ZVALUE::DIMENSION);
314
m_fieldFlag->setPath(path);
317
void QGIWeldSymbol::getTileFeats()
319
std::vector<TechDraw::DrawTileWeld*> tiles = getFeature()->getTiles();
320
m_arrowFeat = nullptr;
321
m_otherFeat = nullptr;
323
if (!tiles.empty()) {
324
TechDraw::DrawTileWeld* tempTile = tiles.at(0);
325
if (tempTile->TileRow.getValue() == 0) {
326
m_arrowFeat = tempTile;
328
m_otherFeat = tempTile;
331
if (tiles.size() > 1) {
332
TechDraw::DrawTileWeld* tempTile = tiles.at(1);
333
if (tempTile->TileRow.getValue() == 0) {
334
m_arrowFeat = tempTile;
336
m_otherFeat = tempTile;
341
void QGIWeldSymbol::removeQGITiles()
343
std::vector<QGITile*> tiles = getQGITiles();
344
for (auto t: tiles) {
345
QList<QGraphicsItem*> tChildren = t->childItems();
346
for (auto tc: tChildren) {
347
t->removeFromGroup(tc);
348
scene()->removeItem(tc);
349
//tc gets deleted when QGIWS gets deleted
352
scene()->removeItem(t);
357
std::vector<QGITile*> QGIWeldSymbol::getQGITiles() const
359
std::vector<QGITile*> result;
360
QList<QGraphicsItem*> children = childItems();
361
for (auto& c:children) {
362
QGITile* tile = dynamic_cast<QGITile*>(c);
364
result.push_back(tile);
370
void QGIWeldSymbol::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
374
setCurrentColor(getSelectColor());
377
setCurrentColor(getPreColor());
380
QGIView::hoverEnterEvent(event);
383
void QGIWeldSymbol::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
387
setCurrentColor(getSelectColor());
390
setCurrentColor(getNormalColor());
393
QGIView::hoverLeaveEvent(event);
396
void QGIWeldSymbol::drawBorder()
398
////Weld Symbols have no border!
399
// QGIView::drawBorder(); //good for debugging
402
void QGIWeldSymbol::setPrettyNormal()
404
std::vector<QGITile*> tiles = getQGITiles();
405
for (auto t: tiles) {
406
t->setColor(getNormalColor());
409
setCurrentColor(getNormalColor());
410
m_fieldFlag->setNormalColor(getNormalColor());
411
m_fieldFlag->setPrettyNormal();
412
m_allAround->setNormalColor(getNormalColor());
413
m_allAround->setPrettyNormal();
414
m_tailText->setColor(getNormalColor());
415
m_tailText->setPrettyNormal();
418
void QGIWeldSymbol::setPrettyPre()
420
std::vector<QGITile*> tiles = getQGITiles();
421
for (auto t: tiles) {
422
t->setColor(getPreColor());
426
setCurrentColor(getPreColor());
427
m_fieldFlag->setNormalColor(getPreColor());
428
m_fieldFlag->setPrettyPre();
429
m_allAround->setNormalColor(getPreColor());
430
m_allAround->setPrettyPre();
431
m_tailText->setColor(getPreColor());
432
m_tailText->setPrettyPre();
435
void QGIWeldSymbol::setPrettySel()
437
std::vector<QGITile*> tiles = getQGITiles();
438
for (auto t: tiles) {
439
t->setColor(getSelectColor());
443
setCurrentColor(getSelectColor());
444
m_fieldFlag->setNormalColor(getSelectColor());
445
m_fieldFlag->setPrettySel();
446
m_allAround->setNormalColor(getSelectColor());
447
m_allAround->setPrettySel();
448
m_tailText->setColor(getSelectColor());
449
m_tailText->setPrettySel();
452
QPointF QGIWeldSymbol::getTileOrigin()
454
Base::Vector3d org = getLeader()->getTileOrigin();
455
QPointF result(org.x, org.y);
459
QPointF QGIWeldSymbol::getKinkPoint()
461
Base::Vector3d org = getLeader()->getKinkPoint();
462
QPointF result(org.x, org.y);
466
QPointF QGIWeldSymbol::getTailPoint()
468
Base::Vector3d org = getLeader()->getTailPoint();
469
QPointF result(org.x, org.y);
473
TechDraw::DrawWeldSymbol* QGIWeldSymbol::getFeature()
475
return dynamic_cast<TechDraw::DrawWeldSymbol *>(getViewObject());
478
TechDraw::DrawLeaderLine *QGIWeldSymbol::getLeader()
480
DrawWeldSymbol *feature = getFeature();
485
return dynamic_cast<TechDraw::DrawLeaderLine *>(feature->Leader.getValue());
489
QColor QGIWeldSymbol::prefNormalColor()
491
setNormalColor(PreferencesGui::leaderQColor());
492
return getNormalColor();
495
double QGIWeldSymbol::prefArrowSize()
497
return PreferencesGui::dimArrowSize();
500
double QGIWeldSymbol::prefFontSize() const
502
return Preferences::labelFontSizeMM();
505
QRectF QGIWeldSymbol::boundingRect() const
507
return customBoundingRect();
510
QRectF QGIWeldSymbol::customBoundingRect() const
515
QRectF childRect = mapFromItem(m_tailText, m_tailText->boundingRect()).boundingRect();
516
result = result.united(childRect);
520
QRectF childRect = mapFromItem(m_fieldFlag, m_fieldFlag->boundingRect()).boundingRect();
521
result = result.united(childRect);
525
QRectF childRect = mapFromItem(m_allAround, m_allAround->boundingRect()).boundingRect();
526
result = result.united(childRect);
529
std::vector<QGITile*> qgTiles = getQGITiles();
530
for (auto& t: qgTiles) {
531
QRectF childRect = mapFromItem(t, t->boundingRect()).boundingRect();
532
result = result.united(childRect);
538
QPainterPath QGIWeldSymbol::shape() const
540
return QGraphicsItemGroup::shape();
543
void QGIWeldSymbol::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
544
QStyleOptionGraphicsItem myOption(*option);
545
myOption.state &= ~QStyle::State_Selected;
547
// painter->setPen(Qt::red);
548
// painter->drawRect(boundingRect()); //good for debugging
550
QGIView::paint (painter, &myOption, widget);
553
#include <Mod/TechDraw/Gui/moc_QGIWeldSymbol.cpp>