Skip to content

Commit

Permalink
TechDraw: new center/section draw style
Browse files Browse the repository at this point in the history
using custom QT pen styles, the center and section lines look much
better.
With some math we are also able to control the middle position, thus
the centerlines will always look good, regardless of the size of an
object.

Also getting the section label size from the settings, so the font
size is controllable and not hardcoded.

Bonus: adding blank templates for the common paper sizes (as the
        Drawing WB has them)
  • Loading branch information
reox committed Feb 24, 2018
1 parent e5ef0f5 commit c41363c
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 201 deletions.
6 changes: 6 additions & 0 deletions src/Mod/TechDraw/App/CMakeLists.txt
Expand Up @@ -158,6 +158,12 @@ SET(TechDraw_Templates
Templates/A4_LandscapeTD.svg
Templates/A4_Landscape_ISO7200TD.svg
Templates/A4_Portrait_ISO7200TD.svg
Templates/A0_Landscape_blank.svg
Templates/A1_Landscape_blank.svg
Templates/A2_Landscape_blank.svg
Templates/A3_Landscape_blank.svg
Templates/A4_Landscape_blank.svg
Templates/A4_Portrait_blank.svg
)

SET(TechDraw_PATFile
Expand Down
46 changes: 43 additions & 3 deletions src/Mod/TechDraw/Gui/QGICenterLine.cpp
Expand Up @@ -26,6 +26,8 @@
#include <QStyleOptionGraphicsItem>
#endif

#include <cmath>

#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
Expand Down Expand Up @@ -70,15 +72,15 @@ QColor QGICenterLine::getCenterColor()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Decorations");
App::Color fcColor = App::Color((uint32_t) hGrp->GetUnsigned("CenterColor", 0x08080800));
App::Color fcColor = App::Color((uint32_t) hGrp->GetUnsigned("CenterColor", 0x00000000));
return fcColor.asValue<QColor>();
}

Qt::PenStyle QGICenterLine::getCenterStyle()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->
GetGroup("Preferences")->GetGroup("Mod/TechDraw/Decorations");
Qt::PenStyle centerStyle = static_cast<Qt::PenStyle> (hGrp->GetInt("CenterLine",3));
Qt::PenStyle centerStyle = static_cast<Qt::PenStyle> (hGrp->GetInt("CenterLine", 4));
return centerStyle;
}

Expand All @@ -90,10 +92,48 @@ void QGICenterLine::paint ( QPainter * painter, const QStyleOptionGraphicsItem *
QGIDecoration::paint (painter, &myOption, widget);
}

void QGICenterLine::setIntersection(bool isIntersecting) {
/**
* Set the intersection style for the centerline.
* If isIntersecting is set to true, the middle of the centerline
* will be the middle of a dash - therefore if two lines intersect, they
* will form a cross.
* If isIntersecting is set to false, the middle of the centerline will be a
* dot.
*/
m_isintersection = isIntersecting;
}

void QGICenterLine::setTools()
{
if (m_styleCurrent == Qt::DashDotLine) {
QVector<qreal> dashes;
qreal space = 4; // in unit width
qreal dash = 16;
// dot must be really small when using CapStyle RoundCap but > 0
// for CapStyle FlatCap you would need to set it to 1
qreal dot = 0.000001;

dashes << dot << space << dash << space;
qreal dashlen = dot + 2 * space + dash;
qreal l_len = sqrt(pow(m_start.x() - m_end.x(), 2) + pow(m_start.y() - m_end.y(), 2)) / 2.0;
// convert from pixelunits to width units
l_len = l_len / m_width;
// note that the additional length using RoundCap or SquareCap does not
// count here!
if (m_isintersection) {
m_pen.setDashOffset(dashlen - fmod(l_len, dashlen) + space + dash / 2);
} else {
m_pen.setDashOffset(dashlen - fmod(l_len, dashlen));
}

m_pen.setDashPattern(dashes);
}
else {
m_pen.setStyle(m_styleCurrent);
}
m_pen.setCapStyle(Qt::RoundCap);
m_pen.setWidthF(m_width);
m_pen.setColor(m_colCurrent);
m_pen.setStyle(m_styleCurrent);
m_line->setPen(m_pen);
}
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/Gui/QGICenterLine.h
Expand Up @@ -48,6 +48,8 @@ class TechDrawGuiExport QGICenterLine : public QGIDecoration
void setBounds(double x1,double y1,double x2,double y2);
virtual void draw();

void setIntersection(bool isIntersecting);

protected:
QColor getCenterColor();
Qt::PenStyle getCenterStyle();
Expand All @@ -58,6 +60,7 @@ class TechDrawGuiExport QGICenterLine : public QGIDecoration
QGraphicsPathItem* m_line; //primpath?
QPointF m_start;
QPointF m_end;
bool m_isintersection;
};

}
Expand Down
31 changes: 27 additions & 4 deletions src/Mod/TechDraw/Gui/QGISectionLine.cpp
Expand Up @@ -119,7 +119,7 @@ void QGISectionLine::makeSymbols()
{
QPointF extLineStart,extLineEnd;
QPointF offset(m_arrowDir.x,-m_arrowDir.y);
offset = 2.0 * m_extLen * offset;
offset = 1.5 * m_extLen * offset;
extLineStart = m_start + offset;
extLineEnd = m_end + offset;

Expand Down Expand Up @@ -173,15 +173,15 @@ QColor QGISectionLine::getSectionColor()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Colors");
App::Color fcColor = App::Color((uint32_t) hGrp->GetUnsigned("SectionColor", 0x08080800));
App::Color fcColor = App::Color((uint32_t) hGrp->GetUnsigned("SectionColor", 0x00000000));
return fcColor.asValue<QColor>();
}

Qt::PenStyle QGISectionLine::getSectionStyle()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->
GetGroup("Preferences")->GetGroup("Mod/TechDraw");
Qt::PenStyle sectStyle = static_cast<Qt::PenStyle> (hGrp->GetInt("SectionLine",2));
Qt::PenStyle sectStyle = static_cast<Qt::PenStyle> (hGrp->GetInt("SectionLine", 4));
return sectStyle;
}

Expand All @@ -195,9 +195,32 @@ void QGISectionLine::paint ( QPainter * painter, const QStyleOptionGraphicsItem

void QGISectionLine::setTools()
{
// Use our own style
if (m_styleCurrent == Qt::DashDotLine) {
QVector<qreal> dashes;
// the stroke width is double the one of center lines, but we like to
// have the same spacing. thus these values must be half as large
qreal space = 2; // in unit r_width
qreal dash = 8;
// dot must be really small when using CapStyle RoundCap but > 0
// for CapStyle FlatCap you would need to set it to 1
qreal dot = 0.000001;

dashes << dot << space << dash << space;

// TODO for fancyness: calculate the offset so both arrows start with a
// dash!

m_pen.setDashPattern(dashes);

m_pen.setDashOffset(2);
}
else {
m_pen.setStyle(m_styleCurrent);
}
m_pen.setWidthF(m_width);
m_pen.setColor(m_colCurrent);
m_pen.setStyle(m_styleCurrent);
m_pen.setCapStyle(Qt::RoundCap);
m_brush.setStyle(m_brushCurrent);
m_brush.setColor(m_colCurrent);

Expand Down
21 changes: 16 additions & 5 deletions src/Mod/TechDraw/Gui/QGIViewPart.cpp
Expand Up @@ -654,6 +654,7 @@ void QGIViewPart::drawSectionLine(TechDraw::DrawViewSection* viewSection, bool b
double sectionSpan;
double sectionFudge = Rez::guiX(10.0);
double xVal, yVal;
double fontSize = getPrefFontSize();
if (horiz) {
sectionSpan = m_border->rect().width() + sectionFudge;
xVal = sectionSpan / 2.0;
Expand All @@ -664,8 +665,8 @@ void QGIViewPart::drawSectionLine(TechDraw::DrawViewSection* viewSection, bool b
yVal = sectionSpan / 2.0;
}
sectionLine->setBounds(-xVal,-yVal,xVal,yVal);
sectionLine->setWidth(Rez::guiX(vp->IsoWidth.getValue()));
sectionLine->setFont(m_font,Rez::guiX(6.0));
sectionLine->setWidth(Rez::guiX(vp->LineWidth.getValue()));
sectionLine->setFont(m_font, fontSize);
sectionLine->setZValue(ZVALUE::SECTIONLINE);
sectionLine->setRotation(viewPart->Rotation.getValue());
sectionLine->draw();
Expand All @@ -690,7 +691,7 @@ void QGIViewPart::drawCenterLines(bool b)

QGICenterLine* centerLine;
double sectionSpan;
double sectionFudge = 10.0;
double sectionFudge = Rez::guiX(10.0);
double xVal, yVal;
if (horiz) {
centerLine = new QGICenterLine();
Expand All @@ -699,8 +700,9 @@ void QGIViewPart::drawCenterLines(bool b)
sectionSpan = m_border->rect().width() + sectionFudge;
xVal = sectionSpan / 2.0;
yVal = 0.0;
centerLine->setIntersection(horiz && vert);
centerLine->setBounds(-xVal,-yVal,xVal,yVal);
centerLine->setWidth(Rez::guiX(vp->IsoWidth.getValue()));
centerLine->setWidth(Rez::guiX(vp->HiddenWidth.getValue()));
centerLine->setZValue(ZVALUE::SECTIONLINE);
centerLine->setRotation(viewPart->Rotation.getValue());
centerLine->draw();
Expand All @@ -712,8 +714,9 @@ void QGIViewPart::drawCenterLines(bool b)
sectionSpan = (m_border->rect().height() - m_label->boundingRect().height()) + sectionFudge;
xVal = 0.0;
yVal = sectionSpan / 2.0;
centerLine->setIntersection(horiz && vert);
centerLine->setBounds(-xVal,-yVal,xVal,yVal);
centerLine->setWidth(Rez::guiX(vp->IsoWidth.getValue()));
centerLine->setWidth(Rez::guiX(vp->HiddenWidth.getValue()));
centerLine->setZValue(ZVALUE::SECTIONLINE);
centerLine->setRotation(viewPart->Rotation.getValue());
centerLine->draw();
Expand Down Expand Up @@ -1001,3 +1004,11 @@ bool QGIViewPart::getFaceEdgesPref(void)
result = hGrp->GetBool("DrawFaceEdges", 0l);
return result;
}

double QGIViewPart::getPrefFontSize()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Labels");
double fontSize = hGrp->GetFloat("LabelSize", 5.0);
return Rez::guiX(fontSize);
}
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/QGIViewPart.h
Expand Up @@ -100,6 +100,7 @@ class TechDrawGuiExport QGIViewPart : public QGIView
void removePrimitives(void);
void removeDecorations(void);
bool getFaceEdgesPref(void);
double getPrefFontSize(void);

private:
QList<QGraphicsItem*> deleteItems;
Expand Down

0 comments on commit c41363c

Please sign in to comment.