Skip to content

Commit 256999d

Browse files
committed
[FEATURE][composer] Option for manual entry of HTML item source. Sponsored by City of Uster, Switzerland.
1 parent e2d57d9 commit 256999d

8 files changed

Lines changed: 267 additions & 31 deletions

File tree

python/core/composer/qgscomposerhtml.sip

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,75 @@ class QgsComposerHtml: QgsComposerMultiFrame
55
%End
66

77
public:
8+
9+
/** Source modes for the HTML content to render in the item
10+
*/
11+
enum ContentMode
12+
{
13+
Url, /*< Using this mode item fetches its content via a url*/
14+
ManualHtml /*< HTML content is manually set for the item*/
15+
};
16+
817
QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
918
QgsComposerHtml();
1019
~QgsComposerHtml();
1120

21+
/**Sets the source mode for item's HTML content.
22+
* @param mode ContentMode for the item's source
23+
* @see contentMode
24+
* @see setUrl
25+
* @see setHtml
26+
* @note added in 2.5
27+
*/
28+
void setContentMode( ContentMode mode );
29+
30+
/**Returns the source mode for item's HTML content.
31+
* @returns ContentMode for the item's source
32+
* @see setContentMode
33+
* @see url
34+
* @see html
35+
* @note added in 2.5
36+
*/
37+
ContentMode contentMode() const;
38+
39+
/**Sets the URL for content to display in the item when the item is using
40+
* the QgsComposerHtml::Url mode. Content is automatically fetched and the
41+
* HTML item refreshed after calling this function.
42+
* @param url URL of content to display in the item
43+
* @see url
44+
* @see contentMode
45+
*/
1246
void setUrl( const QUrl& url );
47+
48+
/**Returns the URL of the content displayed in the item if the item is using
49+
* the QgsComposerHtml::Url mode.
50+
* @returns url for content displayed in item
51+
* @see setUrl
52+
* @see contentMode
53+
*/
1354
const QUrl& url() const;
1455

56+
/**Sets the HTML to display in the item when the item is using
57+
* the QgsComposerHtml::ManualHtml mode. Setting the HTML using this function
58+
* does not automatically refresh the item's contents. Call loadHtml to trigger
59+
* a refresh of the item after setting the HTML content.
60+
* @param html HTML to display in item
61+
* @see html
62+
* @see contentMode
63+
* @see loadHtml
64+
* @note added in 2.5
65+
*/
66+
void setHtml( const QString html );
67+
68+
/**Returns the HTML source displayed in the item if the item is using
69+
* the QgsComposerHtml::ManualHtml mode.
70+
* @returns HTML displayed in item
71+
* @see setHtml
72+
* @see contentMode
73+
* @note added in 2.5
74+
*/
75+
QString html() const;
76+
1577
QSizeF totalSize() const;
1678
void render( QPainter* p, const QRectF& renderExtent );
1779

src/app/composer/qgscomposerhtmlwidget.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ void QgsComposerHtmlWidget::blockSignals( bool block )
6262
mResizeModeComboBox->blockSignals( block );
6363
mUseSmartBreaksCheckBox->blockSignals( block );
6464
mMaxDistanceSpinBox->blockSignals( block );
65+
mHtmlTextEdit->blockSignals( block );
66+
mRadioManualSource->blockSignals( block );
67+
mRadioUrlSource->blockSignals( block );
6568
}
6669

6770
void QgsComposerHtmlWidget::on_mUrlLineEdit_editingFinished()
@@ -146,6 +149,46 @@ void QgsComposerHtmlWidget::on_mMaxDistanceSpinBox_valueChanged( double val )
146149
mHtml->setMaxBreakDistance( val );
147150
}
148151

152+
void QgsComposerHtmlWidget::on_mHtmlTextEdit_textChanged()
153+
{
154+
if ( !mHtml )
155+
{
156+
return;
157+
}
158+
159+
mHtml->setHtml( mHtmlTextEdit->toPlainText() );
160+
}
161+
162+
void QgsComposerHtmlWidget::on_mRadioManualSource_clicked( bool checked )
163+
{
164+
if ( !mHtml )
165+
{
166+
return;
167+
}
168+
169+
mHtml->setContentMode( checked ? QgsComposerHtml::ManualHtml : QgsComposerHtml::Url );
170+
mHtmlTextEdit->setEnabled( checked );
171+
mUrlLineEdit->setEnabled( !checked );
172+
mFileToolButton->setEnabled( !checked );
173+
174+
mHtml->loadHtml();
175+
}
176+
177+
void QgsComposerHtmlWidget::on_mRadioUrlSource_clicked( bool checked )
178+
{
179+
if ( !mHtml )
180+
{
181+
return;
182+
}
183+
184+
mHtml->setContentMode( checked ? QgsComposerHtml::Url : QgsComposerHtml::ManualHtml );
185+
mHtmlTextEdit->setEnabled( !checked );
186+
mUrlLineEdit->setEnabled( checked );
187+
mFileToolButton->setEnabled( checked );
188+
189+
mHtml->loadHtml();
190+
}
191+
149192
void QgsComposerHtmlWidget::on_mReloadPushButton_clicked()
150193
{
151194
if ( !mHtml )
@@ -193,5 +236,12 @@ void QgsComposerHtmlWidget::setGuiElementValues()
193236
mMaxDistanceSpinBox->setValue( mHtml->maxBreakDistance() );
194237

195238
mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
239+
mHtmlTextEdit->setPlainText( mHtml->html() );
240+
241+
mRadioUrlSource->setChecked( mHtml->contentMode() == QgsComposerHtml::Url );
242+
mUrlLineEdit->setEnabled( mHtml->contentMode() == QgsComposerHtml::Url );
243+
mFileToolButton->setEnabled( mHtml->contentMode() == QgsComposerHtml::Url );
244+
mRadioManualSource->setChecked( mHtml->contentMode() == QgsComposerHtml::ManualHtml );
245+
mHtmlTextEdit->setEnabled( mHtml->contentMode() == QgsComposerHtml::ManualHtml );
196246
blockSignals( false );
197247
}

src/app/composer/qgscomposerhtmlwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class QgsComposerHtmlWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
3434
void on_mResizeModeComboBox_currentIndexChanged( int index );
3535
void on_mUseSmartBreaksCheckBox_toggled( bool checked );
3636
void on_mMaxDistanceSpinBox_valueChanged( double val );
37+
void on_mHtmlTextEdit_textChanged();
38+
void on_mRadioManualSource_clicked( bool checked );
39+
void on_mRadioUrlSource_clicked( bool checked );
3740

3841
void on_mReloadPushButton_clicked();
3942
void on_mAddFramePushButton_clicked();

src/core/composer/qgscomposerhtml.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QImage>
2727

2828
QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
29+
mContentMode( QgsComposerHtml::Url ),
2930
mWebPage( 0 ),
3031
mLoaded( false ),
3132
mHtmlUnitsToMM( 1.0 ),
@@ -45,6 +46,7 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ):
4546
}
4647

4748
QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ),
49+
mContentMode( QgsComposerHtml::Url ),
4850
mWebPage( 0 ),
4951
mLoaded( false ),
5052
mHtmlUnitsToMM( 1.0 ),
@@ -71,15 +73,31 @@ void QgsComposerHtml::setUrl( const QUrl& url )
7173
loadHtml();
7274
}
7375

76+
void QgsComposerHtml::setHtml( const QString html )
77+
{
78+
mHtml = html;
79+
}
80+
7481
void QgsComposerHtml::loadHtml()
7582
{
76-
if ( !mWebPage || mUrl.isEmpty() )
83+
if ( !mWebPage || ( mContentMode == QgsComposerHtml::Url && mUrl.isEmpty() ) )
7784
{
7885
return;
7986
}
8087

8188
mLoaded = false;
82-
mWebPage->mainFrame()->load( mUrl );
89+
//set contents
90+
switch ( mContentMode )
91+
{
92+
case QgsComposerHtml::Url:
93+
mWebPage->mainFrame()->load( mUrl );
94+
break;
95+
case QgsComposerHtml::ManualHtml:
96+
mWebPage->mainFrame()->setHtml( mHtml );
97+
break;
98+
}
99+
100+
//pause until HTML loaded
83101
while ( !mLoaded )
84102
{
85103
qApp->processEvents();
@@ -282,7 +300,9 @@ void QgsComposerHtml::setMaxBreakDistance( double maxBreakDistance )
282300
bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
283301
{
284302
QDomElement htmlElem = doc.createElement( "ComposerHtml" );
303+
htmlElem.setAttribute( "contentMode", QString::number(( int ) mContentMode ) );
285304
htmlElem.setAttribute( "url", mUrl.toString() );
305+
htmlElem.setAttribute( "html", mHtml );
286306
htmlElem.setAttribute( "useSmartBreaks", mUseSmartBreaks ? "true" : "false" );
287307
htmlElem.setAttribute( "maxBreakDistance", QString::number( mMaxBreakDistance ) );
288308

@@ -301,9 +321,15 @@ bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument&
301321
return false;
302322
}
303323

324+
bool contentModeOK;
325+
mContentMode = ( QgsComposerHtml::ContentMode )itemElem.attribute( "contentMode" ).toInt( &contentModeOK );
326+
if ( !contentModeOK )
327+
{
328+
mContentMode = QgsComposerHtml::Url;
329+
}
304330
mUseSmartBreaks = itemElem.attribute( "useSmartBreaks", "true" ) == "true" ? true : false;
305331
mMaxBreakDistance = itemElem.attribute( "maxBreakDistance", "10" ).toDouble();
306-
332+
mHtml = itemElem.attribute( "html" );
307333
//finally load the set url
308334
QString urlString = itemElem.attribute( "url" );
309335
if ( !urlString.isEmpty() )

src/core/composer/qgscomposerhtml.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,75 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
2626
{
2727
Q_OBJECT
2828
public:
29+
30+
/** Source modes for the HTML content to render in the item
31+
*/
32+
enum ContentMode
33+
{
34+
Url, /*< Using this mode item fetches its content via a url*/
35+
ManualHtml /*< HTML content is manually set for the item*/
36+
};
37+
2938
QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
3039
QgsComposerHtml();
3140
~QgsComposerHtml();
3241

42+
/**Sets the source mode for item's HTML content.
43+
* @param mode ContentMode for the item's source
44+
* @see contentMode
45+
* @see setUrl
46+
* @see setHtml
47+
* @note added in 2.5
48+
*/
49+
void setContentMode( ContentMode mode ) { mContentMode = mode; }
50+
51+
/**Returns the source mode for item's HTML content.
52+
* @returns ContentMode for the item's source
53+
* @see setContentMode
54+
* @see url
55+
* @see html
56+
* @note added in 2.5
57+
*/
58+
ContentMode contentMode() const { return mContentMode; }
59+
60+
/**Sets the URL for content to display in the item when the item is using
61+
* the QgsComposerHtml::Url mode. Content is automatically fetched and the
62+
* HTML item refreshed after calling this function.
63+
* @param url URL of content to display in the item
64+
* @see url
65+
* @see contentMode
66+
*/
3367
void setUrl( const QUrl& url );
68+
69+
/**Returns the URL of the content displayed in the item if the item is using
70+
* the QgsComposerHtml::Url mode.
71+
* @returns url for content displayed in item
72+
* @see setUrl
73+
* @see contentMode
74+
*/
3475
const QUrl& url() const { return mUrl; }
3576

77+
/**Sets the HTML to display in the item when the item is using
78+
* the QgsComposerHtml::ManualHtml mode. Setting the HTML using this function
79+
* does not automatically refresh the item's contents. Call loadHtml to trigger
80+
* a refresh of the item after setting the HTML content.
81+
* @param html HTML to display in item
82+
* @see html
83+
* @see contentMode
84+
* @see loadHtml
85+
* @note added in 2.5
86+
*/
87+
void setHtml( const QString html );
88+
89+
/**Returns the HTML source displayed in the item if the item is using
90+
* the QgsComposerHtml::ManualHtml mode.
91+
* @returns HTML displayed in item
92+
* @see setHtml
93+
* @see contentMode
94+
* @note added in 2.5
95+
*/
96+
QString html() const { return mHtml; }
97+
3698
QSizeF totalSize() const;
3799
void render( QPainter* p, const QRectF& renderExtent );
38100

@@ -96,8 +158,10 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
96158
void frameLoaded( bool ok );
97159

98160
private:
161+
ContentMode mContentMode;
99162
QUrl mUrl;
100163
QWebPage* mWebPage;
164+
QString mHtml;
101165
bool mLoaded;
102166
QSizeF mSize; //total size in mm
103167
double mHtmlUnitsToMM;

0 commit comments

Comments
 (0)