30 #include <QApplication>
31 #include <QDomDocument>
32 #include <QDomElement>
35 #include <QMessageBox>
46 QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
58 QStandardItem* currentItem = 0;
59 QStandardItem* currentGroupItem = 0;
62 QList< GroupLayerInfo >::const_iterator infoIt = groupInfo.constBegin();
63 for ( ; infoIt != groupInfo.constEnd() && i < invisibleRootItem()->rowCount(); )
65 currentItem = invisibleRootItem()->child( i, 0 );
66 QString infoKey = infoIt->first;
67 if ( infoKey.isNull() )
73 currentGroupItem =
addGroup( infoKey, i );
75 QList<QString> layerList = infoIt->second;
76 QList<QString>::const_iterator groupLayerIt = layerList.constBegin();
77 for ( ; currentItem && ( groupLayerIt != layerList.constEnd() ); ++groupLayerIt )
86 QString layerID = layerItem->
layerID();
87 if ( layerList.contains( layerID ) )
90 currentGroupItem->setChild( currentGroupItem->rowCount(), 0, currentItem );
96 currentItem = invisibleRootItem()->child( i, 0 );
110 QStringList::const_iterator idIter =
mLayerIds.constBegin();
113 for ( ; idIter !=
mLayerIds.constEnd(); ++idIter )
123 if ( position == -1 )
125 invisibleRootItem()->insertRow( invisibleRootItem()->rowCount(), groupItem );
129 invisibleRootItem()->insertRow( position, groupItem );
136 if ( !layerItem || !vlayer )
148 QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
149 for ( ; symbolIt != lst.constEnd(); ++symbolIt )
152 currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
153 if ( symbolIt->second )
159 currentSymbolItem->
setSymbolV2( symbolIt->second->clone() );
161 layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
169 if ( !layerItem || !vlayer )
177 if ( !vectorRenderer )
184 if ( settings.value(
"/qgis/showLegendClassifiers",
false ).toBool() )
188 QgsAttributeList::const_iterator att_it = attributes.constBegin();
189 for ( ; att_it != attributes.constEnd(); ++att_it )
191 QgsFieldMap::const_iterator fieldIt = layerFields.find( *att_it );
192 if ( fieldIt != layerFields.constEnd() )
195 QStandardItem* attributeItem =
new QStandardItem( attributeName );
197 attributeItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
198 layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
203 const QList<QgsSymbol*> vectorSymbols = vectorRenderer->
symbols();
204 QList<QgsSymbol*>::const_iterator symbolIt = vectorSymbols.constBegin();
206 for ( ; symbolIt != vectorSymbols.constEnd(); ++symbolIt )
208 if ( !( *symbolIt ) )
213 QStandardItem* currentSymbolItem =
itemFromSymbol( *symbolIt, opacity, vlayer->
id() );
214 if ( !currentSymbolItem )
219 layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
228 if ( !layerItem || !rlayer )
243 currentSymbolItem->setIcon( QIcon( rasterLayer->
legendAsPixmap(
true ) ) );
246 int currentRowCount = layerItem->rowCount();
247 layerItem->setChild( currentRowCount, 0, currentSymbolItem );
282 int currentRowCount = lItem->rowCount();
283 for (
int i = currentRowCount - 1; i >= 0; --i )
285 lItem->removeRow( i );
289 layerItem->setText( mapLayer->
name() );
315 int numRootItems = rowCount();
316 for (
int i = 0; i < numRootItems ; ++i )
324 if ( layerId == lItem->
layerID() )
342 layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
344 invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), layerItem );
346 switch ( theMapLayer->
type() )
387 if ( !label.isEmpty() )
392 else if ( lowerValue == upperValue || upperValue.isEmpty() )
394 itemText = lowerValue;
398 itemText = lowerValue +
" - " + upperValue;
418 if ( opacity != 255 )
422 for (
int i = 0; i < symbolImage.height(); ++i )
424 QRgb* scanLineBuffer = ( QRgb* ) symbolImage.scanLine( i );
425 for (
int j = 0; j < symbolImage.width(); ++j )
427 oldColor = symbolImage.pixel( j, i );
428 scanLineBuffer[j] = qRgba( qRed( oldColor ), qGreen( oldColor ), qBlue( oldColor ), opacity );
436 currentSymbolItem->setIcon( QIcon( QPixmap::fromImage( symbolImage ) ) );
439 if ( !currentSymbolItem )
446 currentSymbolItem->
setSymbol( symbolCopy );
447 currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
449 return currentSymbolItem;
454 if ( composerLegendElem.isNull() )
459 QDomElement legendModelElem = doc.createElement(
"Model" );
460 legendModelElem.setAttribute(
"autoUpdate",
mAutoUpdate );
461 int nTopLevelItems = invisibleRootItem()->rowCount();
462 QStandardItem* currentItem = 0;
465 for (
int i = 0; i < nTopLevelItems; ++i )
467 currentItem = invisibleRootItem()->child( i, 0 );
469 if ( currentLegendItem )
471 currentLegendItem->
writeXML( legendModelElem, doc );
475 composerLegendElem.appendChild( legendModelElem );
481 if ( legendModelElem.isNull() )
488 QDomNodeList topLevelItemList = legendModelElem.childNodes();
489 QDomElement currentElem;
492 int nTopLevelItems = topLevelItemList.size();
493 for (
int i = 0; i < nTopLevelItems; ++i )
495 currentElem = topLevelItemList.at( i ).toElement();
496 if ( currentElem.isNull() )
502 if ( currentElem.tagName() ==
"LayerItem" )
506 else if ( currentElem.tagName() ==
"GroupItem" )
511 appendRow( currentItem );
514 setAutoUpdate( legendModelElem.attribute(
"autoUpdate",
"1" ).toInt() );
520 return Qt::MoveAction;
525 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
526 if ( !index.isValid() )
528 flags |= Qt::ItemIsDropEnabled;
532 QStandardItem* item = itemFromIndex( index );
540 flags |= Qt::ItemIsDragEnabled;
541 flags |= Qt::ItemIsDropEnabled;
545 flags |= Qt::ItemIsDragEnabled;
558 if ( parent.isValid() )
560 for (
int i = row + count - 1; i >= row; --i )
562 QStandardItem* item = itemFromIndex( parent );
571 for (
int i = row + count - 1; i >= row; --i )
581 QMimeData*
mimeData =
new QMimeData();
582 QByteArray encodedData;
584 QDomElement xmlRootElement = xmlDoc.createElement(
"LegendModelDragData" );
585 xmlDoc.appendChild( xmlRootElement );
587 QModelIndexList::const_iterator indexIt = indexes.constBegin();
588 for ( ; indexIt != indexes.constEnd(); ++indexIt )
590 QStandardItem* sItem = itemFromIndex( *indexIt );
596 mItem->
writeXML( xmlRootElement, xmlDoc );
600 mimeData->setData(
"text/xml", xmlDoc.toByteArray() );
613 if ( !data->hasFormat(
"text/xml" ) )
618 QStandardItem* dropIntoItem = 0;
619 if ( parent.isValid() )
621 dropIntoItem = itemFromIndex( parent );
625 dropIntoItem = invisibleRootItem();
629 QByteArray encodedData = data->data(
"text/xml" );
631 xmlDoc.setContent( encodedData );
633 QDomElement dragDataElem = xmlDoc.documentElement();
634 if ( dragDataElem.tagName() !=
"LegendModelDragData" )
639 QDomNodeList nodeList = dragDataElem.childNodes();
640 int nChildNodes = nodeList.size();
641 QDomElement currentElem;
642 QString currentTagName;
645 for (
int i = 0; i < nChildNodes; ++i )
647 currentElem = nodeList.at( i ).toElement();
648 if ( currentElem.isNull() )
652 currentTagName = currentElem.tagName();
653 if ( currentTagName ==
"LayerItem" )
657 else if ( currentTagName ==
"GroupItem" )
665 currentItem->
readXML( currentElem );
668 dropIntoItem->insertRow( dropIntoItem->rowCount(), currentItem );
672 dropIntoItem->insertRow( row + i, currentItem );