KiCad PCB EDA Suite
preview_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Kicad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
26 #include <base_units.h>
27 #include <pcb_painter.h>
28 #include <view/view.h>
29 
31 {
32  return aDeemph ? 0.5 : 1.0;
33 }
34 
35 
36 static wxString getDimensionUnit( EDA_UNITS_T aUnits )
37 {
38  switch( aUnits )
39  {
40  case INCHES:
41  return _( "\"" );
42 
43  case MILLIMETRES:
44  return _( "mm" );
45 
46  case DEGREES:
47  return _( "deg" );
48 
49  case UNSCALED_UNITS:
50  break;
51  // no default: handle all cases
52  }
53 
54  return wxEmptyString;
55 }
56 
57 
58 static wxString formatPreviewDimension( double aVal, EDA_UNITS_T aUnits )
59 {
60  int precision = 4;
61 
62  // show a sane precision for the preview, which doesn't need to
63  // be accurate down to the nanometre
64  switch( aUnits )
65  {
66  case MILLIMETRES:
67  precision = 2; // 10um
68  break;
69  case INCHES:
70  precision = 4; // 1mil
71  break;
72  case DEGREES:
73  precision = 1; // 0.1deg (limit of formats anyway)
74  break;
75  case UNSCALED_UNITS:
76  break;
77  }
78 
79  const wxString fmtStr = wxString::Format( "%%.%df", precision );
80 
81  wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
82 
83  const wxString symbol = getDimensionUnit( aUnits );
84 
85  if( symbol.size() )
86  str << " " << symbol;
87 
88  return str;
89 }
90 
91 
92 wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix,
93  double aVal, EDA_UNITS_T aUnits )
94 {
95  wxString str;
96 
97  if( prefix.size() )
98  str << prefix << ": ";
99 
100  str << formatPreviewDimension( aVal, aUnits );
101  return str;
102 }
103 
104 
106 {
107  aHeight /= aGal.GetWorldScale();
108 
109  auto glyphSize = aGal.GetGlyphSize();
110  glyphSize = glyphSize * ( aHeight / glyphSize.y );
111  aGal.SetGlyphSize( glyphSize );
112 }
113 
114 
116  const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
117  const std::vector<wxString>& aStrings )
118 {
119  auto gal = aView->GetGAL();
120  auto glyphSize = gal->GetGlyphSize();
121  auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
122 
123  const auto lineSpace = glyphSize.y * 0.2;
124  auto linePitch = glyphSize.y + lineSpace;
125 
126  // radius string goes on the right of the cursor centre line
127  // with a small horizontal offset (enough to keep clear of a
128  // system cursor if present)
129  auto textPos = aCursorPos;
130 
131  // if the text goes above the cursor, shift it up
132  if( aTextQuadrant.y > 0 )
133  {
134  textPos.y -= linePitch * ( aStrings.size() + 1 );
135  }
136 
137  if( aTextQuadrant.x < 0 )
138  {
139  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
140  textPos.x += 15.0 / gal->GetWorldScale();
141  }
142  else
143  {
144  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
145  textPos.x -= 15.0 / gal->GetWorldScale();
146  }
147 
148  gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha(
149  PreviewOverlayDeemphAlpha( true ) ) );
150  gal->SetIsFill( false );
151 
152  // write strings top-to-bottom
153  for( const auto& str : aStrings )
154  {
155  textPos.y += linePitch;
156  gal->BitmapText( str, textPos, 0.0 );
157  }
158 }
Implementation of conversion functions that require both schematic and board internal units...
static wxString getDimensionUnit(EDA_UNITS_T aUnits)
double GetWorldScale() const
Get the world scale.
static wxString formatPreviewDimension(double aVal, EDA_UNITS_T aUnits)
Class PCB_RENDER_SETTINGS Stores PCB specific render settings.
Definition: pcb_painter.h:62
Auxillary items (guides, rule, etc)
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
Definition: common.h:144
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
const VECTOR2D & GetGlyphSize() const
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.
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void SetConstantGlyphHeight(KIGFX::GAL &aGal, double aHeight)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen...
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...
double To_User_Unit(EDA_UNITS_T aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:90
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
Class VIEW.
Definition: view.h:58
EDA_UNITS_T
Definition: common.h:143
Class GAL is the abstract interface for drawing on a 2D-surface.