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