Add Caption property to Views

This commit is contained in:
WandererFan 2016-10-17 09:10:22 -04:00 committed by Yorik van Havre
parent ce9f4f199a
commit a51b3f7b11
9 changed files with 173 additions and 20 deletions

View File

@ -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");

View File

@ -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()

View File

@ -52,6 +52,8 @@ public:
App::PropertyEnumeration ScaleType;
App::PropertyFloat Rotation;
App::PropertyBool KeepLabel;
App::PropertyString Caption;
/** @name methods overide Feature */
//@{

View File

@ -91,6 +91,8 @@ SET(TechDrawGuiView_SRCS
QGVPage.h
QGCustomText.cpp
QGCustomText.h
QGICaption.cpp
QGICaption.h
QGCustomRect.cpp
QGCustomRect.h
QGCustomSvg.cpp

View File

@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
* *
* 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 <assert.h>
#include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QMouseEvent>
#include <QPaintDevice>
#include <QPainter>
#include <QPrinter>
#include <QSvgGenerator>
#include <QStyleOptionGraphicsItem>
#endif
#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Parameter.h>
#include <qmath.h>
#include <QRectF>
#include "QGICaption.h"
using namespace TechDrawGui;
QGICaption::QGICaption()
{
// setCacheMode(QGraphicsItem::NoCache);
// setAcceptHoverEvents(false);
// setFlag(QGraphicsItem::ItemIsSelectable, false);
// setFlag(QGraphicsItem::ItemIsMovable, false);
}

View File

@ -0,0 +1,44 @@
/***************************************************************************
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
* *
* 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

View File

@ -34,6 +34,7 @@ QGICMark: 171
QGISectionLine: 172
QGIDecoration: 173
QGICenterLine: 174
QGICaption: 180
*/
/*

View File

@ -1,5 +1,3 @@
/***************************************************************************
* Copyright (c) 2012-2013 Luke Parry <l.parry@warwick.ac.uk> *
* *
@ -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<QGraphicsItem*>::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<ParameterGrp> 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());

View File

@ -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<ParameterGrp> 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;
};