Skip to content

Commit dd88fa9

Browse files
committedMay 2, 2016
[feature] New configuration options for attribute table
* Allow reordering the attribute table columns * Allow adding a new column to trigger an action to the attribute table
1 parent aa9010e commit dd88fa9

27 files changed

+738
-105
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
%Include qgsaction.sip
2323
%Include qgsactionmanager.sip
2424
%Include qgsattributeaction.sip
25+
%Include qgsattributetableconfig.sip
2526
%Include qgsbrowsermodel.sip
2627
%Include qgsclipper.sip
2728
%Include qgscolorscheme.sip
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/***************************************************************************
2+
qgsattributetableconfig.h - QgsAttributeTableConfig
3+
4+
---------------------
5+
begin : 27.4.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
/**
18+
* This is a container for configuration of the attribute table.
19+
* The configuration is specific for one vector layer.
20+
*/
21+
22+
class QgsAttributeTableConfig
23+
{
24+
%TypeHeaderCode
25+
#include <qgsattributetableconfig.h>
26+
%End
27+
public:
28+
/**
29+
* The type of an attribute table column.
30+
*/
31+
enum Type
32+
{
33+
Field, //!< This column represents a field
34+
Action //!< This column represents an action widget
35+
};
36+
37+
/**
38+
* Defines the configuration of a column in the attribute table.
39+
*/
40+
struct ColumnConfig
41+
{
42+
QgsAttributeTableConfig::Type mType; //!< The type of this column.
43+
QString mName; //!< The name of the attribute if this column represents a field
44+
bool mHidden; //!< Flag that controls if the column is hidden
45+
};
46+
47+
QgsAttributeTableConfig();
48+
49+
/**
50+
* Get the list with all columns and their configuration.
51+
* The list order defines the order of appearance.
52+
*/
53+
QVector<QgsAttributeTableConfig::ColumnConfig> columns() const;
54+
55+
/**
56+
* Set the list of columns visible in the attribute table.
57+
* The list order defines the order of appearance.
58+
*/
59+
void setColumns( const QVector<QgsAttributeTableConfig::ColumnConfig>& columns );
60+
61+
/**
62+
* Update the configuration with the given fields.
63+
* Any field which is present in the configuration but not present in the
64+
* parameter fields will be removed. Any field which is in the parameter
65+
* fields but not in the configuration will be appended.
66+
*/
67+
void update( const QgsFields& fields );
68+
69+
/**
70+
* Serialize to XML on layer save
71+
*/
72+
void writeXml( QDomNode& node ) const;
73+
74+
/**
75+
* Deserialize to XML on layer load
76+
*/
77+
void readXml( const QDomNode& node );
78+
};

‎python/core/qgsvectorlayer.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,18 @@ class QgsVectorLayer : QgsMapLayer
13211321
*/
13221322
QgsConditionalLayerStyles *conditionalStyles() const;
13231323

1324+
/**
1325+
* Get the attribute table configuration object.
1326+
* This defines the appearance of the attribute table.
1327+
*/
1328+
QgsAttributeTableConfig attributeTableConfig() const;
1329+
1330+
/**
1331+
* Set the attribute table configuration object.
1332+
* This defines the appearance of the attribute table.
1333+
*/
1334+
void setAttributeTableConfig( const QgsAttributeTableConfig& attributeTableConfig );
1335+
13241336
public slots:
13251337
/**
13261338
* Select feature by its ID

‎python/gui/attributetable/qgsattributetablefiltermodel.sip

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ class QgsAttributeTableFilterModel: QSortFilterProxyModel, QgsFeatureModel
1313
ShowEdited
1414
};
1515

16+
enum ColumnType
17+
{
18+
ColumnTypeField, //!< This column shows a field
19+
ColumnTypeActionButton //!< This column shows action buttons
20+
};
21+
22+
enum Role
23+
{
24+
TypeRole = QgsAttributeTableModel::UserRole //!< The type of a given column
25+
};
26+
27+
1628
/**
1729
*
1830
* Make sure, the master model is already loaded, so the selection will get synchronized.
@@ -99,6 +111,25 @@ class QgsAttributeTableFilterModel: QSortFilterProxyModel, QgsFeatureModel
99111
/** Returns the map canvas*/
100112
QgsMapCanvas* mapCanvas() const;
101113

114+
virtual QVariant data( const QModelIndex& index, int role ) const;
115+
116+
QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
117+
118+
/**
119+
* Get the index of the first column that contains an action widget.
120+
* Returns -1 if none is defined.
121+
*/
122+
int actionColumnIndex() const;
123+
124+
int columnCount( const QModelIndex &parent ) const;
125+
126+
/**
127+
* Set the attribute table configuration to control which fields are shown,
128+
* in which order they are shown as well as if and where an action column
129+
* is shown.
130+
*/
131+
void setAttributeTableConfig( const QgsAttributeTableConfig& config );
132+
102133
protected:
103134
/**
104135
* Returns true if the source row will be accepted

‎python/gui/attributetable/qgsattributetableview.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class QgsAttributeTableView : QTableView
1919
/**
2020
* This event filter is installed on the verticalHeader to intercept mouse press and release
2121
* events. These are used to disable / enable live synchronisation with the map canvas selection
22-
* which can be slow due to recurring canvas repaints. Updating the
22+
* which can be slow due to recurring canvas repaints.
2323
*
2424
* @param object The object which is the target of the event.
2525
* @param event The intercepted event

‎python/gui/attributetable/qgsdualview.sip

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class QgsDualView : QStackedWidget
6565
*/
6666
void setFilterMode( QgsAttributeTableFilterModel::FilterMode filterMode );
6767

68+
/**
69+
* Get the filter mode
70+
*
71+
* @return the filter mode
72+
*/
6873
QgsAttributeTableFilterModel::FilterMode filterMode();
6974

7075
/**
@@ -98,6 +103,9 @@ class QgsDualView : QStackedWidget
98103
*/
99104
void setFilteredFeatures( const QgsFeatureIds& filteredFeatures );
100105

106+
/**
107+
* Get a list of currently visible feature ids.
108+
*/
101109
QgsFeatureIds filteredFeatures();
102110

103111
/**
@@ -112,6 +120,11 @@ class QgsDualView : QStackedWidget
112120
void setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager );
113121

114122
QgsAttributeTableView* tableView();
123+
/**
124+
* Set the attribute table config which should be used to control
125+
* the appearance of the attribute table.
126+
*/
127+
void setAttributeTableConfig( const QgsAttributeTableConfig& config );
115128

116129
protected:
117130
/**

‎python/gui/attributetable/qgsorganizetablecolumnsdialog.sip

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ class QgsOrganizeTableColumnsDialog : QDialog
88
/**
99
* Constructor
1010
* @param vl The concerned vector layer
11-
* @param visible the cuurent list of visible fields name
11+
* @param visible the current list of visible fields name
1212
* @param parent parent object
1313
* @param flags window flags
1414
*/
15-
QgsOrganizeTableColumnsDialog( const QgsVectorLayer* vl, QStringList visible, QWidget *parent /TransferThis/ = nullptr, Qt::WindowFlags flags = Qt::Window );
15+
QgsOrganizeTableColumnsDialog(const QgsVectorLayer* vl, QWidget* parent = nullptr, Qt::WindowFlags flags = Qt::Window );
16+
17+
/**
18+
* Destructor
19+
*/
1620
~QgsOrganizeTableColumnsDialog();
1721

1822
/**
19-
* Get the selected fields name
20-
* @return The selected fields name
23+
* Get the updated configuration
2124
*/
22-
QStringList selectedFields();
25+
QgsAttributeTableConfig config() const;
2326
};

‎src/app/qgsattributetabledialog.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
125125

126126
// Initialize dual view
127127
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, context );
128+
mMainView->setAttributeTableConfig( mLayer->attributeTableConfig() );
128129

129130
// Initialize filter gui elements
130131
mFilterActionMapper = new QSignalMapper( this );
@@ -776,17 +777,12 @@ void QgsAttributeTableDialog::on_mFilterTableFields_clicked()
776777
return;
777778
}
778779

779-
QgsOrganizeTableColumnsDialog dialog( mLayer, mVisibleFields );
780+
QgsOrganizeTableColumnsDialog dialog( mLayer );
780781
if ( dialog.exec() == QDialog::Accepted )
781782
{
782-
mVisibleFields = dialog.selectedFields();
783-
784-
const QgsFields layerAttributes = mLayer->fields();
785-
for ( int idx = 0; idx < layerAttributes.count(); ++idx )
786-
{
787-
mMainView->tableView()->setColumnHidden(
788-
idx, !mVisibleFields.contains( layerAttributes[idx].name() ) );
789-
}
783+
QgsAttributeTableConfig config = dialog.config();
784+
mLayer->setAttributeTableConfig( config );
785+
mMainView->setAttributeTableConfig( config );
790786
}
791787
}
792788

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ SET(QGIS_CORE_SRCS
7979
qgsapplication.cpp
8080
qgsaction.cpp
8181
qgsactionmanager.cpp
82+
qgsattributetableconfig.cpp
8283
qgsbrowsermodel.cpp
8384
qgscachedfeatureiterator.cpp
8485
qgscacheindex.cpp
@@ -591,7 +592,7 @@ SET(QGIS_CORE_HDRS
591592

592593
qgis.h
593594
qgsaction.h
594-
qgsactionmanager.h
595+
qgsattributetableconfig.h
595596
qgsattributeaction.h
596597
qgscachedfeatureiterator.h
597598
qgscacheindex.h

‎src/core/geometry/qgsabstractgeometryv2.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ email : marco.hugentobler at sourcepole dot com
2323

2424
#include <QString>
2525

26-
class QgsCoordinateTransform;
2726
class QgsMapToPixel;
2827
class QgsCurveV2;
2928
class QgsMultiCurveV2;

0 commit comments

Comments
 (0)
Please sign in to comment.