KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_edit_table_tool.cpp
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#include <tool/tool_manager.h>
22#include <tools/pcb_actions.h>
23#include <collectors.h>
25#include <wx/filedlg.h>
26#include <wx/msgdlg.h>
27#include <fstream>
28#include <kiplatform/ui.h>
29
30
32 PCB_TOOL_BASE( "pcbnew.TableEditor" )
33{
34}
35
36
38{
40
42
43 return true;
44}
45
46
48{
49 // Use copy constructor to copy all formatting properties (font, colors, borders, etc.)
50 PCB_TABLECELL* cell = new PCB_TABLECELL( *aSource );
51
52 // Generate a new UUID to avoid duplicates (copy constructor preserves the old UUID)
53 cell->ResetUuidDirect();
54
55 // Clear text content - we only want the formatting, not the content
56 cell->SetText( wxEmptyString );
57
58 // Position will be set by the caller, but preserve size from source
59 cell->SetStart( aSource->GetStart() );
60 cell->SetEnd( aSource->GetEnd() );
61
62 return cell;
63}
64
65
67{
68 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
69
70 return selTool->RequestSelection(
71 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
72 {
73 // Iterate from the back so we don't have to worry about removals.
74 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
75 {
76 if( !dynamic_cast<PCB_TABLECELL*>( aCollector[i] ) )
77 aCollector.Remove( aCollector[i] );
78 }
79 } );
80}
81
82
87
88
90{
92 bool clearSelection = selection.IsHover();
93 PCB_TABLE* parentTable = nullptr;
94
95 for( EDA_ITEM* item : selection.Items() )
96 {
97 if( item->Type() != PCB_TABLECELL_T )
98 return 0;
99
100 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
101
102 if( !parentTable )
103 {
104 parentTable = table;
105 }
106 else if( parentTable != table )
107 {
108 parentTable = nullptr;
109 break;
110 }
111 }
112
113 // The frame picks the dialog, since a generated table's cell text is not the user's to edit
114 if( parentTable )
115 frame()->OnEditItemRequest( parentTable );
116
117 if( clearSelection )
119
120 return 0;
121}
122
123
125{
127 bool clearSelection = selection.IsHover();
128 PCB_TABLE* parentTable = nullptr;
129
130 // Find the table from the selection
131 for( EDA_ITEM* item : selection.Items() )
132 {
133 if( item->Type() != PCB_TABLECELL_T )
134 return 0;
135
136 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
137
138 if( !parentTable )
139 {
140 parentTable = table;
141 }
142 else if( parentTable != table )
143 {
144 parentTable = nullptr;
145 break;
146 }
147 }
148
149 if( !parentTable )
150 return 0;
151
152 // Show file save dialog
153 wxFileDialog saveDialog( frame(), _( "Export Table to CSV" ),
154 wxEmptyString, wxEmptyString,
155 _( "CSV files (*.csv)|*.csv" ),
156 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
157
159
160 if( saveDialog.ShowModal() == wxID_CANCEL )
161 {
162 if( clearSelection )
164 return 0;
165 }
166
167 wxString filePath = saveDialog.GetPath();
168
169 // Ensure .csv extension
170 if( !filePath.Lower().EndsWith( ".csv" ) )
171 filePath += ".csv";
172
173 // Open file for writing
174 std::ofstream outFile( filePath.ToStdString() );
175
176 if( !outFile.is_open() )
177 {
178 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
179 _( "Export Error" ), wxOK | wxICON_ERROR, frame() );
180
181 if( clearSelection )
183 return 0;
184 }
185
186 // Helper function to escape CSV fields
187 auto escapeCSV = []( const wxString& field ) -> wxString
188 {
189 wxString escaped = field;
190
191 // If field contains comma, quote, or newline, wrap in quotes and escape quotes
192 if( escaped.Contains( ',' ) || escaped.Contains( '\"' ) || escaped.Contains( '\n' ) )
193 {
194 escaped.Replace( "\"", "\"\"" ); // Escape quotes by doubling them
195 escaped = "\"" + escaped + "\"";
196 }
197
198 return escaped;
199 };
200
201 // Export table data
202 for( int row = 0; row < parentTable->GetRowCount(); ++row )
203 {
204 for( int col = 0; col < parentTable->GetColCount(); ++col )
205 {
206 PCB_TABLECELL* cell = parentTable->GetCell( row, col );
207
208 // Get resolved text (with variables expanded)
209 wxString cellText = cell->GetShownText( FOR_CANVAS, 0 );
210
211 // Write escaped cell text
212 outFile << escapeCSV( cellText ).ToStdString();
213
214 // Add comma separator unless it's the last column
215 if( col < parentTable->GetColCount() - 1 )
216 outFile << ',';
217 }
218
219 // End of row
220 outFile << '\n';
221 }
222
223 outFile.close();
224
225 if( clearSelection )
227
228 return 0;
229}
230
231
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 selectionClear
Clear the current selection.
Definition actions.h:221
static TOOL_ACTION exportTableCSV
Definition actions.h:110
void ResetUuidDirect()
Definition board_item.h:292
int GetCount() const
Return the number of objects in the list.
Definition collector.h:79
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:107
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:334
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
int AddRowAbove(const TOOL_EVENT &aEvent)
int EditTable(const TOOL_EVENT &aEvent)
int DeleteRows(const TOOL_EVENT &aEvent)
int AddRowBelow(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
void setTransitions() override
Set up handlers for various events.
int UnmergeCells(const TOOL_EVENT &aEvent)
int AddColumnAfter(const TOOL_EVENT &aEvent)
int DeleteColumns(const TOOL_EVENT &aEvent)
int AddColumnBefore(const TOOL_EVENT &aEvent)
void clearSelection() override
const SELECTION & getTableCellSelection() override
int MergeCells(const TOOL_EVENT &aEvent)
int ExportTableToCSV(const TOOL_EVENT &aEvent)
PCB_TABLECELL * copyCell(PCB_TABLECELL *aSource) override
The selection tool: currently supports:
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter)
Return the current selection, filtered according to aClientFilter.
void SetEnd(const VECTOR2I &aEnd) override
void SetStart(const VECTOR2I &aStart) override
wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Return the string actually shown after processing of the base text.
int GetRowCount() const
Definition pcb_table.h:128
PCB_TABLECELL * GetCell(int aRow, int aCol) const
Definition pcb_table.h:155
int GetColCount() const
Definition pcb_table.h:126
T * frame() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
virtual bool Init() override
Init() is called once upon a registration of the tool.
const PCB_SELECTION & selection() const
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
Generic, UI-independent tool event.
Definition tool_event.h:167
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
TOOL_MENU & GetToolMenu()
CONDITIONAL_MENU & GetMenu()
Definition tool_menu.cpp:40
@ FOR_CANVAS
Definition common.h:92
#define _(s)
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
const std::string table
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708