Skip to content

Commit

Permalink
Implement moving of snapped features together with the snap line
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Feb 20, 2013
1 parent 717918d commit 9aa865e
Show file tree
Hide file tree
Showing 31 changed files with 141 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposer.h
Expand Up @@ -24,6 +24,8 @@

class QgisApp;
class QgsComposerArrow;
class QgsComposerFrame;
class QgsComposerHtml;
class QgsComposerLabel;
class QgsComposerLegend;
class QgsComposerPicture;
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerhtmlwidget.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgscomposeritemwidget.h"
#include "qgscomposermultiframecommand.h"
#include "qgscomposerhtml.h"
#include "qgscomposition.h"
#include <QFileDialog>
#include <QSettings>

Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerlabelwidget.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgscomposerlabelwidget.h"
#include "qgscomposerlabel.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposition.h"
#include "qgsexpressionbuilderdialog.h"

#include <QColorDialog>
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerlegendwidget.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgscomposerlegendlayersdialog.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include <QFontDialog>
#include <QColorDialog>

Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgscomposermapwidget.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposition.h"
#include "qgsmaprenderer.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgscomposermap.h"
#include "qgscomposerpicture.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposition.h"
#include <QDoubleValidator>
#include <QFileDialog>
#include <QFileInfo>
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerscalebarwidget.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgscomposeritemwidget.h"
#include "qgscomposermap.h"
#include "qgscomposerscalebar.h"
#include "qgscomposition.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QWidget>
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -69,6 +69,7 @@

#include <qgsnetworkaccessmanager.h>
#include <qgsapplication.h>
#include <qgscomposition.h>

#include <QNetworkReply>
#include <QNetworkProxy>
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgsaddremoveitemcommand.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsaddremoveitemcommand.h"
#include "qgscomposeritem.h"
#include "qgscomposition.h"

QgsAddRemoveItemCommand::QgsAddRemoveItemCommand( State s, QgsComposerItem* item, QgsComposition* c, const QString& text, QUndoCommand* parent ):
QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true )
Expand Down
19 changes: 10 additions & 9 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsatlascomposition.h"
#include "qgsvectorlayer.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgsvectordataprovider.h"
#include "qgsexpression.h"
#include "qgsgeometry.h"
Expand Down Expand Up @@ -49,7 +50,7 @@ void QgsAtlasComposition::setCoverageLayer( QgsVectorLayer* layer )
mCoverageLayer = layer;

// update the number of features
QgsExpression::setSpecialColumn( "$numfeatures", QVariant( (int)mFeatureIds.size() ) );
QgsExpression::setSpecialColumn( "$numfeatures", QVariant(( int )mFeatureIds.size() ) );
}

void QgsAtlasComposition::beginRender()
Expand Down Expand Up @@ -213,18 +214,18 @@ void QgsAtlasComposition::prepareForFeature( size_t featureI )
// geometry height is too big
if ( geom_ratio < map_ratio )
{
// extent the bbox's width
double adj_width = ( map_ratio * geom_rect.height() - geom_rect.width() ) / 2.0;
xa1 -= adj_width;
xa2 += adj_width;
// extent the bbox's width
double adj_width = ( map_ratio * geom_rect.height() - geom_rect.width() ) / 2.0;
xa1 -= adj_width;
xa2 += adj_width;
}
// geometry width is too big
else if ( geom_ratio > map_ratio )
{
// extent the bbox's height
double adj_height = (geom_rect.width() / map_ratio - geom_rect.height() ) / 2.0;
ya1 -= adj_height;
ya2 += adj_height;
// extent the bbox's height
double adj_height = ( geom_rect.width() / map_ratio - geom_rect.height() ) / 2.0;
ya1 -= adj_height;
ya2 += adj_height;
}
new_extent = QgsRectangle( xa1, ya1, xa2, ya2 );

Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerarrow.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgscomposerarrow.h"
#include "qgscomposition.h"
#include <QPainter>
#include <QSvgRenderer>

Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposerarrow.h
Expand Up @@ -19,6 +19,8 @@
#define QGSCOMPOSERARROW_H

#include "qgscomposeritem.h"
#include <QBrush>
#include <QPen>

/**An item that draws an arrow between to points*/
class CORE_EXPORT QgsComposerArrow: public QgsComposerItem
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerframe.cpp
Expand Up @@ -15,6 +15,7 @@

#include "qgscomposerframe.h"
#include "qgscomposermultiframe.h"
#include "qgscomposition.h"

QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf, qreal x, qreal y, qreal width, qreal height )
: QgsComposerItem( x, y, width, height, c )
Expand Down
5 changes: 2 additions & 3 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -17,17 +17,16 @@
#ifndef QGSCOMPOSERITEM_H
#define QGSCOMPOSERITEM_H

#include "qgscomposition.h"
#include "qgscomposeritemcommand.h"
#include <QGraphicsRectItem>
#include <QObject>

class QgsComposition;
class QWidget;
class QDomDocument;
class QDomElement;
class QGraphicsLineItem;

class QqsComposition;

/** \ingroup MapComposer
* A item that forms part of a map composition.
*/
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgscomposerlabel.h"
#include "qgscomposition.h"
#include "qgsexpression.h"
#include <QDate>
#include <QDomElement>
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -18,6 +18,7 @@
#define QGSCOMPOSERLABEL_H

#include "qgscomposeritem.h"
#include <QFont>

class QgsVectorLayer;
class QgsFeature;
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgscomposerlegend.h"
#include "qgscomposerlegenditem.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgscoordinatetransform.h"
#include "qgslogger.h"
#include "qgsmaprenderer.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -20,6 +20,7 @@
//#include "ui_qgscomposermapbase.h"
#include "qgscomposeritem.h"
#include "qgsrectangle.h"
#include <QFont>
#include <QGraphicsRectItem>

class QgsComposition;
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -15,6 +15,7 @@

#include "qgscomposermultiframe.h"
#include "qgscomposerframe.h"
#include "qgscomposition.h"

QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands ): mComposition( c ), mResizeMode( UseExistingFrames ), mCreateUndoCommands( createUndoCommands )
{
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerpicture.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgscomposerpicture.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgsproject.h"
#include <QDomDocument>
#include <QDomElement>
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposerpicture.h
Expand Up @@ -22,6 +22,8 @@
#include <QImage>
#include <QSvgRenderer>

class QgsComposerMap;

/** \ingroup MapComposer
* A composer class that displays svg files or raster format (jpg, png, ...)
* */
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerscalebar.cpp
Expand Up @@ -16,6 +16,7 @@

#include "qgscomposerscalebar.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgsdistancearea.h"
#include "qgsscalebarstyle.h"
#include "qgsdoubleboxscalebarstyle.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerscalebar.h
Expand Up @@ -17,6 +17,7 @@
#define QGSCOMPOSERSCALEBAR_H

#include "qgscomposeritem.h"
#include <QFont>
#include <QPen>
#include <QColor>

Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposershape.h
Expand Up @@ -19,6 +19,8 @@
#define QGSCOMPOSERSHAPE_H

#include "qgscomposeritem.h"
#include <QBrush>
#include <QPen>

/**A composer items that draws common shapes (ellipse, triangle, rectangle)*/
class CORE_EXPORT QgsComposerShape: public QgsComposerItem
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposertable.h
Expand Up @@ -19,6 +19,7 @@
#define QGSCOMPOSERTABLE_H

#include "qgscomposeritem.h"
#include "qgscomposition.h"
#include "qgsfeature.h"
#include <QSet>

Expand Down
59 changes: 54 additions & 5 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -17,7 +17,6 @@
#include <stdexcept>

#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgscomposerarrow.h"
#include "qgscomposerframe.h"
#include "qgscomposerhtml.h"
Expand Down Expand Up @@ -1077,25 +1076,28 @@ void QgsComposition::removeSnapLine( QGraphicsLineItem* line )
delete line;
}

QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, double y, double tolerance )
QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, double y, double tolerance,
QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode> >& snappedItems )
{
bool xDirection = doubleNear( y, 0.0 );
double minSqrDist = DBL_MAX;
QGraphicsLineItem* item = 0;
double currentXCoord = 0;
double currentYCoord = 0;
double currentSqrDist = 0;
double sqrTolerance = tolerance * tolerance;

snappedItems.clear();

QList< QGraphicsLineItem* >::const_iterator it = mSnapLines.constBegin();
for ( ; it != mSnapLines.constEnd(); ++it )
{
if ( horizontal )
bool itemHorizontal = doubleNear(( *it )->line().y2() - ( *it )->line().y1(), 0 );
if ( horizontal && itemHorizontal )
{
currentYCoord = ( *it )->line().y1();
currentSqrDist = ( y - currentYCoord ) * ( y - currentYCoord );
}
else
else if ( !itemHorizontal )
{
currentXCoord = ( *it )->line().x1();
currentSqrDist = ( x - currentXCoord ) * ( x - currentXCoord );
Expand All @@ -1108,6 +1110,53 @@ QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, d
}
}

double itemTolerance = 0.0000001;
if ( item )
{
//go through all the items to find items snapped to this snap line
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
QgsComposerItem* currentItem = dynamic_cast<QgsComposerItem*>( *itemIt );
if ( !currentItem || currentItem->type() == QgsComposerItem::ComposerPaper )
{
continue;
}

if ( horizontal )
{
if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().top(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::UpperMiddle ) );
}
else if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().center().y(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::Middle ) );
}
else if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().bottom(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::LowerMiddle ) );
}
}
else
{
if ( doubleNear( currentXCoord, currentItem->transform().dx(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::MiddleLeft ) );
}
else if ( doubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().center().x(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::Middle ) );
}
else if ( doubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().width(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::MiddleRight ) );
}
}
}
}

return item;
}

Expand Down
7 changes: 5 additions & 2 deletions src/core/composer/qgscomposition.h
Expand Up @@ -16,11 +16,14 @@
#ifndef QGSCOMPOSITION_H
#define QGSCOMPOSITION_H

#include "qgscomposeritem.h"
#include <memory>

#include <QDomDocument>
#include <QGraphicsScene>
#include <QLinkedList>
#include <QList>
#include <QPair>
#include <QSet>
#include <QUndoStack>
#include <QPrinter>
Expand All @@ -30,8 +33,8 @@
#include "qgscomposeritemcommand.h"
#include "qgsatlascomposition.h"


class QgsComposerFrame;
class QgsComposerItem;
class QgsComposerMap;
class QgsPaperItem;
class QGraphicsRectItem;
Expand Down Expand Up @@ -264,7 +267,7 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Remove custom snap line (and delete the object)*/
void removeSnapLine( QGraphicsLineItem* line );
/**Get nearest snap line*/
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance );
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );

/**Allocates new item command and saves initial state in it
@param item target item
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgspaperitem.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgspaperitem.h"
#include "qgscomposition.h"
#include <QPainter>

QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false )
Expand Down

0 comments on commit 9aa865e

Please sign in to comment.