Quantum GIS API Documentation  master-693a1fe
src/gui/qgsowssourceselect.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsowssourceselect.cpp  -  selector for WMS,WFS,WCS
00003                              -------------------
00004     begin                : 3 April 2005
00005     copyright            :
00006     original             : (C) 2005 by Brendan Morley email  : morb at ozemail dot com dot au
00007     wms search           : (C) 2009 Mathias Walker <mwa at sourcepole.ch>, Sourcepole AG
00008     wms-c support        : (C) 2010 Juergen E. Fischer < jef at norbit dot de >, norBIT GmbH
00009 
00010     generalized          : (C) 2012 Radim Blazek, based on qgswmssourceselect.cpp
00011 
00012  ***************************************************************************/
00013 
00014 /***************************************************************************
00015  *                                                                         *
00016  *   This program is free software; you can redistribute it and/or modify  *
00017  *   it under the terms of the GNU General Public License as published by  *
00018  *   the Free Software Foundation; either version 2 of the License, or     *
00019  *   (at your option) any later version.                                   *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #include "qgis.h" // GEO_EPSG_CRS_ID
00024 #include "qgscontexthelp.h"
00025 #include "qgscoordinatereferencesystem.h"
00026 #include "qgsdatasourceuri.h"
00027 #include "qgsgenericprojectionselector.h"
00028 #include "qgslogger.h"
00029 #include "qgsmanageconnectionsdialog.h"
00030 #include "qgsmessageviewer.h"
00031 #include "qgsnewhttpconnection.h"
00032 #include "qgsnumericsortlistviewitem.h"
00033 #include "qgsproject.h"
00034 #include "qgsproviderregistry.h"
00035 #include "qgsowsconnection.h"
00036 #include "qgsdataprovider.h"
00037 #include "qgsowssourceselect.h"
00038 #include "qgsnetworkaccessmanager.h"
00039 
00040 #include <QButtonGroup>
00041 #include <QFileDialog>
00042 #include <QRadioButton>
00043 #include <QDomDocument>
00044 #include <QHeaderView>
00045 #include <QImageReader>
00046 #include <QInputDialog>
00047 #include <QMap>
00048 #include <QMessageBox>
00049 #include <QPicture>
00050 #include <QSettings>
00051 #include <QUrl>
00052 #include <QValidator>
00053 
00054 #include <QNetworkRequest>
00055 #include <QNetworkReply>
00056 
00057 QgsOWSSourceSelect::QgsOWSSourceSelect( QString service, QWidget * parent, Qt::WFlags fl, bool managerMode, bool embeddedMode )
00058     : QDialog( parent, fl )
00059     , mService( service )
00060     , mManagerMode( managerMode )
00061     , mEmbeddedMode( embeddedMode )
00062     , mCurrentTileset( 0 )
00063 {
00064   setupUi( this );
00065 
00066   if ( mEmbeddedMode )
00067   {
00068     mDialogButtonBox->button( QDialogButtonBox::Close )->hide();
00069   }
00070 
00071   mAddButton = mDialogButtonBox->button( QDialogButtonBox::Apply );
00072   mAddButton->setText( tr( "&Add" ) );
00073   mAddButton->setToolTip( tr( "Add selected layers to map" ) );
00074   mAddButton->setEnabled( false );
00075 
00076   clearCRS();
00077 
00078   mTileWidthLineEdit->setValidator( new QIntValidator( 0, 9999, this ) );
00079   mTileHeightLineEdit->setValidator( new QIntValidator( 0, 9999, this ) );
00080   mFeatureCountLineEdit->setValidator( new QIntValidator( 0, 9999, this ) );
00081 
00082   mCacheComboBox->addItem( tr( "Always cache" ), QNetworkRequest::AlwaysCache );
00083   mCacheComboBox->addItem( tr( "Prefer cache" ), QNetworkRequest::PreferCache );
00084   mCacheComboBox->addItem( tr( "Prefer network" ), QNetworkRequest::PreferNetwork );
00085   mCacheComboBox->addItem( tr( "Always network" ), QNetworkRequest::AlwaysNetwork );
00086 
00087   if ( !mManagerMode )
00088   {
00089     connect( mAddButton, SIGNAL( clicked() ), this, SLOT( addClicked() ) );
00090     //set the current project CRS if available
00091     long currentCRS = QgsProject::instance()->readNumEntry( "SpatialRefSys", "/ProjectCRSID", -1 );
00092     if ( currentCRS != -1 )
00093     {
00094       //convert CRS id to epsg
00095       QgsCoordinateReferenceSystem currentRefSys( currentCRS, QgsCoordinateReferenceSystem::InternalCrsId );
00096       if ( currentRefSys.isValid() )
00097       {
00098         mSelectedCRS = currentRefSys.authid();
00099       }
00100     }
00101   }
00102   else
00103   {
00104     mTabWidget->removeTab( mTabWidget->indexOf( mLayerOrderTab ) );
00105     mTabWidget->removeTab( mTabWidget->indexOf( mTilesetsTab ) );
00106     mTimeWidget->hide();
00107     mFormatWidget->hide();
00108     mCRSWidget->hide();
00109     mAddButton->hide();
00110     mCacheWidget->hide();
00111   }
00112 
00113   // set up the WMS connections we already know about
00114   populateConnectionList();
00115 
00116   QSettings settings;
00117   QgsDebugMsg( "restoring geometry" );
00118   restoreGeometry( settings.value( "/Windows/WMSSourceSelect/geometry" ).toByteArray() );
00119 }
00120 
00121 QgsOWSSourceSelect::~QgsOWSSourceSelect()
00122 {
00123   QSettings settings;
00124   QgsDebugMsg( "saving geometry" );
00125   settings.setValue( "/Windows/WMSSourceSelect/geometry", saveGeometry() );
00126 }
00127 
00128 void QgsOWSSourceSelect::clearFormats()
00129 {
00130   mFormatComboBox->clear();
00131   mFormatComboBox->setEnabled( false );
00132 }
00133 
00134 void QgsOWSSourceSelect::populateFormats()
00135 {
00136   QgsDebugMsg( "entered" );
00137 
00138   // A server may offer more similar formats, which are mapped
00139   // to the same GDAL format, e.g. GeoTIFF and TIFF
00140   // -> recreate always buttons for all available formats, enable supported
00141 
00142   clearFormats();
00143 
00144   if ( mProviderFormats.size() == 0 )
00145   {
00146     mProviderFormats = providerFormats();
00147     for ( int i = 0; i < mProviderFormats.size(); i++ )
00148     {
00149       // GDAL mime types may be image/tiff, image/png, ...
00150       mMimeLabelMap.insert( mProviderFormats[i].format, mProviderFormats[i].label );
00151     }
00152   }
00153 
00154   // selectedLayersFormats may come in various forms:
00155   // image/tiff, GTiff, GeoTIFF, TIFF, geotiff_int16, geotiff_rgb,
00156   // PNG, GTOPO30, ARCGRID, IMAGEMOSAIC
00157   // and even any string defined in server configuration, for example the
00158   // value used in UMN Mapserver for OUTPUTFORMAT->NAME is used in
00159   // WCS 1.0.0 SupportedFormats/Format
00160 
00161   // TODO: It is impossible to cover all possible formats comming from server
00162   //       -> enabled all formats, GDAL may be able to open them
00163 
00164   QMap<QString, QString> formatsMap;
00165   formatsMap.insert( "geotiff", "tiff" );
00166   formatsMap.insert( "gtiff", "tiff" );
00167   formatsMap.insert( "tiff", "tiff" );
00168   formatsMap.insert( "tif", "tiff" );
00169   formatsMap.insert( "gif", "gif" );
00170   formatsMap.insert( "jpeg", "jpeg" );
00171   formatsMap.insert( "jpg", "jpeg" );
00172   formatsMap.insert( "png", "png" );
00173 
00174   int preferred = -1;
00175   QStringList layersFormats = selectedLayersFormats();
00176   for ( int i = 0; i < layersFormats.size(); i++ )
00177   {
00178     QString format = layersFormats.value( i );
00179     QgsDebugMsg( "server format = " + format );
00180     QString simpleFormat = format.toLower().replace( "image/", "" ).replace( QRegExp( "_.*" ), "" );
00181     QgsDebugMsg( "server simpleFormat = " + simpleFormat );
00182     QString mimeFormat = "image/" + formatsMap.value( simpleFormat );
00183     QgsDebugMsg( "server mimeFormat = " + mimeFormat );
00184 
00185     QString label = format;
00186 
00187     if ( mMimeLabelMap.contains( mimeFormat ) )
00188     {
00189       if ( format != mMimeLabelMap.value( mimeFormat ) )
00190       {
00191         // Append name of GDAL driver
00192         label += " / " + mMimeLabelMap.value( mimeFormat );
00193       }
00194 
00195       if ( simpleFormat.contains( "tif" ) ) // prefer *tif*
00196       {
00197         if ( preferred < 0 || simpleFormat.startsWith( "g" ) ) // prefer geotiff
00198         {
00199           preferred = i;
00200         }
00201       }
00202     }
00203     else
00204     {
00205       // We cannot always say that the format is not supported by GDAL because
00206       // server can use strange names, but format itself is supported
00207       QgsDebugMsg( QString( "format %1 unknown" ).arg( format ) );
00208     }
00209 
00210     mFormatComboBox->insertItem( i, label );
00211   }
00212   // Set preferred
00213   // TODO: all enabled for now, see above
00214   preferred = preferred >= 0 ? preferred : 0;
00215   mFormatComboBox->setCurrentIndex( preferred );
00216 
00217   mFormatComboBox->setEnabled( true );
00218 }
00219 
00220 void QgsOWSSourceSelect::populateTimes()
00221 {
00222   QgsDebugMsg( "entered" );
00223   mTimeComboBox->clear();
00224   mTimeComboBox->insertItems( 0, selectedLayersTimes() );
00225   mTimeComboBox->setEnabled( !selectedLayersTimes().isEmpty() );
00226 }
00227 
00228 void QgsOWSSourceSelect::clearTimes()
00229 {
00230   mTimeComboBox->clear();
00231   mTimeComboBox->setEnabled( false );
00232 }
00233 
00234 void QgsOWSSourceSelect::populateConnectionList()
00235 {
00236   mConnectionsComboBox->clear();
00237   mConnectionsComboBox->addItems( QgsOWSConnection::connectionList( mService ) );
00238 
00239   setConnectionListPosition();
00240 
00241   if ( mConnectionsComboBox->count() == 0 )
00242   {
00243     // No connections - disable various buttons
00244     mConnectButton->setEnabled( false );
00245     mEditButton->setEnabled( false );
00246     mDeleteButton->setEnabled( false );
00247   }
00248   else
00249   {
00250     // Connections - enable various buttons
00251     mConnectButton->setEnabled( true );
00252     mEditButton->setEnabled( true );
00253     mDeleteButton->setEnabled( true );
00254   }
00255 }
00256 void QgsOWSSourceSelect::on_mNewButton_clicked()
00257 {
00258   QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, "/Qgis/connections-" + mService.toLower() + "/" );
00259 
00260   if ( nc->exec() )
00261   {
00262     populateConnectionList();
00263     emit connectionsChanged();
00264   }
00265 
00266   delete nc;
00267 }
00268 
00269 void QgsOWSSourceSelect::on_mEditButton_clicked()
00270 {
00271   QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, "/Qgis/connections-" + mService.toLower() + "/", mConnectionsComboBox->currentText() );
00272 
00273   if ( nc->exec() )
00274   {
00275     populateConnectionList();
00276     emit connectionsChanged();
00277   }
00278 
00279   delete nc;
00280 }
00281 
00282 void QgsOWSSourceSelect::on_mDeleteButton_clicked()
00283 {
00284   QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
00285                 .arg( mConnectionsComboBox->currentText() );
00286   QMessageBox::StandardButton result = QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel );
00287   if ( result == QMessageBox::Ok )
00288   {
00289     QgsOWSConnection::deleteConnection( mService, mConnectionsComboBox->currentText() );
00290     mConnectionsComboBox->removeItem( mConnectionsComboBox->currentIndex() );  // populateConnectionList();
00291     setConnectionListPosition();
00292     emit connectionsChanged();
00293   }
00294 }
00295 
00296 void QgsOWSSourceSelect::on_mSaveButton_clicked()
00297 {
00298   QgsManageConnectionsDialog dlg( this, QgsManageConnectionsDialog::Export, QgsManageConnectionsDialog::WCS );
00299   dlg.exec();
00300 }
00301 
00302 void QgsOWSSourceSelect::on_mLoadButton_clicked()
00303 {
00304   QString fileName = QFileDialog::getOpenFileName( this, tr( "Load connections" ), ".",
00305                      tr( "XML files (*.xml *XML)" ) );
00306   if ( fileName.isEmpty() )
00307   {
00308     return;
00309   }
00310 
00311   QgsManageConnectionsDialog dlg( this, QgsManageConnectionsDialog::Import, QgsManageConnectionsDialog::WCS, fileName );
00312   dlg.exec();
00313   populateConnectionList();
00314   emit connectionsChanged();
00315 }
00316 
00317 QgsNumericSortTreeWidgetItem *QgsOWSSourceSelect::createItem(
00318   int id,
00319   const QStringList &names,
00320   QMap<int, QgsNumericSortTreeWidgetItem *> &items,
00321   int &layerAndStyleCount,
00322   const QMap<int, int> &layerParents,
00323   const QMap<int, QStringList> &layerParentNames )
00324 {
00325   QgsDebugMsg( QString( "id = %1 layerAndStyleCount = %2 names = %3 " ).arg( id ).arg( layerAndStyleCount ).arg( names.join( "," ) ) );
00326   if ( items.contains( id ) )
00327     return items[id];
00328 
00329 
00330   QgsNumericSortTreeWidgetItem *item;
00331   if ( layerParents.contains( id ) )
00332   {
00333     // it has parent -> create first its parent
00334     int parent = layerParents[ id ];
00335     item = new QgsNumericSortTreeWidgetItem( createItem( parent, layerParentNames[ parent ], items, layerAndStyleCount, layerParents, layerParentNames ) );
00336   }
00337   else
00338     item = new QgsNumericSortTreeWidgetItem( mLayersTreeWidget );
00339 
00340   item->setText( 0, QString::number( ++layerAndStyleCount ) );
00341   item->setText( 1, names[0].simplified() );
00342   item->setText( 2, names[1].simplified() );
00343   item->setText( 3, names[2].simplified() );
00344   item->setToolTip( 3, "<font color=black>" + names[2].simplified()  + "</font>" );
00345 
00346   items[ id ] = item;
00347 
00348   return item;
00349 }
00350 
00351 void QgsOWSSourceSelect::populateLayerList( )
00352 {
00353 }
00354 
00355 void QgsOWSSourceSelect::on_mConnectButton_clicked()
00356 {
00357   QgsDebugMsg( "entered" );
00358 
00359   mLayersTreeWidget->clear();
00360   clearFormats();
00361   clearTimes();
00362   clearCRS();
00363 
00364   mConnName = mConnectionsComboBox->currentText();
00365 
00366   QgsOWSConnection connection( mService, mConnectionsComboBox->currentText() );
00367   //QgsDataProvider *theProvider = connection.provider( );
00368   mConnectionInfo = connection.connectionInfo();
00369   mUri = connection.uri();
00370 
00371   QApplication::setOverrideCursor( Qt::WaitCursor );
00372 
00373   QgsDebugMsg( "call populateLayerList" );
00374   populateLayerList();
00375 
00376   QApplication::restoreOverrideCursor();
00377 }
00378 
00379 void QgsOWSSourceSelect::addClicked()
00380 {
00381   QgsDebugMsg( "entered" );
00382 }
00383 
00384 void QgsOWSSourceSelect::enableLayersForCrs( QTreeWidgetItem * )
00385 {
00386 }
00387 
00388 void QgsOWSSourceSelect::on_mChangeCRSButton_clicked()
00389 {
00390   QStringList layers;
00391   foreach ( QTreeWidgetItem *item, mLayersTreeWidget->selectedItems() )
00392   {
00393     QString layer = item->data( 0, Qt::UserRole + 0 ).toString();
00394     if ( !layer.isEmpty() )
00395       layers << layer;
00396   }
00397 
00398   QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
00399   mySelector->setMessage();
00400   mySelector->setOgcWmsCrsFilter( mSelectedLayersCRSs );
00401 
00402   QString myDefaultCrs = QgsProject::instance()->readEntry( "SpatialRefSys", "/ProjectCrs", GEO_EPSG_CRS_AUTHID );
00403   QgsCoordinateReferenceSystem defaultCRS;
00404   if ( defaultCRS.createFromOgcWmsCrs( myDefaultCrs ) )
00405   {
00406     mySelector->setSelectedCrsId( defaultCRS.srsid() );
00407   }
00408 
00409   if ( !mySelector->exec() )
00410     return;
00411 
00412   mSelectedCRS = mySelector->selectedAuthId();
00413   delete mySelector;
00414 
00415   mSelectedCRSLabel->setText( descriptionForAuthId( mSelectedCRS ) );
00416 
00417   for ( int i = 0; i < mLayersTreeWidget->topLevelItemCount(); i++ )
00418   {
00419     enableLayersForCrs( mLayersTreeWidget->topLevelItem( i ) );
00420   }
00421 
00422   updateButtons();
00423 }
00424 
00425 void QgsOWSSourceSelect::on_mLayersTreeWidget_itemSelectionChanged()
00426 {
00427 }
00428 
00429 void QgsOWSSourceSelect::populateCRS()
00430 {
00431   QgsDebugMsg( "Entered" );
00432   clearCRS();
00433   mSelectedLayersCRSs = selectedLayersCRSs().toSet();
00434   mCRSLabel->setText( tr( "Coordinate Reference System (%n available)", "crs count", mSelectedLayersCRSs.count() ) + ":" );
00435 
00436   mChangeCRSButton->setDisabled( mSelectedLayersCRSs.isEmpty() );
00437 
00438   if ( !mSelectedLayersCRSs.isEmpty() )
00439   {
00440     // check whether current CRS is supported
00441     // if not, use one of the available CRS
00442     QString defaultCRS;
00443     QSet<QString>::const_iterator it = mSelectedLayersCRSs.begin();
00444     for ( ; it != mSelectedLayersCRSs.end(); it++ )
00445     {
00446       if ( it->compare( mSelectedCRS, Qt::CaseInsensitive ) == 0 )
00447         break;
00448 
00449       // save first CRS in case the current CRS is not available
00450       if ( it == mSelectedLayersCRSs.begin() )
00451         defaultCRS = *it;
00452 
00453       // prefer value of DEFAULT_GEO_EPSG_CRS_ID if available
00454       if ( *it == GEO_EPSG_CRS_AUTHID )
00455         defaultCRS = *it;
00456     }
00457 
00458     if ( it == mSelectedLayersCRSs.end() )
00459     {
00460       // not found
00461       mSelectedCRS = defaultCRS;
00462     }
00463     mSelectedCRSLabel->setText( descriptionForAuthId( mSelectedCRS ) );
00464     mChangeCRSButton->setEnabled( true );
00465   }
00466   QgsDebugMsg( "mSelectedCRS = " + mSelectedCRS );
00467 }
00468 
00469 void QgsOWSSourceSelect::clearCRS()
00470 {
00471   mCRSLabel->setText( tr( "Coordinate Reference System" ) + ":" );
00472   mSelectedCRS = "";
00473   mSelectedCRSLabel->setText( "" );
00474   mChangeCRSButton->setEnabled( false );
00475 }
00476 
00477 void QgsOWSSourceSelect::on_mTilesetsTableWidget_itemClicked( QTableWidgetItem *item )
00478 {
00479   Q_UNUSED( item );
00480 
00481   QTableWidgetItem *rowItem = mTilesetsTableWidget->item( mTilesetsTableWidget->currentRow(), 0 );
00482   bool wasSelected = mCurrentTileset == rowItem;
00483 
00484   mTilesetsTableWidget->blockSignals( true );
00485   mTilesetsTableWidget->clearSelection();
00486   if ( !wasSelected )
00487   {
00488     QgsDebugMsg( QString( "selecting current row %1" ).arg( mTilesetsTableWidget->currentRow() ) );
00489     mTilesetsTableWidget->selectRow( mTilesetsTableWidget->currentRow() );
00490     mCurrentTileset = rowItem;
00491   }
00492   else
00493   {
00494     mCurrentTileset = 0;
00495   }
00496   mTilesetsTableWidget->blockSignals( false );
00497 
00498   updateButtons();
00499 }
00500 
00501 
00502 
00503 QString QgsOWSSourceSelect::connName()
00504 {
00505   return mConnName;
00506 }
00507 
00508 QString QgsOWSSourceSelect::connectionInfo()
00509 {
00510   return mConnectionInfo;
00511 }
00512 
00513 QString QgsOWSSourceSelect::selectedFormat()
00514 {
00515   return selectedLayersFormats().value( mFormatComboBox->currentIndex() );
00516 }
00517 
00518 QNetworkRequest::CacheLoadControl QgsOWSSourceSelect::selectedCacheLoadControl()
00519 {
00520   int cache = mCacheComboBox->itemData( mCacheComboBox->currentIndex() ).toInt();
00521   return static_cast<QNetworkRequest::CacheLoadControl>( cache );
00522 }
00523 
00524 QString QgsOWSSourceSelect::selectedCRS()
00525 {
00526   return mSelectedCRS;
00527 }
00528 
00529 QString QgsOWSSourceSelect::selectedTime()
00530 {
00531   return mTimeComboBox->currentText();
00532 }
00533 
00534 void QgsOWSSourceSelect::setConnectionListPosition()
00535 {
00536   QString toSelect = QgsOWSConnection::selectedConnection( mService );
00537 
00538   mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->findText( toSelect ) );
00539 
00540   if ( mConnectionsComboBox->currentIndex() < 0 )
00541   {
00542     if ( toSelect.isNull() )
00543       mConnectionsComboBox->setCurrentIndex( 0 );
00544     else
00545       mConnectionsComboBox->setCurrentIndex( mConnectionsComboBox->count() - 1 );
00546   }
00547   QgsOWSConnection::setSelectedConnection( mService, mConnectionsComboBox->currentText() );
00548 }
00549 
00550 void QgsOWSSourceSelect::showStatusMessage( QString const &theMessage )
00551 {
00552   mStatusLabel->setText( theMessage );
00553 
00554   // update the display of this widget
00555   update();
00556 }
00557 
00558 
00559 void QgsOWSSourceSelect::showError( QString const &theTitle, QString const &theFormat, QString const &theError )
00560 {
00561   QgsMessageViewer * mv = new QgsMessageViewer( this );
00562   mv->setWindowTitle( theTitle );
00563 
00564   if ( theFormat == "text/html" )
00565   {
00566     mv->setMessageAsHtml( theError );
00567   }
00568   else
00569   {
00570     mv->setMessageAsPlainText( tr( "Could not understand the response:\n%1" ).arg( theError ) );
00571   }
00572   mv->showMessage( true ); // Is deleted when closed
00573 }
00574 
00575 void QgsOWSSourceSelect::on_mConnectionsComboBox_activated( int )
00576 {
00577   // Remember which server was selected.
00578   QgsOWSConnection::setSelectedConnection( mService, mConnectionsComboBox->currentText() );
00579 }
00580 
00581 void QgsOWSSourceSelect::on_mAddDefaultButton_clicked()
00582 {
00583   addDefaultServers();
00584 }
00585 
00586 QString QgsOWSSourceSelect::descriptionForAuthId( QString authId )
00587 {
00588   if ( mCrsNames.contains( authId ) )
00589     return mCrsNames[ authId ];
00590 
00591   QgsCoordinateReferenceSystem qgisSrs;
00592   qgisSrs.createFromOgcWmsCrs( authId );
00593   mCrsNames.insert( authId, qgisSrs.description() );
00594   return qgisSrs.description();
00595 }
00596 
00597 void QgsOWSSourceSelect::addDefaultServers()
00598 {
00599   QMap<QString, QString> exampleServers;
00600   exampleServers["DM Solutions GMap"] = "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap";
00601   exampleServers["Lizardtech server"] =  "http://wms.lizardtech.com/lizardtech/iserv/ows";
00602   // Nice to have the qgis users map, but I'm not sure of the URL at the moment.
00603   //  exampleServers["Qgis users map"] = "http://qgis.org/wms.cgi";
00604 
00605   QSettings settings;
00606   settings.beginGroup( "/Qgis/connections-" + mService.toLower() );
00607   QMap<QString, QString>::const_iterator i = exampleServers.constBegin();
00608   for ( ; i != exampleServers.constEnd(); ++i )
00609   {
00610     // Only do a server if it's name doesn't already exist.
00611     QStringList keys = settings.childGroups();
00612     if ( !keys.contains( i.key() ) )
00613     {
00614       QString path = i.key();
00615       settings.setValue( path + "/url", i.value() );
00616     }
00617   }
00618   settings.endGroup();
00619   populateConnectionList();
00620 
00621   QMessageBox::information( this, tr( "WMS proxies" ), "<p>" + tr( "Several WMS servers have "
00622                             "been added to the server list. Note that if "
00623                             "you access the internet via a web proxy, you will "
00624                             "need to set the proxy settings in the QGIS options dialog." ) + "</p>" );
00625 }
00626 
00627 void QgsOWSSourceSelect::addWMSListRow( const QDomElement& item, int row )
00628 {
00629   QDomElement title = item.firstChildElement( "title" );
00630   addWMSListItem( title, row, 0 );
00631   QDomElement description = item.firstChildElement( "description" );
00632   addWMSListItem( description, row, 1 );
00633   QDomElement link = item.firstChildElement( "link" );
00634   addWMSListItem( link, row, 2 );
00635 }
00636 
00637 void QgsOWSSourceSelect::addWMSListItem( const QDomElement& el, int row, int column )
00638 {
00639   if ( !el.isNull() )
00640   {
00641     QTableWidgetItem* tableItem = new QTableWidgetItem( el.text() );
00642     // TODO: add linebreaks to long tooltips?
00643     tableItem->setToolTip( el.text() );
00644     mSearchTableWidget->setItem( row, column, tableItem );
00645   }
00646 }
00647 
00648 void QgsOWSSourceSelect::on_mSearchButton_clicked()
00649 {
00650   // clear results
00651   mSearchTableWidget->clearContents();
00652   mSearchTableWidget->setRowCount( 0 );
00653 
00654   // disable Add WMS button
00655   mSearchAddButton->setEnabled( false );
00656 
00657   QApplication::setOverrideCursor( Qt::WaitCursor );
00658 
00659   QSettings settings;
00660   // geopole.org (geopole.ch) 25.4.2012 : 503 Service Unavailable, archive: Recently added 20 Jul 2011
00661   QString mySearchUrl = settings.value( "/qgis/WMSSearchUrl", "http://geopole.org/wms/search?search=%1&type=rss" ).toString();
00662   QUrl url( mySearchUrl.arg( mSearchTermLineEdit->text() ) );
00663   QgsDebugMsg( url.toString() );
00664 
00665   QNetworkReply *r = QgsNetworkAccessManager::instance()->get( QNetworkRequest( url ) );
00666   connect( r, SIGNAL( finished() ), SLOT( searchFinished() ) );
00667 }
00668 
00669 void QgsOWSSourceSelect::searchFinished()
00670 {
00671   QApplication::restoreOverrideCursor();
00672 
00673   QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
00674   if ( !r )
00675     return;
00676 
00677   if ( r->error() == QNetworkReply::NoError )
00678   {
00679     // parse results
00680     QDomDocument doc( "RSS" );
00681     QByteArray res = r->readAll();
00682     QString error;
00683     int line, column;
00684     if ( doc.setContent( res, &error, &line, &column ) )
00685     {
00686       QDomNodeList list = doc.elementsByTagName( "item" );
00687       mSearchTableWidget->setRowCount( list.size() );
00688       for ( int i = 0; i < list.size(); i++ )
00689       {
00690         if ( list.item( i ).isElement() )
00691         {
00692           QDomElement item = list.item( i ).toElement();
00693           addWMSListRow( item, i );
00694         }
00695       }
00696 
00697       mSearchTableWidget->resizeColumnsToContents();
00698     }
00699     else
00700     {
00701       QgsDebugMsg( "setContent failed" );
00702       showStatusMessage( tr( "parse error at row %1, column %2: %3" ).arg( line ).arg( column ).arg( error ) );
00703     }
00704   }
00705   else
00706   {
00707     showStatusMessage( tr( "network error: %1" ).arg( r->error() ) );
00708   }
00709 
00710   r->deleteLater();
00711 }
00712 
00713 void QgsOWSSourceSelect::on_mAddWMSButton_clicked()
00714 {
00715   // TODO: deactivate button if dialog is open?
00716   // TODO: remove from config on close?
00717 
00718   int selectedRow = mSearchTableWidget->currentRow();
00719   if ( selectedRow == -1 )
00720   {
00721     return;
00722   }
00723 
00724   QString wmsTitle = mSearchTableWidget->item( selectedRow, 0 )->text();
00725   QString wmsUrl = mSearchTableWidget->item( selectedRow, 2 )->text();
00726 
00727   QSettings settings;
00728   if ( settings.contains( QString( "Qgis/connections-wms/%1/url" ).arg( wmsTitle ) ) )
00729   {
00730     QString msg = tr( "The %1 connection already exists. Do you want to overwrite it?" ).arg( wmsTitle );
00731     QMessageBox::StandardButton result = QMessageBox::information( this, tr( "Confirm Overwrite" ), msg, QMessageBox::Ok | QMessageBox::Cancel );
00732     if ( result != QMessageBox::Ok )
00733     {
00734       return;
00735     }
00736   }
00737 
00738   // add selected WMS to config and mark as current
00739   settings.setValue( QString( "Qgis/connections-wms/%1/url" ).arg( wmsTitle ), wmsUrl );
00740   QgsOWSConnection::setSelectedConnection( mService, wmsTitle );
00741   populateConnectionList();
00742 
00743   mTabWidget->setCurrentIndex( 0 );
00744 }
00745 
00746 void QgsOWSSourceSelect::on_mSearchTableWidget_itemSelectionChanged()
00747 {
00748   mSearchAddButton->setEnabled( mSearchTableWidget->currentRow() != -1 );
00749 }
00750 
00751 void QgsOWSSourceSelect::on_mLayerUpButton_clicked()
00752 {
00753   QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
00754   if ( selectionList.size() < 1 )
00755   {
00756     return;
00757   }
00758   int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
00759   if ( selectedIndex < 1 )
00760   {
00761     return; //item not existing or already on top
00762   }
00763 
00764   QTreeWidgetItem* selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
00765   mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex - 1, selectedItem );
00766   mLayerOrderTreeWidget->clearSelection();
00767   selectedItem->setSelected( true );
00768 }
00769 
00770 void QgsOWSSourceSelect::on_mLayerDownButton_clicked()
00771 {
00772   QList<QTreeWidgetItem *> selectionList = mLayerOrderTreeWidget->selectedItems();
00773   if ( selectionList.size() < 1 )
00774   {
00775     return;
00776   }
00777   int selectedIndex = mLayerOrderTreeWidget->indexOfTopLevelItem( selectionList[0] );
00778   if ( selectedIndex < 0 || selectedIndex > mLayerOrderTreeWidget->topLevelItemCount() - 2 )
00779   {
00780     return; //item not existing or already at bottom
00781   }
00782 
00783   QTreeWidgetItem* selectedItem = mLayerOrderTreeWidget->takeTopLevelItem( selectedIndex );
00784   mLayerOrderTreeWidget->insertTopLevelItem( selectedIndex + 1, selectedItem );
00785   mLayerOrderTreeWidget->clearSelection();
00786   selectedItem->setSelected( true );
00787 }
00788 
00789 QList<QgsOWSSourceSelect::SupportedFormat> QgsOWSSourceSelect::providerFormats()
00790 {
00791   return QList<SupportedFormat>();
00792 }
00793 
00794 QStringList QgsOWSSourceSelect::selectedLayersFormats()
00795 {
00796   return QStringList();
00797 }
00798 
00799 QStringList QgsOWSSourceSelect::selectedLayersCRSs()
00800 {
00801   return QStringList();
00802 }
00803 
00804 QStringList QgsOWSSourceSelect::selectedLayersTimes()
00805 {
00806   return QStringList();
00807 }
00808 
00809 void QgsOWSSourceSelect::updateButtons()
00810 {
00811 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines