QGIS API Documentation  2.15.0-Master (af20121)
qgssublayersdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssublayersdialog.cpp - dialog for selecting sublayers
3  ---------------------
4  begin : January 2009
5  copyright : (C) 2009 by Florian El Ahdab
6  email : felahdab at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgssublayersdialog.h"
17 
18 #include "qgslogger.h"
19 
20 #include <QSettings>
21 #include <QTableWidgetItem>
22 #include <QPushButton>
23 
24 
26  QWidget* parent, const Qt::WindowFlags& fl )
27  : QDialog( parent, fl )
28  , mName( name )
29 {
30  setupUi( this );
31 
32  if ( providerType == QgsSublayersDialog::Ogr )
33  {
34  setWindowTitle( tr( "Select vector layers to add..." ) );
35  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" )
36  << tr( "Number of features" ) << tr( "Geometry type" ) );
37  }
38  else if ( providerType == QgsSublayersDialog::Gdal )
39  {
40  setWindowTitle( tr( "Select raster layers to add..." ) );
41  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" ) );
42  }
43  else
44  {
45  setWindowTitle( tr( "Select layers to add..." ) );
46  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" )
47  << tr( "Type" ) );
48  }
49 
50  // add a "Select All" button - would be nicer with an icon
51  QPushButton* button = new QPushButton( tr( "Select All" ) );
52  buttonBox->addButton( button, QDialogButtonBox::ActionRole );
53  connect( button, SIGNAL( pressed() ), layersTable, SLOT( selectAll() ) );
54  // connect( pbnSelectNone, SIGNAL( pressed() ), SLOT( layersTable->selectNone() ) );
55 
56  QSettings settings;
57  restoreGeometry( settings.value( "/Windows/" + mName + "SubLayers/geometry" ).toByteArray() );
58 }
59 
61 {
62  QSettings settings;
63  settings.setValue( "/Windows/" + mName + "SubLayers/geometry", saveGeometry() );
64  settings.setValue( "/Windows/" + mName + "SubLayers/headerState",
65  layersTable->header()->saveState() );
66 }
67 
69 {
70  QStringList list;
71  for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
72  {
73  // If there are more sub layers of the same name (virtual for geometry types),
74  // add geometry type
75 
76  QString name = layersTable->selectedItems().at( i )->text( 1 );
77  int count = 0;
78  for ( int j = 0; j < layersTable->topLevelItemCount(); j++ )
79  {
80  if ( layersTable->topLevelItem( j )->text( 1 ) == name )
81  {
82  count++;
83  }
84  }
85 
86  if ( count > 1 )
87  {
88  name += ':' + layersTable->selectedItems().at( i )->text( 3 );
89  }
90  else
91  {
92  name += ":any";
93  }
94 
95  list << name;
96  }
97  return list;
98 }
99 
101 {
102  QList<int> list;
103  for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
104  {
105  list << layersTable->selectedItems().at( i )->text( 0 ).toInt();
106  }
107  return list;
108 }
109 
110 void QgsSublayersDialog::populateLayerTable( const QStringList& theList, const QString& delim )
111 {
112  Q_FOREACH ( const QString& item, theList )
113  {
114  QStringList elements = item.split( delim );
115  while ( elements.size() > 4 )
116  {
117  elements[1] += delim + elements[2];
118  elements.removeAt( 2 );
119  }
120  layersTable->addTopLevelItem( new QTreeWidgetItem( elements ) );
121  }
122 
123  // resize columns
124  QSettings settings;
125  QByteArray ba = settings.value( "/Windows/" + mName + "SubLayers/headerState" ).toByteArray();
126  if ( ! ba.isNull() )
127  {
128  layersTable->header()->restoreState( ba );
129  }
130  else
131  {
132  for ( int i = 0; i < layersTable->columnCount(); i++ )
133  layersTable->resizeColumnToContents( i );
134  layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
135  }
136 }
137 
138 // override exec() instead of using showEvent()
139 // because in some case we don't want the dialog to appear (depending on user settings)
140 // TODO alert the user when dialog is not opened
142 {
143  QSettings settings;
144  QString promptLayers = settings.value( "/qgis/promptForSublayers", 1 ).toString();
145 
146  // make sure three are sublayers to choose
147  if ( layersTable->topLevelItemCount() == 0 )
148  return QDialog::Rejected;
149 
150  // check promptForSublayers settings - perhaps this should be in QgsDataSource instead?
151  if ( promptLayers == "no" )
152  return QDialog::Rejected;
153  else if ( promptLayers == "all" )
154  {
155  layersTable->selectAll();
156  return QDialog::Accepted;
157  }
158 
159  // if there is only 1 sublayer (probably the main layer), just select that one and return
160  if ( layersTable->topLevelItemCount() == 1 )
161  {
162  layersTable->selectAll();
163  return QDialog::Accepted;
164  }
165 
166  layersTable->sortByColumn( 1, Qt::AscendingOrder );
167  layersTable->setSortingEnabled( true );
168 
169  // if we got here, disable override cursor, open dialog and return result
170  // TODO add override cursor where it is missing (e.g. when opening via "Add Raster")
171  QCursor cursor;
172  bool overrideCursor = nullptr != QApplication::overrideCursor();
173  if ( overrideCursor )
174  {
175  cursor = QCursor( * QApplication::overrideCursor() );
177  }
178  int ret = QDialog::exec();
179  if ( overrideCursor )
181  return ret;
182 }
QByteArray toByteArray() const
void setupUi(QWidget *widget)
QCursor cursor() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
const T & at(int i) const
bool isNull() const
void removeAt(int i)
QStringList selectionNames()
int exec()
QString tr(const char *sourceText, const char *disambiguation, int n)
int size() const
void setValue(const QString &key, const QVariant &value)
const char * name() const
void populateLayerTable(const QStringList &theList, const QString &delim=":")
bool restoreGeometry(const QByteArray &geometry)
QgsSublayersDialog(ProviderType providerType, const QString &name, QWidget *parent=nullptr, const Qt::WindowFlags &fl=nullptr)
void setOverrideCursor(const QCursor &cursor)
void restoreOverrideCursor()
QList< int > selectionIndexes()
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
const QChar at(int position) const
void setWindowTitle(const QString &)
typedef WindowFlags
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
QCursor * overrideCursor()