FreeCAD

Форк
0
/
QGIWeldSymbol.cpp 
553 строки · 16.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2019 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 <cmath>
26
# include <QGraphicsScene>
27
# include <QPainterPath>
28
#endif
29

30
#include <Base/Console.h>
31
#include <Base/Tools.h>
32

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>
37

38
#include "QGIWeldSymbol.h"
39
#include "PreferencesGui.h"
40
#include "QGCustomText.h"
41
#include "QGILeaderLine.h"
42
#include "QGIPrimPath.h"
43
#include "QGITile.h"
44
#include "QGIVertex.h"
45

46
#include "ViewProviderWeld.h"
47
#include "ZVALUE.h"
48

49

50
using namespace TechDraw;
51
using namespace TechDrawGui;
52

53

54
//**************************************************************
55
QGIWeldSymbol::QGIWeldSymbol() :
56
    m_arrowFeat(nullptr),
57
    m_otherFeat(nullptr),
58
    m_tailText(nullptr),
59
    m_fieldFlag(nullptr),
60
    m_allAround(nullptr),
61
    m_blockDraw(false)
62
{
63
    setFiltersChildEvents(true);    //qt5
64
    setFlag(QGraphicsItem::ItemIsMovable, false);
65

66
    setCacheMode(QGraphicsItem::NoCache);
67
    setZValue(ZVALUE::DIMENSION);
68

69
    m_tailText = new QGCustomText();
70
    m_tailText->setPlainText(
71
                QString::fromUtf8(" "));
72
    addToGroup(m_tailText);
73
    m_tailText->hide();
74
    m_tailText->setPos(0.0, 0.0);         //avoid bRect issues
75

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);
85

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);
96

97
    setNormalColor(prefNormalColor());
98
    setCurrentColor(getNormalColor());
99
    setSettingColor(getNormalColor());
100

101
    setPrettyNormal();
102
}
103

104
QVariant QGIWeldSymbol::itemChange(GraphicsItemChange change, const QVariant &value)
105
{
106
//    Base::Console().Message("QGIWS::itemChange(%d)\n", change);
107
    if (change == ItemSelectedHasChanged && scene()) {
108
        if(isSelected()) {
109
            setPrettySel();
110
        } else {
111
            setPrettyNormal();
112
        }
113
    } else if(change == ItemSceneChange && scene()) {
114
        // nothing special!
115
    }
116
    return QGIView::itemChange(change, value);
117
}
118

119
void QGIWeldSymbol::updateView(bool update)
120
{
121
//    Base::Console().Message("QGIWS::updateView()\n");
122
    Q_UNUSED(update);
123

124
    TechDraw::DrawWeldSymbol *feature = getFeature();
125
    if (!feature) {
126
        Base::Console().Warning("QGIWS::updateView - no feature!\n");
127
        return;
128
    }
129

130
    if (feature->isRestoring() || !getLeader()) {
131
        return;
132
    }
133

134
    draw();
135
}
136

137
void QGIWeldSymbol::draw()
138
{
139
//    Base::Console().Message("QGIWS::draw()- %s\n", getFeature()->getNameInDocument());
140
    if (!isVisible()) {
141
        return;
142
    }
143
    getTileFeats();
144

145
    removeQGITiles();
146

147
    if (m_arrowFeat) {
148
        drawTile(m_arrowFeat);
149
    }
150

151
    if (m_otherFeat) {
152
        drawTile(m_otherFeat);
153
    }
154

155
    drawAllAround();
156

157
    drawFieldFlag();
158

159
    drawTailText();
160
}
161

162
void QGIWeldSymbol::drawTile(TechDraw::DrawTileWeld* tileFeat)
163
{
164
//    Base::Console().Message("QGIWS::drawTile() - tileFeat: %X\n", tileFeat);
165
    if (!tileFeat) {
166
        Base::Console().Message("QGIWS::drawTile - tile is null\n");
167
        return;
168
    }
169

170
    const auto sym = getFeature();
171
    if (!sym)
172
        return;
173
    auto vp = dynamic_cast<ViewProviderWeld *>(getViewProvider(sym));
174
    if (!vp)
175
        return;
176
    std::string fontName = vp->Font.getValue();
177
    int         fontSize = QGIView::exactFontSize(vp->Font.getValue(),
178
                                                  vp->TileFontSize.getValue());
179

180
    double featScale = getLeader()->getScale();
181

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();
188

189
    QGITile* tile = new QGITile(tileFeat);
190
    addToGroup(tile);
191

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());
204

205
    tile->draw();
206
}
207

208
void QGIWeldSymbol::drawAllAround()
209
{
210
//    Base::Console().Message("QGIWS::drawAllAround()\n");
211
    QPointF allAroundPos = getKinkPoint();
212
    m_allAround->setPos(allAroundPos);
213

214
    if (getFeature()->AllAround.getValue()) {
215
        m_allAround->show();
216
    } else {
217

218
        m_allAround->hide();
219
        return;
220
    }
221

222
    m_allAround->setNormalColor(getCurrentColor());
223

224
    m_allAround->setFill(Qt::NoBrush);
225
//    m_allAround->setRadius(calculateFontPixelSize(getDimFontSize()));
226
    m_allAround->setRadius(PreferencesGui::dimFontSizePX());
227

228
    auto qgiLead = dynamic_cast<QGILeaderLine *>(getQGIVByName(getLeader()->getNameInDocument()));
229
    if (qgiLead) {
230
         m_allAround->setWidth(qgiLead->getLineWidth());
231
    }
232
    m_allAround->setZValue(ZVALUE::DIMENSION);
233
}
234

235
void QGIWeldSymbol::drawTailText()
236
{
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();
241
    if (tText.empty()) {
242
        m_tailText->hide();
243
        return;
244
    } else {
245
        m_tailText->show();
246
    }
247
    const auto sym = getFeature();
248
    if (!sym)
249
        return;
250
    auto vp = dynamic_cast<ViewProviderWeld *>(getViewProvider(sym));
251
    if (!vp) {
252
        return;
253
    }
254
    QString qFontName = Base::Tools::fromStdString(vp->Font.getValue());
255
    int fontSize = QGIView::exactFontSize(vp->Font.getValue(),
256
                                          vp->FontSize.getValue());
257

258
    m_font.setFamily(qFontName);
259
    m_font.setPixelSize(fontSize);
260

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);
266

267
    double textWidth = m_tailText->boundingRect().width();
268
    double charWidth = textWidth / tText.size();
269
    double hMargin = charWidth + prefArrowSize();
270

271
    double textHeight = m_tailText->boundingRect().width();
272
    double vFudge = textHeight * 0.1;
273

274
    if (getFeature()->isTailRightSide()) {
275
        m_tailText->justifyLeftAt(textPos.x() + hMargin, textPos.y() - vFudge, true);
276
    } else {
277
        m_tailText->justifyRightAt(textPos.x() - hMargin, textPos.y() - vFudge, true);
278
    }
279
}
280

281
void QGIWeldSymbol::drawFieldFlag()
282
{
283
//    Base::Console().Message("QGIWS::drawFieldFlag()\n");
284
    QPointF fieldFlagPos = getKinkPoint();
285
    m_fieldFlag->setPos(fieldFlagPos);
286

287
    if (getFeature()->FieldWeld.getValue()) {
288
        m_fieldFlag->show();
289
    } else {
290
        m_fieldFlag->hide();
291
        return;
292
    }
293
    std::vector<QPointF> flagPoints = { QPointF(0.0, 0.0),
294
                                        QPointF(0.0, -3.0),
295
                                        QPointF(-2.0, -2.5),
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;
300
    QPainterPath path;
301
    path.moveTo(flagPoints.at(0) * scale);
302
    int i = 1;
303
    int stop = flagPoints.size();
304
    for ( ; i < stop; i++) {
305
        path.lineTo(flagPoints.at(i) * scale);
306
    }
307

308
    auto qgiLead = dynamic_cast<QGILeaderLine *>(getQGIVByName(getLeader()->getNameInDocument()));
309
    if (qgiLead) {
310
        m_fieldFlag->setWidth(qgiLead->getLineWidth());
311
    }
312
    m_fieldFlag->setZValue(ZVALUE::DIMENSION);
313

314
    m_fieldFlag->setPath(path);
315
}
316

317
void QGIWeldSymbol::getTileFeats()
318
{
319
    std::vector<TechDraw::DrawTileWeld*> tiles = getFeature()->getTiles();
320
    m_arrowFeat = nullptr;
321
    m_otherFeat = nullptr;
322

323
    if (!tiles.empty()) {
324
        TechDraw::DrawTileWeld* tempTile = tiles.at(0);
325
        if (tempTile->TileRow.getValue() == 0) {
326
            m_arrowFeat = tempTile;
327
        } else {
328
            m_otherFeat = tempTile;
329
        }
330
    }
331
    if (tiles.size() > 1) {
332
        TechDraw::DrawTileWeld* tempTile = tiles.at(1);
333
        if (tempTile->TileRow.getValue() == 0) {
334
            m_arrowFeat = tempTile;
335
        } else {
336
            m_otherFeat = tempTile;
337
        }
338
    }
339
}
340

341
void QGIWeldSymbol::removeQGITiles()
342
{
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
350
            }
351
        removeFromGroup(t);
352
        scene()->removeItem(t);
353
        delete t;
354
    }
355
}
356

357
std::vector<QGITile*> QGIWeldSymbol::getQGITiles() const
358
{
359
    std::vector<QGITile*> result;
360
    QList<QGraphicsItem*> children = childItems();
361
    for (auto& c:children) {
362
         QGITile* tile = dynamic_cast<QGITile*>(c);
363
         if (tile) {
364
            result.push_back(tile);
365
         }
366
     }
367
     return result;
368
}
369

370
void QGIWeldSymbol::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
371
{
372
    Q_UNUSED(event);
373
    if (isSelected()) {
374
        setCurrentColor(getSelectColor());
375
        setPrettySel();
376
    } else {
377
        setCurrentColor(getPreColor());
378
        setPrettyPre();
379
    }
380
    QGIView::hoverEnterEvent(event);
381
}
382

383
void QGIWeldSymbol::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
384
{
385
    Q_UNUSED(event);
386
    if(isSelected()) {
387
        setCurrentColor(getSelectColor());
388
        setPrettySel();
389
    } else {
390
        setCurrentColor(getNormalColor());
391
        setPrettyNormal();
392
    }
393
    QGIView::hoverLeaveEvent(event);
394
}
395

396
void QGIWeldSymbol::drawBorder()
397
{
398
////Weld Symbols have no border!
399
//    QGIView::drawBorder();   //good for debugging
400
}
401

402
void QGIWeldSymbol::setPrettyNormal()
403
{
404
    std::vector<QGITile*> tiles = getQGITiles();
405
    for (auto t: tiles) {
406
        t->setColor(getNormalColor());
407
        t->draw();
408
    }
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();
416
}
417

418
void QGIWeldSymbol::setPrettyPre()
419
{
420
    std::vector<QGITile*> tiles = getQGITiles();
421
    for (auto t: tiles) {
422
        t->setColor(getPreColor());
423
        t->draw();
424
    }
425

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();
433
}
434

435
void QGIWeldSymbol::setPrettySel()
436
{
437
    std::vector<QGITile*> tiles = getQGITiles();
438
    for (auto t: tiles) {
439
        t->setColor(getSelectColor());
440
        t->draw();
441
    }
442

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();
450
}
451

452
QPointF QGIWeldSymbol::getTileOrigin()
453
{
454
    Base::Vector3d org = getLeader()->getTileOrigin();
455
    QPointF result(org.x, org.y);
456
    return result;
457
}
458

459
QPointF QGIWeldSymbol::getKinkPoint()
460
{
461
    Base::Vector3d org = getLeader()->getKinkPoint();
462
    QPointF result(org.x, org.y);
463
    return result;
464
}
465

466
QPointF QGIWeldSymbol::getTailPoint()
467
{
468
    Base::Vector3d org = getLeader()->getTailPoint();
469
    QPointF result(org.x, org.y);
470
    return result;
471
}
472

473
TechDraw::DrawWeldSymbol* QGIWeldSymbol::getFeature()
474
{
475
    return dynamic_cast<TechDraw::DrawWeldSymbol *>(getViewObject());
476
}
477

478
TechDraw::DrawLeaderLine *QGIWeldSymbol::getLeader()
479
{
480
    DrawWeldSymbol *feature = getFeature();
481
    if (!feature) {
482
        return nullptr;
483
    }
484

485
    return dynamic_cast<TechDraw::DrawLeaderLine *>(feature->Leader.getValue());
486
}
487

488
//preference
489
QColor QGIWeldSymbol::prefNormalColor()
490
{
491
    setNormalColor(PreferencesGui::leaderQColor());
492
    return getNormalColor();
493
}
494

495
double QGIWeldSymbol::prefArrowSize()
496
{
497
    return PreferencesGui::dimArrowSize();
498
}
499

500
double QGIWeldSymbol::prefFontSize() const
501
{
502
    return Preferences::labelFontSizeMM();
503
}
504

505
QRectF QGIWeldSymbol::boundingRect() const
506
{
507
    return customBoundingRect();
508
}
509

510
QRectF QGIWeldSymbol::customBoundingRect() const
511
{
512
    QRectF result;
513

514
    if (m_tailText) {
515
        QRectF childRect = mapFromItem(m_tailText, m_tailText->boundingRect()).boundingRect();
516
        result = result.united(childRect);
517
    }
518

519
    if (m_fieldFlag) {
520
        QRectF childRect = mapFromItem(m_fieldFlag, m_fieldFlag->boundingRect()).boundingRect();
521
        result = result.united(childRect);
522
    }
523

524
    if (m_allAround) {
525
        QRectF childRect = mapFromItem(m_allAround, m_allAround->boundingRect()).boundingRect();
526
        result = result.united(childRect);
527
    }
528

529
    std::vector<QGITile*> qgTiles = getQGITiles();
530
    for (auto& t: qgTiles) {
531
        QRectF childRect = mapFromItem(t, t->boundingRect()).boundingRect();
532
        result = result.united(childRect);
533
    }
534
    return result;
535
}
536

537

538
QPainterPath QGIWeldSymbol::shape() const
539
{
540
    return QGraphicsItemGroup::shape();
541
}
542

543
void QGIWeldSymbol::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
544
    QStyleOptionGraphicsItem myOption(*option);
545
    myOption.state &= ~QStyle::State_Selected;
546

547
//    painter->setPen(Qt::red);
548
//    painter->drawRect(boundingRect());          //good for debugging
549

550
    QGIView::paint (painter, &myOption, widget);
551
}
552

553
#include <Mod/TechDraw/Gui/moc_QGIWeldSymbol.cpp>
554

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

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

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

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