KiCad PCB EDA Suite
draw_context.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
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 <view/view.h>
29 
30 using namespace KIGFX::PREVIEW;
31 
32 
33 static constexpr double ANGLE_EPSILON = 1e-9;
34 
35 static bool angleIsSpecial( double aRadians )
36 {
37  return std::fabs( std::remainder( aRadians, M_PI_4 ) ) < ANGLE_EPSILON;
38 }
39 
40 
41 static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
42 {
43  return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
44 }
45 
46 
48  : m_gal( *aView.GetGAL() ),
49  m_render_settings( *aView.GetPainter()->GetSettings() ),
50  m_currLayer( LAYER_AUX_ITEMS ),
51  m_lineWidth( 1.0f )
52 {
53 }
54 
55 
56 void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
57 {
59 
61  m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
62  m_gal.SetIsStroke( true );
63  m_gal.SetIsFill( false );
64  m_gal.DrawCircle( aOrigin, aRad );
65 }
66 
67 
69  const VECTOR2I& aOrigin, double aRad, double aStartAngle, double aEndAngle )
70 {
72 
73  if( angleIsSpecial( aStartAngle - aEndAngle ) )
75 
77  m_gal.SetIsStroke( true );
78  m_gal.SetIsFill( true );
80  m_gal.SetFillColor( color.WithAlpha( 0.2 ) );
81 
82  // draw the angle reference arc
83  m_gal.DrawArc( aOrigin, aRad, -aStartAngle, -aEndAngle );
84 }
85 
86 
87 void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
88 {
90 
92  m_gal.SetIsStroke( true );
93  m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
94  m_gal.DrawLine( aStart, aEnd );
95 }
96 
97 
99  const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
100 {
101  const VECTOR2I vec = aEnd - aStart;
103 
104  if( angleIsSpecial( vec.Angle() ) )
105  strokeColor = getSpecialAngleColour();
106 
108  m_gal.SetIsStroke( true );
109  m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
110  m_gal.DrawLine( aStart, aEnd );
111 }
112 
113 
115 {
116  return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
117  COLOR4D( 0.0, 0.7, 0.0, 1.0 );
118 }
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
DRAW_CONTEXT(KIGFX::VIEW &aView)
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle)
Draw an arc.
int color
Definition: DXF_plotter.cpp:62
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: painter.h:219
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:255
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
virtual bool IsBackgroundDark() const
Definition: painter.h:237
virtual void SetLineWidth(float aLineWidth)
Set the line width.
COLOR4D getSpecialAngleColour() const
The GAL to draw into.
void DrawLineWithAngleHighlight(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a straight line on the current layer, with a special highlight when the line angle is a multiple...
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
Auxiliary items (guides, rule, etc)
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
static constexpr double ANGLE_EPSILON
const KIGFX::RENDER_SETTINGS & m_render_settings
The current layer to draw onto.
Definition: draw_context.h:100
void DrawCircle(const VECTOR2I &aOrigin, double aRad, bool aDeEmphasised)
Draw a preview circle on the current layer.
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:306
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
static COLOR4D deemphasise(const COLOR4D &aColor, bool aDeEmphasised)
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
static bool angleIsSpecial(double aRadians)
Class VIEW.
Definition: view.h:61
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
void DrawArcWithAngleHighlight(const VECTOR2I &aOrigin, double aRad, double aStartAngle, double aEndAngle)
Draw an arc on the current layer, with a special highlight when the line angle is a multiple of 45 de...
float m_lineWidth
The line width to use for items.
Definition: draw_context.h:106
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39