Skip to content

Commit e386ae4

Browse files
committed
[FEATURE]: allow item selection tolerance in composer (default is 0)
1 parent 34f4ab6 commit e386ae4

File tree

7 files changed

+86
-14
lines changed

7 files changed

+86
-14
lines changed

python/core/qgscomposition.sip

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class QgsComposition: QGraphicsScene
7979
bool printAsRaster() const;
8080
void setPrintAsRaster(bool enabled);
8181

82+
double selectionTolerance() const;
83+
void setSelectionTolerance( double tol );
84+
8285
/**Returns pointer to map renderer of qgis map canvas*/
8386
QgsMapRenderer* mapRenderer();
8487

src/app/composer/qgscompositionwidget.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
9191
{
9292
mGridStyleComboBox->setCurrentIndex( 2 );
9393
}
94+
95+
mSelectionToleranceSpinBox->setValue( mComposition->selectionTolerance() );
9496
}
9597
blockSignals( false );
9698
}
@@ -503,6 +505,14 @@ void QgsCompositionWidget::on_mPenWidthSpinBox_valueChanged( double d )
503505
}
504506
}
505507

508+
void QgsCompositionWidget::on_mSelectionToleranceSpinBox_valueChanged( double d )
509+
{
510+
if ( mComposition )
511+
{
512+
mComposition->setSelectionTolerance( d );
513+
}
514+
}
515+
506516
void QgsCompositionWidget::blockSignals( bool block )
507517
{
508518
mPaperSizeComboBox->blockSignals( block );
@@ -519,4 +529,5 @@ void QgsCompositionWidget::blockSignals( bool block )
519529
mPenWidthSpinBox->blockSignals( block );
520530
mGridColorButton->blockSignals( block );
521531
mGridStyleComboBox->blockSignals( block );
532+
mSelectionToleranceSpinBox->blockSignals( block );
522533
}

src/app/composer/qgscompositionwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
5555
void on_mGridColorButton_clicked();
5656
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
5757
void on_mPenWidthSpinBox_valueChanged( double d );
58+
void on_mSelectionToleranceSpinBox_valueChanged( double d );
59+
5860
/**Sets GUI elements to width/height from composition*/
5961
void displayCompositionWidthHeight();
6062

src/core/composer/qgscomposeritem.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ void QgsComposerItem::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
296296
return;
297297
}
298298

299+
if ( !isSelected() )
300+
{
301+
return;
302+
}
303+
299304
if ( mBoundingResizeRectangle )
300305
{
301306
double diffX = event->lastScenePos().x() - mLastMouseEventPos.x();
@@ -313,6 +318,11 @@ void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
313318
return;
314319
}
315320

321+
if ( !isSelected() )
322+
{
323+
return;
324+
}
325+
316326
//set current position and type of mouse move action
317327
mMouseMoveStartPos = event->lastScenePos();
318328
mLastMouseEventPos = event->lastScenePos();
@@ -347,6 +357,11 @@ void QgsComposerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
347357
return;
348358
}
349359

360+
if ( !isSelected() )
361+
{
362+
return;
363+
}
364+
350365
//delete frame rectangle
351366
if ( mBoundingResizeRectangle )
352367
{
@@ -728,6 +743,10 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
728743
{
729744
setCursor( cursorForPosition( event->pos() ) );
730745
}
746+
else
747+
{
748+
setCursor( Qt::ArrowCursor );
749+
}
731750
}
732751

733752
void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const

src/core/composer/qgscomposition.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include <QSettings>
2626

2727
QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
28-
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ), mSnapToGrid( false ),
29-
mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
28+
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ), mSelectionTolerance( 0.0 ),
29+
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
3030
{
3131
setBackgroundBrush( Qt::gray );
3232

@@ -36,14 +36,14 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
3636
addItem( mPaperItem );
3737
mPaperItem->setZValue( 0 );
3838
mPrintResolution = 300; //hardcoded default
39-
loadGridAppearanceSettings();
39+
loadSettings();
4040
}
4141

4242
QgsComposition::QgsComposition():
4343
QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ),
44-
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
44+
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
4545
{
46-
loadGridAppearanceSettings();
46+
loadSettings();
4747
}
4848

4949
QgsComposition::~QgsComposition()
@@ -77,7 +77,16 @@ double QgsComposition::paperWidth() const
7777

7878
QgsComposerItem* QgsComposition::composerItemAt( const QPointF & position )
7979
{
80-
QList<QGraphicsItem *> itemList = items( position );
80+
QList<QGraphicsItem*> itemList;
81+
if ( mSelectionTolerance <= 0.0 )
82+
{
83+
itemList = items( position );
84+
}
85+
else
86+
{
87+
itemList = items( QRectF( position.x() - mSelectionTolerance, position.y() - mSelectionTolerance, 2 * mSelectionTolerance, 2 * mSelectionTolerance ),
88+
Qt::IntersectsItemShape, Qt::DescendingOrder );
89+
}
8190
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
8291

8392
for ( ; itemIt != itemList.end(); ++itemIt )
@@ -663,6 +672,7 @@ void QgsComposition::setSnapToGridEnabled( bool b )
663672
{
664673
mPaperItem->update();
665674
}
675+
saveSettings();
666676
}
667677

668678
void QgsComposition::setSnapGridResolution( double r )
@@ -672,6 +682,7 @@ void QgsComposition::setSnapGridResolution( double r )
672682
{
673683
mPaperItem->update();
674684
}
685+
saveSettings();
675686
}
676687

677688
void QgsComposition::setSnapGridOffsetX( double offset )
@@ -681,6 +692,7 @@ void QgsComposition::setSnapGridOffsetX( double offset )
681692
{
682693
mPaperItem->update();
683694
}
695+
saveSettings();
684696
}
685697

686698
void QgsComposition::setSnapGridOffsetY( double offset )
@@ -690,6 +702,7 @@ void QgsComposition::setSnapGridOffsetY( double offset )
690702
{
691703
mPaperItem->update();
692704
}
705+
saveSettings();
693706
}
694707

695708
void QgsComposition::setGridPen( const QPen& p )
@@ -699,7 +712,7 @@ void QgsComposition::setGridPen( const QPen& p )
699712
{
700713
mPaperItem->update();
701714
}
702-
saveGridAppearanceSettings();
715+
saveSettings();
703716
}
704717

705718
void QgsComposition::setGridStyle( GridStyle s )
@@ -709,10 +722,16 @@ void QgsComposition::setGridStyle( GridStyle s )
709722
{
710723
mPaperItem->update();
711724
}
712-
saveGridAppearanceSettings();
725+
saveSettings();
726+
}
727+
728+
void QgsComposition::setSelectionTolerance( double tol )
729+
{
730+
mSelectionTolerance = tol;
731+
saveSettings();
713732
}
714733

715-
void QgsComposition::loadGridAppearanceSettings()
734+
void QgsComposition::loadSettings()
716735
{
717736
//read grid style, grid color and pen width from settings
718737
QSettings s;
@@ -742,9 +761,11 @@ void QgsComposition::loadGridAppearanceSettings()
742761
{
743762
mGridStyle = Solid;
744763
}
764+
765+
mSelectionTolerance = s.value( "/qgis/composerSelectionTolerance", 0.0 ).toDouble();
745766
}
746767

747-
void QgsComposition::saveGridAppearanceSettings()
768+
void QgsComposition::saveSettings()
748769
{
749770
//store grid appearance settings
750771
QSettings s;
@@ -765,6 +786,9 @@ void QgsComposition::saveGridAppearanceSettings()
765786
{
766787
s.setValue( "/qgis/composerGridStyle", "Crosses" );
767788
}
789+
790+
//store also selection tolerance
791+
s.setValue( "/qgis/composerSelectionTolerance", mSelectionTolerance );
768792
}
769793

770794
void QgsComposition::beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c )

src/core/composer/qgscomposition.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
109109
bool printAsRaster() const {return mPrintAsRaster;}
110110
void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
111111

112+
double selectionTolerance() const { return mSelectionTolerance; }
113+
void setSelectionTolerance( double tol );
114+
112115
/**Returns pointer to map renderer of qgis map canvas*/
113116
QgsMapRenderer* mapRenderer() {return mMapRenderer;}
114117

@@ -186,6 +189,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
186189
/**Flag if map should be printed as a raster (via QImage). False by default*/
187190
bool mPrintAsRaster;
188191

192+
/**Distance tolerance for item selection (in mm)*/
193+
double mSelectionTolerance;
194+
189195
/**Parameters for snap to grid function*/
190196
bool mSnapToGrid;
191197
double mSnapGridResolution;
@@ -207,8 +213,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
207213
@return 0 in case of success*/
208214
int boundingRectOfSelectedItems( QRectF& bRect );
209215

210-
void loadGridAppearanceSettings();
211-
void saveGridAppearanceSettings();
216+
void loadSettings();
217+
void saveSettings();
212218

213219
signals:
214220
void paperSizeChanged();

src/ui/qgscompositionwidgetbase.ui

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<property name="geometry">
3333
<rect>
3434
<x>0</x>
35-
<y>-11</y>
36-
<width>397</width>
35+
<y>0</y>
36+
<width>395</width>
3737
<height>579</height>
3838
</rect>
3939
</property>
@@ -281,6 +281,13 @@
281281
<item row="8" column="0">
282282
<widget class="QComboBox" name="mGridStyleComboBox"/>
283283
</item>
284+
<item row="9" column="0">
285+
<widget class="QDoubleSpinBox" name="mSelectionToleranceSpinBox">
286+
<property name="prefix">
287+
<string>Selection tolerance (mm) </string>
288+
</property>
289+
</widget>
290+
</item>
284291
</layout>
285292
</widget>
286293
</item>

0 commit comments

Comments
 (0)