QGIS API Documentation  3.0.2-Girona (307d082)
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 
43 QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLayer *vl, QWidget *parent, Qt::WindowFlags flags )
44  : QDialog( parent, flags )
45 {
46  setupUi( this );
47 
48  connect( mShowAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::showAll );
49  connect( mHideAllButton, &QAbstractButton::clicked, this, &QgsOrganizeTableColumnsDialog::hideAll );
50 
51  if ( vl )
52  {
53  mConfig = vl->attributeTableConfig();
54  mConfig.update( vl->fields() );
55 
56  mFieldsList->clear();
57 
58  Q_FOREACH ( const QgsAttributeTableConfig::ColumnConfig &columnConfig, mConfig.columns() )
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( QStringLiteral( "/propertyicons/action.svg" ) ) );
65  }
66  else
67  {
68  int idx = vl->fields().lookupField( columnConfig.name );
69  item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
70 
71  switch ( vl->fields().fieldOrigin( idx ) )
72  {
74  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
75  break;
76 
78  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/join.png" ) ) );
79  break;
80 
81  default:
82  item->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/attributes.png" ) ) );
83  break;
84  }
85  }
86 
87  item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
88  item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
89  }
90  }
91 
92  if ( !vl || mConfig.columns().count() < 7 )
93  {
94  mShowAllButton->hide();
95  mHideAllButton->hide();
96  }
97 
98  QgsSettings settings;
99  restoreGeometry( settings.value( QStringLiteral( "Windows/QgsOrganizeTableColumnsDialog/geometry" ) ).toByteArray() );
100 }
101 
103 {
104  QgsSettings settings;
105  settings.setValue( QStringLiteral( "Windows/QgsOrganizeTableColumnsDialog/geometry" ), saveGeometry() );
106 }
107 
109 {
110  QVector<QgsAttributeTableConfig::ColumnConfig> columns;
111  columns.reserve( mFieldsList->count() );
112 
113  for ( int i = 0; i < mFieldsList->count() ; i++ )
114  {
115  const QListWidgetItem *item = mFieldsList->item( i );
116  QgsAttributeTableConfig::ColumnConfig columnConfig = item->data( Qt::UserRole ).value<QgsAttributeTableConfig::ColumnConfig>();
117 
118  columnConfig.hidden = item->checkState() == Qt::Unchecked;
119 
120  columns.append( columnConfig );
121  }
122 
124  config.setColumns( columns );
125  return config;
126 }
127 
129 {
130  for ( int i = 0; i < mFieldsList->count() ; i++ )
131  {
132  mFieldsList->item( i )->setCheckState( Qt::Checked );
133  }
134 }
135 
137 {
138  for ( int i = 0; i < mFieldsList->count() ; i++ )
139  {
140  mFieldsList->item( i )->setCheckState( Qt::Unchecked );
141  }
142 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:299
QgsAttributeTableConfig::Type type
The type of this column.
void update(const QgsFields &fields)
Update the configuration with the given fields.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:50
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfields.cpp:171
QgsAttributeTableConfig config() const
Get the updated configuration.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
This column represents an action widget.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsFields fields() const override
Returns the list of fields of this layer.
void hideAll()
hideAll unchecks all the fields to hide them all in the attribute table
bool hidden
Flag that controls if the column is hidden.
void showAll()
showAll checks all the fields to show them all in the attribute table
QString name
The name of the attribute if this column represents a field.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Get the list with all columns and their configuration.
QgsAttributeTableConfig attributeTableConfig() const
Get the attribute table configuration object.
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
Defines the configuration of a column in the attribute table.
This is a container for configuration of the attribute table.
QgsOrganizeTableColumnsDialog(const QgsVectorLayer *vl, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::Window)
Constructor.
Represents a vector layer which manages a vector based data sets.
Field is calculated from an expression.
Definition: qgsfields.h:52
QString attributeDisplayName(int index) const
Convenience function that returns the attribute alias if defined or the field name else...