LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
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 
13 namespace 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 
63  int ModelIterator::operator- (const ModelIterator& other) const
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 
86  int ModelIterator::GetRow () const
87  {
88  return Row_;
89  }
90 
91  int ModelIterator::GetCol () const
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 }
LC::Util::operator!=
bool operator!=(const ModelIterator &left, const ModelIterator &right)
Definition: modeliterator.cpp:82
LC::Util::ModelIterator::operator++
ModelIterator & operator++()
Increments the traversable index and returns the modified iterator.
Definition: modeliterator.cpp:31
LC::Util::ModelIterator::operator+=
ModelIterator & operator+=(int count)
Adds the given count to the traversable index.
Definition: modeliterator.cpp:57
LC::Util
Definition: icoreproxy.h:33
LC::Util::ModelIterator::GetRow
int GetRow() const
Returns the current row.
Definition: modeliterator.cpp:92
LC::Util::ModelIterator::GetCol
int GetCol() const
Returns the current column.
Definition: modeliterator.cpp:97
LC::Util::ModelIterator
Provides an iterator-based API to a Qt model.
Definition: modeliterator.h:36
LC::Util::ModelIterator::operator-=
ModelIterator & operator-=(int count)
Subtracts the given count to the traversable index.
Definition: modeliterator.cpp:63
LC::Util::ModelIterator::operator*
QModelIndex operator*() const
Returns the index currently pointed by the iterator.
Definition: modeliterator.cpp:87
LC::Util::ModelIterator::operator-
int operator-(const ModelIterator &other) const
Computes the distance between this and another iterator.
Definition: modeliterator.cpp:69
LC::Util::ModelIterator::operator--
ModelIterator & operator--()
Decrements the traversable index and returns the modified iterator.
Definition: modeliterator.cpp:44
LC::Util::operator==
bool operator==(const ModelIterator &left, const ModelIterator &right)
Definition: modeliterator.cpp:74
LC::Util::ModelIterator::Direction::Rows
@ Rows
The model should be traversed by rows.
modeliterator.h
LC::Util::ModelIterator::Direction
Direction
The direction of traversal.
Definition: modeliterator.h:52
LC::Util::ModelIterator::Direction::Cols
@ Cols
The model should be traversed by columns.
LC::Util::ModelIterator::ModelIterator
ModelIterator(QAbstractItemModel *model, int row, int col=0, Direction dir=Direction::Rows, const QModelIndex &parent={})
Constructs an iterator.
Definition: modeliterator.cpp:21