/
qgsmaptoolshowhidelabels.cpp
312 lines (258 loc) · 8.55 KB
/
qgsmaptoolshowhidelabels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/***************************************************************************
qgsmaptoolshowhidelabels.cpp
-----------------------
begin : 2012-08-12
copyright : (C) 2012 by Larry Shaffer
email : larrys at dakotacarto dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmaptoolshowhidelabels.h"
#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayer.h"
#include "qgsmaptoolselectutils.h"
#include "qgsrubberband.h"
#include <qgslogger.h>
#include <QMouseEvent>
QgsMapToolShowHideLabels::QgsMapToolShowHideLabels( QgsMapCanvas* canvas )
: QgsMapToolLabel( canvas )
, mDragging( false )
{
mToolName = tr( "Show/hide labels" );
mRubberBand = nullptr;
}
QgsMapToolShowHideLabels::~QgsMapToolShowHideLabels()
{
delete mRubberBand;
}
void QgsMapToolShowHideLabels::canvasPressEvent( QgsMapMouseEvent* e )
{
Q_UNUSED( e );
mSelectRect.setRect( 0, 0, 0, 0 );
mSelectRect.setTopLeft( e->pos() );
mSelectRect.setBottomRight( e->pos() );
mRubberBand = new QgsRubberBand( mCanvas, QGis::Polygon );
}
void QgsMapToolShowHideLabels::canvasMoveEvent( QgsMapMouseEvent* e )
{
if ( e->buttons() != Qt::LeftButton )
return;
if ( !mDragging )
{
mDragging = true;
mSelectRect.setTopLeft( e->pos() );
}
mSelectRect.setBottomRight( e->pos() );
QgsMapToolSelectUtils::setRubberBand( mCanvas, mSelectRect, mRubberBand );
}
void QgsMapToolShowHideLabels::canvasReleaseEvent( QgsMapMouseEvent* e )
{
//if the user simply clicked without dragging a rect
//we will fabricate a small 1x1 pix rect and then continue
//as if they had dragged a rect
if ( !mDragging )
{
mSelectRect.setLeft( e->pos().x() - 1 );
mSelectRect.setRight( e->pos().x() + 1 );
mSelectRect.setTop( e->pos().y() - 1 );
mSelectRect.setBottom( e->pos().y() + 1 );
}
else
{
// Set valid values for rectangle's width and height
if ( mSelectRect.width() == 1 )
{
mSelectRect.setLeft( mSelectRect.left() + 1 );
}
if ( mSelectRect.height() == 1 )
{
mSelectRect.setBottom( mSelectRect.bottom() + 1 );
}
}
if ( mRubberBand )
{
QgsMapToolSelectUtils::setRubberBand( mCanvas, mSelectRect, mRubberBand );
showHideLabels( e );
mRubberBand->reset( QGis::Polygon );
delete mRubberBand;
mRubberBand = nullptr;
}
mDragging = false;
}
void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent * e )
{
QgsMapLayer* layer = mCanvas->currentLayer();
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
if ( !vlayer )
{
QgsDebugMsg( "Failed to cast label layer to vector layer" );
return;
}
if ( !vlayer->isEditable() )
{
QgsDebugMsg( "Vector layer not editable, skipping label" );
return;
}
bool doHide = e->modifiers() & Qt::ShiftModifier;
bool labelChanged = false;
QString editTxt = doHide ? tr( "Hid labels" ) : tr( "Showed labels" );
vlayer->beginEditCommand( editTxt );
if ( !doHide )
{
QgsDebugMsg( "Showing labels operation" );
QgsFeatureIds selectedFeatIds;
if ( !selectedFeatures( vlayer, selectedFeatIds ) )
{
vlayer->destroyEditCommand();
return;
}
QgsDebugMsg( "Number of selected labels or features: " + QString::number( selectedFeatIds.size() ) );
if ( selectedFeatIds.isEmpty() )
{
vlayer->destroyEditCommand();
return;
}
Q_FOREACH ( QgsFeatureId fid, selectedFeatIds )
{
mCurrentLabel.pos.featureId = fid;
mCurrentLabel.pos.isDiagram = false;
bool labChanged = showHide( vlayer, true );
mCurrentLabel.pos.isDiagram = true;
bool diagChanged = showHide( vlayer, true );
if ( labChanged || diagChanged )
{
// TODO: highlight features (maybe with QTimer?)
labelChanged = labelChanged || true;
}
}
}
else
{
QgsDebugMsg( "Hiding labels operation" );
QList<QgsLabelPosition> positions;
if ( selectedLabelFeatures( vlayer, positions ) )
{
Q_FOREACH ( QgsLabelPosition pos, positions )
{
mCurrentLabel.pos = pos;
if ( showHide( vlayer, false ) )
labelChanged = labelChanged || true;
}
}
}
if ( labelChanged )
{
vlayer->endEditCommand();
mCanvas->refresh();
}
else
{
vlayer->destroyEditCommand();
}
}
bool QgsMapToolShowHideLabels::selectedFeatures( QgsVectorLayer* vlayer,
QgsFeatureIds& selectedFeatIds )
{
// culled from QgsMapToolSelectUtils::setSelectFeatures()
QgsGeometry* selectGeometry = mRubberBand->asGeometry();
// toLayerCoordinates will throw an exception for any 'invalid' points in
// the rubber band.
// For example, if you project a world map onto a globe using EPSG 2163
// and then click somewhere off the globe, an exception will be thrown.
QgsGeometry selectGeomTrans( *selectGeometry );
if ( mCanvas->hasCrsTransformEnabled() )
{
try
{
QgsCoordinateTransform ct( mCanvas->mapSettings().destinationCrs(), vlayer->crs() );
selectGeomTrans.transform( ct );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
// catch exception for 'invalid' point and leave existing selection unchanged
QgsLogger::warning( "Caught CRS exception " + QString( __FILE__ ) + ": " + QString::number( __LINE__ ) );
emit messageEmitted( tr( "CRS Exception: selection extends beyond layer's coordinate system." ), QgsMessageBar::WARNING );
return false;
}
}
QApplication::setOverrideCursor( Qt::WaitCursor );
QgsDebugMsg( "Selection layer: " + vlayer->name() );
QgsDebugMsg( "Selection polygon: " + selectGeomTrans.exportToWkt() );
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest()
.setFilterRect( selectGeomTrans.boundingBox() )
.setFlags( QgsFeatureRequest::NoGeometry | QgsFeatureRequest::ExactIntersect )
.setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
selectedFeatIds.insert( f.id() );
}
QApplication::restoreOverrideCursor();
return true;
}
bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer* vlayer,
QList<QgsLabelPosition> &listPos )
{
listPos.clear();
// get list of all drawn labels from current layer that intersect rubberband
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
if ( !labelingResults )
{
QgsDebugMsg( "No labeling engine" );
return false;
}
QApplication::setOverrideCursor( Qt::WaitCursor );
QgsRectangle ext = mRubberBand->asGeometry()->boundingBox();
QList<QgsLabelPosition> labelPosList = labelingResults->labelsWithinRect( ext );
QList<QgsLabelPosition>::const_iterator it;
for ( it = labelPosList.constBegin() ; it != labelPosList.constEnd(); ++it )
{
const QgsLabelPosition& pos = *it;
if ( pos.layerID != vlayer->id() )
{
// only work with labels from the current active and editable layer
continue;
}
listPos.append( pos );
}
QApplication::restoreOverrideCursor();
return true;
}
bool QgsMapToolShowHideLabels::showHide( QgsVectorLayer *vl, const bool show )
{
// verify attribute table has proper field setup
bool showSuccess;
int showCol;
int showVal;
if ( !dataDefinedShowHide( vl, mCurrentLabel.pos.featureId, showVal,
showSuccess, showCol ) )
{
return false;
}
// check if attribute value is already the same
if ( showSuccess && ( showVal != 0 ) == show )
{
return false;
}
// allow NULL (maybe default) value to stand for show label (i.e. 1)
// skip NULL attributes if trying to show label
if ( !showSuccess && show == 1 )
{
return false;
}
// different attribute value, edit table
if ( ! vl->changeAttributeValue( mCurrentLabel.pos.featureId, showCol, show ) )
{
QgsDebugMsg( "Failed write to attribute table" );
return false;
}
return true;
}