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 void drawCursorStrings (KIGFX::GAL &aGal, const VECTOR2D &aCursor, const VECTOR2D &aRulerVec)
 
static TICK_FORMAT getTickFormatForScale (double aScale, double &aTickSpace)
 
void drawTicksAlongLine (KIGFX::GAL &aGal, const VECTOR2D &aOrigin, const VECTOR2D &aLine, double aMinorTickLen)
 Draw labelled ticks on a line. More...
 
void drawBacksideTicks (KIGFX::GAL &aGal, const VECTOR2D &aOrigin, const VECTOR2D &aLine, double aTickLen, int aNumDivisions)
 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

void drawBacksideTicks ( KIGFX::GAL aGal,
const VECTOR2D aOrigin,
const VECTOR2D aLine,
double  aTickLen,
int  aNumDivisions 
)

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

References KIGFX::GAL::DrawLine(), VECTOR2< T >::EuclideanNorm(), VECTOR2< T >::Resize(), and VECTOR2< T >::Rotate().

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

194 {
195  const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
196  const auto backTickVec = aLine.Rotate( M_PI_2 ).Resize( aTickLen );
197 
198  for( int i = 0; i < aNumDivisions + 1; ++i )
199  {
200  const auto backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
201  aGal.DrawLine( backTickPos, backTickPos + backTickVec );
202  }
203 }
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:388
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:373
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
static void drawCursorStrings ( KIGFX::GAL aGal,
const VECTOR2D aCursor,
const VECTOR2D aRulerVec 
)
static

Definition at line 41 of file ruler_item.cpp.

References VECTOR2< T >::Angle(), DEGREES, KIGFX::PREVIEW::DimensionLabel(), KIGFX::PREVIEW::DrawTextNextToCursor(), VECTOR2< T >::EuclideanNorm(), g_UserUnit, and RAD2DECIDEG().

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

43 {
44  // draw the cursor labels
45  std::vector<wxString> cursorStrings;
46 
47  cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), g_UserUnit ) );
48 
49  double degs = RAD2DECIDEG( -aRulerVec.Angle() );
50  cursorStrings.push_back( DimensionLabel( "θ", degs, DEGREES ) );
51 
52  for( auto& str: cursorStrings )
53  {
54  // FIXME: remove spaces that choke OpenGL lp:1668455
55  str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
56  }
57 
58  auto temp = aRulerVec;
59  DrawTextNextToCursor( aGal, aCursor, -temp, cursorStrings );
60 }
double RAD2DECIDEG(double rad)
Definition: trigo.h:196
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:309
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS_T aUnits)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
void DrawTextNextToCursor(KIGFX::GAL &aGal, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
void drawTicksAlongLine ( KIGFX::GAL aGal,
const VECTOR2D aOrigin,
const VECTOR2D aLine,
double  aMinorTickLen 
)

Draw labelled ticks on a line.

Ticks are spaced according to a maximum density. Miror 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 120 of file ruler_item.cpp.

References VECTOR2< T >::Angle(), KIGFX::GAL::BitmapText(), KIGFX::PREVIEW::DimensionLabel(), KIGFX::GAL::DrawLine(), VECTOR2< T >::EuclideanNorm(), g_UserUnit, getTickFormatForScale(), KIGFX::GAL::GetWorldScale(), GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, TICK_FORMAT::majorStep, majorTickLengthFactor, TICK_FORMAT::midStep, midTickLengthFactor, KIGFX::PREVIEW::PreviewOverlayDeemphAlpha(), KIGFX::PREVIEW::PreviewOverlayDefaultColor(), VECTOR2< T >::Resize(), VECTOR2< T >::Rotate(), KIGFX::GAL::SetHorizontalJustify(), and KIGFX::GAL::SetStrokeColor().

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

122 {
123  VECTOR2D tickLine = aLine.Rotate( -M_PI_2 );
124 
125  double tickSpace;
126  TICK_FORMAT tickF = getTickFormatForScale( aGal.GetWorldScale(), tickSpace );
127 
128  // number of ticks in whole ruler
129  int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
130 
131  // work out which way up the tick labels go
132  double labelAngle = -tickLine.Angle();
133 
134  if( aLine.Angle() > 0 )
135  {
137  }
138  else
139  {
141  labelAngle += M_PI;
142  }
143 
144  // text and ticks are dimmed
146 
147  const auto labelOffset = tickLine.Resize( aMinorTickLen * ( majorTickLengthFactor + 1 ) );
148 
149  for( int i = 0; i < numTicks; ++i )
150  {
151  const auto tickPos = aOrigin + aLine.Resize( tickSpace * i );
152 
153  double length = aMinorTickLen;
154  bool drawLabel = false;
155 
156  if( i % tickF.majorStep == 0)
157  {
158  drawLabel = true;
159  length *= majorTickLengthFactor;
160  }
161  else if( tickF.midStep && i % tickF.midStep == 0 )
162  {
163  drawLabel = true;
164  length *= midTickLengthFactor;
165  }
166 
167  aGal.DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
168 
169  if( drawLabel )
170  {
171  wxString label = DimensionLabel( "", tickSpace * i, g_UserUnit );
172 
173  // FIXME: spaces choke OpenGL lp:1668455
174  label.erase( std::remove( label.begin(), label.end(), ' ' ), label.end() );
175 
176  aGal.BitmapText( label, tickPos + labelOffset, labelAngle );
177  }
178  }
179 }
virtual void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a text using a bitmap font.
double GetWorldScale() const
Get the world scale.
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:388
COLOR4D PreviewOverlayDefaultColor()
The default fill/stroke color of preview overlay items.
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:373
int midStep
ticks between major ticks
Definition: ruler_item.cpp:71
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
static const double majorTickLengthFactor
Definition: ruler_item.cpp:38
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:309
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
static const double midTickLengthFactor
Definition: ruler_item.cpp:37
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
static TICK_FORMAT getTickFormatForScale(double aScale, double &aTickSpace)
Definition: ruler_item.cpp:75
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
Description of a "tick format" for a scale factor - how many ticks there are between medium/major tic...
Definition: ruler_item.cpp:67
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS_T aUnits)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
void SetHorizontalJustify(const EDA_TEXT_HJUSTIFY_T aHorizontalJustify)
Set the horizontal justify for text drawing.
int majorStep
multiple from the last scale
Definition: ruler_item.cpp:70
static TICK_FORMAT getTickFormatForScale ( double  aScale,
double &  aTickSpace 
)
static

Definition at line 75 of file ruler_item.cpp.

References TICK_FORMAT::divisionBase, g_UserUnit, INCHES, and maxTickDensity.

Referenced by drawTicksAlongLine().

76 {
77  // simple 1/2/5 scales per decade
78  static std::vector<TICK_FORMAT> tickFormats =
79  {
80  { 2, 10, 5 }, // |....:....|
81  { 2, 5, 0 }, // |....|
82  { 2.5, 2, 0 }, // |.|.|
83  };
84 
85  // could start at a set number of MM, but that's not available in common
86  aTickSpace = 1;
87 
88  // convert to a round (mod-10) number of mils
89  if( g_UserUnit == INCHES )
90  {
91  aTickSpace *= 2.54;
92  }
93 
94  int tickFormat = 0;
95 
96  while( true )
97  {
98  const auto pixelSpace = aTickSpace * aScale;
99 
100  if( pixelSpace >= maxTickDensity )
101  break;
102 
103  tickFormat = ( tickFormat + 1 ) % tickFormats.size();
104  aTickSpace *= tickFormats[tickFormat].divisionBase;
105  }
106 
107  return tickFormats[tickFormat];
108 }
Definition: common.h:145
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
static const double maxTickDensity
Definition: ruler_item.cpp:36

Variable Documentation

const double majorTickLengthFactor = 2.5
static

Definition at line 38 of file ruler_item.cpp.

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

const double maxTickDensity = 10.0
static

Definition at line 36 of file ruler_item.cpp.

Referenced by getTickFormatForScale().

const double midTickLengthFactor = 1.5
static

Definition at line 37 of file ruler_item.cpp.

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