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"
22#include <wx/clipbrd.h>
23#include <wx/log.h>
24
25
27 GRID_TRICKS( aGrid )
28{
29 m_grid->Disconnect( wxEVT_CHAR_HOOK );
30 m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this );
31}
32
34 std::function<void( wxCommandEvent& )> aAddHandler ) :
35 GRID_TRICKS( aGrid, aAddHandler )
36{
37 m_grid->Disconnect( wxEVT_CHAR_HOOK );
38 m_grid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( LIB_TABLE_GRID_TRICKS::onCharHook ), nullptr, this );
39}
40
41
42void LIB_TABLE_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
43{
45 _( "Edit options..." ),
46 _( "Edit options for this library entry" ) );
47
48 menu.AppendSeparator();
49
50 bool showActivate = false;
51 bool showDeactivate = false;
52 bool showSetVisible = false;
53 bool showUnsetVisible = false;
54 LIB_TABLE_GRID* tbl = static_cast<LIB_TABLE_GRID*>( m_grid->GetTable() );
55
56 // Ensure selection parameters are up to date
58
59 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
60 {
61 if( tbl->GetValueAsBool( row, 0 ) )
62 showDeactivate = true;
63 else
64 showActivate = true;
65
66 if( tbl->GetValueAsBool( row, 1 ) )
67 showUnsetVisible = true;
68 else
69 showSetVisible = true;
70
71 if( showActivate && showDeactivate && showSetVisible && showUnsetVisible )
72 break;
73 }
74
75 if( showActivate )
76 menu.Append( LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED, _( "Activate selected" ) );
77
78 if( showDeactivate )
79 menu.Append( LIB_TABLE_GRID_TRICKS_DEACTIVATE_SELECTED, _( "Deactivate selected" ) );
80
82 {
83 if( showSetVisible )
84 menu.Append( LIB_TABLE_GRID_TRICKS_SET_VISIBLE, _( "Set visible flag" ) );
85
86 if( showUnsetVisible )
87 menu.Append( LIB_TABLE_GRID_TRICKS_UNSET_VISIBLE, _( "Unset visible flag" ) );
88 }
89
90 bool showSettings = false;
91
93 {
94 showSettings = true;
96 wxString::Format( _( "Library settings for %s..." ),
97 tbl->GetValue( m_sel_row_start, 2 ) ) );
98 }
99
100 if( showActivate || showDeactivate || showSetVisible || showUnsetVisible || showSettings )
101 menu.AppendSeparator();
102
103 GRID_TRICKS::showPopupMenu( menu, aEvent );
104}
105
106
107void LIB_TABLE_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
108{
109 int menu_id = event.GetId();
110 LIB_TABLE_GRID* tbl = (LIB_TABLE_GRID*) m_grid->GetTable();
111
113 {
114 optionsEditor( m_grid->GetGridCursorRow() );
115 }
118 {
119 bool selected_state = menu_id == LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED;
120
121 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
122 tbl->SetValueAsBool( row, 0, selected_state );
123
124 // Ensure the new state (on/off) of the widgets is immediately shown:
125 m_grid->Refresh();
126 }
127 else if( menu_id == LIB_TABLE_GRID_TRICKS_SET_VISIBLE
129 {
130 bool selected_state = menu_id == LIB_TABLE_GRID_TRICKS_SET_VISIBLE;
131
132 for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
133 tbl->SetValueAsBool( row, 1, selected_state );
134
135 // Ensure the new state (on/off) of the widgets is immediately shown:
136 m_grid->Refresh();
137 }
138 else if( menu_id == LIB_TABLE_GRID_TRICKS_LIBRARY_SETTINGS )
139 {
140 LIB_TABLE_ROW* row = tbl->At( m_sel_row_start );
141 row->Refresh();
142 row->ShowSettingsDialog( m_grid->GetParent() );
143 }
144 else
145 {
147 }
148}
150{
151 if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' && m_grid->IsCellEditControlShown() )
152 {
153 wxLogNull doNotLog;
154
155 if( wxTheClipboard->Open() )
156 {
157 if( wxTheClipboard->IsSupported( wxDF_TEXT ) || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
158 {
159 wxTextDataObject data;
160 wxTheClipboard->GetData( data );
161
162 wxString text = data.GetText();
163
164 if( !text.Contains( '\t' ) && text.Contains( ',' ) )
165 text.Replace( ',', '\t' );
166
167 if( text.Contains( '\t' ) || text.Contains( '\n' ) || text.Contains( '\r' ) )
168 {
169 m_grid->CancelPendingChanges();
170 int row = m_grid->GetGridCursorRow();
171
172 // Check if the current row already has data (has a nickname)
173 wxGridTableBase* table = m_grid->GetTable();
174 if( table && row >= 0 && row < table->GetNumberRows() )
175 {
176 // Check if the row has a nickname (indicating it has existing data)
177 wxString nickname = table->GetValue( row, COL_NICKNAME );
178 if( !nickname.IsEmpty() )
179 {
180 // Row already has data, don't allow pasting over it
181 wxTheClipboard->Close();
182 wxBell(); // Provide audio feedback
183 return;
184 }
185 }
186
187 m_grid->ClearSelection();
188 m_grid->SelectRow( row );
189 m_grid->SetGridCursor( row, 0 );
191 paste_text( text );
192 wxTheClipboard->Close();
193 m_grid->ForceRefresh();
194 return;
195 }
196 }
197
198 wxTheClipboard->Close();
199 }
200 }
201
203}
204
205
207{
208 if( aEvent.GetCol() == COL_OPTIONS )
209 {
210 optionsEditor( aEvent.GetRow() );
211 return true;
212 }
213
214 return false;
215}
216
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)
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
LIB_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()
virtual bool SupportsSettingsDialog() const
#define _(s)
@ COL_NICKNAME
@ COL_OPTIONS