GroupListWidget Pile   version 0.0.1
The pile attempts to provide a widget that is capable of presenting
groupmodel.h
Go to the documentation of this file.
1 
9 #ifndef GUARD_GROUPMODEL_H_INCLUDE
10 #define GUARD_GROUPMODEL_H_INCLUDE
11 
12 #include <grouplistwidget/grouplistwidget-config.h>
13 #include <QVariant>
14 #include <QList>
15 #include <QObject>
16 #include <QVector>
17 
18 QT_BEGIN_NAMESPACE
19 class QAbstractItemModel;
20 QT_END_NAMESPACE
21 
22 class GroupSubModel;
23 
25 class ModelId : private QPair<int,Qt::ItemDataRole> {
26 public:
27  ModelId () : QPair<int,Qt::ItemDataRole>(-1, Qt::UserRole) {}
28  ModelId (int column, Qt::ItemDataRole role) : QPair<int,Qt::ItemDataRole>(column, role) {}
29  int column () const { return first; }
30  void setColumn (int value) { first = value; }
31  Qt::ItemDataRole role () const { return second; }
32  void setRole (Qt::ItemDataRole value) { second = value; }
33 };
34 
36 class GROUPLISTWIDGET_EXPORT GroupModel : public QObject {
37  Q_OBJECT
38 public:
39 
40 
41  enum { BaseColRole = Qt::UserRole + 1000 };
42 
45  Equal = 0,
46  Smaller = -1,
47  Larger = 1
48  };
49 
51  typedef ComparisionReslt (*Compare) (
52  GroupModel * model,
53  int column,
54  const QVariant & v1,
55  const QVariant & v2);
56 
57  /* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
58 
60  GroupModel (
61  QAbstractItemModel *model = NULL,
62  QObject * parent = NULL);
63 
65  virtual ~GroupModel();
66 
68  virtual void
69  setBaseModel (
70  QAbstractItemModel * model,
71  int grouping_col = -1,
72  int sorting_col = -1,
73  Qt::SortOrder group_dir = Qt::AscendingOrder,
74  Qt::SortOrder sort_dir = Qt::AscendingOrder);
75 
77  virtual QAbstractItemModel *
78  baseModel () const;
79 
81  QAbstractItemModel *
82  takeBaseModel ();
83 
85  virtual void
86  setPixmapColumn (
87  int column);
88 
90  virtual int
91  pixmapColumn () const;
92 
94  virtual void
95  setPixmapRole (
96  Qt::ItemDataRole role);
97 
99  virtual Qt::ItemDataRole
100  pixmapRole () const;
101 
103  virtual QPixmap
104  pixmap (
105  int row) const;
106 
107 
109  virtual void
111  const QVariant & value) {
112  user_data_ = value;
113  }
114 
116  virtual const QVariant &
117  userData () const {
118  return user_data_;
119  }
120 
121 
122 signals:
123 
125  void
126  modelAboutToBeReset ();
127 
129  void
130  modelReset ();
131 
132 public:
133 
135  static ComparisionReslt
136  defaultCompare (
137  GroupModel * model,
138  int column,
139  const QVariant & v1,
140  const QVariant & v2);
141 
142 protected:
143 
145  virtual QStringList
146  columnLabels (
147  const QList<int> & scols,
148  int * idx_crt,
149  int highlite) const;
150 
152  void
153  resetAllSubGroups ();
154 
155 
156  /* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
167 
169 public:
170 
171 
173  const ModelId &
174  label () const {
175  Q_ASSERT(additional_labels_.length () > 0);
176  return additional_labels_.at (0);
177  }
178 
180  int
181  labelCount () const {
182  return additional_labels_.count ();
183  }
184 
186  ModelId
187  label (int idx) const {
188  if ((idx < 0) || (idx > additional_labels_.length ())) {
189  return ModelId ();
190  } else {
191  return additional_labels_.at (idx);
192  }
193  }
194 
196  void
198  const ModelId & value) {
199  additional_labels_.append (value);
200  resetAllSubGroups ();
201  }
202 
204  void
206  int column, Qt::ItemDataRole role = Qt::DisplayRole) {
207  addLabel (ModelId (column, role));
208  }
209 
211  void
213  const ModelId & value) {
214  Q_ASSERT(additional_labels_.length () > 0);
215  additional_labels_[0] = value;
216  resetAllSubGroups ();
217  }
218 
220  void
222  const ModelId & value,
223  int idx) {
224  if ((idx < 0) || (idx > additional_labels_.length ())) {
225  return;
226  } else {
227  additional_labels_[idx] = value;
228  resetAllSubGroups ();
229  }
230  }
231 
233  void
235  int column,
236  Qt::ItemDataRole role = Qt::DisplayRole,
237  int idx = 0) {
238  setLabel (ModelId (column, role), idx);
239  }
240 
242  /* - - - - - - - - - - - - - - - - */
243 
244 
245  /* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
250 
252 public:
253 
255  virtual int
256  groupingColumn () const {
257  return group_.column ();
258  }
259 
261  virtual int
262  isGrouping () const {
263  return group_.column() != -1;
264  }
265 
267  virtual void
269  Qt::ItemDataRole role) {
270  group_.setRole (role);
271  }
272 
274  virtual Qt::ItemDataRole
275  groupingRole () const {
276  return group_.role ();
277  }
278 
280  virtual void
282  Qt::ItemDataRole role) {
283  group_label_role_ = role;
284  }
285 
287  virtual Qt::ItemDataRole
288  groupLabelRole () const {
289  return group_label_role_;
290  }
291 
293  virtual Qt::SortOrder
294  groupingDirection () const {
295  return group_dir_;
296  }
297 
299  virtual void
301  Compare value) {
302  if (value == NULL)
303  value = defaultCompare;
304  group_func_ = value;
305  }
306 
308  virtual Compare
309  groupingFunc () const {
310  return group_func_;
311  }
312 
314  virtual GroupSubModel *
315  group (
316  int idx) const;
317 
319  virtual int
320  groupCount () const {
321  return groups_.count();
322  }
323 
325  virtual QList<int>
326  groupingColumns () const;
327 
329  virtual QStringList
330  groupingColumnLabels (
331  int * idx_crt) const;
332 
333 
334 public slots:
335 
337  virtual bool
338  setGroupingColumn (
339  int column);
340 
342  virtual bool
344  return setGroupingColumn (sender()->property("columnIndex").toInt ());
345  }
346 
348  void
350  setGroupingColumn (-1);
351  }
352 
354  virtual void
356  Qt::SortOrder value) {
357  group_dir_ = value;
358  if (!supress_signals_)
359  emit groupingChanged (group_.column(), group_dir_);
360  }
361 
363  void
365  setGroupingDirection (Qt::AscendingOrder);
366  }
367 
369  void
371  setGroupingDirection (Qt::DescendingOrder);
372  }
373 
374 protected:
375 
377  virtual void
378  buildAllGroups();
379 
381  virtual void
382  buildNoGroupingGroup();
383 
385  virtual void
386  clearAllGroups ();
387 
388 signals:
389 
391  void
392  groupingChanged (
393  int column,
394  Qt::SortOrder);
395 
397  /* - - - - - - - - - - - - - - - - */
398 
399 
400 
401  /* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& */
406 
408 public:
409 
411  virtual int
412  sortingColumn () const {
413  return sort_.column();
414  }
415 
417  virtual int
418  isSorting () const {
419  return sort_.column() != -1;
420  }
421 
423  virtual void
425  Qt::ItemDataRole role) {
426  sort_.setRole (role);
427  if (!supress_signals_)
428  emit sortingChanged (sort_.column(), sort_dir_);
429  }
430 
432  virtual Qt::ItemDataRole
433  sortingRole () const {
434  return sort_.role ();
435  }
436 
437 
439  virtual Qt::SortOrder
440  sortingDirection () const {
441  return sort_dir_;
442  }
443 
445  virtual void
447  Compare value) {
448  if (value == NULL)
449  value = defaultCompare;
450  sort_func_ = value;
451  }
452 
454  virtual Compare
455  sortingFunc () const {
456  return sort_func_;
457  }
458 
460  virtual QList<int>
461  sortingColumns () const;
462 
464  virtual QStringList
465  sortingColumnLabels (
466  int * idx_crt) const;
467 
468 public slots:
469 
471  virtual bool
472  setSortingColumn (
473  int column);
474 
476  virtual bool
478  return setSortingColumn (sender()->property ("columnIndex").toInt ());
479  }
480 
482  void
484  setSortingColumn (-1);
485  }
486 
488  virtual void
489  setSortingDirection (
490  Qt::SortOrder value);
491 
493  void
495  setSortingDirection (Qt::AscendingOrder);
496  }
497 
499  void
501  setSortingDirection (Qt::DescendingOrder);
502  }
503 
504 protected:
505 
507  virtual void
508  performSorting ();
509 
511  virtual void
512  performUnsorting ();
513 
514 signals:
515 
517  void
518  sortingChanged (
519  int column,
520  Qt::SortOrder);
521 
523  /* - - - - - - - - - - - - - - - - */
524 
525 
526 private slots:
527 
528  void
529  baseModelDataChange (
530  const QModelIndex &topLeft,
531  const QModelIndex &bottomRight,
532  const QVector<int> &roles = QVector<int>());
533 
534 private:
535 
537  void
538  installBaseModel (
539  QAbstractItemModel * value);
540 
542  void
543  uninstallBaseModel (
544  bool do_delete = true);
545 
547  GroupSubModel *
548  groupFromBaseRow (
549  int base_row,
550  int * index_in_group);
551 
552 
553  QAbstractItemModel * m_base_;
555  ModelId pixmap_;
557  ModelId group_;
558  Qt::ItemDataRole group_label_role_;
559  Qt::SortOrder group_dir_;
560  Compare group_func_;
562  ModelId sort_;
563  Qt::SortOrder sort_dir_;
564  Compare sort_func_;
566  QVariant user_data_;
568  QList<GroupSubModel*> groups_;
569  bool supress_signals_;
571  QList<ModelId> additional_labels_;
572 }; // class GroupModel
573 
574 #endif // GUARD_GROUPMODEL_H_INCLUDE
virtual void setGroupingFunc(Compare value)
Sets the function used to assign records to groups.
Definition: groupmodel.h:300
virtual int groupingColumn() const
Retreive the column in base model that decides grouping.
Definition: groupmodel.h:256
void setLabel(const ModelId &value)
Set the column and role for main label.
Definition: groupmodel.h:212
void addLabel(int column, Qt::ItemDataRole role=Qt::DisplayRole)
Add the column and role for a label to be presented to the user.
Definition: groupmodel.h:205
virtual Qt::SortOrder sortingDirection() const
Retreive the direction of sorting.
Definition: groupmodel.h:440
void addLabel(const ModelId &value)
Add the column and role for an additional label to be presented to the user.
Definition: groupmodel.h:197
virtual void setGroupingRole(Qt::ItemDataRole role)
Sets the role in base model that decides grouping.
Definition: groupmodel.h:268
virtual Qt::ItemDataRole groupLabelRole() const
Retreive the role in base model that provides group label.
Definition: groupmodel.h:288
virtual int groupCount() const
Get the number of groups curently defined inside this model.
Definition: groupmodel.h:320
const ModelId & label() const
Column and role for main label.
Definition: groupmodel.h:174
virtual Compare groupingFunc() const
Retreive the function used to assign records to groups.
Definition: groupmodel.h:309
virtual void setSortingRole(Qt::ItemDataRole role)
Sets the role in base model that decides sorting.
Definition: groupmodel.h:424
virtual bool setGroupingColumnByProperty()
Sets the column in base model that decides grouping by reading columnIndex property of the sender...
Definition: groupmodel.h:343
void setSortingAscending()
Sets the direction of sorting.
Definition: groupmodel.h:494
void setLabel(int column, Qt::ItemDataRole role=Qt::DisplayRole, int idx=0)
Set the column and role for a label.
Definition: groupmodel.h:234
virtual void setSortingFunc(Compare value)
Sets the function used to sort records inside groups.
Definition: groupmodel.h:446
void removeSorting()
No sorting.
Definition: groupmodel.h:483
int labelCount() const
Number of labels.
Definition: groupmodel.h:181
ModelId label(int idx) const
Column and role for a label.
Definition: groupmodel.h:187
virtual void setGroupLabelRole(Qt::ItemDataRole role)
Sets the role in base model that provides group label.
Definition: groupmodel.h:281
virtual Compare sortingFunc() const
Retreive the function used to sort records inside groups.
Definition: groupmodel.h:455
void setLabel(const ModelId &value, int idx)
Set the column and role for a label.
Definition: groupmodel.h:221
ComparisionReslt
The result of comparing two values.
Definition: groupmodel.h:44
void removeGrouping()
No grouping.
Definition: groupmodel.h:349
void setSortingDescending()
Sets the direction of sorting.
Definition: groupmodel.h:500
virtual int sortingColumn() const
Retreive the column in base model that decides sorting.
Definition: groupmodel.h:412
void setGroupingAscending()
Sets the direction of grouping.
Definition: groupmodel.h:364
void setGroupingDescending()
Sets the direction of grouping.
Definition: groupmodel.h:370
virtual Qt::ItemDataRole sortingRole() const
Retreive the role in base model that decides sorting.
Definition: groupmodel.h:433
virtual int isGrouping() const
Tell if we're grouping the records or not.
Definition: groupmodel.h:262
Groups the column and the role for a specific task.
Definition: groupmodel.h:25
virtual void setGroupingDirection(Qt::SortOrder value)
Sets the direction of grouping.
Definition: groupmodel.h:355
virtual const QVariant & userData() const
Retreive the opaque data useful for the user.
Definition: groupmodel.h:117
A model representing a group that is used by the embedded lists.
Definition: groupsubmodel.h:25
virtual Qt::ItemDataRole groupingRole() const
Retreive the role in base model that decides grouping.
Definition: groupmodel.h:275
virtual void setUserData(const QVariant &value)
Sets some opaque data useful for the user.
Definition: groupmodel.h:110
virtual Qt::SortOrder groupingDirection() const
Retreive the direction of grouping.
Definition: groupmodel.h:294
virtual int isSorting() const
Tell if we're sorting the records inside groups or not.
Definition: groupmodel.h:418
A model that is to be installed into a GroupListWidget.
Definition: groupmodel.h:36
virtual bool setSortingColumnByProperty()
Sets the column in base model that decides sorting by reading columnIndex property of the sender...
Definition: groupmodel.h:477