QGIS API Documentation 3.38.0-Grenoble (exported)
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
21
22#include "qgsapplication.h"
23#include "qgsvectorlayer.h"
24#include "qgsexpression.h"
25
27#include "qgsmapcanvas.h"
28#include "qgsproject.h"
29#include "qgsmessagebar.h"
30#include "qgsrubberband.h"
31#include "qgsfields.h"
33
34#include "qgsgui.h"
35
36
37QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, const QgsAttributeTableConfig &config, QWidget *parent, Qt::WindowFlags flags )
38 : QDialog( parent, flags )
39{
40 setupUi( this );
42
43 connect( mShowAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::showAll );
44 connect( mHideAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::hideAll );
45 connect( mToggleSelectionButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::toggleSelection );
46
47 if ( vl )
48 {
49 mConfig = config;
50 const QgsFields fields = vl->fields();
51 mConfig.update( fields );
52
53 mFieldsList->clear();
54
55 const auto constColumns = mConfig.columns();
56 for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
57 {
58 QListWidgetItem *item = nullptr;
59 if ( columnConfig.type == QgsAttributeTableConfig::Action )
60 {
61 item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
62 item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) ) );
63 }
64 else
65 {
66 const int idx = fields.lookupField( columnConfig.name );
67 item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
68 item->setIcon( fields.iconForField( idx, true ) );
69 }
70
71 item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
72 item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
73 }
74 }
75
76 if ( !vl || mConfig.columns().count() < 5 )
77 {
78 mShowAllButton->hide();
79 mHideAllButton->hide();
80 mToggleSelectionButton->hide();
81 }
82}
83
85QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
86 : QgsOrganizeTableColumnsDialog( vl, vl->attributeTableConfig(), parent, flags )
87{
88}
90
92{
93 QVector<QgsAttributeTableConfig::ColumnConfig> columns;
94 columns.reserve( mFieldsList->count() );
95
96 for ( int i = 0; i < mFieldsList->count() ; i++ )
97 {
98 const QListWidgetItem *item = mFieldsList->item( i );
99 QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
100
101 columnConfig.hidden = item->checkState() == Qt::Unchecked;
102
103 columns.append( columnConfig );
104 }
105
107 config.setColumns( columns );
108 return config;
109}
110
112{
113 for ( int i = 0; i < mFieldsList->count() ; i++ )
114 {
115 mFieldsList->item( i )->setCheckState( Qt::Checked );
116 }
117}
118
120{
121 for ( int i = 0; i < mFieldsList->count() ; i++ )
122 {
123 mFieldsList->item( i )->setCheckState( Qt::Unchecked );
124 }
125}
126
128{
129 for ( QListWidgetItem *item : mFieldsList->selectedItems() )
130 {
131 item->setCheckState( item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked );
132 }
133}
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: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.
Defines the configuration of a column in the attribute table.
bool hidden
Flag that controls if the column is hidden.