Skip to content

Commit

Permalink
[composer] Composer item controls in GUI show position relative to pa…
Browse files Browse the repository at this point in the history
…ge origin (also save page number and relative position to xml) (fix #9411)
  • Loading branch information
manisandro authored and nyalldawson committed Jun 1, 2014
1 parent c0e9440 commit b7423cf
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 44 deletions.
9 changes: 9 additions & 0 deletions python/core/composer/qgscomposeritem.sip
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ class QgsComposerItem : QObject, QGraphicsRectItem
@param y y-position of mouse cursor (in item coordinates)*/
virtual void zoomContent( int delta, double x, double y );

/** gets the page the item is currently on */
int page() const;

/** Returns the position relative to the current page */
QPointF pagePos() const;

/** Updates the page relative position for the new paper size */
void updatePagePos( double newPageWidth, double newPageHeight );

/**Moves the item to a new position (in canvas coordinates)*/
void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );

Expand Down
44 changes: 25 additions & 19 deletions src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgscomposeritemwidget.h"
#include "qgscomposeritem.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgspoint.h"
#include <QColorDialog>
#include <QPen>
Expand Down Expand Up @@ -46,6 +47,7 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
mHeightLineEdit->setValidator( new QDoubleValidator( 0 ) );

setValuesForGuiElements();
connect( mItem->composition(), SIGNAL( paperSizeChanged() ), this, SLOT( setValuesForGuiPositionElements() ) );
connect( mItem, SIGNAL( sizeChanged() ), this, SLOT( setValuesForGuiPositionElements() ) );
connect( mItem, SIGNAL( itemChanged() ), this, SLOT( setValuesForGuiNonPositionElements() ) );

Expand Down Expand Up @@ -142,7 +144,7 @@ void QgsComposerItemWidget::changeItemPosition()
return;
}

mItem->setItemPosition( x, y, width, height, positionMode() );
mItem->setItemPosition( x, y, width, height, positionMode(), false, mPageSpinBox->value() );

mItem->update();
mItem->endCommand();
Expand Down Expand Up @@ -270,73 +272,76 @@ void QgsComposerItemWidget::setValuesForGuiPositionElements()
mLowerLeftCheckBox->blockSignals( true );
mLowerMiddleCheckBox->blockSignals( true );
mLowerRightCheckBox->blockSignals( true );
mPageSpinBox->blockSignals( true );

QPointF pos = mItem->pagePos();

if ( mItem->lastUsedPositionMode() == QgsComposerItem::UpperLeft )
{
mUpperLeftCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() ) );
mXLineEdit->setText( QString::number( pos.x() ) );
mYLineEdit->setText( QString::number( pos.y() ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::UpperMiddle )
{
mUpperMiddleCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( mItem->pos().y() ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( pos.y() ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::UpperRight )
{
mUpperRightCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( pos.y() ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::MiddleLeft )
{
mMiddleLeftCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() / 2.0 ) );
mXLineEdit->setText( QString::number( pos.x() ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() / 2.0 ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::Middle )
{
mMiddleCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() / 2.0 ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() / 2.0 ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::MiddleRight )
{
mMiddleRightCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() / 2.0 ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() / 2.0 ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::LowerLeft )
{
mLowerLeftCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() ) );
mXLineEdit->setText( QString::number( pos.x() ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::LowerMiddle )
{
mLowerMiddleCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() / 2.0 ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() ) );
}

if ( mItem->lastUsedPositionMode() == QgsComposerItem::LowerRight )
{
mLowerRightCheckBox->setChecked( true );
mXLineEdit->setText( QString::number( mItem->pos().x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( mItem->pos().y() + mItem->rect().height() ) );
mXLineEdit->setText( QString::number( pos.x() + mItem->rect().width() ) );
mYLineEdit->setText( QString::number( pos.y() + mItem->rect().height() ) );
}

mWidthLineEdit->setText( QString::number( mItem->rect().width() ) );
mHeightLineEdit->setText( QString::number( mItem->rect().height() ) );
mPageSpinBox->setValue( mItem->page() );


mXLineEdit->blockSignals( false );
Expand All @@ -352,6 +357,7 @@ void QgsComposerItemWidget::setValuesForGuiPositionElements()
mLowerLeftCheckBox->blockSignals( false );
mLowerMiddleCheckBox->blockSignals( false );
mLowerRightCheckBox->blockSignals( false );
mPageSpinBox->blockSignals( false );
}

void QgsComposerItemWidget::setValuesForGuiNonPositionElements()
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposeritemwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
void on_mItemIdLineEdit_editingFinished();

//adjust coordinates in line edits
void on_mPageSpinBox_valueChanged( int ) { changeItemPosition(); }
void on_mXLineEdit_editingFinished() { changeItemPosition(); }
void on_mYLineEdit_editingFinished() { changeItemPosition(); }
void on_mWidthLineEdit_editingFinished() { changeItemPosition(); }
Expand Down
61 changes: 56 additions & 5 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
}

//scene rect
QPointF pagepos = pagePos();
composerItemElem.setAttribute( "x", QString::number( pos().x() ) );
composerItemElem.setAttribute( "y", QString::number( pos().y() ) );
composerItemElem.setAttribute( "page", page() );
composerItemElem.setAttribute( "pagex", QString::number( pagepos.x() ) );
composerItemElem.setAttribute( "pagey", QString::number( pagepos.y() ) );
composerItemElem.setAttribute( "width", QString::number( rect().width() ) );
composerItemElem.setAttribute( "height", QString::number( rect().height() ) );
composerItemElem.setAttribute( "positionMode", QString::number(( int ) mLastUsedPositionMode ) );
Expand Down Expand Up @@ -280,18 +284,29 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
}

//position
double x, y, width, height;
bool xOk, yOk, widthOk, heightOk, positionModeOK;
int page;
double x, y, pagex, pagey, width, height;
bool xOk, yOk, pageOk, pagexOk, pageyOk, widthOk, heightOk, positionModeOK;

x = itemElem.attribute( "x" ).toDouble( &xOk );
y = itemElem.attribute( "y" ).toDouble( &yOk );
page = itemElem.attribute( "page" ).toInt( &pageOk );
pagex = itemElem.attribute( "pagex" ).toDouble( &pagexOk );
pagey = itemElem.attribute( "pagey" ).toDouble( &pageyOk );
width = itemElem.attribute( "width" ).toDouble( &widthOk );
height = itemElem.attribute( "height" ).toDouble( &heightOk );
mLastUsedPositionMode = ( ItemPositionMode )itemElem.attribute( "positionMode" ).toInt( &positionModeOK );
if ( !positionModeOK )
{
mLastUsedPositionMode = UpperLeft;
}
if ( pageOk && pagexOk && pageyOk )
{
xOk = true;
yOk = true;
x = pagex;
y = ( page - 1 ) * ( mComposition->paperHeight() + composition()->spaceBetweenPages() ) + pagey;
}

if ( !xOk || !yOk || !widthOk || !heightOk )
{
Expand Down Expand Up @@ -481,18 +496,54 @@ void QgsComposerItem::move( double dx, double dy )
setSceneRect( newSceneRect );
}

void QgsComposerItem::setItemPosition( double x, double y, ItemPositionMode itemPoint )
int QgsComposerItem::page() const
{
double y = pos().y();
double h = composition()->paperHeight() + composition()->spaceBetweenPages();
int page = 1;
while ( y - h >= 0. )
{
y -= h;
++page;
}
return page;
}

QPointF QgsComposerItem::pagePos() const
{
QPointF p = pos();
double h = composition()->paperHeight() + composition()->spaceBetweenPages();
p.ry() -= ( page() - 1 ) * h;
return p;
}

void QgsComposerItem::updatePagePos( double newPageWidth, double newPageHeight )
{
Q_UNUSED( newPageWidth )
QPointF curPagePos = pagePos();
int curPage = page() - 1;
setY( curPage * ( newPageHeight + composition()->spaceBetweenPages() ) + curPagePos.y() );
emit sizeChanged();
}

void QgsComposerItem::setItemPosition( double x, double y, ItemPositionMode itemPoint, int page )
{
double width = rect().width();
double height = rect().height();
setItemPosition( x, y, width, height, itemPoint );
setItemPosition( x, y, width, height, itemPoint, false, page );
}

void QgsComposerItem::setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint, bool posIncludesFrame )
void QgsComposerItem::setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint, bool posIncludesFrame, int page )
{
double upperLeftX = x;
double upperLeftY = y;

if ( page > 0 )
{
double h = composition()->paperHeight() + composition()->spaceBetweenPages();
upperLeftY += ( page - 1 ) * h;
}

//store the item position mode
mLastUsedPositionMode = itemPoint;

Expand Down
15 changes: 12 additions & 3 deletions src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,17 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
@param y y-position of mouse cursor (in item coordinates)*/
virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }

/** gets the page the item is currently on */
int page() const;

/** Returns the position relative to the current page */
QPointF pagePos() const;

/** Updates the page relative position for the new paper size */
void updatePagePos( double newPageWidth, double newPageHeight );

/**Moves the item to a new position (in canvas coordinates)*/
void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft );
void setItemPosition( double x, double y, ItemPositionMode itemPoint = UpperLeft, int page = -1 );

/**Sets item position and width / height in one go
@param x item position x
Expand All @@ -143,9 +152,9 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
@param height item height
@param itemPoint item position mode
@param posIncludesFrame set to true if the position and size arguments include the item's frame border
@param page if page > 0, y is interpreted as relative to the origin of the specified page, if page <= 0, y is in absolute canvas coordinates
@note: this method was added in version 1.6*/
void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false );
void setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint = UpperLeft, bool posIncludesFrame = false, int page = -1 );

/**Returns item's last used position mode.
@note: This property has no effect on actual's item position, which is always the top-left corner.
Expand Down
11 changes: 11 additions & 0 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ QRectF QgsComposition::compositionBounds() const

void QgsComposition::setPaperSize( double width, double height )
{
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
QgsComposerItem* composerItem = dynamic_cast<QgsComposerItem *>( *itemIt );
if ( composerItem )
{
composerItem->updatePagePos( width, height );
}
}

mPageWidth = width;
mPageHeight = height;
double currentY = 0;
Expand Down
48 changes: 31 additions & 17 deletions src/ui/qgscomposeritemwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -179,45 +179,59 @@
</item>
<item row="0" column="0" rowspan="2">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="mXLabel">
<property name="text">
<string>X</string>
</property>
</widget>
<item row="4" column="1">
<widget class="QLineEdit" name="mHeightLineEdit"/>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLineEdit" name="mXLineEdit"/>
</item>
<item row="1" column="0">
<item row="3" column="1">
<widget class="QLineEdit" name="mWidthLineEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mYLabel">
<property name="text">
<string>Y</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="mYLineEdit"/>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="mWidthLabel">
<property name="text">
<string>Width</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="mWidthLineEdit"/>
<item row="1" column="0">
<widget class="QLabel" name="mXLabel">
<property name="text">
<string>X</string>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="mHeightLabel">
<property name="text">
<string>Height</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="mHeightLineEdit"/>
<item row="2" column="1">
<widget class="QLineEdit" name="mYLineEdit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="mPageLabel">
<property name="text">
<string>Page</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="mPageSpinBox">
<property name="minimum">
<number>1</number>
</property>
</widget>
</item>
</layout>
</item>
Expand Down

0 comments on commit b7423cf

Please sign in to comment.