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 
25 
27 
28 #include <base_units.h>
29 
30 using namespace KIGFX;
31 
32 
34 {
35  return COLOR4D( 1.0, 1.0, 1.0, 1.0 );
36 }
37 
38 
40 {
41  return 0.2;
42 }
43 
44 
46 {
47  return aDeemph ? 0.5 : 1.0;
48 }
49 
50 
52 {
53  return COLOR4D( 0.5, 1.0, 0.5, 1.0 );
54 }
55 
56 
57 static wxString getDimensionUnit( EDA_UNITS_T aUnits )
58 {
59  switch( aUnits )
60  {
61  case INCHES:
62  return _( "\"" );
63 
64  case MILLIMETRES:
65  return _( "mm" );
66 
67  case DEGREES:
68  return _( "°" );
69 
70  case UNSCALED_UNITS:
71  break;
72  // no default: handle all cases
73  }
74 
75  return wxEmptyString;
76 }
77 
78 
79 static wxString formatPreviewDimension( double aVal, EDA_UNITS_T aUnits )
80 {
81  int precision = 4;
82 
83  // show a sane precision for the preview, which doesn't need to
84  // be accurate down to the nanometre
85  switch( aUnits )
86  {
87  case MILLIMETRES:
88  precision = 2; // 10um
89  break;
90  case INCHES:
91  precision = 4; // 1mil
92  break;
93  case DEGREES:
94  precision = 1; // 0.1deg (limit of formats anyway)
95  break;
96  case UNSCALED_UNITS:
97  break;
98  }
99 
100  const wxString fmtStr = wxString::Format( "%%.%df", precision );
101 
102  wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
103 
104  const wxString symbol = getDimensionUnit( aUnits );
105 
106  if( symbol.size() )
107  str << " " << symbol;
108 
109  return str;
110 }
111 
112 
113 wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix,
114  double aVal, EDA_UNITS_T aUnits )
115 {
116  wxString str;
117 
118  if( prefix.size() )
119  str << prefix << ": ";
120 
121  str << formatPreviewDimension( aVal, aUnits );
122  return str;
123 }
124 
125 
127 {
128  aHeight /= aGal.GetWorldScale();
129 
130  auto glyphSize = aGal.GetGlyphSize();
131  glyphSize = glyphSize * ( aHeight / glyphSize.y );
132  aGal.SetGlyphSize( glyphSize );
133 }
134 
135 
137  const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
138  const std::vector<wxString>& aStrings )
139 {
140  auto glyphSize = aGal.GetGlyphSize();
141 
142  const auto lineSpace = glyphSize.y * 0.2;
143  auto linePitch = glyphSize.y + lineSpace;
144 
145  // radius string goes on the right of the cursor centre line
146  // with a small horizontal offset (enough to keep clear of a
147  // system cursor if present)
148  auto textPos = aCursorPos;
149 
150  // if the text goes above the cursor, shift it up
151  if( aTextQuadrant.y > 0 )
152  {
153  textPos.y -= linePitch * ( aStrings.size() + 1 );
154  }
155 
156  if( aTextQuadrant.x < 0 )
157  {
159  textPos.x += 15.0 / aGal.GetWorldScale();
160  }
161  else
162  {
164  textPos.x -= 15.0 / aGal.GetWorldScale();
165  }
166 
167  aGal.SetStrokeColor( PreviewOverlayDefaultColor().WithAlpha(
168  PreviewOverlayDeemphAlpha( true ) ) );
169  aGal.SetIsFill( false );
170 
171  // write strings top-to-bottom
172  for( const auto& str : aStrings )
173  {
174  textPos.y += linePitch;
175  aGal.BitmapText( str, textPos, 0.0 );
176  }
177 }
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
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