KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_grid.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
20#include <lib_table_grid.h>
21#include <string_utils.h>
22
23
24wxString LIB_TABLE_GRID::GetValue( int aRow, int aCol )
25{
26 wxCHECK( aRow >= 0, wxEmptyString );
27 size_t row = static_cast<size_t>( aRow );
28
29 if( row < size() )
30 {
31 const LIBRARY_TABLE_ROW& r = at( row );
32
33 switch( aCol )
34 {
35 case COL_NICKNAME: return UnescapeString( r.Nickname() );
36 case COL_URI: return r.URI();
37 case COL_TYPE: return r.Type();
38 case COL_OPTIONS: return r.Options();
39 case COL_DESCR: return r.Description();
40 case COL_ENABLED: return r.Disabled() ? wxT( "0" ) : wxT( "1" );
41 case COL_VISIBLE: return r.Hidden() ? wxT( "0" ) : wxT( "1" );
42 case COL_STATUS: return r.ErrorDescription();
43 default: return wxEmptyString;
44 }
45 }
46
47 return wxEmptyString;
48}
49
50
51bool LIB_TABLE_GRID::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
52{
53 if( aRow < static_cast<int>( size() ) )
54 {
55 switch( aCol )
56 {
57 case COL_ENABLED:
58 case COL_VISIBLE:
59 return aTypeName == wxGRID_VALUE_BOOL;
60
61 default:
62 return aTypeName == wxGRID_VALUE_STRING;
63 }
64 }
65
66 return false;
67}
68
69
70bool LIB_TABLE_GRID::GetValueAsBool( int aRow, int aCol )
71{
72 wxCHECK( aRow >= 0, false );
73 size_t row = static_cast<size_t>( aRow );
74
75 if( row < size() && aCol == COL_ENABLED )
76 return !at( row ).Disabled();
77 else if( row < size() && aCol == COL_VISIBLE )
78 return !at( row ).Hidden();
79 else
80 return false;
81}
82
83
84void LIB_TABLE_GRID::SetValue( int aRow, int aCol, const wxString& aValue )
85{
86 wxCHECK( aRow >= 0, /* void */ );
87 size_t row = static_cast<size_t>( aRow );
88
89 if( row < size() )
90 {
91 LIBRARY_TABLE_ROW& r = at( row );
92
93 switch( aCol )
94 {
95 case COL_NICKNAME: r.SetNickname( EscapeString( aValue, CTX_LIBID ) ); break;
96 case COL_URI: r.SetURI( aValue ); break;
97 case COL_TYPE: r.SetType( aValue ); break;
98 case COL_OPTIONS: r.SetOptions( aValue ); break;
99 case COL_DESCR: r.SetDescription( aValue ); break;
100 case COL_ENABLED: r.SetDisabled( aValue == wxT( "0" ) ); break;
101 case COL_VISIBLE: r.SetHidden( aValue == wxT( "0" ) ); break;
102 case COL_STATUS: break;
103 }
104 }
105}
106
107void LIB_TABLE_GRID::SetValueAsBool( int aRow, int aCol, bool aValue )
108{
109 wxCHECK( aRow >= 0, /* void */ );
110 size_t row = static_cast<size_t>( aRow );
111
112 if( row < size() && aCol == COL_ENABLED )
113 at( row ).SetDisabled( !aValue );
114 else if( row < size() && aCol == COL_VISIBLE )
115 at( row ).SetHidden( !aValue );
116}
117
118
119bool LIB_TABLE_GRID::InsertRows( size_t aPos, size_t aNumRows )
120{
121 if( aPos < size() )
122 {
123 for( size_t i = 0; i < aNumRows; i++ )
124 {
125 insert( begin() + i, makeNewRow() );
126 }
127
128 // use the (wxGridStringTable) source Luke.
129 if( GetView() )
130 {
131 wxGridTableMessage msg( this,
132 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
133 aPos,
134 aNumRows );
135
136 GetView()->ProcessTableMessage( msg );
137 }
138
139 return true;
140 }
141
142 return false;
143}
144
145
146bool LIB_TABLE_GRID::AppendRows( size_t aNumRows )
147{
148 // do not modify aNumRows, original value needed for wxGridTableMessage below
149 for( int i = aNumRows; i; --i )
151
152 if( GetView() )
153 {
154 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
155 GetView()->ProcessTableMessage( msg );
156 }
157
158 return true;
159}
160
161
162bool LIB_TABLE_GRID::DeleteRows( size_t aPos, size_t aNumRows )
163{
164 // aPos may be a large positive, e.g. size_t(-1), and the sum of
165 // aPos+aNumRows may wrap here, so both ends of the range are tested.
166 if( aPos < size() && aPos + aNumRows <= size() )
167 {
168 LIBRARY_TABLE_ROWS_ITER start = begin() + aPos;
169 erase( start, start + aNumRows );
170
171 if( GetView() )
172 {
173 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
174 GetView()->ProcessTableMessage( msg );
175 }
176
177 return true;
178 }
179
180 return false;
181}
182
183
185{
186 switch( aCol )
187 {
188 case COL_NICKNAME: return _( "Nickname" );
189 case COL_URI: return _( "Library Path" );
190
191 // keep this "Library Format" text fairly long so column is sized wide enough
192 case COL_TYPE: return _( "Library Format" );
193 case COL_OPTIONS: return _( "Options" );
194 case COL_DESCR: return _( "Description" );
195 case COL_ENABLED: return _( "Enable" );
196 case COL_VISIBLE: return _( "Show" );
197 case COL_STATUS: return wxEmptyString;
198
199 default: return wxEmptyString;
200 }
201}
202
203
204bool LIB_TABLE_GRID::ContainsNickname( const wxString& aNickname )
205{
206 for( size_t i = 0; i < size(); ++i )
207 {
208 LIBRARY_TABLE_ROW& row = at( i );
209
210 if( row.Nickname() == aNickname )
211 return true;
212 }
213 return false;
214}
215
216
218{
219 return m_table.Rows().at( aIndex );
220}
221
222
224{
225 return m_table.Rows().size();
226}
227
228
230{
231 return m_table.MakeRow();
232}
233
234
236{
237 return m_table.Rows().begin();
238}
239
240
242 const LIBRARY_TABLE_ROW& aRow )
243{
244 return m_table.Rows().insert( aIterator, aRow );
245}
246
247
249{
250 m_table.Rows().push_back( aRow );
251}
252
253
256{
257 return m_table.Rows().erase( aFirst, aLast );
258}
void SetOptions(const wxString &aOptions)
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
bool Disabled() const
const wxString & ErrorDescription() const
void SetDescription(const wxString &aDescription)
const wxString & Type() const
void SetURI(const wxString &aUri)
const wxString & Description() const
void SetDisabled(bool aDisabled=true)
bool Hidden() const
void SetHidden(bool aHidden=true)
const wxString & URI() const
const wxString & Nickname() const
const wxString & Options() const
LIBRARY_TABLE m_table
Working copy of a table.
void SetValueAsBool(int aRow, int aCol, bool aValue) override
virtual LIBRARY_TABLE_ROWS_ITER erase(LIBRARY_TABLE_ROWS_ITER aFirst, LIBRARY_TABLE_ROWS_ITER aLast)
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
void SetValue(int aRow, int aCol, const wxString &aValue) override
bool DeleteRows(size_t aPos, size_t aNumRows) override
bool AppendRows(size_t aNumRows=1) override
virtual void push_back(const LIBRARY_TABLE_ROW &aRow)
wxString GetValue(int aRow, int aCol) override
bool InsertRows(size_t aPos=0, size_t aNumRows=1) override
virtual size_t size() const
virtual LIBRARY_TABLE_ROWS_ITER begin()
bool GetValueAsBool(int aRow, int aCol) override
bool ContainsNickname(const wxString &aNickname)
virtual LIBRARY_TABLE_ROWS_ITER insert(LIBRARY_TABLE_ROWS_ITER aIterator, const LIBRARY_TABLE_ROW &aRow)
virtual LIBRARY_TABLE_ROW & at(size_t aIndex)
virtual LIBRARY_TABLE_ROW makeNewRow()
wxString GetColLabelValue(int aCol) override
#define _(s)
@ COL_DESCR
@ COL_VISIBLE
@ COL_NICKNAME
@ COL_OPTIONS
@ COL_STATUS
@ COL_ENABLED
@ COL_URI
@ COL_TYPE
std::vector< LIBRARY_TABLE_ROW >::iterator LIBRARY_TABLE_ROWS_ITER
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