KiCad PCB EDA Suite
ruler_item.cpp File Reference

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, EDA_UNITS aUnits, bool aDrawingDropShadows)
 
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, 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 234 of file ruler_item.cpp.

236 {
237  KIGFX::GAL* gal = aView->GetGAL();
238  const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
239  const VECTOR2D backTickVec = aLine.Rotate( M_PI_2 ).Resize( aTickLen );
240  TEXT_DIMS textDims = SetConstantGlyphHeight( gal, -1 );
241 
242  BOX2D viewportD = aView->GetViewport();
243  BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
244 
245  viewport.Inflate( aTickLen * 4 ); // Doesn't have to be accurate, just big enough not to
246  // exclude anything that should be partially drawn
247 
248  for( int i = 0; i < aNumDivisions + 1; ++i )
249  {
250  const VECTOR2D backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
251 
252  if( !viewport.Contains( backTickPos ) )
253  continue;
254 
255  gal->SetLineWidth( getTickLineWidth( textDims, aDrawingDropShadows ) );
256  gal->DrawLine( backTickPos, backTickPos + backTickVec );
257  }
258 }
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:514
TEXT_DIMS SetConstantGlyphHeight(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.
#define M_PI_2
Definition: transline.cpp:37
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
virtual void SetLineWidth(float aLineWidth)
Set the line width.
static double getTickLineWidth(const TEXT_DIMS &textDims, bool aDrawingDropShadows)
Definition: ruler_item.cpp:76
const Vec & GetPosition() const
Definition: box2.h:194
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:404
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
const Vec & GetSize() const
Definition: box2.h:189
Abstract interface for drawing on a 2D-surface.

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

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

◆ drawCursorStrings()

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

Definition at line 55 of file ruler_item.cpp.

58 {
59  // draw the cursor labels
60  std::vector<wxString> cursorStrings;
61 
62  cursorStrings.push_back( DimensionLabel( "x", aRulerVec.x, aUnits ) );
63  cursorStrings.push_back( DimensionLabel( "y", aRulerVec.y, aUnits ) );
64 
65  cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), aUnits ) );
66 
67  double degs = RAD2DECIDEG( -aRulerVec.Angle() );
68  cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), degs,
70 
71  auto temp = aRulerVec;
72  DrawTextNextToCursor( aView, aCursor, -temp, cursorStrings, aDrawingDropShadows );
73 }
double RAD2DECIDEG(double rad)
Definition: trigo.h:236
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
double Angle() const
Compute the angle of the vector.
Definition: vector2d.h:307
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.

References VECTOR2< T >::Angle(), DEGREES, KIGFX::PREVIEW::DimensionLabel(), KIGFX::PREVIEW::DrawTextNextToCursor(), VECTOR2< T >::EuclideanNorm(), RAD2DECIDEG(), 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,
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 142 of file ruler_item.cpp.

144 {
145  KIGFX::GAL* gal = aView->GetGAL();
146  VECTOR2D tickLine = aLine.Rotate( -M_PI_2 );
147  double tickSpace;
148  TICK_FORMAT tickF = getTickFormatForScale( gal->GetWorldScale(), tickSpace, aUnits );
149 
150  // number of ticks in whole ruler
151  int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
152 
153  // work out which way up the tick labels go
154  TEXT_DIMS textDims = SetConstantGlyphHeight( gal, -1 );
155  double textThickness = textDims.StrokeWidth;
156  double labelAngle = -tickLine.Angle();
157  double textOffset = 0;
158 
159  if( aDrawingDropShadows )
160  {
161  textOffset = textDims.ShadowWidth;
162  textThickness += 2 * textDims.ShadowWidth;
163  }
164 
165  double majorTickLen = aMinorTickLen * ( majorTickLengthFactor + 1 );
166  VECTOR2D labelOffset = tickLine.Resize( majorTickLen - textOffset );
167 
168  if( aView->IsMirroredX() )
169  {
170  textOffset = -textOffset;
171  labelOffset = -labelOffset;
172  }
173 
174  if( aLine.Angle() > 0 )
175  {
177  }
178  else
179  {
181  labelAngle += M_PI;
182  }
183 
184  BOX2D viewportD = aView->GetViewport();
185  BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
186 
187  viewport.Inflate( majorTickLen * 2 ); // Doesn't have to be accurate, just big enough not
188  // to exclude anything that should be partially drawn
189 
190  for( int i = 0; i < numTicks; ++i )
191  {
192  const VECTOR2D tickPos = aOrigin + aLine.Resize( tickSpace * i );
193 
194  if( !viewport.Contains( tickPos ) )
195  continue;
196 
197  double length = aMinorTickLen;
198  bool drawLabel = false;
199 
200  if( i % tickF.majorStep == 0 )
201  {
202  drawLabel = true;
203  length *= majorTickLengthFactor;
204  }
205  else if( tickF.midStep && i % tickF.midStep == 0 )
206  {
207  drawLabel = true;
208  length *= midTickLengthFactor;
209  }
210 
211  gal->SetLineWidth( textThickness / 2 );
212  gal->DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
213 
214  if( drawLabel )
215  {
216  wxString label = DimensionLabel( "", tickSpace * i, aUnits, false );
217  gal->SetLineWidth( textThickness );
218  gal->StrokeText( label, tickPos + labelOffset, labelAngle );
219  }
220  }
221 }
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:514
TEXT_DIMS SetConstantGlyphHeight(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.
#define M_PI_2
Definition: transline.cpp:37
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
int midStep
ticks between medium ticks (0 if no medium ticks)
Definition: ruler_item.cpp:95
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
virtual void SetLineWidth(float aLineWidth)
Set the line width.
static const double majorTickLengthFactor
Definition: ruler_item.cpp:36
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a vector type text using preloaded Newstroke font.
static const double midTickLengthFactor
Definition: ruler_item.cpp:35
double Angle() const
Compute the angle of the vector.
Definition: vector2d.h:307
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:237
const Vec & GetPosition() const
Definition: box2.h:194
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:404
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
Description of a "tick format" for a scale factor - how many ticks there are between medium/major tic...
Definition: ruler_item.cpp:91
static TICK_FORMAT getTickFormatForScale(double aScale, double &aTickSpace, EDA_UNITS aUnits)
Definition: ruler_item.cpp:99
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
const Vec & GetSize() const
Definition: box2.h:189
void SetHorizontalJustify(const EDA_TEXT_HJUSTIFY_T aHorizontalJustify)
Set the horizontal justify for text drawing.
int majorStep
ticks between major ticks
Definition: ruler_item.cpp:94
double GetWorldScale() const
Get the world scale.
Abstract interface for drawing on a 2D-surface.

References VECTOR2< T >::Angle(), BOX2< Vec >::Contains(), KIGFX::PREVIEW::DimensionLabel(), KIGFX::GAL::DrawLine(), VECTOR2< T >::EuclideanNorm(), KIGFX::VIEW::GetGAL(), BOX2< Vec >::GetPosition(), BOX2< Vec >::GetSize(), getTickFormatForScale(), KIGFX::VIEW::GetViewport(), KIGFX::GAL::GetWorldScale(), GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, BOX2< Vec >::Inflate(), KIGFX::VIEW::IsMirroredX(), M_PI_2, TICK_FORMAT::majorStep, majorTickLengthFactor, TICK_FORMAT::midStep, midTickLengthFactor, VECTOR2< T >::Resize(), VECTOR2< T >::Rotate(), KIGFX::PREVIEW::SetConstantGlyphHeight(), KIGFX::GAL::SetHorizontalJustify(), KIGFX::GAL::SetLineWidth(), KIGFX::PREVIEW::TEXT_DIMS::ShadowWidth, KIGFX::GAL::StrokeText(), 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() )
49  return LAYER_SELECT_OVERLAY;
50  else
51  return LAYER_GP_OVERLAY;
52 }
virtual bool IsCairoEngine()
Return true if the GAL engine is a Cairo based type.
currently selected items overlay

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 99 of file ruler_item.cpp.

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

77 {
78  double width = textDims.StrokeWidth * 0.8;
79 
80  if( aDrawingDropShadows )
81  width += textDims.ShadowWidth;
82 
83  return width;
84 }

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