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