24#ifndef EDIT_TABLE_TOOL_BASE_H
25#define EDIT_TABLE_TOOL_BASE_H
30#include <wx/translation.h>
41template<
typename T_TABLE,
typename T_TABLECELL,
typename T_COMMIT>
51 auto cellBlockSelection = [](
const SELECTION& sel )
56 int colMin = std::numeric_limits<int>::max();
58 int rowMin = std::numeric_limits<int>::max();
64 if( T_TABLECELL* cell =
dynamic_cast<T_TABLECELL*
>( item ) )
66 colMin = std::min( colMin, cell->GetColumn() );
67 colMax = std::max( colMax, cell->GetColumn() + cell->GetColSpan() );
68 rowMin = std::min( rowMin, cell->GetRow() );
69 rowMax = std::max( rowMax, cell->GetRow() + cell->GetRowSpan() );
71 selectedArea += cell->GetColSpan() * cell->GetRowSpan();
75 return selectedArea == ( colMax - colMin ) * ( rowMax - rowMin );
78 auto mergedCellsSelection = [](
const SELECTION& sel )
82 if( T_TABLECELL* cell =
dynamic_cast<T_TABLECELL*
>( item ) )
84 if( cell->GetColSpan() > 1 || cell->GetRowSpan() > 1 )
119 T_TABLECELL* topmost =
nullptr;
123 T_TABLECELL* cell =
static_cast<T_TABLECELL*
>( item );
125 if( !topmost || cell->GetRow() < topmost->GetRow() )
132 int row = topmost->GetRow();
133 T_TABLE*
table =
static_cast<T_TABLE*
>( topmost->GetParent() );
138 std::vector<T_TABLECELL*> sources;
139 sources.reserve(
table->GetColCount() );
141 for(
int col = 0; col <
table->GetColCount(); ++col )
142 sources.push_back(
table->GetCell( row, col ) );
146 for(
int col = 0; col <
table->GetColCount(); ++col )
148 T_TABLECELL* cell =
copyCell( sources[col] );
149 table->InsertCell( row *
table->GetColCount(), cell );
152 for(
int afterRow =
table->GetRowCount() - 1; afterRow > row; afterRow-- )
153 table->SetRowHeight( afterRow,
table->GetRowHeight( afterRow - 1 ) );
155 table->SetPosition( pos );
160 commit.Push(
_(
"Add Row Above" ) );
168 T_TABLECELL* bottommost =
nullptr;
170 if( selection.
Empty() )
175 T_TABLECELL* cell =
static_cast<T_TABLECELL*
>( item );
177 if( !bottommost || cell->GetRow() > bottommost->GetRow() )
184 int row = bottommost->GetRow();
185 T_TABLE*
table =
static_cast<T_TABLE*
>( bottommost->GetParent() );
190 std::vector<T_TABLECELL*> sources;
191 sources.reserve(
table->GetColCount() );
193 for(
int col = 0; col <
table->GetColCount(); ++col )
194 sources.push_back(
table->GetCell( row, col ) );
198 for(
int col = 0; col <
table->GetColCount(); ++col )
200 T_TABLECELL* cell =
copyCell( sources[col] );
201 table->InsertCell( ( row + 1 ) *
table->GetColCount(), cell );
204 for(
int afterRow =
table->GetRowCount() - 1; afterRow > row; afterRow-- )
205 table->SetRowHeight( afterRow,
table->GetRowHeight( afterRow - 1 ) );
207 table->SetPosition( pos );
212 commit.Push(
_(
"Add Row Below" ) );
220 T_TABLECELL* leftmost =
nullptr;
224 T_TABLECELL* cell =
static_cast<T_TABLECELL*
>( item );
226 if( !leftmost || cell->GetColumn() < leftmost->GetColumn() )
233 int col = leftmost->GetColumn();
234 T_TABLE*
table =
static_cast<T_TABLE*
>( leftmost->GetParent() );
235 int rowCount =
table->GetRowCount();
240 std::vector<T_TABLECELL*> sources;
241 sources.reserve( rowCount );
243 for(
int row = 0; row < rowCount; ++row )
244 sources.push_back(
table->GetCell( row, col ) );
247 table->SetColCount(
table->GetColCount() + 1 );
249 for(
int row = 0; row < rowCount; ++row )
251 T_TABLECELL* cell =
copyCell( sources[row] );
252 table->InsertCell( row *
table->GetColCount() + col, cell );
255 for(
int afterCol =
table->GetColCount() - 1; afterCol > col; afterCol-- )
256 table->SetColWidth( afterCol,
table->GetColWidth( afterCol - 1 ) );
258 table->SetPosition( pos );
263 commit.Push(
_(
"Add Column Before" ) );
271 T_TABLECELL* rightmost =
nullptr;
275 T_TABLECELL* cell =
static_cast<T_TABLECELL*
>( item );
277 if( !rightmost || cell->GetColumn() > rightmost->GetColumn() )
284 int col = rightmost->GetColumn();
285 T_TABLE*
table =
static_cast<T_TABLE*
>( rightmost->GetParent() );
286 int rowCount =
table->GetRowCount();
291 std::vector<T_TABLECELL*> sources;
292 sources.reserve( rowCount );
294 for(
int row = 0; row < rowCount; ++row )
295 sources.push_back(
table->GetCell( row, col ) );
298 table->SetColCount(
table->GetColCount() + 1 );
300 for(
int row = 0; row < rowCount; ++row )
302 T_TABLECELL* cell =
copyCell( sources[row] );
303 table->InsertCell( row *
table->GetColCount() + col + 1, cell );
306 for(
int afterCol =
table->GetColCount() - 1; afterCol > col; afterCol-- )
307 table->SetColWidth( afterCol,
table->GetColWidth( afterCol - 1 ) );
309 table->SetPosition( pos );
314 commit.Push(
_(
"Add Column After" ) );
323 if( selection.
Empty() )
326 T_TABLE*
table =
static_cast<T_TABLE*
>( selection[0]->GetParent() );
327 std::vector<int> deleted;
329 for( T_TABLECELL* cell :
table->GetCells() )
332 for(
int row = 0; row <
table->GetRowCount(); ++row )
334 bool deleteRow =
false;
336 for(
int col = 0; col <
table->GetColCount(); ++col )
338 if(
table->GetCell( row, col )->IsSelected() )
347 for(
int col = 0; col <
table->GetColCount(); ++col )
350 deleted.push_back( row );
356 if( deleted.size() == (
unsigned)
table->GetRowCount() )
367 table->DeleteMarkedCells();
369 for(
int row = 0; row <
table->GetRowCount(); ++row )
373 for(
int deletedRow : deleted )
375 if( deletedRow >= row )
379 table->SetRowHeight( row,
table->GetRowHeight( row + offset ) );
382 table->SetPosition( pos );
388 if( deleted.size() > 1 )
389 commit.Push(
_(
"Delete Rows" ) );
391 commit.Push(
_(
"Delete Row" ) );
400 if( selection.
Empty() )
403 T_TABLE*
table =
static_cast<T_TABLE*
>( selection[0]->GetParent() );
404 std::vector<int> deleted;
406 for( T_TABLECELL* cell :
table->GetCells() )
409 for(
int col = 0; col <
table->GetColCount(); ++col )
411 bool deleteColumn =
false;
413 for(
int row = 0; row <
table->GetRowCount(); ++row )
415 if(
table->GetCell( row, col )->IsSelected() )
424 for(
int row = 0; row <
table->GetRowCount(); ++row )
427 deleted.push_back( col );
433 if( deleted.size() == (
unsigned)
table->GetColCount() )
444 table->DeleteMarkedCells();
445 table->SetColCount(
table->GetColCount() - deleted.size() );
447 for(
int col = 0; col <
table->GetColCount(); ++col )
451 for(
int deletedCol : deleted )
453 if( deletedCol >= col )
457 table->SetColWidth( col,
table->GetColWidth( col + offset ) );
460 table->SetPosition( pos );
466 if( deleted.size() > 1 )
467 commit.Push(
_(
"Delete Columns" ) );
469 commit.Push(
_(
"Delete Column" ) );
481 int colMin = std::numeric_limits<int>::max();
483 int rowMin = std::numeric_limits<int>::max();
487 T_TABLE*
table =
static_cast<T_TABLE*
>( sel[0]->GetParent() );
491 if( T_TABLECELL* cell =
dynamic_cast<T_TABLECELL*
>( item ) )
493 colMin = std::min( colMin, cell->GetColumn() );
494 colMax = std::max( colMax, cell->GetColumn() + cell->GetColSpan() );
495 rowMin = std::min( rowMin, cell->GetRow() );
496 rowMax = std::max( rowMax, cell->GetRow() + cell->GetRowSpan() );
503 for(
int row = rowMin; row < rowMax; ++row )
505 extents.
y +=
table->GetRowHeight( row );
508 for(
int col = colMin; col < colMax; ++col )
510 extents.
x +=
table->GetColWidth( col );
512 T_TABLECELL* cell =
table->GetCell( row, col );
514 if( !cell->GetText().IsEmpty() )
516 if( !content.IsEmpty() )
519 content += cell->GetText();
523 cell->SetColSpan( 0 );
524 cell->SetRowSpan( 0 );
525 cell->SetText( wxEmptyString );
529 T_TABLECELL* topLeft =
table->GetCell( rowMin, colMin );
530 topLeft->SetColSpan( colMax - colMin );
531 topLeft->SetRowSpan( rowMax - rowMin );
532 topLeft->SetText( content );
533 topLeft->SetEnd( topLeft->GetStart() + extents );
536 commit.Push(
_(
"Merge Cells" ) );
551 T_TABLE*
table =
static_cast<T_TABLE*
>( sel[0]->GetParent() );
555 if( T_TABLECELL* cell =
dynamic_cast<T_TABLECELL*
>( item ) )
557 int rowSpan = cell->GetRowSpan();
558 int colSpan = cell->GetColSpan();
560 for(
int row = cell->GetRow(); row < cell->GetRow() + rowSpan; ++row )
562 for(
int col = cell->GetColumn(); col < cell->GetColumn() + colSpan; ++col )
564 T_TABLECELL* target =
table->GetCell( row, col );
566 target->SetColSpan( 1 );
567 target->SetRowSpan( 1 );
570 target->SetEnd( target->GetStart() + extents );
577 commit.Push(
_(
"Unmerge Cells" ) );
590 virtual T_TABLECELL*
copyCell( T_TABLECELL* aSource ) = 0;
static TOOL_ACTION addRowAbove
static TOOL_ACTION addColBefore
static TOOL_ACTION deleteRows
static TOOL_ACTION addRowBelow
static TOOL_ACTION deleteColumns
static TOOL_ACTION unmergeCells
static TOOL_ACTION mergeCells
static TOOL_ACTION addColAfter
static TOOL_ACTION editTable
Handles how to draw a screen (a board, a schematic ...)
A base class for most all the KiCad significant classes used in schematics and boards.
static const TOOL_EVENT SelectedEvent
static SELECTION_CONDITION MoreThan(int aNumber)
Create a functor that tests if the number of selected items is greater than the value given as parame...
static bool Idle(const SELECTION &aSelection)
Test if there no items selected or being edited.
static SELECTION_CONDITION OnlyTypes(std::vector< KICAD_T > aTypes)
Create a functor that tests if the selected items are only of given types.
bool Empty() const
Checks if there is anything selected.
#define STRUCT_DELETED
flag indication structures to be erased
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
VECTOR2< int32_t > VECTOR2I