KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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 <sch_edit_frame.h>
21#include <tool/tool_manager.h>
23#include <sch_actions.h>
26#include <wx/filedlg.h>
27#include <fstream>
28#include <kiplatform/ui.h>
29#include <sch_sheet_path.h>
30#include <wx/msgdlg.h>
31
36
37
39{
41
42 addMenus( m_selectionTool->GetToolMenu().GetMenu() );
43
44 return true;
45}
46
47
49{
50 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::EditableItems );
51 bool clearSelection = selection.IsHover();
52 SCH_TABLE* parentTable = nullptr;
53
54 for( EDA_ITEM* item : selection.Items() )
55 {
56 if( item->Type() != SCH_TABLECELL_T )
57 return 0;
58
59 SCH_TABLE* table = static_cast<SCH_TABLE*>( item->GetParent() );
60
61 if( !parentTable )
62 {
63 parentTable = table;
64 }
65 else if( parentTable != table )
66 {
67 parentTable = nullptr;
68 break;
69 }
70 }
71
72 if( parentTable )
73 {
74 DIALOG_TABLE_PROPERTIES dlg( m_frame, parentTable );
75
76 // QuasiModal required for Scintilla auto-complete
77 dlg.ShowQuasiModal();
78 }
79
80 if( clearSelection )
82
83 return 0;
84}
85
86
88{
89 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::EditableItems );
90 bool clearSelection = selection.IsHover();
91 SCH_TABLE* parentTable = nullptr;
92
93 // Find the table from the selection
94 for( EDA_ITEM* item : selection.Items() )
95 {
96 if( item->Type() != SCH_TABLECELL_T )
97 return 0;
98
99 SCH_TABLE* table = static_cast<SCH_TABLE*>( item->GetParent() );
100
101 if( !parentTable )
102 {
103 parentTable = table;
104 }
105 else if( parentTable != table )
106 {
107 parentTable = nullptr;
108 break;
109 }
110 }
111
112 if( !parentTable )
113 return 0;
114
115 // Get current sheet path for variable resolution
116 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
117
118 // Show file save dialog
119 wxFileDialog saveDialog( m_frame, _( "Export Table to CSV" ), wxEmptyString, wxEmptyString,
120 _( "CSV files (*.csv)|*.csv" ), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
121
123
124 if( saveDialog.ShowModal() == wxID_CANCEL )
125 {
126 if( clearSelection )
128
129 return 0;
130 }
131
132 wxString filePath = saveDialog.GetPath();
133
134 // Ensure .csv extension
135 if( !filePath.Lower().EndsWith( ".csv" ) )
136 filePath += ".csv";
137
138 // Open file for writing
139 std::ofstream outFile( filePath.ToStdString() );
140
141 if( !outFile.is_open() )
142 {
143 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
144 _( "Export Error" ), wxOK | wxICON_ERROR, m_frame );
145
146 if( clearSelection )
148
149 return 0;
150 }
151
152 // Helper function to escape CSV fields
153 auto escapeCSV =
154 []( const wxString& field ) -> wxString
155 {
156 wxString escaped = field;
157
158 // If field contains comma, quote, or newline, wrap in quotes and escape quotes
159 if( escaped.Contains( ',' ) || escaped.Contains( '\"' ) || escaped.Contains( '\n' ) )
160 {
161 escaped.Replace( "\"", "\"\"" ); // Escape quotes by doubling them
162 escaped = "\"" + escaped + "\"";
163 }
164
165 return escaped;
166 };
167
168 // Export table data
169 for( int row = 0; row < parentTable->GetRowCount(); ++row )
170 {
171 for( int col = 0; col < parentTable->GetColCount(); ++col )
172 {
173 SCH_TABLECELL* cell = parentTable->GetCell( row, col );
174
175 // Get resolved text (with variables expanded)
176 wxString cellText = cell->GetShownText( nullptr, &currentSheet, FOR_CANVAS, 0 );
177
178 // Write escaped cell text
179 outFile << escapeCSV( cellText ).ToStdString();
180
181 // Add comma separator unless it's the last column
182 if( col < parentTable->GetColCount() - 1 )
183 outFile << ',';
184 }
185
186 // End of row
187 outFile << '\n';
188 }
189
190 outFile.close();
191
192 if( clearSelection )
194
195 return 0;
196}
197
198
200{
201 // Use copy constructor to copy all formatting properties (font, colors, borders, etc.)
202 SCH_TABLECELL* cell = new SCH_TABLECELL( *aSource );
203
204 // Generate a new UUID to avoid duplicates (copy constructor preserves the old UUID)
205 const_cast<KIID&>( cell->m_Uuid ) = KIID();
206
207 // Clear text content - we only want the formatting, not the content
208 cell->SetText( wxEmptyString );
209
210 // Position will be set by the caller, but preserve size from source
211 cell->SetStart( aSource->GetStart() );
212 cell->SetEnd( aSource->GetEnd() );
213
214 return cell;
215}
216
217
219{
220 return m_selectionTool->RequestSelection( { SCH_TABLECELL_T } );
221}
222
223
static TOOL_ACTION addRowAbove
Definition actions.h:100
static TOOL_ACTION addColBefore
Definition actions.h:102
static TOOL_ACTION deleteRows
Definition actions.h:104
static TOOL_ACTION addRowBelow
Definition actions.h:101
static TOOL_ACTION deleteColumns
Definition actions.h:105
static TOOL_ACTION unmergeCells
Definition actions.h:107
static TOOL_ACTION mergeCells
Definition actions.h:106
static TOOL_ACTION addColAfter
Definition actions.h:103
static TOOL_ACTION editTable
Definition actions.h:108
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
static TOOL_ACTION exportTableCSV
Definition actions.h:109
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:599
virtual void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:329
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:325
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:275
virtual void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:279
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:236
Definition kiid.h:46
static const std::vector< KICAD_T > EditableItems
Schematic editor (Eeschema) main window.
void setTransitions() override
Set up handlers for various events.
int DeleteRows(const TOOL_EVENT &aEvent)
int ExportTableToCSV(const TOOL_EVENT &aEvent)
int MergeCells(const TOOL_EVENT &aEvent)
void clearSelection() override
int UnmergeCells(const TOOL_EVENT &aEvent)
int AddRowBelow(const TOOL_EVENT &aEvent)
int AddColumnBefore(const TOOL_EVENT &aEvent)
int EditTable(const TOOL_EVENT &aEvent)
const SELECTION & getTableCellSelection() override
int DeleteColumns(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
int AddColumnAfter(const TOOL_EVENT &aEvent)
int AddRowAbove(const TOOL_EVENT &aEvent)
SCH_TABLECELL * copyCell(SCH_TABLECELL *aSource) override
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString GetShownText(const RENDER_SETTINGS *aSettings, const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, int aDepth=0) const override
int GetColCount() const
Definition sch_table.h:119
SCH_TABLECELL * GetCell(int aRow, int aCol) const
Definition sch_table.h:148
int GetRowCount() const
Definition sch_table.h:121
bool Init() override
Init() is called once upon a registration of the tool.
SCH_TOOL_BASE(const std::string &aName)
SCH_SELECTION_TOOL * m_selectionTool
bool IsHover() const
Definition selection.h:85
std::deque< EDA_ITEM * > & Items()
Definition selection.h:181
Generic, UI-independent tool event.
Definition tool_event.h:167
void Go(int(SCH_EDIT_FRAME::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
@ FOR_CANVAS
Definition common.h:90
#define _(s)
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:533
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SCH_TABLECELL_T
Definition typeinfo.h:162