GroupListWidget Pile   version 0.0.1
The pile attempts to provide a widget that is capable of presenting
GroupModel Class Reference

A model that is to be installed into a GroupListWidget. More...

#include <groupmodel.h>

+ Inheritance diagram for GroupModel:

Public Types

enum  { BaseColRole = Qt::UserRole + 1000 }
 
enum  ComparisionReslt { Equal = 0, Smaller = -1, Larger = 1 }
 The result of comparing two values.
 
typedef ComparisionReslt(* Compare) (GroupModel *model, int column, const QVariant &v1, const QVariant &v2)
 Compare two QVariants.
 

Signals

void modelAboutToBeReset ()
 Before the actual changes are implemented.
 
void modelReset ()
 After the model was updated.
 

Public Member Functions

 GroupModel (QAbstractItemModel *model=NULL, QObject *parent=NULL)
 Default constructor. More...
 
virtual ~GroupModel ()
 Destructor.
 
virtual void setBaseModel (QAbstractItemModel *model, int grouping_col=-1, int sorting_col=-1, Qt::SortOrder group_dir=Qt::AscendingOrder, Qt::SortOrder sort_dir=Qt::AscendingOrder)
 Sets the user model. More...
 
virtual QAbstractItemModel * baseModel () const
 Retreive user model. More...
 
QAbstractItemModel * takeBaseModel ()
 Take ownership of user model. More...
 
virtual void setPixmapColumn (int column)
 Sets the column in base model that provides the image for the icon.
 
virtual int pixmapColumn () const
 Retreive the column in base model that provides the image for the icon.
 
virtual void setPixmapRole (Qt::ItemDataRole role)
 Sets the role in base model that provides the image for the icon.
 
virtual Qt::ItemDataRole pixmapRole () const
 Retreive the role in base model that provides the image for the icon.
 
virtual QPixmap pixmap (int row) const
 Retreive the pixmap for a particular row.
 
virtual void setUserData (const QVariant &value)
 Sets some opaque data useful for the user.
 
virtual const QVariant & userData () const
 Retreive the opaque data useful for the user.
 
Labels

The model can present one or more labels for each item. The methods in this group allow the user to manage the list of column:role pairs for each label.

Note
The standard, unenhanced view will only show the first label and will always use DisplayRole. Use GroupListDelegate to show all these labels.
const ModelIdlabel () const
 Column and role for main label.
 
int labelCount () const
 Number of labels.
 
ModelId label (int idx) const
 Column and role for a label.
 
void addLabel (const ModelId &value)
 Add the column and role for an additional label to be presented to the user.
 
void addLabel (int column, Qt::ItemDataRole role=Qt::DisplayRole)
 Add the column and role for a label to be presented to the user.
 
void setLabel (const ModelId &value)
 Set the column and role for main label.
 
void setLabel (const ModelId &value, int idx)
 Set the column and role for a label.
 
void setLabel (int column, Qt::ItemDataRole role=Qt::DisplayRole, int idx=0)
 Set the column and role for a label.
 

Static Public Member Functions

static ComparisionReslt defaultCompare (GroupModel *model, int column, const QVariant &v1, const QVariant &v2)
 Default implementation for comparision function. More...
 

Protected Member Functions

virtual QStringList columnLabels (const QList< int > &scols, int *idx_crt, int highlite) const
 Implementation used by sortingColumnLabels() and groupingColumnLabels ()
 
void resetAllSubGroups ()
 Reset all sub-groups models.
 

Grouping

All methods related to grouping are gathered in this section.

virtual int groupingColumn () const
 Retreive the column in base model that decides grouping.
 
virtual int isGrouping () const
 Tell if we're grouping the records or not.
 
virtual void setGroupingRole (Qt::ItemDataRole role)
 Sets the role in base model that decides grouping.
 
virtual Qt::ItemDataRole groupingRole () const
 Retreive the role in base model that decides grouping.
 
virtual void setGroupLabelRole (Qt::ItemDataRole role)
 Sets the role in base model that provides group label.
 
virtual Qt::ItemDataRole groupLabelRole () const
 Retreive the role in base model that provides group label.
 
virtual Qt::SortOrder groupingDirection () const
 Retreive the direction of grouping.
 
virtual void setGroupingFunc (Compare value)
 Sets the function used to assign records to groups.
 
virtual Compare groupingFunc () const
 Retreive the function used to assign records to groups.
 
virtual GroupSubModelgroup (int idx) const
 Get the group at a particular index.
 
virtual int groupCount () const
 Get the number of groups curently defined inside this model.
 
virtual QList< int > groupingColumns () const
 Retreive the list of columns where grouping can be applied.
 
virtual QStringList groupingColumnLabels (int *idx_crt) const
 Retreive the labels for all columns where grouping can be applied.
 
void groupingChanged (int column, Qt::SortOrder)
 The direction (NOT the column) of the grouping has changed.
 
virtual void buildAllGroups ()
 Constructs all groups. More...
 
virtual void buildNoGroupingGroup ()
 Special case when no grouping is enabled. More...
 
virtual void clearAllGroups ()
 Destroys all groups. More...
 
virtual bool setGroupingColumn (int column)
 Sets the column in base model that decides grouping. More...
 
virtual bool setGroupingColumnByProperty ()
 Sets the column in base model that decides grouping by reading columnIndex property of the sender.
 
void removeGrouping ()
 No grouping.
 
virtual void setGroupingDirection (Qt::SortOrder value)
 Sets the direction of grouping.
 
void setGroupingAscending ()
 Sets the direction of grouping.
 
void setGroupingDescending ()
 Sets the direction of grouping.
 

Sorting

All methods related to sorting are gathered in this section.

virtual int sortingColumn () const
 Retreive the column in base model that decides sorting.
 
virtual int isSorting () const
 Tell if we're sorting the records inside groups or not.
 
virtual void setSortingRole (Qt::ItemDataRole role)
 Sets the role in base model that decides sorting.
 
virtual Qt::ItemDataRole sortingRole () const
 Retreive the role in base model that decides sorting.
 
virtual Qt::SortOrder sortingDirection () const
 Retreive the direction of sorting.
 
virtual void setSortingFunc (Compare value)
 Sets the function used to sort records inside groups.
 
virtual Compare sortingFunc () const
 Retreive the function used to sort records inside groups.
 
virtual QList< int > sortingColumns () const
 Retreive the list of columns where sorting can be applied. More...
 
virtual QStringList sortingColumnLabels (int *idx_crt) const
 Retreive the labels for all columns where sorting can be applied.
 
void sortingChanged (int column, Qt::SortOrder)
 The column or direction of the sorting has changed.
 
virtual void performSorting ()
 Sorts the items in all groups. More...
 
virtual void performUnsorting ()
 Arranges the items in all groups according to their original order. More...
 
virtual bool setSortingColumn (int column)
 Sets the column in base model that decides sorting. More...
 
virtual bool setSortingColumnByProperty ()
 Sets the column in base model that decides sorting by reading columnIndex property of the sender.
 
void removeSorting ()
 No sorting.
 
virtual void setSortingDirection (Qt::SortOrder value)
 Sets the direction of sorting.
 
void setSortingAscending ()
 Sets the direction of sorting.
 
void setSortingDescending ()
 Sets the direction of sorting.
 

Detailed Description

A model that is to be installed into a GroupListWidget.

This class provides an intermediate layer between the model provided by the user and the actual widget. The class may be subclassed to get full control of the internal workings. However, most of the times, it is enough to provide a customizer that formats the data accodingly.

Internally, the groups are always sorted in ascending order. The group() method inspects the desired groupingDirection() and returns the result acordingly.

A number of signals are used to communicate with the widgets presenting the data:

  • modelAboutToBeReset() and modelReset() are raised both when the base model changes and when the grouping column changes; the widgets must respond by reconstructing the entire view.
  • groupingChanged() informs the widget that the order of the grouping should chnage; the view may simply reorder the groups without reconstructing everything.
  • sortingChanged() is raised when either the direction or the column used fr sorting changes; as the underlying GroupSubModel also raises signals for associated list widgets, the top level widget does not use this signal.

Constructor & Destructor Documentation

GroupModel::GroupModel ( QAbstractItemModel *  model = NULL,
QObject *  parent = NULL 
)

Default constructor.

Default constructor creates a usable instance that can be readly used with a list widget. The user still needs to install a model for the widget to present anything useful. Alternativelly, the user may provide a model to the constructor that will be installed by the constructor.

The instance owns the QAbstractItemModel that is destroyed in the destructor. To avoid the destructor the user may choose to takeUnderModel() before the destructor kicks in.

Member Function Documentation

QAbstractItemModel * GroupModel::baseModel ( ) const
virtual

Retreive user model.

Returns
installed model (may be NULL).
void GroupModel::buildAllGroups ( )
protectedvirtual

Constructs all groups.

The method asserts that there is a base model installed and that the grouping column has a value inside valid range.

void GroupModel::buildNoGroupingGroup ( )
protectedvirtual

Special case when no grouping is enabled.

The method asserts that there is a base model installed.

void GroupModel::clearAllGroups ( )
protectedvirtual

Destroys all groups.

The state at the end of this method is not appropriate for run-time. It should be followed by either buildNoGroupingGroup() or buildAllGroups().

GroupModel::ComparisionReslt GroupModel::defaultCompare ( GroupModel model,
int  column,
const QVariant &  v1,
const QVariant &  v2 
)
static

Default implementation for comparision function.

This default implementation expects the two types to be the same. Some of the types will be handled and others will throw an assertion error. Check the code to see supported types.

Parameters
modelthis instance
columnthe column where the values belong
v1the value for first row
row1first row
v2the value for second row
row2second row
Returns
the result of the comparision
void GroupModel::performSorting ( )
protectedvirtual

Sorts the items in all groups.

The method asserts that there is a base model installed.

void GroupModel::performUnsorting ( )
protectedvirtual

Arranges the items in all groups according to their original order.

The method asserts that there is a base model installed.

void GroupModel::setBaseModel ( QAbstractItemModel *  model,
int  grouping_col = -1,
int  sorting_col = -1,
Qt::SortOrder  group_dir = Qt::AscendingOrder,
Qt::SortOrder  sort_dir = Qt::AscendingOrder 
)
virtual

Sets the user model.

This instance takes ownership of provided pointer. Currently installed model will be deleted by this method. To avoid this use takeBaseModel() prior to calling this method.

Parameters
modelThe model that provides the data to sort, group and present.
bool GroupModel::setGroupingColumn ( int  column)
virtualslot

Sets the column in base model that decides grouping.

The column may be -1 to indicate that no grouping should be performed or an index inside the valid range for columns in base model.

Please note that, if the old column is the same as the new one, nothing happens and the result is true.

Parameters
columnzero-based index of the column inside base model
Returns
true if the grouping column is the one in the argument.
bool GroupModel::setSortingColumn ( int  column)
virtualslot

Sets the column in base model that decides sorting.

The column may be -1 to indicate that no sorting should be performed or an index inside the valid range for columns in base model.

The column may not be the same as the grouping column or the pixmap column.

Please note that, if the old column is the same as the new one, nothing happens and the result is true.

Parameters
columnzero-based index of the column inside base model
Returns
true if the sorting column is the one in the argument.
QList< int > GroupModel::sortingColumns ( ) const
virtual

Retreive the list of columns where sorting can be applied.

Default implementation simply excludes the grouping and pixmap columns and returns everything else.

Returns
the index in the base model of the columns where sorting can be applied.
QAbstractItemModel * GroupModel::takeBaseModel ( )

Take ownership of user model.

The ownership of the pointer is transferred to the caller. The model is uninstalled and a NULL model is installed.

Returns
previously installed model

The documentation for this class was generated from the following files: