Skip to content

Commit 9a8b7c5

Browse files
committed
better message display in map edit tools (fix #8873) emit signal from map tool, display message in qgisapp
1 parent 1c0d5e2 commit 9a8b7c5

18 files changed

+126
-46
lines changed

python/gui/qgsmapcanvas.sip

+4
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ class QgsMapCanvas : QGraphicsView
354354

355355
//! Emit map tool changed event
356356
void mapToolSet( QgsMapTool *tool );
357+
358+
//! Emit map tool changed with the old tool
359+
//! @note added in 2.3
360+
void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
357361

358362
// ### QGIS 3: remove the signal
359363
//! Emitted when selection in any layer gets changed

python/gui/qgsmaptool.sip

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ class QgsMapTool : QObject
105105

106106
//! returns pointer to the tool's map canvas
107107
QgsMapCanvas* canvas();
108+
109+
/** return the tool name
110+
* @note added in 2.3
111+
*/
112+
QString toolName();
108113

109114
protected:
110115

src/app/nodetool/qgsmaptoolnodetool.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
362362

363363
if ( snapResults.size() < 1 )
364364
{
365-
displaySnapToleranceWarning();
365+
emit displayMessage( tr( "could not snap to a segment on the current layer." ) );
366366
return;
367367
}
368368

369+
// remove previous warning
370+
emit removeMessage();
371+
369372
mSelectedFeature = new QgsSelectedFeature( snapResults[0].snappedAtGeometry, vlayer, mCanvas );
370373
connect( QgisApp::instance()->legend(), SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( currentLayerChanged( QgsMapLayer* ) ) );
371374
connect( mSelectedFeature, SIGNAL( destroyed() ), this, SLOT( selectedFeatureDestroyed() ) );
@@ -374,6 +377,9 @@ void QgsMapToolNodeTool::canvasPressEvent( QMouseEvent * e )
374377
}
375378
else
376379
{
380+
// remove previous warning
381+
emit removeMessage();
382+
377383
QgsVectorLayer *vlayer = mSelectedFeature->vlayer();
378384
Q_ASSERT( vlayer );
379385

src/app/qgisapp.cpp

+45-8
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
579579
mpGpsDock->setWidget( mpGpsWidget );
580580
mpGpsDock->hide();
581581

582+
mLastMapToolMessage = 0;
583+
582584
mLogViewer = new QgsMessageLogViewer( statusBar(), this );
583585

584586
mLogDock = new QDockWidget( tr( "Log Messages" ), this );
@@ -1890,8 +1892,8 @@ void QgisApp::setupConnections()
18901892
this, SLOT( showScale( double ) ) );
18911893
connect( mMapCanvas, SIGNAL( scaleChanged( double ) ),
18921894
this, SLOT( updateMouseCoordinatePrecision() ) );
1893-
connect( mMapCanvas, SIGNAL( mapToolSet( QgsMapTool * ) ),
1894-
this, SLOT( mapToolChanged( QgsMapTool * ) ) );
1895+
connect( mMapCanvas, SIGNAL( mapToolSet( QgsMapTool *, QgsMapTool * ) ),
1896+
this, SLOT( mapToolChanged( QgsMapTool *, QgsMapTool * ) ) );
18951897
connect( mMapCanvas, SIGNAL( selectionChanged( QgsMapLayer * ) ),
18961898
this, SLOT( selectionChanged( QgsMapLayer * ) ) );
18971899
connect( mMapCanvas, SIGNAL( extentsChanged() ),
@@ -2006,9 +2008,9 @@ void QgisApp::createCanvasTools()
20062008
mMapTools.mMoveFeature->setAction( mActionMoveFeature );
20072009
mMapTools.mRotateFeature = new QgsMapToolRotateFeature( mMapCanvas );
20082010
mMapTools.mRotateFeature->setAction( mActionRotateFeature );
2009-
//need at least geos 3.3 for OffsetCurve tool
2011+
//need at least geos 3.3 for OffsetCurve tool
20102012
#if defined(GEOS_VERSION_MAJOR) && defined(GEOS_VERSION_MINOR) && \
2011-
((GEOS_VERSION_MAJOR>3) || ((GEOS_VERSION_MAJOR==3) && (GEOS_VERSION_MINOR>=3)))
2013+
((GEOS_VERSION_MAJOR>3) || ((GEOS_VERSION_MAJOR==3) && (GEOS_VERSION_MINOR>=3)))
20122014
mMapTools.mOffsetCurve = new QgsMapToolOffsetCurve( mMapCanvas );
20132015
mMapTools.mOffsetCurve->setAction( mActionOffsetCurve );
20142016
#else
@@ -2059,7 +2061,7 @@ void QgisApp::createCanvasTools()
20592061
mMapTools.mRotateLabel->setAction( mActionRotateLabel );
20602062
mMapTools.mChangeLabelProperties = new QgsMapToolChangeLabelProperties( mMapCanvas );
20612063
mMapTools.mChangeLabelProperties->setAction( mActionChangeLabelProperties );
2062-
//ensure that non edit tool is initialised or we will get crashes in some situations
2064+
//ensure that non edit tool is initialised or we will get crashes in some situations
20632065
mNonEditMapTool = mMapTools.mPan;
20642066
}
20652067

@@ -8020,11 +8022,25 @@ void QgisApp::showProgress( int theProgress, int theTotalSteps )
80208022
}
80218023
}
80228024

8023-
void QgisApp::mapToolChanged( QgsMapTool *tool )
8025+
void QgisApp::mapToolChanged( QgsMapTool *newTool, QgsMapTool *oldTool )
80248026
{
8025-
if ( tool && !tool->isEditTool() )
8027+
if ( oldTool )
80268028
{
8027-
mNonEditMapTool = tool;
8029+
disconnect( oldTool, SIGNAL( displayMessage( QString ) ), this, SLOT( displayMapToolMessage( QString ) ) );
8030+
disconnect( oldTool, SIGNAL( displayMessage( QString, QgsMessageBar::MessageLevel ) ), this, SLOT( displayMapToolMessage( QString, QgsMessageBar::MessageLevel ) ) );
8031+
disconnect( oldTool, SIGNAL( removeMessage() ), this, SLOT( removeMapToolMessage() ) );
8032+
}
8033+
8034+
if ( newTool )
8035+
{
8036+
if ( !newTool->isEditTool() )
8037+
{
8038+
mNonEditMapTool = newTool;
8039+
}
8040+
8041+
connect( newTool, SIGNAL( displayMessage( QString ) ), this, SLOT( displayMapToolMessage( QString ) ) );
8042+
connect( newTool, SIGNAL( displayMessage( QString, QgsMessageBar::MessageLevel ) ), this, SLOT( displayMapToolMessage( QString, QgsMessageBar::MessageLevel ) ) );
8043+
connect( newTool, SIGNAL( removeMessage() ), this, SLOT( removeMapToolMessage() ) );
80288044
}
80298045
}
80308046

@@ -8151,6 +8167,27 @@ void QgisApp::showStatusMessage( QString theMessage )
81518167
statusBar()->showMessage( theMessage );
81528168
}
81538169

8170+
void QgisApp::displayMapToolMessage( QString message, QgsMessageBar::MessageLevel level )
8171+
{
8172+
// remove previous message
8173+
messageBar()->popWidget( mLastMapToolMessage );
8174+
8175+
QgsMapTool* tool = mapCanvas()->mapTool();
8176+
8177+
if ( tool )
8178+
{
8179+
mLastMapToolMessage = new QgsMessageBarItem( tool->toolName(), message, level, messageTimeout() );
8180+
QgisApp::instance()->messageBar()->pushItem( mLastMapToolMessage );
8181+
}
8182+
}
8183+
8184+
void QgisApp::removeMapToolMessage()
8185+
{
8186+
// remove previous message
8187+
messageBar()->popWidget( mLastMapToolMessage );
8188+
}
8189+
8190+
81548191
// Show the maptip using tooltip
81558192
void QgisApp::showMapTip()
81568193
{

src/app/qgisapp.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class QgsTileScaleWidget;
9292
#include "qgsrasterlayer.h"
9393
#include "qgssnappingdialog.h"
9494
#include "qgspluginmanager.h"
95+
#include "qgsmessagebar.h"
9596

9697
#include "ui_qgisapp.h"
9798

@@ -999,7 +1000,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
9991000
void layerSubsetString();
10001001

10011002
//! map tool changed
1002-
void mapToolChanged( QgsMapTool *tool );
1003+
void mapToolChanged( QgsMapTool *newTool , QgsMapTool* oldTool );
10031004

10041005
/** Called when some layer's editing mode was toggled on/off
10051006
* @note added in 1.9 */
@@ -1015,6 +1016,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
10151016
void extentsViewToggled( bool theFlag );
10161017
void showExtents();
10171018
void showStatusMessage( QString theMessage );
1019+
void displayMapToolMessage( QString message, QgsMessageBar::MessageLevel level = QgsMessageBar::INFO );
1020+
void removeMapToolMessage();
10181021
void updateMouseCoordinatePrecision();
10191022
void hasCrsTransformEnabled( bool theFlag );
10201023
void destinationCrsChanged();
@@ -1522,6 +1525,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15221525
//! Persistent GPS toolbox
15231526
QgsGPSInformationWidget * mpGpsWidget;
15241527

1528+
QgsMessageBarItem* mLastMapToolMessage;
1529+
15251530
QgsMessageLogViewer *mLogViewer;
15261531

15271532
//! project changed
@@ -1541,7 +1546,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15411546
bool gestureEvent( QGestureEvent *event );
15421547
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
15431548
#endif
1544-
15451549
};
15461550

15471551
#ifdef ANDROID

src/app/qgsmaptooladdpart.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
6262

6363
if ( !selectionErrorMsg.isEmpty() )
6464
{
65-
QMessageBox::critical( 0, tr( "Error. Could not add part." ), selectionErrorMsg );
65+
emit displayMessage( tr( "Could not add part. %1" ).arg( selectionErrorMsg ) , QgsMessageBar::WARNING );
6666
stopCapturing();
6767
return;
6868
}
@@ -101,9 +101,7 @@ void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
101101
else if ( error == 2 )
102102
{
103103
//problem with coordinate transformation
104-
QMessageBox::information( 0,
105-
tr( "Coordinate transform error" ),
106-
tr( "Cannot transform the point to the layers coordinate system" ) );
104+
emit displayMessage( tr( "Coordinate transform error. Cannot transform the point to the layers coordinate system" ) , QgsMessageBar::WARNING );
107105
return;
108106
}
109107

@@ -156,6 +154,9 @@ void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
156154
{
157155
case 0:
158156
{
157+
// remove previous message
158+
emit removeMessage();
159+
159160
//add points to other features to keep topology up-to-date
160161
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
161162
if ( topologicalEditing )
@@ -194,6 +195,6 @@ void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
194195
break;
195196
}
196197

197-
QMessageBox::critical( 0, tr( "Error, could not add part" ), errorMessage );
198+
emit displayMessage( errorMessage , QgsMessageBar::WARNING );
198199
vlayer->destroyEditCommand();
199200
}

src/app/qgsmaptooldeletepart.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ void QgsMapToolDeletePart::canvasPressEvent( QMouseEvent *e )
5252

5353
if ( mRecentSnappingResults.size() > 0 )
5454
{
55+
// remove previous warning
56+
emit removeMessage();
57+
5558
QgsPoint markerPoint = mRecentSnappingResults.begin()->snappedVertex;
5659

5760
//show vertex marker
@@ -61,7 +64,7 @@ void QgsMapToolDeletePart::canvasPressEvent( QMouseEvent *e )
6164
}
6265
else
6366
{
64-
displaySnapToleranceWarning();
67+
emit displayMessage( tr( "could not snap to a part on the current layer." ) );
6568
}
6669
}
6770

src/app/qgsmaptooldeletering.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include <QMessageBox>
2424

2525
QgsMapToolDeleteRing::QgsMapToolDeleteRing( QgsMapCanvas* canvas )
26-
: QgsMapToolVertexEdit( canvas ), mCross( 0 )
26+
: QgsMapToolVertexEdit( canvas )
27+
, mCross( 0 )
2728
{
29+
mToolName = tr( "Delete ring" );
2830
}
2931

3032
QgsMapToolDeleteRing::~QgsMapToolDeleteRing()
@@ -52,6 +54,9 @@ void QgsMapToolDeleteRing::canvasPressEvent( QMouseEvent *e )
5254

5355
if ( mRecentSnappingResults.size() > 0 )
5456
{
57+
// remove previous warning
58+
emit removeMessage();
59+
5560
QgsPoint markerPoint = mRecentSnappingResults.begin()->snappedVertex;
5661

5762
//show vertex marker
@@ -61,7 +66,7 @@ void QgsMapToolDeleteRing::canvasPressEvent( QMouseEvent *e )
6166
}
6267
else
6368
{
64-
displaySnapToleranceWarning();
69+
emit displayMessage( tr( "could not snap to a ring on the current layer." ) );
6570
}
6671
}
6772

src/app/qgsmaptooledit.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
***************************************************************************/
1515

1616
#include "qgsmaptooledit.h"
17-
#include "qgisapp.h"
1817
#include "qgsmessagebar.h"
1918
#include "qgsproject.h"
2019
#include "qgsmapcanvas.h"
2120
#include "qgsrubberband.h"
2221
#include "qgsvectorlayer.h"
22+
2323
#include <QKeyEvent>
2424
#include <QSettings>
2525

@@ -134,18 +134,10 @@ int QgsMapToolEdit::addTopologicalPoints( const QList<QgsPoint>& geom )
134134

135135
void QgsMapToolEdit::notifyNotVectorLayer()
136136
{
137-
QgisApp::instance()->messageBar()->pushMessage(
138-
tr( "No active vector layer" ),
139-
tr( "Choose a vector layer in the legend" ),
140-
QgsMessageBar::INFO,
141-
QgisApp::instance()->messageTimeout() );
137+
emit displayMessage( tr( "No active vector layer" ) );
142138
}
143139

144140
void QgsMapToolEdit::notifyNotEditableLayer()
145141
{
146-
QgisApp::instance()->messageBar()->pushMessage(
147-
tr( "Layer not editable" ),
148-
tr( "Use 'Toggle Editing' to make it editable" ),
149-
QgsMessageBar::INFO,
150-
QgisApp::instance()->messageTimeout() );
142+
emit displayMessage( tr( "Layer not editable" ) );
151143
}

src/app/qgsmaptooledit.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class APP_EXPORT QgsMapToolEdit: public QgsMapTool
3636
and applies it to the map canvas*/
3737
QgsMapCanvasSnapper mSnapper;
3838

39+
/**keeps trace of last displayed message*/
40+
QgsMessageBarItem* mMessageItem;
41+
3942
/**Inserts vertices to the snapped segments of the editing layer.
4043
This is useful for topological editing if snap to segment is enabled.
4144
@param snapResults results collected from the snapping operation
@@ -72,7 +75,6 @@ class APP_EXPORT QgsMapToolEdit: public QgsMapTool
7275
/**Display a timed message bar noting the active vector layer is not editable.
7376
@note added in QGIS 1.9*/
7477
void notifyNotEditableLayer();
75-
7678
};
7779

7880
#endif

src/app/qgsmaptoolvertexedit.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,3 @@ QgsMapToolVertexEdit::~QgsMapToolVertexEdit()
3939
{
4040

4141
}
42-
43-
void QgsMapToolVertexEdit::displaySnapToleranceWarning()
44-
{
45-
QgisApp::instance()->messageBar()->pushMessage(
46-
tr( "Snap tolerance" ),
47-
tr( "Could not snap segment. Have you set the tolerance in Settings > Snapping Options?" ),
48-
QgsMessageBar::INFO,
49-
QgisApp::instance()->messageTimeout() );
50-
}

src/app/qgsmaptoolvertexedit.h

-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class APP_EXPORT QgsMapToolVertexEdit: public QgsMapToolEdit
3838
/**Snapping results that are collected during the mouse press event
3939
(search for vertices/segments to manipulate)*/
4040
QList<QgsSnappingResult> mRecentSnappingResults;
41-
42-
//! Displays a warning about the snap tolerance settings
43-
void displaySnapToleranceWarning();
4441
};
4542

4643
#endif

src/gui/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ qgsmanageconnectionsdialog.h
239239
qgsmapcanvas.h
240240
qgsmaplayeractionregistry.h
241241
qgsmapoverviewcanvas.h
242+
qgsmaptool.h
242243
qgsmaptoolemitpoint.h
243244
qgsmaptoolidentify.h
244245
qgsmessagebaritem.h

src/gui/qgsmapcanvas.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,8 @@ void QgsMapCanvas::setMapTool( QgsMapTool* tool )
14291429
mLastNonZoomMapTool = NULL;
14301430
}
14311431

1432+
QgsMapTool* oldTool = mMapTool;
1433+
14321434
// set new map tool and activate it
14331435
mMapTool = tool;
14341436
if ( mMapTool )
@@ -1438,6 +1440,7 @@ void QgsMapCanvas::setMapTool( QgsMapTool* tool )
14381440
}
14391441

14401442
emit mapToolSet( mMapTool );
1443+
emit mapToolSet( mMapTool, oldTool );
14411444
} // setMapTool
14421445

14431446
void QgsMapCanvas::unsetMapTool( QgsMapTool* tool )
@@ -1447,6 +1450,7 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool* tool )
14471450
mMapTool->deactivate();
14481451
mMapTool = NULL;
14491452
emit mapToolSet( NULL );
1453+
emit mapToolSet( NULL, mMapTool );
14501454
setCursor( Qt::ArrowCursor );
14511455
}
14521456

src/gui/qgsmapcanvas.h

+5
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
435435
//! Emit map tool changed event
436436
void mapToolSet( QgsMapTool *tool );
437437

438+
/** Emit map tool changed with the old tool
439+
* @note added in 2.3
440+
*/
441+
void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
442+
438443
// ### QGIS 3: remove the signal
439444
//! Emitted when selection in any layer gets changed
440445
void selectionChanged( QgsMapLayer * layer );

0 commit comments

Comments
 (0)