Skip to content

Commit

Permalink
Some improvements and bugfixes for moving and resizing composer items
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@10776 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed May 13, 2009
1 parent d3d5343 commit 524023d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
103 changes: 70 additions & 33 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,21 @@ QgsComposerItem::MouseMoveAction QgsComposerItem::mouseMoveActionForPosition( co
bool nearLowerBorder = false;
bool nearUpperBorder = false;

if ( itemCoordPos.x() < 5 )
double borderTolerance = rectHandlerBorderTolerance();

if ( itemCoordPos.x() < borderTolerance )
{
nearLeftBorder = true;
}
if ( itemCoordPos.y() < 5 )
if ( itemCoordPos.y() < borderTolerance )
{
nearUpperBorder = true;
}
if ( itemCoordPos.x() > ( rect().width() - 5 ) )
if ( itemCoordPos.x() > ( rect().width() - borderTolerance ) )
{
nearRightBorder = true;
}
if ( itemCoordPos.y() > ( rect().height() - 5 ) )
if ( itemCoordPos.y() > ( rect().height() - borderTolerance ) )
{
nearLowerBorder = true;
}
Expand Down Expand Up @@ -431,6 +433,9 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition, const
return;
}

//test if change item is a composer item. If so, prefer call to setSceneRect() instead of setTransform() and setRect()
QgsComposerItem* changeComposerItem = dynamic_cast<QgsComposerItem*>(changeItem);

double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
QPointF snappedPosition = mComposition->snapPointToGrid( currentPosition );
//double diffX = snappedPosition.x() - mouseMoveStartPos.x();
Expand Down Expand Up @@ -499,17 +504,34 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition, const
double moveRectX = snappedLeftPoint.x() - originalItem->transform().dx();
double moveRectY = snappedLeftPoint.y() - originalItem->transform().dy();

QTransform moveTransform;
moveTransform.translate( originalItem->transform().dx() + moveRectX, originalItem->transform().dy() + moveRectY );
changeItem->setTransform( moveTransform );
if(!changeComposerItem)
{
QTransform moveTransform;
moveTransform.translate( originalItem->transform().dx() + moveRectX, originalItem->transform().dy() + moveRectY );
changeItem->setTransform( moveTransform );
}
else //for composer items, we prefer setSceneRect as subclasses can implement custom behaviour (e.g. item group)
{
changeComposerItem->setSceneRect(QRectF(originalItem->transform().dx() + moveRectX, \
originalItem->transform().dy() + moveRectY, \
originalItem->rect().width(), originalItem->rect().height()));
}
return;
}

QTransform itemTransform;
itemTransform.translate( originalItem->transform().dx() + mx, originalItem->transform().dy() + my );
changeItem->setTransform( itemTransform );
QRectF itemRect( 0, 0, originalItem->rect().width() + rx, originalItem->rect().height() + ry );
changeItem->setRect( itemRect );
if(!changeComposerItem)
{
QTransform itemTransform;
itemTransform.translate( originalItem->transform().dx() + mx, originalItem->transform().dy() + my );
changeItem->setTransform( itemTransform );
QRectF itemRect( 0, 0, originalItem->rect().width() + rx, originalItem->rect().height() + ry );
changeItem->setRect( itemRect );
}
else //for composer items, we prefer setSceneRect as subclasses can implement custom behaviour (e.g. item group)
{
changeComposerItem->setSceneRect( QRectF(originalItem->transform().dx() + mx, originalItem->transform().dy() + my, \
originalItem->rect().width() + rx, originalItem->rect().height() + ry) );
}
}

void QgsComposerItem::drawSelectionBoxes( QPainter* p )
Expand All @@ -523,26 +545,8 @@ void QgsComposerItem::drawSelectionBoxes( QPainter* p )
{
//size of symbol boxes depends on zoom level in composer view
double viewScaleFactor = horizontalViewScaleFactor();
double rectHandlerSize = 10.0 / viewScaleFactor;
double lockSymbolSize = 20.0 / viewScaleFactor;

//make sure the boxes don't get too large
if(rectHandlerSize > (rect().width() / 3))
{
rectHandlerSize = rect().width() / 3;
}
if(rectHandlerSize > (rect().height() / 3))
{
rectHandlerSize = rect().height() / 3;
}
if(lockSymbolSize > (rect().width() / 3))
{
lockSymbolSize = rect().width() / 3;
}
if(lockSymbolSize > (rect().height() / 3))
{
lockSymbolSize = rect().height() / 3;
}
double rectHandlerSize = rectHandlerBorderTolerance();
double sizeLockSymbol = lockSymbolSize();

if(mItemPositionLocked)
{
Expand All @@ -556,7 +560,7 @@ void QgsComposerItem::drawSelectionBoxes( QPainter* p )
QImage lockImage(lockIconPath);
if(!lockImage.isNull())
{
p->drawImage(QRectF(0, 0, lockSymbolSize, lockSymbolSize), lockImage, QRectF(0, 0, lockImage.width(), lockImage.height()));
p->drawImage(QRectF(0, 0, sizeLockSymbol, sizeLockSymbol), lockImage, QRectF(0, 0, lockImage.width(), lockImage.height()));
}
}
else //draw blue squares
Expand Down Expand Up @@ -743,6 +747,39 @@ double QgsComposerItem::horizontalViewScaleFactor() const
return result;
}

double QgsComposerItem::rectHandlerBorderTolerance() const
{
//size of symbol boxes depends on zoom level in composer view
double viewScaleFactor = horizontalViewScaleFactor();
double rectHandlerSize = 10.0 / viewScaleFactor;

//make sure the boxes don't get too large
if(rectHandlerSize > (rect().width() / 3))
{
rectHandlerSize = rect().width() / 3;
}
if(rectHandlerSize > (rect().height() / 3))
{
rectHandlerSize = rect().height() / 3;
}
return rectHandlerSize;
}

double QgsComposerItem::lockSymbolSize() const
{
double lockSymbolSize = 20.0 / horizontalViewScaleFactor();

if(lockSymbolSize > (rect().width() / 3))
{
lockSymbolSize = rect().width() / 3;
}
if(lockSymbolSize > (rect().height() / 3))
{
lockSymbolSize = rect().height() / 3;
}
return lockSymbolSize;
}

void QgsComposerItem::updateCursor(const QPointF& itemPos)
{
setCursor( cursorForPosition(itemPos) );
Expand Down
11 changes: 10 additions & 1 deletion src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,17 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
/**Draw background*/
virtual void drawBackground( QPainter* p );

/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the \
item border for resizing*/
double rectHandlerBorderTolerance() const;

/**Returns the size of the lock symbol depending on the composer zoom level and the item size
@note: this function was introduced in version 1.2*/
double lockSymbolSize() const;

/**Returns the zoom factor of the graphics view. If no
graphics view exists, the default 1 is returned*/
graphics view exists, the default 1 is returned
@note: this function was introduced in version 1.2*/
double horizontalViewScaleFactor() const;
};

Expand Down

0 comments on commit 524023d

Please sign in to comment.