QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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, Qt::WindowFlags fl )
27  : QDialog( parent, fl ), mName( name )
28 {
29  setupUi( this );
30 
31  if ( providerType == QgsSublayersDialog::Ogr )
32  {
33  setWindowTitle( tr( "Select vector layers to add..." ) );
34  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" )
35  << tr( "Number of features" ) << tr( "Geometry type" ) );
36  }
37  else if ( providerType == QgsSublayersDialog::Gdal )
38  {
39  setWindowTitle( tr( "Select raster layers to add..." ) );
40  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" ) );
41  }
42  else
43  {
44  setWindowTitle( tr( "Select layers to add..." ) );
45  layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" )
46  << tr( "Type" ) );
47  }
48 
49  // add a "Select All" button - would be nicer with an icon
50  QPushButton* button = new QPushButton( tr( "Select All" ) );
51  buttonBox->addButton( button, QDialogButtonBox::ActionRole );
52  connect( button, SIGNAL( pressed() ), layersTable, SLOT( selectAll() ) );
53  // connect( pbnSelectNone, SIGNAL( pressed() ), SLOT( layersTable->selectNone() ) );
54 
55  QSettings settings;
56  restoreGeometry( settings.value( "/Windows/" + mName + "SubLayers/geometry" ).toByteArray() );
57 }
58 
60 {
61  QSettings settings;
62  settings.setValue( "/Windows/" + mName + "SubLayers/geometry", saveGeometry() );
63  settings.setValue( "/Windows/" + mName + "SubLayers/headerState",
64  layersTable->header()->saveState() );
65 }
66 
68 {
69  QStringList list;
70  for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
71  {
72  // If there are more sub layers of the same name (virtual for geometry types),
73  // add geometry type
74 
75  QString name = layersTable->selectedItems().at( i )->text( 1 );
76  int count = 0;
77  for ( int j = 0; j < layersTable->topLevelItemCount(); j++ )
78  {
79  if ( layersTable->topLevelItem( j )->text( 1 ) == name )
80  {
81  count++;
82  }
83  }
84 
85  if ( count > 1 )
86  {
87  name += ":" + layersTable->selectedItems().at( i )->text( 3 );
88  }
89  else
90  {
91  name += ":any";
92  }
93 
94  list << name;
95  }
96  return list;
97 }
98 
100 {
101  QList<int> list;
102  for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
103  {
104  list << layersTable->selectedItems().at( i )->text( 0 ).toInt();
105  }
106  return list;
107 }
108 
110 {
111  foreach ( QString item, theList )
112  {
113  QStringList elements = item.split( delim );
114  while ( elements.size() > 4 )
115  {
116  elements[1] += delim + elements[2];
117  elements.removeAt( 2 );
118  }
119  layersTable->addTopLevelItem( new QTreeWidgetItem( elements ) );
120  }
121 
122  // resize columns
123  QSettings settings;
124  QByteArray ba = settings.value( "/Windows/" + mName + "SubLayers/headerState" ).toByteArray();
125  if ( ! ba.isNull() )
126  {
127  layersTable->header()->restoreState( ba );
128  }
129  else
130  {
131  for ( int i = 0; i < layersTable->columnCount(); i++ )
132  layersTable->resizeColumnToContents( i );
133  layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
134  }
135 }
136 
137 // override exec() instead of using showEvent()
138 // because in some case we don't want the dialog to appear (depending on user settings)
139 // TODO alert the user when dialog is not opened
141 {
142  QSettings settings;
143  QString promptLayers = settings.value( "/qgis/promptForSublayers", 1 ).toString();
144 
145  // make sure three are sublayers to choose
146  if ( layersTable->topLevelItemCount() == 0 )
147  return QDialog::Rejected;
148 
149  // check promptForSublayers settings - perhaps this should be in QgsDataSource instead?
150  if ( promptLayers == "no" )
151  return QDialog::Rejected;
152  else if ( promptLayers == "all" )
153  {
154  layersTable->selectAll();
155  return QDialog::Accepted;
156  }
157 
158  // if there is only 1 sublayer (probably the main layer), just select that one and return
159  if ( layersTable->topLevelItemCount() == 1 )
160  {
161  layersTable->selectAll();
162  return QDialog::Accepted;
163  }
164 
165  // if we got here, disable override cursor, open dialog and return result
166  // TODO add override cursor where it is missing (e.g. when opening via "Add Raster")
167  QCursor cursor;
168  bool overrideCursor = ( QApplication::overrideCursor() != 0 );
169  if ( overrideCursor )
170  {
171  cursor = QCursor( * QApplication::overrideCursor() );
173  }
174  int ret = QDialog::exec();
175  if ( overrideCursor )
177  return ret;
178 }
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
bool restoreGeometry(const QByteArray &geometry)
void setOverrideCursor(const QCursor &cursor)
void restoreOverrideCursor()
QList< int > selectionIndexes()
QgsSublayersDialog(ProviderType providerType, QString name, QWidget *parent=0, Qt::WindowFlags fl=0)
void populateLayerTable(QStringList theList, QString delim=":")
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()