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, std::unique_ptr<ZONE> aZone, PCB_LAYER_ID aLayer,
101 wxWindow* aParentWindow, KIGFX::GAL_DISPLAY_OPTIONS& aOptions,
102 GAL_TYPE aGalType ) :
103 PCB_DRAW_PANEL_GAL( aParentWindow, wxID_ANY, wxDefaultPosition, wxDefaultSize, aOptions, aGalType ),
104 m_pcb( aPcb ),
105 m_pcb_bounding_box( std::make_unique<BOARD_EDGES_BOUNDING_ITEM>( aPcb->GetBoardEdgesBoundingBox() ) ),
106 m_zone( std::move( aZone ) ),
107 m_zoomLocked( false )
108{
109 m_view->UseDrawPriority( true );
110 m_painter = std::make_unique<ZONE_PAINTER>( m_gal, FRAME_FOOTPRINT_PREVIEW );
111 m_view->SetPainter( m_painter.get() );
113
114 if( m_zone )
115 m_view->Add( m_zone.get(), DRAW_ORDER_ZONE );
116
117 UpdateColors();
118 m_painter->GetSettings()->SetBackgroundColor( getCanvasBackgroundColor() );
119
120 // Load layer & elements visibility settings
121 for( int i = 0; i < PCB_LAYER_ID_COUNT; ++i )
122 m_view->SetLayerVisible( i, aLayer == i || Edge_Cuts == i );
123
124 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
125
126 Bind( wxEVT_SIZE,
127 [this]( wxSizeEvent& aEvent )
128 {
129 if( !m_zoomLocked )
131
132 aEvent.Skip();
133 } );
134
135 Bind( wxEVT_RIGHT_UP,
136 [this]( wxMouseEvent& aEvent )
137 {
138 wxMenu menu;
139 menu.Append( wxID_ANY, _( "Zoom to Fit" ) );
140
141 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
142 [this]( wxCommandEvent& )
143 {
144 UnlockZoom();
146 } );
147
148 PopupMenu( &menu );
149 } );
150
151 StartDrawing();
153}
154
155
157{
158 BOX2I area = aBoardEdgesOnly ? m_pcb->GetBoardEdgesBoundingBox() : m_pcb->GetBoundingBox();
159
160 if( area.GetWidth() == 0 && area.GetHeight() == 0 )
161 {
162 wxSize pageSize = GetPageSizeIU();
163 area.SetOrigin( 0, 0 );
164 area.SetEnd( pageSize.x, pageSize.y );
165 }
166
167 return area;
168}
169
170
171const BOX2I ZONE_PREVIEW_CANVAS::GetDocumentExtents( bool aIncludeAllVisible ) const
172{
173 if( aIncludeAllVisible || !m_pcb->IsLayerVisible( Edge_Cuts ) )
174 return GetBoardBoundingBox( false );
175 else
176 return GetBoardBoundingBox( true );
177}
178
179
181{
182 const VECTOR2D sizeIU = m_pcb->GetPageSettings().GetSizeIU( pcbIUScale.IU_PER_MILS );
183 return wxSize( KiROUND( sizeIU.x ), KiROUND( sizeIU.y ) );
184}
185
186
188{
189 BOX2I bBox = GetDocumentExtents( false );
190 BOX2I defaultBox = GetDefaultViewBBox();
191
192 m_view->SetScale( 1.0 );
193 const wxSize clientSize = GetSize();
194 VECTOR2D screenSize = m_view->ToWorld( VECTOR2D( (double) clientSize.x, (double) clientSize.y ), false );
195
196 if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
197 bBox = defaultBox;
198
199 VECTOR2D vsize = bBox.GetSize();
200 double scale = m_view->GetScale() / std::max( fabs( vsize.x / screenSize.x ), fabs( vsize.y / screenSize.y ) );
201
202 if( !std::isfinite( scale ) )
203 {
204 m_view->SetCenter( VECTOR2D( 0, 0 ) );
205 Refresh();
206 return;
207 }
208
209 m_view->SetScale( scale );
210 m_view->SetCenter( bBox.Centre() );
212}
213
214
215void ZONE_PREVIEW_CANVAS::LockZoom( double aScale, const VECTOR2D& aCenter )
216{
217 m_zoomLocked = true;
218 m_view->SetScale( aScale );
219 m_view->SetCenter( aCenter );
221}
222
223
225{
226 m_zoomLocked = false;
227}
BASE_SCREEN class implementation.
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
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:411
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:234
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr Vec Centre() const
Definition box2.h:94
constexpr size_type GetHeight() const
Definition box2.h:212
constexpr const SizeVec & GetSize() const
Definition box2.h:203
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:294
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:104
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.
BOX2I GetDefaultViewBBox() const override
Return the bounding box of the view that should be used if model is not valid.
PCB_DRAW_PANEL_GAL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_MODERN)
bool Draw(const KIGFX::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.
const wxSize GetPageSizeIU() const
std::unique_ptr< ZONE > m_zone
ZONE_PREVIEW_CANVAS(BOARD *aPcb, std::unique_ptr< ZONE > aZone, PCB_LAYER_ID aLayer, wxWindow *aParentWindow, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType)
std::unique_ptr< BOARD_EDGES_BOUNDING_ITEM > m_pcb_bounding_box
void LockZoom(double aScale, const VECTOR2D &aCenter)
BOX2I GetBoardBoundingBox(bool aBoardEdgesOnly) const
const BOX2I GetDocumentExtents(bool aIncludeAllVisible=true) const
#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:54
STL namespace.
const int scale
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
@ DRAW_ORDER_BOARD_BOUNDING
@ DRAW_ORDER_ZONE
wxColour getBoundBoundingFillColor()
wxColour getCanvasBackgroundColor()