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 
24 #include <preview_items/draw_context.h>
25 
26 #include <preview_items/preview_utils.h>
27 
28 #include <view/view.h>
29 
30 using namespace KIGFX::PREVIEW;
31 
32 
33 static constexpr double ANGLE_EPSILON = 1e-9;
34 
angleIsSpecial(double aRadians)35 static bool angleIsSpecial( double aRadians )
36 {
37     return std::fabs( std::remainder( aRadians, M_PI_4 ) ) < ANGLE_EPSILON;
38 }
39 
40 
deemphasise(const COLOR4D & aColor,bool aDeEmphasised)41 static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
42 {
43     return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
44 }
45 
46 
DRAW_CONTEXT(KIGFX::VIEW & aView)47 DRAW_CONTEXT::DRAW_CONTEXT( KIGFX::VIEW& aView )
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 
DrawCircle(const VECTOR2I & aOrigin,double aRad,bool aDeEmphasised)56 void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
57 {
58     const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
59 
60     m_gal.SetLineWidth( m_lineWidth );
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 
DrawLine(const VECTOR2I & aStart,const VECTOR2I & aEnd,bool aDeEmphasised)68 void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
69 {
70     COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
71 
72     m_gal.SetLineWidth( m_lineWidth );
73     m_gal.SetIsStroke( true );
74     m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
75     m_gal.DrawLine( aStart, aEnd );
76 }
77 
78 
DrawLineWithAngleHighlight(const VECTOR2I & aStart,const VECTOR2I & aEnd,bool aDeEmphasised)79 void DRAW_CONTEXT::DrawLineWithAngleHighlight( const VECTOR2I& aStart, const VECTOR2I& aEnd,
80                                                bool aDeEmphasised )
81 {
82     const VECTOR2I vec = aEnd - aStart;
83     COLOR4D        strokeColor = m_render_settings.GetLayerColor( m_currLayer );
84 
85     if( angleIsSpecial( vec.Angle() ) )
86         strokeColor = getSpecialAngleColour();
87 
88     m_gal.SetLineWidth( m_lineWidth );
89     m_gal.SetIsStroke( true );
90     m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
91     m_gal.DrawLine( aStart, aEnd );
92 }
93 
94 
getSpecialAngleColour() const95 COLOR4D DRAW_CONTEXT::getSpecialAngleColour() const
96 {
97     return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
98                                                   COLOR4D( 0.0, 0.7, 0.0, 1.0 );
99 }