23
#include "PreCompiled.h"
25
# include <QGraphicsSceneHoverEvent>
26
# include <QGraphicsSceneMouseEvent>
28
# include <QStyleOptionGraphicsItem>
32
#include <App/Application.h>
33
#include <App/DocumentObject.h>
34
#include <Base/Console.h>
35
#include <Gui/Application.h>
36
#include <Gui/Document.h>
37
#include <Gui/Selection.h>
39
#include <Gui/ViewProvider.h>
40
#include <Mod/TechDraw/App/DrawPage.h>
41
#include <Mod/TechDraw/App/DrawProjGroup.h>
42
#include <Mod/TechDraw/App/DrawProjGroupItem.h>
43
#include <Mod/TechDraw/App/DrawUtil.h>
44
#include <Mod/TechDraw/App/DrawView.h>
47
#include "MDIViewPage.h"
48
#include "PreferencesGui.h"
49
#include "QGCustomBorder.h"
50
#include "QGCustomClip.h"
51
#include "QGCustomImage.h"
52
#include "QGCustomLabel.h"
53
#include "QGICaption.h"
55
#include "QGIViewClip.h"
59
#include "ViewProviderDrawingView.h"
60
#include "ViewProviderPage.h"
64
using namespace TechDrawGui;
65
using namespace TechDraw;
67
const float labelCaptionFudge = 0.2f;
70
:QGraphicsItemGroup(),
73
m_multiselectActivated(false)
75
setCacheMode(QGraphicsItem::NoCache);
76
setHandlesChildEvents(false);
77
setAcceptHoverEvents(true);
78
setFlag(QGraphicsItem::ItemIsSelectable, true);
79
setFlag(QGraphicsItem::ItemIsMovable, true);
80
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
81
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
83
m_colNormal = prefNormalColor();
84
m_colCurrent = m_colNormal;
85
m_pen.setColor(m_colCurrent);
87
m_decorPen.setStyle(Qt::DashLine);
88
m_decorPen.setWidth(0);
90
m_label = new QGCustomLabel();
92
m_border = new QGCustomBorder();
94
m_caption = new QGICaption();
95
addToGroup(m_caption);
96
m_lock = new QGCustomImage();
97
m_lock->setParentItem(m_border);
98
m_lock->load(QString::fromUtf8(":/icons/TechDraw_Lock.svg"));
99
QSize sizeLock = m_lock->imageSize();
100
m_lockWidth = (double) sizeLock.width();
101
m_lockHeight = (double) sizeLock.height();
105
void QGIView::isVisible(bool state)
107
auto feat = getViewObject();
109
auto vp = QGIView::getViewProvider(feat);
111
Gui::ViewProviderDocumentObject* vpdo = dynamic_cast<Gui::ViewProviderDocumentObject*>(vp);
113
vpdo->Visibility.setValue(state);
116
bool QGIView::isVisible()
118
auto feat = getViewObject();
119
if (!feat) return false;
120
auto vp = QGIView::getViewProvider(feat);
121
if (!vp) return false;
122
Gui::ViewProviderDocumentObject* vpdo = dynamic_cast<Gui::ViewProviderDocumentObject*>(vp);
123
if (!vpdo) return false;
124
return vpdo->Visibility.getValue();
128
bool QGIView::getGroupSelection()
135
void QGIView::setGroupSelection(bool isSelected)
137
setSelected(isSelected);
141
void QGIView::setGroupSelection(bool isSelected, const std::vector<std::string> &subNames)
144
setGroupSelection(isSelected);
147
void QGIView::alignTo(QGraphicsItem*item, const QString &alignment)
150
alignHash.insert(alignment, item);
153
QVariant QGIView::itemChange(GraphicsItemChange change, const QVariant &value)
155
QPointF newPos(0.0, 0.0);
157
if(change == ItemPositionChange && scene()) {
158
newPos = value.toPointF();
160
TechDraw::DrawView *viewObj = getViewObject();
161
if (viewObj->isDerivedFrom(TechDraw::DrawProjGroupItem::getClassTypeId())) {
163
TechDraw::DrawProjGroupItem* dpgi = static_cast<TechDraw::DrawProjGroupItem*>(viewObj);
164
TechDraw::DrawProjGroup* dpg = dpgi->getPGroup();
166
if(alignHash.size() == 1) {
167
QGraphicsItem* item = alignHash.begin().value();
168
QString alignMode = alignHash.begin().key();
169
if(alignMode == QString::fromLatin1("Vertical")) {
170
newPos.setX(item->pos().x());
171
} else if(alignMode == QString::fromLatin1("Horizontal")) {
172
newPos.setY(item->pos().y());
178
Gui::ViewProvider *vp = getViewProvider(viewObj);
179
if (vp && !vp->isRestoring()) {
180
viewObj->setPosition(Rez::appX(newPos.x()), Rez::appX(-newPos.y()));
186
if (change == ItemSelectedHasChanged && scene()) {
188
m_colCurrent = getSelectColor();
191
m_colCurrent = PreferencesGui::getAccessibleQColor(PreferencesGui::normalQColor());
197
return QGraphicsItemGroup::itemChange(change, value);
200
void QGIView::mousePressEvent(QGraphicsSceneMouseEvent * event)
203
Qt::KeyboardModifiers originalModifiers = event->modifiers();
204
if (event->button()&Qt::LeftButton) {
205
m_multiselectActivated = false;
208
if (event->button() == Qt::LeftButton && PreferencesGui::multiSelection()) {
209
std::vector<Gui::SelectionObject> selection = Gui::Selection().getSelectionEx();
210
if (selection.size() == 1
211
&& selection.front().getObject() == getViewObject()
212
&& selection.front().hasSubNames()) {
214
m_multiselectActivated = true;
215
event->setModifiers(originalModifiers | Qt::ControlModifier);
219
QGraphicsItemGroup::mousePressEvent(event);
221
event->setModifiers(originalModifiers);
224
void QGIView::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
226
QGraphicsItemGroup::mouseMoveEvent(event);
229
void QGIView::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
232
Qt::KeyboardModifiers originalModifiers = event->modifiers();
233
if ((event->button()&Qt::LeftButton) && m_multiselectActivated) {
234
if (PreferencesGui::multiSelection()) {
235
event->setModifiers(originalModifiers | Qt::ControlModifier);
238
m_multiselectActivated = false;
241
QGraphicsItemGroup::mouseReleaseEvent(event);
243
event->setModifiers(originalModifiers);
246
void QGIView::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
252
m_colCurrent = getSelectColor();
255
m_colCurrent = getPreColor();
260
void QGIView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
264
m_colCurrent = getSelectColor();
266
m_colCurrent = PreferencesGui::getAccessibleQColor(PreferencesGui::normalQColor());
272
void QGIView::setPosition(qreal xPos, qreal yPos)
277
double oldX = pos().x();
278
double oldY = pos().y();
280
if (TechDraw::DrawUtil::fpCompare(newX, oldX) &&
281
TechDraw::DrawUtil::fpCompare(newY, oldY)) {
288
QGIViewClip* QGIView::getClipGroup()
290
if (!getViewObject()->isInClip()) {
294
auto parentClip( dynamic_cast<QGCustomClip*>( parentItem() ) );
295
if (!parentClip) return nullptr;
297
auto parentView( dynamic_cast<QGIViewClip*>( parentClip->parentItem() ) );
301
void QGIView::updateView(bool forceUpdate)
306
if (getViewObject()->isLocked()) {
307
setFlag(QGraphicsItem::ItemIsMovable, false);
309
setFlag(QGraphicsItem::ItemIsMovable, true);
312
if (getViewObject() && forceUpdate) {
313
setPosition(Rez::guiX(getViewObject()->X.getValue()),
314
Rez::guiX(getViewObject()->Y.getValue()));
317
double appRotation = getViewObject()->Rotation.getValue();
318
double guiRotation = rotation();
319
if (!TechDraw::DrawUtil::fpCompare(appRotation, guiRotation)) {
327
void QGIView::rotateView()
333
double rot = getViewObject()->Rotation.getValue();
334
QPointF centre = boundingRect().center();
335
setTransform(QTransform().translate(centre.x(), centre.y()).rotate(-rot).translate(-centre.x(), -centre.y()));
338
double QGIView::getScale()
340
TechDraw::DrawView* feat = getViewObject();
344
return feat->getScale();
346
const char * QGIView::getViewName() const
348
return viewName.c_str();
350
const std::string QGIView::getViewNameAsString() const
356
TechDraw::DrawView * QGIView::getViewObject() const
361
void QGIView::setViewFeature(TechDraw::DrawView *obj)
367
viewName = obj->getNameInDocument();
370
setData(0, QString::fromUtf8("QGIV"));
371
setData(1, QString::fromUtf8(obj->getNameInDocument()));
374
void QGIView::toggleCache(bool state)
379
setCacheMode(NoCache);
386
if (getViewObject()) {
387
xFeat = Rez::guiX(getViewObject()->X.getValue());
388
yFeat = Rez::guiX(getViewObject()->Y.getValue());
389
if (!getViewObject()->LockPosition.getValue()) {
390
setPosition(xFeat, yFeat);
401
void QGIView::drawCaption()
404
prepareGeometryChange();
405
QRectF displayArea = customChildrenBoundingRect();
406
m_caption->setDefaultTextColor(m_colCurrent);
407
m_font.setFamily(Preferences::labelFontQString());
408
int fontSize = exactFontSize(Preferences::labelFont(),
409
Preferences::labelFontSizeMM());
410
m_font.setPixelSize(fontSize);
411
m_caption->setFont(m_font);
412
QString captionStr = QString::fromUtf8(getViewObject()->Caption.getValue());
413
m_caption->setPlainText(captionStr);
414
QRectF captionArea = m_caption->boundingRect();
415
QPointF displayCenter = displayArea.center();
416
m_caption->setX(displayCenter.x() - captionArea.width()/2.);
417
double labelHeight = (1 - labelCaptionFudge) * m_label->boundingRect().height();
418
auto vp = static_cast<ViewProviderDrawingView*>(getViewProvider(getViewObject()));
419
if (getFrameState() || vp->KeepLabel.getValue()) {
420
m_caption->setY(displayArea.bottom() + labelHeight);
422
m_caption->setY(displayArea.bottom() + labelCaptionFudge * Preferences::labelFontSizeMM());
427
void QGIView::drawBorder()
430
auto feat = getViewObject();
436
auto vp = static_cast<ViewProviderDrawingView*>(getViewProvider(getViewObject()));
437
if (!getFrameState() && !vp->KeepLabel.getValue()) {
448
m_label->setDefaultTextColor(m_colCurrent);
449
m_font.setFamily(Preferences::labelFontQString());
450
int fontSize = exactFontSize(Preferences::labelFont(),
451
Preferences::labelFontSizeMM());
452
m_font.setPixelSize(fontSize);
453
m_label->setFont(m_font);
455
QString labelStr = QString::fromStdString( getViewObject()->Label.getValue() );
456
m_label->setPlainText(labelStr);
457
QRectF labelArea = m_label->boundingRect();
458
double labelWidth = m_label->boundingRect().width();
459
double labelHeight = (1 - labelCaptionFudge) * m_label->boundingRect().height();
461
QBrush b(Qt::NoBrush);
462
m_border->setBrush(b);
463
m_decorPen.setColor(m_colCurrent);
464
m_border->setPen(m_decorPen);
466
QRectF displayArea = customChildrenBoundingRect();
467
double displayWidth = displayArea.width();
468
double displayHeight = displayArea.height();
469
QPointF displayCenter = displayArea.center();
470
m_label->setX(displayCenter.x() - labelArea.width()/2.);
471
m_label->setY(displayArea.bottom());
473
double frameWidth = displayWidth;
474
if (labelWidth > displayWidth) {
475
frameWidth = labelWidth;
477
double frameHeight = labelHeight + displayHeight;
479
QRectF frameArea = QRectF(displayCenter.x() - frameWidth/2.,
484
double lockX = frameArea.left();
485
double lockY = frameArea.bottom() - m_lockHeight;
486
if (feat->isLocked() &&
488
m_lock->setZValue(ZVALUE::LOCK);
489
m_lock->setPos(lockX, lockY);
495
prepareGeometryChange();
496
m_border->setRect(frameArea.adjusted(-2, -2, 2,2));
497
m_border->setPos(0., 0.);
500
if (getFrameState()) {
505
void QGIView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
507
QStyleOptionGraphicsItem myOption(*option);
508
myOption.state &= ~QStyle::State_Selected;
513
QGraphicsItemGroup::paint(painter, &myOption, widget);
516
QRectF QGIView::customChildrenBoundingRect() const
518
QList<QGraphicsItem*> children = childItems();
520
int dimItemType = QGraphicsItem::UserType + 106;
521
int borderItemType = QGraphicsItem::UserType + 136;
522
int labelItemType = QGraphicsItem::UserType + 135;
523
int captionItemType = QGraphicsItem::UserType + 180;
524
int leaderItemType = QGraphicsItem::UserType + 232;
525
int textLeaderItemType = QGraphicsItem::UserType + 233;
526
int editablePathItemType = QGraphicsItem::UserType + 301;
527
int movableTextItemType = QGraphicsItem::UserType + 300;
528
int weldingSymbolItemType = QGraphicsItem::UserType + 340;
529
int centerMarkItemType = QGraphicsItem::UserType + 171;
531
for (auto& child : children) {
532
if (!child->isVisible()) {
535
if ( (child->type() != dimItemType) &&
536
(child->type() != leaderItemType) &&
537
(child->type() != textLeaderItemType) &&
538
(child->type() != editablePathItemType) &&
539
(child->type() != movableTextItemType) &&
540
(child->type() != borderItemType) &&
541
(child->type() != labelItemType) &&
542
(child->type() != weldingSymbolItemType) &&
543
(child->type() != captionItemType) &&
544
(child->type() != centerMarkItemType)) {
545
QRectF childRect = mapFromItem(child, child->boundingRect()).boundingRect();
546
result = result.united(childRect);
552
QRectF QGIView::boundingRect() const
554
return m_border->rect().adjusted(-2., -2., 2., 2.);
557
QGIView* QGIView::getQGIVByName(std::string name)
559
QList<QGraphicsItem*> qgItems = scene()->items();
560
QList<QGraphicsItem*>::iterator it = qgItems.begin();
561
for (; it != qgItems.end(); it++) {
562
QGIView* qv = dynamic_cast<QGIView*>((*it));
564
std::string qvName = qv->getViewNameAsString();
565
if (name == qvName) {
574
Gui::ViewProvider* QGIView::getViewProvider(App::DocumentObject* obj)
577
Gui::Document* guiDoc = Gui::Application::Instance->getDocument(obj->getDocument());
578
return guiDoc->getViewProvider(obj);
583
MDIViewPage* QGIView::getMDIViewPage() const
585
if (!getViewObject()) {
588
ViewProviderPage* vpp = getViewProviderPage(getViewObject());
590
return vpp->getMDIViewPage();
595
ViewProviderPage* QGIView::getViewProviderPage(TechDraw::DrawView* dView)
600
TechDraw::DrawPage* page = dView->findParentPage();
605
Gui::Document* activeGui = Gui::Application::Instance->getDocument(page->getDocument());
610
return dynamic_cast<ViewProviderPage*>(activeGui->getViewProvider(page));
614
void QGIView::removeChild(QGIView* child)
616
if (child && (child->parentItem() == this) ) {
617
prepareGeometryChange();
618
scene()->removeItem(child);
622
bool QGIView::getFrameState()
625
TechDraw::DrawView* dv = getViewObject();
626
if (!dv) return true;
628
TechDraw::DrawPage* page = dv->findParentPage();
629
if (!page) return true;
631
Gui::Document* activeGui = Gui::Application::Instance->getDocument(page->getDocument());
632
Gui::ViewProvider* vp = activeGui->getViewProvider(page);
633
ViewProviderPage* vpp = dynamic_cast<ViewProviderPage*>(vp);
634
if (!vpp) return true;
636
return vpp->getFrameState();
639
void QGIView::hideFrame()
645
void QGIView::addArbitraryItem(QGraphicsItem* qgi)
654
void QGIView::switchParentItem(QGIView *targetParent)
656
auto currentParent = dynamic_cast<QGIView *>(this->parentItem());
657
if (currentParent != targetParent) {
659
targetParent->addToGroup(this);
660
targetParent->updateView();
662
currentParent->updateView();
666
while (currentParent) {
667
currentParent->removeFromGroup(this);
668
currentParent->updateView();
669
currentParent = dynamic_cast<QGIView *>(this->parentItem());
675
void QGIView::setStack(int z)
682
void QGIView::setStackFromVP()
684
TechDraw::DrawView* feature = getViewObject();
685
ViewProviderDrawingView* vpdv = static_cast<ViewProviderDrawingView*>
686
(getViewProvider(feature));
687
int z = vpdv->getZ();
691
QColor QGIView::prefNormalColor()
693
return PreferencesGui::getAccessibleQColor(PreferencesGui::normalQColor());
696
QColor QGIView::getPreColor()
698
return PreferencesGui::getAccessibleQColor(PreferencesGui::preselectQColor());
701
QColor QGIView::getSelectColor()
703
return PreferencesGui::getAccessibleQColor(PreferencesGui::selectQColor());
706
Base::Reference<ParameterGrp> QGIView::getParmGroupCol()
708
return App::GetApplication().GetUserParameter()
709
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Colors");
716
int QGIView::calculateFontPixelSize(double sizeInMillimetres)
720
return (int) (Rez::guiX(sizeInMillimetres) + 0.5);
723
int QGIView::calculateFontPixelWidth(const QFont &font)
726
return Gui::QtTools::horizontalAdvance(QFontMetrics(font), QChar::fromLatin1('0'));
729
const double QGIView::DefaultFontSizeInMM = 5.0;
731
void QGIView::dumpRect(const char* text, QRectF rect) {
732
Base::Console().Message("DUMP - %s - rect: (%.3f, %.3f) x (%.3f, %.3f)\n", text,
733
rect.left(), rect.top(), rect.right(), rect.bottom());
738
int QGIView::exactFontSize(std::string fontFamily, double nominalSize)
740
double sceneSize = Rez::guiX(nominalSize);
742
font.setFamily(QString::fromUtf8(fontFamily.c_str()));
743
font.setPixelSize(sceneSize);
745
QFontMetricsF fm(font);
746
double capHeight = fm.capHeight();
747
double ratio = sceneSize / capHeight;
748
return (int) sceneSize * ratio;
751
void QGIView::makeMark(double xPos, double yPos, QColor color)
753
QGIVertex* vItem = new QGIVertex(-1);
754
vItem->setParentItem(this);
755
vItem->setPos(xPos, yPos);
756
vItem->setWidth(2.0);
757
vItem->setRadius(20.0);
758
vItem->setNormalColor(color);
759
vItem->setFillColor(color);
760
vItem->setPrettyNormal();
761
vItem->setZValue(ZVALUE::VERTEX);
764
void QGIView::makeMark(Base::Vector3d pos, QColor color)
766
makeMark(pos.x, pos.y, color);
769
void QGIView::makeMark(QPointF pos, QColor color)
771
makeMark(pos.x(), pos.y(), color);
774
#include <Mod/TechDraw/Gui/moc_QGIView.cpp>