From 8afe2dfbce981414417dff4ce13f0bd8d71c5b45 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sun, 2 Feb 2014 20:51:50 +0100 Subject: [PATCH] Eeschema: Fix bug #1271155 relative to Grid selection in Eeschema. --- common/base_screen.cpp | 22 +++++++++++++++------- common/drawframe.cpp | 26 ++++++++++++++++---------- eeschema/eeschema_config.cpp | 3 ++- eeschema/files-io.cpp | 2 +- eeschema/hierarch.cpp | 4 ++++ eeschema/libeditframe.cpp | 7 +------ include/class_base_screen.h | 18 +++++++++++++++--- include/wxstruct.h | 4 +++- 8 files changed, 57 insertions(+), 29 deletions(-) diff --git a/common/base_screen.cpp b/common/base_screen.cpp index cb2cfd0e98..a441a418d5 100644 --- a/common/base_screen.cpp +++ b/common/base_screen.cpp @@ -182,23 +182,27 @@ void BASE_SCREEN::GetGrids( GRIDS& aList ) } -void BASE_SCREEN::SetGrid( const wxRealPoint& size ) +int BASE_SCREEN::SetGrid( const wxRealPoint& size ) { wxASSERT( !m_grids.empty() ); GRID_TYPE nearest_grid = m_grids[0]; + int gridIdx = 0; for( unsigned i = 0; i < m_grids.size(); i++ ) { if( m_grids[i].m_Size == size ) { m_Grid = m_grids[i]; - return; + return m_grids[i].m_Id - ID_POPUP_GRID_LEVEL_1000; } // keep track of the nearest larger grid size, if the exact size is not found if ( size.x < m_grids[i].m_Size.x ) + { + gridIdx = m_grids[i].m_Id - ID_POPUP_GRID_LEVEL_1000; nearest_grid = m_grids[i]; + } } m_Grid = nearest_grid; @@ -206,27 +210,31 @@ void BASE_SCREEN::SetGrid( const wxRealPoint& size ) wxLogWarning( wxT( "Grid size( %f, %f ) not in grid list, falling back " ) \ wxT( "to grid size( %f, %f )." ), size.x, size.y, m_Grid.m_Size.x, m_Grid.m_Size.y ); + + return gridIdx; } -void BASE_SCREEN::SetGrid( int id ) +int BASE_SCREEN::SetGrid( int aCommandId ) { wxASSERT( !m_grids.empty() ); for( unsigned i = 0; i < m_grids.size(); i++ ) { - if( m_grids[i].m_Id == id ) + if( m_grids[i].m_Id == aCommandId ) { m_Grid = m_grids[i]; - return; + return m_grids[i].m_Id - ID_POPUP_GRID_LEVEL_1000; } } m_Grid = m_grids[0]; wxLogWarning( wxT( "Grid ID %d not in grid list, falling back to " ) \ - wxT( "grid size( %g, %g )." ), id, m_Grid.m_Size.x, - m_Grid.m_Size.y ); + wxT( "grid size( %g, %g )." ), aCommandId, + m_Grid.m_Size.x, m_Grid.m_Size.y ); + + return m_grids[0].m_Id - ID_POPUP_GRID_LEVEL_1000; } diff --git a/common/drawframe.cpp b/common/drawframe.cpp index 22db54e619..5820bed89b 100644 --- a/common/drawframe.cpp +++ b/common/drawframe.cpp @@ -60,7 +60,7 @@ static const wxString traceScrollSettings( wxT( "KicadScrollSettings" ) ); static const wxString CursorShapeEntryKeyword( wxT( "CursorShape" ) ); static const wxString ShowGridEntryKeyword( wxT( "ShowGrid" ) ); static const wxString GridColorEntryKeyword( wxT( "GridColor" ) ); -static const wxString LastGridSizeId( wxT( "_LastGridSize" ) ); +static const wxString LastGridSizeIdKeyword( wxT( "_LastGridSize" ) ); BEGIN_EVENT_TABLE( EDA_DRAW_FRAME, EDA_BASE_FRAME ) @@ -334,7 +334,7 @@ void EDA_DRAW_FRAME::PrintPage( wxDC* aDC,LAYER_MSK aPrintMask, bool aPrintMirro void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) { int* clientData; - int id = ID_POPUP_GRID_LEVEL_100; + int eventId = ID_POPUP_GRID_LEVEL_100; if( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED ) { @@ -351,11 +351,11 @@ void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) clientData = (int*) m_gridSelectBox->wxItemContainer::GetClientData( index ); if( clientData != NULL ) - id = *clientData; + eventId = *clientData; } else { - id = event.GetId(); + eventId = event.GetId(); /* Update the grid select combobox if the grid size was changed * by menu event. @@ -366,7 +366,7 @@ void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) { clientData = (int*) m_gridSelectBox->wxItemContainer::GetClientData( i ); - if( clientData && id == *clientData ) + if( clientData && eventId == *clientData ) { m_gridSelectBox->SetSelection( i ); break; @@ -375,9 +375,12 @@ void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) } } + // Be sure m_LastGridSizeId is up to date. + m_LastGridSizeId = eventId - ID_POPUP_GRID_LEVEL_1000; + BASE_SCREEN* screen = GetScreen(); - if( screen->GetGridId() == id ) + if( screen->GetGridId() == eventId ) return; /* @@ -385,8 +388,7 @@ void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) * may be used in the grid size combobox. Do not use the selection * index returned by GetSelection(). */ - m_LastGridSizeId = id - ID_POPUP_GRID_LEVEL_1000; - screen->SetGrid( id ); + screen->SetGrid( eventId ); SetCrossHairPosition( RefPos( true ) ); if( IsGalCanvasActive() ) @@ -598,7 +600,11 @@ void EDA_DRAW_FRAME::LoadSettings() if( cfg->Read( m_FrameName + GridColorEntryKeyword, &itmp ) ) SetGridColor( ColorFromInt( itmp ) ); - cfg->Read( m_FrameName + LastGridSizeId, &m_LastGridSizeId, 0L ); + cfg->Read( m_FrameName + LastGridSizeIdKeyword, &m_LastGridSizeId, 0L ); + + // m_LastGridSizeId is an offset, expected to be >= 0 + if( m_LastGridSizeId < 0 ) + m_LastGridSizeId = 0; } @@ -612,7 +618,7 @@ void EDA_DRAW_FRAME::SaveSettings() cfg->Write( m_FrameName + CursorShapeEntryKeyword, m_cursorShape ); cfg->Write( m_FrameName + ShowGridEntryKeyword, IsGridVisible() ); cfg->Write( m_FrameName + GridColorEntryKeyword, ( long ) GetGridColor() ); - cfg->Write( m_FrameName + LastGridSizeId, ( long ) m_LastGridSizeId ); + cfg->Write( m_FrameName + LastGridSizeIdKeyword, ( long ) m_LastGridSizeId ); } diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp index 65cab5dc82..43e5ec21e3 100644 --- a/eeschema/eeschema_config.cpp +++ b/eeschema/eeschema_config.cpp @@ -302,7 +302,8 @@ void SCH_EDIT_FRAME::OnSetOptions( wxCommandEvent& event ) g_UserUnit = (EDA_UNITS_T)dlg.GetUnitsSelection(); - GetScreen()->SetGrid( grid_list[ (size_t) dlg.GetGridSelection() ].m_Size ); + wxRealPoint gridsize = grid_list[ (size_t) dlg.GetGridSelection() ].m_Size; + m_LastGridSizeId = GetScreen()->SetGrid( gridsize ); int sep, firstId; dlg.GetRefIdSeparator( sep, firstId); diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 4c05edc058..fa6f17b91e 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -329,7 +329,7 @@ bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& aFileName, bool aIsNew ) */ screen->SetZoom( 32 ); - screen->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId ); + m_LastGridSizeId = screen->SetGrid( ID_POPUP_GRID_LEVEL_50 ); TITLE_BLOCK tb; wxString title; diff --git a/eeschema/hierarch.cpp b/eeschema/hierarch.cpp index 6ff3cba4f8..26bb344671 100644 --- a/eeschema/hierarch.cpp +++ b/eeschema/hierarch.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -274,7 +275,10 @@ void SCH_EDIT_FRAME::DisplayCurrentSheet() SCH_SCREEN* screen = m_CurrentSheet->LastScreen(); + // Switch to current sheet, + // and update the grid size, because it can be modified in latest screen SetScreen( screen ); + GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 ); // update the References m_CurrentSheet->UpdateAllScreenReferences(); diff --git a/eeschema/libeditframe.cpp b/eeschema/libeditframe.cpp index e0459b09f5..8b7b5a1794 100644 --- a/eeschema/libeditframe.cpp +++ b/eeschema/libeditframe.cpp @@ -207,23 +207,18 @@ LIB_EDIT_FRAME::LIB_EDIT_FRAME( SCH_EDIT_FRAME* aParent, m_tempCopyComponent = NULL; m_HotkeysZoomAndGridList = s_Libedit_Hokeys_Descr; m_editPinsPerPartOrConvert = false; + // Initialize grid id to the default value 50 mils: m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000; wxIcon icon; - icon.CopyFromBitmap( KiBitmap( libedit_icon_xpm ) ); - SetIcon( icon ); SetScreen( new SCH_SCREEN() ); - GetScreen()->m_Center = true; SetCrossHairPosition( wxPoint( 0, 0 ) ); - // Initialize grid id to the default value 50 mils: - m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000; - LoadSettings(); SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y ); diff --git a/include/class_base_screen.h b/include/class_base_screen.h index 6d133982a9..eed5987e39 100644 --- a/include/class_base_screen.h +++ b/include/class_base_screen.h @@ -402,13 +402,25 @@ public: */ const GRID_TYPE& GetGrid() const { return m_Grid; } - void SetGrid( const wxRealPoint& size ); + /** + * set the current grid size m_Grid. + * The size must be existing in grid list (in m_grids) + * If not, the near existing grid size is used + * @param size = the size of the new grid + * @return the grid id offset (id from ID_POPUP_GRID_LEVEL_1000 ) + * of the currently selected grid. + */ + int SetGrid( const wxRealPoint& size ); /** * Function SetGrid - * sets the grid size from command ID. + * sets the grid size from command ID (not an index in grid list, but a wxID). + * @param aCommandId = the wxWidgets command ID + * @return the grid id offset (id from ID_POPUP_GRID_LEVEL_1000 ) + * of the currently selected grid. */ - void SetGrid( int id ); + int SetGrid( int aCommandId ); + void SetGridList( GRIDS& sizelist ); void AddGrid( const GRID_TYPE& grid ); void AddGrid( const wxRealPoint& size, int id ); diff --git a/include/wxstruct.h b/include/wxstruct.h index 448cd4b344..96476b363c 100644 --- a/include/wxstruct.h +++ b/include/wxstruct.h @@ -414,7 +414,9 @@ class EDA_DRAW_FRAME : public EDA_BASE_FRAME protected: EDA_HOTKEY_CONFIG* m_HotkeysZoomAndGridList; - int m_LastGridSizeId; + int m_LastGridSizeId; // the command id offset (>= 0) of the last selected grid + // 0 is for the grid corresponding to + // a wxCommand ID = ID_POPUP_GRID_LEVEL_1000. bool m_DrawGrid; // hide/Show grid EDA_COLOR_T m_GridColor; // Grid color