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 );
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 ) );
284 currentSymbolItem->
setColor( itemIt->second );
285 int currentRowCount = layerItem->rowCount();
286 layerItem->setChild( currentRowCount, 0, currentSymbolItem );
291 for (
int i = layerItem->rowCount() - 1; i >= row; --i )
293 layerItem->removeRow( i );
303 if ( !sv2Item )
return;
306 if ( !lItem )
return;
309 if ( !mapLayer )
return;
312 if ( !vLayer )
return;
315 if ( !renderer )
return;
321 QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() );
323 QString label = sv2Item->
userText().isEmpty() ? symbol.first : sv2Item->
userText();
325 if ( renderer->
type() ==
"singleSymbol" )
327 if ( !sv2Item->
userText().isEmpty() )
331 else if ( !lItem->
userText().isEmpty() )
335 else if ( !vLayer->
title().isEmpty() )
337 label = vLayer->
title();
341 label = vLayer->
name();
349 if ( symbolList.size() > 1 || !label.isEmpty() )
351 label += QString(
" [%1]" ).arg( vLayer->
featureCount( symbol.second ) );
354 symbolItem->setText( label );
360 if ( !rItem )
return;
363 if ( !lItem )
return;
366 if ( !mapLayer )
return;
369 if ( !rLayer )
return;
373 QString label = rItem->
userText().isEmpty() ? symbol.first : rItem->
userText();
375 symbolItem->setText( label );
435 cItem->setText( cItem->
userText() );
467 if ( !lItem )
return;
470 if ( !mapLayer )
return;
480 label += QString(
" [%1]" ).arg( vLayer->
featureCount() );
483 lItem->setText( label );
488 int numRootItems = rowCount();
489 for (
int i = 0; i < numRootItems ; ++i )
497 if ( layerId == lItem->
layerID() )
514 if ( theMapLayer->
title() !=
"" )
516 layerItem->setText( theMapLayer->
title() );
521 layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
523 QList<QStandardItem *> itemsList;
525 invisibleRootItem()->appendRow( itemsList );
527 switch ( theMapLayer->
type() )
550 if ( composerLegendElem.isNull() )
555 QDomElement legendModelElem = doc.createElement(
"Model" );
556 legendModelElem.setAttribute(
"autoUpdate",
mAutoUpdate );
557 int nTopLevelItems = invisibleRootItem()->rowCount();
558 QStandardItem* currentItem = 0;
561 for (
int i = 0; i < nTopLevelItems; ++i )
563 currentItem = invisibleRootItem()->child( i, 0 );
565 if ( currentLegendItem )
567 currentLegendItem->
writeXML( legendModelElem, doc );
571 composerLegendElem.appendChild( legendModelElem );
579 if ( legendModelElem.isNull() )
586 QDomNodeList topLevelItemList = legendModelElem.childNodes();
587 QDomElement currentElem;
590 int nTopLevelItems = topLevelItemList.size();
591 for (
int i = 0; i < nTopLevelItems; ++i )
593 currentElem = topLevelItemList.at( i ).toElement();
594 if ( currentElem.isNull() )
600 if ( currentElem.tagName() ==
"LayerItem" )
604 else if ( currentElem.tagName() ==
"GroupItem" )
610 QList<QStandardItem *> itemsList;
612 appendRow( itemsList );
615 setAutoUpdate( legendModelElem.attribute(
"autoUpdate",
"1" ).toInt() );
621 return Qt::MoveAction;
626 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
627 if ( !index.isValid() )
629 flags |= Qt::ItemIsDropEnabled;
633 QStandardItem* item = itemFromIndex( index );
641 flags |= Qt::ItemIsDragEnabled;
642 flags |= Qt::ItemIsDropEnabled;
646 flags |= Qt::ItemIsDragEnabled;
649 if ( index.column() == 1 && item )
652 QStandardItem* firstColumnItem = 0;
653 if ( item->parent() )
655 firstColumnItem = item->parent()->child( index.row(), 0 );
659 firstColumnItem = QgsLegendModel::item( index.row(), 0 );
668 flags |= Qt::ItemIsEditable;
682 if ( parent.isValid() )
684 for (
int i = row + count - 1; i >= row; --i )
686 QStandardItem* item = itemFromIndex( parent );
695 for (
int i = row + count - 1; i >= row; --i )
705 QMimeData*
mimeData =
new QMimeData();
706 QByteArray encodedData;
708 QDomElement xmlRootElement = xmlDoc.createElement(
"LegendModelDragData" );
709 xmlDoc.appendChild( xmlRootElement );
711 QModelIndexList::const_iterator indexIt = indexes.constBegin();
712 for ( ; indexIt != indexes.constEnd(); ++indexIt )
714 QStandardItem* sItem = itemFromIndex( *indexIt );
720 mItem->
writeXML( xmlRootElement, xmlDoc );
724 mimeData->setData(
"text/xml", xmlDoc.toByteArray() );
740 if ( !data->hasFormat(
"text/xml" ) )
745 QStandardItem* dropIntoItem = 0;
746 if ( parent.isValid() )
748 dropIntoItem = itemFromIndex( parent );
752 dropIntoItem = invisibleRootItem();
756 QByteArray encodedData = data->data(
"text/xml" );
758 xmlDoc.setContent( encodedData );
760 QDomElement dragDataElem = xmlDoc.documentElement();
761 if ( dragDataElem.tagName() !=
"LegendModelDragData" )
766 QDomNodeList nodeList = dragDataElem.childNodes();
767 int nChildNodes = nodeList.size();
768 QDomElement currentElem;
769 QString currentTagName;
772 for (
int i = 0; i < nChildNodes; ++i )
774 currentElem = nodeList.at( i ).toElement();
775 if ( currentElem.isNull() )
779 currentTagName = currentElem.tagName();
780 if ( currentTagName ==
"LayerItem" )
784 else if ( currentTagName ==
"GroupItem" )
792 currentItem->
readXML( currentElem );
796 index = dropIntoItem->rowCount();
802 QList<QStandardItem *> itemsList;
804 dropIntoItem->insertRow( index, itemsList );
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="")
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?
void updateLayer(QStandardItem *layerItem)
Updates the whole symbology of a layer.
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)
QList< QPair< QString, QColor > > legendSymbologyItems() const
Returns a list with classification items (Text and color)
Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Implemented to support drag operations.