28 #include <QApplication>
29 #include <QDomDocument>
30 #include <QDomElement>
33 #include <QMessageBox>
45 QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
57 QStandardItem* currentItem = 0;
58 QStandardItem* currentGroupItem = 0;
61 QList< GroupLayerInfo >::const_iterator infoIt = groupInfo.constBegin();
62 for ( ; infoIt != groupInfo.constEnd() && i < invisibleRootItem()->rowCount(); )
64 currentItem = invisibleRootItem()->child( i, 0 );
65 QString infoKey = infoIt->first;
66 if ( infoKey.isNull() )
72 currentGroupItem =
addGroup( infoKey, i );
74 QList<QString> layerList = infoIt->second;
75 QList<QString>::const_iterator groupLayerIt = layerList.constBegin();
76 for ( ; currentItem && ( groupLayerIt != layerList.constEnd() ); ++groupLayerIt )
84 QString layerID = layerItem->
layerID();
85 if ( layerList.contains( layerID ) )
88 currentGroupItem->setChild( currentGroupItem->rowCount(), 0, currentItem );
94 currentItem = invisibleRootItem()->child( i, 0 );
108 QStringList::const_iterator idIter =
mLayerIds.constBegin();
111 for ( ; idIter !=
mLayerIds.constEnd(); ++idIter )
114 addLayer( currentLayer, scaleDenominator, rule );
121 text =
tr(
"Group" );
126 if ( position == -1 )
128 position = invisibleRootItem()->rowCount();
130 QList<QStandardItem *> itemsList;
132 invisibleRootItem()->insertRow( position, itemsList );
142 if ( !layerItem || !lItem || !vlayer )
162 QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
164 for ( ; symbolIt != lst.constEnd(); ++symbolIt )
166 if ( scaleDenominator == -1 && rule.isEmpty() )
177 currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
178 if ( symbolIt->second )
184 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
186 layerItem->setChild( row, 0, currentSymbolItem );
198 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
199 layerItem->setChild( row, 0, currentSymbolItem );
200 currentSymbolItem->setText( symbolIt->first );
207 if ( scaleDenominator == -1 && rule.isEmpty() )
210 for (
int i = layerItem->rowCount() - 1; i >= row; --i )
212 layerItem->removeRow( i );
221 if ( !layerItem || !rlayer )
243 if ( !legendGraphic.isNull() )
245 QgsDebugMsg( QString(
"downloaded legend with dimension width:" ) + QString::number( legendGraphic.width() ) + QString(
" and Height:" ) + QString::number( legendGraphic.height() ) );
248 currentSymbolItem->setIcon( QIcon( QPixmap::fromImage( legendGraphic ) ) );
253 currentSymbolItem->setText(
tr(
"No Legend Available" ) );
257 currentSymbolItem->
setColor( QColor() );
258 layerItem->removeRows( 0, layerItem->rowCount() );
259 layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
264 QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
266 for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
274 currentSymbolItem->setText( currentSymbolItem->
userText() );
279 QPixmap itemPixmap( 20, 20 );
280 itemPixmap.fill( itemIt->second );
281 currentSymbolItem->setIcon( QIcon( itemPixmap ) );
285 QColor itemColor = itemIt->second;
290 if ( rasterRenderer )
292 opacity = rasterRenderer->
opacity() * 255.0;
294 itemColor.setAlpha( opacity );
296 currentSymbolItem->
setColor( itemColor );
298 int currentRowCount = layerItem->rowCount();
299 layerItem->setChild( currentRowCount, 0, currentSymbolItem );
304 for (
int i = layerItem->rowCount() - 1; i >= row; --i )
306 layerItem->removeRow( i );
316 if ( !sv2Item )
return;
319 if ( !lItem )
return;
322 if ( !mapLayer )
return;
325 if ( !vLayer )
return;
328 if ( !renderer )
return;
334 QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() );
336 QString label = sv2Item->
userText().isEmpty() ? symbol.first : sv2Item->
userText();
338 if ( renderer->
type() ==
"singleSymbol" )
340 if ( !sv2Item->
userText().isEmpty() )
344 else if ( !lItem->
userText().isEmpty() )
348 else if ( !vLayer->
title().isEmpty() )
350 label = vLayer->
title();
354 label = vLayer->
name();
362 if ( symbolList.size() > 1 || !label.isEmpty() )
364 label += QString(
" [%1]" ).arg( vLayer->
featureCount( symbol.second ) );
367 symbolItem->setText( label );
373 if ( !rItem )
return;
376 if ( !lItem )
return;
379 if ( !mapLayer )
return;
382 if ( !rLayer )
return;
386 QString label = rItem->
userText().isEmpty() ? symbol.first : rItem->
userText();
388 symbolItem->setText( label );
448 cItem->setText( cItem->
userText() );
480 if ( !lItem )
return;
483 if ( !mapLayer )
return;
493 label += QString(
" [%1]" ).arg( vLayer->
featureCount() );
496 lItem->setText( label );
501 int numRootItems = rowCount();
502 for (
int i = 0; i < numRootItems ; ++i )
510 if ( layerId == lItem->
layerID() )
517 disconnect( layer, SIGNAL( rendererChanged() ),
this, SLOT(
updateLayer() ) );
535 if ( theMapLayer->
title() !=
"" )
537 layerItem->setText( theMapLayer->
title() );
542 layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
544 QList<QStandardItem *> itemsList;
546 invisibleRootItem()->appendRow( itemsList );
548 switch ( theMapLayer->
type() )
568 connect( theMapLayer, SIGNAL( rendererChanged() ),
this, SLOT(
updateLayer() ) );
576 QString layerId = qobject_cast<
QgsMapLayer*>( QObject::sender() )->
id();
578 for (
int i = 0, n = rowCount(); i < n ; ++i )
581 if ( lItem && layerId == lItem->
layerID() )
592 if ( composerLegendElem.isNull() )
597 QDomElement legendModelElem = doc.createElement(
"Model" );
598 legendModelElem.setAttribute(
"autoUpdate",
mAutoUpdate );
599 int nTopLevelItems = invisibleRootItem()->rowCount();
600 QStandardItem* currentItem = 0;
603 for (
int i = 0; i < nTopLevelItems; ++i )
605 currentItem = invisibleRootItem()->child( i, 0 );
607 if ( currentLegendItem )
609 currentLegendItem->
writeXML( legendModelElem, doc );
613 composerLegendElem.appendChild( legendModelElem );
621 if ( legendModelElem.isNull() )
631 QDomNodeList topLevelItemList = legendModelElem.childNodes();
632 QDomElement currentElem;
635 int nTopLevelItems = topLevelItemList.size();
636 for (
int i = 0; i < nTopLevelItems; ++i )
638 currentElem = topLevelItemList.at( i ).toElement();
639 if ( currentElem.isNull() )
645 if ( currentElem.tagName() ==
"LayerItem" )
649 else if ( currentElem.tagName() ==
"GroupItem" )
655 QList<QStandardItem *> itemsList;
657 appendRow( itemsList );
660 setAutoUpdate( legendModelElem.attribute(
"autoUpdate",
"1" ).toInt() );
666 return Qt::MoveAction;
671 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
672 if ( !index.isValid() )
674 flags |= Qt::ItemIsDropEnabled;
678 QStandardItem* item = itemFromIndex( index );
686 flags |= Qt::ItemIsDragEnabled;
687 flags |= Qt::ItemIsDropEnabled;
691 flags |= Qt::ItemIsDragEnabled;
694 if ( index.column() == 1 && item )
697 QStandardItem* firstColumnItem = 0;
698 if ( item->parent() )
700 firstColumnItem = item->parent()->child( index.row(), 0 );
704 firstColumnItem = QgsLegendModel::item( index.row(), 0 );
713 flags |= Qt::ItemIsEditable;
727 if ( parent.isValid() )
729 for (
int i = row + count - 1; i >= row; --i )
731 QStandardItem* item = itemFromIndex( parent );
740 for (
int i = row + count - 1; i >= row; --i )
750 QMimeData*
mimeData =
new QMimeData();
751 QByteArray encodedData;
753 QDomElement xmlRootElement = xmlDoc.createElement(
"LegendModelDragData" );
754 xmlDoc.appendChild( xmlRootElement );
756 QModelIndexList::const_iterator indexIt = indexes.constBegin();
757 for ( ; indexIt != indexes.constEnd(); ++indexIt )
759 QStandardItem* sItem = itemFromIndex( *indexIt );
765 mItem->
writeXML( xmlRootElement, xmlDoc );
769 mimeData->setData(
"text/xml", xmlDoc.toByteArray() );
785 if ( !data->hasFormat(
"text/xml" ) )
790 QStandardItem* dropIntoItem = 0;
791 if ( parent.isValid() )
793 dropIntoItem = itemFromIndex( parent );
797 dropIntoItem = invisibleRootItem();
801 QByteArray encodedData = data->data(
"text/xml" );
803 xmlDoc.setContent( encodedData );
805 QDomElement dragDataElem = xmlDoc.documentElement();
806 if ( dragDataElem.tagName() !=
"LegendModelDragData" )
811 QDomNodeList nodeList = dragDataElem.childNodes();
812 int nChildNodes = nodeList.size();
813 QDomElement currentElem;
814 QString currentTagName;
817 for (
int i = 0; i < nChildNodes; ++i )
819 currentElem = nodeList.at( i ).toElement();
820 if ( currentElem.isNull() )
824 currentTagName = currentElem.tagName();
825 if ( currentTagName ==
"LayerItem" )
829 else if ( currentTagName ==
"GroupItem" )
837 currentItem->
readXML( currentElem );
841 index = dropIntoItem->rowCount();
847 QList<QStandardItem *> itemsList;
849 dropIntoItem->insertRow( index, itemsList );
870 for (
int i = 0, n = rowCount(); i < n ; ++i )
878 connect( layer, SIGNAL( rendererChanged() ),
this, SLOT(
updateLayer() ) );
891 for (
int i = 0, n = rowCount(); i < n ; ++i )
899 disconnect( layer, SIGNAL( rendererChanged() ),
this, SLOT(
updateLayer() ) );
void removeLayer(const QString &layerId)
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true)=0
Read item content from xml.
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
Base class for all map layer types.
QgsMapLayer::LayerType type() const
Get the type of the layer.
void addLayer(QgsMapLayer *theMapLayer, double scaleDenominator=-1, QString rule="")
void setLayerID(const QString &id)
void updateItemText(QStandardItem *item)
Update single item text using item userText and other properties like showFeatureCount.
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.
void updateRasterSymbolItemText(QStandardItem *symbolItem)
Qt::DropActions supportedDropActions() const
void updateItem(QStandardItem *item)
Tries to automatically update a model entry (e.g.
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, QString rule="")
return a list of item text / symbol
virtual QString userText() const
int addRasterLayerItems(QStandardItem *layerItem, QgsMapLayer *rlayer)
Adds item of raster layer.
void setLayerID(const QString &id)
const QString & name() const
Get the display name of the layer.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Implements the drop operation.
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
void updateLayerItemText(QStandardItem *layerItem)
void setDefaultStyle(double scaleDenominator=-1, QString rule="")
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, QString rule="")
QgsRasterRenderer * renderer() const
void setAutoUpdate(bool autoUpdate)
void updateSymbolV2ItemText(QStandardItem *symbolItem)
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const =0
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
bool showFeatureCount() const
int addVectorLayerItemsV2(QStandardItem *layerItem, QgsVectorLayer *vlayer, double scaleDenominator=-1, QString rule="")
Adds classification items of vector layers using new symbology.
bool writeXML(QDomElement &composerLegendElem, QDomDocument &doc) const
void setSymbolV2(QgsSymbolV2 *s)
Set symbol (takes ownership)
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.
bool readXML(const QDomElement &legendModelElem, const QDomDocument &doc)
bool countSymbolFeatures(bool showProgress=true)
Count features for symbols.
virtual QImage getLegendGraphic(double scale=0, bool forceRefresh=false)
Returns the legend rendered as pixmap useful for that layer that need to get legend layer remotly as ...
QStandardItem * addGroup(QString text=QString::null, int position=-1)
Adds a group.
virtual long featureCount() const
Number of features in the layer.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setLayerSetAndGroups(const QStringList &layerIds, const QList< GroupLayerInfo > &groupInfo)
Sets layer set and groups.
QMimeData * mimeData(const QModelIndexList &indexes) const
For the drag operation.
virtual ItemType itemType() const =0
QStringList mimeTypes() 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.
QString providerType() const
[ data provider interface ] Which provider is being used for this Raster Layer?
QgsLegendColorList legendSymbologyItems() const
Returns a list with classification items (Text and color)
Represents a vector layer which manages a vector based data sets.
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
const QString & title() const
virtual void setUserText(const QString &text)
Raster renderer pipe that applies colors to a raster.
Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Implemented to support drag operations.