LeechCraft 0.6.70-18450-gabe19ee3b0
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 *Model_ = nullptr;
33 QModelIndex Parent_;
34
35 int Row_;
36 int Col_;
37 public:
40 enum class Direction
41 {
46 Rows,
47
52 Cols
53 };
54 private:
55 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
160 Direction GetDirection () const;
161 private:
162 int& GetIncrementable ();
163 int GetIncrementable () const;
164 };
165
166
168 {
170
171 struct Sentinel
172 {
173 // One past the end
174 int End_ = 0;
175 };
177
178 auto begin () const { return Begin_; }
179 auto end () const { return End_; }
180 };
181
182 bool UTIL_MODELS_API operator== (const ModelIterator& left, const ModelRange::Sentinel& right);
183
184 inline ModelRange AllModelRows (const QAbstractItemModel& model, int column = 0)
185 {
186 return ModelRange
187 {
188 .Begin_ = { &model, 0, column, ModelIterator::Direction::Rows },
189 .End_ = { model.rowCount () },
190 };
191 }
192
193 inline ModelRange ModelRows (const QAbstractItemModel& model, int from, int to, int column = 0)
194 {
195 return ModelRange
196 {
197 .Begin_ = { &model, from, column, ModelIterator::Direction::Rows },
198 .End_ = { to + 1 },
199 };
200 }
201}
202
203template<>
204struct std::iterator_traits<LC::Util::ModelIterator>
205{
206 typedef QModelIndex value_type;
207 typedef int difference_type;
208
209 typedef random_access_iterator_tag iterator_category;
210};
211
212static_assert (std::ranges::input_range<LC::Util::ModelRange>);
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.
auto operator==(const T &left, const T &right)
Definition common.h:21
#define UTIL_MODELS_API
ModelRange ModelRows(const QAbstractItemModel &model, int from, int to, int column=0)
bool operator==(const ModelIterator &left, const ModelIterator &right)
ModelRange AllModelRows(const QAbstractItemModel &model, int column=0)
Definition constants.h:15
const Sentinel End_
const ModelIterator Begin_