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, 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
24#include <tool/tool_manager.h>
26#include <tools/pcb_actions.h>
27#include <collectors.h>
28#include <dialogs/dialog_table_properties.h>
30#include <wx/filedlg.h>
31#include <wx/msgdlg.h>
32#include <fstream>
33#include <kiplatform/ui.h>
34
35
37 PCB_TOOL_BASE( "pcbnew.TableEditor" )
38{
39}
40
41
43{
45
47
48 return true;
49}
50
51
53{
54 // Use copy constructor to copy all formatting properties (font, colors, borders, etc.)
55 PCB_TABLECELL* cell = new PCB_TABLECELL( *aSource );
56
57 // Generate a new UUID to avoid duplicates (copy constructor preserves the old UUID)
58 const_cast<KIID&>( cell->m_Uuid ) = KIID();
59
60 // Clear text content - we only want the formatting, not the content
61 cell->SetText( wxEmptyString );
62
63 // Position will be set by the caller, but preserve size from source
64 cell->SetStart( aSource->GetStart() );
65 cell->SetEnd( aSource->GetEnd() );
66
67 return cell;
68}
69
70
72{
73 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
74
75 return selTool->RequestSelection(
76 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
77 {
78 // Iterate from the back so we don't have to worry about removals.
79 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
80 {
81 if( !dynamic_cast<PCB_TABLECELL*>( aCollector[i] ) )
82 aCollector.Remove( aCollector[i] );
83 }
84 } );
85}
86
87
92
93
95{
97 bool clearSelection = selection.IsHover();
98 PCB_TABLE* parentTable = nullptr;
99
100 for( EDA_ITEM* item : selection.Items() )
101 {
102 if( item->Type() != PCB_TABLECELL_T )
103 return 0;
104
105 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
106
107 if( !parentTable )
108 {
109 parentTable = table;
110 }
111 else if( parentTable != table )
112 {
113 parentTable = nullptr;
114 break;
115 }
116 }
117
118 if( parentTable )
119 {
120 DIALOG_TABLE_PROPERTIES dlg( frame(), parentTable );
121
122 dlg.ShowQuasiModal(); // Scintilla's auto-complete requires quasiModal
123 }
124
125 if( clearSelection )
127
128 return 0;
129}
130
131
133{
135 bool clearSelection = selection.IsHover();
136 PCB_TABLE* parentTable = nullptr;
137
138 // Find the table from the selection
139 for( EDA_ITEM* item : selection.Items() )
140 {
141 if( item->Type() != PCB_TABLECELL_T )
142 return 0;
143
144 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
145
146 if( !parentTable )
147 {
148 parentTable = table;
149 }
150 else if( parentTable != table )
151 {
152 parentTable = nullptr;
153 break;
154 }
155 }
156
157 if( !parentTable )
158 return 0;
159
160 // Show file save dialog
161 wxFileDialog saveDialog( frame(), _( "Export Table to CSV" ),
162 wxEmptyString, wxEmptyString,
163 _( "CSV files (*.csv)|*.csv" ),
164 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
165
167
168 if( saveDialog.ShowModal() == wxID_CANCEL )
169 {
170 if( clearSelection )
172 return 0;
173 }
174
175 wxString filePath = saveDialog.GetPath();
176
177 // Ensure .csv extension
178 if( !filePath.Lower().EndsWith( ".csv" ) )
179 filePath += ".csv";
180
181 // Open file for writing
182 std::ofstream outFile( filePath.ToStdString() );
183
184 if( !outFile.is_open() )
185 {
186 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
187 _( "Export Error" ), wxOK | wxICON_ERROR, frame() );
188
189 if( clearSelection )
191 return 0;
192 }
193
194 // Helper function to escape CSV fields
195 auto escapeCSV = []( const wxString& field ) -> wxString
196 {
197 wxString escaped = field;
198
199 // If field contains comma, quote, or newline, wrap in quotes and escape quotes
200 if( escaped.Contains( ',' ) || escaped.Contains( '\"' ) || escaped.Contains( '\n' ) )
201 {
202 escaped.Replace( "\"", "\"\"" ); // Escape quotes by doubling them
203 escaped = "\"" + escaped + "\"";
204 }
205
206 return escaped;
207 };
208
209 // Export table data
210 for( int row = 0; row < parentTable->GetRowCount(); ++row )
211 {
212 for( int col = 0; col < parentTable->GetColCount(); ++col )
213 {
214 PCB_TABLECELL* cell = parentTable->GetCell( row, col );
215
216 // Get resolved text (with variables expanded)
217 wxString cellText = cell->GetShownText( false, 0 );
218
219 // Write escaped cell text
220 outFile << escapeCSV( cellText ).ToStdString();
221
222 // Add comma separator unless it's the last column
223 if( col < parentTable->GetColCount() - 1 )
224 outFile << ',';
225 }
226
227 // End of row
228 outFile << '\n';
229 }
230
231 outFile.close();
232
233 if( clearSelection )
235
236 return 0;
237}
238
239
static TOOL_ACTION addRowAbove
Definition actions.h:104
static TOOL_ACTION addColBefore
Definition actions.h:106
static TOOL_ACTION deleteRows
Definition actions.h:108
static TOOL_ACTION addRowBelow
Definition actions.h:105
static TOOL_ACTION deleteColumns
Definition actions.h:109
static TOOL_ACTION unmergeCells
Definition actions.h:111
static TOOL_ACTION mergeCells
Definition actions.h:110
static TOOL_ACTION addColAfter
Definition actions.h:107
static TOOL_ACTION editTable
Definition actions.h:112
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:224
static TOOL_ACTION exportTableCSV
Definition actions.h:113
int GetCount() const
Return the number of objects in the list.
Definition collector.h:83
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:111
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:521
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:216
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:178
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:174
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:220
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:282
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:207
Definition kiid.h:49
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.
wxString GetShownText(bool aAllowExtraText, int aDepth=0) const override
Return the string actually shown after processing of the base text.
int GetRowCount() const
Definition pcb_table.h:124
PCB_TABLECELL * GetCell(int aRow, int aCol) const
Definition pcb_table.h:149
int GetColCount() const
Definition pcb_table.h:122
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:171
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:44
#define _(s)
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:95
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695