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 );