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 
27 #include <view/view.h>
28 
29 
30 using namespace KIGFX::PREVIEW;
31 
33  EDA_UNITS aUnits, GEOM_SHAPE aShape ) :
34  EDA_ITEM( NOT_USED ),
35  m_constructMan( aManager ),
36  m_units( aUnits ),
37  m_shape( aShape )
38 {
39 }
40 
41 
43 {
44  BOX2I tmp;
45 
46  // no bounding box when no graphic shown
47  if( m_constructMan.IsReset() )
48  return tmp;
49 
50  // this is an edit-time artefact; no reason to try and be smart with the bounding box
51  // (besides, we can't tell the text extents without a view to know what the scale is)
52  tmp.SetMaximum();
53  return tmp;
54 }
55 
56 
57 void TWO_POINT_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
58 {
59  KIGFX::GAL& gal = *aView->GetGAL();
60 
61  // not in a position to draw anything
62  if( m_constructMan.IsReset() )
63  return;
64 
65  const VECTOR2I origin = m_constructMan.GetOrigin();
66  const VECTOR2I end = m_constructMan.GetEnd();
67  const VECTOR2I radVec = end - origin;
68 
69  if( radVec.x == 0 && radVec.y == 0 )
70  {
71  return; // text next to cursor jumps around a lot in this corner case
72  }
73 
74  gal.ResetTextAttributes();
75 
76  std::vector<wxString> cursorStrings;
77 
79  {
80  cursorStrings.push_back( DimensionLabel( "l", radVec.EuclideanNorm(), m_units ) );
81  }
82  else if( m_shape == GEOM_SHAPE::RECT )
83  {
84  cursorStrings.push_back( DimensionLabel( "x", std::abs( radVec.x ), m_units ) );
85  cursorStrings.push_back( DimensionLabel( "y", std::abs( radVec.y ), m_units ) );
86  }
87  else if( m_shape == GEOM_SHAPE::CIRCLE )
88  {
89  KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
90  preview_ctx.DrawLine( origin, end, false );
91 
92  cursorStrings.push_back( DimensionLabel( "r", radVec.EuclideanNorm(), m_units ) );
93  }
94 
95  // place the text next to cursor, on opposite side from drawing
96  DrawTextNextToCursor( aView, end, origin - end, cursorStrings, aLayer == LAYER_SELECT_OVERLAY );
97 }
currently selected items overlay
Definition: layer_ids.h:215
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Represent a very simple geometry manager for items that have a start and end point.
the 3d code uses this value
Definition: typeinfo.h:79
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:190
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override final
Draw the assistance (with reference to the construction manager.
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
void ResetTextAttributes()
Reset text attributes to default styling.
const TWO_POINT_GEOMETRY_MANAGER & m_constructMan
void SetMaximum()
Definition: box2.h:57
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
EDA_UNITS
Definition: eda_units.h:38
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
TWO_POINT_ASSISTANT(const TWO_POINT_GEOMETRY_MANAGER &aManager, EDA_UNITS aUnits, GEOM_SHAPE aShape)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.
Abstract interface for drawing on a 2D-surface.