Skip to content

Commit

Permalink
[FEATURE]: allow item selection tolerance in composer (default is 0)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Nov 25, 2011
1 parent 34f4ab6 commit e386ae4
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 14 deletions.
3 changes: 3 additions & 0 deletions python/core/qgscomposition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class QgsComposition: QGraphicsScene
bool printAsRaster() const;
void setPrintAsRaster(bool enabled);

double selectionTolerance() const;
void setSelectionTolerance( double tol );

/**Returns pointer to map renderer of qgis map canvas*/
QgsMapRenderer* mapRenderer();

Expand Down
11 changes: 11 additions & 0 deletions src/app/composer/qgscompositionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
{
mGridStyleComboBox->setCurrentIndex( 2 );
}

mSelectionToleranceSpinBox->setValue( mComposition->selectionTolerance() );
}
blockSignals( false );
}
Expand Down Expand Up @@ -503,6 +505,14 @@ void QgsCompositionWidget::on_mPenWidthSpinBox_valueChanged( double d )
}
}

void QgsCompositionWidget::on_mSelectionToleranceSpinBox_valueChanged( double d )
{
if ( mComposition )
{
mComposition->setSelectionTolerance( d );
}
}

void QgsCompositionWidget::blockSignals( bool block )
{
mPaperSizeComboBox->blockSignals( block );
Expand All @@ -519,4 +529,5 @@ void QgsCompositionWidget::blockSignals( bool block )
mPenWidthSpinBox->blockSignals( block );
mGridColorButton->blockSignals( block );
mGridStyleComboBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
}
2 changes: 2 additions & 0 deletions src/app/composer/qgscompositionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mGridColorButton_clicked();
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
void on_mPenWidthSpinBox_valueChanged( double d );
void on_mSelectionToleranceSpinBox_valueChanged( double d );

/**Sets GUI elements to width/height from composition*/
void displayCompositionWidthHeight();

Expand Down
19 changes: 19 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ void QgsComposerItem::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
return;
}

if ( !isSelected() )
{
return;
}

if ( mBoundingResizeRectangle )
{
double diffX = event->lastScenePos().x() - mLastMouseEventPos.x();
Expand All @@ -313,6 +318,11 @@ void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
return;
}

if ( !isSelected() )
{
return;
}

//set current position and type of mouse move action
mMouseMoveStartPos = event->lastScenePos();
mLastMouseEventPos = event->lastScenePos();
Expand Down Expand Up @@ -347,6 +357,11 @@ void QgsComposerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
return;
}

if ( !isSelected() )
{
return;
}

//delete frame rectangle
if ( mBoundingResizeRectangle )
{
Expand Down Expand Up @@ -728,6 +743,10 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
{
setCursor( cursorForPosition( event->pos() ) );
}
else
{
setCursor( Qt::ArrowCursor );
}
}

void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const
Expand Down
44 changes: 34 additions & 10 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#include <QSettings>

QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ), mSnapToGrid( false ),
mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ), mSelectionTolerance( 0.0 ),
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
{
setBackgroundBrush( Qt::gray );

Expand All @@ -36,14 +36,14 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
addItem( mPaperItem );
mPaperItem->setZValue( 0 );
mPrintResolution = 300; //hardcoded default
loadGridAppearanceSettings();
loadSettings();
}

QgsComposition::QgsComposition():
QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ),
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
{
loadGridAppearanceSettings();
loadSettings();
}

QgsComposition::~QgsComposition()
Expand Down Expand Up @@ -77,7 +77,16 @@ double QgsComposition::paperWidth() const

QgsComposerItem* QgsComposition::composerItemAt( const QPointF & position )
{
QList<QGraphicsItem *> itemList = items( position );
QList<QGraphicsItem*> itemList;
if ( mSelectionTolerance <= 0.0 )
{
itemList = items( position );
}
else
{
itemList = items( QRectF( position.x() - mSelectionTolerance, position.y() - mSelectionTolerance, 2 * mSelectionTolerance, 2 * mSelectionTolerance ),
Qt::IntersectsItemShape, Qt::DescendingOrder );
}
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();

for ( ; itemIt != itemList.end(); ++itemIt )
Expand Down Expand Up @@ -663,6 +672,7 @@ void QgsComposition::setSnapToGridEnabled( bool b )
{
mPaperItem->update();
}
saveSettings();
}

void QgsComposition::setSnapGridResolution( double r )
Expand All @@ -672,6 +682,7 @@ void QgsComposition::setSnapGridResolution( double r )
{
mPaperItem->update();
}
saveSettings();
}

void QgsComposition::setSnapGridOffsetX( double offset )
Expand All @@ -681,6 +692,7 @@ void QgsComposition::setSnapGridOffsetX( double offset )
{
mPaperItem->update();
}
saveSettings();
}

void QgsComposition::setSnapGridOffsetY( double offset )
Expand All @@ -690,6 +702,7 @@ void QgsComposition::setSnapGridOffsetY( double offset )
{
mPaperItem->update();
}
saveSettings();
}

void QgsComposition::setGridPen( const QPen& p )
Expand All @@ -699,7 +712,7 @@ void QgsComposition::setGridPen( const QPen& p )
{
mPaperItem->update();
}
saveGridAppearanceSettings();
saveSettings();
}

void QgsComposition::setGridStyle( GridStyle s )
Expand All @@ -709,10 +722,16 @@ void QgsComposition::setGridStyle( GridStyle s )
{
mPaperItem->update();
}
saveGridAppearanceSettings();
saveSettings();
}

void QgsComposition::setSelectionTolerance( double tol )
{
mSelectionTolerance = tol;
saveSettings();
}

void QgsComposition::loadGridAppearanceSettings()
void QgsComposition::loadSettings()
{
//read grid style, grid color and pen width from settings
QSettings s;
Expand Down Expand Up @@ -742,9 +761,11 @@ void QgsComposition::loadGridAppearanceSettings()
{
mGridStyle = Solid;
}

mSelectionTolerance = s.value( "/qgis/composerSelectionTolerance", 0.0 ).toDouble();
}

void QgsComposition::saveGridAppearanceSettings()
void QgsComposition::saveSettings()
{
//store grid appearance settings
QSettings s;
Expand All @@ -765,6 +786,9 @@ void QgsComposition::saveGridAppearanceSettings()
{
s.setValue( "/qgis/composerGridStyle", "Crosses" );
}

//store also selection tolerance
s.setValue( "/qgis/composerSelectionTolerance", mSelectionTolerance );
}

void QgsComposition::beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c )
Expand Down
10 changes: 8 additions & 2 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
bool printAsRaster() const {return mPrintAsRaster;}
void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }

double selectionTolerance() const { return mSelectionTolerance; }
void setSelectionTolerance( double tol );

/**Returns pointer to map renderer of qgis map canvas*/
QgsMapRenderer* mapRenderer() {return mMapRenderer;}

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

/**Distance tolerance for item selection (in mm)*/
double mSelectionTolerance;

/**Parameters for snap to grid function*/
bool mSnapToGrid;
double mSnapGridResolution;
Expand All @@ -207,8 +213,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
@return 0 in case of success*/
int boundingRectOfSelectedItems( QRectF& bRect );

void loadGridAppearanceSettings();
void saveGridAppearanceSettings();
void loadSettings();
void saveSettings();

signals:
void paperSizeChanged();
Expand Down
11 changes: 9 additions & 2 deletions src/ui/qgscompositionwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-11</y>
<width>397</width>
<y>0</y>
<width>395</width>
<height>579</height>
</rect>
</property>
Expand Down Expand Up @@ -281,6 +281,13 @@
<item row="8" column="0">
<widget class="QComboBox" name="mGridStyleComboBox"/>
</item>
<item row="9" column="0">
<widget class="QDoubleSpinBox" name="mSelectionToleranceSpinBox">
<property name="prefix">
<string>Selection tolerance (mm) </string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit e386ae4

Please sign in to comment.