Skip to content
Permalink
Browse files
New dock in QGIS for layer tree view
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent de146fb commit 129d295ea0d9f884f4187eebc63c598906be7774
@@ -443,6 +443,7 @@ INCLUDE_DIRECTORIES(
../gui/raster
../gui/editorwidgets
../gui/editorwidgets/core
../gui/layertree
../plugins
../python
gps
@@ -134,6 +134,8 @@
#include "qgsgpsinformationwidget.h"
#include "qgsguivectorlayertools.h"
#include "qgslabelinggui.h"
#include "qgslayertreemodel.h"
#include "qgslayertreeview.h"
#include "qgslegend.h"
#include "qgslayerorder.h"
#include "qgslegendlayer.h"
@@ -538,6 +540,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
createStatusBar();
createCanvasTools();
mMapCanvas->freeze();
initLayerTreeView();
initLegend();
createOverview();
createMapTips();
@@ -2170,6 +2173,21 @@ QgsMessageBar* QgisApp::messageBar()
return mInfoBar;
}

void QgisApp::initLayerTreeView()
{
mLayerTreeDock = new QDockWidget( tr( "Layers NEW" ), this );
mLayerTreeDock->setObjectName( "LayersNEW" );
mLayerTreeDock->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );

QgsLayerTreeModel* model = new QgsLayerTreeModel( QgsProject::instance()->layerTreeRoot(), this );

mLayerTreeView = new QgsLayerTreeView( this );
mLayerTreeView->setModel( model );

mLayerTreeDock->setWidget( mLayerTreeView );
addDockWidget( Qt::LeftDockWidgetArea, mLayerTreeDock );
}

void QgisApp::initLegend()
{
mMapLegend->setWhatsThis( tr( "Map legend that displays all the layers currently on the map canvas. Click on the check box to turn a layer on or off. Double click on a layer in the legend to customize its appearance and set other properties." ) );
@@ -46,7 +46,7 @@ class QgsComposerManager;
class QgsContrastEnhancement;
class QgsGeometry;
class QgsFeature;

class QgsLayerTreeView;
class QgsLegend;
class QgsLayerOrder;
class QgsMapCanvas;
@@ -1310,6 +1310,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void createToolBars();
void createStatusBar();
void setupConnections();
void initLayerTreeView();
void initLegend();
void createOverview();
void createCanvasTools();
@@ -1353,6 +1354,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QMenu *mToolbarMenu;

// docks ------------------------------------------
QDockWidget *mLayerTreeDock;
QDockWidget *mLegendDock;
QDockWidget *mLayerOrderDock;
QDockWidget *mOverviewDock;
@@ -1452,6 +1454,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Map canvas
QgsMapCanvas *mMapCanvas;
//! Table of contents (legend) for the map
QgsLayerTreeView* mLayerTreeView;
//! Table of contents (legend) for the map
QgsLegend *mMapLegend;
//! Table of contents (legend) to order layers of the map
QgsLayerOrder *mMapLayerOrder;
@@ -223,15 +223,7 @@ QgsLayerTreeGroup* QgsLayerTreeGroup::readXML(QDomElement& element)

groupNode->readCommonXML(element);

QDomElement childElem = element.firstChildElement();
while (!childElem.isNull())
{
QgsLayerTreeNode* newNode = QgsLayerTreeNode::readXML(childElem);
if (newNode)
groupNode->addChildNode(newNode);

childElem = childElem.nextSiblingElement();
}
groupNode->readChildrenFromXML(element);

return groupNode;
}
@@ -252,6 +244,19 @@ void QgsLayerTreeGroup::writeXML(QDomElement& parentElement)
parentElement.appendChild(elem);
}

void QgsLayerTreeGroup::readChildrenFromXML(QDomElement& element)
{
QDomElement childElem = element.firstChildElement();
while (!childElem.isNull())
{
QgsLayerTreeNode* newNode = QgsLayerTreeNode::readXML(childElem);
if (newNode)
addChildNode(newNode);

childElem = childElem.nextSiblingElement();
}
}

QString QgsLayerTreeGroup::dump() const
{
QString header = QString( "GROUP: %1 visible=%2 expanded=%3\n" ).arg( name() ).arg( mChecked ).arg( mExpanded );
@@ -152,6 +152,8 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode
static QgsLayerTreeGroup* readXML(QDomElement& element);
virtual void writeXML(QDomElement& parentElement);

void readChildrenFromXML(QDomElement& element);

virtual QString dump() const;

Qt::CheckState isVisible() const { return mChecked; }
@@ -4,13 +4,12 @@

#include <QDomElement>

QgsLayerTreeGroup* QgsLayerTreeUtils::readOldLegend(const QDomElement& legendElem)
bool QgsLayerTreeUtils::readOldLegend(QgsLayerTreeGroup* root, const QDomElement& legendElem)
{
if (legendElem.isNull())
return 0;
return false;

QDomNodeList legendChildren = legendElem.childNodes();
QgsLayerTreeGroup* root = new QgsLayerTreeGroup;

for ( int i = 0; i < legendChildren.size(); ++i )
{
@@ -25,7 +24,7 @@ QgsLayerTreeGroup* QgsLayerTreeUtils::readOldLegend(const QDomElement& legendEle
}
}

return root;
return true;
}

QString QgsLayerTreeUtils::checkStateToXml(Qt::CheckState state)
@@ -12,7 +12,7 @@ class CORE_EXPORT QgsLayerTreeUtils
public:

// return a new instance - or null on error
static QgsLayerTreeGroup* readOldLegend(const QDomElement& legendElem);
static bool readOldLegend(QgsLayerTreeGroup* root, const QDomElement& legendElem);

static QString checkStateToXml(Qt::CheckState state);
static Qt::CheckState checkStateFromXml(QString txt);
@@ -24,6 +24,7 @@
#include "qgsexception.h"
#include "qgslayertreenode.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeregistrybridge.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgspluginlayer.h"
@@ -334,10 +335,15 @@ QgsProject::QgsProject()
: imp_( new QgsProject::Imp )
, mBadLayerHandler( new QgsProjectBadLayerDefaultHandler() )
, mRelationManager( new QgsRelationManager( this ) )
, mRootGroup( 0 )
, mRootGroup( new QgsLayerTreeGroup )
{
clear();

// bind the layer tree to the map layer registry.
// whenever layers are added to or removed from the registry,
// layer tree will be updated
new QgsLayerTreeRegistryBridge(mRootGroup, this);

} // QgsProject ctor


@@ -419,8 +425,7 @@ void QgsProject::clear()
mEmbeddedLayers.clear();
mRelationManager->clear();

delete mRootGroup;
mRootGroup = new QgsLayerTreeGroup;
mRootGroup->removeChildren(0, mRootGroup->children().count());

// reset some default project properties
// XXX THESE SHOULD BE MOVED TO STATUSBAR RELATED SOURCE
@@ -898,23 +903,17 @@ bool QgsProject::read()

// read the layer tree from project file

QgsLayerTreeGroup* newRoot = 0;
QDomElement layerTreeElem = doc->documentElement().firstChildElement( "layer-tree-group" );
if ( !layerTreeElem.isNull() )
{
newRoot = QgsLayerTreeGroup::readXML( layerTreeElem );
mRootGroup->readChildrenFromXML( layerTreeElem );
}
else
{
newRoot = QgsLayerTreeUtils::readOldLegend( doc->documentElement().firstChildElement( "legend" ) );
QgsLayerTreeUtils::readOldLegend( mRootGroup, doc->documentElement().firstChildElement( "legend" ) );
}

if ( newRoot )
{
delete mRootGroup;
mRootGroup = newRoot;
QgsDebugMsg( "Loaded layer tree:\n " + mRootGroup->dump() );
}
QgsDebugMsg( "Loaded layer tree:\n " + mRootGroup->dump() );

// get the map layers
QPair< bool, QList<QDomNode> > getMapLayersResults = _getMapLayers( *doc );

0 comments on commit 129d295

Please sign in to comment.