LeechCraft 0.6.70-17609-g3dde4097dd
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
modeliterator.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <QModelIndex>
12#include "modelsconfig.h"
13
14namespace LC::Util
15{
31 {
32 const QAbstractItemModel * const Model_;
33 const QModelIndex Parent_;
34
35 int Row_;
36 int Col_;
37 public:
40 enum class Direction
41 {
47
53 };
54 private:
55 const Direction Dir_;
56 public:
65 ModelIterator (const QAbstractItemModel *model, int row, int col = 0,
66 Direction dir = Direction::Rows, const QModelIndex& parent = {});
67
73 ModelIterator& operator++ ();
74
80 ModelIterator operator++ (int);
81
87 ModelIterator& operator-- ();
88
94 ModelIterator operator-- (int);
95
101 ModelIterator& operator+= (int count);
102
109 ModelIterator& operator-= (int count);
110
123 int operator- (const ModelIterator& other) const;
124
135 friend UTIL_MODELS_API bool operator== (const ModelIterator& left, const ModelIterator& right);
136
142 QModelIndex operator* () const;
143
150 int GetRow () const;
151
158 int GetCol () const;
159 private:
160 int& GetIncrementable ();
161 int GetIncrementable () const;
162 };
163
165 {
168
169 auto begin () const { return Begin_; }
170 auto end () const { return End_; }
171 };
172
173 inline ModelRange AllModelRows (const QAbstractItemModel& model, int column = 0)
174 {
175 return ModelRange
176 {
177 .Begin_ = { &model, 0, column, ModelIterator::Direction::Rows },
178 .End_ = { &model, model.rowCount (), column, ModelIterator::Direction::Rows },
179 };
180 }
181
182 inline ModelRange ModelRows (const QAbstractItemModel& model, int from, int to, int column = 0)
183 {
184 return ModelRange
185 {
186 .Begin_ = { &model, from, column, ModelIterator::Direction::Rows },
187 .End_ = { &model, to + 1, column, ModelIterator::Direction::Rows },
188 };
189 }
190}
191
192template<>
193struct std::iterator_traits<LC::Util::ModelIterator>
194{
195 typedef QModelIndex value_type;
196 typedef int difference_type;
197
198 typedef random_access_iterator_tag iterator_category;
199};
Provides an iterator-based API to a Qt model.
ModelIterator(const QAbstractItemModel *model, int row, int col=0, Direction dir=Direction::Rows, const QModelIndex &parent={})
Constructs an iterator.
Direction
The direction of traversal.
@ Rows
The model should be traversed by rows.
@ Cols
The model should be traversed by columns.
auto operator==(const T &left, const T &right)
Definition common.h:38
#define UTIL_MODELS_API
ModelRange ModelRows(const QAbstractItemModel &model, int from, int to, int column=0)
ModelRange AllModelRows(const QAbstractItemModel &model, int column=0)
Definition constants.h:15
const ModelIterator Begin_
const ModelIterator End_