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() ? "1" : "";
72  default:
73  ; // fall thru to wxEmptyString
74  }
75  }
76 
77  return wxEmptyString;
78  }
79 
80  void SetValue( int aRow, int aCol, const wxString &aValue ) override
81  {
82  if( aRow < (int) size() )
83  {
84  LIB_TABLE_ROW* r = at( (size_t) aRow );
85 
86  switch( aCol )
87  {
88  case COL_NICKNAME: r->SetNickName( aValue ); break;
89  case COL_URI: r->SetFullURI( aValue ); break;
90  case COL_TYPE: r->SetType( aValue ); break;
91  case COL_OPTIONS: r->SetOptions( aValue ); break;
92  case COL_DESCR: r->SetDescr( aValue ); break;
93  case COL_ENABLED:
94  // Any non-empty string will set enabled to true
95  r->SetEnabled( !aValue.IsEmpty() );
96  break;
97  }
98  }
99  }
100 
101  bool IsEmptyCell( int aRow, int aCol ) override
102  {
103  return !GetValue( aRow, aCol );
104  }
105 
106  bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
107  {
108  if( aPos < size() )
109  {
110  for( size_t i = 0; i < aNumRows; i++ )
111  {
112  insert( begin() + i, makeNewRow() );
113  }
114 
115  // use the (wxGridStringTable) source Luke.
116  if( GetView() )
117  {
118  wxGridTableMessage msg( this,
119  wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
120  aPos,
121  aNumRows );
122 
123  GetView()->ProcessTableMessage( msg );
124  }
125 
126  return true;
127  }
128 
129  return false;
130  }
131 
132  bool AppendRows( size_t aNumRows = 1 ) override
133  {
134  // do not modify aNumRows, original value needed for wxGridTableMessage below
135  for( int i = aNumRows; i; --i )
136  push_back( makeNewRow() );
137 
138  if( GetView() )
139  {
140  wxGridTableMessage msg( this,
141  wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
142  aNumRows );
143 
144  GetView()->ProcessTableMessage( msg );
145  }
146 
147  return true;
148  }
149 
150  bool DeleteRows( size_t aPos, size_t aNumRows ) override
151  {
152  // aPos may be a large positive, e.g. size_t(-1), and the sum of
153  // aPos+aNumRows may wrap here, so both ends of the range are tested.
154  if( aPos < size() && aPos + aNumRows <= size() )
155  {
156  LIB_TABLE_ROWS_ITER start = begin() + aPos;
157  erase( start, start + aNumRows );
158 
159  if( GetView() )
160  {
161  wxGridTableMessage msg( this,
162  wxGRIDTABLE_NOTIFY_ROWS_DELETED,
163  aPos,
164  aNumRows );
165 
166  GetView()->ProcessTableMessage( msg );
167  }
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 "Plugin Type" text fairly long so column is sized wide enough
183  case COL_TYPE: return _( "Plugin Type" );
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 
196  virtual wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
197  {
198  auto* attr = wxGridTableBase::GetAttr( aRow, aCol, aKind );
199 
200  if( aRow < (int) size() )
201  {
202  if( !attr )
203  {
204  attr = new wxGridCellAttr();
205  }
206 
207  wxFont font;
208 
209  if( attr->HasFont() )
210  {
211  font = attr->GetFont();
212  }
213 
214  LIB_TABLE_ROW* r = at( (size_t) aRow );
215 
216  if( r && r->GetIsEnabled() )
217  {
218  font.SetStyle( wxFONTSTYLE_NORMAL );
219  attr->SetTextColour( COLOUR_ROW_ENABLED );
220  }
221  else
222  {
223  font.SetStyle( wxFONTSTYLE_ITALIC );
224  attr->SetTextColour( COLOUR_ROW_DISABLED );
225  }
226 
227  attr->SetFont( font );
228  }
229 
230  return attr;
231  }
232 
233 protected:
234  virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
235 
236  virtual size_t size() const = 0;
237 
238  virtual LIB_TABLE_ROW* makeNewRow() = 0;
239 
240  virtual LIB_TABLE_ROWS_ITER begin() = 0;
241 
242  virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
243 
244  virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
245 
247 };
248 
249 
250 #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.
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 SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
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
virtual wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Customize the appearance of LIB_TABLE_ROW entries.
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.