Qt::ItemFlags QgsLegendModel::flags( const QModelIndex &index ) const { Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( !index.isValid() ) { flags |= Qt::ItemIsDropEnabled; return flags; } QStandardItem* item = itemFromIndex( index ); QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); if ( cItem ) { QgsComposerLegendItem::ItemType type = cItem->itemType(); if ( type == QgsComposerLegendItem::GroupItem ) { flags |= Qt::ItemIsDragEnabled; flags |= Qt::ItemIsDropEnabled; } else if ( type == QgsComposerLegendItem::LayerItem ) { flags |= Qt::ItemIsDragEnabled; } } return flags; }
bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const { if ( composerLegendElem.isNull() ) { return false; } QDomElement legendModelElem = doc.createElement( "Model" ); legendModelElem.setAttribute( "autoUpdate", mAutoUpdate ); int nTopLevelItems = invisibleRootItem()->rowCount(); QStandardItem* currentItem = 0; QgsComposerLegendItem* currentLegendItem = 0; for ( int i = 0; i < nTopLevelItems; ++i ) { currentItem = invisibleRootItem()->child( i, 0 ); currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentItem ); if ( currentLegendItem ) { currentLegendItem->writeXML( legendModelElem, doc ); } } composerLegendElem.appendChild( legendModelElem ); return true; }
void QgsComposerGroupItem::readXML( const QDomElement& itemElem, bool xServerAvailable ) { if ( itemElem.isNull() ) { return; } // text is always user text but for backward compatibility we read also text QString userText = itemElem.attribute( "userText", "" ); if ( userText.isEmpty() ) { userText = itemElem.attribute( "text", "" ); } setText( userText ); setUserText( userText ); setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "group" ) ) ); //now call readXML for all the child items QDomNodeList childList = itemElem.childNodes(); QDomNode currentNode; QDomElement currentElem; QgsComposerLegendItem* currentChildItem = 0; int nChildItems = childList.count(); for ( int i = 0; i < nChildItems; ++i ) { currentNode = childList.at( i ); if ( !currentNode.isElement() ) { continue; } currentElem = currentNode.toElement(); QString elemTag = currentElem.tagName(); if ( elemTag == "GroupItem" ) { currentChildItem = new QgsComposerGroupItem(); } else if ( elemTag == "LayerItem" ) { currentChildItem = new QgsComposerLayerItem(); } else { continue; //unsupported child item type } currentChildItem->readXML( currentElem, xServerAvailable ); QList<QStandardItem *> itemsList; itemsList << currentChildItem << new QgsComposerStyleItem( currentChildItem ); appendRow( itemsList ); } }
void QgsComposerLegendItem::writeXMLChildren( QDomElement& elem, QDomDocument& doc ) const { int numRows = rowCount(); QgsComposerLegendItem* currentItem = 0; for ( int i = 0; i < numRows; ++i ) { currentItem = dynamic_cast<QgsComposerLegendItem*>( child( i, 0 ) ); if ( currentItem ) { currentItem->writeXML( elem, doc ); } } }
Qt::ItemFlags QgsLegendModel::flags( const QModelIndex &index ) const { Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( !index.isValid() ) { flags |= Qt::ItemIsDropEnabled; return flags; } QStandardItem* item = itemFromIndex( index ); QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); if ( cItem ) { QgsComposerLegendItem::ItemType type = cItem->itemType(); if ( type == QgsComposerLegendItem::GroupItem ) { flags |= Qt::ItemIsDragEnabled; flags |= Qt::ItemIsDropEnabled; } else if ( type == QgsComposerLegendItem::LayerItem ) { flags |= Qt::ItemIsDragEnabled; } } if ( index.column() == 1 && item ) { // Style QStandardItem* firstColumnItem = 0; if ( item->parent() ) { firstColumnItem = item->parent()->child( index.row(), 0 ); } else { firstColumnItem = QgsLegendModel::item( index.row(), 0 ); } cItem = dynamic_cast<QgsComposerLegendItem*>( firstColumnItem ); if ( cItem ) { if ( cItem->itemType() == QgsComposerLegendItem::GroupItem || cItem->itemType() == QgsComposerLegendItem::LayerItem ) { flags |= Qt::ItemIsEditable; } } } return flags; }
void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAvailable ) { if ( itemElem.isNull() ) { return; } setText( itemElem.attribute( "text", "" ) ); setUserText( itemElem.attribute( "userText", "" ) ); setLayerID( itemElem.attribute( "layerId", "" ) ); setShowFeatureCount( itemElem.attribute( "showFeatureCount", "" ) == "1" ? true : false ); setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "subgroup" ) ) ); //now call readXML for all the child items QDomNodeList childList = itemElem.childNodes(); QDomNode currentNode; QDomElement currentElem; QgsComposerLegendItem* currentChildItem = 0; int nChildItems = childList.count(); for ( int i = 0; i < nChildItems; ++i ) { currentNode = childList.at( i ); if ( !currentNode.isElement() ) { continue; } currentElem = currentNode.toElement(); QString elemTag = currentElem.tagName(); if ( elemTag == "VectorClassificationItem" ) { continue; // legacy - unsupported } else if ( elemTag == "VectorClassificationItemNg" ) { currentChildItem = new QgsComposerSymbolV2Item(); } else if ( elemTag == "RasterClassificationItem" ) { currentChildItem = new QgsComposerRasterSymbolItem(); } else { continue; //unsupported child type } currentChildItem->readXML( currentElem, xServerAvailable ); appendRow( currentChildItem ); } }
void QgsComposerLayerItem::readXML( const QDomElement& itemElem ) { if ( itemElem.isNull() ) { return; } setText( itemElem.attribute( "text", "" ) ); setLayerID( itemElem.attribute( "layerId", "" ) ); //now call readXML for all the child items QDomNodeList childList = itemElem.childNodes(); QDomNode currentNode; QDomElement currentElem; QgsComposerLegendItem* currentChildItem = 0; int nChildItems = childList.count(); for ( int i = 0; i < nChildItems; ++i ) { currentNode = childList.at( i ); if ( !currentNode.isElement() ) { continue; } currentElem = currentNode.toElement(); QString elemTag = currentElem.tagName(); if ( elemTag == "VectorClassificationItem" ) { currentChildItem = new QgsComposerSymbolItem(); } else if ( elemTag == "VectorClassificationItemNg" ) { currentChildItem = new QgsComposerSymbolV2Item(); } else if ( elemTag == "RasterClassificationItem" ) { currentChildItem = new QgsComposerRasterSymbolItem(); } else { continue; //unsupported child type } currentChildItem->readXML( currentElem ); appendRow( currentChildItem ); } }
bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocument& doc ) { Q_UNUSED( doc ); if ( legendModelElem.isNull() ) { return false; } clear(); QDomNodeList topLevelItemList = legendModelElem.childNodes(); QDomElement currentElem; QgsComposerLegendItem* currentItem = 0; int nTopLevelItems = topLevelItemList.size(); for ( int i = 0; i < nTopLevelItems; ++i ) { currentElem = topLevelItemList.at( i ).toElement(); if ( currentElem.isNull() ) { continue; } //toplevel items can be groups or layers if ( currentElem.tagName() == "LayerItem" ) { currentItem = new QgsComposerLayerItem(); } else if ( currentElem.tagName() == "GroupItem" ) { currentItem = new QgsComposerGroupItem(); } currentItem->readXML( currentElem, mHasTopLevelWindow ); QList<QStandardItem *> itemsList; itemsList << currentItem << new QgsComposerStyleItem( currentItem ); appendRow( itemsList ); } setAutoUpdate( legendModelElem.attribute( "autoUpdate", "1" ).toInt() ); return true; }
void QgsLegendModel::updateItem( QStandardItem* item ) { if ( !item ) { return; } //only layer items are supported for update QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); if ( ! cItem ) { return; } QgsComposerLegendItem::ItemType type = cItem->itemType(); if ( type == QgsComposerLegendItem::LayerItem ) { updateLayer( cItem ); } }
void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, double& currentYCoord, double& maxXCoord ) { if ( !p || !groupItem ) { return; } currentYCoord += mLayerSpace; currentYCoord += fontAscentMillimeters( mGroupFont ); p->setPen( QColor( 0, 0, 0 ) ); drawText( p, mBoxSpace, currentYCoord, groupItem->text(), mGroupFont ); //maximum x-coordinate of current item double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() ); maxXCoord = qMax( currentMaxXCoord, maxXCoord ); //children can be other group items or layer items int numChildItems = groupItem->rowCount(); QStandardItem* currentChildItem = 0; for ( int i = 0; i < numChildItems; ++i ) { currentChildItem = groupItem->child( i ); QgsComposerLegendItem* currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentChildItem ); QgsComposerLegendItem::ItemType type = currentLegendItem->itemType(); if ( type == QgsComposerLegendItem::GroupItem ) { drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord ); maxXCoord = qMax( currentMaxXCoord, maxXCoord ); } else if ( type == QgsComposerLegendItem::LayerItem ) { drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord ); maxXCoord = qMax( currentMaxXCoord, maxXCoord ); } } }
void QgsLegendModel::updateItemText( QStandardItem* item ) { if ( !item ) { return; } //only layer items are supported for update QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item ); if ( ! cItem ) { return; } QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( cItem ); if ( lItem ) { updateLayerItemText( lItem ); return; } QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( cItem ); if ( sv2Item ) { updateSymbolV2ItemText( sv2Item ); return; } QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( cItem ); if ( rItem ) { updateRasterSymbolItemText( rItem ); return; } // group cItem->setText( cItem->userText() ); }
QMimeData* QgsLegendModel::mimeData( const QModelIndexList &indexes ) const { QMimeData* mimeData = new QMimeData(); QByteArray encodedData; QDomDocument xmlDoc; QDomElement xmlRootElement = xmlDoc.createElement( "LegendModelDragData" ); xmlDoc.appendChild( xmlRootElement ); QModelIndexList::const_iterator indexIt = indexes.constBegin(); for ( ; indexIt != indexes.constEnd(); ++indexIt ) { QStandardItem* sItem = itemFromIndex( *indexIt ); if ( sItem ) { QgsComposerLegendItem* mItem = dynamic_cast<QgsComposerLegendItem*>( sItem ); if ( mItem ) { mItem->writeXML( xmlRootElement, xmlDoc ); } } } mimeData->setData( "text/xml", xmlDoc.toByteArray() ); return mimeData; }
bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) { Q_UNUSED( action ); Q_UNUSED( column ); if ( !data->hasFormat( "text/xml" ) ) { return false; } QStandardItem* dropIntoItem = 0; if ( parent.isValid() ) { dropIntoItem = itemFromIndex( parent ); } else { dropIntoItem = invisibleRootItem(); } //get XML doc QByteArray encodedData = data->data( "text/xml" ); QDomDocument xmlDoc; xmlDoc.setContent( encodedData ); QDomElement dragDataElem = xmlDoc.documentElement(); if ( dragDataElem.tagName() != "LegendModelDragData" ) { return false; } QDomNodeList nodeList = dragDataElem.childNodes(); int nChildNodes = nodeList.size(); QDomElement currentElem; QString currentTagName; QgsComposerLegendItem* currentItem = 0; for ( int i = 0; i < nChildNodes; ++i ) { currentElem = nodeList.at( i ).toElement(); if ( currentElem.isNull() ) { continue; } currentTagName = currentElem.tagName(); if ( currentTagName == "LayerItem" ) { currentItem = new QgsComposerLayerItem(); } else if ( currentTagName == "GroupItem" ) { currentItem = new QgsComposerGroupItem(); } else { continue; } currentItem->readXML( currentElem ); if ( row < 0 ) { dropIntoItem->insertRow( dropIntoItem->rowCount(), currentItem ); } else { dropIntoItem->insertRow( row + i, currentItem ); } } emit layersChanged(); return true; }
QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter ) { QSizeF size; double maxXCoord = 0; //go through model... QStandardItem* rootItem = mLegendModel.invisibleRootItem(); if ( !rootItem ) { return size; } if ( painter ) { painter->save(); drawBackground( painter ); painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); } int numLayerItems = rootItem->rowCount(); QStandardItem* currentLayerItem = 0; double currentYCoordinate = mBoxSpace; //font metrics //draw title currentYCoordinate += fontAscentMillimeters( mTitleFont ); if ( painter ) { painter->setPen( QColor( 0, 0, 0 ) ); drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont ); } maxXCoord = 2 * mBoxSpace + textWidthMillimeters( mTitleFont, mTitle ); double currentItemMaxX = 0; //maximum x-coordinate for current item for ( int i = 0; i < numLayerItems; ++i ) { currentLayerItem = rootItem->child( i ); QgsComposerLegendItem* currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentLayerItem ); if ( currentLegendItem ) { QgsComposerLegendItem::ItemType type = currentLegendItem->itemType(); if ( type == QgsComposerLegendItem::GroupItem ) { drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX ); maxXCoord = qMax( maxXCoord, currentItemMaxX ); } else if ( type == QgsComposerLegendItem::LayerItem ) { drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX ); maxXCoord = qMax( maxXCoord, currentItemMaxX ); } } } currentYCoordinate += mBoxSpace; size.setHeight( currentYCoordinate ); size.setWidth( maxXCoord ); //adjust box if width or height is to small if ( painter && currentYCoordinate > rect().height() ) { setSceneRect( QRectF( transform().dx(), transform().dy(), rect().width(), currentYCoordinate ) ); } if ( painter && maxXCoord > rect().width() ) { setSceneRect( QRectF( transform().dx(), transform().dy(), maxXCoord, rect().height() ) ); } if ( painter ) { painter->restore(); //draw frame and selection boxes if necessary drawFrame( painter ); if ( isSelected() ) { drawSelectionBoxes( painter ); } } return size; }