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 CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
85 {
86 if( aRow < (int) size() )
87 {
88 switch( aCol )
89 {
90 case COL_ENABLED:
91 case COL_VISIBLE:
92 return aTypeName == wxGRID_VALUE_BOOL;
93
94 default:
95 return aTypeName == wxGRID_VALUE_STRING;
96 }
97 }
98
99 return false;
100 }
101
102 bool GetValueAsBool( int aRow, int aCol ) override
103 {
104 if( aRow < (int) size() && aCol == COL_ENABLED )
105 return at( (size_t) aRow )->GetIsEnabled();
106 else if( aRow < (int) size() && aCol == COL_VISIBLE )
107 return at( (size_t) aRow )->GetIsVisible();
108 else
109 return false;
110 }
111
112 void SetValue( int aRow, int aCol, const wxString& aValue ) override
113 {
114 if( aRow < (int) size() )
115 {
116 LIB_TABLE_ROW* r = at( (size_t) aRow );
117
118 switch( aCol )
119 {
120 case COL_NICKNAME: r->SetNickName( EscapeString( aValue, CTX_LIBID ) ); break;
121 case COL_URI: r->SetFullURI( aValue ); break;
122 case COL_TYPE: r->SetType( aValue ); break;
123 case COL_OPTIONS: r->SetOptions( aValue ); break;
124 case COL_DESCR: r->SetDescr( aValue ); break;
125 case COL_ENABLED: r->SetEnabled( aValue == wxT( "1" ) ); break;
126 case COL_VISIBLE: r->SetVisible( aValue == wxT( "1" ) ); break;
127 }
128 }
129 }
130
131 void SetValueAsBool( int aRow, int aCol, bool aValue ) override
132 {
133 if( aRow < (int) size() && aCol == COL_ENABLED )
134 at( (size_t) aRow )->SetEnabled( aValue );
135 else if( aRow < (int) size() && aCol == COL_VISIBLE )
136 at( (size_t) aRow )->SetVisible( aValue );
137 }
138
139 bool IsEmptyCell( int aRow, int aCol ) override
140 {
141 return !GetValue( aRow, aCol );
142 }
143
144 bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
145 {
146 if( aPos < size() )
147 {
148 for( size_t i = 0; i < aNumRows; i++ )
149 {
150 insert( begin() + i, makeNewRow() );
151 }
152
153 // use the (wxGridStringTable) source Luke.
154 if( GetView() )
155 {
156 wxGridTableMessage msg( this,
157 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
158 aPos,
159 aNumRows );
160
161 GetView()->ProcessTableMessage( msg );
162 }
163
164 return true;
165 }
166
167 return false;
168 }
169
170 bool AppendRows( size_t aNumRows = 1 ) override
171 {
172 // do not modify aNumRows, original value needed for wxGridTableMessage below
173 for( int i = aNumRows; i; --i )
175
176 if( GetView() )
177 {
178 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
179 GetView()->ProcessTableMessage( msg );
180 }
181
182 return true;
183 }
184
185 bool DeleteRows( size_t aPos, size_t aNumRows ) override
186 {
187 // aPos may be a large positive, e.g. size_t(-1), and the sum of
188 // aPos+aNumRows may wrap here, so both ends of the range are tested.
189 if( aPos < size() && aPos + aNumRows <= size() )
190 {
191 LIB_TABLE_ROWS_ITER start = begin() + aPos;
192 erase( start, start + aNumRows );
193 if( GetView() )
194 {
195 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
196 GetView()->ProcessTableMessage( msg );
197 }
198 return true;
199 }
200
201 return false;
202 }
203
204 wxString GetColLabelValue( int aCol ) override
205 {
206 switch( aCol )
207 {
208 case COL_NICKNAME: return _( "Nickname" );
209 case COL_URI: return _( "Library Path" );
210
211 // keep this "Library Format" text fairly long so column is sized wide enough
212 case COL_TYPE: return _( "Library Format" );
213 case COL_OPTIONS: return _( "Options" );
214 case COL_DESCR: return _( "Description" );
215 case COL_ENABLED: return _( "Active" );
216 case COL_VISIBLE: return _( "Visible" );
217
218 default: return wxEmptyString;
219 }
220 }
221
222 bool ContainsNickname( const wxString& aNickname )
223 {
224 for( size_t i = 0; i < size(); ++i )
225 {
226 LIB_TABLE_ROW* row = at( i );
227
228 if( row->GetNickName() == aNickname )
229 return true;
230 }
231 return false;
232 }
233
234 LIB_TABLE_ROW* At( size_t aIndex )
235 {
236 return at( aIndex );
237 }
238
239protected:
240 virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
241
242 virtual size_t size() const = 0;
243
244 virtual LIB_TABLE_ROW* makeNewRow() = 0;
245
247
249
250 virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
251
253};
254
255
256#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
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
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