LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
modeliterator.cpp
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#include "modeliterator.h"
10#include <QAbstractItemModel>
11#include <QtDebug>
12
13namespace LC::Util
14{
15 ModelIterator::ModelIterator (QAbstractItemModel *model,
16 int row, int col, ModelIterator::Direction dir, const QModelIndex& parent)
17 : Model_ (model)
18 , Parent_ (parent)
19 , Row_ (row)
20 , Col_ (col)
21 , Dir_ (dir)
22 {
23 }
24
26 {
27 ++GetIncrementable ();
28 return *this;
29 }
30
32 {
33 ModelIterator oldThis (*this);
34 ++GetIncrementable ();
35 return oldThis;
36 }
37
39 {
40 --GetIncrementable ();
41 return *this;
42 }
43
45 {
46 ModelIterator oldThis (*this);
47 --GetIncrementable ();
48 return oldThis;
49 }
50
52 {
53 GetIncrementable () += diff;
54 return *this;
55 }
56
58 {
59 GetIncrementable () -= diff;
60 return *this;
61 }
62
64 {
65 return GetIncrementable () - other.GetIncrementable ();
66 }
67
68 bool operator== (const ModelIterator& left, const ModelIterator& right)
69 {
70 return left.Row_ == right.Row_ &&
71 left.Col_ == right.Col_ &&
72 left.Model_ == right.Model_ &&
73 left.Parent_ == right.Parent_;
74 }
75
76 bool operator!= (const ModelIterator& left, const ModelIterator& right)
77 {
78 return !(left == right);
79 }
80
81 QModelIndex ModelIterator::operator*() const
82 {
83 return Model_->index (Row_, Col_, Parent_);
84 }
85
87 {
88 return Row_;
89 }
90
92 {
93 return Col_;
94 }
95
96 int& ModelIterator::GetIncrementable ()
97 {
98 switch (Dir_)
99 {
100 case Direction::Rows:
101 return Row_;
102 case Direction::Cols:
103 return Col_;
104 }
105
106 qWarning () << Q_FUNC_INFO
107 << "unknown direction";
108 return Row_;
109 }
110
111 int ModelIterator::GetIncrementable () const
112 {
113 switch (Dir_)
114 {
115 case Direction::Rows:
116 return Row_;
117 case Direction::Cols:
118 return Col_;
119 }
120
121 qWarning () << Q_FUNC_INFO
122 << "unknown direction";
123 return Row_;
124 }
125}
ModelIterator & operator-=(int count)
Subtracts the given count to the traversable index.
ModelIterator(QAbstractItemModel *model, int row, int col=0, Direction dir=Direction::Rows, const QModelIndex &parent={})
Constructs an iterator.
int operator-(const ModelIterator &other) const
Computes the distance between this and another iterator.
Direction
The direction of traversal.
@ Rows
The model should be traversed by rows.
@ Cols
The model should be traversed by columns.
int GetRow() const
Returns the current row.
ModelIterator & operator++()
Increments the traversable index and returns the modified iterator.
int GetCol() const
Returns the current column.
ModelIterator & operator+=(int count)
Adds the given count to the traversable index.
QModelIndex operator*() const
Returns the index currently pointed by the iterator.
ModelIterator & operator--()
Decrements the traversable index and returns the modified iterator.
bool operator!=(const ModelIterator &left, const ModelIterator &right)
bool operator==(const ModelIterator &left, const ModelIterator &right)