File: oxygenlistmodel.h

package info (click to toggle)
oxygen 4%3A5.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,660 kB
  • sloc: cpp: 21,511; python: 39; sh: 14; makefile: 7
file content (360 lines) | stat: -rw-r--r-- 11,104 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef ListModel_h
#define ListModel_h
//////////////////////////////////////////////////////////////////////////////
// listmodel.h
// -------------------
//
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////

#include "oxygenitemmodel.h"

#include <QSet>
#include <QList>

#include <algorithm>

namespace Oxygen
{
    //* Job model. Stores job information for display in lists
    template<class T> class ListModel : public ItemModel
    {

        public:

        //* value type
        using ValueType = T;

        //* reference
        using Reference = T&;

        //* pointer
        using Pointer = T*;

        //* value list and iterators
        using List = QList<ValueType>;
        using ListIterator = QListIterator<ValueType>;
        using MutableListIterator = QMutableListIterator<ValueType>;

        //* constructor
        ListModel(QObject *parent = nullptr):
            ItemModel( parent )
        {}

        //*@name methods reimplemented from base class
        //@{

        //* flags
        Qt::ItemFlags flags(const QModelIndex &index) const override
        {
            if (!index.isValid()) return Qt::NoItemFlags;
            return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        }

        //* unique index for given row, column and parent index
        QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
        {

            // check if index is valid
            if( !hasIndex( row, column, parent ) ) return QModelIndex();

            // return invalid index if parent is valid
            if( parent.isValid() ) return QModelIndex();

            // check against _values
            return ( row < (int) _values.size() ) ? createIndex( row, column ):QModelIndex();

        }

        //* index of parent
        QModelIndex parent(const QModelIndex &) const override
        { return QModelIndex(); }

        //* number of rows below given index
        int rowCount(const QModelIndex &parent = QModelIndex()) const override
        { return parent.isValid() ? 0:_values.size(); }

        //@}

        //*@name selection
        //@{

        //* clear internal list selected items
        void clearSelectedIndexes( void )
        { _selection.clear(); }

        //* store index internal selection state
        void setIndexSelected( const QModelIndex& index, bool value )
        {
            if( value ) _selection.push_back( get(index) );
            else _selection.erase( std::remove( _selection.begin(), _selection.end(), get(index) ), _selection.end() );
        }

        //* get list of internal selected items
        QModelIndexList selectedIndexes( void ) const
        {

            QModelIndexList out;
            for( typename List::const_iterator iter = _selection.begin(); iter != _selection.end(); iter++ )
            {
                QModelIndex index( ListModel::index( *iter ) );
                if( index.isValid() ) out.push_back( index );
            }
            return out;

        }

        //@}

        //*@name interface
        //@{

        //* add value
        void add( const ValueType& value )
        {

            emit layoutAboutToBeChanged();
            _add( value );
            privateSort();
            emit layoutChanged();

        }

        //* add values
        void add( const List& values )
        {

            // check if not empty
            // this avoids sending useless signals
            if( values.empty() ) return;

            emit layoutAboutToBeChanged();

            for( typename List::const_iterator iter = values.begin(); iter != values.end(); iter++ )
            { _add( *iter ); }

            privateSort();
            emit layoutChanged();

        }


        //* insert values
        void insert( const QModelIndex& index, const ValueType& value )
        {
            emit layoutAboutToBeChanged();
            _insert( index, value );
            emit layoutChanged();
        }

        //* insert values
        void insert( const QModelIndex& index, const List& values )
        {
            emit layoutAboutToBeChanged();

            // need to loop in reverse order so that the "values" ordering is preserved
            ListIterator iter( values );
            iter.toBack();
            while( iter.hasPrevious() )
            { _insert( index, iter.previous() ); }

            emit layoutChanged();

        }

        //* insert values
        void replace( const QModelIndex& index, const ValueType& value )
        {
            if( !index.isValid() ) add( value );
            else {
                emit layoutAboutToBeChanged();
                setIndexSelected( index, false );
                _values[index.row()] = value;
                setIndexSelected( index, true );
                emit layoutChanged();
            }
        }

        //* remove
        void remove( const ValueType& value )
        {

            emit layoutAboutToBeChanged();
            _remove( value );
            emit layoutChanged();
            return;

        }

        //* remove
        void remove( const List& values )
        {

            // check if not empty
            // this avoids sending useless signals
            if( values.empty() ) return;

            emit layoutAboutToBeChanged();
            for( typename List::const_iterator iter = values.begin(); iter != values.end(); iter++ )
            { _remove( *iter ); }
            emit layoutChanged();
            return;

        }

        //* clear
        void clear( void )
        { set( List() ); }

        //* update values from list
        /**
        values that are not found in current are removed
        new values are set to the end.
        This is slower than the "set" method, but the selection is not cleared in the process
        */
        void update( List values )
        {

            emit layoutAboutToBeChanged();

            // store values to be removed
            List removed_values;

            // update values that are common to both lists
            for( typename List::iterator iter = _values.begin(); iter != _values.end(); iter++ )
            {

                // see if iterator is in list
                typename List::iterator found_iter( std::find( values.begin(), values.end(), *iter ) );
                if( found_iter == values.end() ) removed_values.push_back( *iter );
                else {
                    *iter = *found_iter;
                    values.erase( found_iter );
                }

            }

            // remove values that have not been found in new list
            for( typename List::const_iterator iter = removed_values.constBegin(); iter != removed_values.constEnd(); iter++ )
            { _remove( *iter ); }

            // add remaining values
            for( typename List::const_iterator iter = values.constBegin(); iter != values.constEnd(); iter++ )
            { _add( *iter ); }

            privateSort();
            emit layoutChanged();

        }

        //* set all values
        void set( const List& values )
        {

            emit layoutAboutToBeChanged();
            _values = values;
            _selection.clear();
            privateSort();
            emit layoutChanged();

            return;
        }

        //* return all values
        const List& get( void ) const
        { return _values; }

        //* return value for given index
        ValueType get( const QModelIndex& index ) const
        { return (index.isValid() && index.row() < int(_values.size()) ) ? _values[index.row()]:ValueType(); }

        //* return value for given index
        ValueType& get( const QModelIndex& index )
        {
            Q_ASSERT( index.isValid() && index.row() < int( _values.size() ) );
            return _values[index.row()];
        }

        //* return all values
        List get( const QModelIndexList& indexes ) const
        {
            List out;
            for( QModelIndexList::const_iterator iter = indexes.begin(); iter != indexes.end(); iter++ )
            { if( iter->isValid() && iter->row() < int(_values.size()) ) out.push_back( get( *iter ) ); }
            return out;
        }

        //* return index associated to a given value
        QModelIndex index( const ValueType& value, int column = 0 ) const
        {
            for( int row = 0; row < _values.size(); ++row )
            { if( value == _values[row] ) return index( row, column ); }
            return QModelIndex();
        }

        //@}

        //* return true if model contains given index
        bool contains( const QModelIndex& index ) const
        { return index.isValid() && index.row() < _values.size(); }

        private:

        //* return all values
        List& _get( void )
        { return _values; }

        //* add, without update
        void _add( const ValueType& value )
        {
            typename List::iterator iter = std::find( _values.begin(), _values.end(), value );
            if( iter == _values.end() ) _values.push_back( value );
            else *iter = value;
        }

        //* add, without update
        void _insert( const QModelIndex& index, const ValueType& value )
        {
            if( !index.isValid() ) add( value );
            int row = 0;
            typename List::iterator iter( _values.begin() );
            for( ;iter != _values.end() && row != index.row(); iter++, row++ )
            {}

            _values.insert( iter, value );
        }

        //* remove, without update
        void _remove( const ValueType& value )
        {
            _values.erase( std::remove( _values.begin(), _values.end(), value ), _values.end() );
            _selection.erase( std::remove( _selection.begin(), _selection.end(), value ), _selection.end() );
        }

        //* values
        List _values;

        //* selection
        List _selection;

    };
}
#endif