Skip to content

Commit 9aa865e

Browse files
committed
Implement moving of snapped features together with the snap line
1 parent 717918d commit 9aa865e

31 files changed

+141
-20
lines changed

src/app/composer/qgscomposer.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
class QgisApp;
2626
class QgsComposerArrow;
27+
class QgsComposerFrame;
28+
class QgsComposerHtml;
2729
class QgsComposerLabel;
2830
class QgsComposerLegend;
2931
class QgsComposerPicture;

src/app/composer/qgscomposerhtmlwidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgscomposeritemwidget.h"
1818
#include "qgscomposermultiframecommand.h"
1919
#include "qgscomposerhtml.h"
20+
#include "qgscomposition.h"
2021
#include <QFileDialog>
2122
#include <QSettings>
2223

src/app/composer/qgscomposerlabelwidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgscomposerlabelwidget.h"
1919
#include "qgscomposerlabel.h"
2020
#include "qgscomposeritemwidget.h"
21+
#include "qgscomposition.h"
2122
#include "qgsexpressionbuilderdialog.h"
2223

2324
#include <QColorDialog>

src/app/composer/qgscomposerlegendwidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgscomposerlegendlayersdialog.h"
2323
#include "qgscomposeritemwidget.h"
2424
#include "qgscomposermap.h"
25+
#include "qgscomposition.h"
2526
#include <QFontDialog>
2627
#include <QColorDialog>
2728

src/app/composer/qgscomposermapwidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposermapwidget.h"
1919
#include "qgscomposeritemwidget.h"
20+
#include "qgscomposition.h"
2021
#include "qgsmaprenderer.h"
2122
#include "qgsstylev2.h"
2223
#include "qgssymbolv2.h"

src/app/composer/qgscomposerpicturewidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgscomposermap.h"
2121
#include "qgscomposerpicture.h"
2222
#include "qgscomposeritemwidget.h"
23+
#include "qgscomposition.h"
2324
#include <QDoubleValidator>
2425
#include <QFileDialog>
2526
#include <QFileInfo>

src/app/composer/qgscomposerscalebarwidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgscomposeritemwidget.h"
1919
#include "qgscomposermap.h"
2020
#include "qgscomposerscalebar.h"
21+
#include "qgscomposition.h"
2122
#include <QColorDialog>
2223
#include <QFontDialog>
2324
#include <QWidget>

src/app/qgisapp.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
#include <qgsnetworkaccessmanager.h>
7171
#include <qgsapplication.h>
72+
#include <qgscomposition.h>
7273

7374
#include <QNetworkReply>
7475
#include <QNetworkProxy>

src/core/composer/qgsaddremoveitemcommand.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsaddremoveitemcommand.h"
1919
#include "qgscomposeritem.h"
20+
#include "qgscomposition.h"
2021

2122
QgsAddRemoveItemCommand::QgsAddRemoveItemCommand( State s, QgsComposerItem* item, QgsComposition* c, const QString& text, QUndoCommand* parent ):
2223
QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true )

src/core/composer/qgsatlascomposition.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsatlascomposition.h"
2020
#include "qgsvectorlayer.h"
2121
#include "qgscomposermap.h"
22+
#include "qgscomposition.h"
2223
#include "qgsvectordataprovider.h"
2324
#include "qgsexpression.h"
2425
#include "qgsgeometry.h"
@@ -49,7 +50,7 @@ void QgsAtlasComposition::setCoverageLayer( QgsVectorLayer* layer )
4950
mCoverageLayer = layer;
5051

5152
// update the number of features
52-
QgsExpression::setSpecialColumn( "$numfeatures", QVariant( (int)mFeatureIds.size() ) );
53+
QgsExpression::setSpecialColumn( "$numfeatures", QVariant(( int )mFeatureIds.size() ) );
5354
}
5455

5556
void QgsAtlasComposition::beginRender()
@@ -213,18 +214,18 @@ void QgsAtlasComposition::prepareForFeature( size_t featureI )
213214
// geometry height is too big
214215
if ( geom_ratio < map_ratio )
215216
{
216-
// extent the bbox's width
217-
double adj_width = ( map_ratio * geom_rect.height() - geom_rect.width() ) / 2.0;
218-
xa1 -= adj_width;
219-
xa2 += adj_width;
217+
// extent the bbox's width
218+
double adj_width = ( map_ratio * geom_rect.height() - geom_rect.width() ) / 2.0;
219+
xa1 -= adj_width;
220+
xa2 += adj_width;
220221
}
221222
// geometry width is too big
222223
else if ( geom_ratio > map_ratio )
223224
{
224-
// extent the bbox's height
225-
double adj_height = (geom_rect.width() / map_ratio - geom_rect.height() ) / 2.0;
226-
ya1 -= adj_height;
227-
ya2 += adj_height;
225+
// extent the bbox's height
226+
double adj_height = ( geom_rect.width() / map_ratio - geom_rect.height() ) / 2.0;
227+
ya1 -= adj_height;
228+
ya2 += adj_height;
228229
}
229230
new_extent = QgsRectangle( xa1, ya1, xa2, ya2 );
230231

src/core/composer/qgscomposerarrow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposerarrow.h"
19+
#include "qgscomposition.h"
1920
#include <QPainter>
2021
#include <QSvgRenderer>
2122

src/core/composer/qgscomposerarrow.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define QGSCOMPOSERARROW_H
2020

2121
#include "qgscomposeritem.h"
22+
#include <QBrush>
23+
#include <QPen>
2224

2325
/**An item that draws an arrow between to points*/
2426
class CORE_EXPORT QgsComposerArrow: public QgsComposerItem

src/core/composer/qgscomposerframe.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "qgscomposerframe.h"
1717
#include "qgscomposermultiframe.h"
18+
#include "qgscomposition.h"
1819

1920
QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf, qreal x, qreal y, qreal width, qreal height )
2021
: QgsComposerItem( x, y, width, height, c )

src/core/composer/qgscomposeritem.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
#ifndef QGSCOMPOSERITEM_H
1818
#define QGSCOMPOSERITEM_H
1919

20-
#include "qgscomposition.h"
20+
#include "qgscomposeritemcommand.h"
2121
#include <QGraphicsRectItem>
2222
#include <QObject>
2323

24+
class QgsComposition;
2425
class QWidget;
2526
class QDomDocument;
2627
class QDomElement;
2728
class QGraphicsLineItem;
2829

29-
class QqsComposition;
30-
3130
/** \ingroup MapComposer
3231
* A item that forms part of a map composition.
3332
*/

src/core/composer/qgscomposerlabel.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposerlabel.h"
19+
#include "qgscomposition.h"
1920
#include "qgsexpression.h"
2021
#include <QDate>
2122
#include <QDomElement>

src/core/composer/qgscomposerlabel.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define QGSCOMPOSERLABEL_H
1919

2020
#include "qgscomposeritem.h"
21+
#include <QFont>
2122

2223
class QgsVectorLayer;
2324
class QgsFeature;

src/core/composer/qgscomposerlegend.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgscomposerlegend.h"
2020
#include "qgscomposerlegenditem.h"
2121
#include "qgscomposermap.h"
22+
#include "qgscomposition.h"
2223
#include "qgslogger.h"
2324
#include "qgsmaplayer.h"
2425
#include "qgsmaplayerregistry.h"

src/core/composer/qgscomposermap.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposermap.h"
19+
#include "qgscomposition.h"
1920
#include "qgscoordinatetransform.h"
2021
#include "qgslogger.h"
2122
#include "qgsmaprenderer.h"

src/core/composer/qgscomposermap.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//#include "ui_qgscomposermapbase.h"
2121
#include "qgscomposeritem.h"
2222
#include "qgsrectangle.h"
23+
#include <QFont>
2324
#include <QGraphicsRectItem>
2425

2526
class QgsComposition;

src/core/composer/qgscomposermultiframe.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "qgscomposermultiframe.h"
1717
#include "qgscomposerframe.h"
18+
#include "qgscomposition.h"
1819

1920
QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands ): mComposition( c ), mResizeMode( UseExistingFrames ), mCreateUndoCommands( createUndoCommands )
2021
{

src/core/composer/qgscomposerpicture.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposerpicture.h"
1919
#include "qgscomposermap.h"
20+
#include "qgscomposition.h"
2021
#include "qgsproject.h"
2122
#include <QDomDocument>
2223
#include <QDomElement>

src/core/composer/qgscomposerpicture.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <QImage>
2323
#include <QSvgRenderer>
2424

25+
class QgsComposerMap;
26+
2527
/** \ingroup MapComposer
2628
* A composer class that displays svg files or raster format (jpg, png, ...)
2729
* */

src/core/composer/qgscomposerscalebar.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgscomposerscalebar.h"
1818
#include "qgscomposermap.h"
19+
#include "qgscomposition.h"
1920
#include "qgsdistancearea.h"
2021
#include "qgsscalebarstyle.h"
2122
#include "qgsdoubleboxscalebarstyle.h"

src/core/composer/qgscomposerscalebar.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define QGSCOMPOSERSCALEBAR_H
1818

1919
#include "qgscomposeritem.h"
20+
#include <QFont>
2021
#include <QPen>
2122
#include <QColor>
2223

src/core/composer/qgscomposershape.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define QGSCOMPOSERSHAPE_H
2020

2121
#include "qgscomposeritem.h"
22+
#include <QBrush>
23+
#include <QPen>
2224

2325
/**A composer items that draws common shapes (ellipse, triangle, rectangle)*/
2426
class CORE_EXPORT QgsComposerShape: public QgsComposerItem

src/core/composer/qgscomposertable.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSCOMPOSERTABLE_H
2020

2121
#include "qgscomposeritem.h"
22+
#include "qgscomposition.h"
2223
#include "qgsfeature.h"
2324
#include <QSet>
2425

src/core/composer/qgscomposition.cpp

+54-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <stdexcept>
1818

1919
#include "qgscomposition.h"
20-
#include "qgscomposeritem.h"
2120
#include "qgscomposerarrow.h"
2221
#include "qgscomposerframe.h"
2322
#include "qgscomposerhtml.h"
@@ -1077,25 +1076,28 @@ void QgsComposition::removeSnapLine( QGraphicsLineItem* line )
10771076
delete line;
10781077
}
10791078

1080-
QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, double y, double tolerance )
1079+
QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, double y, double tolerance,
1080+
QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode> >& snappedItems )
10811081
{
1082-
bool xDirection = doubleNear( y, 0.0 );
10831082
double minSqrDist = DBL_MAX;
10841083
QGraphicsLineItem* item = 0;
10851084
double currentXCoord = 0;
10861085
double currentYCoord = 0;
10871086
double currentSqrDist = 0;
10881087
double sqrTolerance = tolerance * tolerance;
10891088

1089+
snappedItems.clear();
1090+
10901091
QList< QGraphicsLineItem* >::const_iterator it = mSnapLines.constBegin();
10911092
for ( ; it != mSnapLines.constEnd(); ++it )
10921093
{
1093-
if ( horizontal )
1094+
bool itemHorizontal = doubleNear(( *it )->line().y2() - ( *it )->line().y1(), 0 );
1095+
if ( horizontal && itemHorizontal )
10941096
{
10951097
currentYCoord = ( *it )->line().y1();
10961098
currentSqrDist = ( y - currentYCoord ) * ( y - currentYCoord );
10971099
}
1098-
else
1100+
else if ( !itemHorizontal )
10991101
{
11001102
currentXCoord = ( *it )->line().x1();
11011103
currentSqrDist = ( x - currentXCoord ) * ( x - currentXCoord );
@@ -1108,6 +1110,53 @@ QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, d
11081110
}
11091111
}
11101112

1113+
double itemTolerance = 0.0000001;
1114+
if ( item )
1115+
{
1116+
//go through all the items to find items snapped to this snap line
1117+
QList<QGraphicsItem *> itemList = items();
1118+
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
1119+
for ( ; itemIt != itemList.end(); ++itemIt )
1120+
{
1121+
QgsComposerItem* currentItem = dynamic_cast<QgsComposerItem*>( *itemIt );
1122+
if ( !currentItem || currentItem->type() == QgsComposerItem::ComposerPaper )
1123+
{
1124+
continue;
1125+
}
1126+
1127+
if ( horizontal )
1128+
{
1129+
if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().top(), itemTolerance ) )
1130+
{
1131+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::UpperMiddle ) );
1132+
}
1133+
else if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().center().y(), itemTolerance ) )
1134+
{
1135+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::Middle ) );
1136+
}
1137+
else if ( doubleNear( currentYCoord, currentItem->transform().dy() + currentItem->rect().bottom(), itemTolerance ) )
1138+
{
1139+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::LowerMiddle ) );
1140+
}
1141+
}
1142+
else
1143+
{
1144+
if ( doubleNear( currentXCoord, currentItem->transform().dx(), itemTolerance ) )
1145+
{
1146+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::MiddleLeft ) );
1147+
}
1148+
else if ( doubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().center().x(), itemTolerance ) )
1149+
{
1150+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::Middle ) );
1151+
}
1152+
else if ( doubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().width(), itemTolerance ) )
1153+
{
1154+
snappedItems.append( qMakePair( currentItem, QgsComposerItem::MiddleRight ) );
1155+
}
1156+
}
1157+
}
1158+
}
1159+
11111160
return item;
11121161
}
11131162

src/core/composer/qgscomposition.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
#ifndef QGSCOMPOSITION_H
1717
#define QGSCOMPOSITION_H
1818

19+
#include "qgscomposeritem.h"
1920
#include <memory>
2021

2122
#include <QDomDocument>
2223
#include <QGraphicsScene>
2324
#include <QLinkedList>
25+
#include <QList>
26+
#include <QPair>
2427
#include <QSet>
2528
#include <QUndoStack>
2629
#include <QPrinter>
@@ -30,8 +33,8 @@
3033
#include "qgscomposeritemcommand.h"
3134
#include "qgsatlascomposition.h"
3235

36+
3337
class QgsComposerFrame;
34-
class QgsComposerItem;
3538
class QgsComposerMap;
3639
class QgsPaperItem;
3740
class QGraphicsRectItem;
@@ -264,7 +267,7 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
264267
/**Remove custom snap line (and delete the object)*/
265268
void removeSnapLine( QGraphicsLineItem* line );
266269
/**Get nearest snap line*/
267-
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance );
270+
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
268271

269272
/**Allocates new item command and saves initial state in it
270273
@param item target item

src/core/composer/qgspaperitem.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgspaperitem.h"
19+
#include "qgscomposition.h"
1920
#include <QPainter>
2021

2122
QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false )

0 commit comments

Comments
 (0)