3
//////////////////////////////////////////////////////////////////////////////
7
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to
11
// deal in the Software without restriction, including without limitation the
12
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13
// sell copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26
//////////////////////////////////////////////////////////////////////////////
33
#include "oxygenitemmodel.h"
37
//! Job model. Stores job information for display in lists
38
template<class T> class ListModel : public ItemModel
53
typedef std::vector<ValueType> List;
56
typedef std::set<ValueType> Set;
59
ListModel(QObject *parent = 0):
67
//!@name methods reimplemented from base class
71
virtual Qt::ItemFlags flags(const QModelIndex &index) const
73
if (!index.isValid()) return 0;
74
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
77
//! unique index for given row, column and parent index
78
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
81
// check if index is valid
82
if( !hasIndex( row, column, parent ) ) return QModelIndex();
84
// return invalid index if parent is valid
85
if( parent.isValid() ) return QModelIndex();
87
// check against _values
88
return ( row < (int) _values.size() ) ? createIndex( row, column ):QModelIndex();
93
virtual QModelIndex parent(const QModelIndex &) const
94
{ return QModelIndex(); }
96
//! number of rows below given index
97
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const
98
{ return parent.isValid() ? 0:_values.size(); }
105
//! clear internal list selected items
106
virtual void clearSelectedIndexes( void )
107
{ _selection.clear(); }
109
//! store index internal selection state
110
virtual void setIndexSelected( const QModelIndex& index, bool value )
112
if( value ) _selection.push_back( get(index) );
113
else _selection.erase( std::remove( _selection.begin(), _selection.end(), get(index) ), _selection.end() );
116
//! get list of internal selected items
117
virtual QModelIndexList selectedIndexes( void ) const
121
for( typename List::const_iterator iter = _selection.begin(); iter != _selection.end(); iter++ )
123
QModelIndex index( ListModel::index( *iter ) );
124
if( index.isValid() ) out.push_back( index );
136
virtual void add( const ValueType& value )
139
emit layoutAboutToBeChanged();
142
emit layoutChanged();
147
virtual void add( const List& values )
150
// check if not empty
151
// this avoids sending useless signals
152
if( values.empty() ) return;
154
emit layoutAboutToBeChanged();
156
for( typename List::const_iterator iter = values.begin(); iter != values.end(); iter++ )
160
emit layoutChanged();
166
/*! this method uses a Set to add the values. It speeds up the updating of existing values */
167
virtual void add( Set values )
170
emit layoutAboutToBeChanged();
172
for( typename List::iterator iter = _values.begin(); iter != _values.end(); iter++ )
174
// see if current iterator is found in values set
175
typename Set::iterator found_iter( values.find( *iter ) );
176
if( found_iter != values.end() )
179
values.erase( found_iter );
183
// insert remaining values at the end
184
_values.insert( _values.end(), values.begin(), values.end() );
187
emit layoutChanged();
193
virtual void insert( const QModelIndex& index, const ValueType& value )
195
emit layoutAboutToBeChanged();
196
_insert( index, value );
197
emit layoutChanged();
201
virtual void insert( const QModelIndex& index, const List& values )
203
emit layoutAboutToBeChanged();
205
// need to loop in reverse order so that the "values" ordering is preserved
206
for( typename List::const_reverse_iterator iter = values.rbegin(); iter != values.rend(); iter++ )
207
_insert( index, *iter );
208
emit layoutChanged();
212
virtual void replace( const QModelIndex& index, const ValueType& value )
214
if( !index.isValid() ) add( value );
216
emit layoutAboutToBeChanged();
217
setIndexSelected( index, false );
218
_values[index.row()] = value;
219
setIndexSelected( index, true );
220
emit layoutChanged();
225
virtual void remove( const ValueType& value )
228
emit layoutAboutToBeChanged();
230
emit layoutChanged();
236
virtual void remove( const List& values )
239
// check if not empty
240
// this avoids sending useless signals
241
if( values.empty() ) return;
243
emit layoutAboutToBeChanged();
244
for( typename List::const_iterator iter = values.begin(); iter != values.end(); iter++ )
245
{ _remove( *iter ); }
246
emit layoutChanged();
252
virtual void clear( void )
255
//! update values from list
257
values that are not found in current are removed
258
new values are set to the end.
259
This is slower than the "set" method, but the selection is not cleared in the process
261
virtual void update( List values )
264
emit layoutAboutToBeChanged();
266
// store values to be removed
269
// update values that are common to both lists
270
for( typename List::iterator iter = _values.begin(); iter != _values.end(); iter++ )
273
// see if iterator is in list
274
typename List::iterator found_iter( std::find( values.begin(), values.end(), *iter ) );
275
if( found_iter == values.end() ) removed_values.push_back( *iter );
278
values.erase( found_iter );
283
// remove values that have not been found in new list
284
for( typename List::const_iterator iter = removed_values.begin(); iter != removed_values.end(); iter++ )
285
{ _remove( *iter ); }
287
// add remaining values
288
for( typename List::const_iterator iter = values.begin(); iter != values.end(); iter++ )
292
emit layoutChanged();
297
virtual void set( const List& values )
300
emit layoutAboutToBeChanged();
304
emit layoutChanged();
309
//! return all values
310
const List& get( void ) const
313
//! return value for given index
314
virtual ValueType get( const QModelIndex& index ) const
315
{ return (index.isValid() && index.row() < int(_values.size()) ) ? _values[index.row()]:ValueType(); }
317
//! return value for given index
318
virtual ValueType& get( const QModelIndex& index )
320
assert( index.isValid() && index.row() < int( _values.size() ) );
321
return _values[index.row()];
324
//! return all values
325
List get( const QModelIndexList& indexes ) const
328
for( QModelIndexList::const_iterator iter = indexes.begin(); iter != indexes.end(); iter++ )
329
{ if( iter->isValid() && iter->row() < int(_values.size()) ) out.push_back( get( *iter ) ); }
333
//! return index associated to a given value
334
virtual QModelIndex index( const ValueType& value, int column = 0 ) const
336
for( unsigned int row=0; row<_values.size(); row++ )
337
{ if( value == _values[row] ) return index( row, column ); }
338
return QModelIndex();
345
//! return all values
349
//! add, without update
350
virtual void _add( const ValueType& value )
352
typename List::iterator iter = std::find( _values.begin(), _values.end(), value );
353
if( iter == _values.end() ) _values.push_back( value );
357
//! add, without update
358
virtual void _insert( const QModelIndex& index, const ValueType& value )
360
if( !index.isValid() ) add( value );
362
typename List::iterator iter( _values.begin() );
363
for( ;iter != _values.end() && row != index.row(); iter++, row++ )
366
_values.insert( iter, value );
369
//! remove, without update
370
virtual void _remove( const ValueType& value )
372
_values.erase( std::remove( _values.begin(), _values.end(), value ), _values.end() );
373
_selection.erase( std::remove( _selection.begin(), _selection.end(), value ), _selection.end() );