Skip to content

Commit

Permalink
[layouts] Removed diff images and adapted tests
Browse files Browse the repository at this point in the history
to use QGIS font

Also addressed comments in the PR
  • Loading branch information
elpaso committed Jan 9, 2018
1 parent 943025c commit a5fc570
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 166 deletions.
194 changes: 97 additions & 97 deletions src/core/layout/qgscompositionconverter.cpp
Expand Up @@ -105,47 +105,47 @@ QgsPropertiesDefinition QgsCompositionConverter::propertyDefinitions()
}


QgsLayout *QgsCompositionConverter::createLayoutFromCompositionXml( const QDomElement &parentElement, QgsProject *project )
std::unique_ptr< QgsLayout > QgsCompositionConverter::createLayoutFromCompositionXml( const QDomElement &parentElement, QgsProject *project )
{
initPropertyDefinitions();
QgsLayout *layout = new QgsLayout( project );
std::unique_ptr< QgsLayout > layout = qgis::make_unique< QgsLayout >( project );
// Create pages
int pages = parentElement.attribute( QStringLiteral( "numPages" ) ).toInt( );
float paperHeight = parentElement.attribute( QStringLiteral( "paperHeight" ) ).toFloat( );
float paperWidth = parentElement.attribute( QStringLiteral( "paperWidth" ) ).toFloat( );
float paperHeight = parentElement.attribute( QStringLiteral( "paperHeight" ) ).toDouble( );
float paperWidth = parentElement.attribute( QStringLiteral( "paperWidth" ) ).toDouble( );

if ( parentElement.elementsByTagName( QStringLiteral( "symbol" ) ).size() )
{
QDomElement symbolElement = parentElement.elementsByTagName( QStringLiteral( "symbol" ) ).at( 0 ).toElement();
QgsReadWriteContext context;
context.setPathResolver( project->pathResolver() );
if ( project )
context.setPathResolver( project->pathResolver() );
QgsFillSymbol *symbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElement, context );
if ( symbol )
layout->pageCollection()->setPageStyleSymbol( symbol );
}

QString name = parentElement.attribute( QStringLiteral( "name" ) );
layout->setName( name );
// TODO: check that it is always landscape
QgsLayoutSize pageSize( paperWidth, paperHeight );
for ( int j = 0; j < pages; j++ )
{
QgsLayoutItemPage *page = QgsLayoutItemPage::create( layout );
QgsLayoutItemPage *page = QgsLayoutItemPage::create( layout.get() );
page->setPageSize( pageSize );
layout->pageCollection()->addPage( page );
}
addItemsFromCompositionXml( layout, parentElement );
addItemsFromCompositionXml( layout.get(), parentElement );
return layout;
}

void QgsCompositionConverter::adjustPos( QgsLayout *layout, QgsLayoutItem *layoutItem, QDomNode &itemNode, QPointF *position, bool &pasteInPlace, int zOrderOffset, QPointF &pasteShiftPos, int &pageNumber )
{
Q_UNUSED( itemNode );
if ( position )
{
if ( pasteInPlace )
{
QgsLayoutPoint posOnPage = QgsLayoutPoint::decodePoint( itemNode.toElement().attribute( QStringLiteral( "positionOnPage" ) ) );
layoutItem->attemptMove( posOnPage, true, false, pageNumber );
layoutItem->attemptMove( QgsLayoutPoint( *position ), true, false, pageNumber );
}
else
{
Expand All @@ -171,12 +171,79 @@ void QgsCompositionConverter::restoreGeneralComposeItemProperties( QgsLayoutItem
//check for old (pre 2.1) rotation attribute
layoutItem->setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), false );
}

QgsCompositionConverter::readXml( layoutItem, composerItemElem );
}
}

// Frame color
QRectF QgsCompositionConverter::itemPosition( QgsLayoutItem *layoutItem, const QDomElement &itemElem )
{
int page;
double x, y, pagex, pagey, width, height;
bool xOk, yOk, pageOk, pagexOk, pageyOk, widthOk, heightOk, positionModeOk;

// Background color
x = itemElem.attribute( QStringLiteral( "x" ) ).toDouble( &xOk );
y = itemElem.attribute( QStringLiteral( "y" ) ).toDouble( &yOk );
page = itemElem.attribute( QStringLiteral( "page" ) ).toInt( &pageOk );
pagex = itemElem.attribute( QStringLiteral( "pagex" ) ).toDouble( &pagexOk );
pagey = itemElem.attribute( QStringLiteral( "pagey" ) ).toDouble( &pageyOk );
width = itemElem.attribute( QStringLiteral( "width" ) ).toDouble( &widthOk );
height = itemElem.attribute( QStringLiteral( "height" ) ).toDouble( &heightOk );


layoutItem->mReferencePoint = static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "positionMode" ) ).toInt( &positionModeOk ) );
if ( !positionModeOk )
{
layoutItem->setReferencePoint( QgsLayoutItem::ReferencePoint::UpperLeft );
}

if ( pageOk && pagexOk && pageyOk )
{
xOk = true;
yOk = true;
x = pagex;
// position in the page (1-based)
if ( page <= layoutItem->layout()->pageCollection()->pageCount() )
{
QgsLayoutItemPage *pageObject = layoutItem->layout()->pageCollection()->pages().at( page - 1 );
y = ( page - 1 )
* ( pageObject->sizeWithUnits().height()
+ layoutItem->layout()->pageCollection()->spaceBetweenPages() )
+ pagey;
}
else
{
y = pagey;
}
}
return QRectF( x, y, width, height );
}

QPointF QgsCompositionConverter::minPointFromXml( const QDomElement &elem )
{
double minX = std::numeric_limits<double>::max();
double minY = std::numeric_limits<double>::max();
QDomNodeList composerItemList = elem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
for ( int i = 0; i < composerItemList.size(); ++i )
{
QDomElement currentComposerItemElem = composerItemList.at( i ).toElement();
double x, y;
bool xOk, yOk;
x = currentComposerItemElem.attribute( QStringLiteral( "x" ) ).toDouble( &xOk );
y = currentComposerItemElem.attribute( QStringLiteral( "y" ) ).toDouble( &yOk );
if ( !xOk || !yOk )
{
continue;
}
minX = std::min( minX, x );
minY = std::min( minY, y );
}
if ( minX < std::numeric_limits<double>::max() )
{
return QPointF( minX, minY );
}
else
{
return QPointF( 0, 0 );
}
}

Expand All @@ -200,8 +267,7 @@ QList<QgsLayoutItem *> QgsCompositionConverter::addItemsFromCompositionXml( QgsL
//If we are placing items relative to a certain point, then calculate how much we need
//to shift the items by so that they are placed at this point
//First, calculate the minimum position from the xml
// TODO: check this!
QPointF minItemPos = layout->minPointFromXml( parentElement );
QPointF minItemPos = minPointFromXml( parentElement );
//next, calculate how much each item needs to be shifted from its original position
//so that it's placed at the correct relative position
pasteShiftPos = *position - minItemPos;
Expand Down Expand Up @@ -383,20 +449,16 @@ bool QgsCompositionConverter::readShapeXml( QgsLayoutItemShape *layoutItem, cons
restoreGeneralComposeItemProperties( layoutItem, itemElem );

QgsReadWriteContext context;
context.setPathResolver( project->pathResolver() );
if ( project )
context.setPathResolver( project->pathResolver() );

if ( itemElem.elementsByTagName( QStringLiteral( "symbol" ) ).size() )
{
QDomElement symbolElement = itemElem.elementsByTagName( QStringLiteral( "symbol" ) ).at( 0 ).toElement();
QgsFillSymbol *shapeStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElement, context );
if ( shapeStyleSymbol )
layoutItem->setSymbol( shapeStyleSymbol );
} /*
QDomElement shapeStyleSymbolElem = itemElem.firstChildElement( QStringLiteral( "symbol" ) );
if ( !shapeStyleSymbolElem.isNull() )
{
layoutItem->setSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( shapeStyleSymbolElem, context ) );
} */
}
else
{
//upgrade project file from 2.0 to use symbol styling
Expand Down Expand Up @@ -431,10 +493,10 @@ bool QgsCompositionConverter::readShapeXml( QgsLayoutItemShape *layoutItem, cons
double penWidth;

penWidth = itemElem.attribute( QStringLiteral( "outlineWidth" ) ).toDouble( &widthOk );
penRed = frameColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
penGreen = frameColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
penBlue = frameColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
penAlpha = frameColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
penRed = frameColorElem.attribute( QStringLiteral( "red" ) ).toInt( &redOk );
penGreen = frameColorElem.attribute( QStringLiteral( "green" ) ).toInt( &greenOk );
penBlue = frameColorElem.attribute( QStringLiteral( "blue" ) ).toInt( &blueOk );
penAlpha = frameColorElem.attribute( QStringLiteral( "alpha" ) ).toInt( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk && widthOk )
{
Expand All @@ -449,10 +511,10 @@ bool QgsCompositionConverter::readShapeXml( QgsLayoutItemShape *layoutItem, cons
bool redOk, greenOk, blueOk, alphaOk;
int fillRed, fillGreen, fillBlue, fillAlpha;

fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toInt( &redOk );
fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toInt( &greenOk );
fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toInt( &blueOk );
fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toInt( &alphaOk );

if ( redOk && greenOk && blueOk && alphaOk )
{
Expand Down Expand Up @@ -632,7 +694,9 @@ bool QgsCompositionConverter::readMapXml( QgsLayoutItemMap *layoutItem, const QD
*/

QgsReadWriteContext context;
context.setPathResolver( project->pathResolver() );

if ( project )
context.setPathResolver( project->pathResolver() );

//extent
QDomNodeList extentNodeList = itemElem.elementsByTagName( QStringLiteral( "Extent" ) );
Expand Down Expand Up @@ -812,29 +876,6 @@ bool QgsCompositionConverter::readMapXml( QgsLayoutItemMap *layoutItem, const QD
layoutItem->mGridStack->addGrid( mapGrid );
}

/* TODO: skip?
//load overview in old xml format
QDomElement overviewFrameElem = itemElem.firstChildElement( QStringLiteral( "overviewFrame" ) );
if ( !overviewFrameElem.isNull() )
{
QgsComposerMapOverview *mapOverview = new QgsComposerMapOverview( tr( "Overview %1" ).arg( mOverviewStack->size() + 1 ), this );
mapOverview->setFrameMap( overviewFrameElem.attribute( QStringLiteral( "overviewFrameMap" ), QStringLiteral( "-1" ) ).toInt() );
mapOverview->setBlendMode( QgsPainting::getCompositionMode( static_cast< QgsPainting::BlendMode >( overviewFrameElem.attribute( QStringLiteral( "overviewBlendMode" ), QStringLiteral( "0" ) ).toUInt() ) ) );
mapOverview->setInverted( overviewFrameElem.attribute( QStringLiteral( "overviewInverted" ) ).compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 );
mapOverview->setCentered( overviewFrameElem.attribute( QStringLiteral( "overviewCentered" ) ).compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 );
QgsFillSymbol *fillSymbol = nullptr;
QDomElement overviewFrameSymbolElem = overviewFrameElem.firstChildElement( QStringLiteral( "symbol" ) );
if ( !overviewFrameSymbolElem.isNull() )
{
fillSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( overviewFrameSymbolElem, context );
mapOverview->setFrameSymbol( fillSymbol );
}
mOverviewStack->addOverview( mapOverview );
}
*/

//atlas TODO:
/*
QDomNodeList atlasNodeList = itemElem.elementsByTagName( QStringLiteral( "AtlasMap" ) );
Expand Down Expand Up @@ -1208,49 +1249,8 @@ bool QgsCompositionConverter::readXml( QgsLayoutItem *layoutItem, const QDomElem
}
layoutItem->mTemplateUuid = itemElem.attribute( "templateUuid" );

int page;
double x, y, pagex, pagey, width, height;
bool xOk, yOk, pageOk, pagexOk, pageyOk, widthOk, heightOk, positionModeOK;

x = itemElem.attribute( QStringLiteral( "x" ) ).toDouble( &xOk );
y = itemElem.attribute( QStringLiteral( "y" ) ).toDouble( &yOk );
page = itemElem.attribute( QStringLiteral( "page" ) ).toInt( &pageOk );
pagex = itemElem.attribute( QStringLiteral( "pagex" ) ).toDouble( &pagexOk );
pagey = itemElem.attribute( QStringLiteral( "pagey" ) ).toDouble( &pageyOk );
width = itemElem.attribute( QStringLiteral( "width" ) ).toDouble( &widthOk );
height = itemElem.attribute( QStringLiteral( "height" ) ).toDouble( &heightOk );


layoutItem->mReferencePoint = static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "positionMode" ) ).toInt( &positionModeOK ) );
if ( !positionModeOK )
{
layoutItem->setReferencePoint( QgsLayoutItem::ReferencePoint::UpperLeft );
}

if ( pageOk && pagexOk && pageyOk )
{
xOk = true;
yOk = true;
x = pagex;
// position in the page (1-based)
if ( page <= layoutItem->layout()->pageCollection()->pageCount() )
{
QgsLayoutItemPage *pageObject = layoutItem->layout()->pageCollection()->pages().at( page - 1 );
y = ( page - 1 )
* ( pageObject->sizeWithUnits().height()
+ layoutItem->layout()->pageCollection()->spaceBetweenPages() )
+ pagey;
}
else
{
y = pagey;
}
}

if ( !xOk || !yOk || !widthOk || !heightOk )
{
return false;
}
QRectF position = itemPosition( layoutItem, itemElem );

// TODO: missing?
// mLastValidViewScaleFactor = itemElem.attribute( QStringLiteral( "lastValidViewScaleFactor" ), QStringLiteral( "-1" ) ).toDouble();
Expand Down Expand Up @@ -1323,7 +1323,7 @@ bool QgsCompositionConverter::readXml( QgsLayoutItem *layoutItem, const QDomElem
layoutItem->mEvaluatedExcludeFromExports = layoutItem->mExcludeFromExports;

// positioning
layoutItem->attemptSetSceneRect( QRectF( x, y, width, height ) );
layoutItem->attemptSetSceneRect( position );
//rotation
layoutItem->setItemRotation( itemElem.attribute( QStringLiteral( "itemRotation" ), QStringLiteral( "0" ) ).toDouble(), false );

Expand Down
10 changes: 7 additions & 3 deletions src/core/layout/qgscompositionconverter.h
Expand Up @@ -125,9 +125,8 @@ class CORE_EXPORT QgsCompositionConverter
* \param project the QGIS project
* \return a QgsLayout instance
* \since QGIS 3.0
* \note Not available in Python bindings.
*/
static QgsLayout *createLayoutFromCompositionXml( const QDomElement &parentElement,
static std::unique_ptr< QgsLayout > createLayoutFromCompositionXml( const QDomElement &parentElement,
QgsProject *project );


Expand All @@ -140,7 +139,6 @@ class CORE_EXPORT QgsCompositionConverter
* \param pasteInPlace if true element position is translated to \a position
* \return a list of layout items
* \since QGIS 3.0
* \note Not available in Python bindings.
*/
static QList<QgsLayoutItem *> addItemsFromCompositionXml( QgsLayout *layout,
const QDomElement &parentElement,
Expand Down Expand Up @@ -207,6 +205,12 @@ class CORE_EXPORT QgsCompositionConverter
//! Restore general composer item properties
static void restoreGeneralComposeItemProperties( QgsLayoutItem *layoutItem, const QDomElement &itemElem );

//! Get item position
static QRectF itemPosition( QgsLayoutItem *layoutItem, const QDomElement &itemElem );

//! Calculates the item minimum position from an xml string
static QPointF minPointFromXml( const QDomElement &elem );

};

#endif // QGSCOMPOSITIONCONVERTER_H
5 changes: 2 additions & 3 deletions src/core/layout/qgslayoutmanager.cpp
Expand Up @@ -193,10 +193,9 @@ bool QgsLayoutManager::readXml( const QDomElement &element, const QDomDocument &
QDomNodeList compositionNodes = composerNodes.at( i ).toElement().elementsByTagName( QStringLiteral( "Composition" ) );
for ( int j = 0; j < compositionNodes.size(); ++j )
{
QgsLayout *l = nullptr;
l = QgsCompositionConverter::createLayoutFromCompositionXml( compositionNodes.at( j ).toElement(), mProject );
std::unique_ptr< QgsLayout > l( QgsCompositionConverter::createLayoutFromCompositionXml( compositionNodes.at( j ).toElement(), mProject ) );
if ( l )
addLayout( l );
addLayout( l.release() );
}

// legacy import
Expand Down

0 comments on commit a5fc570

Please sign in to comment.