[composer] Implement saving/loading of QgsComposerTableV2 items

(Sponsored by City of Uster, Switzerland)
This commit is contained in:
Nyall Dawson 2014-09-10 07:03:14 +10:00
parent ced281c832
commit 7d63adeec9
4 changed files with 34 additions and 84 deletions

View File

@ -477,9 +477,9 @@ QList<QPair<int, bool> > QgsComposerAttributeTableV2::sortAttributes() const
return attributesBySortRank; return attributesBySortRank;
} }
bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & doc ) const bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{ {
QDomElement composerTableElem = doc.createElement( "ComposerAttributeTable" ); QDomElement composerTableElem = doc.createElement( "ComposerAttributeTableV2" );
composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures ); composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures );
composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures ); composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures );
composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" ); composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" );
@ -498,30 +498,30 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
composerTableElem.setAttribute( "vectorLayer", mVectorLayer->id() ); composerTableElem.setAttribute( "vectorLayer", mVectorLayer->id() );
} }
bool ok = QgsComposerTableV2::writeXML( composerTableElem, doc, ignoreFrames );
elem.appendChild( composerTableElem ); elem.appendChild( composerTableElem );
//todo
//bool ok = tableWriteXML( composerTableElem, doc );
bool ok = true;
return ok; return ok;
} }
bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QDomDocument& doc ) bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
{ {
if ( itemElem.isNull() ) if ( itemElem.isNull() )
{ {
return false; return false;
} }
//TODO
//read general table properties //read general table properties
//if ( !tableReadXML( itemElem, doc ) ) if ( !QgsComposerTableV2::readXML( itemElem, doc, ignoreFrames ) )
//{ {
// return false; return false;
// } }
mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt(); mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt();
mFilterFeatures = itemElem.attribute( "filterFeatures", "false" ) == "true" ? true : false; mFilterFeatures = itemElem.attribute( "filterFeatures", "false" ) == "true" ? true : false;
mFeatureFilter = itemElem.attribute( "featureFilter", "" ); mFeatureFilter = itemElem.attribute( "featureFilter", "" );
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();
//composer map //composer map
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt(); int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
@ -565,74 +565,9 @@ bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QD
} }
} }
//restore display attribute map. This is required to upgrade pre 2.4 projects.
QSet<int> displayAttributes;
QDomNodeList displayAttributeList = itemElem.elementsByTagName( "displayAttributes" );
if ( displayAttributeList.size() > 0 )
{
QDomElement displayAttributesElem = displayAttributeList.at( 0 ).toElement();
QDomNodeList attributeEntryList = displayAttributesElem.elementsByTagName( "attributeEntry" );
for ( int i = 0; i < attributeEntryList.size(); ++i )
{
QDomElement attributeEntryElem = attributeEntryList.at( i ).toElement();
int index = attributeEntryElem.attribute( "index", "-1" ).toInt();
if ( index != -1 )
{
displayAttributes.insert( index );
}
}
setDisplayAttributes( displayAttributes, false );
}
//restore alias map. This is required to upgrade pre 2.4 projects.
QMap<int, QString> fieldAliasMap;
QDomNodeList aliasMapNodeList = itemElem.elementsByTagName( "attributeAliasMap" );
if ( aliasMapNodeList.size() > 0 )
{
QDomElement attributeAliasMapElem = aliasMapNodeList.at( 0 ).toElement();
QDomNodeList aliasMepEntryList = attributeAliasMapElem.elementsByTagName( "aliasEntry" );
for ( int i = 0; i < aliasMepEntryList.size(); ++i )
{
QDomElement aliasEntryElem = aliasMepEntryList.at( i ).toElement();
int key = aliasEntryElem.attribute( "key", "-1" ).toInt();
QString value = aliasEntryElem.attribute( "value", "" );
fieldAliasMap.insert( key, value );
}
restoreFieldAliasMap( fieldAliasMap );
}
//restore sort columns. This is required to upgrade pre 2.4 projects.
QDomElement sortColumnsElem = itemElem.firstChildElement( "sortColumns" );
if ( !sortColumnsElem.isNull() && mVectorLayer )
{
QDomNodeList columns = sortColumnsElem.elementsByTagName( "column" );
const QgsFields& fields = mVectorLayer->pendingFields();
for ( int i = 0; i < columns.size(); ++i )
{
QDomElement columnElem = columns.at( i ).toElement();
int attribute = columnElem.attribute( "index" ).toInt();
Qt::SortOrder order = columnElem.attribute( "ascending" ) == "true" ? Qt::AscendingOrder : Qt::DescendingOrder;
//find corresponding column
QList<QgsComposerTableColumn*>::iterator columnIt = mColumns.begin();
for ( ; columnIt != mColumns.end(); ++columnIt )
{
if (( *columnIt )->attribute() == fields[attribute].name() )
{
( *columnIt )->setSortByRank( i + 1 );
( *columnIt )->setSortOrder( order );
break;
}
}
}
}
//must be done here because tableReadXML->setSceneRect changes mMaximumNumberOfFeatures
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();
refreshAttributes(); refreshAttributes();
emit itemChanged(); emit changed();
return true; return true;
} }

View File

@ -60,14 +60,14 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @param doc QDomDocument for the destination xml. * @param doc QDomDocument for the destination xml.
* @see readXML * @see readXML
*/ */
bool writeXML( QDomElement& elem, QDomDocument & doc ) const; virtual bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const;
/**Reads the properties specific to an attribute table from xml. /**Reads the properties specific to an attribute table from xml.
* @param itemElem a QDomElement holding the attribute table's desired properties. * @param itemElem a QDomElement holding the attribute table's desired properties.
* @param doc QDomDocument for the source xml. * @param doc QDomDocument for the source xml.
* @see writeXML * @see writeXML
*/ */
bool readXML( const QDomElement& itemElem, const QDomDocument& doc ); virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );
virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ); virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true );

View File

@ -56,7 +56,6 @@ QgsComposerTableV2::~QgsComposerTableV2()
bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{ {
QDomElement tableElem = doc.createElement( "ComposerTableV2" );
elem.setAttribute( "cellMargin", QString::number( mCellMargin ) ); elem.setAttribute( "cellMargin", QString::number( mCellMargin ) );
elem.setAttribute( "headerFont", mHeaderFont.toString() ); elem.setAttribute( "headerFont", mHeaderFont.toString() );
elem.setAttribute( "headerFontColor", QgsSymbolLayerV2Utils::encodeColor( mHeaderFontColor ) ); elem.setAttribute( "headerFontColor", QgsSymbolLayerV2Utils::encodeColor( mHeaderFontColor ) );
@ -77,10 +76,9 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i
( *columnIt )->writeXML( columnElem, doc ); ( *columnIt )->writeXML( columnElem, doc );
displayColumnsElem.appendChild( columnElem ); displayColumnsElem.appendChild( columnElem );
} }
tableElem.appendChild( displayColumnsElem ); elem.appendChild( displayColumnsElem );
bool state = _writeXML( tableElem, doc, ignoreFrames ); bool state = _writeXML( elem, doc, ignoreFrames );
elem.appendChild( tableElem );
return state; return state;
} }

View File

@ -1267,7 +1267,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
} }
} }
// html // html
//TODO - fix this. pasting html items has no effect //TODO - fix this. pasting multiframe frame items has no effect
QDomNodeList composerHtmlList = elem.elementsByTagName( "ComposerHtml" ); QDomNodeList composerHtmlList = elem.elementsByTagName( "ComposerHtml" );
for ( int i = 0; i < composerHtmlList.size(); ++i ) for ( int i = 0; i < composerHtmlList.size(); ++i )
{ {
@ -1285,6 +1285,23 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
frame->setZValue( frame->zValue() + zOrderOffset ); frame->setZValue( frame->zValue() + zOrderOffset );
}*/ }*/
} }
QDomNodeList composerAttributeTableV2List = elem.elementsByTagName( "ComposerAttributeTableV2" );
for ( int i = 0; i < composerAttributeTableV2List.size(); ++i )
{
QDomElement currentTableElem = composerAttributeTableV2List.at( i ).toElement();
QgsComposerAttributeTableV2* newTable = new QgsComposerAttributeTableV2( this, false );
newTable->readXML( currentTableElem, doc );
newTable->setCreateUndoCommands( true );
this->addMultiFrame( newTable );
//offset z values for frames
//TODO - fix this after fixing html item paste
/*for ( int frameIdx = 0; frameIdx < newHtml->frameCount(); ++frameIdx )
{
QgsComposerFrame * frame = newHtml->frame( frameIdx );
frame->setZValue( frame->zValue() + zOrderOffset );
}*/
}
// groups (must be last as it references uuids of above items) // groups (must be last as it references uuids of above items)
//TODO - pasted groups lose group properties, since the uuids of group items //TODO - pasted groups lose group properties, since the uuids of group items