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