Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-friendly warnings when built without webkit and opening layouts with html items #58997

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,9 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutNorthArrowValidityCheck() );
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutOverviewValidityCheck() );
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutPictureSourceValidityCheck() );
#ifndef WITH_QTWEBKIT
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutHtmlItemValidityCheck() );
#endif

mSplash->showMessage( tr( "Initializing file filters" ), Qt::AlignHCenter | Qt::AlignBottom, splashTextColor );
qApp->processEvents();
Expand Down
37 changes: 36 additions & 1 deletion src/core/layout/qgslayoutitemhtml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <QNetworkReply>
#include <QThread>
#include <QUrl>
#include <QAbstractTextDocumentLayout>

// clazy:excludeall=lambda-in-connect

Expand Down Expand Up @@ -335,8 +336,10 @@ QSizeF QgsLayoutItemHtml::totalSize() const
return mSize;
}

void QgsLayoutItemHtml::render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, const int )
void QgsLayoutItemHtml::render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, const int frameIndex )
{
#ifdef WITH_QTWEBKIT
Q_UNUSED( frameIndex )
if ( !mWebPage )
return;

Expand All @@ -346,6 +349,38 @@ void QgsLayoutItemHtml::render( QgsLayoutItemRenderContext &context, const QRect
painter->scale( context.renderContext().scaleFactor() / mHtmlUnitsToLayoutUnits, context.renderContext().scaleFactor() / mHtmlUnitsToLayoutUnits );
painter->translate( 0.0, -renderExtent.top() * mHtmlUnitsToLayoutUnits );
mWebPage->mainFrame()->render( painter, QRegion( renderExtent.left(), renderExtent.top() * mHtmlUnitsToLayoutUnits, renderExtent.width() * mHtmlUnitsToLayoutUnits, renderExtent.height() * mHtmlUnitsToLayoutUnits ) );
#else
Q_UNUSED( renderExtent )
if ( mLayout->renderContext().isPreviewRender() )
{
if ( QgsLayoutFrame *currentFrame = frame( frameIndex ) )
{
QPainter *painter = context.renderContext().painter();

// painter is scaled to dots, so scale back to layout units
const QRectF painterRect = QRectF( currentFrame->rect().left() * context.renderContext().scaleFactor(),
currentFrame->rect().top() * context.renderContext().scaleFactor(),
currentFrame->rect().width() * context.renderContext().scaleFactor(),
currentFrame->rect().height() * context.renderContext().scaleFactor()
);

painter->setBrush( QBrush( QColor( 255, 125, 125, 125 ) ) );
painter->setPen( Qt::NoPen );
painter->drawRect( painterRect );
painter->setBrush( Qt::NoBrush );

painter->setPen( QColor( 200, 0, 0, 255 ) );
QTextDocument td;
td.setTextWidth( painterRect.width() );
td.setHtml( QStringLiteral( "<span style=\"color: rgb(200,0,0);\"><b>%1</b><br>%2</span>" ).arg(
tr( "WebKit not available!" ),
tr( "The item cannot be rendered because this QGIS install was built without WebKit support." ) ) );
painter->setClipRect( painterRect );
QAbstractTextDocumentLayout::PaintContext ctx;
td.documentLayout()->draw( painter, ctx );
}
}
#endif
}

double QgsLayoutItemHtml::htmlUnitsToLayoutUnits()
Expand Down
55 changes: 55 additions & 0 deletions src/gui/layout/qgslayoutvaliditychecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "qgslayoutitemscalebar.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitempicture.h"
#ifndef WITH_QTWEBKIT
#include "qgslayoutmultiframe.h"
#endif
#include "qgslayout.h"
#include "qgssettings.h"
#include <QUrl>
Expand Down Expand Up @@ -245,3 +248,55 @@ QList<QgsValidityCheckResult> QgsLayoutPictureSourceValidityCheck::runCheck( con
{
return mResults;
}

#ifndef WITH_QTWEBKIT
//
// QgsLayoutHtmlItemValidityCheck
//

QgsLayoutHtmlItemValidityCheck *QgsLayoutHtmlItemValidityCheck::create() const
{
return new QgsLayoutHtmlItemValidityCheck();
}

QString QgsLayoutHtmlItemValidityCheck::id() const
{
return QStringLiteral( "layout_html_item_check" );
}

int QgsLayoutHtmlItemValidityCheck::checkType() const
{
return static_cast< int >( QgsAbstractValidityCheck::Type::LayoutCheck );
}

bool QgsLayoutHtmlItemValidityCheck::prepareCheck( const QgsValidityCheckContext *context, QgsFeedback * )
{
if ( context->type() != QgsValidityCheckContext::TypeLayoutContext )
return false;

const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
if ( !layoutContext )
return false;

const QList<QgsLayoutMultiFrame *> multiFrames = layoutContext->layout->multiFrames();
for ( QgsLayoutMultiFrame *multiFrame : std::as_const( multiFrames ) )
{
if ( multiFrame->type() == QgsLayoutItemRegistry::LayoutHtml && multiFrame->frameCount() > 0 )
{
QgsValidityCheckResult res;
res.type = QgsValidityCheckResult::Warning;
res.title = QObject::tr( "HTML item cannot be rendered" );
res.detailedDescription = QObject::tr( "HTML items cannot be rendered because this QGIS install was built without WebKit support. These items will be missing from the export." );
mResults.append( res );
break;
}
}

return true;
}

QList<QgsValidityCheckResult> QgsLayoutHtmlItemValidityCheck::runCheck( const QgsValidityCheckContext *, QgsFeedback * )
{
return mResults;
}
#endif
23 changes: 23 additions & 0 deletions src/gui/layout/qgslayoutvaliditychecks.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,27 @@ class GUI_EXPORT QgsLayoutPictureSourceValidityCheck : public QgsAbstractValidit
QList<QgsValidityCheckResult> mResults;
};

#ifndef WITH_QTWEBKIT
/**
* \ingroup gui
* \brief Layout HTML item validity check
*
* \note This class is not a part of public API
* \since QGIS 3.40
*/
class GUI_EXPORT QgsLayoutHtmlItemValidityCheck : public QgsAbstractValidityCheck
{
public:
//! constructor
QgsLayoutHtmlItemValidityCheck *create() const override;
QString id() const override;
int checkType() const override;
bool prepareCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;
QList< QgsValidityCheckResult > runCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;

private:
QList<QgsValidityCheckResult> mResults;
};
#endif

#endif // QGSLAYOUTVALIDITYCHECKS_H