Skip to content

Commit 7d63ade

Browse files
committed
[composer] Implement saving/loading of QgsComposerTableV2 items
(Sponsored by City of Uster, Switzerland)
1 parent ced281c commit 7d63ade

File tree

4 files changed

+34
-84
lines changed

4 files changed

+34
-84
lines changed

src/core/composer/qgscomposerattributetablev2.cpp

Lines changed: 12 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ QList<QPair<int, bool> > QgsComposerAttributeTableV2::sortAttributes() const
477477
return attributesBySortRank;
478478
}
479479

480-
bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & doc ) const
480+
bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
481481
{
482-
QDomElement composerTableElem = doc.createElement( "ComposerAttributeTable" );
482+
QDomElement composerTableElem = doc.createElement( "ComposerAttributeTableV2" );
483483
composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures );
484484
composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures );
485485
composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" );
@@ -498,30 +498,30 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
498498
composerTableElem.setAttribute( "vectorLayer", mVectorLayer->id() );
499499
}
500500

501+
bool ok = QgsComposerTableV2::writeXML( composerTableElem, doc, ignoreFrames );
502+
501503
elem.appendChild( composerTableElem );
502-
//todo
503-
//bool ok = tableWriteXML( composerTableElem, doc );
504-
bool ok = true;
504+
505505
return ok;
506506
}
507507

508-
bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QDomDocument& doc )
508+
bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
509509
{
510510
if ( itemElem.isNull() )
511511
{
512512
return false;
513513
}
514514

515-
//TODO
516515
//read general table properties
517-
//if ( !tableReadXML( itemElem, doc ) )
518-
//{
519-
// return false;
520-
// }
516+
if ( !QgsComposerTableV2::readXML( itemElem, doc, ignoreFrames ) )
517+
{
518+
return false;
519+
}
521520

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

526526
//composer map
527527
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
@@ -565,74 +565,9 @@ bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QD
565565
}
566566
}
567567

568-
//restore display attribute map. This is required to upgrade pre 2.4 projects.
569-
QSet<int> displayAttributes;
570-
QDomNodeList displayAttributeList = itemElem.elementsByTagName( "displayAttributes" );
571-
if ( displayAttributeList.size() > 0 )
572-
{
573-
QDomElement displayAttributesElem = displayAttributeList.at( 0 ).toElement();
574-
QDomNodeList attributeEntryList = displayAttributesElem.elementsByTagName( "attributeEntry" );
575-
for ( int i = 0; i < attributeEntryList.size(); ++i )
576-
{
577-
QDomElement attributeEntryElem = attributeEntryList.at( i ).toElement();
578-
int index = attributeEntryElem.attribute( "index", "-1" ).toInt();
579-
if ( index != -1 )
580-
{
581-
displayAttributes.insert( index );
582-
}
583-
}
584-
setDisplayAttributes( displayAttributes, false );
585-
}
586-
587-
//restore alias map. This is required to upgrade pre 2.4 projects.
588-
QMap<int, QString> fieldAliasMap;
589-
QDomNodeList aliasMapNodeList = itemElem.elementsByTagName( "attributeAliasMap" );
590-
if ( aliasMapNodeList.size() > 0 )
591-
{
592-
QDomElement attributeAliasMapElem = aliasMapNodeList.at( 0 ).toElement();
593-
QDomNodeList aliasMepEntryList = attributeAliasMapElem.elementsByTagName( "aliasEntry" );
594-
for ( int i = 0; i < aliasMepEntryList.size(); ++i )
595-
{
596-
QDomElement aliasEntryElem = aliasMepEntryList.at( i ).toElement();
597-
int key = aliasEntryElem.attribute( "key", "-1" ).toInt();
598-
QString value = aliasEntryElem.attribute( "value", "" );
599-
fieldAliasMap.insert( key, value );
600-
}
601-
restoreFieldAliasMap( fieldAliasMap );
602-
}
603-
604-
//restore sort columns. This is required to upgrade pre 2.4 projects.
605-
QDomElement sortColumnsElem = itemElem.firstChildElement( "sortColumns" );
606-
if ( !sortColumnsElem.isNull() && mVectorLayer )
607-
{
608-
QDomNodeList columns = sortColumnsElem.elementsByTagName( "column" );
609-
const QgsFields& fields = mVectorLayer->pendingFields();
610-
611-
for ( int i = 0; i < columns.size(); ++i )
612-
{
613-
QDomElement columnElem = columns.at( i ).toElement();
614-
int attribute = columnElem.attribute( "index" ).toInt();
615-
Qt::SortOrder order = columnElem.attribute( "ascending" ) == "true" ? Qt::AscendingOrder : Qt::DescendingOrder;
616-
//find corresponding column
617-
QList<QgsComposerTableColumn*>::iterator columnIt = mColumns.begin();
618-
for ( ; columnIt != mColumns.end(); ++columnIt )
619-
{
620-
if (( *columnIt )->attribute() == fields[attribute].name() )
621-
{
622-
( *columnIt )->setSortByRank( i + 1 );
623-
( *columnIt )->setSortOrder( order );
624-
break;
625-
}
626-
}
627-
}
628-
}
629-
630-
//must be done here because tableReadXML->setSceneRect changes mMaximumNumberOfFeatures
631-
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();
632-
633568
refreshAttributes();
634569

635-
emit itemChanged();
570+
emit changed();
636571
return true;
637572
}
638573

src/core/composer/qgscomposerattributetablev2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
6060
* @param doc QDomDocument for the destination xml.
6161
* @see readXML
6262
*/
63-
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
63+
virtual bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const;
6464

6565
/**Reads the properties specific to an attribute table from xml.
6666
* @param itemElem a QDomElement holding the attribute table's desired properties.
6767
* @param doc QDomDocument for the source xml.
6868
* @see writeXML
6969
*/
70-
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
70+
virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );
7171

7272
virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true );
7373

src/core/composer/qgscomposertablev2.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ QgsComposerTableV2::~QgsComposerTableV2()
5656

5757
bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
5858
{
59-
QDomElement tableElem = doc.createElement( "ComposerTableV2" );
6059
elem.setAttribute( "cellMargin", QString::number( mCellMargin ) );
6160
elem.setAttribute( "headerFont", mHeaderFont.toString() );
6261
elem.setAttribute( "headerFontColor", QgsSymbolLayerV2Utils::encodeColor( mHeaderFontColor ) );
@@ -77,10 +76,9 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i
7776
( *columnIt )->writeXML( columnElem, doc );
7877
displayColumnsElem.appendChild( columnElem );
7978
}
80-
tableElem.appendChild( displayColumnsElem );
79+
elem.appendChild( displayColumnsElem );
8180

82-
bool state = _writeXML( tableElem, doc, ignoreFrames );
83-
elem.appendChild( tableElem );
81+
bool state = _writeXML( elem, doc, ignoreFrames );
8482
return state;
8583
}
8684

src/core/composer/qgscomposition.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
12671267
}
12681268
}
12691269
// html
1270-
//TODO - fix this. pasting html items has no effect
1270+
//TODO - fix this. pasting multiframe frame items has no effect
12711271
QDomNodeList composerHtmlList = elem.elementsByTagName( "ComposerHtml" );
12721272
for ( int i = 0; i < composerHtmlList.size(); ++i )
12731273
{
@@ -1285,6 +1285,23 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
12851285
frame->setZValue( frame->zValue() + zOrderOffset );
12861286
}*/
12871287
}
1288+
QDomNodeList composerAttributeTableV2List = elem.elementsByTagName( "ComposerAttributeTableV2" );
1289+
for ( int i = 0; i < composerAttributeTableV2List.size(); ++i )
1290+
{
1291+
QDomElement currentTableElem = composerAttributeTableV2List.at( i ).toElement();
1292+
QgsComposerAttributeTableV2* newTable = new QgsComposerAttributeTableV2( this, false );
1293+
newTable->readXML( currentTableElem, doc );
1294+
newTable->setCreateUndoCommands( true );
1295+
this->addMultiFrame( newTable );
1296+
1297+
//offset z values for frames
1298+
//TODO - fix this after fixing html item paste
1299+
/*for ( int frameIdx = 0; frameIdx < newHtml->frameCount(); ++frameIdx )
1300+
{
1301+
QgsComposerFrame * frame = newHtml->frame( frameIdx );
1302+
frame->setZValue( frame->zValue() + zOrderOffset );
1303+
}*/
1304+
}
12881305

12891306
// groups (must be last as it references uuids of above items)
12901307
//TODO - pasted groups lose group properties, since the uuids of group items

0 commit comments

Comments
 (0)