|
QGIS API Documentation
master-3f58142
|
00001 /*************************************************************************** 00002 qgslegendmodel.cpp - description 00003 ------------------ 00004 begin : June 2008 00005 copyright : (C) 2008 by Marco Hugentobler 00006 email : marco dot hugentobler at karto dot baug dot ethz dot ch 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "qgslegendmodel.h" 00019 #include "qgscomposerlegenditem.h" 00020 #include "qgsfield.h" 00021 #include "qgsmaplayer.h" 00022 #include "qgsmaplayerregistry.h" 00023 #include "qgsrasterlayer.h" 00024 #include "qgsrendererv2.h" 00025 #include "qgssymbollayerv2utils.h" 00026 #include "qgsvectordataprovider.h" 00027 #include "qgsvectorlayer.h" 00028 #include <QApplication> 00029 #include <QDomDocument> 00030 #include <QDomElement> 00031 #include <QMimeData> 00032 #include <QSettings> 00033 #include <QMessageBox> 00034 00035 QgsLegendModel::QgsLegendModel(): QStandardItemModel(), mAutoUpdate( true ) 00036 { 00037 setColumnCount( 2 ); 00038 00039 if ( QgsMapLayerRegistry::instance() ) 00040 { 00041 connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); 00042 connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); 00043 } 00044 00045 QWidgetList topLevelWidgets = QApplication::topLevelWidgets(); 00046 mHasTopLevelWindow = ( topLevelWidgets.size() > 0 ); 00047 } 00048 00049 QgsLegendModel::~QgsLegendModel() 00050 { 00051 } 00052 00053 void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo ) 00054 { 00055 setLayerSet( layerIds ); 00056 00057 QStandardItem* currentItem = 0; 00058 QStandardItem* currentGroupItem = 0; 00059 int i = 0; 00060 00061 QList< GroupLayerInfo >::const_iterator infoIt = groupInfo.constBegin(); 00062 for ( ; infoIt != groupInfo.constEnd() && i < invisibleRootItem()->rowCount(); ) 00063 { 00064 currentItem = invisibleRootItem()->child( i, 0 ); 00065 QString infoKey = infoIt->first; 00066 if ( infoKey.isNull() ) //a toplevel layer 00067 { 00068 ++i; 00069 } 00070 else //a group 00071 { 00072 currentGroupItem = addGroup( infoKey, i ); 00073 ++i; 00074 QList<QString> layerList = infoIt->second; 00075 QList<QString>::const_iterator groupLayerIt = layerList.constBegin(); 00076 for ( ; currentItem && ( groupLayerIt != layerList.constEnd() ); ++groupLayerIt ) 00077 { 00078 //check if current item is contained in this group 00079 QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( currentItem ); 00080 if ( !layerItem ) 00081 { 00082 return; //should never happen 00083 } 00084 QString layerID = layerItem->layerID(); 00085 if ( layerList.contains( layerID ) ) 00086 { 00087 takeRow( i ); 00088 currentGroupItem->setChild( currentGroupItem->rowCount(), 0, currentItem ); 00089 } 00090 else 00091 { 00092 ++i; 00093 } 00094 currentItem = invisibleRootItem()->child( i, 0 ); 00095 } 00096 } 00097 ++infoIt; 00098 } 00099 } 00100 00101 void QgsLegendModel::setLayerSet( const QStringList& layerIds ) 00102 { 00103 mLayerIds = layerIds; 00104 00105 //for now clear the model and add the new entries 00106 clear(); 00107 00108 QStringList::const_iterator idIter = mLayerIds.constBegin(); 00109 QgsMapLayer* currentLayer = 0; 00110 00111 for ( ; idIter != mLayerIds.constEnd(); ++idIter ) 00112 { 00113 currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *idIter ); 00114 addLayer( currentLayer ); 00115 } 00116 } 00117 00118 QStandardItem* QgsLegendModel::addGroup( QString text, int position ) 00119 { 00120 if ( text.isNull() ) 00121 text = tr( "Group" ); 00122 00123 QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text ); 00124 groupItem->setUserText( text ); 00125 00126 if ( position == -1 ) 00127 { 00128 position = invisibleRootItem()->rowCount(); 00129 } 00130 QList<QStandardItem *> itemsList; 00131 itemsList << groupItem << new QgsComposerStyleItem( groupItem ); 00132 invisibleRootItem()->insertRow( position, itemsList ); 00133 00134 emit layersChanged(); 00135 return groupItem; 00136 } 00137 00138 int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer ) 00139 { 00140 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem ); 00141 00142 if ( !layerItem || !lItem || !vlayer ) 00143 { 00144 return 1; 00145 } 00146 00147 QgsFeatureRendererV2* renderer = vlayer->rendererV2(); 00148 if ( !renderer ) 00149 { 00150 return 2; 00151 } 00152 00153 if ( lItem->showFeatureCount() ) 00154 { 00155 if ( !vlayer->countSymbolFeatures() ) 00156 { 00157 QgsDebugMsg( "Cannot get feature counts" ); 00158 } 00159 } 00160 00161 QgsLegendSymbolList lst = renderer->legendSymbolItems(); 00162 QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin(); 00163 int row = 0; 00164 for ( ; symbolIt != lst.constEnd(); ++symbolIt ) 00165 { 00166 QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" ); 00167 00168 // Get userText from old item if exists 00169 QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( row, 0 ) ); 00170 if ( oldSymbolItem ) 00171 { 00172 currentSymbolItem->setUserText( oldSymbolItem->userText() ); 00173 } 00174 00175 currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable ); 00176 if ( symbolIt->second ) 00177 { 00178 if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server 00179 { 00180 currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) ); 00181 } 00182 currentSymbolItem->setSymbolV2( symbolIt->second->clone() ); 00183 } 00184 layerItem->setChild( row, 0, currentSymbolItem ); 00185 00186 // updateSymbolV2ItemText needs layer set 00187 updateSymbolV2ItemText( currentSymbolItem ); 00188 00189 row++; 00190 } 00191 00192 // Delete following old items (if current number of items decreased) 00193 for ( int i = layerItem->rowCount() - 1; i >= row; --i ) 00194 { 00195 layerItem->removeRow( i ); 00196 } 00197 00198 return 0; 00199 } 00200 00201 int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer ) 00202 { 00203 if ( !layerItem || !rlayer ) 00204 { 00205 return 1; 00206 } 00207 00208 QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer ); 00209 if ( !rasterLayer ) 00210 { 00211 return 2; 00212 } 00213 00214 QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems(); 00215 QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin(); 00216 int row = 0; 00217 for ( ; itemIt != rasterItemList.constEnd(); ++itemIt ) 00218 { 00219 QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first ); 00220 00221 QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) ); 00222 if ( oldSymbolItem ) 00223 { 00224 currentSymbolItem->setUserText( oldSymbolItem->userText() ); 00225 currentSymbolItem->setText( currentSymbolItem->userText() ); 00226 } 00227 00228 if ( mHasTopLevelWindow ) 00229 { 00230 QPixmap itemPixmap( 20, 20 ); 00231 itemPixmap.fill( itemIt->second ); 00232 currentSymbolItem->setIcon( QIcon( itemPixmap ) ); 00233 } 00234 currentSymbolItem->setLayerID( rasterLayer->id() ); 00235 currentSymbolItem->setColor( itemIt->second ); 00236 int currentRowCount = layerItem->rowCount(); 00237 layerItem->setChild( currentRowCount, 0, currentSymbolItem ); 00238 row++; 00239 } 00240 00241 // Delete following old items (if current number of items decreased) 00242 for ( int i = layerItem->rowCount() - 1; i >= row; --i ) 00243 { 00244 layerItem->removeRow( i ); 00245 } 00246 00247 return 0; 00248 } 00249 00250 void QgsLegendModel::updateSymbolV2ItemText( QStandardItem* symbolItem ) 00251 { 00252 QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( symbolItem ); 00253 if ( !sv2Item ) return; 00254 00255 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( sv2Item->parent() ); 00256 if ( !lItem ) return; 00257 00258 QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() ); 00259 if ( !mapLayer ) return; 00260 00261 QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer ); 00262 if ( !vLayer ) return; 00263 00264 QgsFeatureRendererV2* renderer = vLayer->rendererV2(); 00265 if ( !renderer ) return; 00266 00267 if ( lItem->showFeatureCount() ) vLayer->countSymbolFeatures(); 00268 00269 QgsLegendSymbolList symbolList = renderer->legendSymbolItems(); 00270 00271 QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() ); 00272 00273 QString label = sv2Item->userText().isEmpty() ? symbol.first : sv2Item->userText(); 00274 00275 if ( lItem->showFeatureCount() ) 00276 { 00277 // Add counts to multi symbols layers only or labeled single symbols, 00278 // so that single symbol layers are still drawn on single line 00279 if ( symbolList.size() > 1 || !label.isEmpty() ) 00280 { 00281 label += QString( " [%1]" ).arg( vLayer->featureCount( symbol.second ) ); 00282 } 00283 } 00284 symbolItem->setText( label ); 00285 } 00286 00287 void QgsLegendModel::updateRasterSymbolItemText( QStandardItem* symbolItem ) 00288 { 00289 QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( symbolItem ); 00290 if ( !rItem ) return; 00291 00292 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( rItem->parent() ); 00293 if ( !lItem ) return; 00294 00295 QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() ); 00296 if ( !mapLayer ) return; 00297 00298 QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer ); 00299 if ( !rLayer ) return; 00300 00301 QPair< QString, QColor> symbol = rLayer->legendSymbologyItems().value( symbolItem->row() ); 00302 00303 QString label = rItem->userText().isEmpty() ? symbol.first : rItem->userText(); 00304 00305 symbolItem->setText( label ); 00306 } 00307 00308 void QgsLegendModel::updateItem( QStandardItem* item ) 00309 { 00310 if ( !item ) 00311 { 00312 return; 00313 } 00314 00315 //only layer items are supported for update 00316 QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); 00317 if ( ! cItem ) 00318 { 00319 return; 00320 } 00321 00322 QgsComposerLegendItem::ItemType type = cItem->itemType(); 00323 if ( type == QgsComposerLegendItem::LayerItem ) 00324 { 00325 updateLayer( cItem ); 00326 } 00327 } 00328 00329 void QgsLegendModel::updateItemText( QStandardItem* item ) 00330 { 00331 if ( !item ) 00332 { 00333 return; 00334 } 00335 00336 //only layer items are supported for update 00337 QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); 00338 if ( ! cItem ) 00339 { 00340 return; 00341 } 00342 00343 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( cItem ); 00344 if ( lItem ) 00345 { 00346 updateLayerItemText( lItem ); 00347 return; 00348 } 00349 00350 QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( cItem ); 00351 if ( sv2Item ) 00352 { 00353 updateSymbolV2ItemText( sv2Item ); 00354 return; 00355 } 00356 00357 QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( cItem ); 00358 if ( rItem ) 00359 { 00360 updateRasterSymbolItemText( rItem ); 00361 return; 00362 } 00363 00364 // group 00365 cItem->setText( cItem->userText() ); 00366 } 00367 00368 void QgsLegendModel::updateLayer( QStandardItem* layerItem ) 00369 { 00370 QgsDebugMsg( "Entered." ); 00371 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem ); 00372 if ( lItem ) 00373 { 00374 QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() ); 00375 if ( mapLayer ) 00376 { 00377 QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer ); 00378 00379 updateLayerItemText( lItem ); 00380 00381 if ( vLayer ) 00382 { 00383 addVectorLayerItemsV2( lItem, vLayer ); 00384 } 00385 00386 QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer ); 00387 if ( rLayer ) 00388 { 00389 addRasterLayerItems( lItem, rLayer ); 00390 } 00391 } 00392 } 00393 } 00394 00395 void QgsLegendModel::updateLayerItemText( QStandardItem* layerItem ) 00396 { 00397 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem ); 00398 if ( !lItem ) return; 00399 00400 QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() ); 00401 if ( !mapLayer ) return; 00402 00403 QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer ); 00404 if ( !vLayer ) return; 00405 00406 QString label = lItem->userText().isEmpty() ? mapLayer->name() : lItem->userText(); 00407 00408 if ( vLayer && lItem->showFeatureCount() ) 00409 { 00410 label += QString( " [%1]" ).arg( vLayer->featureCount() ); 00411 } 00412 lItem->setText( label ); 00413 } 00414 00415 void QgsLegendModel::removeLayer( const QString& layerId ) 00416 { 00417 int numRootItems = rowCount(); 00418 for ( int i = 0; i < numRootItems ; ++i ) 00419 { 00420 QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) ); 00421 if ( !lItem ) 00422 { 00423 continue; 00424 } 00425 00426 if ( layerId == lItem->layerID() ) 00427 { 00428 removeRow( i ); //todo: also remove the subitems and their symbols... 00429 emit layersChanged(); 00430 return; 00431 } 00432 } 00433 } 00434 00435 void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer ) 00436 { 00437 if ( !theMapLayer ) 00438 { 00439 return; 00440 } 00441 00442 QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() ); 00443 layerItem->setLayerID( theMapLayer->id() ); 00444 layerItem->setDefaultStyle(); 00445 layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable ); 00446 00447 QList<QStandardItem *> itemsList; 00448 itemsList << layerItem << new QgsComposerStyleItem( layerItem ); 00449 invisibleRootItem()->appendRow( itemsList ); 00450 00451 switch ( theMapLayer->type() ) 00452 { 00453 case QgsMapLayer::VectorLayer: 00454 { 00455 QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( theMapLayer ); 00456 if ( vl ) 00457 { 00458 addVectorLayerItemsV2( layerItem, vl ); 00459 } 00460 break; 00461 } 00462 case QgsMapLayer::RasterLayer: 00463 addRasterLayerItems( layerItem, theMapLayer ); 00464 break; 00465 default: 00466 break; 00467 } 00468 emit layersChanged(); 00469 } 00470 00471 00472 bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const 00473 { 00474 if ( composerLegendElem.isNull() ) 00475 { 00476 return false; 00477 } 00478 00479 QDomElement legendModelElem = doc.createElement( "Model" ); 00480 legendModelElem.setAttribute( "autoUpdate", mAutoUpdate ); 00481 int nTopLevelItems = invisibleRootItem()->rowCount(); 00482 QStandardItem* currentItem = 0; 00483 QgsComposerLegendItem* currentLegendItem = 0; 00484 00485 for ( int i = 0; i < nTopLevelItems; ++i ) 00486 { 00487 currentItem = invisibleRootItem()->child( i, 0 ); 00488 currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentItem ); 00489 if ( currentLegendItem ) 00490 { 00491 currentLegendItem->writeXML( legendModelElem, doc ); 00492 } 00493 } 00494 00495 composerLegendElem.appendChild( legendModelElem ); 00496 return true; 00497 } 00498 00499 bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocument& doc ) 00500 { 00501 Q_UNUSED( doc ); 00502 00503 if ( legendModelElem.isNull() ) 00504 { 00505 return false; 00506 } 00507 00508 clear(); 00509 00510 QDomNodeList topLevelItemList = legendModelElem.childNodes(); 00511 QDomElement currentElem; 00512 QgsComposerLegendItem* currentItem = 0; 00513 00514 int nTopLevelItems = topLevelItemList.size(); 00515 for ( int i = 0; i < nTopLevelItems; ++i ) 00516 { 00517 currentElem = topLevelItemList.at( i ).toElement(); 00518 if ( currentElem.isNull() ) 00519 { 00520 continue; 00521 } 00522 00523 //toplevel items can be groups or layers 00524 if ( currentElem.tagName() == "LayerItem" ) 00525 { 00526 currentItem = new QgsComposerLayerItem(); 00527 } 00528 else if ( currentElem.tagName() == "GroupItem" ) 00529 { 00530 currentItem = new QgsComposerGroupItem(); 00531 } 00532 currentItem->readXML( currentElem, mHasTopLevelWindow ); 00533 00534 QList<QStandardItem *> itemsList; 00535 itemsList << currentItem << new QgsComposerStyleItem( currentItem ); 00536 appendRow( itemsList ); 00537 } 00538 00539 setAutoUpdate( legendModelElem.attribute( "autoUpdate", "1" ).toInt() ); 00540 return true; 00541 } 00542 00543 Qt::DropActions QgsLegendModel::supportedDropActions() const 00544 { 00545 return Qt::MoveAction; 00546 } 00547 00548 Qt::ItemFlags QgsLegendModel::flags( const QModelIndex &index ) const 00549 { 00550 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; 00551 if ( !index.isValid() ) 00552 { 00553 flags |= Qt::ItemIsDropEnabled; 00554 return flags; 00555 } 00556 00557 QStandardItem* item = itemFromIndex( index ); 00558 QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); 00559 00560 if ( cItem ) 00561 { 00562 QgsComposerLegendItem::ItemType type = cItem->itemType(); 00563 if ( type == QgsComposerLegendItem::GroupItem ) 00564 { 00565 flags |= Qt::ItemIsDragEnabled; 00566 flags |= Qt::ItemIsDropEnabled; 00567 } 00568 else if ( type == QgsComposerLegendItem::LayerItem ) 00569 { 00570 flags |= Qt::ItemIsDragEnabled; 00571 } 00572 } 00573 if ( index.column() == 1 && item ) 00574 { 00575 // Style 00576 QStandardItem* firstColumnItem = 0; 00577 if ( item->parent() ) 00578 { 00579 firstColumnItem = item->parent()->child( index.row(), 0 ); 00580 } 00581 else 00582 { 00583 firstColumnItem = QgsLegendModel::item( index.row(), 0 ); 00584 } 00585 cItem = dynamic_cast<QgsComposerLegendItem*>( firstColumnItem ); 00586 00587 if ( cItem ) 00588 { 00589 if ( cItem->itemType() == QgsComposerLegendItem::GroupItem || 00590 cItem->itemType() == QgsComposerLegendItem::LayerItem ) 00591 { 00592 flags |= Qt::ItemIsEditable; 00593 } 00594 } 00595 } 00596 return flags; 00597 } 00598 00599 bool QgsLegendModel::removeRows( int row, int count, const QModelIndex & parent ) 00600 { 00601 if ( count < 1 ) 00602 { 00603 return false; 00604 } 00605 00606 if ( parent.isValid() ) 00607 { 00608 for ( int i = row + count - 1; i >= row; --i ) 00609 { 00610 QStandardItem* item = itemFromIndex( parent ); 00611 if ( item ) 00612 { 00613 item->takeRow( i ); 00614 } 00615 } 00616 } 00617 else 00618 { 00619 for ( int i = row + count - 1; i >= row; --i ) 00620 { 00621 takeRow( i ); 00622 } 00623 } 00624 return true; 00625 } 00626 00627 QMimeData* QgsLegendModel::mimeData( const QModelIndexList &indexes ) const 00628 { 00629 QMimeData* mimeData = new QMimeData(); 00630 QByteArray encodedData; 00631 QDomDocument xmlDoc; 00632 QDomElement xmlRootElement = xmlDoc.createElement( "LegendModelDragData" ); 00633 xmlDoc.appendChild( xmlRootElement ); 00634 00635 QModelIndexList::const_iterator indexIt = indexes.constBegin(); 00636 for ( ; indexIt != indexes.constEnd(); ++indexIt ) 00637 { 00638 QStandardItem* sItem = itemFromIndex( *indexIt ); 00639 if ( sItem ) 00640 { 00641 QgsComposerLegendItem* mItem = dynamic_cast<QgsComposerLegendItem*>( sItem ); 00642 if ( mItem ) 00643 { 00644 mItem->writeXML( xmlRootElement, xmlDoc ); 00645 } 00646 } 00647 } 00648 mimeData->setData( "text/xml", xmlDoc.toByteArray() ); 00649 return mimeData; 00650 } 00651 00652 QStringList QgsLegendModel::mimeTypes() const 00653 { 00654 QStringList types; 00655 types << "text/xml"; 00656 return types; 00657 } 00658 00659 bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) 00660 { 00661 Q_UNUSED( action ); 00662 Q_UNUSED( column ); 00663 00664 if ( !data->hasFormat( "text/xml" ) ) 00665 { 00666 return false; 00667 } 00668 00669 QStandardItem* dropIntoItem = 0; 00670 if ( parent.isValid() ) 00671 { 00672 dropIntoItem = itemFromIndex( parent ); 00673 } 00674 else 00675 { 00676 dropIntoItem = invisibleRootItem(); 00677 } 00678 00679 //get XML doc 00680 QByteArray encodedData = data->data( "text/xml" ); 00681 QDomDocument xmlDoc; 00682 xmlDoc.setContent( encodedData ); 00683 00684 QDomElement dragDataElem = xmlDoc.documentElement(); 00685 if ( dragDataElem.tagName() != "LegendModelDragData" ) 00686 { 00687 return false; 00688 } 00689 00690 QDomNodeList nodeList = dragDataElem.childNodes(); 00691 int nChildNodes = nodeList.size(); 00692 QDomElement currentElem; 00693 QString currentTagName; 00694 QgsComposerLegendItem* currentItem = 0; 00695 00696 for ( int i = 0; i < nChildNodes; ++i ) 00697 { 00698 currentElem = nodeList.at( i ).toElement(); 00699 if ( currentElem.isNull() ) 00700 { 00701 continue; 00702 } 00703 currentTagName = currentElem.tagName(); 00704 if ( currentTagName == "LayerItem" ) 00705 { 00706 currentItem = new QgsComposerLayerItem(); 00707 } 00708 else if ( currentTagName == "GroupItem" ) 00709 { 00710 currentItem = new QgsComposerGroupItem(); 00711 } 00712 else 00713 { 00714 continue; 00715 } 00716 currentItem->readXML( currentElem ); 00717 int index; 00718 if ( row < 0 ) 00719 { 00720 index = dropIntoItem->rowCount(); 00721 } 00722 else 00723 { 00724 index = row + i; 00725 } 00726 QList<QStandardItem *> itemsList; 00727 itemsList << currentItem << new QgsComposerStyleItem( currentItem ); 00728 dropIntoItem->insertRow( index, itemsList ); 00729 } 00730 emit layersChanged(); 00731 return true; 00732 } 00733 00734 void QgsLegendModel::setAutoUpdate( bool autoUpdate ) 00735 { 00736 if ( mAutoUpdate == autoUpdate ) //prevent multiple signal/slot connections 00737 { 00738 return; 00739 } 00740 00741 mAutoUpdate = autoUpdate; 00742 if ( autoUpdate ) 00743 { 00744 if ( QgsMapLayerRegistry::instance() ) 00745 { 00746 connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); 00747 connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); 00748 } 00749 } 00750 else 00751 { 00752 if ( QgsMapLayerRegistry::instance() ) 00753 { 00754 disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); 00755 disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); 00756 } 00757 } 00758 }