Project

General

Profile

Feature #3513 » WIdentityProxyModel.C

Anonymous, 08/04/2014 01:33 PM

 
1
#include "WIdentityProxyModel"
2

    
3
#include <assert.h>
4

    
5
#include <Wt/WReadOnlyProxyModel>
6
#include <Wt/WStandardItemModel>
7

    
8
namespace Wt {
9

    
10
class WIdentityProxyModel::DefaultModel : public WReadOnlyProxyModel {
11
public:
12
  DefaultModel() :
13
    empty_(this)
14
  {
15
    setSourceModel(&empty_);
16
  }
17
  static DefaultModel *cur() {
18
    if (!cur_)
19
      cur_ = new DefaultModel();
20
    return cur_;
21
  }
22
private:
23
  WStandardItemModel empty_;
24
  static DefaultModel *cur_;
25
};
26

    
27
WIdentityProxyModel::DefaultModel
28
  *WIdentityProxyModel::DefaultModel::cur_ = 0;
29

    
30
#define S() (sourceModel() ? sourceModel() : DefaultModel::cur())
31

    
32
WIdentityProxyModel::WIdentityProxyModel(WObject *parent) :
33
  WAbstractProxyModel(parent)
34
{
35
}
36

    
37
WIdentityProxyModel::~WIdentityProxyModel()
38
{
39
}
40

    
41
int WIdentityProxyModel::columnCount(const WModelIndex &parent) const
42
{
43
  return S()->columnCount(mapToSource(parent));
44
}
45

    
46
WModelIndex WIdentityProxyModel::index(int row, int column, const WModelIndex &parent) const
47
{
48
  if (!hasIndex(row, column, parent))
49
    return WModelIndex();
50
  const WModelIndex sourceParent = mapToSource(parent);
51
  const WModelIndex sourceIndex = S()->index(row, column, sourceParent);
52
  assert(sourceIndex.isValid());
53
  return mapFromSource(sourceIndex);
54
}
55

    
56
WModelIndex WIdentityProxyModel::mapFromSource(const WModelIndex &sourceIndex) const
57
{
58
  if (!sourceModel() || !sourceIndex.isValid())
59
    return WModelIndex();
60

    
61
  assert(sourceIndex.model() == sourceModel());
62
  return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
63
}
64

    
65
WModelIndex WIdentityProxyModel::mapToSource(const WModelIndex &proxyIndex) const
66
{
67
  if (!sourceModel() || !proxyIndex.isValid())
68
    return WModelIndex();
69
  assert(proxyIndex.model() == this);
70
  return createSourceIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer());
71
}
72

    
73
WModelIndex WIdentityProxyModel::parent(const WModelIndex &child) const
74
{
75
  assert(!child.isValid() || child.model() == this);
76
  const WModelIndex sourceIndex = mapToSource(child);
77
  const WModelIndex sourceParent = sourceIndex.parent();
78
  return mapFromSource(sourceParent);
79
}
80

    
81
int WIdentityProxyModel::rowCount(const WModelIndex &parent) const
82
{
83
  assert(!parent.isValid() || parent.model() == this);
84
  return S()->rowCount(mapToSource(parent));
85
}
86

    
87
void WIdentityProxyModel::setSourceModel(WAbstractItemModel *newSourceModel)
88
{
89
  if (sourceModel()) {
90
    for (unsigned int i = 0; i < modelConnections_.size(); ++i)
91
      modelConnections_[i].disconnect();
92
    modelConnections_.clear();
93
  }
94

    
95
  WAbstractProxyModel::setSourceModel(newSourceModel);
96

    
97
  if (newSourceModel) {
98

    
99
    modelConnections_.push_back(newSourceModel->rowsAboutToBeInserted().connect
100
      (this, &WIdentityProxyModel::sourceRowsAboutToBeInserted));
101
    modelConnections_.push_back(newSourceModel->rowsInserted().connect
102
      (this, &WIdentityProxyModel::sourceRowsInserted));
103
    modelConnections_.push_back(newSourceModel->rowsAboutToBeRemoved().connect
104
      (this, &WIdentityProxyModel::sourceRowsAboutToBeRemoved));
105
    modelConnections_.push_back(newSourceModel->rowsRemoved().connect
106
      (this, &WIdentityProxyModel::sourceRowsRemoved));
107
    modelConnections_.push_back(newSourceModel->columnsAboutToBeInserted().connect
108
      (this, &WIdentityProxyModel::sourceColumnsAboutToBeInserted));
109
    modelConnections_.push_back(newSourceModel->columnsInserted().connect
110
      (this, &WIdentityProxyModel::sourceColumnsInserted));
111
    modelConnections_.push_back(newSourceModel->columnsAboutToBeRemoved().connect
112
      (this, &WIdentityProxyModel::sourceColumnsAboutToBeRemoved));
113
    modelConnections_.push_back(newSourceModel->columnsRemoved().connect
114
      (this, &WIdentityProxyModel::sourceColumnsRemoved));
115
    modelConnections_.push_back(newSourceModel->modelReset().connect
116
      (this, &WIdentityProxyModel::sourceModelReset));
117
    modelConnections_.push_back(newSourceModel->dataChanged().connect
118
      (this, &WIdentityProxyModel::sourceDataChanged));
119
    modelConnections_.push_back(newSourceModel->headerDataChanged().connect
120
      (this, &WIdentityProxyModel::sourceHeaderDataChanged));
121
    modelConnections_.push_back(newSourceModel->layoutAboutToBeChanged().connect
122
      (this, &WIdentityProxyModel::sourceLayoutAboutToBeChanged));
123
    modelConnections_.push_back(newSourceModel->layoutChanged().connect
124
      (this, &WIdentityProxyModel::sourceLayoutChanged));
125
  }
126
}
127

    
128
bool WIdentityProxyModel::insertColumns(int column, int count, const WModelIndex &parent)
129
{
130
  assert(!parent.isValid() || parent.model() == this);
131
  return S()->insertColumns(column, count, mapToSource(parent));
132
}
133

    
134
bool WIdentityProxyModel::insertRows(int row, int count, const WModelIndex &parent)
135
{
136
  assert(!parent.isValid() || parent.model() == this);
137
  return S()->insertRows(row, count, mapToSource(parent));
138
}
139

    
140
bool WIdentityProxyModel::removeColumns(int column, int count, const WModelIndex &parent)
141
{
142
  assert(!parent.isValid() || parent.model() == this);
143
  return S()->removeColumns(column, count, mapToSource(parent));
144
}
145

    
146
bool WIdentityProxyModel::removeRows(int row, int count, const WModelIndex &parent)
147
{
148
  assert(!parent.isValid() || parent.model() == this);
149
  return S()->removeRows(row, count, mapToSource(parent));
150
}
151

    
152
void WIdentityProxyModel::sort(int column, SortOrder order)
153
{
154
  S()->sort(column, order);
155
}
156

    
157
boost::any WIdentityProxyModel::headerData(int section,
158
					    Orientation orientation,
159
					    int role) const
160
{
161
    return S()->headerData(section, orientation, role);
162
}
163

    
164
void WIdentityProxyModel::sourceColumnsAboutToBeInserted(const WModelIndex &parent, int start, int end)
165
{
166
  assert(!parent.isValid() || parent.model() == sourceModel());
167
  beginInsertColumns(mapFromSource(parent), start, end);
168
}
169

    
170
void WIdentityProxyModel::sourceColumnsAboutToBeRemoved(const WModelIndex &parent, int start, int end)
171
{
172
  assert(!parent.isValid() || parent.model() == sourceModel());
173
  beginRemoveColumns(mapFromSource(parent), start, end);
174
}
175

    
176
void WIdentityProxyModel::sourceColumnsInserted(const WModelIndex &parent, int start, int end)
177
{
178
  assert(!parent.isValid() || parent.model() == sourceModel());
179
  endInsertColumns();
180
}
181

    
182
void WIdentityProxyModel::sourceColumnsRemoved(const WModelIndex &parent, int start, int end)
183
{
184
  assert(!parent.isValid() || parent.model() == sourceModel());
185
  endRemoveColumns();
186
}
187

    
188
void WIdentityProxyModel::sourceRowsAboutToBeInserted(const WModelIndex &parent, int start, int end)
189
{
190
  assert(!parent.isValid() || parent.model() == sourceModel());
191
  beginInsertRows(mapFromSource(parent), start, end);
192
}
193

    
194
void WIdentityProxyModel::sourceRowsAboutToBeRemoved(const WModelIndex &parent, int start, int end)
195
{
196
  assert(!parent.isValid() || parent.model() == sourceModel());
197
  beginRemoveRows(mapFromSource(parent), start, end);
198
}
199

    
200
void WIdentityProxyModel::sourceRowsInserted(const WModelIndex &parent, int start, int end)
201
{
202
  assert(!parent.isValid() || parent.model() == sourceModel());
203
  endInsertRows();
204
}
205

    
206
void WIdentityProxyModel::sourceRowsRemoved(const WModelIndex &parent, int start, int end)
207
{
208
  assert(!parent.isValid() || parent.model() == sourceModel());
209
  endRemoveRows();
210
}
211

    
212
void WIdentityProxyModel::sourceDataChanged(const WModelIndex &topLeft, const WModelIndex &bottomRight)
213
{
214
  assert(!topLeft.isValid() || topLeft.model() == sourceModel());
215
  assert(!bottomRight.isValid() || bottomRight.model() == sourceModel());
216
  dataChanged().emit(mapFromSource(topLeft), mapFromSource(bottomRight));
217
}
218

    
219
void WIdentityProxyModel::sourceHeaderDataChanged(Orientation orientation, int start, int end)
220
{
221
  headerDataChanged().emit(orientation, start, end);
222
}
223

    
224
void WIdentityProxyModel::sourceLayoutAboutToBeChanged()
225
{
226
  layoutAboutToBeChanged().emit();
227
}
228

    
229
void WIdentityProxyModel::sourceLayoutChanged()
230
{
231
  layoutChanged().emit();
232
}
233

    
234
void WIdentityProxyModel::sourceModelReset()
235
{
236
  modelReset().emit();
237
}
238

    
239
} // namespace Wt
(2-2/2)