Skip to content

Commit

Permalink
Merge pull request #43838 from mhugent/annotation_inline_html
Browse files Browse the repository at this point in the history
Feature: Annotation item inline html
  • Loading branch information
mhugent committed Jul 8, 2021
2 parents 8a4683f + 38fa7ee commit 011478e
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
12 changes: 12 additions & 0 deletions python/core/auto_generated/annotations/qgshtmlannotation.sip.in
Expand Up @@ -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;
Expand Down
32 changes: 30 additions & 2 deletions src/app/qgshtmlannotationdialog.cpp
Expand Up @@ -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 );
Expand All @@ -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();
}
}
Expand All @@ -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() )
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgshtmlannotationdialog.h
Expand Up @@ -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();
};

Expand Down
28 changes: 25 additions & 3 deletions src/core/annotations/qgshtmlannotation.cpp
Expand Up @@ -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() )
Expand Down Expand Up @@ -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() )
{
Expand All @@ -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() ) );
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/core/annotations/qgshtmlannotation.h
Expand Up @@ -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;

Expand Down
35 changes: 31 additions & 4 deletions src/ui/qgsformannotationdialogbase.ui
Expand Up @@ -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">
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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/>
Expand Down Expand Up @@ -81,4 +101,11 @@
</hints>
</connection>
</connections>
<customwidgets>
<customwidget>
<class>QgsCodeEditorHTML</class>
<extends>QWidget</extends>
<header>qgscodeeditorhtml.h</header>
</customwidget>
</customwidgets>
</ui>
13 changes: 13 additions & 0 deletions tests/src/python/test_qgsannotation.py
Expand Up @@ -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()
Expand Down

0 comments on commit 011478e

Please sign in to comment.