void QgsComposerPicture::refreshPicture( const QgsExpressionContext *context ) { const QgsExpressionContext* evalContext = context; QScopedPointer< QgsExpressionContext > scopedContext; if ( !evalContext ) { scopedContext.reset( createExpressionContext() ); evalContext = scopedContext.data(); } QString source = mSourcePath; //data defined source set? mHasExpressionError = false; QVariant exprVal; if ( dataDefinedProperty( QgsComposerObject::PictureSource )->isActive() ) { if ( dataDefinedEvaluate( QgsComposerObject::PictureSource, exprVal, *evalContext ) ) { source = exprVal.toString().trimmed(); QgsDebugMsg( QString( "exprVal PictureSource:%1" ).arg( source ) ); } else { mHasExpressionError = true; source = QString(); QgsMessageLog::logMessage( tr( "Picture expression eval error" ) ); } } loadPicture( source ); }
void QgsComposerItem::refreshTransparency( const bool updateItem, const QgsExpressionContext& context ) { int transparency = mTransparency; //data defined transparency set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::Transparency, exprVal, context ) ) { bool ok; int transparencyD = exprVal.toInt( &ok ); QgsDebugMsg( QString( "exprVal Transparency:%1" ).arg( transparencyD ) ); if ( ok && !exprVal.isNull() ) { transparency = transparencyD; } } // Set the QGraphicItem's opacity setOpacity( 1. - ( transparency / 100. ) ); if ( updateItem ) { update(); } }
void QgsComposerItem::refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const QgsExpressionContext *context ) { //maintain 2.10 API //TODO QGIS 3.0 - remove this const QgsExpressionContext* evalContext = context; QScopedPointer< QgsExpressionContext > scopedContext; if ( !evalContext ) { scopedContext.reset( createExpressionContext() ); evalContext = scopedContext.data(); } //update data defined properties and redraw item to match if ( property == QgsComposerObject::PositionX || property == QgsComposerObject::PositionY || property == QgsComposerObject::ItemWidth || property == QgsComposerObject::ItemHeight || property == QgsComposerObject::AllProperties ) { QRectF beforeRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() ); QRectF evaluatedRect = evalItemRect( beforeRect, false, evalContext ); if ( evaluatedRect != beforeRect ) { setSceneRect( evaluatedRect ); } } if ( property == QgsComposerObject::ItemRotation || property == QgsComposerObject::AllProperties ) { refreshRotation( false, true, *evalContext ); } if ( property == QgsComposerObject::Transparency || property == QgsComposerObject::AllProperties ) { refreshTransparency( false, *evalContext ); } if ( property == QgsComposerObject::BlendMode || property == QgsComposerObject::AllProperties ) { refreshBlendMode( *evalContext ); } if ( property == QgsComposerObject::ExcludeFromExports || property == QgsComposerObject::AllProperties ) { bool exclude = mExcludeFromExports; //data defined exclude from exports set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::ExcludeFromExports, exprVal, *evalContext ) && !exprVal.isNull() ) { exclude = exprVal.toBool(); } mEvaluatedExcludeFromExports = exclude; } update(); }
void QgsComposerItem::refreshRotation( const bool updateItem, const bool adjustPosition, const QgsExpressionContext& context ) { double rotation = mItemRotation; //data defined rotation set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::ItemRotation, exprVal, context ) ) { bool ok; double rotD = exprVal.toDouble( &ok ); QgsDebugMsg( QString( "exprVal Rotation:%1" ).arg( rotD ) ); if ( ok && !exprVal.isNull() ) { rotation = rotD; } } if ( qgsDoubleNear( rotation, mEvaluatedItemRotation ) ) { return; } if ( adjustPosition ) { //adjustPosition set, so shift the position of the item so that rotation occurs around item center //create a line from the centrepoint of the rect() to its origin, in scene coordinates QLineF refLine = QLineF( mapToScene( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) ), mapToScene( QPointF( 0, 0 ) ) ); //rotate this line by the current rotation angle refLine.setAngle( refLine.angle() - rotation + mEvaluatedItemRotation ); //get new end point of line - this is the new item position QPointF rotatedReferencePoint = refLine.p2(); setPos( rotatedReferencePoint ); emit sizeChanged(); } setTransformOriginPoint( 0, 0 ); QGraphicsItem::setRotation( rotation ); mEvaluatedItemRotation = rotation; emit itemRotationChanged( rotation ); //update bounds of scene, since rotation may affect this mComposition->updateBounds(); if ( updateItem ) { update(); } }
void QgsComposerItem::refreshBlendMode( const QgsExpressionContext& context ) { QPainter::CompositionMode blendMode = mBlendMode; //data defined blend mode set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::BlendMode, exprVal, context ) && !exprVal.isNull() ) { QString blendstr = exprVal.toString().trimmed(); QPainter::CompositionMode blendModeD = QgsSymbolLayerV2Utils::decodeBlendMode( blendstr ); QgsDebugMsg( QString( "exprVal BlendMode:%1" ).arg( blendModeD ) ); blendMode = blendModeD; } // Update the composer effect to use the new blend mode mEffect->setCompositionMode( blendMode ); }
void QgsComposerPicture::refreshPicture() { QString source = mSourcePath; //data defined source set? mHasExpressionError = false; QVariant exprVal; if ( dataDefinedProperty( QgsComposerObject::PictureSource )->isActive() ) { if ( dataDefinedEvaluate( QgsComposerObject::PictureSource, exprVal ) ) { source = exprVal.toString().trimmed(); QgsDebugMsg( QString( "exprVal PictureSource:%1" ).arg( source ) ); } else { mHasExpressionError = true; source = QString(); QgsMessageLog::logMessage( tr( "Picture expression eval error" ) ); } } loadPicture( source ); }
void QgsComposerHtml::loadHtml( const bool useCache, const QgsExpressionContext *context ) { if ( !mWebPage ) { return; } QgsExpressionContext scopedContext = createExpressionContext(); const QgsExpressionContext* evalContext = context ? context : &scopedContext; QString loadedHtml; switch ( mContentMode ) { case QgsComposerHtml::Url: { QString currentUrl = mUrl.toString(); //data defined url set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::SourceUrl, exprVal, *evalContext ) ) { currentUrl = exprVal.toString().trimmed(); QgsDebugMsg( QString( "exprVal Source Url:%1" ).arg( currentUrl ) ); } if ( currentUrl.isEmpty() ) { return; } if ( !( useCache && currentUrl == mLastFetchedUrl ) ) { loadedHtml = fetchHtml( QUrl( currentUrl ) ); mLastFetchedUrl = currentUrl; } else { loadedHtml = mFetchedHtml; } break; } case QgsComposerHtml::ManualHtml: loadedHtml = mHtml; break; } //evaluate expressions if ( mEvaluateExpressions ) { loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, mDistanceArea ); } mLoaded = false; //reset page size. otherwise viewport size increases but never decreases again mWebPage->setViewportSize( QSize( maxFrameWidth() * mHtmlUnitsToMM, 0 ) ); //set html, using the specified url as base if in Url mode or the project file if in manual mode const QUrl baseUrl = mContentMode == QgsComposerHtml::Url ? QUrl( mActualFetchedUrl ) : QUrl::fromLocalFile( QgsProject::instance()->fileInfo().absoluteFilePath() ); mWebPage->mainFrame()->setHtml( loadedHtml, baseUrl ); //set user stylesheet QWebSettings* settings = mWebPage->settings(); if ( mEnableUserStylesheet && ! mUserStylesheet.isEmpty() ) { QByteArray ba; ba.append( mUserStylesheet.toUtf8() ); QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() ); settings->setUserStyleSheetUrl( cssFileURL ); } else { settings->setUserStyleSheetUrl( QUrl() ); } while ( !mLoaded ) { qApp->processEvents(); } //inject JSON feature if ( !mAtlasFeatureJSON.isEmpty() ) { mWebPage->mainFrame()->evaluateJavaScript( QString( "if ( typeof setFeature === \"function\" ) { setFeature(%1); }" ).arg( mAtlasFeatureJSON ) ); //needs an extra process events here to give javascript a chance to execute qApp->processEvents(); } recalculateFrameSizes(); //trigger a repaint emit contentsChanged(); }
QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOnly, const QgsExpressionContext* context ) { QRectF result = newRect; //TODO QGIS 3.0 //maintain pre 2.12 API. remove when API break allowed QScopedPointer< QgsExpressionContext > scopedContext; const QgsExpressionContext* evalContext = context; if ( !evalContext ) { scopedContext.reset( createExpressionContext() ); evalContext = scopedContext.data(); } //data defined position or size set? if so, update rect with data defined values QVariant exprVal; //evaulate width and height first, since they may affect position if non-top-left reference point set if ( dataDefinedEvaluate( QgsComposerObject::ItemWidth, exprVal, *evalContext ) ) { bool ok; double width = exprVal.toDouble( &ok ); QgsDebugMsg( QString( "exprVal Width:%1" ).arg( width ) ); if ( ok && !exprVal.isNull() ) { result.setWidth( width ); } } if ( dataDefinedEvaluate( QgsComposerObject::ItemHeight, exprVal, *evalContext ) ) { bool ok; double height = exprVal.toDouble( &ok ); QgsDebugMsg( QString( "exprVal Height:%1" ).arg( height ) ); if ( ok && !exprVal.isNull() ) { result.setHeight( height ); } } double x = result.left(); //initially adjust for position mode to get x coordinate if ( !resizeOnly ) { //adjust x-coordinate if placement is not done to a left point if ( mLastUsedPositionMode == UpperMiddle || mLastUsedPositionMode == Middle || mLastUsedPositionMode == LowerMiddle ) { x += newRect.width() / 2.0; } else if ( mLastUsedPositionMode == UpperRight || mLastUsedPositionMode == MiddleRight || mLastUsedPositionMode == LowerRight ) { x += newRect.width(); } } else { if ( mLastUsedPositionMode == UpperMiddle || mLastUsedPositionMode == Middle || mLastUsedPositionMode == LowerMiddle ) { x += rect().width() / 2.0; } else if ( mLastUsedPositionMode == UpperRight || mLastUsedPositionMode == MiddleRight || mLastUsedPositionMode == LowerRight ) { x += rect().width(); } } if ( dataDefinedEvaluate( QgsComposerObject::PositionX, exprVal, *evalContext ) ) { bool ok; double positionX = exprVal.toDouble( &ok ); QgsDebugMsg( QString( "exprVal Position X:%1" ).arg( positionX ) ); if ( ok && !exprVal.isNull() ) { x = positionX; } } double y = result.top(); //initially adjust for position mode to get y coordinate if ( !resizeOnly ) { //adjust y-coordinate if placement is not done to an upper point if ( mLastUsedPositionMode == MiddleLeft || mLastUsedPositionMode == Middle || mLastUsedPositionMode == MiddleRight ) { y += newRect.height() / 2.0; } else if ( mLastUsedPositionMode == LowerLeft || mLastUsedPositionMode == LowerMiddle || mLastUsedPositionMode == LowerRight ) { y += newRect.height(); } } else { if ( mLastUsedPositionMode == MiddleLeft || mLastUsedPositionMode == Middle || mLastUsedPositionMode == MiddleRight ) { y += rect().height() / 2.0; } else if ( mLastUsedPositionMode == LowerLeft || mLastUsedPositionMode == LowerMiddle || mLastUsedPositionMode == LowerRight ) { y += rect().height(); } } if ( dataDefinedEvaluate( QgsComposerObject::PositionY, exprVal, *evalContext ) ) { bool ok; double positionY = exprVal.toDouble( &ok ); QgsDebugMsg( QString( "exprVal Position Y:%1" ).arg( positionY ) ); if ( ok && !exprVal.isNull() ) { y = positionY; } } //adjust x-coordinate if placement is not done to a left point if ( mLastUsedPositionMode == UpperMiddle || mLastUsedPositionMode == Middle || mLastUsedPositionMode == LowerMiddle ) { x -= result.width() / 2.0; } else if ( mLastUsedPositionMode == UpperRight || mLastUsedPositionMode == MiddleRight || mLastUsedPositionMode == LowerRight ) { x -= result.width(); } //adjust y-coordinate if placement is not done to an upper point if ( mLastUsedPositionMode == MiddleLeft || mLastUsedPositionMode == Middle || mLastUsedPositionMode == MiddleRight ) { y -= result.height() / 2.0; } else if ( mLastUsedPositionMode == LowerLeft || mLastUsedPositionMode == LowerMiddle || mLastUsedPositionMode == LowerRight ) { y -= result.height(); } result.moveLeft( x ); result.moveTop( y ); return result; }
void QgsComposerHtml::loadHtml( const bool useCache, const QgsExpressionContext *context ) { if ( !mWebPage ) { return; } const QgsExpressionContext* evalContext = context; QScopedPointer< QgsExpressionContext > scopedContext; if ( !evalContext ) { scopedContext.reset( createExpressionContext() ); evalContext = scopedContext.data(); } QString loadedHtml; switch ( mContentMode ) { case QgsComposerHtml::Url: { QString currentUrl = mUrl.toString(); //data defined url set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::SourceUrl, exprVal, *evalContext ) ) { currentUrl = exprVal.toString().trimmed(); QgsDebugMsg( QString( "exprVal Source Url:%1" ).arg( currentUrl ) ); } if ( currentUrl.isEmpty() ) { return; } if ( !( useCache && currentUrl == mLastFetchedUrl ) ) { loadedHtml = fetchHtml( QUrl( currentUrl ) ); mLastFetchedUrl = currentUrl; } else { loadedHtml = mFetchedHtml; } break; } case QgsComposerHtml::ManualHtml: loadedHtml = mHtml; break; } //evaluate expressions if ( mEvaluateExpressions ) { loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, nullptr, mDistanceArea ); } mLoaded = false; //reset page size. otherwise viewport size increases but never decreases again mWebPage->setViewportSize( QSize( maxFrameWidth() * mHtmlUnitsToMM, 0 ) ); //set html, using the specified url as base if in Url mode mWebPage->mainFrame()->setHtml( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl( mActualFetchedUrl ) : QUrl() ); //set user stylesheet QWebSettings* settings = mWebPage->settings(); if ( mEnableUserStylesheet && ! mUserStylesheet.isEmpty() ) { QByteArray ba; ba.append( mUserStylesheet.toUtf8() ); QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() ); settings->setUserStyleSheetUrl( cssFileURL ); } else { settings->setUserStyleSheetUrl( QUrl() ); } while ( !mLoaded ) { qApp->processEvents(); } recalculateFrameSizes(); //trigger a repaint emit contentsChanged(); }
void QgsComposerHtml::loadHtml() { if ( !mWebPage ) { return; } QString loadedHtml; switch ( mContentMode ) { case QgsComposerHtml::Url: { QString currentUrl = mUrl.toString(); //data defined url set? QVariant exprVal; if ( dataDefinedEvaluate( QgsComposerObject::SourceUrl, exprVal ) ) { currentUrl = exprVal.toString().trimmed();; QgsDebugMsg( QString( "exprVal Source Url:%1" ).arg( currentUrl ) ); } if ( currentUrl.isEmpty() ) { return; } if ( currentUrl != mLastFetchedUrl ) { loadedHtml = fetchHtml( QUrl( currentUrl ) ); mLastFetchedUrl = currentUrl; } else { loadedHtml = mFetchedHtml; } break; } case QgsComposerHtml::ManualHtml: loadedHtml = mHtml; break; } //evaluate expressions if ( mEvaluateExpressions ) { loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, mExpressionFeature, mExpressionLayer ); } mLoaded = false; //set html, using the specified url as base if in Url mode mWebPage->mainFrame()->setHtml( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl( mActualFetchedUrl ) : QUrl() ); //set user stylesheet QWebSettings* settings = mWebPage->settings(); if ( mEnableUserStylesheet && ! mUserStylesheet.isEmpty() ) { QByteArray ba; ba.append( mUserStylesheet.toUtf8() ); QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() ); settings->setUserStyleSheetUrl( cssFileURL ); } else { settings->setUserStyleSheetUrl( QUrl() ); } while ( !mLoaded ) { qApp->processEvents(); } if ( frameCount() < 1 ) return; QSize contentsSize = mWebPage->mainFrame()->contentsSize(); //find maximum frame width double maxFrameWidth = 0; QList<QgsComposerFrame*>::const_iterator frameIt = mFrameItems.constBegin(); for ( ; frameIt != mFrameItems.constEnd(); ++frameIt ) { maxFrameWidth = qMax( maxFrameWidth, ( *frameIt )->boundingRect().width() ); } //set content width to match maximum frame width contentsSize.setWidth( maxFrameWidth * mHtmlUnitsToMM ); mWebPage->setViewportSize( contentsSize ); mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff ); mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff ); mSize.setWidth( contentsSize.width() / mHtmlUnitsToMM ); mSize.setHeight( contentsSize.height() / mHtmlUnitsToMM ); renderCachedImage(); recalculateFrameSizes(); emit changed(); //trigger a repaint emit contentsChanged(); }