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 
28 
30 {
31  return COLOR4D( 1.0, 1.0, 1.0, 1.0 );
32 }
33 
34 
36 {
37  return 0.2;
38 }
39 
40 
42 {
43  return aDeemph ? 0.5 : 1.0;
44 }
45 
46 
48 {
49  return COLOR4D( 0.5, 1.0, 0.5, 1.0 );
50 }
51 
52 
53 static wxString getDimensionUnit( EDA_UNITS_T aUnits )
54 {
55  switch( aUnits )
56  {
57  case INCHES:
58  return _( "\"" );
59 
60  case MILLIMETRES:
61  return _( "mm" );
62 
63  case DEGREES:
64  return _( "deg" );
65 
66  case UNSCALED_UNITS:
67  break;
68  // no default: handle all cases
69  }
70 
71  return wxEmptyString;
72 }
73 
74 
75 static wxString formatPreviewDimension( double aVal, EDA_UNITS_T aUnits )
76 {
77  int precision = 4;
78 
79  // show a sane precision for the preview, which doesn't need to
80  // be accurate down to the nanometre
81  switch( aUnits )
82  {
83  case MILLIMETRES:
84  precision = 2; // 10um
85  break;
86  case INCHES:
87  precision = 4; // 1mil
88  break;
89  case DEGREES:
90  precision = 1; // 0.1deg (limit of formats anyway)
91  break;
92  case UNSCALED_UNITS:
93  break;
94  }
95 
96  const wxString fmtStr = wxString::Format( "%%.%df", precision );
97 
98  wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
99 
100  const wxString symbol = getDimensionUnit( aUnits );
101 
102  if( symbol.size() )
103  str << " " << symbol;
104 
105  return str;
106 }
107 
108 
109 wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix,
110  double aVal, EDA_UNITS_T aUnits )
111 {
112  wxString str;
113 
114  if( prefix.size() )
115  str << prefix << ": ";
116 
117  str << formatPreviewDimension( aVal, aUnits );
118  return str;
119 }
120 
121 
123 {
124  aHeight /= aGal.GetWorldScale();
125 
126  auto glyphSize = aGal.GetGlyphSize();
127  glyphSize = glyphSize * ( aHeight / glyphSize.y );
128  aGal.SetGlyphSize( glyphSize );
129 }
130 
131 
133  const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
134  const std::vector<wxString>& aStrings )
135 {
136  auto glyphSize = aGal.GetGlyphSize();
137 
138  const auto lineSpace = glyphSize.y * 0.2;
139  auto linePitch = glyphSize.y + lineSpace;
140 
141  // radius string goes on the right of the cursor centre line
142  // with a small horizontal offset (enough to keep clear of a
143  // system cursor if present)
144  auto textPos = aCursorPos;
145 
146  // if the text goes above the cursor, shift it up
147  if( aTextQuadrant.y > 0 )
148  {
149  textPos.y -= linePitch * ( aStrings.size() + 1 );
150  }
151 
152  if( aTextQuadrant.x < 0 )
153  {
155  textPos.x += 15.0 / aGal.GetWorldScale();
156  }
157  else
158  {
160  textPos.x -= 15.0 / aGal.GetWorldScale();
161  }
162 
163  aGal.SetStrokeColor( PreviewOverlayDefaultColor().WithAlpha(
164  PreviewOverlayDeemphAlpha( true ) ) );
165  aGal.SetIsFill( false );
166 
167  // write strings top-to-bottom
168  for( const auto& str : aStrings )
169  {
170  textPos.y += linePitch;
171  aGal.BitmapText( str, textPos, 0.0 );
172  }
173 }
virtual void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a text using a bitmap font.
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.
COLOR4D PreviewOverlayDefaultColor()
The default fill/stroke color of preview overlay items.
static wxString formatPreviewDimension(double aVal, EDA_UNITS_T aUnits)
double PreviewOverlayFillAlpha()
The default alpha of overlay fills.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
Definition: common.h:145
const VECTOR2D & GetGlyphSize() const
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
COLOR4D PreviewOverlaySpecialAngleColor()
The colour of "special" angle overlay features.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
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.
void DrawTextNextToCursor(KIGFX::GAL &aGal, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
void SetHorizontalJustify(const EDA_TEXT_HJUSTIFY_T aHorizontalJustify)
Set the horizontal justify for text drawing.
EDA_UNITS_T
Definition: common.h:144
Class GAL is the abstract interface for drawing on a 2D-surface.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39