QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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"
34 #include "qgsattributeaction.h"
36 #include "qgsmessagebar.h"
38 #include "qgsfeaturelistmodel.h"
39 #include "qgsrubberband.h"
40 #include "qgsfield.h"
42 
43 
45  : QDialog( parent, flags )
46 {
47  setupUi( this );
48 
49  connect( mShowAllButton, SIGNAL( clicked( bool ) ), this, SLOT( showAll() ) );
50  connect( mHideAllButton, SIGNAL( clicked( bool ) ), this, SLOT( hideAll() ) );
51 
52  if ( vl )
53  {
54  mConfig = vl->attributeTableConfig();
55  mConfig.update( vl->fields() );
56 
57  mFieldsList->clear();
58 
59  Q_FOREACH ( const QgsAttributeTableConfig::ColumnConfig& columnConfig, mConfig.columns() )
60  {
61  QListWidgetItem* item;
62  if ( columnConfig.type == QgsAttributeTableConfig::Action )
63  {
64  item = new QListWidgetItem( tr( "[Action Widget]" ), mFieldsList );
65  item->setIcon( QgsApplication::getThemeIcon( "/propertyicons/action.svg" ) );
66  }
67  else
68  {
69  int idx = vl->fieldNameIndex( columnConfig.name );
70  item = new QListWidgetItem( vl->attributeDisplayName( idx ), mFieldsList );
71 
72  switch ( vl->fields().fieldOrigin( idx ) )
73  {
75  item->setIcon( QgsApplication::getThemeIcon( "/mIconExpression.svg" ) );
76  break;
77 
79  item->setIcon( QgsApplication::getThemeIcon( "/propertyicons/join.png" ) );
80  break;
81 
82  default:
83  item->setIcon( QgsApplication::getThemeIcon( "/propertyicons/attributes.png" ) );
84  break;
85  }
86  }
87 
88  item->setCheckState( columnConfig.hidden ? Qt::Unchecked : Qt::Checked );
89  item->setData( Qt::UserRole, QVariant::fromValue( columnConfig ) );
90  }
91  }
92 
93  if ( !vl || mConfig.columns().count() < 7 )
94  {
95  mShowAllButton->hide();
96  mHideAllButton->hide();
97  }
98 
99 
100  QSettings settings;
101  restoreGeometry( settings.value( "/Windows/QgsOrganizeTableColumnsDialog/geometry" ).toByteArray() );
102 }
103 
105 {
106  QSettings settings;
107  settings.setValue( "/Windows/QgsOrganizeTableColumnsDialog/geometry", saveGeometry() );
108 }
109 
111 {
113 
114  for ( int i = 0 ; i < mFieldsList->count() ; i++ )
115  {
116  const QListWidgetItem* item = mFieldsList->item( i );
118 
119  columnConfig.hidden = item->checkState() == Qt::Unchecked;
120 
121  columns.append( columnConfig );
122  }
123 
125  config.setColumns( columns );
126  return config;
127 }
128 
130 {
131  for ( int i = 0 ; i < mFieldsList->count() ; i++ )
132  {
133  mFieldsList->item( i )->setCheckState( Qt::Checked );
134  }
135 }
136 
138 {
139  for ( int i = 0 ; i < mFieldsList->count() ; i++ )
140  {
141  mFieldsList->item( i )->setCheckState( Qt::Unchecked );
142  }
143 }
QByteArray toByteArray() const
void update(const QgsFields &fields)
Update the configuration with the given fields.
Qt::CheckState checkState() const
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:260
void setupUi(QWidget *widget)
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfield.cpp:448
QgsAttributeTableConfig config() const
Get the updated configuration.
void append(const T &value)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
T value() const
This column represents an action widget.
QString tr(const char *sourceText, const char *disambiguation, int n)
QgsFields fields() const
Returns the list of fields of this layer.
void setValue(const QString &key, const QVariant &value)
bool restoreGeometry(const QByteArray &geometry)
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.
virtual QVariant data(int role) const
void setCheckState(Qt::CheckState state)
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.
void setIcon(const QIcon &icon)
virtual void setData(int role, const QVariant &value)
QVariant fromValue(const T &value)
QVector< ColumnConfig > columns() const
Get the list with all columns and their configuration.
QgsAttributeTableConfig attributeTableConfig() const
Get the attribute table configuration object.
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
Defines the configuration of a column in the attribute table.
typedef WindowFlags
This is a container for configuration of the attribute table.
QgsOrganizeTableColumnsDialog(const QgsVectorLayer *vl, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::Window)
Constructor.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
field is calculated from an expression
Definition: qgsfield.h:262
QString attributeDisplayName(int attributeIndex) const
Convenience function that returns the attribute alias if defined or the field name else...
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
void setColumns(const QVector< ColumnConfig > &columns)
Set the list of columns visible in the attribute table.