QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsorganizetablecolumnsdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsorganizetablecolumnsdialog.cpp
3 -------------------
4 date : Feb 2016
5 copyright : Stéphane Brunner
7
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#include <QMessageBox>
18
23#include "qgsdockwidget.h"
24
25#include "qgsapplication.h"
27#include "qgsvectorlayer.h"
28#include "qgsexpression.h"
29
31#include "qgslogger.h"
32#include "qgsmapcanvas.h"
33#include "qgsproject.h"
35#include "qgsmessagebar.h"
37#include "qgsfeaturelistmodel.h"
38#include "qgsrubberband.h"
39#include "qgsfields.h"
41
42#include "qgsgui.h"
43
44
45QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, const QgsAttributeTableConfig &config, QWidget *parent, Qt::WindowFlags flags )
46 : QDialog( parent, flags )
47{
48 setupUi( this );
50
51 connect( mShowAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::showAll );
52 connect( mHideAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::hideAll );
53 connect( mToggleSelectionButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::toggleSelection );
54
55 if ( vl )
56 {
57 mConfig = config;
58 mConfig.update( vl->fields() );
59
60 mFieldsList->clear();
61
62 const auto constColumns = mConfig.columns();
63 for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
64 {
65 QListWidgetItem *item = nullptr;
66 if ( columnConfig.type == QgsAttributeTableConfig::Action )
67 {
68 item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
69 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) ) );
70 }
71 else
72 {
73 const int idx = vl->fields().lookupField( columnConfig.name );
74 item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
75
76 switch ( vl->fields().fieldOrigin( idx ) )
77 {
79 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
80 break;
81
83 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/join.svg" ) ) );
84 break;
85
86 default:
87 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/attributes.svg" ) ) );
88 break;
89 }
90 }
91
92 item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
93 item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
94 }
95 }
96
97 if ( !vl || mConfig.columns().count() < 5 )
98 {
99 mShowAllButton->hide();
100 mHideAllButton->hide();
101 mToggleSelectionButton->hide();
102 }
103}
104
106QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
107 : QgsOrganizeTableColumnsDialog( vl, vl->attributeTableConfig(), parent, flags )
108{
109}
111
113{
114 QVector<QgsAttributeTableConfig::ColumnConfig> columns;
115 columns.reserve( mFieldsList->count() );
116
117 for ( int i = 0; i < mFieldsList->count() ; i++ )
118 {
119 const QListWidgetItem *item = mFieldsList->item( i );
120 QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
121
122 columnConfig.hidden = item->checkState() == Qt::Unchecked;
123
124 columns.append( columnConfig );
125 }
126
128 config.setColumns( columns );
129 return config;
130}
131
133{
134 for ( int i = 0; i < mFieldsList->count() ; i++ )
135 {
136 mFieldsList->item( i )->setCheckState( Qt::Checked );
137 }
138}
139
141{
142 for ( int i = 0; i < mFieldsList->count() ; i++ )
143 {
144 mFieldsList->item( i )->setCheckState( Qt::Unchecked );
145 }
146}
147
149{
150 for ( QListWidgetItem *item : mFieldsList->selectedItems() )
151 {
152 item->setCheckState( item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked );
153 }
154}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
This is a container for configuration of the attribute table.
@ Action
This column represents an action widget.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
void update(const QgsFields &fields)
Update the configuration with the given fields.
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
@ OriginExpression
Field is calculated from an expression.
Definition: qgsfields.h:54
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:52
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Definition: qgsfields.cpp:189
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:359
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:194
Dialog for organising (hiding and reordering) columns in the attributes table.
void toggleSelection()
Toggle the check state of selected fields to hide or show them in the attribute table.
void showAll()
showAll checks all the fields to show them all in the attribute table
QgsOrganizeTableColumnsDialog(const QgsVectorLayer *vl, const QgsAttributeTableConfig &config, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::Window)
Constructor.
void hideAll()
hideAll unchecks all the fields to hide them all in the attribute table
QgsAttributeTableConfig config() const
Gets the updated configuration.
Represents a vector layer which manages a vector based data sets.
QString attributeDisplayName(int index) const
Convenience function that returns the attribute alias if defined or the field name else.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Defines the configuration of a column in the attribute table.
bool hidden
Flag that controls if the column is hidden.