|
| 1 | +#include "qgsbrowserdockwidget.h" |
| 2 | + |
| 3 | +#include <QTreeView> |
| 4 | + |
| 5 | +#include "qgsbrowsermodel.h" |
| 6 | +#include "qgsdataitem.h" |
| 7 | +#include "qgslogger.h" |
| 8 | +#include "qgsmaplayerregistry.h" |
| 9 | +#include "qgsrasterlayer.h" |
| 10 | +#include "qgsvectorlayer.h" |
| 11 | + |
| 12 | +QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) : |
| 13 | + QDockWidget( parent ), mModel( NULL ) |
| 14 | +{ |
| 15 | + setWindowTitle( tr( "Browser" ) ); |
| 16 | + |
| 17 | + mBrowserView = new QTreeView( this ); |
| 18 | + setWidget( mBrowserView ); |
| 19 | + |
| 20 | + //connect( mBrowserView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( itemClicked( const QModelIndex& ) ) ); |
| 21 | + connect( mBrowserView, SIGNAL( doubleClicked( const QModelIndex& ) ), this, SLOT( itemClicked( const QModelIndex& ) ) ); |
| 22 | +} |
| 23 | + |
| 24 | +void QgsBrowserDockWidget::showEvent( QShowEvent * e ) |
| 25 | +{ |
| 26 | + // delayed initialization of the model |
| 27 | + if ( mModel == NULL ) |
| 28 | + { |
| 29 | + mModel = new QgsBrowserModel( mBrowserView ); |
| 30 | + mBrowserView->setModel( mModel ); |
| 31 | + } |
| 32 | + |
| 33 | + QDockWidget::showEvent( e ); |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +void QgsBrowserDockWidget::itemClicked( const QModelIndex& index ) |
| 38 | +{ |
| 39 | + QgsDataItem *item = mModel->dataItem( index ); |
| 40 | + if ( !item ) |
| 41 | + return; |
| 42 | + |
| 43 | + QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( mModel->dataItem( index ) ); |
| 44 | + if ( layerItem == NULL ) |
| 45 | + return; |
| 46 | + |
| 47 | + QString uri = layerItem->uri(); |
| 48 | + if ( uri.isEmpty() ) |
| 49 | + return; |
| 50 | + |
| 51 | + QgsMapLayer::LayerType type = layerItem->mapLayerType(); |
| 52 | + QString providerKey = layerItem->providerKey(); |
| 53 | + |
| 54 | + QgsDebugMsg( providerKey + " : " + uri ); |
| 55 | + QgsMapLayer* layer = NULL; |
| 56 | + if ( type == QgsMapLayer::VectorLayer ) |
| 57 | + { |
| 58 | + layer = new QgsVectorLayer( uri, layerItem->name(), providerKey ); |
| 59 | + } |
| 60 | + if ( type == QgsMapLayer::RasterLayer ) |
| 61 | + { |
| 62 | + // This should go to WMS provider |
| 63 | + QStringList URIParts = uri.split( "|" ); |
| 64 | + QString rasterLayerPath = URIParts.at( 0 ); |
| 65 | + QStringList layers; |
| 66 | + QStringList styles; |
| 67 | + QString format; |
| 68 | + QString crs; |
| 69 | + for ( int i = 1 ; i < URIParts.size(); i++ ) |
| 70 | + { |
| 71 | + QString part = URIParts.at( i ); |
| 72 | + int pos = part.indexOf( "=" ); |
| 73 | + QString field = part.left( pos ); |
| 74 | + QString value = part.mid( pos + 1 ); |
| 75 | + |
| 76 | + if ( field == "layers" ) |
| 77 | + layers = value.split( "," ); |
| 78 | + if ( field == "styles" ) |
| 79 | + styles = value.split( "," ); |
| 80 | + if ( field == "format" ) |
| 81 | + format = value; |
| 82 | + if ( field == "crs" ) |
| 83 | + crs = value; |
| 84 | + } |
| 85 | + QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath ); |
| 86 | + QgsDebugMsg( "layers = " + layers.join( " " ) ); |
| 87 | + |
| 88 | + layer = new QgsRasterLayer( 0, rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs ); |
| 89 | + } |
| 90 | + |
| 91 | + if ( !layer || !layer->isValid() ) |
| 92 | + { |
| 93 | + qDebug( "No layer" ); |
| 94 | + delete layer; |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // add layer to the application |
| 99 | + QgsMapLayerRegistry::instance()->addMapLayer( layer ); |
| 100 | +} |
0 commit comments