KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
23#include <view/view.h>
24
25using namespace KIGFX::PREVIEW;
26
27using KIGFX::COLOR4D;
28
29
30static constexpr double ANGLE_EPSILON = 1e-9;
31
32static bool angleIsSpecial( EDA_ANGLE aAngle )
33{
34 return std::fabs( std::remainder( aAngle.AsRadians(), M_PI_4 ) ) < ANGLE_EPSILON;
35}
36
37
38static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
39{
40 return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
41}
42
43
45 : m_gal( *aView.GetGAL() ),
46 m_render_settings( *aView.GetPainter()->GetSettings() ),
48 m_lineWidth( 1.0f )
49{
50}
51
52
53void DRAW_CONTEXT::DrawRectangle( const VECTOR2I& aC1, const VECTOR2I& aC2, bool aDeEmphasised )
54{
55 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
56
57 m_gal.SetLineWidth( m_lineWidth );
58 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
59 m_gal.SetIsStroke( true );
60 m_gal.SetIsFill( false );
61 m_gal.DrawRectangle( aC1, aC2 );
62}
63
64
65void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
66{
67 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
68
69 m_gal.SetLineWidth( m_lineWidth );
70 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
71 m_gal.SetIsStroke( true );
72 m_gal.SetIsFill( false );
73 m_gal.DrawCircle( aOrigin, aRad );
74}
75
76
77void DRAW_CONTEXT::DrawCircleDashed( const VECTOR2I& aOrigin, double aRad, double aStepAngle,
78 double aFillAngle, bool aDeEmphasised )
79{
80 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
81
82 m_gal.SetLineWidth( m_lineWidth );
83 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
84 m_gal.SetIsStroke( true );
85 m_gal.SetIsFill( false );
86
87 for( int i = 0; i < 360; i += aStepAngle )
88 {
89 m_gal.DrawArc( aOrigin, aRad, EDA_ANGLE( i, DEGREES_T ),
90 EDA_ANGLE( i + aFillAngle, DEGREES_T ) );
91 }
92}
93
94
95void DRAW_CONTEXT::DrawEllipse( const ELLIPSE<int>& aEllipse, bool aDeEmphasised )
96{
97 DrawEllipse( aEllipse.Center, aEllipse.MajorRadius, aEllipse.MinorRadius, aEllipse.Rotation, aDeEmphasised );
98}
99
100
101void DRAW_CONTEXT::DrawEllipse( const VECTOR2I& aOrigin, double aA, double aB, EDA_ANGLE aRot, bool aDeEmphasised )
102{
103 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
104
105 m_gal.SetLineWidth( m_lineWidth );
106 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
107 m_gal.SetIsStroke( true );
108 m_gal.SetIsFill( false );
109 m_gal.DrawEllipse( aOrigin, aA, aB, aRot );
110}
111
112
113void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
114{
115 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
116
117 m_gal.SetLineWidth( m_lineWidth );
118 m_gal.SetIsStroke( true );
119 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
120 m_gal.DrawLine( aStart, aEnd );
121}
122
123
124void DRAW_CONTEXT::DrawLineDashed( const VECTOR2I& aStart, const VECTOR2I& aEnd, int aDashStep,
125 int aDashFill, bool aDeEmphasised )
126{
127 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
128
129 m_gal.SetLineWidth( m_lineWidth );
130 m_gal.SetIsStroke( true );
131 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
132
133 VECTOR2I delta = aEnd - aStart;
134 int vecLen = delta.EuclideanNorm();
135
136 for( int i = 0; i < vecLen; i += aDashStep )
137 {
138 VECTOR2I a = aStart + delta.Resize( i );
139 VECTOR2I b = aStart + delta.Resize( std::min( i + aDashFill, vecLen ) );
140
141 m_gal.DrawLine( a, b );
142 }
143}
144
145
147 bool aDeEmphasised )
148{
149 const VECTOR2I vec = aEnd - aStart;
150 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
151
152 if( angleIsSpecial( EDA_ANGLE( vec ) ) )
153 strokeColor = getSpecialAngleColour();
154
155 m_gal.SetLineWidth( m_lineWidth );
156 m_gal.SetIsStroke( true );
157 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
158 m_gal.DrawLine( aStart, aEnd );
159}
160
161
163{
164 return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
165 COLOR4D( 0.0, 0.7, 0.0, 1.0 );
166}
double AsRadians() const
Definition eda_angle.h:120
Plain ellipse / elliptical-arc data.
Definition ellipse.h:32
NumericType MinorRadius
Definition ellipse.h:103
EDA_ANGLE Rotation
Definition ellipse.h:104
NumericType MajorRadius
Definition ellipse.h:102
VECTOR2< NumericType > Center
Definition ellipse.h:101
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
float m_lineWidth
The line width to use for items.
const KIGFX::RENDER_SETTINGS & m_render_settings
The current layer to draw onto.
void DrawCircleDashed(const VECTOR2I &aOrigin, double aRad, double aStepAngle, double aFillAngle, bool aDeEmphasised)
Draw a dashed preview circle on the current layer.
void DrawRectangle(const VECTOR2I &aC1, const VECTOR2I &aC2, bool aDeEmphasised)
Draw a rectangle on the current layer.
void DrawEllipse(const VECTOR2I &aOrigin, double aA, double aB, EDA_ANGLE aRot, bool aDeEmphasised)
Draw a preview ellipse on the current layer.
DRAW_CONTEXT(KIGFX::VIEW &aView)
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
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 DrawLineDashed(const VECTOR2I &aStart, const VECTOR2I &aEn, int aDashStep, int aDashFill, bool aDeEmphasised)
Draw a dashed line on the current layer.
COLOR4D getSpecialAngleColour() const
The GAL to draw into.
void DrawCircle(const VECTOR2I &aOrigin, double aRad, bool aDeEmphasised)
Draw a preview circle on the current layer.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
static COLOR4D deemphasise(const COLOR4D &aColor, bool aDeEmphasised)
static constexpr double ANGLE_EPSILON
static bool angleIsSpecial(EDA_ANGLE aAngle)
@ DEGREES_T
Definition eda_angle.h:31
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc).
Definition layer_ids.h:279
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683