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::VIEW *aView, const VECTOR2D &aCursor, const VECTOR2D &aRulerVec)
 
static TICK_FORMAT getTickFormatForScale (double aScale, double &aTickSpace)
 
void drawTicksAlongLine (KIGFX::VIEW *aView, 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 194 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().

196 {
197  const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
198  const auto backTickVec = aLine.Rotate( M_PI_2 ).Resize( aTickLen );
199 
200  for( int i = 0; i < aNumDivisions + 1; ++i )
201  {
202  const auto backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
203  aGal.DrawLine( backTickPos, backTickPos + backTickVec );
204  }
205 }
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::VIEW aView,
const VECTOR2D aCursor,
const VECTOR2D aRulerVec 
)
static

Definition at line 42 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().

44 {
45  // draw the cursor labels
46  std::vector<wxString> cursorStrings;
47 
48  cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), g_UserUnit ) );
49 
50  double degs = RAD2DECIDEG( -aRulerVec.Angle() );
51  cursorStrings.push_back( DimensionLabel( "θ", degs, DEGREES ) );
52 
53  for( auto& str: cursorStrings )
54  {
55  // FIXME: remove spaces that choke OpenGL lp:1668455
56  str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
57  }
58 
59  auto temp = aRulerVec;
60  DrawTextNextToCursor( aView, aCursor, -temp, cursorStrings );
61 }
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
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
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 drawTicksAlongLine ( KIGFX::VIEW aView,
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 121 of file ruler_item.cpp.

References VECTOR2< T >::Angle(), KIGFX::PREVIEW::DimensionLabel(), VECTOR2< T >::EuclideanNorm(), g_UserUnit, KIGFX::VIEW::GetGAL(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), getTickFormatForScale(), GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, LAYER_AUX_ITEMS, TICK_FORMAT::majorStep, majorTickLengthFactor, TICK_FORMAT::midStep, midTickLengthFactor, KIGFX::PREVIEW::PreviewOverlayDeemphAlpha(), VECTOR2< T >::Resize(), and VECTOR2< T >::Rotate().

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

123 {
124  VECTOR2D tickLine = aLine.Rotate( -M_PI_2 );
125  auto gal = aView->GetGAL();
126  double tickSpace;
127  TICK_FORMAT tickF = getTickFormatForScale( gal->GetWorldScale(), tickSpace );
128  auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
129 
130  // number of ticks in whole ruler
131  int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
132 
133  // work out which way up the tick labels go
134  double labelAngle = -tickLine.Angle();
135 
136  if( aLine.Angle() > 0 )
137  {
138  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
139  }
140  else
141  {
142  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
143  labelAngle += M_PI;
144  }
145 
146  // text and ticks are dimmed
147  gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha( PreviewOverlayDeemphAlpha( true ) ) );
148 
149  const auto labelOffset = tickLine.Resize( aMinorTickLen * ( majorTickLengthFactor + 1 ) );
150 
151  for( int i = 0; i < numTicks; ++i )
152  {
153  const auto tickPos = aOrigin + aLine.Resize( tickSpace * i );
154 
155  double length = aMinorTickLen;
156  bool drawLabel = false;
157 
158  if( i % tickF.majorStep == 0)
159  {
160  drawLabel = true;
161  length *= majorTickLengthFactor;
162  }
163  else if( tickF.midStep && i % tickF.midStep == 0 )
164  {
165  drawLabel = true;
166  length *= midTickLengthFactor;
167  }
168 
169  gal->DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
170 
171  if( drawLabel )
172  {
173  wxString label = DimensionLabel( "", tickSpace * i, g_UserUnit );
174 
175  // FIXME: spaces choke OpenGL lp:1668455
176  label.erase( std::remove( label.begin(), label.end(), ' ' ), label.end() );
177 
178  gal->BitmapText( label, tickPos + labelOffset, labelAngle );
179  }
180  }
181 }
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
int midStep
ticks between major ticks
Definition: ruler_item.cpp:72
static const double majorTickLengthFactor
Definition: ruler_item.cpp:39
Class PCB_RENDER_SETTINGS Stores PCB specific render settings.
Definition: pcb_painter.h:62
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:309
Auxillary items (guides, rule, etc)
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
static const double midTickLengthFactor
Definition: ruler_item.cpp:38
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
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:76
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
Description of a "tick format" for a scale factor - how many ticks there are between medium/major tic...
Definition: ruler_item.cpp:68
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...
int majorStep
multiple from the last scale
Definition: ruler_item.cpp:71
static TICK_FORMAT getTickFormatForScale ( double  aScale,
double &  aTickSpace 
)
static

Definition at line 76 of file ruler_item.cpp.

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

Referenced by drawTicksAlongLine().

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

Variable Documentation

const double majorTickLengthFactor = 2.5
static

Definition at line 39 of file ruler_item.cpp.

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

const double maxTickDensity = 10.0
static

Definition at line 37 of file ruler_item.cpp.

Referenced by getTickFormatForScale().

const double midTickLengthFactor = 1.5
static

Definition at line 38 of file ruler_item.cpp.

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