QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsorganizetablecolumnsdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsorganizetablecolumnsdialog.cpp
3  -------------------
4  date : Feb 2016
5  copyright : Stéphane Brunner
6  email : [email protected]
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 "qgsattributetablemodel.h"
22 #include "qgsattributetableview.h"
23 #include "qgsdockwidget.h"
24 
25 #include "qgsapplication.h"
26 #include "qgsvectordataprovider.h"
27 #include "qgsvectorlayer.h"
28 #include "qgsexpression.h"
29 
30 #include "qgssearchquerybuilder.h"
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 
45 QgsOrganizeTableColumnsDialog::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 
54  if ( vl )
55  {
56  mConfig = config;
57  mConfig.update( vl->fields() );
58 
59  mFieldsList->clear();
60 
61  const auto constColumns = mConfig.columns();
62  for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
63  {
64  QListWidgetItem *item = nullptr;
65  if ( columnConfig.type == QgsAttributeTableConfig::Action )
66  {
67  item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
68  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) ) );
69  }
70  else
71  {
72  const int idx = vl->fields().lookupField( columnConfig.name );
73  item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
74 
75  switch ( vl->fields().fieldOrigin( idx ) )
76  {
78  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
79  break;
80 
82  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/join.svg" ) ) );
83  break;
84 
85  default:
86  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/attributes.svg" ) ) );
87  break;
88  }
89  }
90 
91  item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
92  item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
93  }
94  }
95 
96  if ( !vl || mConfig.columns().count() < 7 )
97  {
98  mShowAllButton->hide();
99  mHideAllButton->hide();
100  }
101 }
102 
104 QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
105  : QgsOrganizeTableColumnsDialog( vl, vl->attributeTableConfig(), parent, flags )
106 {
107 }
109 
111 {
112  QVector<QgsAttributeTableConfig::ColumnConfig> columns;
113  columns.reserve( mFieldsList->count() );
114 
115  for ( int i = 0; i < mFieldsList->count() ; i++ )
116  {
117  const QListWidgetItem *item = mFieldsList->item( i );
118  QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
119 
120  columnConfig.hidden = item->checkState() == Qt::Unchecked;
121 
122  columns.append( columnConfig );
123  }
124 
125  QgsAttributeTableConfig config = mConfig;
126  config.setColumns( columns );
127  return config;
128 }
129 
131 {
132  for ( int i = 0; i < mFieldsList->count() ; i++ )
133  {
134  mFieldsList->item( i )->setCheckState( Qt::Checked );
135  }
136 }
137 
139 {
140  for ( int i = 0; i < mFieldsList->count() ; i++ )
141  {
142  mFieldsList->item( i )->setCheckState( Qt::Unchecked );
143  }
144 }
qgsfields.h
QgsOrganizeTableColumnsDialog::config
QgsAttributeTableConfig config() const
Gets the updated configuration.
Definition: qgsorganizetablecolumnsdialog.cpp:110
qgsmapcanvas.h
qgsexpression.h
qgsattributetableview.h
QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog
QgsOrganizeTableColumnsDialog(const QgsVectorLayer *vl, const QgsAttributeTableConfig &config, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::Window)
Constructor.
Definition: qgsorganizetablecolumnsdialog.cpp:45
qgsgui.h
QgsAttributeTableConfig::ColumnConfig
Defines the configuration of a column in the attribute table.
Definition: qgsattributetableconfig.h:52
qgsdockwidget.h
qgsfeaturelistmodel.h
QgsOrganizeTableColumnsDialog::hideAll
void hideAll()
hideAll unchecks all the fields to hide them all in the attribute table
Definition: qgsorganizetablecolumnsdialog.cpp:138
qgsrubberband.h
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:180
QgsAttributeTableConfig
This is a container for configuration of the attribute table. The configuration is specific for one v...
Definition: qgsattributetableconfig.h:36
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsOrganizeTableColumnsDialog::showAll
void showAll()
showAll checks all the fields to show them all in the attribute table
Definition: qgsorganizetablecolumnsdialog.cpp:130
QgsAttributeTableConfig::Action
@ Action
This column represents an action widget.
Definition: qgsattributetableconfig.h:46
qgsattributetablefiltermodel.h
QgsAttributeTableConfig::columns
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
Definition: qgsattributetableconfig.cpp:20
QgsFields::fieldOrigin
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Definition: qgsfields.cpp:189
qgsvectordataprovider.h
qgsmessagebar.h
qgsattributetablemodel.h
QgsAttributeTableConfig::setColumns
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
Definition: qgsattributetableconfig.cpp:50
qgsexpressionselectiondialog.h
qgsorganizetablecolumnsdialog.h
qgsvectorlayer.h
qgseditorwidgetregistry.h
QgsFields::OriginExpression
@ OriginExpression
Field is calculated from an expression.
Definition: qgsfields.h:54
QgsVectorLayer::attributeDisplayName
QString attributeDisplayName(int index) const
Convenience function that returns the attribute alias if defined or the field name else.
Definition: qgsvectorlayer.cpp:3245
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsAttributeTableConfig::ColumnConfig::hidden
bool hidden
Flag that controls if the column is hidden.
Definition: qgsattributetableconfig.h:67
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
qgslogger.h
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:349
qgsproject.h
QgsFields::OriginJoin
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:52
qgssearchquerybuilder.h
qgsexpressionbuilderdialog.h
QgsAttributeTableConfig::update
void update(const QgsFields &fields)
Update the configuration with the given fields.
Definition: qgsattributetableconfig.cpp:55