Skip to content

Commit f57bef4

Browse files
committed
[FEATURE] Add browser dock widget to QGIS main window. Layers can be added to canvas by double-clicking them in the browser.
1 parent ddc5b67 commit f57bef4

9 files changed

+144
-4
lines changed

src/app/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SET(QGIS_APP_SRCS
1313
qgsattributetypeloaddialog.cpp
1414
qgsbookmarkitem.cpp
1515
qgsbookmarks.cpp
16+
qgsbrowserdockwidget.cpp
1617
qgsclipboard.cpp
1718
qgscontinuouscolordialog.cpp
1819
qgsconfigureshortcutsdialog.cpp
@@ -146,6 +147,7 @@ SET (QGIS_APP_MOC_HDRS
146147
qgsattributetypedialog.h
147148
qgsattributetypeloaddialog.h
148149
qgsbookmarks.h
150+
qgsbrowserdockwidget.h
149151
qgsconfigureshortcutsdialog.h
150152
qgscontinuouscolordialog.h
151153
qgscustomization.h

src/app/qgisapp.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#include "qgsattributetabledialog.h"
104104
#include "qgsbookmarkitem.h"
105105
#include "qgsbookmarks.h"
106+
#include "qgsbrowserdockwidget.h"
106107
#include "qgsclipboard.h"
107108
#include "qgscomposer.h"
108109
#include "qgscomposermanager.h"
@@ -446,6 +447,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
446447
mSnappingDialog = new QgsSnappingDialog( this, mMapCanvas );
447448
mSnappingDialog->setObjectName( "SnappingOption" );
448449

450+
mBrowserWidget = new QgsBrowserDockWidget( this );
451+
mBrowserWidget->setObjectName( "Browser" );
452+
addDockWidget( Qt::LeftDockWidgetArea, mBrowserWidget );
453+
mBrowserWidget->hide();
454+
449455
mInternalClipboard = new QgsClipboard; // create clipboard
450456
mQgisInterface = new QgisAppInterface( this ); // create the interfce
451457

src/app/qgisapp.h

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class QNetworkReply;
6565
class QNetworkProxy;
6666
class QAuthenticator;
6767

68+
class QgsBrowserDockWidget;
6869
class QgsSnappingDialog;
6970
class QgsGPSInformationWidget;
7071

@@ -1049,6 +1050,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
10491050

10501051
QgsUndoWidget* mUndoWidget;
10511052

1053+
QgsBrowserDockWidget* mBrowserWidget;
1054+
10521055
QgsSnappingDialog* mSnappingDialog;
10531056

10541057
//! Persistent tile scale slider

src/app/qgsbrowserdockwidget.cpp

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

src/app/qgsbrowserdockwidget.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef QGSBROWSERDOCKWIDGET_H
2+
#define QGSBROWSERDOCKWIDGET_H
3+
4+
#include <QDockWidget>
5+
6+
class QgsBrowserModel;
7+
class QModelIndex;
8+
class QTreeView;
9+
10+
class QgsBrowserDockWidget : public QDockWidget
11+
{
12+
Q_OBJECT
13+
public:
14+
explicit QgsBrowserDockWidget( QWidget *parent = 0 );
15+
16+
signals:
17+
18+
public slots:
19+
void itemClicked( const QModelIndex& index );
20+
21+
protected:
22+
23+
void showEvent( QShowEvent * event );
24+
25+
QTreeView* mBrowserView;
26+
QgsBrowserModel* mModel;
27+
};
28+
29+
#endif // QGSBROWSERDOCKWIDGET_H

src/browser/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
SET (BROWSER_SRCS
66
main.cpp
77
qgsbrowser.cpp
8-
qgsbrowsermodel.cpp
98
)
109

1110
SET (BROWSER_UIS qgsbrowserbase.ui)
1211

1312
SET (BROWSER_MOC_HDRS
1413
qgsbrowser.h
15-
qgsbrowsermodel.h
1614
)
1715

1816
#SET (BROWSER_RCCS qgsgps_plugin.qrc)

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SET(QGIS_CORE_SRCS
4242
qgis.cpp
4343
qgsapplication.cpp
4444
qgsattributeaction.cpp
45+
qgsbrowsermodel.cpp
4546
qgscentralpointpositionmanager.cpp
4647
qgsclipper.cpp
4748
qgscontexthelp.cpp
@@ -220,6 +221,7 @@ ADD_BISON_FILES(QGIS_CORE_SRCS qgssearchstringparser.yy)
220221

221222
SET(QGIS_CORE_MOC_HDRS
222223
qgsapplication.h
224+
qgsbrowsermodel.h
223225
qgscontexthelp.h
224226
qgscoordinatetransform.h
225227
qgsdataitem.h
File renamed without changes.

src/browser/qgsbrowsermodel.h renamed to src/core/qgsbrowsermodel.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "qgsdataitem.h"
99

10-
class QgsBrowserModel : public QAbstractItemModel
10+
class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
1111
{
1212
Q_OBJECT
1313

@@ -50,7 +50,7 @@ class QgsBrowserModel : public QAbstractItemModel
5050

5151
virtual QStringList mimeTypes() const;
5252

53-
QMimeData * mimeData(const QModelIndexList &indexes) const;
53+
QMimeData * mimeData( const QModelIndexList &indexes ) const;
5454

5555
QgsDataItem *dataItem( const QModelIndex &idx ) const;
5656

0 commit comments

Comments
 (0)