KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_grid.h
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 (C) 2017-2021 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
20#ifndef __LIB_TABLE_GRID_H__
21#define __LIB_TABLE_GRID_H__
22
23#include <lib_table_base.h>
24#include <string_utils.h>
25#include <wx/grid.h>
26
28
29const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 );
30const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 );
31
34{
42
43 COL_COUNT // keep as last
44};
45
50class LIB_TABLE_GRID : public wxGridTableBase
51{
53
54public:
55
56 //-----<wxGridTableBase overloads>-------------------------------------------
57
58 int GetNumberRows() override { return (int) size(); }
59
60 int GetNumberCols() override { return COL_COUNT; }
61
62 wxString GetValue( int aRow, int aCol ) override
63 {
64 if( aRow < (int) size() )
65 {
66 const LIB_TABLE_ROW* r = at( (size_t) aRow );
67
68 switch( aCol )
69 {
70 case COL_NICKNAME: return UnescapeString( r->GetNickName() );
71 case COL_URI: return r->GetFullURI();
72 case COL_TYPE: return r->GetType();
73 case COL_OPTIONS: return r->GetOptions();
74 case COL_DESCR: return r->GetDescr();
75 case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
76 case COL_VISIBLE: return r->GetIsVisible() ? wxT( "1" ) : wxT( "0" );
77 default: return wxEmptyString;
78 }
79 }
80
81 return wxEmptyString;
82 }
83
84 bool GetValueAsBool( int aRow, int aCol ) override
85 {
86 if( aRow < (int) size() && aCol == COL_ENABLED )
87 return at( (size_t) aRow )->GetIsEnabled();
88 else if( aRow < (int) size() && aCol == COL_VISIBLE )
89 return at( (size_t) aRow )->GetIsVisible();
90 else
91 return false;
92 }
93
94 void SetValue( int aRow, int aCol, const wxString& aValue ) override
95 {
96 if( aRow < (int) size() )
97 {
98 LIB_TABLE_ROW* r = at( (size_t) aRow );
99
100 switch( aCol )
101 {
102 case COL_NICKNAME: r->SetNickName( EscapeString( aValue, CTX_LIBID ) ); break;
103 case COL_URI: r->SetFullURI( aValue ); break;
104 case COL_TYPE: r->SetType( aValue ); break;
105 case COL_OPTIONS: r->SetOptions( aValue ); break;
106 case COL_DESCR: r->SetDescr( aValue ); break;
107 case COL_ENABLED: r->SetEnabled( aValue == wxT( "1" ) ); break;
108 case COL_VISIBLE: r->SetVisible( aValue == wxT( "1" ) ); break;
109 }
110 }
111 }
112
113 void SetValueAsBool( int aRow, int aCol, bool aValue ) override
114 {
115 if( aRow < (int) size() && aCol == COL_ENABLED )
116 at( (size_t) aRow )->SetEnabled( aValue );
117 else if( aRow < (int) size() && aCol == COL_VISIBLE )
118 at( (size_t) aRow )->SetVisible( aValue );
119 }
120
121 bool IsEmptyCell( int aRow, int aCol ) override
122 {
123 return !GetValue( aRow, aCol );
124 }
125
126 bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
127 {
128 if( aPos < size() )
129 {
130 for( size_t i = 0; i < aNumRows; i++ )
131 {
132 insert( begin() + i, makeNewRow() );
133 }
134
135 // use the (wxGridStringTable) source Luke.
136 if( GetView() )
137 {
138 wxGridTableMessage msg( this,
139 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
140 aPos,
141 aNumRows );
142
143 GetView()->ProcessTableMessage( msg );
144 }
145
146 return true;
147 }
148
149 return false;
150 }
151
152 bool AppendRows( size_t aNumRows = 1 ) override
153 {
154 // do not modify aNumRows, original value needed for wxGridTableMessage below
155 for( int i = aNumRows; i; --i )
157
158 if( GetView() )
159 {
160 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
161 GetView()->ProcessTableMessage( msg );
162 }
163
164 return true;
165 }
166
167 bool DeleteRows( size_t aPos, size_t aNumRows ) override
168 {
169 // aPos may be a large positive, e.g. size_t(-1), and the sum of
170 // aPos+aNumRows may wrap here, so both ends of the range are tested.
171 if( aPos < size() && aPos + aNumRows <= size() )
172 {
173 LIB_TABLE_ROWS_ITER start = begin() + aPos;
174 erase( start, start + aNumRows );
175 if( GetView() )
176 {
177 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
178 GetView()->ProcessTableMessage( msg );
179 }
180 return true;
181 }
182
183 return false;
184 }
185
186 wxString GetColLabelValue( int aCol ) override
187 {
188 switch( aCol )
189 {
190 case COL_NICKNAME: return _( "Nickname" );
191 case COL_URI: return _( "Library Path" );
192
193 // keep this "Library Format" text fairly long so column is sized wide enough
194 case COL_TYPE: return _( "Library Format" );
195 case COL_OPTIONS: return _( "Options" );
196 case COL_DESCR: return _( "Description" );
197 case COL_ENABLED: return _( "Active" );
198 case COL_VISIBLE: return _( "Visible" );
199
200 default: return wxEmptyString;
201 }
202 }
203
204 bool ContainsNickname( const wxString& aNickname )
205 {
206 for( size_t i = 0; i < size(); ++i )
207 {
208 LIB_TABLE_ROW* row = at( i );
209
210 if( row->GetNickName() == aNickname )
211 return true;
212 }
213 return false;
214 }
215
216 LIB_TABLE_ROW* At( size_t aIndex )
217 {
218 return at( aIndex );
219 }
220
221protected:
222 virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
223
224 virtual size_t size() const = 0;
225
226 virtual LIB_TABLE_ROW* makeNewRow() = 0;
227
229
231
232 virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
233
235};
236
237
238#endif // __LIB_TABLE_GRID_H__
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
virtual LIB_TABLE_ROWS_ITER erase(LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast)=0
virtual LIB_TABLE_ROWS_ITER insert(LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW *aRow)=0
void SetValueAsBool(int aRow, int aCol, bool aValue) override
virtual LIB_TABLE_ROW * at(size_t aIndex)=0
void SetValue(int aRow, int aCol, const wxString &aValue) override
bool IsEmptyCell(int aRow, int aCol) override
virtual size_t size() const =0
bool DeleteRows(size_t aPos, size_t aNumRows) override
bool AppendRows(size_t aNumRows=1) override
wxString GetValue(int aRow, int aCol) override
bool InsertRows(size_t aPos=0, size_t aNumRows=1) override
virtual LIB_TABLE_ROWS_ITER begin()=0
virtual void push_back(LIB_TABLE_ROW *aRow)=0
LIB_TABLE_ROW * At(size_t aIndex)
bool GetValueAsBool(int aRow, int aCol) override
bool ContainsNickname(const wxString &aNickname)
int GetNumberCols() override
virtual LIB_TABLE_ROW * makeNewRow()=0
int GetNumberRows() override
wxString GetColLabelValue(int aCol) override
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
const wxString & GetDescr() const
Return the description of the library referenced by this row.
void SetVisible(bool aVisible=true)
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
virtual const wxString GetType() const =0
Return the type of library represented by this row.
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
void SetDescr(const wxString &aDescr)
Change the description of the library referenced by this row.
virtual void SetType(const wxString &aType)=0
Change the type of library represented by this row that must be implemented in the derived object to ...
const wxString & GetNickName() const
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
bool GetIsEnabled() const
void SetOptions(const wxString &aOptions)
Change the library options strings.
bool GetIsVisible() const
#define _(s)
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
@ COL_DESCR
@ COL_VISIBLE
@ COL_NICKNAME
@ COL_OPTIONS
@ COL_COUNT
@ COL_ENABLED
@ COL_URI
@ COL_TYPE
const wxColour COLOUR_ROW_ENABLED(0, 0, 0)
const wxColour COLOUR_ROW_DISABLED(100, 100, 100)
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Definition: string_utils.h:54