Skip to content

Commit

Permalink
[composer] Allow user to set maximum distance for page breaking in ht…
Browse files Browse the repository at this point in the history
…ml items.
  • Loading branch information
nyalldawson committed May 8, 2014
1 parent 7bcba1f commit 0d38387
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 12 deletions.
25 changes: 25 additions & 0 deletions python/core/composer/qgscomposerhtml.sip
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ class QgsComposerHtml: QgsComposerMultiFrame
*/ */
void setUseSmartBreaks( bool useSmartBreaks ); void setUseSmartBreaks( bool useSmartBreaks );


/**Sets the maximum distance allowed when calculating where to place page breaks
* in the html. This distance is the maximum amount of empty space allowed
* at the bottom of a frame after calculating the optimum break location. Setting
* a larger value will result in better choice of page break location, but more
* wasted space at the bottom of frames. This setting is only effective if
* useSmartBreaks is true.
* @param maxBreakDistance maximum amount of empty space to leave when calculating
* page break locations
* @note added in 2.3
* @see maxBreakDistance
* @see setUseSmartBreaks
*/
void setMaxBreakDistance( double maxBreakDistance );

/**Returns the maximum distance allowed when calculating where to place page breaks
* in the html. This distance is the maximum amount of empty space allowed
* at the bottom of a frame after calculating the optimum break location. This setting
* is only effective if useSmartBreaks is true.
* @returns maximum amount of empty space to leave when calculating page break locations
* @note added in 2.3
* @see setMaxBreakDistance
* @see useSmartBreaks
*/
double maxBreakDistance() const;

public slots: public slots:


/**Reloads the html source from the url and redraws the item. /**Reloads the html source from the url and redraws the item.
Expand Down
16 changes: 14 additions & 2 deletions src/app/composer/qgscomposerhtmlwidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void QgsComposerHtmlWidget::blockSignals( bool block )
mFileToolButton->blockSignals( block ); mFileToolButton->blockSignals( block );
mResizeModeComboBox->blockSignals( block ); mResizeModeComboBox->blockSignals( block );
mUseSmartBreaksCheckBox->blockSignals( block ); mUseSmartBreaksCheckBox->blockSignals( block );
mMaxDistanceSpinBox->blockSignals( block );
} }


void QgsComposerHtmlWidget::on_mUrlLineEdit_editingFinished() void QgsComposerHtmlWidget::on_mUrlLineEdit_editingFinished()
Expand Down Expand Up @@ -117,7 +118,7 @@ void QgsComposerHtmlWidget::on_mResizeModeComboBox_currentIndexChanged( int inde
mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames ); mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
} }


void QgsComposerHtmlWidget::on_mUseSmartBreaksCheckBox_stateChanged( int state ) void QgsComposerHtmlWidget::on_mUseSmartBreaksCheckBox_toggled( bool checked )
{ {
if ( !mHtml ) if ( !mHtml )
{ {
Expand All @@ -129,12 +130,22 @@ void QgsComposerHtmlWidget::on_mUseSmartBreaksCheckBox_stateChanged( int state )
{ {
blockSignals( true ); blockSignals( true );
composition->beginMultiFrameCommand( mHtml, tr( "Use smart breaks changed" ) ); composition->beginMultiFrameCommand( mHtml, tr( "Use smart breaks changed" ) );
mHtml->setUseSmartBreaks( state ); mHtml->setUseSmartBreaks( checked );
composition->endMultiFrameCommand(); composition->endMultiFrameCommand();
blockSignals( false ); blockSignals( false );
} }
} }


void QgsComposerHtmlWidget::on_mMaxDistanceSpinBox_valueChanged( double val )
{
if ( !mHtml )
{
return;
}

mHtml->setMaxBreakDistance( val );
}

void QgsComposerHtmlWidget::on_mReloadPushButton_clicked() void QgsComposerHtmlWidget::on_mReloadPushButton_clicked()
{ {
if ( !mHtml ) if ( !mHtml )
Expand Down Expand Up @@ -179,6 +190,7 @@ void QgsComposerHtmlWidget::setGuiElementValues()
mUrlLineEdit->setText( mHtml->url().toString() ); mUrlLineEdit->setText( mHtml->url().toString() );
mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mHtml->resizeMode() ) ); mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mHtml->resizeMode() ) );
mUseSmartBreaksCheckBox->setChecked( mHtml->useSmartBreaks() ); mUseSmartBreaksCheckBox->setChecked( mHtml->useSmartBreaks() );
mMaxDistanceSpinBox->setValue( mHtml->maxBreakDistance() );


mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames ); mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
blockSignals( false ); blockSignals( false );
Expand Down
4 changes: 3 additions & 1 deletion src/app/composer/qgscomposerhtmlwidget.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class QgsComposerHtmlWidget: public QWidget, private Ui::QgsComposerHtmlWidgetBa
void on_mUrlLineEdit_editingFinished(); void on_mUrlLineEdit_editingFinished();
void on_mFileToolButton_clicked(); void on_mFileToolButton_clicked();
void on_mResizeModeComboBox_currentIndexChanged( int index ); void on_mResizeModeComboBox_currentIndexChanged( int index );
void on_mUseSmartBreaksCheckBox_stateChanged( int state ); void on_mUseSmartBreaksCheckBox_toggled( bool checked );
void on_mMaxDistanceSpinBox_valueChanged( double val );

void on_mReloadPushButton_clicked(); void on_mReloadPushButton_clicked();
void on_mAddFramePushButton_clicked(); void on_mAddFramePushButton_clicked();


Expand Down
18 changes: 15 additions & 3 deletions src/core/composer/qgscomposerhtml.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ):
mLoaded( false ), mLoaded( false ),
mHtmlUnitsToMM( 1.0 ), mHtmlUnitsToMM( 1.0 ),
mRenderedPage( 0 ), mRenderedPage( 0 ),
mUseSmartBreaks( true ) mUseSmartBreaks( true ),
mMaxBreakDistance( 10 )
{ {
mHtmlUnitsToMM = htmlUnitsToMM(); mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage(); mWebPage = new QWebPage();
Expand All @@ -48,7 +49,8 @@ QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ),
mLoaded( false ), mLoaded( false ),
mHtmlUnitsToMM( 1.0 ), mHtmlUnitsToMM( 1.0 ),
mRenderedPage( 0 ), mRenderedPage( 0 ),
mUseSmartBreaks( true ) mUseSmartBreaks( true ),
mMaxBreakDistance( 10 )
{ {
} }


Expand Down Expand Up @@ -191,7 +193,7 @@ double QgsComposerHtml::findNearbyPageBreak( double yPos )
return yPos; return yPos;
} }


int maxSearchDistance = 100; int maxSearchDistance = mMaxBreakDistance * htmlUnitsToMM();


//loop through all lines just before ideal break location, up to max distance //loop through all lines just before ideal break location, up to max distance
//of maxSearchDistance //of maxSearchDistance
Expand Down Expand Up @@ -256,13 +258,22 @@ void QgsComposerHtml::setUseSmartBreaks( bool useSmartBreaks )
{ {
mUseSmartBreaks = useSmartBreaks; mUseSmartBreaks = useSmartBreaks;
recalculateFrameSizes(); recalculateFrameSizes();
emit changed();
}

void QgsComposerHtml::setMaxBreakDistance( double maxBreakDistance )
{
mMaxBreakDistance = maxBreakDistance;
recalculateFrameSizes();
emit changed();
} }


bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{ {
QDomElement htmlElem = doc.createElement( "ComposerHtml" ); QDomElement htmlElem = doc.createElement( "ComposerHtml" );
htmlElem.setAttribute( "url", mUrl.toString() ); htmlElem.setAttribute( "url", mUrl.toString() );
htmlElem.setAttribute( "useSmartBreaks", mUseSmartBreaks ? "true" : "false" ); htmlElem.setAttribute( "useSmartBreaks", mUseSmartBreaks ? "true" : "false" );
htmlElem.setAttribute( "maxBreakDistance", QString::number( mMaxBreakDistance ) );


bool state = _writeXML( htmlElem, doc, ignoreFrames ); bool state = _writeXML( htmlElem, doc, ignoreFrames );
elem.appendChild( htmlElem ); elem.appendChild( htmlElem );
Expand All @@ -280,6 +291,7 @@ bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument&
} }


mUseSmartBreaks = itemElem.attribute( "useSmartBreaks", "true" ) == "true" ? true : false; mUseSmartBreaks = itemElem.attribute( "useSmartBreaks", "true" ) == "true" ? true : false;
mMaxBreakDistance = itemElem.attribute( "maxBreakDistance", "10" ).toDouble();


//finally load the set url //finally load the set url
QString urlString = itemElem.attribute( "url" ); QString urlString = itemElem.attribute( "url" );
Expand Down
26 changes: 26 additions & 0 deletions src/core/composer/qgscomposerhtml.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
*/ */
void setUseSmartBreaks( bool useSmartBreaks ); void setUseSmartBreaks( bool useSmartBreaks );


/**Sets the maximum distance allowed when calculating where to place page breaks
* in the html. This distance is the maximum amount of empty space allowed
* at the bottom of a frame after calculating the optimum break location. Setting
* a larger value will result in better choice of page break location, but more
* wasted space at the bottom of frames. This setting is only effective if
* useSmartBreaks is true.
* @param maxBreakDistance maximum amount of empty space to leave when calculating
* page break locations
* @note added in 2.3
* @see maxBreakDistance
* @see setUseSmartBreaks
*/
void setMaxBreakDistance( double maxBreakDistance );

/**Returns the maximum distance allowed when calculating where to place page breaks
* in the html. This distance is the maximum amount of empty space allowed
* at the bottom of a frame after calculating the optimum break location. This setting
* is only effective if useSmartBreaks is true.
* @returns maximum amount of empty space to leave when calculating page break locations
* @note added in 2.3
* @see setMaxBreakDistance
* @see useSmartBreaks
*/
double maxBreakDistance() const { return mMaxBreakDistance; }

public slots: public slots:


/**Reloads the html source from the url and redraws the item. /**Reloads the html source from the url and redraws the item.
Expand All @@ -78,6 +103,7 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
double mHtmlUnitsToMM; double mHtmlUnitsToMM;
QImage* mRenderedPage; QImage* mRenderedPage;
bool mUseSmartBreaks; bool mUseSmartBreaks;
double mMaxBreakDistance;


double htmlUnitsToMM(); //calculate scale factor double htmlUnitsToMM(); //calculate scale factor


Expand Down
46 changes: 40 additions & 6 deletions src/ui/qgscomposerhtmlwidgetbase.ui
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>409</width> <width>409</width>
<height>370</height> <height>383</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
Expand Down Expand Up @@ -47,7 +47,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>407</width> <width>407</width>
<height>347</height> <height>360</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="mainLayout"> <layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -107,17 +107,51 @@
<item row="2" column="1"> <item row="2" column="1">
<widget class="QComboBox" name="mResizeModeComboBox"/> <widget class="QComboBox" name="mResizeModeComboBox"/>
</item> </item>
<item row="4" column="0" colspan="2"> <item row="3" column="0" colspan="2">
<widget class="QPushButton" name="mAddFramePushButton"> <widget class="QPushButton" name="mAddFramePushButton">
<property name="text"> <property name="text">
<string>Add Frame</string> <string>Add Frame</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0" colspan="2"> </layout>
<widget class="QCheckBox" name="mUseSmartBreaksCheckBox"> </widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="mUseSmartBreaksCheckBox">
<property name="title">
<string>Use smart page breaks</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="collapsed" stdset="0">
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="mLabelMaxDistance">
<property name="text"> <property name="text">
<string>Use smart page breaks</string> <string>Maximum distance</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="mMaxDistanceSpinBox">
<property name="suffix">
<string> mm</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="maximum">
<double>1000.000000000000000</double>
</property>
<property name="singleStep">
<double>1.000000000000000</double>
</property> </property>
</widget> </widget>
</item> </item>
Expand Down

0 comments on commit 0d38387

Please sign in to comment.