KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_grid_tricks.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include "lib_table_grid.h"
23#include <wx/clipbrd.h>
24#include <wx/log.h>
25
26
28 GRID_TRICKS( aGrid )
29{
30 m_grid->Disconnect( wxEVT_CHAR_HOOK );
31 m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this );
32}
33
35 std::function<void( wxCommandEvent& )> aAddHandler ) :
36 GRID_TRICKS( aGrid, aAddHandler )
37{
38 m_grid->Disconnect( wxEVT_CHAR_HOOK );
39 m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this );
40}
41
42
43void LIB_TABLE_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
44{
46 _( "Edit options..." ),
47 _( "Edit options for this library entry" ) );
48
49 menu.AppendSeparator();
50
51 bool showActivate = false;
52 bool showDeactivate = false;
53 bool showSetVisible = false;
54 bool showUnsetVisible = false;
55 LIB_TABLE_GRID* tbl = static_cast<LIB_TABLE_GRID*>( m_grid->GetTable() );
56
57 // Ensure selection parameters are up to date
59
60 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
61 {
62 if( tbl->GetValueAsBool( row, 0 ) )
63 showDeactivate = true;
64 else
65 showActivate = true;
66
67 if( tbl->GetValueAsBool( row, 1 ) )
68 showUnsetVisible = true;
69 else
70 showSetVisible = true;
71
72 if( showActivate && showDeactivate && showSetVisible && showUnsetVisible )
73 break;
74 }
75
76 if( showActivate )
77 menu.Append( LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED, _( "Activate selected" ) );
78
79 if( showDeactivate )
80 menu.Append( LIB_TABLE_GRID_TRICKS_DEACTIVATE_SELECTED, _( "Deactivate selected" ) );
81
83 {
84 if( showSetVisible )
85 menu.Append( LIB_TABLE_GRID_TRICKS_SET_VISIBLE, _( "Set visible flag" ) );
86
87 if( showUnsetVisible )
88 menu.Append( LIB_TABLE_GRID_TRICKS_UNSET_VISIBLE, _( "Unset visible flag" ) );
89 }
90
91 bool showSettings = false;
92
93 if( LIBRARY_MANAGER_ADAPTER* adapter = tbl->Adapter() )
94 {
95 wxString nickname = tbl->GetValue( m_sel_row_start, COL_NICKNAME );
96
97 if( m_sel_row_count == 1 && adapter->SupportsConfigurationDialog( nickname ) )
98 {
99 showSettings = true;
101 wxString::Format( _( "Library settings for %s..." ), nickname ) );
102 }
103 }
104
105 if( showActivate || showDeactivate || showSetVisible || showUnsetVisible || showSettings )
106 menu.AppendSeparator();
107
108 GRID_TRICKS::showPopupMenu( menu, aEvent );
109}
110
111
112void LIB_TABLE_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
113{
114 int menu_id = event.GetId();
115 LIB_TABLE_GRID* tbl = (LIB_TABLE_GRID*) m_grid->GetTable();
116
118 {
119 optionsEditor( m_grid->GetGridCursorRow() );
120 }
123 {
124 bool selected_state = menu_id == LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED;
125
126 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
127 tbl->SetValueAsBool( row, 0, selected_state );
128
129 // Ensure the new state (on/off) of the widgets is immediately shown:
130 m_grid->Refresh();
131 }
132 else if( menu_id == LIB_TABLE_GRID_TRICKS_SET_VISIBLE
134 {
135 bool selected_state = menu_id == LIB_TABLE_GRID_TRICKS_SET_VISIBLE;
136
137 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
138 tbl->SetValueAsBool( row, 1, selected_state );
139
140 // Ensure the new state (on/off) of the widgets is immediately shown:
141 m_grid->Refresh();
142 }
143 else if( menu_id == LIB_TABLE_GRID_TRICKS_LIBRARY_SETTINGS )
144 {
145 // TODO(JE) library tables
146#if 0
147 LIB_TABLE_ROW* row = tbl->At( m_sel_row_start );
148 row->Refresh();
149 row->ShowSettingsDialog( m_grid->GetParent() );
150#endif
151 }
152 else
153 {
155 }
156}
158{
159 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' && m_grid->IsCellEditControlShown() )
160 {
161 wxLogNull doNotLog;
162
163 if( wxTheClipboard->Open() )
164 {
165 if( wxTheClipboard->IsSupported( wxDF_TEXT ) || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
166 {
167 wxTextDataObject data;
168 wxTheClipboard->GetData( data );
169
170 wxString text = data.GetText();
171
172 if( !text.Contains( '\t' ) && text.Contains( ',' ) )
173 text.Replace( ',', '\t' );
174
175 if( text.Contains( '\t' ) || text.Contains( '\n' ) || text.Contains( '\r' ) )
176 {
177 m_grid->CancelPendingChanges();
178 int row = m_grid->GetGridCursorRow();
179
180 // Check if the current row already has data (has a nickname)
181 wxGridTableBase* table = m_grid->GetTable();
182 if( table && row >= 0 && row < table->GetNumberRows() )
183 {
184 // Check if the row has a nickname (indicating it has existing data)
185 wxString nickname = table->GetValue( row, COL_NICKNAME );
186 if( !nickname.IsEmpty() )
187 {
188 // Row already has data, don't allow pasting over it
189 wxTheClipboard->Close();
190 wxBell(); // Provide audio feedback
191 return;
192 }
193 }
194
195 m_grid->ClearSelection();
196 m_grid->SelectRow( row );
197 m_grid->SetGridCursor( row, 0 );
199 paste_text( text );
200 wxTheClipboard->Close();
201 m_grid->ForceRefresh();
202 return;
203 }
204 }
205
206 wxTheClipboard->Close();
207 }
208 }
209
211}
212
213
215{
216 if( aEvent.GetCol() == COL_OPTIONS )
217 {
218 optionsEditor( aEvent.GetRow() );
219 return true;
220 }
221
222 return false;
223}
224
virtual void paste_text(const wxString &cb_text)
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.
GRID_TRICKS(WX_GRID *aGrid)
virtual void doPopupSelection(wxCommandEvent &event)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
int m_sel_row_start
WX_GRID * m_grid
I don't own the grid, but he owns me.
int m_sel_row_count
void onCharHook(wxKeyEvent &event)
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
bool handleDoubleClick(wxGridEvent &aEvent) override
LIB_TABLE_GRID_TRICKS(WX_GRID *aGrid)
virtual void optionsEditor(int aRow)=0
virtual bool supportsVisibilityColumn()
void doPopupSelection(wxCommandEvent &event) override
void onCharHook(wxKeyEvent &ev)
void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent) override
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
void SetValueAsBool(int aRow, int aCol, bool aValue) override
wxString GetValue(int aRow, int aCol) override
LIBRARY_MANAGER_ADAPTER * Adapter() const
LIBRARY_TABLE_ROW & At(size_t aIndex)
bool GetValueAsBool(int aRow, int aCol) override
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
virtual void ShowSettingsDialog(wxWindow *aParent) const
virtual bool Refresh()
#define _(s)
@ COL_NICKNAME
@ COL_OPTIONS