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 <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" ),
120 wxEmptyString, wxEmptyString,
121 _( "CSV files (*.csv)|*.csv" ),
122 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
123
124 if( saveDialog.ShowModal() == wxID_CANCEL )
125 {
126 if( clearSelection )
128 return 0;
129 }
130
131 wxString filePath = saveDialog.GetPath();
132
133 // Ensure .csv extension
134 if( !filePath.Lower().EndsWith( ".csv" ) )
135 filePath += ".csv";
136
137 // Open file for writing
138 std::ofstream outFile( filePath.ToStdString() );
139
140 if( !outFile.is_open() )
141 {
142 wxMessageBox( wxString::Format( _( "Failed to open file:\n%s" ), filePath ),
143 _( "Export Error" ), wxOK | wxICON_ERROR, m_frame );
144
145 if( clearSelection )
147 return 0;
148 }
149
150 // Helper function to escape CSV fields
151 auto escapeCSV = []( const wxString& field ) -> wxString
152 {
153 wxString escaped = field;
154
155 // If field contains comma, quote, or newline, wrap in quotes and escape quotes
156 if( escaped.Contains( ',' ) || escaped.Contains( '\"' ) || escaped.Contains( '\n' ) )
157 {
158 escaped.Replace( "\"", "\"\"" ); // Escape quotes by doubling them
159 escaped = "\"" + escaped + "\"";
160 }
161
162 return escaped;
163 };
164
165 // Export table data
166 for( int row = 0; row < parentTable->GetRowCount(); ++row )
167 {
168 for( int col = 0; col < parentTable->GetColCount(); ++col )
169 {
170 SCH_TABLECELL* cell = parentTable->GetCell( row, col );
171
172 // Get resolved text (with variables expanded)
173 wxString cellText = cell->GetShownText( nullptr, &currentSheet, false, 0 );
174
175 // Write escaped cell text
176 outFile << escapeCSV( cellText ).ToStdString();
177
178 // Add comma separator unless it's the last column
179 if( col < parentTable->GetColCount() - 1 )
180 outFile << ',';
181 }
182
183 // End of row
184 outFile << '\n';
185 }
186
187 outFile.close();
188
189 if( clearSelection )
191
192 return 0;
193}
194
195
197{
198 // Use copy constructor to copy all formatting properties (font, colors, borders, etc.)
199 SCH_TABLECELL* cell = new SCH_TABLECELL( *aSource );
200
201 // Generate a new UUID to avoid duplicates (copy constructor preserves the old UUID)
202 const_cast<KIID&>( cell->m_Uuid ) = KIID();
203
204 // Clear text content - we only want the formatting, not the content
205 cell->SetText( wxEmptyString );
206
207 // Position will be set by the caller, but preserve size from source
208 cell->SetStart( aSource->GetStart() );
209 cell->SetEnd( aSource->GetEnd() );
210
211 return cell;
212}
213
214
216{
217 return m_selectionTool->RequestSelection( { SCH_TABLECELL_T } );
218}
219
220
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: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
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)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SCH_TABLECELL_T
Definition typeinfo.h:170