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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include "zone_preview_canvas.h"
26#include <wx/gdicmn.h>
27#include <wx/string.h>
28
29#include <base_screen.h>
30#include <base_units.h>
33#include <eda_draw_frame.h>
35#include <id.h>
36#include <kiface_base.h>
37#include <tool/actions.h>
38#include <tool/common_tools.h>
39#include <tool/tool_manager.h>
40#include <view/view.h>
41#include <view/view_controls.h>
42#include <board.h>
43#include <pcb_track.h>
44#include <pcb_marker.h>
45#include <pcb_painter.h>
47#include <kiplatform/ui.h>
48
49
56
57
59{
61 return wxColour( 0, 0, 0, 30 );
62
63 return wxColour( 238, 243, 243 );
64}
65
66
68{
70 return wxColour( 238, 243, 243, 60 );
71
72 return wxColour( 84, 84, 84, 40 );
73}
74
75
77{
78public:
80
81 bool Draw( const KIGFX::VIEW_ITEM* aItem, int aLayer ) override
82 {
83 const BOARD_EDGES_BOUNDING_ITEM* item = dynamic_cast<const BOARD_EDGES_BOUNDING_ITEM*>( aItem );
84
85 if( item )
86 {
87 m_gal->Save();
88 m_gal->SetFillColor( getBoundBoundingFillColor() );
89 m_gal->SetLineWidth( 0 );
90 m_gal->SetIsFill( true );
91 m_gal->SetIsStroke( false );
92 m_gal->DrawRectangle( item->ViewBBox() );
93 m_gal->Restore();
94 return true;
95 }
96
97 return KIGFX::PCB_PAINTER::Draw( aItem, aLayer );
98 }
99};
100
101
102ZONE_PREVIEW_CANVAS::ZONE_PREVIEW_CANVAS( BOARD* aPcb, ZONE* aZone, PCB_LAYER_ID aLayer, wxWindow* aParentWindow,
103 KIGFX::GAL_DISPLAY_OPTIONS& aOptions, wxWindowID aWindowId,
104 const wxPoint& aPosition, const wxSize& aSize, GAL_TYPE aGalType ) :
105 PCB_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, wxDefaultSize, aOptions, aGalType ),
106 m_pcb( aPcb ),
107 m_pcb_bounding_box( std::make_unique<BOARD_EDGES_BOUNDING_ITEM>( aPcb->GetBoardEdgesBoundingBox() ) )
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( aZone )
115 m_view->Add( aZone, 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 StartDrawing();
128}
129
130
132{
133 BOX2I area = aBoardEdgesOnly ? m_pcb->GetBoardEdgesBoundingBox() : m_pcb->GetBoundingBox();
134
135 if( area.GetWidth() == 0 && area.GetHeight() == 0 )
136 {
137 wxSize pageSize = GetPageSizeIU();
138 area.SetOrigin( 0, 0 );
139 area.SetEnd( pageSize.x, pageSize.y );
140 }
141
142 return area;
143}
144
145
146const BOX2I ZONE_PREVIEW_CANVAS::GetDocumentExtents( bool aIncludeAllVisible ) const
147{
148 if( aIncludeAllVisible || !m_pcb->IsLayerVisible( Edge_Cuts ) )
149 return GetBoardBoundingBox( false );
150 else
151 return GetBoardBoundingBox( true );
152}
153
154
156{
157 const VECTOR2D sizeIU = m_pcb->GetPageSettings().GetSizeIU( pcbIUScale.IU_PER_MILS );
158 return wxSize( KiROUND( sizeIU.x ), KiROUND( sizeIU.y ) );
159}
160
161
163{
164 BOX2I bBox = GetDocumentExtents();
165 BOX2I defaultBox = GetDefaultViewBBox();
166
167 m_view->SetScale( 1.0 );
168 const wxSize clientSize = GetSize();
169 VECTOR2D screenSize = m_view->ToWorld( VECTOR2D( (double) clientSize.x, (double) clientSize.y ), false );
170
171 if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
172 bBox = defaultBox;
173
174 VECTOR2D vsize = bBox.GetSize();
175 double scale = m_view->GetScale() / std::max( fabs( vsize.x / screenSize.x ), fabs( vsize.y / screenSize.y ) );
176
177 if( !std::isfinite( scale ) )
178 {
179 m_view->SetCenter( VECTOR2D( 0, 0 ) );
180 Refresh();
181 return;
182 }
183
184 m_view->SetScale( scale );
185 m_view->SetCenter( bBox.Centre() );
187}
BASE_SCREEN class implementation.
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
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:322
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:237
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr Vec Centre() const
Definition box2.h:97
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr const SizeVec & GetSize() const
Definition box2.h:206
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:297
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.
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:102
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:86
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
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:74
@ FRAME_FOOTPRINT_PREVIEW
Definition frame_type.h:48
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ Edge_Cuts
Definition layer_ids.h:112
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:171
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:48
STL namespace.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
const int scale
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
@ DRAW_ORDER_BOARD_BOUNDING
@ DRAW_ORDER_ZONE
wxColour getBoundBoundingFillColor()
wxColour getCanvasBackgroundColor()