KiCad PCB EDA Suite
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
27const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 );
28const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 );
29
32{
40
41 COL_COUNT // keep as last
42};
43
48class LIB_TABLE_GRID : public wxGridTableBase
49{
50public:
51
52 //-----<wxGridTableBase overloads>-------------------------------------------
53
54 int GetNumberRows() override { return (int) size(); }
55
56 int GetNumberCols() override { return COL_COUNT; }
57
58 wxString GetValue( int aRow, int aCol ) override
59 {
60 if( aRow < (int) size() )
61 {
62 const LIB_TABLE_ROW* r = at( (size_t) aRow );
63
64 switch( aCol )
65 {
66 case COL_NICKNAME: return UnescapeString( r->GetNickName() );
67 case COL_URI: return r->GetFullURI();
68 case COL_TYPE: return r->GetType();
69 case COL_OPTIONS: return r->GetOptions();
70 case COL_DESCR: return r->GetDescr();
71 case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
72 case COL_VISIBLE: return r->GetIsVisible() ? wxT( "1" ) : wxT( "0" );
73 default: return wxEmptyString;
74 }
75 }
76
77 return wxEmptyString;
78 }
79
80 bool GetValueAsBool( int aRow, int aCol ) override
81 {
82 if( aRow < (int) size() && aCol == COL_ENABLED )
83 return at( (size_t) aRow )->GetIsEnabled();
84 else if( aRow < (int) size() && aCol == COL_VISIBLE )
85 return at( (size_t) aRow )->GetIsVisible();
86 else
87 return false;
88 }
89
90 void SetValue( int aRow, int aCol, const wxString& aValue ) override
91 {
92 if( aRow < (int) size() )
93 {
94 LIB_TABLE_ROW* r = at( (size_t) aRow );
95
96 switch( aCol )
97 {
98 case COL_NICKNAME: r->SetNickName( EscapeString( aValue, CTX_LIBID ) ); break;
99 case COL_URI: r->SetFullURI( aValue ); break;
100 case COL_TYPE: r->SetType( aValue ); break;
101 case COL_OPTIONS: r->SetOptions( aValue ); break;
102 case COL_DESCR: r->SetDescr( aValue ); break;
103 case COL_ENABLED: r->SetEnabled( aValue == wxT( "1" ) ); break;
104 case COL_VISIBLE: r->SetVisible( aValue == wxT( "1" ) ); break;
105 }
106 }
107 }
108
109 void SetValueAsBool( int aRow, int aCol, bool aValue ) override
110 {
111 if( aRow < (int) size() && aCol == COL_ENABLED )
112 at( (size_t) aRow )->SetEnabled( aValue );
113 else if( aRow < (int) size() && aCol == COL_VISIBLE )
114 at( (size_t) aRow )->SetVisible( aValue );
115 }
116
117 bool IsEmptyCell( int aRow, int aCol ) override
118 {
119 return !GetValue( aRow, aCol );
120 }
121
122 bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
123 {
124 if( aPos < size() )
125 {
126 for( size_t i = 0; i < aNumRows; i++ )
127 {
128 insert( begin() + i, makeNewRow() );
129 }
130
131 // use the (wxGridStringTable) source Luke.
132 if( GetView() )
133 {
134 wxGridTableMessage msg( this,
135 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
136 aPos,
137 aNumRows );
138
139 GetView()->ProcessTableMessage( msg );
140 }
141
142 return true;
143 }
144
145 return false;
146 }
147
148 bool AppendRows( size_t aNumRows = 1 ) override
149 {
150 // do not modify aNumRows, original value needed for wxGridTableMessage below
151 for( int i = aNumRows; i; --i )
153
154 if( GetView() )
155 {
156 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
157 GetView()->ProcessTableMessage( msg );
158 }
159
160 return true;
161 }
162
163 bool DeleteRows( size_t aPos, size_t aNumRows ) override
164 {
165 // aPos may be a large positive, e.g. size_t(-1), and the sum of
166 // aPos+aNumRows may wrap here, so both ends of the range are tested.
167 if( aPos < size() && aPos + aNumRows <= size() )
168 {
169 LIB_TABLE_ROWS_ITER start = begin() + aPos;
170 erase( start, start + aNumRows );
171 if( GetView() )
172 {
173 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
174 GetView()->ProcessTableMessage( msg );
175 }
176 return true;
177 }
178
179 return false;
180 }
181
182 wxString GetColLabelValue( int aCol ) override
183 {
184 switch( aCol )
185 {
186 case COL_NICKNAME: return _( "Nickname" );
187 case COL_URI: return _( "Library Path" );
188
189 // keep this "Library Format" text fairly long so column is sized wide enough
190 case COL_TYPE: return _( "Library Format" );
191 case COL_OPTIONS: return _( "Options" );
192 case COL_DESCR: return _( "Description" );
193 case COL_ENABLED: return _( "Active" );
194 case COL_VISIBLE: return _( "Visible" );
195
196 default: return wxEmptyString;
197 }
198 }
199
200 bool ContainsNickname( const wxString& aNickname )
201 {
202 for( size_t i = 0; i < size(); ++i )
203 {
204 LIB_TABLE_ROW* row = at( i );
205
206 if( row->GetNickName() == aNickname )
207 return true;
208 }
209 return false;
210 }
211
212protected:
213 virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
214
215 virtual size_t size() const = 0;
216
217 virtual LIB_TABLE_ROW* makeNewRow() = 0;
218
220
222
223 virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
224
226};
227
228
229#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
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 SetVisible(bool aVisible=true)
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
const wxString & GetNickName() const
bool GetIsEnabled() const
bool GetIsVisible() const
#define _(s)
E_SERIE r
Definition: eserie.cpp:41
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:55