From c37df54c0f6297ea3ed704672ac79189e75f166d Mon Sep 17 00:00:00 2001 From: WandererFan Date: Sun, 8 May 2016 10:21:08 -0400 Subject: [PATCH] fix BRect calc for non-custom items --- src/Mod/TechDraw/Gui/CMakeLists.txt | 4 ++ src/Mod/TechDraw/Gui/QGCustomBorder.cpp | 75 ++++++++++++++++++++++++ src/Mod/TechDraw/Gui/QGCustomBorder.h | 59 +++++++++++++++++++ src/Mod/TechDraw/Gui/QGCustomLabel.cpp | 76 +++++++++++++++++++++++++ src/Mod/TechDraw/Gui/QGCustomLabel.h | 59 +++++++++++++++++++ src/Mod/TechDraw/Gui/QGIView.cpp | 29 ++++++---- src/Mod/TechDraw/Gui/QGIView.h | 12 ++-- 7 files changed, 296 insertions(+), 18 deletions(-) create mode 100644 src/Mod/TechDraw/Gui/QGCustomBorder.cpp create mode 100644 src/Mod/TechDraw/Gui/QGCustomBorder.h create mode 100644 src/Mod/TechDraw/Gui/QGCustomLabel.cpp create mode 100644 src/Mod/TechDraw/Gui/QGCustomLabel.h diff --git a/src/Mod/TechDraw/Gui/CMakeLists.txt b/src/Mod/TechDraw/Gui/CMakeLists.txt index d4af8a26f..c738f3afb 100644 --- a/src/Mod/TechDraw/Gui/CMakeLists.txt +++ b/src/Mod/TechDraw/Gui/CMakeLists.txt @@ -94,6 +94,10 @@ SET(TechDrawGuiView_SRCS QGCustomSvg.h QGCustomClip.cpp QGCustomClip.h + QGCustomLabel.cpp + QGCustomLabel.h + QGCustomBorder.cpp + QGCustomBorder.h QGIView.cpp QGIView.h QGIArrow.cpp diff --git a/src/Mod/TechDraw/Gui/QGCustomBorder.cpp b/src/Mod/TechDraw/Gui/QGCustomBorder.cpp new file mode 100644 index 000000000..ddbbd1ade --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGCustomBorder.cpp @@ -0,0 +1,75 @@ +/*************************************************************************** + * Copyright (c) 2016 WandererFan * + * * + * 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 +#include +#include +#include +#include +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include "QGCustomBorder.h" + +using namespace TechDrawGui; + +QGCustomBorder::QGCustomBorder() +{ + setCacheMode(QGraphicsItem::NoCache); +} + +void QGCustomBorder::centerAt(QPointF centerPos) +{ + QRectF box = boundingRect(); + double width = box.width(); + double height = box.height(); + double newX = centerPos.x() - width/2.; + double newY = centerPos.y() - height/2.; + setPos(newX,newY); +} + +void QGCustomBorder::centerAt(double cX, double cY) +{ + QRectF box = boundingRect(); + double width = box.width(); + double height = box.height(); + double newX = cX - width/2.; + double newY = cY - height/2.; + setPos(newX,newY); +} + + +void QGCustomBorder::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { + QStyleOptionGraphicsItem myOption(*option); + myOption.state &= ~QStyle::State_Selected; + + QGraphicsRectItem::paint (painter, &myOption, widget); +} diff --git a/src/Mod/TechDraw/Gui/QGCustomBorder.h b/src/Mod/TechDraw/Gui/QGCustomBorder.h new file mode 100644 index 000000000..4968c5af8 --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGCustomBorder.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (c) 2016 WandererFan * + * * + * 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 * + * * + ***************************************************************************/ + +#ifndef DRAWINGGUI_QGCUSTOMBORDER_H +#define DRAWINGGUI_QGCUSTOMBORDER_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QPainter; +class QStyleOptionGraphicsItem; +QT_END_NAMESPACE + +namespace TechDrawGui +{ + +class TechDrawGuiExport QGCustomBorder : public QGraphicsRectItem +{ +public: + explicit QGCustomBorder(void); + ~QGCustomBorder() {} + + enum {Type = QGraphicsItem::UserType + 136}; + int type() const { return Type;} + + virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); + virtual void centerAt(QPointF centerPos); + virtual void centerAt(double cX, double cY); + +protected: + +private: + +}; + +} // namespace MDIViewPageGui + +#endif // DRAWINGGUI_QGCUSTOMBORDER_H diff --git a/src/Mod/TechDraw/Gui/QGCustomLabel.cpp b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp new file mode 100644 index 000000000..429e4f241 --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (c) 2015 WandererFan * + * * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include "QGCustomLabel.h" + +using namespace TechDrawGui; + +QGCustomLabel::QGCustomLabel() +{ + setCacheMode(QGraphicsItem::NoCache); +} + +void QGCustomLabel::centerAt(QPointF centerPos) +{ + QRectF box = boundingRect(); + double width = box.width(); + double height = box.height(); + double newX = centerPos.x() - width/2.; + double newY = centerPos.y() - height/2.; + setPos(newX,newY); +} + +void QGCustomLabel::centerAt(double cX, double cY) +{ + QRectF box = boundingRect(); + double width = box.width(); + double height = box.height(); + double newX = cX - width/2.; + double newY = cY - height/2.; + setPos(newX,newY); +} + +void QGCustomLabel::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { + QStyleOptionGraphicsItem myOption(*option); + myOption.state &= ~QStyle::State_Selected; + QGraphicsTextItem::paint (painter, &myOption, widget); +} diff --git a/src/Mod/TechDraw/Gui/QGCustomLabel.h b/src/Mod/TechDraw/Gui/QGCustomLabel.h new file mode 100644 index 000000000..e34d4b288 --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGCustomLabel.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (c) 2015 WandererFan * + * * + * 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 * + * * + ***************************************************************************/ + +#ifndef DRAWINGGUI_QGCUSTOMLABEL_H +#define DRAWINGGUI_QGCUSTOMLABEL_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QPainter; +class QStyleOptionGraphicsItem; +QT_END_NAMESPACE + +namespace TechDrawGui +{ + +class TechDrawGuiExport QGCustomLabel : public QGraphicsTextItem +{ +public: + explicit QGCustomLabel(void); + ~QGCustomLabel() {} + + enum {Type = QGraphicsItem::UserType + 135}; + int type() const { return Type;} + + virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); + virtual void centerAt(QPointF centerPos); + virtual void centerAt(double cX, double cY); + +protected: + +private: + +}; + +} // namespace MDIViewPageGui + +#endif // DRAWINGGUI_QGCUSTOMLABEL_H diff --git a/src/Mod/TechDraw/Gui/QGIView.cpp b/src/Mod/TechDraw/Gui/QGIView.cpp index 126174b8d..327b8fc7f 100644 --- a/src/Mod/TechDraw/Gui/QGIView.cpp +++ b/src/Mod/TechDraw/Gui/QGIView.cpp @@ -47,12 +47,15 @@ #include #include -#include "../App/DrawView.h" -#include "../App/DrawViewClip.h" +#include "QGCustomBorder.h" +#include "QGCustomLabel.h" + #include "QGIView.h" #include "QGCustomClip.h" #include "QGIViewClip.h" +#include "../App/DrawViewClip.h" + using namespace TechDrawGui; void _debugRect(char* text, QRectF r); @@ -90,14 +93,15 @@ QGIView::QGIView(const QPoint &pos, QGraphicsScene *scene) if(scene) // TODO: Get rid of the ctor args as in the refactor attempt scene->addItem(this); - m_label = new QGraphicsTextItem(); + m_decorPen.setStyle(Qt::DashLine); + m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen" + + m_label = new QGCustomLabel(); addToGroup(m_label); m_label->setFont(m_font); - m_border = new QGraphicsRectItem(); + m_border = new QGCustomBorder(); addToGroup(m_border); - m_decorPen.setStyle(Qt::DashLine); - m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen" } @@ -354,16 +358,17 @@ void QGIView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q QGraphicsItemGroup::paint(painter, &myOption, widget); } -//This should count everything except Frame,Label,Dimension - custom or not QRectF QGIView::customChildrenBoundingRect() { QList children = childItems(); - int dimItemType = QGraphicsItem::UserType + 106; + int dimItemType = QGraphicsItem::UserType + 106; // TODO: Magic number warning. make include file for custom types? + int borderItemType = QGraphicsItem::UserType + 136; // TODO: Magic number warning + int labelItemType = QGraphicsItem::UserType + 135; // TODO: Magic number warning QRectF result; for (QList::iterator it = children.begin(); it != children.end(); ++it) { - if ((*it)->type() >= QGraphicsItem::UserType) { - if ((*it)->type() != dimItemType) { //Dimensions don't count towards bRect - result = result.united((*it)->boundingRect()); - } + if ( ((*it)->type() != dimItemType) && + ((*it)->type() != borderItemType) && + ((*it)->type() != labelItemType) ) { + result = result.united((*it)->boundingRect()); } } return result; diff --git a/src/Mod/TechDraw/Gui/QGIView.h b/src/Mod/TechDraw/Gui/QGIView.h index 4cf3615a7..b62a85a74 100644 --- a/src/Mod/TechDraw/Gui/QGIView.h +++ b/src/Mod/TechDraw/Gui/QGIView.h @@ -27,17 +27,17 @@ #include #include +#include "../App/DrawView.h" + QT_BEGIN_NAMESPACE class QGraphicsScene; class QGraphicsSceneMouseEvent; QT_END_NAMESPACE -namespace TechDraw { -class DrawView; -} - namespace TechDrawGui { +class QGCustomBorder; +class QGCustomLabel; class TechDrawGuiExport QGIView : public QObject, public QGraphicsItemGroup { @@ -102,8 +102,8 @@ protected: QColor m_colPre; QColor m_colSel; QFont m_font; - QGraphicsTextItem* m_label; - QGraphicsRectItem* m_border; + QGCustomLabel* m_label; + QGCustomBorder* m_border; QPen m_decorPen; };