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  // this is an edit-time artefact; no reason to try and be smart with the bounding box
52  // (besides, we can't tell the text extents without a view to know what the scale is)
53  tmp.SetMaximum();
54  return tmp;
55 }
56 
57 
58 void TWO_POINT_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
59 {
60  auto& gal = *aView->GetGAL();
61 
62  // not in a position to draw anything
63  if( m_constructMan.IsReset() )
64  return;
65 
66  const auto origin = m_constructMan.GetOrigin();
67  const auto end = m_constructMan.GetEnd();
68  const auto radVec = end - origin;
69 
70  if( radVec.x == 0 && radVec.y == 0 )
71  {
72  return; // text next to cursor jumps alot around in this corner case
73  }
74 
75  gal.ResetTextAttributes();
76 
77  std::vector<wxString> cursorStrings;
78 
80  {
81  cursorStrings.push_back( DimensionLabel( "l", radVec.EuclideanNorm(), m_units ) );
82  }
83  else if( m_shape == GEOM_SHAPE::RECT )
84  {
85  cursorStrings.push_back( DimensionLabel( "x", std::abs( radVec.x ), m_units ) );
86  cursorStrings.push_back( DimensionLabel( "y", std::abs( radVec.y ), m_units ) );
87  }
88  else if( m_shape == GEOM_SHAPE::CIRCLE )
89  {
90  KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
91  preview_ctx.DrawLine( origin, end, false );
92 
93  cursorStrings.push_back( DimensionLabel( "r", radVec.EuclideanNorm(), m_units ) );
94  }
95 
96  // place the text next to cursor, on opposite side from drawing
97  DrawTextNextToCursor( aView, end, origin - end, cursorStrings,
98  aLayer == LAYER_SELECT_OVERLAY );
99 }
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:182
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 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
void SetMaximum()
Definition: box2.h:73
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
currently selected items overlay
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: eda_item.h:148
The common library.
TWO_POINT_ASSISTANT(const TWO_POINT_GEOMETRY_MANAGER &aManager, EDA_UNITS aUnits, GEOM_SHAPE aShape)
VIEW.
Definition: view.h:63
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.