Skip to content

Commit

Permalink
[FEATURE][composer] Option for manual entry of HTML item source. Spon…
Browse files Browse the repository at this point in the history
…sored by City of Uster, Switzerland.
  • Loading branch information
nyalldawson committed Jul 16, 2014
1 parent e2d57d9 commit 256999d
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 31 deletions.
62 changes: 62 additions & 0 deletions python/core/composer/qgscomposerhtml.sip
Expand Up @@ -5,13 +5,75 @@ class QgsComposerHtml: QgsComposerMultiFrame
%End

public:

/** Source modes for the HTML content to render in the item
*/
enum ContentMode
{
Url, /*< Using this mode item fetches its content via a url*/
ManualHtml /*< HTML content is manually set for the item*/
};

QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
QgsComposerHtml();
~QgsComposerHtml();

/**Sets the source mode for item's HTML content.
* @param mode ContentMode for the item's source
* @see contentMode
* @see setUrl
* @see setHtml
* @note added in 2.5
*/
void setContentMode( ContentMode mode );

/**Returns the source mode for item's HTML content.
* @returns ContentMode for the item's source
* @see setContentMode
* @see url
* @see html
* @note added in 2.5
*/
ContentMode contentMode() const;

/**Sets the URL for content to display in the item when the item is using
* the QgsComposerHtml::Url mode. Content is automatically fetched and the
* HTML item refreshed after calling this function.
* @param url URL of content to display in the item
* @see url
* @see contentMode
*/
void setUrl( const QUrl& url );

/**Returns the URL of the content displayed in the item if the item is using
* the QgsComposerHtml::Url mode.
* @returns url for content displayed in item
* @see setUrl
* @see contentMode
*/
const QUrl& url() const;

/**Sets the HTML to display in the item when the item is using
* the QgsComposerHtml::ManualHtml mode. Setting the HTML using this function
* does not automatically refresh the item's contents. Call loadHtml to trigger
* a refresh of the item after setting the HTML content.
* @param html HTML to display in item
* @see html
* @see contentMode
* @see loadHtml
* @note added in 2.5
*/
void setHtml( const QString html );

/**Returns the HTML source displayed in the item if the item is using
* the QgsComposerHtml::ManualHtml mode.
* @returns HTML displayed in item
* @see setHtml
* @see contentMode
* @note added in 2.5
*/
QString html() const;

QSizeF totalSize() const;
void render( QPainter* p, const QRectF& renderExtent );

Expand Down
50 changes: 50 additions & 0 deletions src/app/composer/qgscomposerhtmlwidget.cpp
Expand Up @@ -62,6 +62,9 @@ void QgsComposerHtmlWidget::blockSignals( bool block )
mResizeModeComboBox->blockSignals( block );
mUseSmartBreaksCheckBox->blockSignals( block );
mMaxDistanceSpinBox->blockSignals( block );
mHtmlTextEdit->blockSignals( block );
mRadioManualSource->blockSignals( block );
mRadioUrlSource->blockSignals( block );
}

void QgsComposerHtmlWidget::on_mUrlLineEdit_editingFinished()
Expand Down Expand Up @@ -146,6 +149,46 @@ void QgsComposerHtmlWidget::on_mMaxDistanceSpinBox_valueChanged( double val )
mHtml->setMaxBreakDistance( val );
}

void QgsComposerHtmlWidget::on_mHtmlTextEdit_textChanged()
{
if ( !mHtml )
{
return;
}

mHtml->setHtml( mHtmlTextEdit->toPlainText() );
}

void QgsComposerHtmlWidget::on_mRadioManualSource_clicked( bool checked )
{
if ( !mHtml )
{
return;
}

mHtml->setContentMode( checked ? QgsComposerHtml::ManualHtml : QgsComposerHtml::Url );
mHtmlTextEdit->setEnabled( checked );
mUrlLineEdit->setEnabled( !checked );
mFileToolButton->setEnabled( !checked );

mHtml->loadHtml();
}

void QgsComposerHtmlWidget::on_mRadioUrlSource_clicked( bool checked )
{
if ( !mHtml )
{
return;
}

mHtml->setContentMode( checked ? QgsComposerHtml::Url : QgsComposerHtml::ManualHtml );
mHtmlTextEdit->setEnabled( !checked );
mUrlLineEdit->setEnabled( checked );
mFileToolButton->setEnabled( checked );

mHtml->loadHtml();
}

void QgsComposerHtmlWidget::on_mReloadPushButton_clicked()
{
if ( !mHtml )
Expand Down Expand Up @@ -193,5 +236,12 @@ void QgsComposerHtmlWidget::setGuiElementValues()
mMaxDistanceSpinBox->setValue( mHtml->maxBreakDistance() );

mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
mHtmlTextEdit->setPlainText( mHtml->html() );

mRadioUrlSource->setChecked( mHtml->contentMode() == QgsComposerHtml::Url );
mUrlLineEdit->setEnabled( mHtml->contentMode() == QgsComposerHtml::Url );
mFileToolButton->setEnabled( mHtml->contentMode() == QgsComposerHtml::Url );
mRadioManualSource->setChecked( mHtml->contentMode() == QgsComposerHtml::ManualHtml );
mHtmlTextEdit->setEnabled( mHtml->contentMode() == QgsComposerHtml::ManualHtml );
blockSignals( false );
}
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposerhtmlwidget.h
Expand Up @@ -34,6 +34,9 @@ class QgsComposerHtmlWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
void on_mResizeModeComboBox_currentIndexChanged( int index );
void on_mUseSmartBreaksCheckBox_toggled( bool checked );
void on_mMaxDistanceSpinBox_valueChanged( double val );
void on_mHtmlTextEdit_textChanged();
void on_mRadioManualSource_clicked( bool checked );
void on_mRadioUrlSource_clicked( bool checked );

void on_mReloadPushButton_clicked();
void on_mAddFramePushButton_clicked();
Expand Down
32 changes: 29 additions & 3 deletions src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QImage>

QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
mContentMode( QgsComposerHtml::Url ),
mWebPage( 0 ),
mLoaded( false ),
mHtmlUnitsToMM( 1.0 ),
Expand All @@ -45,6 +46,7 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ):
}

QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ),
mContentMode( QgsComposerHtml::Url ),
mWebPage( 0 ),
mLoaded( false ),
mHtmlUnitsToMM( 1.0 ),
Expand All @@ -71,15 +73,31 @@ void QgsComposerHtml::setUrl( const QUrl& url )
loadHtml();
}

void QgsComposerHtml::setHtml( const QString html )
{
mHtml = html;
}

void QgsComposerHtml::loadHtml()
{
if ( !mWebPage || mUrl.isEmpty() )
if ( !mWebPage || ( mContentMode == QgsComposerHtml::Url && mUrl.isEmpty() ) )
{
return;
}

mLoaded = false;
mWebPage->mainFrame()->load( mUrl );
//set contents
switch ( mContentMode )
{
case QgsComposerHtml::Url:
mWebPage->mainFrame()->load( mUrl );
break;
case QgsComposerHtml::ManualHtml:
mWebPage->mainFrame()->setHtml( mHtml );
break;
}

//pause until HTML loaded
while ( !mLoaded )
{
qApp->processEvents();
Expand Down Expand Up @@ -282,7 +300,9 @@ void QgsComposerHtml::setMaxBreakDistance( double maxBreakDistance )
bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{
QDomElement htmlElem = doc.createElement( "ComposerHtml" );
htmlElem.setAttribute( "contentMode", QString::number(( int ) mContentMode ) );
htmlElem.setAttribute( "url", mUrl.toString() );
htmlElem.setAttribute( "html", mHtml );
htmlElem.setAttribute( "useSmartBreaks", mUseSmartBreaks ? "true" : "false" );
htmlElem.setAttribute( "maxBreakDistance", QString::number( mMaxBreakDistance ) );

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

bool contentModeOK;
mContentMode = ( QgsComposerHtml::ContentMode )itemElem.attribute( "contentMode" ).toInt( &contentModeOK );
if ( !contentModeOK )
{
mContentMode = QgsComposerHtml::Url;
}
mUseSmartBreaks = itemElem.attribute( "useSmartBreaks", "true" ) == "true" ? true : false;
mMaxBreakDistance = itemElem.attribute( "maxBreakDistance", "10" ).toDouble();

mHtml = itemElem.attribute( "html" );
//finally load the set url
QString urlString = itemElem.attribute( "url" );
if ( !urlString.isEmpty() )
Expand Down
64 changes: 64 additions & 0 deletions src/core/composer/qgscomposerhtml.h
Expand Up @@ -26,13 +26,75 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
{
Q_OBJECT
public:

/** Source modes for the HTML content to render in the item
*/
enum ContentMode
{
Url, /*< Using this mode item fetches its content via a url*/
ManualHtml /*< HTML content is manually set for the item*/
};

QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
QgsComposerHtml();
~QgsComposerHtml();

/**Sets the source mode for item's HTML content.
* @param mode ContentMode for the item's source
* @see contentMode
* @see setUrl
* @see setHtml
* @note added in 2.5
*/
void setContentMode( ContentMode mode ) { mContentMode = mode; }

/**Returns the source mode for item's HTML content.
* @returns ContentMode for the item's source
* @see setContentMode
* @see url
* @see html
* @note added in 2.5
*/
ContentMode contentMode() const { return mContentMode; }

/**Sets the URL for content to display in the item when the item is using
* the QgsComposerHtml::Url mode. Content is automatically fetched and the
* HTML item refreshed after calling this function.
* @param url URL of content to display in the item
* @see url
* @see contentMode
*/
void setUrl( const QUrl& url );

/**Returns the URL of the content displayed in the item if the item is using
* the QgsComposerHtml::Url mode.
* @returns url for content displayed in item
* @see setUrl
* @see contentMode
*/
const QUrl& url() const { return mUrl; }

/**Sets the HTML to display in the item when the item is using
* the QgsComposerHtml::ManualHtml mode. Setting the HTML using this function
* does not automatically refresh the item's contents. Call loadHtml to trigger
* a refresh of the item after setting the HTML content.
* @param html HTML to display in item
* @see html
* @see contentMode
* @see loadHtml
* @note added in 2.5
*/
void setHtml( const QString html );

/**Returns the HTML source displayed in the item if the item is using
* the QgsComposerHtml::ManualHtml mode.
* @returns HTML displayed in item
* @see setHtml
* @see contentMode
* @note added in 2.5
*/
QString html() const { return mHtml; }

QSizeF totalSize() const;
void render( QPainter* p, const QRectF& renderExtent );

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

private:
ContentMode mContentMode;
QUrl mUrl;
QWebPage* mWebPage;
QString mHtml;
bool mLoaded;
QSizeF mSize; //total size in mm
double mHtmlUnitsToMM;
Expand Down

0 comments on commit 256999d

Please sign in to comment.