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, 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 <sch_actions.h>
26#include <dialogs/dialog_table_properties.h>
27#include <wx/filedlg.h>
28#include <fstream>
29#include <kiplatform/ui.h>
30#include <sch_sheet_path.h>
31#include <wx/msgdlg.h>
32
37
38
40{
42
43 addMenus( m_selectionTool->GetToolMenu().GetMenu() );
44
45 return true;
46}
47
48
50{
51 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::EditableItems );
52 bool clearSelection = selection.IsHover();
53 SCH_TABLE* parentTable = nullptr;
54
55 for( EDA_ITEM* item : selection.Items() )
56 {
57 if( item->Type() != SCH_TABLECELL_T )
58 return 0;
59
60 SCH_TABLE* table = static_cast<SCH_TABLE*>( item->GetParent() );
61
62 if( !parentTable )
63 {
64 parentTable = table;
65 }
66 else if( parentTable != table )
67 {
68 parentTable = nullptr;
69 break;
70 }
71 }
72
73 if( parentTable )
74 {
75 DIALOG_TABLE_PROPERTIES dlg( m_frame, parentTable );
76
77 // QuasiModal required for Scintilla auto-complete
78 dlg.ShowQuasiModal();
79 }
80
81 if( clearSelection )
83
84 return 0;
85}
86
87
89{
90 SCH_SELECTION& selection = m_selectionTool->RequestSelection( SCH_COLLECTOR::EditableItems );
91 bool clearSelection = selection.IsHover();
92 SCH_TABLE* parentTable = nullptr;
93
94 // Find the table from the selection
95 for( EDA_ITEM* item : selection.Items() )
96 {
97 if( item->Type() != SCH_TABLECELL_T )
98 return 0;
99
100 SCH_TABLE* table = static_cast<SCH_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 if( !parentTable )
114 return 0;
115
116 // Get current sheet path for variable resolution
117 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
118
119 // Show file save dialog
120 wxFileDialog saveDialog( m_frame, _( "Export Table to CSV" ),
121 wxEmptyString, wxEmptyString,
122 _( "CSV files (*.csv)|*.csv" ),
123 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
124
126
127 if( saveDialog.ShowModal() == wxID_CANCEL )
128 {
129 if( clearSelection )
131 return 0;
132 }
133
134 wxString filePath = saveDialog.GetPath();
135
136 // Ensure .csv extension
137 if( !filePath.Lower().EndsWith( ".csv" ) )
138 filePath += ".csv";
139
140 // Open file for writing
141 std::ofstream outFile( filePath.ToStdString() );
142
143 if( !outFile.is_open() )
144 {
145 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
146 _( "Export Error" ), wxOK | wxICON_ERROR, m_frame );
147
148 if( clearSelection )
150 return 0;
151 }
152
153 // Helper function to escape CSV fields
154 auto escapeCSV = []( 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, false, 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: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
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
Definition kiid.h:49
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, bool aAllowExtraText, int aDepth=0) const override
int GetColCount() const
Definition sch_table.h:120
SCH_TABLECELL * GetCell(int aRow, int aCol) const
Definition sch_table.h:147
int GetRowCount() const
Definition sch_table.h:122
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:89
std::deque< EDA_ITEM * > & Items()
Definition selection.h:182
Generic, UI-independent tool event.
Definition tool_event.h:171
void Go(int(SCH_EDIT_FRAME::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
#define _(s)
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SCH_TABLECELL_T
Definition typeinfo.h:170