QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
20#include "moc_qgsorganizetablecolumnsdialog.cpp"
22
23#include "qgsapplication.h"
24#include "qgsvectorlayer.h"
25#include "qgsexpression.h"
26
28#include "qgsmapcanvas.h"
29#include "qgsproject.h"
30#include "qgsmessagebar.h"
31#include "qgsrubberband.h"
32#include "qgsfields.h"
34
35#include "qgsgui.h"
36
37
38QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, const QgsAttributeTableConfig &config, QWidget *parent, Qt::WindowFlags flags )
39 : QDialog( parent, flags )
40{
41 setupUi( this );
43
44 connect( mShowAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::showAll );
45 connect( mHideAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::hideAll );
46 connect( mToggleSelectionButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::toggleSelection );
47
48 if ( vl )
49 {
50 mConfig = config;
51 const QgsFields fields = vl->fields();
52 mConfig.update( fields );
53
54 mFieldsList->clear();
55
56 const auto constColumns = mConfig.columns();
57 for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
58 {
59 QListWidgetItem *item = nullptr;
60 if ( columnConfig.type == QgsAttributeTableConfig::Action )
61 {
62 item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
63 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) ) );
64 }
65 else
66 {
67 const int idx = fields.lookupField( columnConfig.name );
68 item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
69 item->setIcon( fields.iconForField( idx, true ) );
70 }
71
72 item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
73 item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
74 }
75 }
76
77 if ( !vl || mConfig.columns().count() < 5 )
78 {
79 mShowAllButton->hide();
80 mHideAllButton->hide();
81 mToggleSelectionButton->hide();
82 }
83}
84
86QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
87 : QgsOrganizeTableColumnsDialog( vl, vl->attributeTableConfig(), parent, flags )
88{
89}
91
93{
94 QVector<QgsAttributeTableConfig::ColumnConfig> columns;
95 columns.reserve( mFieldsList->count() );
96
97 for ( int i = 0; i < mFieldsList->count() ; i++ )
98 {
99 const QListWidgetItem *item = mFieldsList->item( i );
100 QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
101
102 columnConfig.hidden = item->checkState() == Qt::Unchecked;
103
104 columns.append( columnConfig );
105 }
106
108 config.setColumns( columns );
109 return config;
110}
111
113{
114 for ( int i = 0; i < mFieldsList->count() ; i++ )
115 {
116 mFieldsList->item( i )->setCheckState( Qt::Checked );
117 }
118}
119
121{
122 for ( int i = 0; i < mFieldsList->count() ; i++ )
123 {
124 mFieldsList->item( i )->setCheckState( Qt::Unchecked );
125 }
126}
127
129{
130 for ( QListWidgetItem *item : mFieldsList->selectedItems() )
131 {
132 item->setCheckState( item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked );
133 }
134}
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.
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QIcon iconForField(int fieldIdx, bool considerOrigin=false) const
Returns an icon corresponding to a field index, based on the field's type and source.
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:209
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.
Defines the configuration of a column in the attribute table.
bool hidden
Flag that controls if the column is hidden.