Skip to content

Commit

Permalink
[FEATURE][composer] Add button to create new frame when using compose…
Browse files Browse the repository at this point in the history
…r items in Use Existing Frames mode
  • Loading branch information
nyalldawson committed May 7, 2014
1 parent 7414a80 commit 93fa94f
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 30 deletions.
28 changes: 27 additions & 1 deletion python/core/composer/qgscomposermultiframe.sip
Expand Up @@ -22,6 +22,13 @@ class QgsComposerMultiFrame: QObject

virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) = 0;

/**Finds the optimal position to break a frame at.
* @param yPos maximum vertical position for break
* @returns the optimal breakable position which occurs in the multi frame close
* to and before the specified yPos
* @note added in version 2.3*/
virtual double findNearbyPageBreak( double yPos );

void removeFrame( int i );

void update();
Expand Down Expand Up @@ -49,8 +56,27 @@ class QgsComposerMultiFrame: QObject
int frameCount() const;
QgsComposerFrame* frame( int i );

protected slots:
/**Creates a new frame and adds it to the multi frame and composition.
* @param currentFrame an existing QgsComposerFrame from which to copy the size
* and general frame properties (eg frame style, background, rendering settings).
* @param pos position of top-left corner of the new frame
* @param size size of the new frame
* @returns new QgsComposerFrame
* @note added in version 2.3
*/
QgsComposerFrame* createNewFrame(QgsComposerFrame* currentFrame, QPointF pos, QSizeF size);

public slots:

/**Recalculates the portion of the multiframe item which is shown in each of it's
* component frames. If the resize mode is set to anything but UseExistingFrames then
* this may cause new frames to be added or frames to be removed, in order to fit
* the current size of the multiframe's content.
*/
void recalculateFrameSizes();

protected slots:

/**Called before a frame is going to be removed (update frame list)*/
void handleFrameRemoval( QgsComposerItem* item );
/**Adapts to changed number of pages if resize type is RepeatOnEveryPage*/
Expand Down
5 changes: 5 additions & 0 deletions python/core/composer/qgscomposition.sip
Expand Up @@ -413,6 +413,11 @@ class QgsComposition : QGraphicsScene
* to refresh their contents.
@note added in version 2.3*/
void refreshItems();

/**Clears any selected items and sets an item as the current selection.
* @param item item to set as selected
* @note added in version 2.3*/
void setSelectedItem( QgsComposerItem* item );

protected:
void init();
Expand Down
27 changes: 27 additions & 0 deletions src/app/composer/qgscomposerhtmlwidget.cpp
Expand Up @@ -113,6 +113,8 @@ void QgsComposerHtmlWidget::on_mResizeModeComboBox_currentIndexChanged( int inde
mHtml->setResizeMode(( QgsComposerMultiFrame::ResizeMode )mResizeModeComboBox->itemData( index ).toInt() );
composition->endMultiFrameCommand();
}

mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
}

void QgsComposerHtmlWidget::on_mUseSmartBreaksCheckBox_stateChanged( int state )
Expand Down Expand Up @@ -143,6 +145,29 @@ void QgsComposerHtmlWidget::on_mReloadPushButton_clicked()
mHtml->loadHtml();
}

void QgsComposerHtmlWidget::on_mAddFramePushButton_clicked()
{
if ( !mHtml || !mFrame )
{
return;
}

//create a new frame based on the current frame
QPointF pos = mFrame->pos();
//shift new frame so that it sits 10 units below current frame
pos.ry() += mFrame->rect().height() + 10;

QgsComposerFrame * newFrame = mHtml->createNewFrame( mFrame, pos, mFrame->rect().size() );
mHtml->recalculateFrameSizes();

//set new frame as selection
QgsComposition* composition = mHtml->composition();
if ( composition )
{
composition->setSelectedItem( newFrame );
}
}

void QgsComposerHtmlWidget::setGuiElementValues()
{
if ( !mHtml )
Expand All @@ -154,5 +179,7 @@ void QgsComposerHtmlWidget::setGuiElementValues()
mUrlLineEdit->setText( mHtml->url().toString() );
mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mHtml->resizeMode() ) );
mUseSmartBreaksCheckBox->setChecked( mHtml->useSmartBreaks() );

mAddFramePushButton->setEnabled( mHtml->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
blockSignals( false );
}
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerhtmlwidget.h
Expand Up @@ -33,6 +33,7 @@ class QgsComposerHtmlWidget: public QWidget, private Ui::QgsComposerHtmlWidgetBa
void on_mResizeModeComboBox_currentIndexChanged( int index );
void on_mUseSmartBreaksCheckBox_stateChanged( int state );
void on_mReloadPushButton_clicked();
void on_mAddFramePushButton_clicked();

/**Sets the GUI elements to the values of mHtmlItem*/
void setGuiElementValues();
Expand Down
44 changes: 30 additions & 14 deletions src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -137,9 +137,12 @@ void QgsComposerMultiFrame::recalculateFrameSizes()
{
newFrameY += currentItem->pos().y() - ( page - 1 ) * ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
}
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, currentItem->pos().x(),
newFrameY,
currentItem->rect().width(), frameHeight );

//create new frame
QgsComposerFrame* newFrame = createNewFrame( currentItem,
QPointF( currentItem->pos().x(), newFrameY ),
QSizeF( currentItem->rect().width(), frameHeight ) );

if ( mResizeMode == RepeatOnEveryPage )
{
newFrame->setContentSection( QRectF( 0, 0, newFrame->rect().width(), newFrame->rect().height() ) );
Expand All @@ -148,23 +151,36 @@ void QgsComposerMultiFrame::recalculateFrameSizes()
{
newFrame->setContentSection( QRectF( 0, currentY, newFrame->rect().width(), newFrame->rect().height() ) );
}

//copy some settings from the parent frame
newFrame->setBackgroundColor( currentItem->backgroundColor() );
newFrame->setBackgroundEnabled( currentItem->hasBackground() );
newFrame->setBlendMode( currentItem->blendMode() );
newFrame->setFrameEnabled( currentItem->hasFrame() );
newFrame->setFrameJoinStyle( currentItem->frameJoinStyle() );
newFrame->setFrameOutlineWidth( currentItem->frameOutlineWidth() );
newFrame->setOpacity( currentItem->opacity() );

currentY += frameHeight;
currentItem = newFrame;
addFrame( newFrame, false );
}
}
}

QgsComposerFrame* QgsComposerMultiFrame::createNewFrame( QgsComposerFrame* currentFrame, QPointF pos, QSizeF size )
{
if ( !currentFrame )
{
return 0;
}

QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, pos.x(),
pos.y(), size.width(), size.height() );

//copy some settings from the parent frame
newFrame->setBackgroundColor( currentFrame->backgroundColor() );
newFrame->setBackgroundEnabled( currentFrame->hasBackground() );
newFrame->setBlendMode( currentFrame->blendMode() );
newFrame->setFrameEnabled( currentFrame->hasFrame() );
newFrame->setFrameJoinStyle( currentFrame->frameJoinStyle() );
newFrame->setFrameOutlineWidth( currentFrame->frameOutlineWidth() );
newFrame->setOpacity( currentFrame->opacity() );

addFrame( newFrame, false );

return newFrame;
}

void QgsComposerMultiFrame::handleFrameRemoval( QgsComposerItem* item )
{
QgsComposerFrame* frame = dynamic_cast<QgsComposerFrame*>( item );
Expand Down
23 changes: 21 additions & 2 deletions src/core/composer/qgscomposermultiframe.h
Expand Up @@ -18,6 +18,7 @@

#include <QObject>
#include <QSizeF>
#include <QPointF>

class QgsComposerFrame;
class QgsComposerItem;
Expand Down Expand Up @@ -52,7 +53,7 @@ class CORE_EXPORT QgsComposerMultiFrame: public QObject
* @param yPos maximum vertical position for break
* @returns the optimal breakable position which occurs in the multi frame close
* to and before the specified yPos
* Note: added in version 2.3*/
* @note added in version 2.3*/
virtual double findNearbyPageBreak( double yPos ) { return yPos; }

void removeFrame( int i );
Expand Down Expand Up @@ -82,6 +83,25 @@ class CORE_EXPORT QgsComposerMultiFrame: public QObject
int frameCount() const { return mFrameItems.size(); }
QgsComposerFrame* frame( int i ) const;

/**Creates a new frame and adds it to the multi frame and composition.
* @param currentFrame an existing QgsComposerFrame from which to copy the size
* and general frame properties (eg frame style, background, rendering settings).
* @param pos position of top-left corner of the new frame
* @param size size of the new frame
* @returns new QgsComposerFrame
* @note added in version 2.3
*/
QgsComposerFrame* createNewFrame( QgsComposerFrame* currentFrame, QPointF pos, QSizeF size );

public slots:

/**Recalculates the portion of the multiframe item which is shown in each of it's
* component frames. If the resize mode is set to anything but UseExistingFrames then
* this may cause new frames to be added or frames to be removed, in order to fit
* the current size of the multiframe's content.
*/
void recalculateFrameSizes();

protected:
QgsComposition* mComposition;
QList<QgsComposerFrame*> mFrameItems;
Expand All @@ -90,7 +110,6 @@ class CORE_EXPORT QgsComposerMultiFrame: public QObject
bool mCreateUndoCommands;

protected slots:
void recalculateFrameSizes();
/**Called before a frame is going to be removed (update frame list)*/
void handleFrameRemoval( QgsComposerItem* item );
/**Adapts to changed number of pages if resize type is RepeatOnEveryPage*/
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -186,6 +186,13 @@ void QgsComposition::refreshItems()
emit refreshItemsTriggered();
}

void QgsComposition::setSelectedItem( QgsComposerItem *item )
{
clearSelection();
item->setSelected( true );
emit selectedItemChanged( item );
}

QRectF QgsComposition::compositionBounds() const
{
//start with an empty rectangle
Expand Down
5 changes: 5 additions & 0 deletions src/core/composer/qgscomposition.h
Expand Up @@ -470,6 +470,11 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
@note added in version 2.3*/
void refreshItems();

/**Clears any selected items and sets an item as the current selection.
* @param item item to set as selected
* @note added in version 2.3*/
void setSelectedItem( QgsComposerItem* item );

protected:
void init();

Expand Down
27 changes: 14 additions & 13 deletions src/ui/qgscomposerhtmlwidgetbase.ui
Expand Up @@ -63,12 +63,6 @@
<bool>false</bool>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="mUrlLabel">
<property name="text">
Expand All @@ -93,7 +87,14 @@
</item>
</layout>
</item>
<item row="3" column="0">
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="mReloadPushButton">
<property name="text">
<string>Refresh HTML</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mResizeModeLabel">
<property name="text">
<string>Resize mode</string>
Expand All @@ -103,20 +104,20 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QComboBox" name="mResizeModeComboBox"/>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="mUseSmartBreaksCheckBox">
<widget class="QPushButton" name="mAddFramePushButton">
<property name="text">
<string>Use smart page breaks</string>
<string>Add Frame</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="mReloadPushButton">
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="mUseSmartBreaksCheckBox">
<property name="text">
<string>Refresh HTML</string>
<string>Use smart page breaks</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 93fa94f

Please sign in to comment.