29 #include <QApplication>
30 #include <QDomDocument>
31 #include <QDomElement>
34 #include <QMessageBox>
68 addGroupFromLayerTree( nodeGroup, groupItem );
73 if ( nodeLayer->
layer() )
91 QString infoKey = infoIt->first;
98 currentGroupItem =
addGroup( infoKey, i );
102 for ( ; currentItem && ( groupLayerIt != layerList.
constEnd() ); ++groupLayerIt )
111 if ( layerList.
contains( layerID ) )
140 addLayer( currentLayer, scaleDenominator, rule );
147 text =
tr(
"Group" );
155 if ( position == -1 )
161 parentItem->
insertRow( position, itemsList );
171 if ( !layerItem || !lItem || !vlayer )
193 for ( ; symbolIt != lst.
constEnd(); ++symbolIt )
195 if ( scaleDenominator == -1 && rule.
isEmpty() )
206 currentSymbolItem->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
207 if ( symbolIt->second )
213 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
215 layerItem->
setChild( row, 0, currentSymbolItem );
218 updateSymbolV2ItemText( currentSymbolItem );
227 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
228 layerItem->
setChild( row, 0, currentSymbolItem );
229 currentSymbolItem->
setText( symbolIt->first );
236 if ( scaleDenominator == -1 && rule.
isEmpty() )
239 for (
int i = layerItem->
rowCount() - 1; i >= row; --i )
250 if ( !layerItem || !rlayer )
272 if ( !legendGraphic.
isNull() )
282 currentSymbolItem->
setText(
tr(
"No Legend Available" ) );
295 for ( ; itemIt != rasterItemList.
constEnd(); ++itemIt )
309 itemPixmap.fill( itemIt->second );
314 QColor itemColor = itemIt->second;
319 if ( rasterRenderer )
321 opacity = rasterRenderer->
opacity() * 255.0;
325 currentSymbolItem->
setColor( itemColor );
327 int currentRowCount = layerItem->
rowCount();
328 layerItem->
setChild( currentRowCount, 0, currentSymbolItem );
333 for (
int i = layerItem->
rowCount() - 1; i >= row; --i )
342 void QgsLegendModel::updateSymbolV2ItemText(
QStandardItem* symbolItem )
345 if ( !sv2Item )
return;
348 if ( !lItem )
return;
351 if ( !mapLayer )
return;
354 if ( !vLayer )
return;
357 if ( !renderer )
return;
367 if ( renderer->
type() ==
"singleSymbol" )
379 label = vLayer->
title();
383 label = vLayer->
name();
399 void QgsLegendModel::updateRasterSymbolItemText(
QStandardItem* symbolItem )
402 if ( !rItem )
return;
405 if ( !lItem )
return;
408 if ( !mapLayer )
return;
411 if ( !rLayer )
return;
458 updateLayerItemText( lItem );
465 updateSymbolV2ItemText( sv2Item );
472 updateRasterSymbolItemText( rItem );
489 updateLayerItemText( lItem );
494 addVectorLayerItemsV2( lItem, vLayer );
500 addRasterLayerItems( lItem, rLayer );
506 void QgsLegendModel::updateLayerItemText(
QStandardItem* layerItem )
509 if ( !lItem )
return;
512 if ( !mapLayer )
return;
519 addVectorLayerItemsV2( lItem, vLayer );
531 for (
int i = 0; i < numRootItems ; ++i )
539 if ( layerId == lItem->
layerID() )
567 if ( theMapLayer->
title() !=
"" )
574 layerItem->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
580 switch ( theMapLayer->
type() )
587 addVectorLayerItemsV2( layerItem, vl, scaleDenominator, rule );
592 addRasterLayerItems( layerItem, theMapLayer );
606 void QgsLegendModel::updateLayer()
610 for (
int i = 0, n =
rowCount(); i < n ; ++i )
613 if ( lItem && layerId == lItem->
layerID() )
615 updateLayer( lItem );
624 if ( composerLegendElem.
isNull() )
635 for (
int i = 0; i < nTopLevelItems; ++i )
639 if ( currentLegendItem )
641 currentLegendItem->
writeXML( legendModelElem, doc );
653 if ( legendModelElem.
isNull() )
666 int nTopLevelItems = topLevelItemList.
size();
667 for (
int i = 0; i < nTopLevelItems; ++i )
669 currentElem = topLevelItemList.
at( i ).
toElement();
670 if ( currentElem.
isNull() )
677 if ( currentElem.
tagName() ==
"LayerItem" )
681 else if ( currentElem.
tagName() ==
"GroupItem" )
702 return Qt::MoveAction;
710 flags |= Qt::ItemIsDropEnabled;
722 flags |= Qt::ItemIsDragEnabled;
723 flags |= Qt::ItemIsDropEnabled;
727 flags |= Qt::ItemIsDragEnabled;
749 flags |= Qt::ItemIsEditable;
765 for (
int i = row + count - 1; i >= row; --i )
776 for (
int i = row + count - 1; i >= row; --i )
792 QModelIndexList::const_iterator indexIt = indexes.constBegin();
793 for ( ; indexIt != indexes.constEnd(); ++indexIt )
801 mItem->
writeXML( xmlRootElement, xmlDoc );
842 if ( dragDataElem.
tagName() !=
"LegendModelDragData" )
848 int nChildNodes = nodeList.
size();
853 for (
int i = 0; i < nChildNodes; ++i )
856 if ( currentElem.
isNull() )
860 currentTagName = currentElem.
tagName();
861 if ( currentTagName ==
"LayerItem" )
865 else if ( currentTagName ==
"GroupItem" )
873 currentItem->
readXML( currentElem );
885 dropIntoItem->
insertRow( index, itemsList );
906 for (
int i = 0, n =
rowCount(); i < n ; ++i )
927 for (
int i = 0, n =
rowCount(); i < n ; ++i )
void removeLayer(const QString &layerId)
Layer tree group node serves as a container for layers and further groups.
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true)=0
Read item content from xml.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
Base class for all map layer types.
QStandardItem * addGroup(QString text=QString::null, int position=-1, QStandardItem *parentItem=0)
Adds a group.
QByteArray data(const QString &mimeType) const
QgsMapLayer::LayerType type() const
Get the type of the layer.
void setIcon(const QIcon &icon)
void setLayerID(const QString &id)
void updateItemText(QStandardItem *item)
Update single item text using item userText and other properties like showFeatureCount.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Implements the drop operation.
QStringList mimeTypes() const override
QDomNode appendChild(const QDomNode &newChild)
QStandardItem * invisibleRootItem() const
QList< QStandardItem * > takeRow(int row)
QString attribute(const QString &name, const QString &defValue) const
QgsMapLayer * layer() const
bool mHasTopLevelWindow
True if this application has toplevel windows (normally true).
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
bool removeRow(int row, const QModelIndex &parent)
void updateItem(QStandardItem *item)
Tries to automatically update a model entry (e.g.
void setLayerSetAndGroups(QgsLayerTreeGroup *rootGroup)
Set layers and groups from a layer tree.
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, QString rule="")
return a list of item text / symbol
virtual QString userText() const
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual QImage getLegendGraphic(double scale=0, bool forceRefresh=false, const QgsRectangle *visibleExtent=0)
Returns the legend rendered as pixmap.
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QDomElement documentElement() const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QDomNodeList childNodes() const
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Implemented to support drag operations.
void setLayerID(const QString &id)
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
QDomElement toElement() const
const QString & name() const
Get the display name of the layer.
QString number(int n, int base)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
void setColumnCount(int columns)
void setDefaultStyle(double scaleDenominator=-1, QString rule="")
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, QString rule="")
QgsRasterRenderer * renderer() const
void setAttribute(const QString &name, const QString &value)
void appendRow(const QList< QStandardItem * > &items)
int toInt(bool *ok, int base) const
void setFlags(QFlags< Qt::ItemFlag > flags)
void setAutoUpdate(bool autoUpdate)
QStandardItem * parent() const
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const =0
This class is a base class for nodes in a layer tree.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Qt::DropActions supportedDropActions() const override
void removeRows(int row, int count)
bool showFeatureCount() const
QString name() const
Get group's name.
void setChild(int row, int column, QStandardItem *item)
bool writeXML(QDomElement &composerLegendElem, QDomDocument &doc) const
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setSymbolV2(QgsSymbolV2 *s)
Set symbol (takes ownership)
QStandardItem * child(int row, int column) const
bool mAutoUpdate
True if the legend is auto updated when layers are added or removed from the map canvas.
Abstract base class for the legend item types.
QStandardItem * item(int row, int column) const
bool readXML(const QDomElement &legendModelElem, const QDomDocument &doc)
bool contains(const T &value) const
bool countSymbolFeatures(bool showProgress=true)
Count features for symbols.
void addLayer(QgsMapLayer *theMapLayer, double scaleDenominator=-1, QString rule="", QStandardItem *parentItem=0)
QList< QStandardItem * > takeRow(int row)
void setText(const QString &text)
virtual long featureCount() const
Number of features in the layer.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
QStandardItem * itemFromIndex(const QModelIndex &index) const
virtual ItemType itemType() const =0
virtual int rowCount(const QModelIndex &parent) const
void setColor(const QColor &c)
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
QgsRasterDataProvider * dataProvider()
Returns the data provider.
void setData(const QString &mimeType, const QByteArray &data)
QString providerType() const
[ data provider interface ] Which provider is being used for this Raster Layer?
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
void updateLayer(QStandardItem *layerItem)
Updates the whole symbology of a layer.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsLegendColorList legendSymbologyItems() const
Returns a list with classification items (Text and color)
Represents a vector layer which manages a vector based data sets.
bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
const QString & title() const
virtual void setUserText(const QString &text)
void appendRow(const QList< QStandardItem * > &items)
Raster renderer pipe that applies colors to a raster.
QMimeData * mimeData(const QModelIndexList &indexes) const override
For the drag operation.
void insertRow(int row, const QList< QStandardItem * > &items)
QByteArray toByteArray(int indent) const
Layer tree node points to a map layer.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QDomNode at(int index) const