KiCad PCB EDA Suite
ruler_item.cpp File Reference
#include <preview_items/ruler_item.h>
#include <preview_items/preview_utils.h>
#include <gal/graphics_abstraction_layer.h>
#include <layer_ids.h>
#include <painter.h>
#include <view/view.h>
#include <trigo.h>

Go to the source code of this file.

Classes

struct  TICK_FORMAT
 Description of a "tick format" for a scale factor - how many ticks there are between medium/major ticks and how each scale relates to the last one. More...
 

Functions

static int getShadowLayer (KIGFX::GAL *aGal)
 
static void drawCursorStrings (KIGFX::VIEW *aView, const VECTOR2D &aCursor, const VECTOR2D &aRulerVec, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, bool aDrawingDropShadows, bool aFlipX, bool aFlipY)
 
static double getTickLineWidth (const TEXT_DIMS &textDims, bool aDrawingDropShadows)
 
static TICK_FORMAT getTickFormatForScale (double aScale, double &aTickSpace, EDA_UNITS aUnits)
 
void drawTicksAlongLine (KIGFX::VIEW *aView, const VECTOR2D &aOrigin, const VECTOR2D &aLine, double aMinorTickLen, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, bool aDrawingDropShadows)
 Draw labelled ticks on a line. More...
 
void drawBacksideTicks (KIGFX::VIEW *aView, const VECTOR2D &aOrigin, const VECTOR2D &aLine, double aTickLen, int aNumDivisions, bool aDrawingDropShadows)
 Draw simple ticks on the back of a line such that the line is divided into n parts. More...
 

Variables

static const double maxTickDensity = 10.0
 
static const double midTickLengthFactor = 1.5
 
static const double majorTickLengthFactor = 2.5
 

Function Documentation

◆ drawBacksideTicks()

void drawBacksideTicks ( KIGFX::VIEW aView,
const VECTOR2D aOrigin,
const VECTOR2D aLine,
double  aTickLen,
int  aNumDivisions,
bool  aDrawingDropShadows 
)

Draw simple ticks on the back of a line such that the line is divided into n parts.

Parameters
aGalthe GAL to draw on
aOriginstart of line to draw ticks on
aLineline vector
aTickLenlength of ticks in IU
aNumDivisionsnumber of parts to divide the line into

Definition at line 240 of file ruler_item.cpp.

242{
243 KIGFX::GAL* gal = aView->GetGAL();
244 TEXT_DIMS textDims = GetConstantGlyphHeight( gal, -1 );
245 const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
246 VECTOR2D backTickVec = aLine;
247
248 RotatePoint( backTickVec, -ANGLE_90 );
249 backTickVec = backTickVec.Resize( aTickLen );
250
251 BOX2D viewportD = aView->GetViewport();
252 BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
253
254 viewport.Inflate( aTickLen * 4 ); // Doesn't have to be accurate, just big enough not to
255 // exclude anything that should be partially drawn
256
257 for( int i = 0; i < aNumDivisions + 1; ++i )
258 {
259 const VECTOR2D backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
260
261 if( !viewport.Contains( backTickPos ) )
262 continue;
263
264 gal->SetLineWidth( getTickLineWidth( textDims, aDrawingDropShadows ) );
265 gal->DrawLine( backTickPos, backTickPos + backTickVec );
266 }
267}
const Vec & GetPosition() const
Definition: box2.h:184
const Vec & GetSize() const
Definition: box2.h:179
Abstract interface for drawing on a 2D-surface.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:508
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:378
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:414
TEXT_DIMS GetConstantGlyphHeight(KIGFX::GAL *aGal, int aRelativeSize=0)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen.
static double getTickLineWidth(const TEXT_DIMS &textDims, bool aDrawingDropShadows)
Definition: ruler_item.cpp:84
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618

References ANGLE_90, BOX2< Vec >::Contains(), KIGFX::GAL::DrawLine(), VECTOR2< T >::EuclideanNorm(), KIGFX::PREVIEW::GetConstantGlyphHeight(), KIGFX::VIEW::GetGAL(), BOX2< Vec >::GetPosition(), BOX2< Vec >::GetSize(), getTickLineWidth(), KIGFX::VIEW::GetViewport(), BOX2< Vec >::Inflate(), VECTOR2< T >::Resize(), RotatePoint(), and KIGFX::GAL::SetLineWidth().

Referenced by KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

◆ drawCursorStrings()

static void drawCursorStrings ( KIGFX::VIEW aView,
const VECTOR2D aCursor,
const VECTOR2D aRulerVec,
const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
bool  aDrawingDropShadows,
bool  aFlipX,
bool  aFlipY 
)
static

Definition at line 55 of file ruler_item.cpp.

58{
59 // draw the cursor labels
60 std::vector<wxString> cursorStrings;
61
62 VECTOR2D temp = aRulerVec;
63
64 if( aFlipX )
65 temp.x = -temp.x;
66
67 if( aFlipY )
68 temp.y = -temp.y;
69
70 cursorStrings.push_back( DimensionLabel( "x", temp.x, aIuScale, aUnits ) );
71 cursorStrings.push_back( DimensionLabel( "y", temp.y, aIuScale, aUnits ) );
72
73 cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), aIuScale, aUnits ) );
74
75 EDA_ANGLE angle = -EDA_ANGLE( aRulerVec );
76 cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), angle.AsDegrees(), aIuScale,
78
79 temp = aRulerVec;
80 DrawTextNextToCursor( aView, aCursor, -temp, cursorStrings, aDrawingDropShadows );
81}
wxString DimensionLabel(const wxString &prefix, double aVal, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), DEGREES, KIGFX::PREVIEW::DimensionLabel(), KIGFX::PREVIEW::DrawTextNextToCursor(), VECTOR2< T >::EuclideanNorm(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

◆ drawTicksAlongLine()

void drawTicksAlongLine ( KIGFX::VIEW aView,
const VECTOR2D aOrigin,
const VECTOR2D aLine,
double  aMinorTickLen,
const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
bool  aDrawingDropShadows 
)

Draw labelled ticks on a line.

Ticks are spaced according to a maximum density. Minor ticks are not labelled.

Parameters
aGalthe GAL to draw on
aOriginstart of line to draw ticks on
aLineline vector
aMinorTickLenlength of minor ticks in IU

Definition at line 150 of file ruler_item.cpp.

152{
153 KIGFX::GAL* gal = aView->GetGAL();
155 double tickSpace;
156 TICK_FORMAT tickFormat = getTickFormatForScale( gal->GetWorldScale(), tickSpace, aUnits );
157 double majorTickLen = aMinorTickLen * ( majorTickLengthFactor + 1 );
158 VECTOR2D tickLine = aLine;
159
160 RotatePoint( tickLine, ANGLE_90 );
161
162 // number of ticks in whole ruler
163 int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
164
165 // work out which way up the tick labels go
166 TEXT_DIMS labelDims = GetConstantGlyphHeight( gal, -1 );
167 EDA_ANGLE labelAngle = - EDA_ANGLE( tickLine );
168 VECTOR2I labelOffset = tickLine.Resize( majorTickLen );
169
170 if( aDrawingDropShadows )
171 labelDims.StrokeWidth += 2 * labelDims.ShadowWidth;
172
173 if( aView->IsMirroredX() )
174 labelOffset = -labelOffset;
175
176 TEXT_ATTRIBUTES labelAttrs;
177 labelAttrs.m_Size = labelDims.GlyphSize;
178 labelAttrs.m_StrokeWidth = labelDims.StrokeWidth;
179
180 if( EDA_ANGLE( aLine ) > ANGLE_0 )
181 {
182 labelAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
183 labelAttrs.m_Angle = labelAngle;
184 }
185 else
186 {
187 labelAttrs.m_Halign = GR_TEXT_H_ALIGN_RIGHT;
188 labelAttrs.m_Angle = labelAngle + ANGLE_180;
189 }
190
191 BOX2D viewportD = aView->GetViewport();
192 BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
193
194 viewport.Inflate( majorTickLen * 2 ); // Doesn't have to be accurate, just big enough not
195 // to exclude anything that should be partially drawn
196
197 for( int i = 0; i < numTicks; ++i )
198 {
199 const VECTOR2D tickPos = aOrigin + aLine.Resize( tickSpace * i );
200
201 if( !viewport.Contains( tickPos ) )
202 continue;
203
204 double length = aMinorTickLen;
205 bool drawLabel = false;
206
207 if( i % tickFormat.majorStep == 0 )
208 {
209 drawLabel = true;
210 length *= majorTickLengthFactor;
211 }
212 else if( tickFormat.midStep && i % tickFormat.midStep == 0 )
213 {
214 drawLabel = true;
215 length *= midTickLengthFactor;
216 }
217
218 gal->SetLineWidth( labelAttrs.m_StrokeWidth / 2 );
219 gal->DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
220
221 if( drawLabel )
222 {
223 wxString label = DimensionLabel( "", tickSpace * i, aIuScale, aUnits, false );
224 font->Draw( gal, label, tickPos + labelOffset, labelAttrs );
225 }
226 }
227}
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
double GetWorldScale() const
Get the world scale.
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:243
GR_TEXT_H_ALIGN_T m_Halign
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:416
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:412
static TICK_FORMAT getTickFormatForScale(double aScale, double &aTickSpace, EDA_UNITS aUnits)
Definition: ruler_item.cpp:107
static const double majorTickLengthFactor
Definition: ruler_item.cpp:36
static const double midTickLengthFactor
Definition: ruler_item.cpp:35
Description of a "tick format" for a scale factor - how many ticks there are between medium/major tic...
Definition: ruler_item.cpp:100
int majorStep
ticks between major ticks
Definition: ruler_item.cpp:102
int midStep
ticks between medium ticks (0 if no medium ticks)
Definition: ruler_item.cpp:103
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT

References ANGLE_0, ANGLE_180, ANGLE_90, BOX2< Vec >::Contains(), KIGFX::PREVIEW::DimensionLabel(), KIFONT::FONT::Draw(), KIGFX::GAL::DrawLine(), VECTOR2< T >::EuclideanNorm(), KIGFX::PREVIEW::GetConstantGlyphHeight(), KIFONT::FONT::GetFont(), KIGFX::VIEW::GetGAL(), BOX2< Vec >::GetPosition(), BOX2< Vec >::GetSize(), getTickFormatForScale(), KIGFX::VIEW::GetViewport(), KIGFX::GAL::GetWorldScale(), KIGFX::PREVIEW::TEXT_DIMS::GlyphSize, GR_TEXT_H_ALIGN_LEFT, GR_TEXT_H_ALIGN_RIGHT, BOX2< Vec >::Inflate(), KIGFX::VIEW::IsMirroredX(), TEXT_ATTRIBUTES::m_Angle, TEXT_ATTRIBUTES::m_Halign, TEXT_ATTRIBUTES::m_Size, TEXT_ATTRIBUTES::m_StrokeWidth, TICK_FORMAT::majorStep, majorTickLengthFactor, TICK_FORMAT::midStep, midTickLengthFactor, VECTOR2< T >::Resize(), RotatePoint(), KIGFX::GAL::SetLineWidth(), KIGFX::PREVIEW::TEXT_DIMS::ShadowWidth, and KIGFX::PREVIEW::TEXT_DIMS::StrokeWidth.

Referenced by KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

◆ getShadowLayer()

static int getShadowLayer ( KIGFX::GAL aGal)
static

Definition at line 46 of file ruler_item.cpp.

47{
48 if( aGal->IsCairoEngine() )
50 else
51 return LAYER_GP_OVERLAY;
52}
virtual bool IsCairoEngine()
Return true if the GAL engine is a Cairo based type.
@ LAYER_GP_OVERLAY
general purpose overlay
Definition: layer_ids.h:218
@ LAYER_SELECT_OVERLAY
currently selected items overlay
Definition: layer_ids.h:219

References KIGFX::GAL::IsCairoEngine(), LAYER_GP_OVERLAY, and LAYER_SELECT_OVERLAY.

Referenced by KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

◆ getTickFormatForScale()

static TICK_FORMAT getTickFormatForScale ( double  aScale,
double &  aTickSpace,
EDA_UNITS  aUnits 
)
static

Definition at line 107 of file ruler_item.cpp.

108{
109 // simple 1/2/5 scales per decade
110 static std::vector<TICK_FORMAT> tickFormats =
111 {
112 { 2, 10, 5 }, // |....:....|
113 { 2, 5, 0 }, // |....|
114 { 2.5, 2, 0 }, // |.|.|
115 };
116
117 // could start at a set number of MM, but that's not available in common
118 aTickSpace = 1;
119
120 // Convert to a round (mod-10) number of mils for imperial units
121 if( EDA_UNIT_UTILS::IsImperialUnit( aUnits ) )
122 aTickSpace *= 2.54;
123
124 int tickFormat = 0;
125
126 while( true )
127 {
128 const auto pixelSpace = aTickSpace * aScale;
129
130 if( pixelSpace >= maxTickDensity )
131 break;
132
133 tickFormat = ( tickFormat + 1 ) % tickFormats.size();
134 aTickSpace *= tickFormats[tickFormat].divisionBase;
135 }
136
137 return tickFormats[tickFormat];
138}
bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:29
static const double maxTickDensity
Definition: ruler_item.cpp:34

References TICK_FORMAT::divisionBase, EDA_UNIT_UTILS::IsImperialUnit(), and maxTickDensity.

Referenced by drawTicksAlongLine().

◆ getTickLineWidth()

static double getTickLineWidth ( const TEXT_DIMS textDims,
bool  aDrawingDropShadows 
)
static

Definition at line 84 of file ruler_item.cpp.

85{
86 double width = textDims.StrokeWidth * 0.8;
87
88 if( aDrawingDropShadows )
89 width += textDims.ShadowWidth;
90
91 return width;
92}

References KIGFX::PREVIEW::TEXT_DIMS::ShadowWidth, and KIGFX::PREVIEW::TEXT_DIMS::StrokeWidth.

Referenced by drawBacksideTicks(), and KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

Variable Documentation

◆ majorTickLengthFactor

const double majorTickLengthFactor = 2.5
static

Definition at line 36 of file ruler_item.cpp.

Referenced by drawTicksAlongLine(), and KIGFX::PREVIEW::RULER_ITEM::ViewDraw().

◆ maxTickDensity

const double maxTickDensity = 10.0
static

Definition at line 34 of file ruler_item.cpp.

Referenced by getTickFormatForScale().

◆ midTickLengthFactor

const double midTickLengthFactor = 1.5
static

Definition at line 35 of file ruler_item.cpp.

Referenced by drawTicksAlongLine(), and KIGFX::PREVIEW::RULER_ITEM::ViewDraw().