Skip to content

Commit dc905ac

Browse files
committed
[FEATURE][composer] Rework attribute tables to make tables more flexible.
- Add support for reordering columns in a table (fix #5942) - Add support for expression based columns - Add support for setting column alignment (fix #2957) - Add support for setting column header alignment - Make sorting in tables correctly handle nulls, dates, times, locale aware string sorting This work also paves the way for future expansion of attribute tables to allow for setting column widths, column colors, etc.
1 parent fa0f0bd commit dc905ac

24 files changed

+2985
-585
lines changed

python/core/composer/qgscomposerattributetable.sip

+53-34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Attribute table item for map composer.
33
*/
44

5+
/**Helper class for sorting tables, takes into account sorting column and ascending / descending*/
56
class QgsComposerAttributeTableCompare
67
{
78
%TypeHeaderCode
@@ -11,7 +12,15 @@ class QgsComposerAttributeTableCompare
1112
public:
1213
QgsComposerAttributeTableCompare();
1314
// bool operator()( const QgsAttributeMap& m1, const QgsAttributeMap& m2 );
15+
16+
/**Sets column number to sort by
17+
* @param col column number for sorting
18+
*/
1419
void setSortColumn( int col );
20+
21+
/**Sets sort order for column sorting
22+
* @param asc set to true to sort in ascending order, false to sort in descending order
23+
*/
1524
void setAscending( bool asc );
1625
};
1726

@@ -32,85 +41,94 @@ class QgsComposerAttributeTable : QgsComposerTable
3241
/** \brief Reimplementation of QCanvasItem::paint*/
3342
virtual void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
3443

44+
/**Writes properties specific to attribute tables
45+
* @param elem an existing QDomElement in which to store the attribute table's properties.
46+
* @param doc QDomDocument for the destination xml.
47+
* @see readXML
48+
*/
3549
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
50+
51+
/**Reads the properties specific to an attribute table from xml.
52+
* @param itemElem a QDomElement holding the attribute table's desired properties.
53+
* @param doc QDomDocument for the source xml.
54+
* @see writeXML
55+
*/
3656
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
3757

3858
/**Sets the vector layer from which to display feature attributes
3959
* @param layer Vector layer for attribute table
40-
* @note added in 2.3
4160
* @see vectorLayer
42-
*/
61+
*/
4362
void setVectorLayer( QgsVectorLayer* layer );
4463

4564
/**Returns the vector layer the attribute table is currently using
4665
* @returns attribute table's current vector layer
47-
* @note added in 2.3
4866
* @see setVectorLayer
49-
*/
67+
*/
5068
QgsVectorLayer* vectorLayer() const;
69+
70+
/**Resets the attribute table's columns to match the vector layer's fields
71+
* @note added in 2.3
72+
* @see setVectorLayer
73+
*/
74+
void resetColumns();
5175

5276
/**Sets the composer map to use to limit the extent of features shown in the
5377
* attribute table. This setting only has an effect if setDisplayOnlyVisibleFeatures is
5478
* set to true. Changing the composer map forces the table to refetch features from its
5579
* vector layer, and may result in the table changing size to accomodate the new displayed
5680
* feature attributes.
5781
* @param map QgsComposerMap which drives the extents of the table's features
58-
* @note added in 2.3
5982
* @see composerMap
6083
* @see setDisplayOnlyVisibleFeatures
61-
*/
84+
*/
6285
void setComposerMap( const QgsComposerMap* map /TransferThis/ );
6386

6487
/**Returns the composer map whose extents are controlling the features shown in the
6588
* table. The extents of the map are only used if displayOnlyVisibleFeatures() is true.
6689
* @returns composer map controlling the attribute table
67-
* @note added in 2.3
6890
* @see setComposerMap
6991
* @see displayOnlyVisibleFeatures
70-
*/
92+
*/
7193
const QgsComposerMap* composerMap() const;
7294

7395
/**Sets the maximum number of features shown by the table. Changing this setting may result
7496
* in the attribute table changing its size to accomodate the new number of rows, and requires
7597
* the table to refetch features from its vector layer.
7698
* @param features maximum number of features to show in the table
77-
* @note added in 2.3
7899
* @see maximumNumberOfFeatures
79-
*/
100+
*/
80101
void setMaximumNumberOfFeatures( int features );
81102

82103
/**Returns the maximum number of features to be shown by the table.
83104
* @returns maximum number of features
84-
* @note added in 2.3
85105
* @see setMaximumNumberOfFeatures
86-
*/
106+
*/
87107
int maximumNumberOfFeatures() const;
88108

89109
/**Sets attribute table to only show features which are visible in a composer map item. Changing
90110
* this setting forces the table to refetch features from its vector layer, and may result in
91111
* the table changing size to accomodate the new displayed feature attributes.
92112
* @param visibleOnly set to true to show only visible features
93-
* @note added in 2.3
94113
* @see displayOnlyVisibleFeatures
95114
* @see setComposerMap
96-
*/
115+
*/
97116
void setDisplayOnlyVisibleFeatures( bool b );
98117

99118
/**Returns true if the table is set to show only features visible on a corresponding
100119
* composer map item.
101120
* @returns true if table only shows visible features
102-
* @note added in 2.3
103121
* @see composerMap
104122
* @see setDisplayOnlyVisibleFeatures
105-
*/
123+
*/
106124
bool displayOnlyVisibleFeatures() const;
107125

108126
/**Returns true if a feature filter is active on the attribute table
109127
* @returns bool state of the feature filter
110128
* @note added in 2.3
111129
* @see setFilterFeatures
112130
* @see featureFilter
113-
*/
131+
*/
114132
bool filterFeatures() const;
115133

116134
/**Sets whether the feature filter is active for the attribute table. Changing
@@ -120,7 +138,7 @@ class QgsComposerAttributeTable : QgsComposerTable
120138
* @note added in 2.3
121139
* @see filterFeatures
122140
* @see setFeatureFilter
123-
*/
141+
*/
124142
void setFilterFeatures( bool filter );
125143

126144
/**Returns the current expression used to filter features for the table. The filter is only
@@ -129,7 +147,7 @@ class QgsComposerAttributeTable : QgsComposerTable
129147
* @note added in 2.3
130148
* @see setFeatureFilter
131149
* @see filterFeatures
132-
*/
150+
*/
133151
QString featureFilter() const;
134152

135153
/**Sets the expression used for filtering features in the table. The filter is only
@@ -140,50 +158,51 @@ class QgsComposerAttributeTable : QgsComposerTable
140158
* @note added in 2.3
141159
* @see featureFilter
142160
* @see setFilterFeatures
143-
*/
161+
*/
144162
void setFeatureFilter( const QString& expression );
145163

146164
/**Returns the attributes fields which are shown by the table.
147165
* @returns a QSet of integers refering to the attributes in the vector layer. If
148166
* result is an empty QSet than all feature attributes will be shown.
149167
* @see setDisplayAttributes
150-
*/
151-
QSet<int> displayAttributes() const;
168+
* @deprecated use QgsComposerTable::columns() instead
169+
*/
170+
QSet<int> displayAttributes() const /Deprecated/;
152171

153172
/**Sets the attributes to display in the table.
154-
* @param attr QSet of integer values refering to the attributes from the vector layer to show
173+
* @param attr QSet of integer values refering to the attributes from the vector layer to show.
155174
* Set to an empty QSet to show all feature attributes.
156175
* @param refresh set to true to force the table to refetch features from its vector layer
157176
* and immediately update the display of the table. This may result in the table changing size
158177
* to accomodate the new displayed feature attributes.
159178
* @see displayAttributes
160-
*/
179+
*/
161180
void setDisplayAttributes( const QSet<int>& attr );
162181

163182
/**Returns the attribute field aliases, which control how fields are named in the table's
164183
* header row.
165-
* @returns a QMap of integers to strings, where the string is the field's alias.
184+
* @returns a QMap of integers to strings, where the string is the field's alias and the
185+
* integer is the field index from the associated vector layer.
166186
* @see setFieldAliasMap
167-
*/
168-
QMap<int, QString> fieldAliasMap() const;
187+
* @deprecated use QgsComposerTable::columns() instead
188+
*/
189+
QMap<int, QString> fieldAliasMap() const /Deprecated/;
169190

170191
/**Sets the attribute field aliases, which control how fields are named in the table's
171192
* header row.
172193
* @param map QMap of integers to strings, where the string is the alias to use for the
173-
* corresponding field.
174-
* @param refresh set to true to force the table to refetch features from its vector layer
175-
* and immediately update the display of the table. This may result in the table changing size
176-
* to accomodate the new displayed feature attributes and field aliases.
194+
* corresponding field, and the integer is the field index from the vector layer
177195
* @see fieldAliasMap
178-
*/
179-
void setFieldAliasMap( const QMap<int, QString>& map );
196+
* @deprecated use QgsComposerTable::columns() instead
197+
*/
198+
void setFieldAliasMap( const QMap<int, QString>& map ) /Deprecated/;
180199

181200
/**Adapts mMaximumNumberOfFeatures depending on the rectangle height. Calling this forces
182201
* the table to refetch features from its vector layer and immediately updates the display
183202
* of the table.
184203
* @see maximumNumberOfFeatures
185204
* @see setMaximumNumberOfFeatures
186-
*/
205+
*/
187206
void setSceneRect( const QRectF& rectangle );
188207

189208
// @note not available in python bindings
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**A model for displaying columns shown in a QgsComposerAttributeTable*/
2+
class QgsComposerAttributeTableColumnModel: public QAbstractTableModel
3+
{
4+
%TypeHeaderCode
5+
#include <qgscomposerattributetablemodel.h>
6+
%End
7+
8+
public:
9+
10+
/*! Controls whether a row/column is shifted up or down
11+
*/
12+
enum ShiftDirection
13+
{
14+
ShiftUp, /*!< shift the row/column up */
15+
ShiftDown /*!< shift the row/column down */
16+
};
17+
18+
/**Constructor for QgsComposerAttributeTableColumnModel.
19+
* @param composerTable QgsComposerAttributeTable the model is attached to
20+
*/
21+
QgsComposerAttributeTableColumnModel( QgsComposerAttributeTable *composerTable, QObject *parent = 0 );
22+
virtual ~QgsComposerAttributeTableColumnModel();
23+
24+
virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
25+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
26+
virtual QVariant data( const QModelIndex &index, int role ) const;
27+
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
28+
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
29+
Qt::ItemFlags flags( const QModelIndex &index ) const;
30+
bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() );
31+
bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() );
32+
QModelIndex index( int row, int column, const QModelIndex &parent ) const;
33+
34+
/**Moves the specified row up or down in the model. Used for rearranging the attribute tables
35+
* columns.
36+
* @returns true if the move is allowed
37+
* @param row row in model representing attribute table column to move
38+
* @param direction direction to move the attribute table column
39+
* @note added in 2.3
40+
*/
41+
bool moveRow( int row , ShiftDirection direction );
42+
43+
/**Resets the attribute table's columns to match the source layer's fields. Remove all existing
44+
* attribute table columns and column customisations.
45+
* @note added in 2.3
46+
*/
47+
void resetToLayer();
48+
49+
/**Returns the QgsComposerTableColumn corresponding to an index in the model.
50+
* @returns QgsComposerTableColumn for specified index
51+
* @param index a QModelIndex
52+
* @note added in 2.3
53+
* @see indexFromColumn
54+
*/
55+
QgsComposerTableColumn* columnFromIndex( const QModelIndex & index ) const;
56+
57+
/**Returns a QModelIndex corresponding to a QgsComposerTableColumn in the model.
58+
* @returns QModelIndex for specified QgsComposerTableColumn
59+
* @param column a QgsComposerTableColumn
60+
* @note added in 2.3
61+
* @see columnFromIndex
62+
*/
63+
QModelIndex indexFromColumn( QgsComposerTableColumn *column );
64+
65+
/**Sets a specified column as a sorted column in the QgsComposerAttributeTable. The column will be
66+
* added to the end of the sort rank list, ie it will take the next largest available sort rank.
67+
* @param column a QgsComposerTableColumn
68+
* @param order sort order for column
69+
* @note added in 2.3
70+
* @see removeColumnFromSort
71+
* @see moveColumnInSortRank
72+
*/
73+
void setColumnAsSorted( QgsComposerTableColumn *column, Qt::SortOrder order );
74+
75+
/**Sets a specified column as an unsorted column in the QgsComposerAttributeTable. The column will be
76+
* removed from the sort rank list.
77+
* @param column a QgsComposerTableColumn
78+
* @note added in 2.3
79+
* @see setColumnAsSorted
80+
*/
81+
void setColumnAsUnsorted( QgsComposerTableColumn * column );
82+
83+
/**Moves a column up or down in the sort rank for the QgsComposerAttributeTable.
84+
* @param column a QgsComposerTableColumn
85+
* @param direction direction to move the column in the sort rank list
86+
* @note added in 2.3
87+
* @see setColumnAsSorted
88+
*/
89+
bool moveColumnInSortRank( QgsComposerTableColumn * column, ShiftDirection direction );
90+
91+
};
92+
93+
/**Allows for filtering QgsComposerAttributeTable columns by columns which are sorted or unsorted*/
94+
class QgsComposerTableSortColumnsProxyModel : public QSortFilterProxyModel
95+
{
96+
%TypeHeaderCode
97+
#include <qgscomposerattributetablemodel.h>
98+
%End
99+
100+
public:
101+
/*! Controls whether the proxy model shows sorted or unsorted columns
102+
*/
103+
enum ColumnFilterType
104+
{
105+
ShowSortedColumns, /*!< show only sorted columns */
106+
ShowUnsortedColumns/*!< show only unsorted columns */
107+
};
108+
109+
/**Constructor for QgsComposerTableSortColumnsProxyModel.
110+
* @param composerTable QgsComposerAttributeTable the model is attached to
111+
* @param filterType filter for columns, controls whether sorted or unsorted columns are shown
112+
*/
113+
QgsComposerTableSortColumnsProxyModel( QgsComposerAttributeTable *composerTable, ColumnFilterType filterType, QObject *parent = 0 );
114+
115+
virtual ~QgsComposerTableSortColumnsProxyModel();
116+
117+
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
118+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
119+
virtual QVariant data( const QModelIndex &index, int role ) const;
120+
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
121+
Qt::ItemFlags flags( const QModelIndex &index ) const;
122+
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
123+
124+
/**Returns the QgsComposerTableColumn corresponding to a row in the proxy model.
125+
* @returns QgsComposerTableColumn for specified row
126+
* @param row a row number
127+
* @note added in 2.3
128+
* @see columnFromIndex
129+
*/
130+
QgsComposerTableColumn* columnFromRow( int row );
131+
132+
/**Returns the QgsComposerTableColumn corresponding to an index in the proxy model.
133+
* @returns QgsComposerTableColumn for specified index
134+
* @param index a QModelIndex
135+
* @note added in 2.3
136+
* @see columnFromRow
137+
* @see columnFromSourceIndex
138+
*/
139+
QgsComposerTableColumn* columnFromIndex( const QModelIndex & index ) const;
140+
141+
142+
/**Returns the QgsComposerTableColumn corresponding to an index from the source
143+
* QgsComposerAttributeTableColumnModel model.
144+
* @returns QgsComposerTableColumn for specified index from QgsComposerAttributeTableColumnModel
145+
* @param index a QModelIndex
146+
* @note added in 2.3
147+
* @see columnFromRow
148+
* @see columnFromIndex
149+
*/
150+
QgsComposerTableColumn* columnFromSourceIndex( const QModelIndex& sourceIndex ) const;
151+
152+
protected:
153+
154+
bool filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const;
155+
156+
};

0 commit comments

Comments
 (0)