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 
29 {
35  COL_COUNT // keep as last
36 };
37 
42 class LIB_TABLE_GRID : public wxGridTableBase
43 {
44 public:
45 
46  //-----<wxGridTableBase overloads>-------------------------------------------
47 
48  int GetNumberRows() override { return (int) size(); }
49 
50  int GetNumberCols() override { return COL_COUNT; }
51 
52  wxString GetValue( int aRow, int aCol ) override
53  {
54  if( aRow < (int) size() )
55  {
56  const LIB_TABLE_ROW* r = at( (size_t) aRow );
57 
58  switch( aCol )
59  {
60  case COL_NICKNAME: return r->GetNickName();
61  case COL_URI: return r->GetFullURI();
62  case COL_TYPE: return r->GetType();
63  case COL_OPTIONS: return r->GetOptions();
64  case COL_DESCR: return r->GetDescr();
65  default:
66  ; // fall thru to wxEmptyString
67  }
68  }
69 
70  return wxEmptyString;
71  }
72 
73  void SetValue( int aRow, int aCol, const wxString &aValue ) override
74  {
75  if( aRow < (int) size() )
76  {
77  LIB_TABLE_ROW* r = at( (size_t) aRow );
78 
79  switch( aCol )
80  {
81  case COL_NICKNAME: r->SetNickName( aValue ); break;
82  case COL_URI: r->SetFullURI( aValue ); break;
83  case COL_TYPE: r->SetType( aValue ); break;
84  case COL_OPTIONS: r->SetOptions( aValue ); break;
85  case COL_DESCR: r->SetDescr( aValue ); break;
86  }
87  }
88  }
89 
90  bool IsEmptyCell( int aRow, int aCol ) override
91  {
92  return !GetValue( aRow, aCol );
93  }
94 
95  bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
96  {
97  if( aPos < size() )
98  {
99  for( size_t i = 0; i < aNumRows; i++ )
100  {
101  insert( begin() + i, makeNewRow() );
102  }
103 
104  // use the (wxGridStringTable) source Luke.
105  if( GetView() )
106  {
107  wxGridTableMessage msg( this,
108  wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
109  aPos,
110  aNumRows );
111 
112  GetView()->ProcessTableMessage( msg );
113  }
114 
115  return true;
116  }
117 
118  return false;
119  }
120 
121  bool AppendRows( size_t aNumRows = 1 ) override
122  {
123  // do not modify aNumRows, original value needed for wxGridTableMessage below
124  for( int i = aNumRows; i; --i )
125  push_back( makeNewRow() );
126 
127  if( GetView() )
128  {
129  wxGridTableMessage msg( this,
130  wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
131  aNumRows );
132 
133  GetView()->ProcessTableMessage( msg );
134  }
135 
136  return true;
137  }
138 
139  bool DeleteRows( size_t aPos, size_t aNumRows ) override
140  {
141  // aPos may be a large positive, e.g. size_t(-1), and the sum of
142  // aPos+aNumRows may wrap here, so both ends of the range are tested.
143  if( aPos < size() && aPos + aNumRows <= size() )
144  {
145  LIB_TABLE_ROWS_ITER start = begin() + aPos;
146  erase( start, start + aNumRows );
147 
148  if( GetView() )
149  {
150  wxGridTableMessage msg( this,
151  wxGRIDTABLE_NOTIFY_ROWS_DELETED,
152  aPos,
153  aNumRows );
154 
155  GetView()->ProcessTableMessage( msg );
156  }
157 
158  return true;
159  }
160 
161  return false;
162  }
163 
164  wxString GetColLabelValue( int aCol ) override
165  {
166  switch( aCol )
167  {
168  case COL_NICKNAME: return _( "Nickname" );
169  case COL_URI: return _( "Library Path" );
170 
171  // keep this "Plugin Type" text fairly long so column is sized wide enough
172  case COL_TYPE: return _( "Plugin Type" );
173  case COL_OPTIONS: return _( "Options" );
174  case COL_DESCR: return _( "Description" );
175  default: return wxEmptyString;
176  }
177  }
178 
179 protected:
180  virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
181 
182  virtual size_t size() const = 0;
183 
184  virtual LIB_TABLE_ROW* makeNewRow() = 0;
185 
186  virtual LIB_TABLE_ROWS_ITER begin() = 0;
187 
188  virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
189 
190  virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
191 
193 };
194 
195 
196 #endif // __LIB_TABLE_GRID_H__
virtual LIB_TABLE_ROWS_ITER insert(LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW *aRow)=0
Class LIB_TABLE_ROW.
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
Function SetType.
virtual LIB_TABLE_ROW * at(size_t aIndex)=0
const wxString & GetOptions() const
Function GetOptions.
bool InsertRows(size_t aPos=0, size_t aNumRows=1) override
virtual const wxString GetType() const =0
Function GetType.
int GetNumberRows() override
const wxString GetFullURI(bool aSubstituted=false) const
Function GetFullURI.
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
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
Function GetDescr.
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
Function GetNickName.
void SetFullURI(const wxString &aFullURI)
Function SetFullURI.
void SetNickName(const wxString &aNickName)
Function SetNickName.
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)
Function SetOptions.
wxString GetColLabelValue(int aCol) override
void SetDescr(const wxString &aDescr)
Function SetDescr.