Skip to content
Permalink
Browse files
[Feature]: Allow setting html text directly in html annotation item (…
…e.g. not from file)
  • Loading branch information
mhugent committed Jun 11, 2021
1 parent 25b89e0 commit 6cda8310d10dc7a77cc55843855c3c1e97a1e806
@@ -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->setPlainText( 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->toPlainText() );
}
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="QPlainTextEdit" 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/>

0 comments on commit 6cda831

Please sign in to comment.