Skip to content

Commit f938b60

Browse files
committedSep 10, 2017
Handle mouse events of adv.digitizing map tools in the map tool base class
1 parent ae713b4 commit f938b60

File tree

5 files changed

+83
-124
lines changed

5 files changed

+83
-124
lines changed
 

‎doc/api_break.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ QgsAction {#qgis_api_break_3_0_QgsAction}
474474
QgsAdvancedDigitizingDockWidget {#qgis_api_break_3_0_QgsAdvancedDigitizingDockWidget}
475475
-------------------------------
476476

477-
- canvasReleaseEvent() does not take second argument anymore. Map tools are expected to clear CAD points themselves whenever needed.
477+
- canvasPressEvent(), canvasReleaseEvent(), canvasMoveEvent() were removed. Handling of events is done in QgsMapToolAdvancedDigitizing.
478478
- snappingMode() was removed. Advanced digitizing now always uses project's snapping configuration.
479479

480480

‎python/gui/qgsadvanceddigitizingdockwidget.sip

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,46 +172,30 @@ class QgsAdvancedDigitizingDockWidget : QgsDockWidget
172172
Disables the CAD tools when hiding the dock
173173
%End
174174

175-
bool canvasPressEvent( QgsMapMouseEvent *e );
176-
%Docstring
177-
Will react on a canvas press event
178-
179-
\param e A mouse event (may be modified)
180-
:return: If the event is hidden (construction mode hides events from the maptool)
181-
:rtype: bool
182-
%End
183-
184-
bool canvasReleaseEvent( QgsMapMouseEvent *e );
175+
bool canvasKeyPressEventFilter( QKeyEvent *e );
185176
%Docstring
186-
Will react on a canvas release event
177+
Filter key events to e.g. toggle construction mode or adapt constraints
187178

188179
\param e A mouse event (may be modified)
189180
:return: If the event is hidden (construction mode hides events from the maptool)
190181
:rtype: bool
191182
%End
192183

193-
bool canvasMoveEvent( QgsMapMouseEvent *e );
184+
bool applyConstraints( QgsMapMouseEvent *e );
194185
%Docstring
195-
Will react on a canvas move event
196-
197-
\param e A mouse event (may be modified)
198-
:return: If the event is hidden (construction mode hides events from the maptool)
186+
:return: false if no solution was found (invalid constraints)
199187
:rtype: bool
200188
%End
201189

202-
bool canvasKeyPressEventFilter( QKeyEvent *e );
190+
bool alignToSegment( QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode = QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock );
203191
%Docstring
204-
Filter key events to e.g. toggle construction mode or adapt constraints
205-
206-
\param e A mouse event (may be modified)
207-
:return: If the event is hidden (construction mode hides events from the maptool)
192+
.. versionadded:: 3.0
208193
:rtype: bool
209194
%End
210195

211-
bool applyConstraints( QgsMapMouseEvent *e );
196+
void releaseLocks( bool releaseRepeatingLocks = true );
212197
%Docstring
213-
:return: false if no solution was found (invalid constraints)
214-
:rtype: bool
198+
.. versionadded:: 3.0
215199
%End
216200

217201
void clear();
@@ -269,6 +253,12 @@ Constraint on a common angle
269253
%Docstring
270254
Removes all points from the CAD point list
271255
.. versionadded:: 3.0
256+
%End
257+
258+
void addPoint( const QgsPointXY &point );
259+
%Docstring
260+
Adds point to the CAD point list
261+
.. versionadded:: 3.0
272262
%End
273263

274264
void setPoints( const QList<QgsPointXY> &points );
@@ -341,6 +331,11 @@ return the action used to enable/disable the tools
341331
Disable the widget. Normally done automatically from QgsMapToolAdvancedDigitizing.deactivate().
342332
%End
343333

334+
void updateCadPaintItem();
335+
%Docstring
336+
.. versionadded:: 3.0
337+
%End
338+
344339
signals:
345340

346341
void pushWarning( const QString &message );

‎src/gui/qgsadvanceddigitizingdockwidget.cpp

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,15 @@ bool QgsAdvancedDigitizingDockWidget::applyConstraints( QgsMapMouseEvent *e )
864864
}
865865
}
866866

867+
if ( res )
868+
{
869+
emit popWarning();
870+
}
871+
else
872+
{
873+
emit pushWarning( tr( "Some constraints are incompatible. Resulting point might be incorrect." ) );
874+
}
875+
867876
return res;
868877
}
869878

@@ -952,66 +961,6 @@ bool QgsAdvancedDigitizingDockWidget::alignToSegment( QgsMapMouseEvent *e, CadCo
952961
return true;
953962
}
954963

955-
bool QgsAdvancedDigitizingDockWidget::canvasPressEvent( QgsMapMouseEvent *e )
956-
{
957-
applyConstraints( e );
958-
return mCadEnabled && mConstructionMode;
959-
}
960-
961-
bool QgsAdvancedDigitizingDockWidget::canvasReleaseEvent( QgsMapMouseEvent *e )
962-
{
963-
if ( !mCadEnabled )
964-
return false;
965-
966-
emit popWarning();
967-
968-
if ( e->button() == Qt::RightButton )
969-
{
970-
clear();
971-
return false;
972-
}
973-
974-
applyConstraints( e );
975-
976-
if ( alignToSegment( e ) )
977-
{
978-
// launch a fake move event so rubber bands of map tools will be adapted with new constraints
979-
// emit pointChanged( e );
980-
981-
// Parallel or perpendicular mode and snapped to segment
982-
// this has emitted the lockAngle signal
983-
return true;
984-
}
985-
986-
addPoint( e->mapPoint() );
987-
988-
releaseLocks( false );
989-
990-
return mConstructionMode;
991-
}
992-
993-
bool QgsAdvancedDigitizingDockWidget::canvasMoveEvent( QgsMapMouseEvent *e )
994-
{
995-
if ( !mCadEnabled )
996-
return false;
997-
998-
if ( !applyConstraints( e ) )
999-
{
1000-
emit pushWarning( tr( "Some constraints are incompatible. Resulting point might be incorrect." ) );
1001-
}
1002-
else
1003-
{
1004-
emit popWarning();
1005-
}
1006-
1007-
// perpendicular/parallel constraint
1008-
// do a soft lock when snapping to a segment
1009-
alignToSegment( e, CadConstraint::SoftLock );
1010-
mCadPaintItem->update();
1011-
1012-
return false;
1013-
}
1014-
1015964
bool QgsAdvancedDigitizingDockWidget::canvasKeyPressEventFilter( QKeyEvent *e )
1016965
{
1017966
// event on map tool
@@ -1267,6 +1216,11 @@ void QgsAdvancedDigitizingDockWidget::disable()
12671216
setCadEnabled( false );
12681217
}
12691218

1219+
void QgsAdvancedDigitizingDockWidget::updateCadPaintItem()
1220+
{
1221+
mCadPaintItem->update();
1222+
}
1223+
12701224
void QgsAdvancedDigitizingDockWidget::addPoint( const QgsPointXY &point )
12711225
{
12721226
if ( !pointsCount() )

‎src/gui/qgsadvanceddigitizingdockwidget.h

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -204,30 +204,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
204204
*/
205205
void hideEvent( QHideEvent * ) override;
206206

207-
/**
208-
* Will react on a canvas press event
209-
*
210-
* \param e A mouse event (may be modified)
211-
* \returns If the event is hidden (construction mode hides events from the maptool)
212-
*/
213-
bool canvasPressEvent( QgsMapMouseEvent *e );
214-
215-
/**
216-
* Will react on a canvas release event
217-
*
218-
* \param e A mouse event (may be modified)
219-
* \returns If the event is hidden (construction mode hides events from the maptool)
220-
*/
221-
bool canvasReleaseEvent( QgsMapMouseEvent *e );
222-
223-
/**
224-
* Will react on a canvas move event
225-
*
226-
* \param e A mouse event (may be modified)
227-
* \returns If the event is hidden (construction mode hides events from the maptool)
228-
*/
229-
bool canvasMoveEvent( QgsMapMouseEvent *e );
230-
231207
/**
232208
* Filter key events to e.g. toggle construction mode or adapt constraints
233209
*
@@ -240,6 +216,16 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
240216
//! \returns false if no solution was found (invalid constraints)
241217
bool applyConstraints( QgsMapMouseEvent *e );
242218

219+
//! align to segment for additional constraint.
220+
//! If additional constraints are used, this will determine the angle to be locked depending on the snapped segment.
221+
//! \since QGIS 3.0
222+
bool alignToSegment( QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode = QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock );
223+
224+
//! unlock all constraints
225+
//! \param releaseRepeatingLocks set to false to preserve the lock for any constraints set to repeating lock mode
226+
//! \since QGIS 3.0
227+
void releaseLocks( bool releaseRepeatingLocks = true );
228+
243229
/**
244230
* Clear any cached previous clicks and helper lines
245231
*/
@@ -271,6 +257,11 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
271257
*/
272258
void clearPoints();
273259

260+
/** Adds point to the CAD point list
261+
* \since QGIS 3.0
262+
*/
263+
void addPoint( const QgsPointXY &point );
264+
274265
/**
275266
* Configures list of current CAD points
276267
*
@@ -332,6 +323,10 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
332323
*/
333324
void disable();
334325

326+
//! Updates canvas item that displays constraints on the ma
327+
//! \since QGIS 3.0
328+
void updateCadPaintItem();
329+
335330
signals:
336331

337332
/**
@@ -369,10 +364,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
369364
//! will be converted to their calculated value
370365
void constraintFocusOut();
371366

372-
//! unlock all constraints
373-
//! \param releaseRepeatingLocks set to false to preserve the lock for any constraints set to repeating lock mode
374-
void releaseLocks( bool releaseRepeatingLocks = true );
375-
376367
//! set the relative properties of constraints
377368
void setConstraintRelative( bool activate );
378369

@@ -404,10 +395,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
404395

405396
QList<QgsPointXY> snapSegment( const QgsPointLocator::Match &snapMatch );
406397

407-
//! align to segment for additional constraint.
408-
//! If additional constraints are used, this will determine the angle to be locked depending on the snapped segment.
409-
bool alignToSegment( QgsMapMouseEvent *e, CadConstraint::LockMode lockMode = CadConstraint::HardLock );
410-
411398
/**
412399
* Returns the first snapped segment. Will try to snap a segment according to the event's snapping mode.
413400
* \param originalMapPoint point to be snapped (in map coordinates)
@@ -416,8 +403,6 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
416403
*/
417404
QList<QgsPointXY> snapSegment( const QgsPointXY &originalMapPoint, bool *snapped = nullptr, bool allLayers = false ) const;
418405

419-
//! add point to the CAD point list
420-
void addPoint( const QgsPointXY &point );
421406
//! update the current point in the CAD point list
422407
void updateCurrentPoint( const QgsPointXY &point );
423408
//! remove previous point in the CAD point list

‎src/gui/qgsmaptooladvanceddigitizing.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
2828
{
2929
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
3030
{
31-
if ( mCadDockWidget->canvasPressEvent( e ) )
31+
mCadDockWidget->applyConstraints( e ); // updates event's map point
32+
33+
if ( mCadDockWidget->constructionMode() )
3234
return; // decided to eat the event and not pass it to the map tool (construction mode)
3335
}
3436
else if ( isAutoSnapEnabled() )
@@ -43,8 +45,27 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
4345
{
4446
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
4547
{
46-
if ( mCadDockWidget->canvasReleaseEvent( e ) )
47-
return; // decided to eat the event and not pass it to the map tool (construction mode or picking a segment)
48+
if ( e->button() == Qt::RightButton )
49+
{
50+
mCadDockWidget->clear();
51+
}
52+
else
53+
{
54+
mCadDockWidget->applyConstraints( e ); // updates event's map point
55+
56+
if ( mCadDockWidget->alignToSegment( e ) )
57+
{
58+
// Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
59+
return;
60+
}
61+
62+
mCadDockWidget->addPoint( e->mapPoint() );
63+
64+
mCadDockWidget->releaseLocks( false );
65+
66+
if ( mCadDockWidget->constructionMode() )
67+
return; // decided to eat the event and not pass it to the map tool (construction mode)
68+
}
4869
}
4970
else if ( isAutoSnapEnabled() )
5071
{
@@ -58,8 +79,12 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
5879
{
5980
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
6081
{
61-
if ( mCadDockWidget->canvasMoveEvent( e ) )
62-
return; // decided to eat the event and not pass it to the map tool (never happens currently)
82+
mCadDockWidget->applyConstraints( e ); // updates event's map point
83+
84+
// perpendicular/parallel constraint
85+
// do a soft lock when snapping to a segment
86+
mCadDockWidget->alignToSegment( e, QgsAdvancedDigitizingDockWidget::CadConstraint::SoftLock );
87+
mCadDockWidget->updateCadPaintItem();
6388
}
6489
else if ( isAutoSnapEnabled() )
6590
{

0 commit comments

Comments
 (0)
Please sign in to comment.