You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file dialog_set_grid.cpp
  25. * @brief Manage user grid.
  26. */
  27. #include <dialog_set_grid_base.h>
  28. #include <base_units.h>
  29. #include <common.h>
  30. #include <widgets/unit_binder.h>
  31. #include <hotkeys.h>
  32. #include <pcb_base_edit_frame.h>
  33. #include <class_drawpanel.h>
  34. #include <class_draw_panel_gal.h>
  35. #include <gal/graphics_abstraction_layer.h>
  36. #include <tools/pcb_actions.h>
  37. #include <tool/tool_manager.h>
  38. // Max values for grid size
  39. static const int MAX_GRID_SIZE = KiROUND( 1000.0 * IU_PER_MM );
  40. static const int MIN_GRID_SIZE = KiROUND( 0.001 * IU_PER_MM );
  41. class DIALOG_SET_GRID : public DIALOG_SET_GRID_BASE
  42. {
  43. PCB_BASE_FRAME* m_parent;
  44. wxArrayString m_fast_grid_opts;
  45. public:
  46. /// This has no dependencies on calling wxFrame derivative, such as PCB_BASE_FRAME.
  47. DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices );
  48. bool TransferDataFromWindow() override;
  49. bool TransferDataToWindow() override;
  50. private:
  51. void OnResetGridOrgClick( wxCommandEvent& event ) override;
  52. UNIT_BINDER m_gridOriginX;
  53. UNIT_BINDER m_gridOriginY;
  54. UNIT_BINDER m_userGridX;
  55. UNIT_BINDER m_userGridY;
  56. };
  57. DIALOG_SET_GRID::DIALOG_SET_GRID( PCB_BASE_FRAME* aParent, const wxArrayString& aGridChoices ):
  58. DIALOG_SET_GRID_BASE( aParent ),
  59. m_parent( aParent ),
  60. m_fast_grid_opts( aGridChoices ),
  61. m_gridOriginX( aParent, m_staticTextGridPosX, m_GridOriginXCtrl, m_TextPosXUnits ),
  62. m_gridOriginY( aParent, m_staticTextGridPosY, m_GridOriginYCtrl, m_TextPosYUnits ),
  63. m_userGridX( aParent, m_staticTextSizeX, m_OptGridSizeX, m_TextSizeXUnits ),
  64. m_userGridY( aParent, m_staticTextSizeY, m_OptGridSizeY, m_TextSizeYUnits )
  65. {
  66. m_comboBoxGrid1->Append( m_fast_grid_opts );
  67. m_comboBoxGrid2->Append( m_fast_grid_opts );
  68. m_sdbSizerOK->SetDefault(); // set OK button as default response to 'Enter' key
  69. SetInitialFocus( m_GridOriginXCtrl );
  70. Layout();
  71. // Now all widgets have the size fixed, call FinishDialogSettings
  72. FinishDialogSettings();
  73. }
  74. bool DIALOG_SET_GRID::TransferDataFromWindow()
  75. {
  76. // Validate new settings
  77. if( !m_userGridX.Validate( MIN_GRID_SIZE, MAX_GRID_SIZE ) )
  78. return false;
  79. if( !m_userGridY.Validate( MIN_GRID_SIZE, MAX_GRID_SIZE ) )
  80. return false;
  81. // Apply the new settings
  82. // Because grid origin is saved in board, show as modified
  83. m_parent->OnModify();
  84. m_parent->SetGridOrigin( wxPoint( m_gridOriginX.GetValue(), m_gridOriginY.GetValue() ) );
  85. m_parent->m_UserGridSize = wxPoint( m_userGridX.GetValue(), m_userGridY.GetValue() );
  86. m_parent->m_FastGrid1 = m_comboBoxGrid1->GetSelection();
  87. m_parent->m_FastGrid2 = m_comboBoxGrid2->GetSelection();
  88. // User grid
  89. BASE_SCREEN* screen = m_parent->GetScreen();
  90. screen->AddGrid( m_parent->m_UserGridSize, EDA_UNITS_T::UNSCALED_UNITS, ID_POPUP_GRID_USER );
  91. // If the user grid is the current option, recall SetGrid()
  92. // to force new values put in list as current grid value
  93. if( screen->GetGridCmdId() == ID_POPUP_GRID_USER )
  94. screen->SetGrid( ID_POPUP_GRID_USER );
  95. // Notify GAL
  96. TOOL_MANAGER* mgr = m_parent->GetToolManager();
  97. if( mgr && m_parent->IsGalCanvasActive() )
  98. {
  99. mgr->RunAction( "common.Control.gridPreset", true,
  100. screen->GetGridCmdId() - ID_POPUP_GRID_LEVEL_1000 );
  101. TOOL_EVENT gridOriginUpdate = ACTIONS::gridSetOrigin.MakeEvent();
  102. gridOriginUpdate.SetParameter( new VECTOR2D( m_parent->GetGridOrigin() ) );
  103. mgr->ProcessEvent( gridOriginUpdate );
  104. }
  105. m_parent->UpdateGridSelectBox();
  106. return wxDialog::TransferDataFromWindow();
  107. }
  108. bool DIALOG_SET_GRID::TransferDataToWindow()
  109. {
  110. m_userGridX.SetValue( m_parent->m_UserGridSize.x );
  111. m_userGridY.SetValue( m_parent->m_UserGridSize.y );
  112. m_gridOriginX.SetValue( m_parent->GetGridOrigin().x );
  113. m_gridOriginY.SetValue( m_parent->GetGridOrigin().y );
  114. m_comboBoxGrid1->SetSelection( m_parent->m_FastGrid1 );
  115. m_comboBoxGrid2->SetSelection( m_parent->m_FastGrid2 );
  116. int hk1 = m_parent->GetHotKeyDescription( HK_SWITCH_GRID_TO_FASTGRID1 )->m_KeyCode;
  117. int hk2 = m_parent->GetHotKeyDescription( HK_SWITCH_GRID_TO_FASTGRID2 )->m_KeyCode;
  118. m_grid1HotKey->SetLabel( wxString::Format( wxT( "(%s)" ), KeyNameFromKeyCode( hk1 ) ) );
  119. m_grid2HotKey->SetLabel( wxString::Format( wxT( "(%s)" ), KeyNameFromKeyCode( hk2 ) ) );
  120. return wxDialog::TransferDataToWindow();
  121. }
  122. void DIALOG_SET_GRID::OnResetGridOrgClick( wxCommandEvent& event )
  123. {
  124. m_gridOriginX.SetValue( 0 );
  125. m_gridOriginY.SetValue( 0 );
  126. }
  127. bool PCB_BASE_EDIT_FRAME::InvokeDialogGrid()
  128. {
  129. DIALOG_SET_GRID dlg( this, m_gridSelectBox->GetStrings() );
  130. return dlg.ShowModal();
  131. }
  132. void PCB_BASE_EDIT_FRAME::OnGridSettings( wxCommandEvent& event )
  133. {
  134. InvokeDialogGrid();
  135. }