KiCad PCB EDA Suite
two_point_assistant.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) 2020 Kicad Developers, see AUTHORS.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 
28 
29 #include <view/view.h>
30 
31 #include <common.h>
32 #include <pcb_painter.h>
33 
34 using namespace KIGFX::PREVIEW;
35 
37  const TWO_POINT_GEOMETRY_MANAGER& aManager, EDA_UNITS aUnits, GEOM_SHAPE aShape )
38  : EDA_ITEM( NOT_USED ), m_constructMan( aManager ), m_units( aUnits ), m_shape( aShape )
39 {
40 }
41 
42 
44 {
45  BOX2I tmp;
46 
47  // no bounding box when no graphic shown
48  if( m_constructMan.IsReset() )
49  return tmp;
50 
51  // just enclose the whle circular area
52  auto origin = m_constructMan.GetOrigin();
53  auto end = m_constructMan.GetEnd();
54 
56  {
57  tmp.SetOrigin( origin );
58  tmp.SetEnd( end );
59  }
60  else
61  {
62  tmp.SetOrigin( origin + end );
63  tmp.SetEnd( origin - end );
64  }
65 
66  tmp.Normalize();
67  return tmp;
68 }
69 
70 
71 void TWO_POINT_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
72 {
73  auto& gal = *aView->GetGAL();
74 
75  // not in a position to draw anything
76  if( m_constructMan.IsReset() )
77  return;
78 
79  const auto origin = m_constructMan.GetOrigin();
80  const auto end = m_constructMan.GetEnd();
81  const auto radVec = end - origin;
82 
83  if( radVec.x == 0 && radVec.y == 0 )
84  {
85  return; // text next to cursor jumps alot around in this corner case
86  }
87 
88  gal.ResetTextAttributes();
89 
90  // constant text size on screen
91  SetConstantGlyphHeight( gal, 12.0 );
92 
93  std::vector<wxString> cursorStrings;
94 
96  {
97  cursorStrings.push_back( DimensionLabel( "l", radVec.EuclideanNorm(), m_units ) );
98  }
99  else if( m_shape == GEOM_SHAPE::RECT )
100  {
101  cursorStrings.push_back( DimensionLabel( "x", std::abs( radVec.x ), m_units ) );
102  cursorStrings.push_back( DimensionLabel( "y", std::abs( radVec.y ), m_units ) );
103  }
104  else if( m_shape == GEOM_SHAPE::CIRCLE )
105  {
106  KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
107  preview_ctx.DrawLine( origin, end, false );
108 
109  cursorStrings.push_back( DimensionLabel( "r", radVec.EuclideanNorm(), m_units ) );
110  }
111 
112  // place the text next to cursor, on opposite side from drawing
113  DrawTextNextToCursor( aView, end, origin - end, cursorStrings );
114 }
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...
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
the 3d code uses this value
Definition: typeinfo.h:80
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override final
Draw the assistance (with reference to the contstruction manager.
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
const TWO_POINT_GEOMETRY_MANAGER & m_constructMan
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:129
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
A KIGFX::PREVIEW::DRAW_CONTEXT is a wrapper around a GAL and some other settings that makes it easy t...
Definition: draw_context.h:44
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:225
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 SetOrigin(const Vec &pos)
Definition: box2.h:210
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
The common library.
TWO_POINT_ASSISTANT(const TWO_POINT_GEOMETRY_MANAGER &aManager, EDA_UNITS aUnits, GEOM_SHAPE aShape)
VIEW.
Definition: view.h:61