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.

160 lines
4.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 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. #include <pcb_base_edit_frame.h>
  24. #include <grid_layer_box_helpers.h>
  25. #include <view/view.h>
  26. #include <widgets/wx_grid.h>
  27. #include <board.h>
  28. #include "dialog_swap_layers.h"
  29. class LAYER_GRID_TABLE : public wxGridTableBase
  30. {
  31. int m_layers[MAX_CU_LAYERS][2];
  32. int m_layerCount;
  33. public:
  34. LAYER_GRID_TABLE( int layerCount ) : m_layerCount( layerCount )
  35. { }
  36. int GetNumberRows() override { return m_layerCount; }
  37. int GetNumberCols() override { return 2; }
  38. wxString GetColLabelValue( int aCol ) override
  39. {
  40. switch( aCol )
  41. {
  42. case 0: return _( "Move items on:" );
  43. case 1: return _( "To layer:" );
  44. default: return wxEmptyString;
  45. }
  46. }
  47. wxString GetValue( int row, int col ) override { return "undefined"; }
  48. void SetValue( int row, int col, const wxString& value ) override { }
  49. long GetValueAsLong( int row, int col ) override
  50. {
  51. return m_layers[ row ][ col ];
  52. }
  53. void SetValueAsLong( int row, int col, long value ) override
  54. {
  55. m_layers[ row ][ col ] = value;
  56. }
  57. };
  58. DIALOG_SWAP_LAYERS::DIALOG_SWAP_LAYERS( PCB_BASE_EDIT_FRAME* aParent, PCB_LAYER_ID* aArray ) :
  59. DIALOG_SWAP_LAYERS_BASE( aParent ),
  60. m_parent( aParent ),
  61. m_layerDestinations( aArray )
  62. {
  63. m_gridTable = new LAYER_GRID_TABLE( m_parent->GetBoard()->GetCopperLayerCount() );
  64. m_grid->SetTable( m_gridTable );
  65. m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
  66. m_grid->SetCellHighlightROPenWidth( 0 );
  67. m_sdbSizerOK->SetDefault();
  68. finishDialogSettings();
  69. }
  70. DIALOG_SWAP_LAYERS::~DIALOG_SWAP_LAYERS()
  71. {
  72. m_grid->DestroyTable( m_gridTable );
  73. }
  74. bool DIALOG_SWAP_LAYERS::TransferDataToWindow()
  75. {
  76. LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
  77. int row = 0;
  78. for( size_t layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  79. {
  80. if( enabledCopperLayers.test( layer ) )
  81. {
  82. auto attr = new wxGridCellAttr;
  83. attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
  84. attr->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_MENU ) );
  85. attr->SetReadOnly();
  86. m_grid->SetAttr( row, 0, attr );
  87. attr = new wxGridCellAttr;
  88. attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
  89. attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_parent, LSET::AllNonCuMask() ) );
  90. m_grid->SetAttr( row, 1, attr );
  91. m_grid->GetTable()->SetValueAsLong( row, 0, (long) layer );
  92. m_grid->GetTable()->SetValueAsLong( row, 1, (long) layer );
  93. ++row;
  94. }
  95. }
  96. return true;
  97. }
  98. bool DIALOG_SWAP_LAYERS::TransferDataFromWindow()
  99. {
  100. if( !m_grid->CommitPendingChanges() )
  101. return false;
  102. LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
  103. wxGridTableBase* table = m_grid->GetTable();
  104. int row = 0;
  105. for( size_t layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
  106. {
  107. if( enabledCopperLayers.test( layer ) )
  108. m_layerDestinations[ layer ] = (PCB_LAYER_ID) table->GetValueAsLong( row++, 1 );
  109. else
  110. m_layerDestinations[ layer ] = (PCB_LAYER_ID) layer;
  111. }
  112. return true;
  113. }
  114. void DIALOG_SWAP_LAYERS::adjustGridColumns( int aWidth )
  115. {
  116. // Account for scroll bars
  117. aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
  118. m_grid->SetColSize( 0, aWidth / 2 );
  119. m_grid->SetColSize( 1, aWidth - m_grid->GetColSize( 0 ) );
  120. }
  121. void DIALOG_SWAP_LAYERS::OnSize( wxSizeEvent& event )
  122. {
  123. adjustGridColumns( event.GetSize().GetX() );
  124. event.Skip();
  125. }