1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LIB_TABLE_GRID_H__
#define __LIB_TABLE_GRID_H__
#include <lib_table_base.h>
#include <wx/grid.h>
const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 );
const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 );
/// The library table grid column order is established by this sequence.
enum COL_ORDER
{
COL_ENABLED,
COL_NICKNAME,
COL_URI,
COL_TYPE,
COL_OPTIONS,
COL_DESCR,
COL_COUNT // keep as last
};
/**
* This abstract base class mixes any object derived from #LIB_TABLE into wxGridTableBase
* so the result can be used as any type of library table within wxGrid.
*/
class LIB_TABLE_GRID : public wxGridTableBase
{
public:
//-----<wxGridTableBase overloads>-------------------------------------------
int GetNumberRows() override { return (int) size(); }
int GetNumberCols() override { return COL_COUNT; }
wxString GetValue( int aRow, int aCol ) override
{
if( aRow < (int) size() )
{
const LIB_TABLE_ROW* r = at( (size_t) aRow );
switch( aCol )
{
case COL_NICKNAME: return r->GetNickName();
case COL_URI: return r->GetFullURI();
case COL_TYPE: return r->GetType();
case COL_OPTIONS: return r->GetOptions();
case COL_DESCR: return r->GetDescr();
// Render a boolean value as its text equivalent
case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
default:
; // fall thru to wxEmptyString
}
}
return wxEmptyString;
}
bool GetValueAsBool( int aRow, int aCol ) override
{
if( aRow < (int) size() && aCol == COL_ENABLED )
return at( (size_t) aRow )->GetIsEnabled();
else
return false;
}
void SetValue( int aRow, int aCol, const wxString &aValue ) override
{
if( aRow < (int) size() )
{
LIB_TABLE_ROW* r = at( (size_t) aRow );
switch( aCol )
{
case COL_NICKNAME: r->SetNickName( aValue ); break;
case COL_URI: r->SetFullURI( aValue ); break;
case COL_TYPE: r->SetType( aValue ); break;
case COL_OPTIONS: r->SetOptions( aValue ); break;
case COL_DESCR: r->SetDescr( aValue ); break;
case COL_ENABLED:
r->SetEnabled( aValue == wxT( "1" ) );
break;
}
}
}
void SetValueAsBool( int aRow, int aCol, bool aValue ) override
{
if( aRow < (int) size() && aCol == COL_ENABLED )
at( (size_t) aRow )->SetEnabled( aValue );
}
bool IsEmptyCell( int aRow, int aCol ) override
{
return !GetValue( aRow, aCol );
}
bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
{
if( aPos < size() )
{
for( size_t i = 0; i < aNumRows; i++ )
{
insert( begin() + i, makeNewRow() );
}
// use the (wxGridStringTable) source Luke.
if( GetView() )
{
wxGridTableMessage msg( this,
wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
aPos,
aNumRows );
GetView()->ProcessTableMessage( msg );
}
return true;
}
return false;
}
bool AppendRows( size_t aNumRows = 1 ) override
{
// do not modify aNumRows, original value needed for wxGridTableMessage below
for( int i = aNumRows; i; --i )
push_back( makeNewRow() );
if( GetView() )
{
wxGridTableMessage msg( this,
wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
aNumRows );
GetView()->ProcessTableMessage( msg );
}
return true;
}
bool DeleteRows( size_t aPos, size_t aNumRows ) override
{
// aPos may be a large positive, e.g. size_t(-1), and the sum of
// aPos+aNumRows may wrap here, so both ends of the range are tested.
if( aPos < size() && aPos + aNumRows <= size() )
{
LIB_TABLE_ROWS_ITER start = begin() + aPos;
erase( start, start + aNumRows );
if( GetView() )
{
wxGridTableMessage msg( this,
wxGRIDTABLE_NOTIFY_ROWS_DELETED,
aPos,
aNumRows );
GetView()->ProcessTableMessage( msg );
}
return true;
}
return false;
}
wxString GetColLabelValue( int aCol ) override
{
switch( aCol )
{
case COL_NICKNAME: return _( "Nickname" );
case COL_URI: return _( "Library Path" );
// keep this "Plugin Type" text fairly long so column is sized wide enough
case COL_TYPE: return _( "Plugin Type" );
case COL_OPTIONS: return _( "Options" );
case COL_DESCR: return _( "Description" );
case COL_ENABLED: return _( "Active" );
default: return wxEmptyString;
}
}
bool ContainsNickname( const wxString& aNickname )
{
for( size_t i = 0; i < size(); ++i )
{
LIB_TABLE_ROW* row = at( i );
if( row->GetNickName() == aNickname )
return true;
}
return false;
}
protected:
virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
virtual size_t size() const = 0;
virtual LIB_TABLE_ROW* makeNewRow() = 0;
virtual LIB_TABLE_ROWS_ITER begin() = 0;
virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
virtual LIB_TABLE_ROWS_ITER erase( LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast ) = 0;
};
#endif // __LIB_TABLE_GRID_H__
|