KiCad PCB EDA Suite
Loading...
Searching...
No Matches
edit_table_tool_base.h
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef EDIT_TABLE_TOOL_BASE_H
21#define EDIT_TABLE_TOOL_BASE_H
22
23#include <tool/tool_base.h>
24#include <tool/selection.h>
25#include <tool/actions.h>
26#include <wx/translation.h>
27
28class BASE_SCREEN;
29
30
35
36
37template<typename T_TABLE, typename T_TABLECELL, typename T_COMMIT>
39{
40protected:
41 void addMenus( CONDITIONAL_MENU& selToolMenu )
42 {
43 auto cellSelection = SELECTION_CONDITIONS::MoreThan( 0 )
45
46 auto cellBlockSelection = []( const SELECTION& sel )
47 {
48 if( sel.Size() < 2 )
49 return false;
50
51 int colMin = std::numeric_limits<int>::max();
52 int colMax = 0;
53 int rowMin = std::numeric_limits<int>::max();
54 int rowMax = 0;
55 int selectedArea = 0;
56
57 for( EDA_ITEM* item : sel )
58 {
59 if( T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item ) )
60 {
61 colMin = std::min( colMin, cell->GetColumn() );
62 colMax = std::max( colMax, cell->GetColumn() + cell->GetColSpan() );
63 rowMin = std::min( rowMin, cell->GetRow() );
64 rowMax = std::max( rowMax, cell->GetRow() + cell->GetRowSpan() );
65
66 selectedArea += cell->GetColSpan() * cell->GetRowSpan();
67 }
68 }
69
70 return selectedArea == ( colMax - colMin ) * ( rowMax - rowMin );
71 };
72
73 auto structureEditable = [this]( const SELECTION& sel )
74 {
76 };
77
78 auto mergedCellsSelection = []( const SELECTION& sel )
79 {
80 for( EDA_ITEM* item : sel )
81 {
82 if( T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item ) )
83 {
84 if( cell->GetColSpan() > 1 || cell->GetRowSpan() > 1 )
85 return true;
86 }
87 }
88
89 return false;
90 };
91
92 //
93 // Add editing actions to the selection tool menu
94 //
95 selToolMenu.AddSeparator( 100 );
96 auto structureSelection = cellSelection && structureEditable;
97
98 selToolMenu.AddItem( ACTIONS::addRowAbove, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
99 selToolMenu.AddItem( ACTIONS::addRowBelow, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
100 selToolMenu.AddItem( ACTIONS::addColBefore, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
101 selToolMenu.AddItem( ACTIONS::addColAfter, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
102
103 selToolMenu.AddSeparator( 100 );
104 selToolMenu.AddItem( ACTIONS::deleteRows, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
105 selToolMenu.AddItem( ACTIONS::deleteColumns, structureSelection && SELECTION_CONDITIONS::Idle, 100 );
106
107 selToolMenu.AddSeparator( 100 );
108 selToolMenu.AddItem( ACTIONS::mergeCells, structureSelection && cellBlockSelection, 100 );
109 selToolMenu.AddItem( ACTIONS::unmergeCells, structureSelection && mergedCellsSelection, 100 );
110
111 selToolMenu.AddSeparator( 100 );
112 selToolMenu.AddItem( ACTIONS::editTable, cellSelection && SELECTION_CONDITIONS::Idle, 100 );
113
114 selToolMenu.AddSeparator( 100 );
115 selToolMenu.AddItem( ACTIONS::exportTableCSV, cellSelection && SELECTION_CONDITIONS::Idle, 100 );
116
117 selToolMenu.AddSeparator( 100 );
118 }
119
120 int doAddRowAbove( const TOOL_EVENT& aEvent )
121 {
123 return 0;
124
125 const SELECTION& selection = getTableCellSelection();
126 T_TABLECELL* topmost = nullptr;
127
128 for( EDA_ITEM* item : selection )
129 {
130 T_TABLECELL* cell = static_cast<T_TABLECELL*>( item );
131
132 if( !topmost || cell->GetRow() < topmost->GetRow() )
133 topmost = cell;
134 }
135
136 if( !topmost )
137 return 0;
138
139 int row = topmost->GetRow();
140 T_TABLE* table = static_cast<T_TABLE*>( topmost->GetParent() );
141 T_COMMIT commit( getToolMgr() );
142 VECTOR2I pos = table->GetPosition();
143
144 // Make a copy of the source row before things start moving around
145 std::vector<T_TABLECELL*> sources;
146 sources.reserve( table->GetColCount() );
147
148 for( int col = 0; col < table->GetColCount(); ++col )
149 sources.push_back( table->GetCell( row, col ) );
150
151 commit.Modify( table, getScreen() );
152
153 for( int col = 0; col < table->GetColCount(); ++col )
154 {
155 T_TABLECELL* cell = copyCell( sources[col] );
156 table->InsertCell( row * table->GetColCount(), cell );
157 }
158
159 for( int afterRow = table->GetRowCount() - 1; afterRow > row; afterRow-- )
160 table->SetRowHeight( afterRow, table->GetRowHeight( afterRow - 1 ) );
161
162 table->SetPosition( pos );
163 table->Normalize();
164
166
167 commit.Push( _( "Add Row Above" ) );
168
169 return 0;
170 }
171
172 int doAddRowBelow( const TOOL_EVENT& aEvent )
173 {
175 return 0;
176
177 const SELECTION& selection = getTableCellSelection();
178 T_TABLECELL* bottommost = nullptr;
179
180 if( selection.Empty() )
181 return 0;
182
183 for( EDA_ITEM* item : selection )
184 {
185 T_TABLECELL* cell = static_cast<T_TABLECELL*>( item );
186
187 if( !bottommost || cell->GetRow() > bottommost->GetRow() )
188 bottommost = cell;
189 }
190
191 if( !bottommost )
192 return 0;
193
194 int row = bottommost->GetRow();
195 T_TABLE* table = static_cast<T_TABLE*>( bottommost->GetParent() );
196 T_COMMIT commit( getToolMgr() );
197 VECTOR2I pos = table->GetPosition();
198
199 // Make a copy of the source row before things start moving around
200 std::vector<T_TABLECELL*> sources;
201 sources.reserve( table->GetColCount() );
202
203 for( int col = 0; col < table->GetColCount(); ++col )
204 sources.push_back( table->GetCell( row, col ) );
205
206 commit.Modify( table, getScreen() );
207
208 for( int col = 0; col < table->GetColCount(); ++col )
209 {
210 T_TABLECELL* cell = copyCell( sources[col] );
211 table->InsertCell( ( row + 1 ) * table->GetColCount(), cell );
212 }
213
214 for( int afterRow = table->GetRowCount() - 1; afterRow > row; afterRow-- )
215 table->SetRowHeight( afterRow, table->GetRowHeight( afterRow - 1 ) );
216
217 table->SetPosition( pos );
218 table->Normalize();
219
221
222 commit.Push( _( "Add Row Below" ) );
223
224 return 0;
225 }
226
227 int doAddColumnBefore( const TOOL_EVENT& aEvent )
228 {
230 return 0;
231
232 const SELECTION& selection = getTableCellSelection();
233 T_TABLECELL* leftmost = nullptr;
234
235 for( EDA_ITEM* item : selection )
236 {
237 T_TABLECELL* cell = static_cast<T_TABLECELL*>( item );
238
239 if( !leftmost || cell->GetColumn() < leftmost->GetColumn() )
240 leftmost = cell;
241 }
242
243 if( !leftmost )
244 return 0;
245
246 int col = leftmost->GetColumn();
247 T_TABLE* table = static_cast<T_TABLE*>( leftmost->GetParent() );
248 int rowCount = table->GetRowCount();
249 T_COMMIT commit( getToolMgr() );
250 VECTOR2I pos = table->GetPosition();
251
252 // Make a copy of the source column before things start moving around
253 std::vector<T_TABLECELL*> sources;
254 sources.reserve( rowCount );
255
256 for( int row = 0; row < rowCount; ++row )
257 sources.push_back( table->GetCell( row, col ) );
258
259 commit.Modify( table, getScreen() );
260 table->SetColCount( table->GetColCount() + 1 );
261
262 for( int row = 0; row < rowCount; ++row )
263 {
264 T_TABLECELL* cell = copyCell( sources[row] );
265 table->InsertCell( row * table->GetColCount() + col, cell );
266 }
267
268 for( int afterCol = table->GetColCount() - 1; afterCol > col; afterCol-- )
269 table->SetColWidth( afterCol, table->GetColWidth( afterCol - 1 ) );
270
271 table->SetPosition( pos );
272 table->Normalize();
273
275
276 commit.Push( _( "Add Column Before" ) );
277
278 return 0;
279 }
280
281 int doAddColumnAfter( const TOOL_EVENT& aEvent )
282 {
284 return 0;
285
286 const SELECTION& selection = getTableCellSelection();
287 T_TABLECELL* rightmost = nullptr;
288
289 for( EDA_ITEM* item : selection )
290 {
291 T_TABLECELL* cell = static_cast<T_TABLECELL*>( item );
292
293 if( !rightmost || cell->GetColumn() > rightmost->GetColumn() )
294 rightmost = cell;
295 }
296
297 if( !rightmost )
298 return 0;
299
300 int col = rightmost->GetColumn();
301 T_TABLE* table = static_cast<T_TABLE*>( rightmost->GetParent() );
302 int rowCount = table->GetRowCount();
303 T_COMMIT commit( getToolMgr() );
304 VECTOR2I pos = table->GetPosition();
305
306 // Make a copy of the source column before things start moving around
307 std::vector<T_TABLECELL*> sources;
308 sources.reserve( rowCount );
309
310 for( int row = 0; row < rowCount; ++row )
311 sources.push_back( table->GetCell( row, col ) );
312
313 commit.Modify( table, getScreen() );
314 table->SetColCount( table->GetColCount() + 1 );
315
316 for( int row = 0; row < rowCount; ++row )
317 {
318 T_TABLECELL* cell = copyCell( sources[row] );
319 table->InsertCell( row * table->GetColCount() + col + 1, cell );
320 }
321
322 for( int afterCol = table->GetColCount() - 1; afterCol > col; afterCol-- )
323 table->SetColWidth( afterCol, table->GetColWidth( afterCol - 1 ) );
324
325 table->SetPosition( pos );
326 table->Normalize();
327
329
330 commit.Push( _( "Add Column After" ) );
331
332 return 0;
333 }
334
335 int doDeleteRows( const TOOL_EVENT& aEvent )
336 {
338 return 0;
339
340 const SELECTION& selection = getTableCellSelection();
341
342 if( selection.Empty() )
343 return 0;
344
345 T_TABLE* table = static_cast<T_TABLE*>( selection[0]->GetParent() );
346 std::vector<int> deleted;
347
348 for( T_TABLECELL* cell : table->GetCells() )
349 cell->ClearFlags( STRUCT_DELETED );
350
351 for( int row = 0; row < table->GetRowCount(); ++row )
352 {
353 bool deleteRow = false;
354
355 for( int col = 0; col < table->GetColCount(); ++col )
356 {
357 if( table->GetCell( row, col )->IsSelected() )
358 {
359 deleteRow = true;
360 break;
361 }
362 }
363
364 if( deleteRow )
365 {
366 for( int col = 0; col < table->GetColCount(); ++col )
367 table->GetCell( row, col )->SetFlags( STRUCT_DELETED );
368
369 deleted.push_back( row );
370 }
371 }
372
373 T_COMMIT commit( getToolMgr() );
374
375 if( deleted.size() == (unsigned) table->GetRowCount() )
376 {
377 commit.Remove( table, getScreen() );
378 }
379 else
380 {
381 commit.Modify( table, getScreen() );
382
383 VECTOR2I pos = table->GetPosition();
384
385 // Save old row heights BEFORE deleting cells
386 std::map<int, int> oldRowHeights;
387 for( int row = 0; row < table->GetRowCount(); ++row )
388 oldRowHeights[row] = table->GetRowHeight( row );
389
391 table->DeleteMarkedCells();
392
393 for( int row = 0; row < table->GetRowCount(); ++row )
394 {
395 int old_row = row;
396
397 for( int deletedRow : deleted )
398 {
399 if( deletedRow <= old_row )
400 old_row++;
401 }
402
403 // Use saved old heights instead of querying the modified table
404 int height = ( oldRowHeights.count( old_row ) ) ? oldRowHeights[old_row] : 0;
405 table->SetRowHeight( row, height );
406 }
407
408 table->SetPosition( pos );
409 table->Normalize();
410
412 }
413
414 if( deleted.size() > 1 )
415 commit.Push( _( "Delete Rows" ) );
416 else
417 commit.Push( _( "Delete Row" ) );
418
419 return 0;
420 }
421
422 int doDeleteColumns( const TOOL_EVENT& aEvent )
423 {
425 return 0;
426
427 const SELECTION& selection = getTableCellSelection();
428
429 if( selection.Empty() )
430 return 0;
431
432 T_TABLE* table = static_cast<T_TABLE*>( selection[0]->GetParent() );
433 std::vector<int> deleted;
434
435 for( T_TABLECELL* cell : table->GetCells() )
436 cell->ClearFlags( STRUCT_DELETED );
437
438 for( int col = 0; col < table->GetColCount(); ++col )
439 {
440 bool deleteColumn = false;
441
442 for( int row = 0; row < table->GetRowCount(); ++row )
443 {
444 if( table->GetCell( row, col )->IsSelected() )
445 {
446 deleteColumn = true;
447 break;
448 }
449 }
450
451 if( deleteColumn )
452 {
453 for( int row = 0; row < table->GetRowCount(); ++row )
454 table->GetCell( row, col )->SetFlags( STRUCT_DELETED );
455
456 deleted.push_back( col );
457 }
458 }
459
460 T_COMMIT commit( getToolMgr() );
461
462 if( deleted.size() == (unsigned) table->GetColCount() )
463 {
464 commit.Remove( table, getScreen() );
465 }
466 else
467 {
468 commit.Modify( table, getScreen() );
469
470 VECTOR2I pos = table->GetPosition();
471
473 table->DeleteMarkedCells();
474 table->SetColCount( table->GetColCount() - deleted.size() );
475
476 for( int col = 0; col < table->GetColCount(); ++col )
477 {
478 int old_col = col;
479
480 for( int deletedCol : deleted )
481 {
482 if( deletedCol <= old_col )
483 old_col++;
484 }
485
486 table->SetColWidth( col, table->GetColWidth( old_col ) );
487 }
488
489 table->SetPosition( pos );
490 table->Normalize();
491
493 }
494
495 if( deleted.size() > 1 )
496 commit.Push( _( "Delete Columns" ) );
497 else
498 commit.Push( _( "Delete Column" ) );
499
500 return 0;
501 }
502
503 int doMergeCells( const TOOL_EVENT& aEvent )
504 {
506 return 0;
507
508 const SELECTION& sel = getTableCellSelection();
509
510 if( sel.Empty() )
511 return 0;
512
513 int colMin = std::numeric_limits<int>::max();
514 int colMax = 0;
515 int rowMin = std::numeric_limits<int>::max();
516 int rowMax = 0;
517
518 T_COMMIT commit( getToolMgr() );
519 T_TABLE* table = static_cast<T_TABLE*>( sel[0]->GetParent() );
520
521 for( EDA_ITEM* item : sel )
522 {
523 if( T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item ) )
524 {
525 colMin = std::min( colMin, cell->GetColumn() );
526 colMax = std::max( colMax, cell->GetColumn() + cell->GetColSpan() );
527 rowMin = std::min( rowMin, cell->GetRow() );
528 rowMax = std::max( rowMax, cell->GetRow() + cell->GetRowSpan() );
529 }
530 }
531
532 wxString content;
533 VECTOR2I extents;
534
535 for( int row = rowMin; row < rowMax; ++row )
536 {
537 extents.y += table->GetRowHeight( row );
538 extents.x = 0;
539
540 for( int col = colMin; col < colMax; ++col )
541 {
542 extents.x += table->GetColWidth( col );
543
544 T_TABLECELL* cell = table->GetCell( row, col );
545
546 if( !cell->GetText().IsEmpty() )
547 {
548 if( !content.IsEmpty() )
549 content += "\n";
550
551 content += cell->GetText();
552 }
553
554 commit.Modify( cell, getScreen() );
555 cell->SetColSpan( 0 );
556 cell->SetRowSpan( 0 );
557 cell->SetText( wxEmptyString );
558 }
559 }
560
561 T_TABLECELL* topLeft = table->GetCell( rowMin, colMin );
562 topLeft->SetColSpan( colMax - colMin );
563 topLeft->SetRowSpan( rowMax - rowMin );
564 topLeft->SetText( content );
565 topLeft->SetEnd( topLeft->GetStart() + extents );
566
567 table->Normalize();
568 commit.Push( _( "Merge Cells" ) );
569
571
572 return 0;
573 }
574
575 int doUnmergeCells( const TOOL_EVENT& aEvent )
576 {
578 return 0;
579
580 const SELECTION& sel = getTableCellSelection();
581
582 if( sel.Empty() )
583 return 0;
584
585 T_COMMIT commit( getToolMgr() );
586 T_TABLE* table = static_cast<T_TABLE*>( sel[0]->GetParent() );
587
588 for( EDA_ITEM* item : sel )
589 {
590 if( T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item ) )
591 {
592 int rowSpan = cell->GetRowSpan();
593 int colSpan = cell->GetColSpan();
594
595 for( int row = cell->GetRow(); row < cell->GetRow() + rowSpan; ++row )
596 {
597 for( int col = cell->GetColumn(); col < cell->GetColumn() + colSpan; ++col )
598 {
599 T_TABLECELL* target = table->GetCell( row, col );
600 commit.Modify( target, getScreen() );
601 target->SetColSpan( 1 );
602 target->SetRowSpan( 1 );
603
604 VECTOR2I extents( table->GetColWidth( col ), table->GetRowHeight( row ) );
605 target->SetEnd( target->GetStart() + extents );
606 }
607 }
608 }
609 }
610
611 table->Normalize();
612 commit.Push( _( "Unmerge Cells" ) );
613
615
616 return 0;
617 }
618
619public:
629 bool getCellBlockBounds( const SELECTION& aSel, int& aColMin, int& aColMax, int& aRowMin, int& aRowMax )
630 {
631 if( aSel.Size() < 1 )
632 return false;
633
634 aColMin = std::numeric_limits<int>::max();
635 aColMax = 0;
636 aRowMin = std::numeric_limits<int>::max();
637 aRowMax = 0;
638 int selectedArea = 0;
639
640 for( EDA_ITEM* item : aSel )
641 {
642 if( T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item ) )
643 {
644 aColMin = std::min( aColMin, cell->GetColumn() );
645 aColMax = std::max( aColMax, cell->GetColumn() + cell->GetColSpan() );
646 aRowMin = std::min( aRowMin, cell->GetRow() );
647 aRowMax = std::max( aRowMax, cell->GetRow() + cell->GetRowSpan() );
648
649 selectedArea += cell->GetColSpan() * cell->GetRowSpan();
650 }
651 }
652
653 // Check if selection is contiguous (for single cell, we allow it)
654 if( aSel.Size() == 1 )
655 return true;
656
657 return selectedArea == ( aColMax - aColMin ) * ( aRowMax - aRowMin );
658 }
659
666 bool validatePasteIntoSelection( const SELECTION& aSel, wxString& aErrorMsg )
667 {
668 if( aSel.Empty() )
669 {
670 aErrorMsg = _( "No cells selected" );
671 return false;
672 }
673
674 // Check if all selected items are table cells from the same table
675 T_TABLE* table = nullptr;
676
677 for( EDA_ITEM* item : aSel )
678 {
679 T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item );
680
681 if( !cell )
682 {
683 aErrorMsg = _( "Selection contains non-cell items" );
684 return false;
685 }
686
687 if( !table )
688 {
689 table = static_cast<T_TABLE*>( cell->GetParent() );
690
692 {
693 aErrorMsg = _( "Cannot paste into a table whose cells are generated" );
694 return false;
695 }
696 }
697 else if( cell->GetParent() != table )
698 {
699 aErrorMsg = _( "Selected cells are from different tables" );
700 return false;
701 }
702
703 // Check for merged cells (block paste-into for now)
704 if( cell->GetColSpan() > 1 || cell->GetRowSpan() > 1 )
705 {
706 aErrorMsg = _( "Cannot paste into merged cells" );
707 return false;
708 }
709 }
710
711 // Check if selection is contiguous
712 int colMin, colMax, rowMin, rowMax;
713
714 if( !getCellBlockBounds( aSel, colMin, colMax, rowMin, rowMax ) )
715 {
716 aErrorMsg = _( "Selected cells must form a contiguous block" );
717 return false;
718 }
719
720 return true;
721 }
722
730 bool pasteCellsIntoSelection( const SELECTION& aSel, T_TABLE* aSourceTable, T_COMMIT& aCommit )
731 {
732 if( !aSourceTable || aSel.Empty() || !selectionAllowsStructureEdits( aSel ) )
733 return false;
734
735 // Get target cell range
736 int targetColMin, targetColMax, targetRowMin, targetRowMax;
737
738 if( !getCellBlockBounds( aSel, targetColMin, targetColMax, targetRowMin, targetRowMax ) )
739 return false;
740
741 T_TABLE* targetTable = static_cast<T_TABLE*>( static_cast<T_TABLECELL*>( aSel[0] )->GetParent() );
742
743 // Get source table dimensions
744 int sourceRows = aSourceTable->GetRowCount();
745 int sourceCols = aSourceTable->GetColCount();
746
747 int pasteEndRow = targetRowMin + sourceRows;
748 int pasteEndCol = targetColMin + sourceCols;
749
750 if( pasteEndRow > targetTable->GetRowCount() || pasteEndCol > targetTable->GetColCount() )
751 {
752 int rowsToAdd = std::max( 0, pasteEndRow - targetTable->GetRowCount() );
753 int colsToAdd = std::max( 0, pasteEndCol - targetTable->GetColCount() );
754
755 VECTOR2I pos = targetTable->GetPosition();
756 aCommit.Modify( targetTable, getScreen() );
757
758 for( int i = 0; i < rowsToAdd; ++i )
759 {
760 int insertRow = targetTable->GetRowCount();
761 int clipboardRow = insertRow - targetRowMin;
762
763 std::vector<T_TABLECELL*> sources;
764 sources.reserve( aSourceTable->GetColCount() );
765
766 for( int col = 0; col < aSourceTable->GetColCount(); ++col )
767 sources.push_back( aSourceTable->GetCell( clipboardRow, col ) );
768
769 for( int col = 0; col < targetTable->GetColCount(); ++col )
770 {
771 T_TABLECELL* sourceCell = ( col < aSourceTable->GetColCount() ) ? sources[col] : sources[0];
772 T_TABLECELL* cell = copyCell( sourceCell );
773 targetTable->InsertCell( insertRow * targetTable->GetColCount(), cell );
774 }
775
776 for( int afterRow = targetTable->GetRowCount() - 1; afterRow > insertRow; afterRow-- )
777 targetTable->SetRowHeight( afterRow, targetTable->GetRowHeight( afterRow - 1 ) );
778
779 targetTable->SetRowHeight( insertRow, aSourceTable->GetRowHeight( clipboardRow ) );
780 }
781
782 for( int i = 0; i < colsToAdd; ++i )
783 {
784 int insertCol = targetTable->GetColCount();
785 int clipboardCol = insertCol - targetColMin;
786 int rowCount = targetTable->GetRowCount();
787
788 targetTable->SetColCount( targetTable->GetColCount() + 1 );
789
790 std::vector<T_TABLECELL*> sources;
791 sources.reserve( aSourceTable->GetRowCount() );
792
793 for( int row = 0; row < aSourceTable->GetRowCount(); ++row )
794 sources.push_back( aSourceTable->GetCell( row, clipboardCol ) );
795
796 for( int row = 0; row < rowCount; ++row )
797 {
798 T_TABLECELL* sourceCell = ( row < aSourceTable->GetRowCount() ) ? sources[row] : sources[0];
799 T_TABLECELL* cell = copyCell( sourceCell );
800 targetTable->InsertCell( row * targetTable->GetColCount() + insertCol, cell );
801 }
802
803 for( int afterCol = targetTable->GetColCount() - 1; afterCol > insertCol; afterCol-- )
804 targetTable->SetColWidth( afterCol, targetTable->GetColWidth( afterCol - 1 ) );
805
806 targetTable->SetColWidth( insertCol, aSourceTable->GetColWidth( clipboardCol ) );
807 }
808
809 targetTable->SetPosition( pos );
810 targetTable->Normalize();
811 }
812
813 for( int srcRow = 0; srcRow < sourceRows; ++srcRow )
814 {
815 for( int srcCol = 0; srcCol < sourceCols; ++srcCol )
816 {
817 int destRow = targetRowMin + srcRow;
818 int destCol = targetColMin + srcCol;
819
820 if( destRow >= targetTable->GetRowCount() || destCol >= targetTable->GetColCount() )
821 continue;
822
823 T_TABLECELL* sourceCell = aSourceTable->GetCell( srcRow, srcCol );
824 T_TABLECELL* targetCell = targetTable->GetCell( destRow, destCol );
825
826 aCommit.Modify( targetCell, getScreen() );
827
828 targetCell->SetText( sourceCell->GetText() );
829 targetCell->SetAttributes( *sourceCell, false );
830 targetCell->SetStroke( sourceCell->GetStroke() );
831 targetCell->SetFillMode( sourceCell->GetFillMode() );
832 targetCell->SetFillColor( sourceCell->GetFillColor() );
833 }
834 }
835
836 targetTable->Normalize();
837 return true;
838 }
839
844 virtual bool canEditTableStructure( const T_TABLE* aTable ) const { return true; }
845
849 bool selectionAllowsStructureEdits( const SELECTION& aSel ) const
850 {
851 for( EDA_ITEM* item : aSel )
852 {
853 T_TABLECELL* cell = dynamic_cast<T_TABLECELL*>( item );
854
855 if( cell && !canEditTableStructure( static_cast<const T_TABLE*>( cell->GetParent() ) ) )
856 return false;
857 }
858
859 return true;
860 }
861
862 virtual TOOL_MANAGER* getToolMgr() = 0;
863 virtual BASE_SCREEN* getScreen() = 0;
864
865 virtual const SELECTION& getTableCellSelection() = 0;
866 virtual void clearSelection() = 0;
867
868 virtual T_TABLECELL* copyCell( T_TABLECELL* aSource ) = 0;
869};
870
871#endif //EDIT_TABLE_TOOL_BASE_H
static TOOL_ACTION addRowAbove
Definition actions.h:101
static TOOL_ACTION addColBefore
Definition actions.h:103
static TOOL_ACTION deleteRows
Definition actions.h:105
static TOOL_ACTION addRowBelow
Definition actions.h:102
static TOOL_ACTION deleteColumns
Definition actions.h:106
static TOOL_ACTION unmergeCells
Definition actions.h:108
static TOOL_ACTION mergeCells
Definition actions.h:107
static TOOL_ACTION addColAfter
Definition actions.h:104
static TOOL_ACTION editTable
Definition actions.h:109
static TOOL_ACTION exportTableCSV
Definition actions.h:110
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
SCH_TABLE_EDIT_TOOL and PCB_TABLE_EDIT_TOOL share most of their algorithms, which are implemented her...
virtual TOOL_MANAGER * getToolMgr()=0
int doDeleteColumns(const TOOL_EVENT &aEvent)
int doAddColumnAfter(const TOOL_EVENT &aEvent)
int doAddRowBelow(const TOOL_EVENT &aEvent)
int doAddColumnBefore(const TOOL_EVENT &aEvent)
bool getCellBlockBounds(const SELECTION &aSel, int &aColMin, int &aColMax, int &aRowMin, int &aRowMax)
Get the bounding box of a cell block selection.
int doAddRowAbove(const TOOL_EVENT &aEvent)
int doMergeCells(const TOOL_EVENT &aEvent)
virtual const SELECTION & getTableCellSelection()=0
virtual BASE_SCREEN * getScreen()=0
bool validatePasteIntoSelection(const SELECTION &aSel, wxString &aErrorMsg)
Validate if paste-into-cells is possible for the given selection.
int doDeleteRows(const TOOL_EVENT &aEvent)
virtual T_TABLECELL * copyCell(T_TABLECELL *aSource)=0
virtual bool canEditTableStructure(const T_TABLE *aTable) const
False for a table whose rows, columns and cell text something else owns, such as one generated from t...
bool selectionAllowsStructureEdits(const SELECTION &aSel) const
True when no cell in aSel belongs to a table canEditTableStructure refuses.
void addMenus(CONDITIONAL_MENU &selToolMenu)
bool pasteCellsIntoSelection(const SELECTION &aSel, T_TABLE *aSourceTable, T_COMMIT &aCommit)
Paste text content from source table into selected cells.
virtual void clearSelection()=0
int doUnmergeCells(const TOOL_EVENT &aEvent)
static const TOOL_EVENT SelectedEvent
Definition actions.h:347
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.
int Size() const
Returns the number of selected parts.
Definition selection.h:120
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
Generic, UI-independent tool event.
Definition tool_event.h:167
Master controller class:
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
#define _(s)
#define STRUCT_DELETED
flag indication structures to be erased
const std::string table
@ SCH_TABLECELL_T
Definition typeinfo.h:162
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708