Skip to content

Commit

Permalink
[Feature]: Allow setting html text directly in html annotation item (…
Browse files Browse the repository at this point in the history
…e.g. not from file)
  • Loading branch information
mhugent committed Jun 11, 2021
1 parent 25b89e0 commit 6cda831
Show file tree
Hide file tree
Showing 6 changed files with 104 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->setPlainText( 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->toPlainText() );
}
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
28 changes: 24 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="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/>
Expand Down

0 comments on commit 6cda831

Please sign in to comment.