Skip to content
Permalink
Browse files
Merge pull request #43838 from mhugent/annotation_inline_html
Feature: Annotation item inline html
  • Loading branch information
mhugent committed Jul 8, 2021
2 parents 8a4683f + 38fa7ee commit 011478ea06f890a1ac42c08d96908e87d254bd50
@@ -48,6 +48,18 @@ Sets the file path for the source HTML file.
Returns the file path for the source HTML file.

.. seealso:: :py:func:`setSourceFile`
%End

void setHtmlSource( const QString &htmlSource );
%Docstring
Sets the html source directly (not coming from a file)

:param htmlSource:
%End

QString htmlSource() const;
%Docstring
Returns html source text
%End

virtual void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
@@ -42,7 +42,17 @@ QgsHtmlAnnotationDialog::QgsHtmlAnnotationDialog( QgsMapCanvasAnnotationItem *it
if ( item && item->annotation() )
{
QgsHtmlAnnotation *annotation = static_cast< QgsHtmlAnnotation * >( item->annotation() );
mFileLineEdit->setText( annotation->sourceFile() );
QString file = annotation->sourceFile();
if ( !file.isEmpty() )
{
mFileLineEdit->setText( file );
mFileRadioButton->setChecked( true );
}
else
{
mHtmlSourceTextEdit->setText( annotation->htmlSource() );
mSourceRadioButton->setChecked( true );
}
}

QObject::connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsHtmlAnnotationDialog::applySettingsToItem );
@@ -65,7 +75,15 @@ void QgsHtmlAnnotationDialog::applySettingsToItem()
if ( mItem && mItem->annotation() )
{
QgsHtmlAnnotation *annotation = static_cast< QgsHtmlAnnotation * >( mItem->annotation() );
annotation->setSourceFile( mFileLineEdit->text() );
QString file = mFileLineEdit->text();
if ( mFileRadioButton->isChecked() )
{
annotation->setSourceFile( mFileLineEdit->text() );
}
else
{
annotation->setHtmlSource( mHtmlSourceTextEdit->text() );
}
mItem->update();
}
}
@@ -86,6 +104,16 @@ void QgsHtmlAnnotationDialog::mBrowseToolButton_clicked()
mFileLineEdit->setText( filename );
}

void QgsHtmlAnnotationDialog::on_mFileRadioButton_toggled( bool checked )
{
mFileLineEdit->setEnabled( checked );
}

void QgsHtmlAnnotationDialog::on_mSourceRadioButton_toggled( bool checked )
{
mHtmlSourceTextEdit->setEnabled( checked );
}

void QgsHtmlAnnotationDialog::deleteItem()
{
if ( mItem && mItem->annotation() )
@@ -36,6 +36,8 @@ class APP_EXPORT QgsHtmlAnnotationDialog: public QDialog, private Ui::QgsFormAnn
void mBrowseToolButton_clicked();
void deleteItem();
void mButtonBox_clicked( QAbstractButton *button );
void on_mFileRadioButton_toggled( bool checked );
void on_mSourceRadioButton_toggled( bool checked );
void showHelp();
};

@@ -78,6 +78,14 @@ void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
emit appearanceChanged();
}

void QgsHtmlAnnotation::setHtmlSource( const QString &htmlSource )
{
mHtmlFile.clear();
mHtmlSource = htmlSource;
setAssociatedFeature( associatedFeature() );
emit appearanceChanged();
}

void QgsHtmlAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
{
if ( !context.painter() )
@@ -112,15 +120,21 @@ QSizeF QgsHtmlAnnotation::minimumFrameSize() const
void QgsHtmlAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
{
QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "HtmlAnnotationItem" ) );
formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
if ( !mHtmlFile.isEmpty() )
{
formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
}
else
{
formAnnotationElem.setAttribute( QStringLiteral( "htmlsource" ), mHtmlSource );
}

_writeXml( formAnnotationElem, doc, context );
elem.appendChild( formAnnotationElem );
}

void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
{
mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
if ( !annotationElem.isNull() )
{
@@ -135,7 +149,15 @@ void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWrite

if ( mWebPage )
{
setSourceFile( mHtmlFile );
mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
if ( !mHtmlFile.isEmpty() )
{
setSourceFile( mHtmlFile );
}
else
{
setHtmlSource( itemElem.attribute( QStringLiteral( "htmlsource" ), QString() ) );
}
}
}

@@ -58,6 +58,17 @@ class CORE_EXPORT QgsHtmlAnnotation: public QgsAnnotation
*/
QString sourceFile() const { return mHtmlFile; }

/**
* Sets the html source directly (not coming from a file)
* \param htmlSource
*/
void setHtmlSource( const QString &htmlSource );

/**
* Returns html source text
*/
QString htmlSource() const { return mHtmlSource; }

void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
void readXml( const QDomElement &itemElem, const QgsReadWriteContext &context ) override;

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>298</width>
<height>134</height>
<width>433</width>
<height>246</height>
</rect>
</property>
<property name="windowTitle">
@@ -16,6 +16,16 @@
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="mFileRadioButton">
<property name="text">
<string>File</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mFileLineEdit"/>
</item>
@@ -28,13 +38,13 @@
</item>
</layout>
</item>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QStackedWidget" name="mStackedWidget">
<widget class="QWidget" name="page"/>
<widget class="QWidget" name="page_2"/>
</widget>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QDialogButtonBox" name="mButtonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -44,6 +54,16 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QgsCodeEditorHTML" name="mHtmlSourceTextEdit"/>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="mSourceRadioButton">
<property name="text">
<string>Source</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
@@ -81,4 +101,11 @@
</hints>
</connection>
</connections>
<customwidgets>
<customwidget>
<class>QgsCodeEditorHTML</class>
<extends>QWidget</extends>
<header>qgscodeeditorhtml.h</header>
</customwidget>
</customwidgets>
</ui>
@@ -135,6 +135,19 @@ def testHtmlAnnotation(self):
im = self.renderAnnotation(clone, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))

def testHtmlAnnotationSetHtmlSource(self):
""" test rendering html annotation where the html is set directly (not from file)"""
a = QgsHtmlAnnotation()
a.fillSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
a.markerSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
a.setFrameSizeMm(QSizeF(400 / 3.7795275, 250 / 3.7795275))
a.setFrameOffsetFromReferencePointMm(QPointF(70 / 3.7795275, 90 / 3.7795275))
htmlFile = open(TEST_DATA_DIR + "/test_html.html")
htmlText = htmlFile.read()
a.setHtmlSource(htmlText)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_annotation_html_source', 'html_annotation', im))

def testHtmlAnnotationInLayout(self):
""" test rendering a svg annotation"""
a = QgsHtmlAnnotation()

0 comments on commit 011478e

Please sign in to comment.