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 )
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 )
176 currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
177 if ( symbolIt->second )
183 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
185 layerItem->setChild( row, 0, currentSymbolItem );
194 for (
int i = layerItem->rowCount() - 1; i >= row; --i )
196 layerItem->removeRow( i );
204 if ( !layerItem || !rlayer )
216 QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
218 for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
226 currentSymbolItem->setText( currentSymbolItem->
userText() );
231 QPixmap itemPixmap( 20, 20 );
232 itemPixmap.fill( itemIt->second );
233 currentSymbolItem->setIcon( QIcon( itemPixmap ) );
236 currentSymbolItem->
setColor( itemIt->second );
237 int currentRowCount = layerItem->rowCount();
238 layerItem->setChild( currentRowCount, 0, currentSymbolItem );
243 for (
int i = layerItem->rowCount() - 1; i >= row; --i )
245 layerItem->removeRow( i );
254 if ( !sv2Item )
return;
257 if ( !lItem )
return;
260 if ( !mapLayer )
return;
263 if ( !vLayer )
return;
266 if ( !renderer )
return;
272 QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() );
274 QString label = sv2Item->
userText().isEmpty() ? symbol.first : sv2Item->
userText();
276 if ( renderer->
type() ==
"singleSymbol" )
278 if ( !sv2Item->
userText().isEmpty() )
282 else if ( !lItem->
userText().isEmpty() )
286 else if ( !vLayer->
title().isEmpty() )
288 label = vLayer->
title();
292 label = vLayer->
name();
300 if ( symbolList.size() > 1 || !label.isEmpty() )
302 label += QString(
" [%1]" ).arg( vLayer->
featureCount( symbol.second ) );
305 symbolItem->setText( label );
311 if ( !rItem )
return;
314 if ( !lItem )
return;
317 if ( !mapLayer )
return;
320 if ( !rLayer )
return;
324 QString label = rItem->
userText().isEmpty() ? symbol.first : rItem->
userText();
326 symbolItem->setText( label );
386 cItem->setText( cItem->
userText() );
418 if ( !lItem )
return;
421 if ( !mapLayer )
return;
431 label += QString(
" [%1]" ).arg( vLayer->
featureCount() );
434 lItem->setText( label );
439 int numRootItems = rowCount();
440 for (
int i = 0; i < numRootItems ; ++i )
448 if ( layerId == lItem->
layerID() )
465 if ( theMapLayer->
title() !=
"" )
467 layerItem->setText( theMapLayer->
title() );
472 layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
474 QList<QStandardItem *> itemsList;
476 invisibleRootItem()->appendRow( itemsList );
478 switch ( theMapLayer->
type() )
501 if ( composerLegendElem.isNull() )
506 QDomElement legendModelElem = doc.createElement(
"Model" );
507 legendModelElem.setAttribute(
"autoUpdate",
mAutoUpdate );
508 int nTopLevelItems = invisibleRootItem()->rowCount();
509 QStandardItem* currentItem = 0;
512 for (
int i = 0; i < nTopLevelItems; ++i )
514 currentItem = invisibleRootItem()->child( i, 0 );
516 if ( currentLegendItem )
518 currentLegendItem->
writeXML( legendModelElem, doc );
522 composerLegendElem.appendChild( legendModelElem );
530 if ( legendModelElem.isNull() )
537 QDomNodeList topLevelItemList = legendModelElem.childNodes();
538 QDomElement currentElem;
541 int nTopLevelItems = topLevelItemList.size();
542 for (
int i = 0; i < nTopLevelItems; ++i )
544 currentElem = topLevelItemList.at( i ).toElement();
545 if ( currentElem.isNull() )
551 if ( currentElem.tagName() ==
"LayerItem" )
555 else if ( currentElem.tagName() ==
"GroupItem" )
561 QList<QStandardItem *> itemsList;
563 appendRow( itemsList );
566 setAutoUpdate( legendModelElem.attribute(
"autoUpdate",
"1" ).toInt() );
572 return Qt::MoveAction;
577 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
578 if ( !index.isValid() )
580 flags |= Qt::ItemIsDropEnabled;
584 QStandardItem* item = itemFromIndex( index );
592 flags |= Qt::ItemIsDragEnabled;
593 flags |= Qt::ItemIsDropEnabled;
597 flags |= Qt::ItemIsDragEnabled;
600 if ( index.column() == 1 && item )
603 QStandardItem* firstColumnItem = 0;
604 if ( item->parent() )
606 firstColumnItem = item->parent()->child( index.row(), 0 );
610 firstColumnItem = QgsLegendModel::item( index.row(), 0 );
619 flags |= Qt::ItemIsEditable;
633 if ( parent.isValid() )
635 for (
int i = row + count - 1; i >= row; --i )
637 QStandardItem* item = itemFromIndex( parent );
646 for (
int i = row + count - 1; i >= row; --i )
656 QMimeData*
mimeData =
new QMimeData();
657 QByteArray encodedData;
659 QDomElement xmlRootElement = xmlDoc.createElement(
"LegendModelDragData" );
660 xmlDoc.appendChild( xmlRootElement );
662 QModelIndexList::const_iterator indexIt = indexes.constBegin();
663 for ( ; indexIt != indexes.constEnd(); ++indexIt )
665 QStandardItem* sItem = itemFromIndex( *indexIt );
671 mItem->
writeXML( xmlRootElement, xmlDoc );
675 mimeData->setData(
"text/xml", xmlDoc.toByteArray() );
691 if ( !data->hasFormat(
"text/xml" ) )
696 QStandardItem* dropIntoItem = 0;
697 if ( parent.isValid() )
699 dropIntoItem = itemFromIndex( parent );
703 dropIntoItem = invisibleRootItem();
707 QByteArray encodedData = data->data(
"text/xml" );
709 xmlDoc.setContent( encodedData );
711 QDomElement dragDataElem = xmlDoc.documentElement();
712 if ( dragDataElem.tagName() !=
"LegendModelDragData" )
717 QDomNodeList nodeList = dragDataElem.childNodes();
718 int nChildNodes = nodeList.size();
719 QDomElement currentElem;
720 QString currentTagName;
723 for (
int i = 0; i < nChildNodes; ++i )
725 currentElem = nodeList.at( i ).toElement();
726 if ( currentElem.isNull() )
730 currentTagName = currentElem.tagName();
731 if ( currentTagName ==
"LayerItem" )
735 else if ( currentTagName ==
"GroupItem" )
743 currentItem->
readXML( currentElem );
747 index = dropIntoItem->rowCount();
753 QList<QStandardItem *> itemsList;
755 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 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 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)
virtual QgsLegendSymbolList legendSymbolItems()
return a list of item text / symbol
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
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.
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.
int addVectorLayerItemsV2(QStandardItem *layerItem, QgsVectorLayer *vlayer)
Adds classification items of vector layers using new symbology.
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.
void addLayer(QgsMapLayer *theMapLayer)
void updateLayer(QStandardItem *layerItem)
Updates the whole symbology of a layer.
void setLayerSet(const QStringList &layerIds)
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.