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.

136 lines
4.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <tool/tool_manager.h>
  24. #include <tools/pcb_selection_tool.h>
  25. #include <tools/pcb_actions.h>
  26. #include <collectors.h>
  27. #include <dialogs/dialog_table_properties.h>
  28. #include <tools/pcb_edit_table_tool.h>
  29. PCB_EDIT_TABLE_TOOL::PCB_EDIT_TABLE_TOOL() :
  30. PCB_TOOL_BASE( "pcbnew.TableEditor" )
  31. {
  32. }
  33. bool PCB_EDIT_TABLE_TOOL::Init()
  34. {
  35. PCB_TOOL_BASE::Init();
  36. addMenus( m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->GetToolMenu().GetMenu() );
  37. return true;
  38. }
  39. PCB_TABLECELL* PCB_EDIT_TABLE_TOOL::copyCell( PCB_TABLECELL* aSource )
  40. {
  41. PCB_TABLECELL* cell = new PCB_TABLECELL( aSource->GetParent() );
  42. cell->SetStart( aSource->GetStart() );
  43. cell->SetEnd( aSource->GetEnd() );
  44. return cell;
  45. }
  46. const SELECTION& PCB_EDIT_TABLE_TOOL::getTableCellSelection()
  47. {
  48. PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
  49. return selTool->RequestSelection(
  50. []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
  51. {
  52. // Iterate from the back so we don't have to worry about removals.
  53. for( int i = aCollector.GetCount() - 1; i >= 0; --i )
  54. {
  55. if( !dynamic_cast<PCB_TABLECELL*>( aCollector[ i ] ) )
  56. aCollector.Remove( aCollector[ i ] );
  57. }
  58. },
  59. false /* prompt user regarding locked items */ );
  60. }
  61. void PCB_EDIT_TABLE_TOOL::clearSelection()
  62. {
  63. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear );
  64. };
  65. int PCB_EDIT_TABLE_TOOL::EditTable( const TOOL_EVENT& aEvent )
  66. {
  67. const SELECTION& selection = getTableCellSelection();
  68. bool clearSelection = selection.IsHover();
  69. PCB_TABLE* parentTable = nullptr;
  70. for( EDA_ITEM* item : selection.Items() )
  71. {
  72. if( item->Type() != PCB_TABLECELL_T )
  73. return 0;
  74. PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
  75. if( !parentTable )
  76. {
  77. parentTable = table;
  78. }
  79. else if( parentTable != table )
  80. {
  81. parentTable = nullptr;
  82. break;
  83. }
  84. }
  85. if( parentTable )
  86. {
  87. DIALOG_TABLE_PROPERTIES dlg( frame(), parentTable );
  88. dlg.ShowQuasiModal(); // Scintilla's auto-complete requires quasiModal
  89. }
  90. if( clearSelection )
  91. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear );
  92. return 0;
  93. }
  94. void PCB_EDIT_TABLE_TOOL::setTransitions()
  95. {
  96. Go( &PCB_EDIT_TABLE_TOOL::AddRowAbove, ACTIONS::addRowAbove.MakeEvent() );
  97. Go( &PCB_EDIT_TABLE_TOOL::AddRowBelow, ACTIONS::addRowBelow.MakeEvent() );
  98. Go( &PCB_EDIT_TABLE_TOOL::AddColumnBefore, ACTIONS::addColBefore.MakeEvent() );
  99. Go( &PCB_EDIT_TABLE_TOOL::AddColumnAfter, ACTIONS::addColAfter.MakeEvent() );
  100. Go( &PCB_EDIT_TABLE_TOOL::DeleteRows, ACTIONS::deleteRows.MakeEvent() );
  101. Go( &PCB_EDIT_TABLE_TOOL::DeleteColumns, ACTIONS::deleteColumns.MakeEvent() );
  102. Go( &PCB_EDIT_TABLE_TOOL::MergeCells, ACTIONS::mergeCells.MakeEvent() );
  103. Go( &PCB_EDIT_TABLE_TOOL::UnmergeCells, ACTIONS::unmergeCells.MakeEvent() );
  104. Go( &PCB_EDIT_TABLE_TOOL::EditTable, ACTIONS::editTable.MakeEvent() );
  105. }