From f0bcf8335e9ad9f3cb53b3499832d6315ee2c0b3 Mon Sep 17 00:00:00 2001 From: WandererFan Date: Mon, 17 Oct 2016 09:10:22 -0400 Subject: [PATCH] Add Caption property to Views --- src/Mod/TechDraw/App/DrawProjGroup.cpp | 2 +- src/Mod/TechDraw/App/DrawView.cpp | 7 ++- src/Mod/TechDraw/App/DrawView.h | 2 + src/Mod/TechDraw/Gui/CMakeLists.txt | 2 + src/Mod/TechDraw/Gui/QGICaption.cpp | 53 ++++++++++++++++++ src/Mod/TechDraw/Gui/QGICaption.h | 44 +++++++++++++++ src/Mod/TechDraw/Gui/QGIUserTypes.h | 1 + src/Mod/TechDraw/Gui/QGIView.cpp | 77 ++++++++++++++++++++------ src/Mod/TechDraw/Gui/QGIView.h | 5 ++ 9 files changed, 173 insertions(+), 20 deletions(-) create mode 100644 src/Mod/TechDraw/Gui/QGICaption.cpp create mode 100644 src/Mod/TechDraw/Gui/QGICaption.h diff --git a/src/Mod/TechDraw/App/DrawProjGroup.cpp b/src/Mod/TechDraw/App/DrawProjGroup.cpp index cdc6100b11..fc6eecd2aa 100644 --- a/src/Mod/TechDraw/App/DrawProjGroup.cpp +++ b/src/Mod/TechDraw/App/DrawProjGroup.cpp @@ -52,7 +52,7 @@ PROPERTY_SOURCE(TechDraw::DrawProjGroup, TechDraw::DrawViewCollection) DrawProjGroup::DrawProjGroup(void) { - static const char *group = "Drawing view"; + static const char *group = "ProjGroup"; ADD_PROPERTY_TYPE(Anchor, (0), group, App::Prop_None, "The root view to align projections with"); diff --git a/src/Mod/TechDraw/App/DrawView.cpp b/src/Mod/TechDraw/App/DrawView.cpp index 0bb1184e6b..1c404d10bc 100644 --- a/src/Mod/TechDraw/App/DrawView.cpp +++ b/src/Mod/TechDraw/App/DrawView.cpp @@ -61,7 +61,9 @@ DrawView::DrawView(void) : autoPos(true), mouseMove(false) { - static const char *group = "Drawing view"; + static const char *group = "BaseView"; + static const char *fgroup = "Format"; + ADD_PROPERTY_TYPE(X ,(0),group,App::Prop_None,"X position of the view on the page in modelling units (mm)"); ADD_PROPERTY_TYPE(Y ,(0),group,App::Prop_None,"Y position of the view on the page in modelling units (mm)"); ADD_PROPERTY_TYPE(Rotation ,(0),group,App::Prop_None,"Rotation of the view on the page in degrees counterclockwise"); @@ -70,6 +72,9 @@ DrawView::DrawView(void) ADD_PROPERTY_TYPE(ScaleType,((long)0),group, App::Prop_None, "Scale Type"); ADD_PROPERTY_TYPE(Scale ,(1.0),group,App::Prop_None,"Scale factor of the view"); + ADD_PROPERTY_TYPE(KeepLabel ,(false),fgroup,App::Prop_None,"Keep Label on Page even if toggled off"); + ADD_PROPERTY_TYPE(Caption ,(""),fgroup,App::Prop_None,"Short text about the view"); + } DrawView::~DrawView() diff --git a/src/Mod/TechDraw/App/DrawView.h b/src/Mod/TechDraw/App/DrawView.h index 8b0ffca19e..4a4d7490d8 100644 --- a/src/Mod/TechDraw/App/DrawView.h +++ b/src/Mod/TechDraw/App/DrawView.h @@ -52,6 +52,8 @@ public: App::PropertyEnumeration ScaleType; App::PropertyFloat Rotation; + App::PropertyBool KeepLabel; + App::PropertyString Caption; /** @name methods overide Feature */ //@{ diff --git a/src/Mod/TechDraw/Gui/CMakeLists.txt b/src/Mod/TechDraw/Gui/CMakeLists.txt index 4282c54d71..a89b583d45 100644 --- a/src/Mod/TechDraw/Gui/CMakeLists.txt +++ b/src/Mod/TechDraw/Gui/CMakeLists.txt @@ -91,6 +91,8 @@ SET(TechDrawGuiView_SRCS QGVPage.h QGCustomText.cpp QGCustomText.h + QGICaption.cpp + QGICaption.h QGCustomRect.cpp QGCustomRect.h QGCustomSvg.cpp diff --git a/src/Mod/TechDraw/Gui/QGICaption.cpp b/src/Mod/TechDraw/Gui/QGICaption.cpp new file mode 100644 index 0000000000..6b722e5ef3 --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGICaption.cpp @@ -0,0 +1,53 @@ +/*************************************************************************** + * 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 +#include +#include +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include "QGICaption.h" + +using namespace TechDrawGui; + +QGICaption::QGICaption() +{ +// setCacheMode(QGraphicsItem::NoCache); +// setAcceptHoverEvents(false); +// setFlag(QGraphicsItem::ItemIsSelectable, false); +// setFlag(QGraphicsItem::ItemIsMovable, false); +} diff --git a/src/Mod/TechDraw/Gui/QGICaption.h b/src/Mod/TechDraw/Gui/QGICaption.h new file mode 100644 index 0000000000..902dea6072 --- /dev/null +++ b/src/Mod/TechDraw/Gui/QGICaption.h @@ -0,0 +1,44 @@ +/*************************************************************************** + * 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_QGICAPTION_H +#define DRAWINGGUI_QGICAPTION_H + +#include "QGCustomText.h" + +namespace TechDrawGui +{ + +class TechDrawGuiExport QGICaption : public QGCustomText +{ +public: + explicit QGICaption(void); + ~QGICaption() {} + + enum {Type = QGraphicsItem::UserType + 180}; + int type() const { return Type;} + +}; + +} + +#endif // DRAWINGGUI_QGICAPTION_H diff --git a/src/Mod/TechDraw/Gui/QGIUserTypes.h b/src/Mod/TechDraw/Gui/QGIUserTypes.h index 4f2f67bae0..46333ed887 100644 --- a/src/Mod/TechDraw/Gui/QGIUserTypes.h +++ b/src/Mod/TechDraw/Gui/QGIUserTypes.h @@ -34,6 +34,7 @@ QGICMark: 171 QGISectionLine: 172 QGIDecoration: 173 QGICenterLine: 174 +QGICaption: 180 */ /* diff --git a/src/Mod/TechDraw/Gui/QGIView.cpp b/src/Mod/TechDraw/Gui/QGIView.cpp index 8ac40ab62a..2a53b4c3f5 100644 --- a/src/Mod/TechDraw/Gui/QGIView.cpp +++ b/src/Mod/TechDraw/Gui/QGIView.cpp @@ -1,5 +1,3 @@ - - /*************************************************************************** * Copyright (c) 2012-2013 Luke Parry * * * @@ -52,6 +50,8 @@ #include "QGIView.h" #include "QGCustomBorder.h" #include "QGCustomLabel.h" +#include "QGCustomText.h" +#include "QGICaption.h" #include "QGCustomClip.h" #include "QGIViewClip.h" @@ -59,6 +59,9 @@ using namespace TechDrawGui; +const float labelCaptionFudge = 0.2; // temp fiddle for devel + + QGIView::QGIView() :QGraphicsItemGroup(), locked(false), @@ -78,7 +81,8 @@ QGIView::QGIView() m_pen.setColor(m_colCurrent); //Border/Label styling - m_font.setPointSize(5.0); //scene units (mm), not points + m_font.setPointSize(getPrefFontSize()); //scene units (mm), not points + m_decorPen.setStyle(Qt::DashLine); m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen" @@ -86,6 +90,8 @@ QGIView::QGIView() addToGroup(m_label); m_border = new QGCustomBorder(); addToGroup(m_border); + m_caption = new QGICaption(); + addToGroup(m_caption); isVisible(true); } @@ -190,7 +196,6 @@ void QGIView::hoverEnterEvent(QGraphicsSceneHoverEvent *event) //} } drawBorder(); - //update(); } void QGIView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) @@ -202,7 +207,6 @@ void QGIView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) m_colCurrent = getNormalColor(); } drawBorder(); - //update(); } void QGIView::setPosition(qreal x, qreal y) @@ -243,7 +247,7 @@ void QGIView::updateView(bool update) } if (update || - getViewObject()->Rotation.isTouched()) { + getViewObject()->Rotation.isTouched() ) { //NOTE: QPainterPaths have to be rotated individually. This transform handles Rotation for everything else. //Scale is handled in GeometryObject for DVP & descendents //Objects not descended from DVP must setScale for themselves @@ -286,29 +290,54 @@ void QGIView::toggleCache(bool state) setCacheMode((state)? NoCache : NoCache); } - void QGIView::toggleBorder(bool state) { borderVisible = state; - drawBorder(); + QGIView::draw(); } + void QGIView::draw() { if (isVisible()) { + drawBorder(); show(); } else { hide(); } } +void QGIView::drawCaption() +{ + prepareGeometryChange(); + QRectF displayArea = customChildrenBoundingRect(); + m_caption->setDefaultTextColor(m_colCurrent); + m_font.setFamily(getPrefFont()); + m_caption->setFont(m_font); + QString captionStr = QString::fromUtf8(getViewObject()->Caption.getValue()); + m_caption->setPlainText(captionStr); + QRectF captionArea = m_caption->boundingRect(); + QPointF displayCenter = displayArea.center(); + m_caption->setX(displayCenter.x() - captionArea.width()/2.); + double labelHeight = (1 - labelCaptionFudge) * m_label->boundingRect().height(); + if (borderVisible || viewObj->KeepLabel.getValue()) { //place below label if label visible + m_caption->setY(displayArea.bottom() + labelHeight); + } else { + m_caption->setY(displayArea.bottom() + labelCaptionFudge * getPrefFontSize()); + } + m_caption->show(); +} + void QGIView::drawBorder() { - if (!borderVisible) { + drawCaption(); + //show neither + if (!borderVisible && !viewObj->KeepLabel.getValue()) { m_label->hide(); m_border->hide(); return; } + //show both or show label //double margin = 2.0; m_label->hide(); m_border->hide(); @@ -320,25 +349,23 @@ void QGIView::drawBorder() m_label->setPlainText(labelStr); QRectF labelArea = m_label->boundingRect(); double labelWidth = m_label->boundingRect().width(); - double labelHeight = m_label->boundingRect().height(); + double labelHeight = (1 - labelCaptionFudge) * m_label->boundingRect().height(); - m_border->hide(); m_decorPen.setColor(m_colCurrent); m_border->setPen(m_decorPen); QRectF displayArea = customChildrenBoundingRect(); double displayWidth = displayArea.width(); double displayHeight = displayArea.height(); + QPointF displayCenter = displayArea.center(); + m_label->setX(displayCenter.x() - labelArea.width()/2.); + m_label->setY(displayArea.bottom()); double frameWidth = displayWidth; if (labelWidth > displayWidth) { frameWidth = labelWidth; } double frameHeight = labelHeight + displayHeight; - QPointF displayCenter = displayArea.center(); - - m_label->setX(displayCenter.x() - labelArea.width()/2.); - m_label->setY(displayArea.bottom()); QRectF frameArea = QRectF(displayCenter.x() - frameWidth/2., displayArea.top(), @@ -349,7 +376,9 @@ void QGIView::drawBorder() m_border->setPos(0.,0.); m_label->show(); - m_border->show(); + if (borderVisible) { + m_border->show(); + } } void QGIView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) @@ -365,12 +394,16 @@ QRectF QGIView::customChildrenBoundingRect() { int dimItemType = QGraphicsItem::UserType + 106; // TODO: Magic number warning. int borderItemType = QGraphicsItem::UserType + 136; // TODO: Magic number warning int labelItemType = QGraphicsItem::UserType + 135; // TODO: Magic number warning + int captionItemType = QGraphicsItem::UserType + 180; // TODO: Magic number warning QRectF result; for (QList::iterator it = children.begin(); it != children.end(); ++it) { if ( ((*it)->type() != dimItemType) && ((*it)->type() != borderItemType) && - ((*it)->type() != labelItemType) ) { - result = result.united((*it)->boundingRect()); + ((*it)->type() != labelItemType) && + ((*it)->type() != captionItemType) ) { + QRectF childRect = mapFromItem(*it,(*it)->boundingRect()).boundingRect(); + result = result.united(childRect); + //result = result.united((*it)->boundingRect()); } } return result; @@ -439,6 +472,14 @@ QString QGIView::getPrefFont() return QString::fromStdString(fontName); } +double QGIView::getPrefFontSize() +{ + Base::Reference hGrp = App::GetApplication().GetUserParameter(). + GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw"); + double fontSize = hGrp->GetFloat("LabelSize", 5.0); + return fontSize; +} + void QGIView::dumpRect(char* text, QRectF r) { Base::Console().Message("DUMP - %s - rect: (%.3f,%.3f) x (%.3f,%.3f)\n",text, r.left(),r.top(),r.right(),r.bottom()); diff --git a/src/Mod/TechDraw/Gui/QGIView.h b/src/Mod/TechDraw/Gui/QGIView.h index 98e0b94979..752d4ada16 100644 --- a/src/Mod/TechDraw/Gui/QGIView.h +++ b/src/Mod/TechDraw/Gui/QGIView.h @@ -42,6 +42,8 @@ namespace TechDrawGui { class QGCustomBorder; class QGCustomLabel; +class QGCustomText; +class QGICaption; class TechDrawGuiExport QGIView : public QGraphicsItemGroup { @@ -61,6 +63,7 @@ public: virtual void isVisible(bool state) { m_visibility = state; }; virtual bool isVisible(void) {return m_visibility;}; virtual void draw(void); + virtual void drawCaption(void); /** Methods to ensure that Y-Coordinates are orientated correctly. * @{ */ @@ -100,6 +103,7 @@ protected: QColor getPreColor(void); QColor getSelectColor(void); QString getPrefFont(void); + double getPrefFontSize(void); Base::Reference getParmGroupCol(void); TechDraw::DrawView *viewObj; @@ -120,6 +124,7 @@ protected: QFont m_font; QGCustomLabel* m_label; QGCustomBorder* m_border; + QGICaption* m_caption; QPen m_decorPen; };