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, EDA_UNITS_T aUnits)
 
static TICK_FORMAT getTickFormatForScale (double aScale, double &aTickSpace, EDA_UNITS_T aUnits)
 
void drawTicksAlongLine (KIGFX::VIEW *aView, const VECTOR2D &aOrigin, const VECTOR2D &aLine, double aMinorTickLen, EDA_UNITS_T aUnits)
 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

◆ drawBacksideTicks()

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

189 {
190  const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
191  const auto backTickVec = aLine.Rotate( M_PI_2 ).Resize( aTickLen );
192 
193  for( int i = 0; i < aNumDivisions + 1; ++i )
194  {
195  const auto backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
196  aGal.DrawLine( backTickPos, backTickPos + backTickVec );
197  }
198 }
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:370
size_t i
Definition: json11.cpp:597
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292

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

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

◆ drawCursorStrings()

static void drawCursorStrings ( KIGFX::VIEW aView,
const VECTOR2D aCursor,
const VECTOR2D aRulerVec,
EDA_UNITS_T  aUnits 
)
static

Definition at line 42 of file ruler_item.cpp.

44 {
45  // draw the cursor labels
46  std::vector<wxString> cursorStrings;
47 
48  cursorStrings.push_back( DimensionLabel( "x", aRulerVec.x, aUnits ) );
49  cursorStrings.push_back( DimensionLabel( "y", aRulerVec.y, aUnits ) );
50 
51  cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), aUnits ) );
52 
53  double degs = RAD2DECIDEG( -aRulerVec.Angle() );
54  cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), degs, DEGREES ) );
55 
56  auto temp = aRulerVec;
57  DrawTextNextToCursor( aView, aCursor, -temp, cursorStrings );
58 }
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:306
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...
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292

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_T  aUnits 
)

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

120 {
121  VECTOR2D tickLine = aLine.Rotate( -M_PI_2 );
122  auto gal = aView->GetGAL();
123  double tickSpace;
124  TICK_FORMAT tickF = getTickFormatForScale( gal->GetWorldScale(), tickSpace, aUnits );
125  auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
126 
127  // number of ticks in whole ruler
128  int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
129 
130  // work out which way up the tick labels go
131  double labelAngle = -tickLine.Angle();
132 
133  if( aLine.Angle() > 0 )
134  {
135  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
136  }
137  else
138  {
139  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
140  labelAngle += M_PI;
141  }
142 
143  // text and ticks are dimmed
144  gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha( PreviewOverlayDeemphAlpha( true ) ) );
145 
146  const auto labelOffset = tickLine.Resize( aMinorTickLen * ( majorTickLengthFactor + 1 ) );
147 
148  for( int i = 0; i < numTicks; ++i )
149  {
150  const auto tickPos = aOrigin + aLine.Resize( tickSpace * i );
151 
152  double length = aMinorTickLen;
153  bool drawLabel = false;
154 
155  if( i % tickF.majorStep == 0)
156  {
157  drawLabel = true;
158  length *= majorTickLengthFactor;
159  }
160  else if( tickF.midStep && i % tickF.midStep == 0 )
161  {
162  drawLabel = true;
163  length *= midTickLengthFactor;
164  }
165 
166  gal->DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
167 
168  if( drawLabel )
169  {
170  wxString label = DimensionLabel( "", tickSpace * i, aUnits );
171  gal->BitmapText( label, tickPos + labelOffset, labelAngle );
172  }
173  }
174 }
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
int midStep
ticks between major ticks
Definition: ruler_item.cpp:69
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
static const double majorTickLengthFactor
Definition: ruler_item.cpp:39
Auxiliary items (guides, rule, etc)
static TICK_FORMAT getTickFormatForScale(double aScale, double &aTickSpace, EDA_UNITS_T aUnits)
Definition: ruler_item.cpp:73
static const double midTickLengthFactor
Definition: ruler_item.cpp:38
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:306
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:370
size_t i
Definition: json11.cpp:597
Description of a "tick format" for a scale factor - how many ticks there are between medium/major tic...
Definition: ruler_item.cpp:65
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...
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292
int majorStep
multiple from the last scale
Definition: ruler_item.cpp:68

References VECTOR2< T >::Angle(), KIGFX::PREVIEW::DimensionLabel(), VECTOR2< T >::EuclideanNorm(), KIGFX::VIEW::GetGAL(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), getTickFormatForScale(), GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, i, 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().

◆ getTickFormatForScale()

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

Definition at line 73 of file ruler_item.cpp.

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

References TICK_FORMAT::divisionBase, INCHES, and maxTickDensity.

Referenced by drawTicksAlongLine().

Variable Documentation

◆ majorTickLengthFactor

const double majorTickLengthFactor = 2.5
static

Definition at line 39 of file ruler_item.cpp.

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

◆ maxTickDensity

const double maxTickDensity = 10.0
static

Definition at line 37 of file ruler_item.cpp.

Referenced by getTickFormatForScale().

◆ midTickLengthFactor

const double midTickLengthFactor = 1.5
static

Definition at line 38 of file ruler_item.cpp.

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