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
34
36 PCB_TOOL_BASE( "pcbnew.TableEditor" )
37{
38}
39
40
42{
44
46
47 return true;
48}
49
50
52{
53 // Use copy constructor to copy all formatting properties (font, colors, borders, etc.)
54 PCB_TABLECELL* cell = new PCB_TABLECELL( *aSource );
55
56 // Generate a new UUID to avoid duplicates (copy constructor preserves the old UUID)
57 const_cast<KIID&>( cell->m_Uuid ) = KIID();
58
59 // Clear text content - we only want the formatting, not the content
60 cell->SetText( wxEmptyString );
61
62 // Position will be set by the caller, but preserve size from source
63 cell->SetStart( aSource->GetStart() );
64 cell->SetEnd( aSource->GetEnd() );
65
66 return cell;
67}
68
69
71{
72 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
73
74 return selTool->RequestSelection(
75 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
76 {
77 // Iterate from the back so we don't have to worry about removals.
78 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
79 {
80 if( !dynamic_cast<PCB_TABLECELL*>( aCollector[i] ) )
81 aCollector.Remove( aCollector[i] );
82 }
83 } );
84}
85
86
91
92
94{
96 bool clearSelection = selection.IsHover();
97 PCB_TABLE* parentTable = nullptr;
98
99 for( EDA_ITEM* item : selection.Items() )
100 {
101 if( item->Type() != PCB_TABLECELL_T )
102 return 0;
103
104 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
105
106 if( !parentTable )
107 {
108 parentTable = table;
109 }
110 else if( parentTable != table )
111 {
112 parentTable = nullptr;
113 break;
114 }
115 }
116
117 if( parentTable )
118 {
119 DIALOG_TABLE_PROPERTIES dlg( frame(), parentTable );
120
121 dlg.ShowQuasiModal(); // Scintilla's auto-complete requires quasiModal
122 }
123
124 if( clearSelection )
126
127 return 0;
128}
129
130
132{
134 bool clearSelection = selection.IsHover();
135 PCB_TABLE* parentTable = nullptr;
136
137 // Find the table from the selection
138 for( EDA_ITEM* item : selection.Items() )
139 {
140 if( item->Type() != PCB_TABLECELL_T )
141 return 0;
142
143 PCB_TABLE* table = static_cast<PCB_TABLE*>( item->GetParent() );
144
145 if( !parentTable )
146 {
147 parentTable = table;
148 }
149 else if( parentTable != table )
150 {
151 parentTable = nullptr;
152 break;
153 }
154 }
155
156 if( !parentTable )
157 return 0;
158
159 // Show file save dialog
160 wxFileDialog saveDialog( frame(), _( "Export Table to CSV" ),
161 wxEmptyString, wxEmptyString,
162 _( "CSV files (*.csv)|*.csv" ),
163 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
164
165 if( saveDialog.ShowModal() == wxID_CANCEL )
166 {
167 if( clearSelection )
169 return 0;
170 }
171
172 wxString filePath = saveDialog.GetPath();
173
174 // Ensure .csv extension
175 if( !filePath.Lower().EndsWith( ".csv" ) )
176 filePath += ".csv";
177
178 // Open file for writing
179 std::ofstream outFile( filePath.ToStdString() );
180
181 if( !outFile.is_open() )
182 {
183 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
184 _( "Export Error" ), wxOK | wxICON_ERROR, frame() );
185
186 if( clearSelection )
188 return 0;
189 }
190
191 // Helper function to escape CSV fields
192 auto escapeCSV = []( const wxString& field ) -> wxString
193 {
194 wxString escaped = field;
195
196 // If field contains comma, quote, or newline, wrap in quotes and escape quotes
197 if( escaped.Contains( ',' ) || escaped.Contains( '\"' ) || escaped.Contains( '\n' ) )
198 {
199 escaped.Replace( "\"", "\"\"" ); // Escape quotes by doubling them
200 escaped = "\"" + escaped + "\"";
201 }
202
203 return escaped;
204 };
205
206 // Export table data
207 for( int row = 0; row < parentTable->GetRowCount(); ++row )
208 {
209 for( int col = 0; col < parentTable->GetColCount(); ++col )
210 {
211 PCB_TABLECELL* cell = parentTable->GetCell( row, col );
212
213 // Get resolved text (with variables expanded)
214 wxString cellText = cell->GetShownText( false, 0 );
215
216 // Write escaped cell text
217 outFile << escapeCSV( cellText ).ToStdString();
218
219 // Add comma separator unless it's the last column
220 if( col < parentTable->GetColCount() - 1 )
221 outFile << ',';
222 }
223
224 // End of row
225 outFile << '\n';
226 }
227
228 outFile.close();
229
230 if( clearSelection )
232
233 return 0;
234}
235
236
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:516
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:215
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:177
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:173
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:219
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:278
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)
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:95
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695