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 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 
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 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  // Render a boolean value as its text equivalent
71  case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
72  default:
73  ; // fall thru to 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
85  return false;
86  }
87 
88  void SetValue( int aRow, int aCol, const wxString &aValue ) override
89  {
90  if( aRow < (int) size() )
91  {
92  LIB_TABLE_ROW* r = at( (size_t) aRow );
93 
94  switch( aCol )
95  {
96  case COL_NICKNAME: r->SetNickName( aValue ); break;
97  case COL_URI: r->SetFullURI( aValue ); break;
98  case COL_TYPE: r->SetType( aValue ); break;
99  case COL_OPTIONS: r->SetOptions( aValue ); break;
100  case COL_DESCR: r->SetDescr( aValue ); break;
101  case COL_ENABLED:
102  r->SetEnabled( aValue == wxT( "1" ) );
103  break;
104  }
105  }
106  }
107 
108  void SetValueAsBool( int aRow, int aCol, bool aValue ) override
109  {
110  if( aRow < (int) size() && aCol == COL_ENABLED )
111  at( (size_t) aRow )->SetEnabled( aValue );
112  }
113 
114  bool IsEmptyCell( int aRow, int aCol ) override
115  {
116  return !GetValue( aRow, aCol );
117  }
118 
119  bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
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  bool AppendRows( size_t aNumRows = 1 ) override
146  {
147  // do not modify aNumRows, original value needed for wxGridTableMessage below
148  for( int i = aNumRows; i; --i )
149  push_back( makeNewRow() );
150 
151  if( GetView() )
152  {
153  wxGridTableMessage msg( this,
154  wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
155  aNumRows );
156 
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 
172  if( GetView() )
173  {
174  wxGridTableMessage msg( this,
175  wxGRIDTABLE_NOTIFY_ROWS_DELETED,
176  aPos,
177  aNumRows );
178 
179  GetView()->ProcessTableMessage( msg );
180  }
181 
182  return true;
183  }
184 
185  return false;
186  }
187 
188  wxString GetColLabelValue( int aCol ) override
189  {
190  switch( aCol )
191  {
192  case COL_NICKNAME: return _( "Nickname" );
193  case COL_URI: return _( "Library Path" );
194 
195  // keep this "Plugin Type" text fairly long so column is sized wide enough
196  case COL_TYPE: return _( "Plugin Type" );
197  case COL_OPTIONS: return _( "Options" );
198  case COL_DESCR: return _( "Description" );
199  case COL_ENABLED: return _( "Active" );
200 
201  default: return wxEmptyString;
202  }
203  }
204 
205 protected:
206  virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
207 
208  virtual size_t size() const = 0;
209 
210  virtual LIB_TABLE_ROW* makeNewRow() = 0;
211 
212  virtual LIB_TABLE_ROWS_ITER begin() = 0;
213 
214  virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
215 
216  virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
217 
219 };
220 
221 
222 #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
COL_ORDER
The library table grid column order is established by this sequence.
bool DeleteRows(size_t aPos, size_t aNumRows) override
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 ...
virtual LIB_TABLE_ROW * at(size_t aIndex)=0
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
bool InsertRows(size_t aPos=0, size_t aNumRows=1) override
virtual const wxString GetType() const =0
Return the type of library represented by this row.
int GetNumberRows() override
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...
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 wxColour COLOUR_ROW_DISABLED(100, 100, 100)
virtual void push_back(LIB_TABLE_ROW *aRow)=0
bool IsEmptyCell(int aRow, int aCol) override
int GetNumberCols() override
bool AppendRows(size_t aNumRows=1) override
const wxString & GetDescr() const
Return the description of the library referenced by this row.
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
const wxString & GetNickName() const
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
void SetValueAsBool(int aRow, int aCol, bool aValue) override
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
size_t i
Definition: json11.cpp:597
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
void SetOptions(const wxString &aOptions)
Change the library options strings.
bool GetIsEnabled() const
wxString GetColLabelValue(int aCol) override
void SetDescr(const wxString &aDescr)
Change the description of the library referenced by this row.