QGIS API Documentation  2.8.2-Wien
 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  if ( count > 1 )
85  {
86  name += ":" + layersTable->selectedItems().at( i )->text( 3 );
87  }
88  list << name;
89  }
90  return list;
91 }
92 
94 {
95  QList<int> list;
96  for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
97  {
98  list << layersTable->selectedItems().at( i )->text( 0 ).toInt();
99  }
100  return list;
101 }
102 
103 void QgsSublayersDialog::populateLayerTable( QStringList theList, QString delim )
104 {
105  foreach ( QString item, theList )
106  {
107  layersTable->addTopLevelItem( new QTreeWidgetItem( item.split( delim ) ) );
108  }
109 
110  // resize columns
111  QSettings settings;
112  QByteArray ba = settings.value( "/Windows/" + mName + "SubLayers/headerState" ).toByteArray();
113  if ( ! ba.isNull() )
114  {
115  layersTable->header()->restoreState( ba );
116  }
117  else
118  {
119  for ( int i = 0; i < layersTable->columnCount(); i++ )
120  layersTable->resizeColumnToContents( i );
121  layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
122  }
123 }
124 
125 // override exec() instead of using showEvent()
126 // because in some case we don't want the dialog to appear (depending on user settings)
127 // TODO alert the user when dialog is not opened
129 {
130  QSettings settings;
131  QString promptLayers = settings.value( "/qgis/promptForSublayers", 1 ).toString();
132 
133  // make sure three are sublayers to choose
134  if ( layersTable->topLevelItemCount() == 0 )
135  return QDialog::Rejected;
136 
137  // check promptForSublayers settings - perhaps this should be in QgsDataSource instead?
138  if ( promptLayers == "no" )
139  return QDialog::Rejected;
140  else if ( promptLayers == "all" )
141  {
142  layersTable->selectAll();
143  return QDialog::Accepted;
144  }
145 
146  // if there is only 1 sublayer (probably the main layer), just select that one and return
147  if ( layersTable->topLevelItemCount() == 1 )
148  {
149  layersTable->selectAll();
150  return QDialog::Accepted;
151  }
152 
153  // if we got here, disable override cursor, open dialog and return result
154  // TODO add override cursor where it is missing (e.g. when opening via "Add Raster")
155  QCursor cursor;
156  bool overrideCursor = ( QApplication::overrideCursor() != 0 );
157  if ( overrideCursor )
158  {
159  cursor = QCursor( * QApplication::overrideCursor() );
160  QApplication::restoreOverrideCursor();
161  }
162  int ret = QDialog::exec();
163  if ( overrideCursor )
164  QApplication::setOverrideCursor( cursor );
165  return ret;
166 }