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) 2019 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #include <base_units.h>
23 #include <painter.h>
24 #include <view/view.h>
25 
27 {
28  return aDeemph ? 0.5 : 1.0;
29 }
30 
31 
32 static wxString formatPreviewDimension( double aVal, EDA_UNITS aUnits )
33 {
34  int precision = 4;
35 
36  // show a sane precision for the preview, which doesn't need to
37  // be accurate down to the nanometre
38  switch( aUnits )
39  {
41  precision = 3; // 1um
42  break;
43 
44  case EDA_UNITS::INCHES:
45  precision = 4; // 0.1mil
46  break;
47 
48  case EDA_UNITS::DEGREES:
49  precision = 1; // 0.1deg
50  break;
51 
52  case EDA_UNITS::PERCENT:
53  precision = 1; // 0.1%
54  break;
55 
57  break;
58  }
59 
60  const wxString fmtStr = wxString::Format( "%%.%df", precision );
61 
62  wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
63 
64  const wxString symbol = GetAbbreviatedUnitsLabel( aUnits, false );
65 
66  if( symbol.size() )
67  str << " " << symbol;
68 
69  return str;
70 }
71 
72 
73 wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix, double aVal, EDA_UNITS aUnits )
74 {
75  wxString str;
76 
77  if( prefix.size() )
78  str << prefix << ": ";
79 
80  str << formatPreviewDimension( aVal, aUnits );
81  return str;
82 }
83 
84 
86 {
87  aHeight /= aGal.GetWorldScale();
88 
89  auto glyphSize = aGal.GetGlyphSize();
90  glyphSize = glyphSize * ( aHeight / glyphSize.y );
91  aGal.SetGlyphSize( glyphSize );
92 }
93 
94 
96  const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
97  const std::vector<wxString>& aStrings )
98 {
99  auto gal = aView->GetGAL();
100  auto glyphSize = gal->GetGlyphSize();
101  auto rs = aView->GetPainter()->GetSettings();
102 
103  const auto lineSpace = glyphSize.y * 0.2;
104  auto linePitch = glyphSize.y + lineSpace;
105 
106  // radius string goes on the right of the cursor centre line
107  // with a small horizontal offset (enough to keep clear of a
108  // system cursor if present)
109  auto textPos = aCursorPos;
110 
111  // if the text goes above the cursor, shift it up
112  if( aTextQuadrant.y > 0 )
113  {
114  textPos.y -= linePitch * ( aStrings.size() + 1 );
115  }
116 
117  if( aTextQuadrant.x < 0 )
118  {
119  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
120  textPos.x += 15.0 / gal->GetWorldScale();
121  }
122  else
123  {
124  gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
125  textPos.x -= 15.0 / gal->GetWorldScale();
126  }
127 
128  gal->SetLineWidth( 1.0f ); // TODO(ISM): Set to the minimum GAL linewidth for HiDPI compatibility
129  gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha(
130  PreviewOverlayDeemphAlpha( true ) ) );
131  gal->SetIsFill( false );
132 
133  // write strings top-to-bottom
134  for( const auto& str : aStrings )
135  {
136  textPos.y += linePitch;
137  gal->BitmapText( str, textPos, 0.0 );
138  }
139 }
double To_User_Unit(EDA_UNITS aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:92
EDA_UNITS
Definition: common.h:198
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS aUnits)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
Implementation of conversion functions that require both schematic and board internal units.
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, bool aUseMils, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:495
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
const VECTOR2D & GetGlyphSize() const
Auxiliary items (guides, rule, etc)
static wxString formatPreviewDimension(double aVal, EDA_UNITS aUnits)
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter 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:201
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.
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
VIEW.
Definition: view.h:61
double GetWorldScale() const
Get the world scale.
Class GAL is the abstract interface for drawing on a 2D-surface.