KiCad PCB EDA Suite
pcb_base_edit_frame.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) 2014 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <kiface_i.h>
27 #include <pcb_base_edit_frame.h>
28 #include <tool/tool_manager.h>
29 #include <pcbnew_settings.h>
30 #include <pgm_base.h>
31 #include <class_board.h>
32 #include "footprint_info_impl.h"
33 #include <project.h>
36 #include <tools/pcb_actions.h>
40 #include <layer_widget.h>
41 #include <class_dimension.h>
43 
44 
45 PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent,
46  FRAME_T aFrameType, const wxString& aTitle,
47  const wxPoint& aPos, const wxSize& aSize, long aStyle,
48  const wxString& aFrameName ) :
49  PCB_BASE_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
50  m_rotationAngle( 900 ), m_undoRedoBlocked( false ),
51  m_Layers( nullptr ),
52  m_selectionFilterPanel( nullptr ),
53  m_appearancePanel( nullptr )
54 {
55  if( !GFootprintList.GetCount() )
56  {
57  wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" );
58  GFootprintList.ReadCacheFromFile( &footprintInfoCache );
59  }
60 }
61 
62 
64 {
65  GetCanvas()->GetView()->Clear();
66 }
67 
68 
70 {
72 
73  if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( Prj().GetProjectPath() ) )
74  {
75  wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" );
76  GFootprintList.WriteCacheToFile( &footprintInfoCache );
77  }
78 
79  // Close the project if we are standalone, so it gets cleaned up properly
80  if( mgr->IsProjectOpen() && Kiface().IsSingle() )
81  mgr->UnloadProject( &Prj() );
82 }
83 
84 
85 bool PCB_BASE_EDIT_FRAME::TryBefore( wxEvent& aEvent )
86 {
87  static bool s_switcherShown = false;
88 
89  if( !s_switcherShown && wxGetKeyState( WXK_RAW_CONTROL ) && wxGetKeyState( WXK_TAB ) )
90  {
91  if( m_appearancePanel && this->IsActive() )
92  {
93  const wxArrayString& mru = m_appearancePanel->GetLayerPresetsMRU();
94  EDA_VIEW_SWITCHER switcher( this, mru );
95 
96  s_switcherShown = true;
97  switcher.ShowModal();
98  s_switcherShown = false;
99 
100  int idx = switcher.GetSelection();
101 
102  if( idx >= 0 && idx < (int) mru.size() )
103  m_appearancePanel->ApplyLayerPreset( mru[idx] );
104 
105  return true;
106  }
107  }
108 
109  return PCB_BASE_FRAME::TryBefore( aEvent );
110 }
111 
112 
113 void PCB_BASE_EDIT_FRAME::SetRotationAngle( int aRotationAngle )
114 {
115  wxCHECK2_MSG( aRotationAngle > 0 && aRotationAngle <= 900, aRotationAngle = 900,
116  wxT( "Invalid rotation angle, defaulting to 90." ) );
117 
118  m_rotationAngle = aRotationAngle;
119 }
120 
121 
123 {
125 
127 }
128 
129 
131 {
132  bool new_board = ( aBoard != m_Pcb );
133 
134  if( new_board )
135  {
136  if( m_toolManager )
138 
139  GetCanvas()->GetView()->Clear();
140  GetCanvas()->GetView()->InitPreview();
141  }
142 
143  PCB_BASE_FRAME::SetBoard( aBoard );
144 
145  if( new_board )
146  {
147  BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
148  bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
149 
150  try
151  {
152  bds.m_DRCEngine->InitEngine( GetDesignRulesPath() );
153  }
154  catch( PARSE_ERROR& pe )
155  {
156  // TODO: We could redirect to Board Setup here and report the error. Or we could
157  // wait till they run DRC or do an Inspect Clearance. Not sure which is better....
158  }
159  }
160 
162 
163  // update the tool manager with the new board and its view.
164  if( m_toolManager )
165  {
166  GetCanvas()->DisplayBoard( aBoard );
167 
168  GetCanvas()->UpdateColors();
169  m_toolManager->SetEnvironment( aBoard, GetCanvas()->GetView(),
170  GetCanvas()->GetViewControls(), config(), this );
171 
172  if( new_board )
174  }
175 }
176 
177 
179 {
181 
182  if( BOARD* board = GetBoard() )
183  {
184  EDA_UNITS units = GetUserUnits();
185  KIGFX::VIEW* view = GetCanvas()->GetView();
186 
187  INSPECTOR_FUNC inspector =
188  [units, view]( EDA_ITEM* aItem, void* aTestData )
189  {
190  DIMENSION* dimension = static_cast<DIMENSION*>( aItem );
191 
192  if( dimension->GetUnitsMode() == DIM_UNITS_MODE::AUTOMATIC )
193  {
194  dimension->SetUnits( units, false );
195  dimension->Update();
196  view->Update( dimension );
197  }
198 
200  };
201 
202  board->Visit( inspector, nullptr, GENERAL_COLLECTOR::Dimensions );
203  }
204 
206 }
207 
208 
210 {
212 
213  // Update the grid checkbox in the layer widget
214  if( m_appearancePanel )
216 }
217 
218 
220 {
221  return Pgm().GetSettingsManager().GetColorSettings( GetPcbNewSettings()->m_ColorTheme );
222 }
223 
224 
226 {
227  if( !GetBoard() )
228  return wxEmptyString;
229 
230  wxFileName fn = GetBoard()->GetFileName();
231  fn.SetExt( DesignRulesFileExtension );
232  return Prj().AbsolutePath( fn.GetFullName() );
233 }
EDA_UNITS
Definition: common.h:198
void WriteCacheToFile(wxTextFile *aFile) override
virtual void SetBoard(BOARD *aBoard)
Function SetBoard sets the m_Pcb member in such as way as to ensure deleting any previous BOARD.
PCB_BASE_EDIT_FRAME(KIWAY *aKiway, wxWindow *aParent, FRAME_T aFrameType, const wxString &aTitle, const wxPoint &aPos, const wxSize &aSize, long aStyle, const wxString &aFrameName)
virtual void unitsChangeRefresh() override
Called when when the units setting has changed to allow for any derived classes to handle refreshing ...
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
std::function< SEARCH_RESULT(EDA_ITEM *aItem, void *aTestData) > INSPECTOR_FUNC
Typedef INSPECTOR is used to inspect and possibly collect the (search) results of iterating over a li...
Definition: base_struct.h:80
wxPoint m_GridOrigin
origin for grid offsets
void ActivateGalCanvas() override
void ApplyLayerPreset(const wxString &aPresetName)
SETTINGS_MANAGER * GetSettingsManager() const
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
wxString GetDesignRulesPath()
Returns the absolute path to the design rules file for the currently-loaded board.
Model changes (required full reload)
Definition: tool_base.h:82
APPEARANCE_CONTROLS * m_appearancePanel
AUI panel for controlling layer and object visibility and appearance.
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
const std::string DesignRulesFileExtension
void Update()
Updates the dimension's cached text and geometry.
void InitPreview()
Definition: view.cpp:1565
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: class_board.h:514
const wxArrayString & GetLayerPresetsMRU()
PCBNEW_SETTINGS * GetPcbNewSettings()
DIM_UNITS_MODE GetUnitsMode() const
void SetUnits(EDA_UNITS aUnits, bool aUseMils)
void SetGridOrigin(const VECTOR2D &aGridOrigin)
Set the origin point for the grid.
const wxString & GetFileName() const
Definition: class_board.h:242
void unitsChangeRefresh() override
Called when when the units setting has changed to allow for any derived classes to handle refreshing ...
VTBL_ENTRY const wxString AbsolutePath(const wxString &aFileName) const
Function AbsolutePath fixes up aFileName if it is relative to the project's directory to be an absolu...
Definition: project.cpp:272
void doCloseWindow() override
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
bool TryBefore(wxEvent &aEvent) override
DIMENSION class definition.
unsigned GetCount() const
bool IsProjectOpen() const
Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once w...
virtual void ActivateGalCanvas() override
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:117
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
COLOR_SETTINGS * GetColorSettings() override
Helper to retrieve the current color settings.
void DisplayBoard(BOARD *aBoard)
Function DisplayBoard adds all items from the current board to the VIEW, so they can be displayed by ...
void Clear()
Function Clear() Removes all items from the view.
Definition: view.cpp:1107
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition of file extensions used in Kicad.
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Function ResetTools() Resets all tools (i.e.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
void SyncLayersVisibility(const BOARD *aBoard)
Function SyncLayersVisibility Updates "visibility" property of each layer of a given BOARD.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1531
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
void UpdateColors()
Updates the color settings in the painter and GAL.
int m_rotationAngle
User defined rotation angle (in tenths of a degree).
int GetSelection() const
void SetGridVisibility(bool aVisible) override
Function SetGridVisibility()
Struct PARSE_ERROR contains a filename or source description, a problem input line,...
Definition: ki_exception.h:123
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:50
see class PGM_BASE
static const KICAD_T Dimensions[]
A scan list for dimensions.
Definition: collectors.h:323
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:176
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Sets the work environment (model, view, view controls and the parent window).
virtual void SetBoard(BOARD *aBoard) override
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
void ReadCacheFromFile(wxTextFile *aFile) override
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
virtual void ReCreateAuxiliaryToolbar()
Color settings are a bit different than most of the settings objects in that there can be more than o...
BOARD * GetBoard() const
VIEW.
Definition: view.h:61
void SetRotationAngle(int aRotationAngle)
Function SetRotationAngle() Sets the angle used for rotate operations.
virtual void SetGridVisibility(bool aVisible)
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Abstract dimension API.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetObjectVisible(GAL_LAYER_ID aLayer, bool isVisible=true)
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.