QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
18
19#include "qgsapplication.h"
22#include "qgsexpression.h"
23#include "qgsfields.h"
24#include "qgsgui.h"
25#include "qgsmapcanvas.h"
26#include "qgsmessagebar.h"
27#include "qgsproject.h"
28#include "qgsrubberband.h"
30#include "qgsvectorlayer.h"
31
32#include <QMessageBox>
33#include <QString>
34
35#include "moc_qgsorganizetablecolumnsdialog.cpp"
36
37using namespace Qt::StringLiterals;
38
40 : QDialog( parent, flags )
41{
42 setupUi( this );
44
45 connect( mShowAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::showAll );
46 connect( mHideAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::hideAll );
47 connect( mToggleSelectionButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::toggleSelection );
48
49 if ( vl )
50 {
51 mConfig = config;
52 const QgsFields fields = vl->fields();
53 mConfig.update( fields );
54
55 mFieldsList->clear();
56
57 const auto constColumns = mConfig.columns();
58 for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
59 {
60 QListWidgetItem *item = nullptr;
61 if ( columnConfig.type == QgsAttributeTableConfig::Action )
62 {
63 item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
64 item->setIcon( QgsApplication::getThemeIcon( u"/propertyicons/action.svg"_s ) );
65 }
66 else
67 {
68 const int idx = fields.lookupField( columnConfig.name );
69 item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
70 item->setIcon( fields.iconForField( idx, true ) );
71 }
72
73 item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
74 item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
75 }
76 }
77
78 if ( !vl || mConfig.columns().count() < 5 )
79 {
80 mShowAllButton->hide();
81 mHideAllButton->hide();
82 mToggleSelectionButton->hide();
83 }
84}
85
87QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
88 : QgsOrganizeTableColumnsDialog( vl, vl->attributeTableConfig(), parent, flags )
89{
90}
92
94{
95 QVector<QgsAttributeTableConfig::ColumnConfig> columns;
96 columns.reserve( mFieldsList->count() );
97
98 for ( int i = 0; i < mFieldsList->count(); i++ )
99 {
100 const QListWidgetItem *item = mFieldsList->item( i );
101 QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
102
103 columnConfig.hidden = item->checkState() == Qt::Unchecked;
104
105 columns.append( columnConfig );
106 }
107
109 config.setColumns( columns );
110 return config;
111}
112
114{
115 for ( int i = 0; i < mFieldsList->count(); i++ )
116 {
117 mFieldsList->item( i )->setCheckState( Qt::Checked );
118 }
119}
120
122{
123 for ( int i = 0; i < mFieldsList->count(); i++ )
124 {
125 mFieldsList->item( i )->setCheckState( Qt::Unchecked );
126 }
127}
128
130{
131 for ( QListWidgetItem *item : mFieldsList->selectedItems() )
132 {
133 item->setCheckState( item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked );
134 }
135}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A container for configuration of the attribute table.
@ Action
This column represents an action widget.
Container of fields for a vector layer.
Definition qgsfields.h:46
void clear()
Removes all fields.
Definition qgsfields.cpp:64
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:224
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 dataset.
Q_INVOKABLE 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.