Skip to content

Commit

Permalink
[composer] Implement saving/loading of QgsComposerTableV2 items
Browse files Browse the repository at this point in the history
(Sponsored by City of Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent ced281c commit 7d63ade
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 84 deletions.
89 changes: 12 additions & 77 deletions src/core/composer/qgscomposerattributetablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ QList<QPair<int, bool> > QgsComposerAttributeTableV2::sortAttributes() const
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( "maxFeatures", mMaximumNumberOfFeatures );
composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" );
Expand All @@ -498,30 +498,30 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
composerTableElem.setAttribute( "vectorLayer", mVectorLayer->id() );
}

bool ok = QgsComposerTableV2::writeXML( composerTableElem, doc, ignoreFrames );

elem.appendChild( composerTableElem );
//todo
//bool ok = tableWriteXML( composerTableElem, doc );
bool ok = true;

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() )
{
return false;
}

//TODO
//read general table properties
//if ( !tableReadXML( itemElem, doc ) )
//{
// return false;
// }
if ( !QgsComposerTableV2::readXML( itemElem, doc, ignoreFrames ) )
{
return false;
}

mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt();
mFilterFeatures = itemElem.attribute( "filterFeatures", "false" ) == "true" ? true : false;
mFeatureFilter = itemElem.attribute( "featureFilter", "" );
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();

//composer map
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
Expand Down Expand Up @@ -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();

emit itemChanged();
emit changed();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerattributetablev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @param doc QDomDocument for the destination xml.
* @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.
* @param itemElem a QDomElement holding the attribute table's desired properties.
* @param doc QDomDocument for the source xml.
* @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 );

Expand Down
6 changes: 2 additions & 4 deletions src/core/composer/qgscomposertablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ QgsComposerTableV2::~QgsComposerTableV2()

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

bool state = _writeXML( tableElem, doc, ignoreFrames );
elem.appendChild( tableElem );
bool state = _writeXML( elem, doc, ignoreFrames );
return state;
}

Expand Down
19 changes: 18 additions & 1 deletion src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
}
}
// 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" );
for ( int i = 0; i < composerHtmlList.size(); ++i )
{
Expand All @@ -1285,6 +1285,23 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
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)
//TODO - pasted groups lose group properties, since the uuids of group items
Expand Down

0 comments on commit 7d63ade

Please sign in to comment.