KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zone_preview_canvas.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) 2023 Ethan Chien <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "zone_preview_canvas.h"
22#include <wx/gdicmn.h>
23#include <wx/intl.h>
24#include <wx/menu.h>
25#include <wx/string.h>
26
27#include <base_screen.h>
28#include <base_units.h>
31#include <eda_draw_frame.h>
33#include <id.h>
34#include <kiface_base.h>
35#include <tool/actions.h>
36#include <tool/common_tools.h>
37#include <tool/tool_manager.h>
38#include <view/view.h>
39#include <view/view_controls.h>
40#include <board.h>
41#include <pcb_track.h>
42#include <pcb_marker.h>
43#include <pcb_painter.h>
45#include <kiplatform/ui.h>
46
47
54
55
57{
59 return wxColour( 0, 0, 0, 30 );
60
61 return wxColour( 238, 243, 243 );
62}
63
64
66{
68 return wxColour( 238, 243, 243, 60 );
69
70 return wxColour( 84, 84, 84, 40 );
71}
72
73
75{
76public:
78
79 bool Draw( const KIGFX::VIEW_ITEM* aItem, int aLayer ) override
80 {
81 const BOARD_EDGES_BOUNDING_ITEM* item = dynamic_cast<const BOARD_EDGES_BOUNDING_ITEM*>( aItem );
82
83 if( item )
84 {
85 m_gal->Save();
86 m_gal->SetFillColor( getBoundBoundingFillColor() );
87 m_gal->SetLineWidth( 0 );
88 m_gal->SetIsFill( true );
89 m_gal->SetIsStroke( false );
90 m_gal->DrawRectangle( item->ViewBBox() );
91 m_gal->Restore();
92 return true;
93 }
94
95 return KIGFX::PCB_PAINTER::Draw( aItem, aLayer );
96 }
97};
98
99
100ZONE_PREVIEW_CANVAS::ZONE_PREVIEW_CANVAS( BOARD* aPcb, ZONE* aZone, PCB_LAYER_ID aLayer, wxWindow* aParentWindow,
101 KIGFX::GAL_DISPLAY_OPTIONS& aOptions, wxWindowID aWindowId,
102 const wxPoint& aPosition, const wxSize& aSize, GAL_TYPE aGalType ) :
103 PCB_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, wxDefaultSize, aOptions, aGalType ),
104 m_pcb( aPcb ),
105 m_pcb_bounding_box( std::make_unique<BOARD_EDGES_BOUNDING_ITEM>( aPcb->GetBoardEdgesBoundingBox() ) ),
106 m_zoomLocked( false )
107{
108 m_view->UseDrawPriority( true );
109 m_painter = std::make_unique<ZONE_PAINTER>( m_gal, FRAME_FOOTPRINT_PREVIEW );
110 m_view->SetPainter( m_painter.get() );
112
113 if( aZone )
114 m_view->Add( aZone, DRAW_ORDER_ZONE );
115
116 UpdateColors();
117 m_painter->GetSettings()->SetBackgroundColor( getCanvasBackgroundColor() );
118
119 // Load layer & elements visibility settings
120 for( int i = 0; i < PCB_LAYER_ID_COUNT; ++i )
121 m_view->SetLayerVisible( i, aLayer == i || Edge_Cuts == i );
122
123 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
124
125 Bind( wxEVT_SIZE,
126 [this]( wxSizeEvent& aEvent )
127 {
128 if( !m_zoomLocked )
130
131 aEvent.Skip();
132 } );
133
134 Bind( wxEVT_RIGHT_UP,
135 [this]( wxMouseEvent& aEvent )
136 {
137 wxMenu menu;
138 menu.Append( wxID_ANY, _( "Zoom to Fit" ) );
139
140 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
141 [this]( wxCommandEvent& )
142 {
143 UnlockZoom();
145 } );
146
147 PopupMenu( &menu );
148 } );
149
150 StartDrawing();
152}
153
154
156{
157 BOX2I area = aBoardEdgesOnly ? m_pcb->GetBoardEdgesBoundingBox() : m_pcb->GetBoundingBox();
158
159 if( area.GetWidth() == 0 && area.GetHeight() == 0 )
160 {
161 wxSize pageSize = GetPageSizeIU();
162 area.SetOrigin( 0, 0 );
163 area.SetEnd( pageSize.x, pageSize.y );
164 }
165
166 return area;
167}
168
169
170const BOX2I ZONE_PREVIEW_CANVAS::GetDocumentExtents( bool aIncludeAllVisible ) const
171{
172 if( aIncludeAllVisible || !m_pcb->IsLayerVisible( Edge_Cuts ) )
173 return GetBoardBoundingBox( false );
174 else
175 return GetBoardBoundingBox( true );
176}
177
178
180{
181 const VECTOR2D sizeIU = m_pcb->GetPageSettings().GetSizeIU( pcbIUScale.IU_PER_MILS );
182 return wxSize( KiROUND( sizeIU.x ), KiROUND( sizeIU.y ) );
183}
184
185
187{
188 BOX2I bBox = GetDocumentExtents( false );
189 BOX2I defaultBox = GetDefaultViewBBox();
190
191 m_view->SetScale( 1.0 );
192 const wxSize clientSize = GetSize();
193 VECTOR2D screenSize = m_view->ToWorld( VECTOR2D( (double) clientSize.x, (double) clientSize.y ), false );
194
195 if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
196 bBox = defaultBox;
197
198 VECTOR2D vsize = bBox.GetSize();
199 double scale = m_view->GetScale() / std::max( fabs( vsize.x / screenSize.x ), fabs( vsize.y / screenSize.y ) );
200
201 if( !std::isfinite( scale ) )
202 {
203 m_view->SetCenter( VECTOR2D( 0, 0 ) );
204 Refresh();
205 return;
206 }
207
208 m_view->SetScale( scale );
209 m_view->SetCenter( bBox.Centre() );
211}
212
213
214void ZONE_PREVIEW_CANVAS::LockZoom( double aScale, const VECTOR2D& aCenter )
215{
216 m_zoomLocked = true;
217 m_view->SetScale( aScale );
218 m_view->SetCenter( aCenter );
220}
221
222
224{
225 m_zoomLocked = false;
226}
BASE_SCREEN class implementation.
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr Vec Centre() const
Definition box2.h:93
constexpr size_type GetHeight() const
Definition box2.h:211
constexpr const SizeVec & GetSize() const
Definition box2.h:202
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:293
std::unique_ptr< KIGFX::PAINTER > m_painter
Contains information about how to draw items using GAL.
KIGFX::GAL * m_gal
Interface for drawing objects on a 2D-surface.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
KIGFX::VIEW * m_view
Stores view settings (scale, center, etc.) and items to be drawn.
void RequestRefresh()
Make sure a refresh gets done on the next idle event if it hasn't already.
void StartDrawing()
Begin drawing if it was stopped previously.
GAL * m_gal
Instance of graphic abstraction layer that gives an interface to call commands used to draw (eg.
Definition painter.h:98
Contains methods for drawing PCB-specific items.
PCB_PAINTER(GAL *aGal, FRAME_T aFrameType)
virtual bool Draw(const VIEW_ITEM *aItem, int aLayer) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
void UpdateColors()
Update the color settings in the painter and GAL.
PCB_DRAW_PANEL_GAL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
BOX2I GetDefaultViewBBox() const override
Return the bounding box of the view that should be used if model is not valid.
bool Draw(const KIGFX::VIEW_ITEM *aItem, int aLayer) override
const wxSize GetPageSizeIU() const
std::unique_ptr< BOARD_EDGES_BOUNDING_ITEM > m_pcb_bounding_box
void LockZoom(double aScale, const VECTOR2D &aCenter)
ZONE_PREVIEW_CANVAS(BOARD *aPcb, ZONE *aZone, PCB_LAYER_ID aLayer, wxWindow *aParentWindow, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, wxWindowID aWindowId=0, const wxPoint &aPosition=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
BOX2I GetBoardBoundingBox(bool aBoardEdgesOnly) const
const BOX2I GetDocumentExtents(bool aIncludeAllVisible=true) const
Handle a list of polygons defining a copper zone.
Definition zone.h:70
#define _(s)
@ FRAME_FOOTPRINT_PREVIEW
Definition frame_type.h:44
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ Edge_Cuts
Definition layer_ids.h:108
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:50
STL namespace.
const int scale
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
@ DRAW_ORDER_BOARD_BOUNDING
@ DRAW_ORDER_ZONE
wxColour getBoundBoundingFillColor()
wxColour getCanvasBackgroundColor()