-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsattributetableview.cpp
499 lines (426 loc) · 17.4 KB
/
qgsattributetableview.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/***************************************************************************
QgsAttributeTableView.cpp
--------------------------------------
Date : Feb 2009
Copyright : (C) 2009 Vita Cizek
Email : weetya (at) gmail.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 <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QToolButton>
#include <QHBoxLayout>
#include "qgssettings.h"
#include "qgsactionmanager.h"
#include "qgsattributetableview.h"
#include "qgsattributetablemodel.h"
#include "qgsattributetabledelegate.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayercache.h"
#include "qgsvectorlayerselectionmanager.h"
#include "qgsvectordataprovider.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfeatureselectionmodel.h"
#include "qgsmaplayeractionregistry.h"
#include "qgsfeatureiterator.h"
#include "qgsgui.h"
QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
: QTableView( parent )
{
QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "BetterAttributeTable/geometry" ) ).toByteArray() );
//verticalHeader()->setDefaultSectionSize( 20 );
horizontalHeader()->setHighlightSections( false );
// We need mouse move events to create the action button on hover
mTableDelegate = new QgsAttributeTableDelegate( this );
setItemDelegate( mTableDelegate );
setEditTriggers( QAbstractItemView::AllEditTriggers );
setSelectionBehavior( QAbstractItemView::SelectRows );
setSelectionMode( QAbstractItemView::ExtendedSelection );
setSortingEnabled( true ); // At this point no data is in the model yet, so actually nothing is sorted.
horizontalHeader()->setSortIndicatorShown( false ); // So hide the indicator to avoid confusion.
setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );
verticalHeader()->viewport()->installEventFilter( this );
connect( verticalHeader(), &QHeaderView::sectionPressed, this, [ = ]( int row ) { selectRow( row, true ); } );
connect( verticalHeader(), &QHeaderView::sectionEntered, this, &QgsAttributeTableView::_q_selectRow );
connect( horizontalHeader(), &QHeaderView::sectionResized, this, &QgsAttributeTableView::columnSizeChanged );
connect( horizontalHeader(), &QHeaderView::sortIndicatorChanged, this, &QgsAttributeTableView::showHorizontalSortIndicator );
connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsAttributeTableView::recreateActionWidgets );
}
bool QgsAttributeTableView::eventFilter( QObject *object, QEvent *event )
{
if ( object == verticalHeader()->viewport() )
{
switch ( event->type() )
{
case QEvent::MouseButtonPress:
mFeatureSelectionModel->enableSync( false );
break;
case QEvent::MouseButtonRelease:
mFeatureSelectionModel->enableSync( true );
break;
default:
break;
}
}
return QTableView::eventFilter( object, event );
}
void QgsAttributeTableView::setAttributeTableConfig( const QgsAttributeTableConfig &config )
{
int i = 0;
const auto constColumns = config.columns();
for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
{
if ( columnConfig.hidden )
continue;
if ( columnConfig.width >= 0 )
{
setColumnWidth( i, columnConfig.width );
}
else
{
setColumnWidth( i, horizontalHeader()->defaultSectionSize() );
}
i++;
}
mConfig = config;
if ( config.sortExpression().isEmpty() )
horizontalHeader()->setSortIndicatorShown( false );
}
QList<QgsFeatureId> QgsAttributeTableView::selectedFeaturesIds() const
{
// In order to get the ids in the right sorted order based on the view we have to get the feature ids first
// from the selection manager which is in the order the user selected them when clicking
// then get the model index, sort that, and finally return the new sorted features ids.
const QgsFeatureIds featureIds = mFeatureSelectionManager->selectedFeatureIds();
QModelIndexList indexList;
for ( const QgsFeatureId &id : featureIds )
{
QModelIndex index = mFilterModel->fidToIndex( id );
indexList << index;
}
std::sort( indexList.begin(), indexList.end() );
QList<QgsFeatureId> ids;
for ( const QModelIndex &index : indexList )
{
QgsFeatureId id = mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
ids.append( id );
}
return ids;
}
void QgsAttributeTableView::setModel( QgsAttributeTableFilterModel *filterModel )
{
mFilterModel = filterModel;
QTableView::setModel( mFilterModel );
if ( mFilterModel )
{
connect( mFilterModel, &QObject::destroyed, this, &QgsAttributeTableView::modelDeleted );
connect( mTableDelegate, &QgsAttributeTableDelegate::actionColumnItemPainted, this, &QgsAttributeTableView::onActionColumnItemPainted );
}
delete mFeatureSelectionModel;
mFeatureSelectionModel = nullptr;
if ( mFilterModel )
{
if ( !mFeatureSelectionManager )
{
mOwnedFeatureSelectionManager = new QgsVectorLayerSelectionManager( mFilterModel->layer(), this );
mFeatureSelectionManager = mOwnedFeatureSelectionManager;
}
mFeatureSelectionModel = new QgsFeatureSelectionModel( mFilterModel, mFilterModel, mFeatureSelectionManager, mFilterModel );
setSelectionModel( mFeatureSelectionModel );
mTableDelegate->setFeatureSelectionModel( mFeatureSelectionModel );
connect( mFeatureSelectionModel, static_cast<void ( QgsFeatureSelectionModel::* )( const QModelIndexList &indexes )>( &QgsFeatureSelectionModel::requestRepaint ),
this, static_cast<void ( QgsAttributeTableView::* )( const QModelIndexList &indexes )>( &QgsAttributeTableView::repaintRequested ) );
connect( mFeatureSelectionModel, static_cast<void ( QgsFeatureSelectionModel::* )()>( &QgsFeatureSelectionModel::requestRepaint ),
this, static_cast<void ( QgsAttributeTableView::* )()>( &QgsAttributeTableView::repaintRequested ) );
connect( mFilterModel->layer(), &QgsVectorLayer::editingStarted, this, &QgsAttributeTableView::recreateActionWidgets );
connect( mFilterModel->layer(), &QgsVectorLayer::editingStopped, this, &QgsAttributeTableView::recreateActionWidgets );
connect( mFilterModel->layer(), &QgsVectorLayer::readOnlyChanged, this, &QgsAttributeTableView::recreateActionWidgets );
}
}
void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionManager *featureSelectionManager )
{
mFeatureSelectionManager = featureSelectionManager;
if ( mFeatureSelectionModel )
mFeatureSelectionModel->setFeatureSelectionManager( mFeatureSelectionManager );
// only delete the owner selection manager and not one created from outside
if ( mOwnedFeatureSelectionManager )
{
mOwnedFeatureSelectionManager->deleteLater();
mOwnedFeatureSelectionManager = nullptr;
}
}
QWidget *QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
{
QgsAttributeTableConfig attributeTableConfig = mConfig;
QToolButton *toolButton = nullptr;
QWidget *container = nullptr;
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
{
toolButton = new QToolButton();
toolButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
toolButton->setPopupMode( QToolButton::MenuButtonPopup );
container = toolButton;
}
else
{
container = new QWidget();
container->setLayout( new QHBoxLayout() );
container->layout()->setMargin( 0 );
}
QList< QAction * > actionList;
QAction *defaultAction = nullptr;
// first add user created layer actions
QList<QgsAction> actions = mFilterModel->layer()->actions()->actions( QStringLiteral( "Feature" ) );
const auto constActions = actions;
for ( const QgsAction &action : constActions )
{
if ( !mFilterModel->layer()->isEditable() && action.isEnabledOnlyWhenEditable() )
continue;
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : QString();
QAction *act = new QAction( action.icon(), actionTitle, container );
act->setToolTip( action.name() );
act->setData( "user_action" );
act->setProperty( "fid", fid );
act->setProperty( "action_id", action.id() );
connect( act, &QAction::triggered, this, &QgsAttributeTableView::actionTriggered );
actionList << act;
if ( mFilterModel->layer()->actions()->defaultAction( QStringLiteral( "Feature" ) ).id() == action.id() )
defaultAction = act;
}
const auto mapLayerActions {QgsGui::mapLayerActionRegistry()->mapLayerActions( mFilterModel->layer(), QgsMapLayerAction::SingleFeature ) };
// next add any registered actions for this layer
for ( QgsMapLayerAction *mapLayerAction : mapLayerActions )
{
QAction *action = new QAction( mapLayerAction->icon(), mapLayerAction->text(), container );
action->setData( "map_layer_action" );
action->setToolTip( mapLayerAction->text() );
action->setProperty( "fid", fid );
action->setProperty( "action", qVariantFromValue( qobject_cast<QObject *>( mapLayerAction ) ) );
connect( action, &QAction::triggered, this, &QgsAttributeTableView::actionTriggered );
actionList << action;
if ( !defaultAction &&
QgsGui::mapLayerActionRegistry()->defaultActionForLayer( mFilterModel->layer() ) == mapLayerAction )
defaultAction = action;
}
if ( !defaultAction && !actionList.isEmpty() )
defaultAction = actionList.at( 0 );
const auto constActionList = actionList;
for ( QAction *act : constActionList )
{
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
{
toolButton->addAction( act );
if ( act == defaultAction )
toolButton->setDefaultAction( act );
container = toolButton;
}
else
{
QToolButton *btn = new QToolButton;
btn->setDefaultAction( act );
container->layout()->addWidget( btn );
}
}
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::ButtonList )
{
static_cast< QHBoxLayout * >( container->layout() )->addStretch();
}
// TODO: Rethink default actions
#if 0
if ( toolButton && !toolButton->actions().isEmpty() && actions->defaultAction() == -1 )
toolButton->setDefaultAction( toolButton->actions().at( 0 ) );
#endif
return container;
}
void QgsAttributeTableView::closeEvent( QCloseEvent *e )
{
Q_UNUSED( e )
QgsSettings settings;
settings.setValue( QStringLiteral( "BetterAttributeTable/geometry" ), QVariant( saveGeometry() ) );
}
void QgsAttributeTableView::mousePressEvent( QMouseEvent *event )
{
setSelectionMode( QAbstractItemView::NoSelection );
QTableView::mousePressEvent( event );
setSelectionMode( QAbstractItemView::ExtendedSelection );
}
void QgsAttributeTableView::mouseReleaseEvent( QMouseEvent *event )
{
setSelectionMode( QAbstractItemView::NoSelection );
QTableView::mouseReleaseEvent( event );
setSelectionMode( QAbstractItemView::ExtendedSelection );
}
void QgsAttributeTableView::mouseMoveEvent( QMouseEvent *event )
{
setSelectionMode( QAbstractItemView::NoSelection );
QTableView::mouseMoveEvent( event );
setSelectionMode( QAbstractItemView::ExtendedSelection );
}
void QgsAttributeTableView::keyPressEvent( QKeyEvent *event )
{
switch ( event->key() )
{
// Default Qt behavior would be to change the selection.
// We don't make it that easy for the user to trash his selection.
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Left:
case Qt::Key_Right:
setSelectionMode( QAbstractItemView::NoSelection );
QTableView::keyPressEvent( event );
setSelectionMode( QAbstractItemView::ExtendedSelection );
break;
default:
QTableView::keyPressEvent( event );
break;
}
}
void QgsAttributeTableView::repaintRequested( const QModelIndexList &indexes )
{
const auto constIndexes = indexes;
for ( const QModelIndex &index : constIndexes )
{
update( index );
}
}
void QgsAttributeTableView::repaintRequested()
{
setDirtyRegion( viewport()->rect() );
}
void QgsAttributeTableView::selectAll()
{
QItemSelection selection;
selection.append( QItemSelectionRange( mFilterModel->index( 0, 0 ), mFilterModel->index( mFilterModel->rowCount() - 1, 0 ) ) );
mFeatureSelectionModel->selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
}
void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent *event )
{
delete mActionPopup;
mActionPopup = nullptr;
const QModelIndex idx = mFilterModel->mapToMaster( indexAt( event->pos() ) );
if ( !idx.isValid() )
{
return;
}
QgsVectorLayer *vlayer = mFilterModel->layer();
if ( !vlayer )
return;
mActionPopup = new QMenu( this );
QAction *selectAllAction = mActionPopup->addAction( tr( "Select All" ) );
selectAllAction->setShortcut( QKeySequence::SelectAll );
connect( selectAllAction, &QAction::triggered, this, &QgsAttributeTableView::selectAll );
// let some other parts of the application add some actions
emit willShowContextMenu( mActionPopup, idx );
if ( !mActionPopup->actions().isEmpty() )
{
mActionPopup->popup( event->globalPos() );
}
}
void QgsAttributeTableView::selectRow( int row )
{
selectRow( row, true );
}
void QgsAttributeTableView::_q_selectRow( int row )
{
selectRow( row, false );
}
void QgsAttributeTableView::modelDeleted()
{
mFilterModel = nullptr;
mFeatureSelectionManager = nullptr;
mFeatureSelectionModel = nullptr;
}
void QgsAttributeTableView::selectRow( int row, bool anchor )
{
if ( selectionBehavior() == QTableView::SelectColumns
|| ( selectionMode() == QTableView::SingleSelection
&& selectionBehavior() == QTableView::SelectItems ) )
return;
if ( row >= 0 && row < model()->rowCount() )
{
int column = horizontalHeader()->logicalIndexAt( isRightToLeft() ? viewport()->width() : 0 );
QModelIndex index = model()->index( row, column );
QItemSelectionModel::SelectionFlags command = selectionCommand( index );
selectionModel()->setCurrentIndex( index, QItemSelectionModel::NoUpdate );
if ( ( anchor && !( command & QItemSelectionModel::Current ) )
|| ( selectionMode() == QTableView::SingleSelection ) )
mRowSectionAnchor = row;
if ( selectionMode() != QTableView::SingleSelection
&& command.testFlag( QItemSelectionModel::Toggle ) )
{
if ( anchor )
mCtrlDragSelectionFlag = mFeatureSelectionModel->isSelected( index )
? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
command &= ~QItemSelectionModel::Toggle;
command |= mCtrlDragSelectionFlag;
if ( !anchor )
command |= QItemSelectionModel::Current;
}
QModelIndex tl = model()->index( std::min( mRowSectionAnchor, row ), 0 );
QModelIndex br = model()->index( std::max( mRowSectionAnchor, row ), model()->columnCount() - 1 );
if ( verticalHeader()->sectionsMoved() && tl.row() != br.row() )
setSelection( visualRect( tl ) | visualRect( br ), command );
else
mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
}
}
void QgsAttributeTableView::showHorizontalSortIndicator()
{
horizontalHeader()->setSortIndicatorShown( true );
}
void QgsAttributeTableView::actionTriggered()
{
QAction *action = qobject_cast<QAction *>( sender() );
QgsFeatureId fid = action->property( "fid" ).toLongLong();
QgsFeature f;
mFilterModel->layerCache()->getFeatures( QgsFeatureRequest( fid ) ).nextFeature( f );
if ( action->data().toString() == QLatin1String( "user_action" ) )
{
mFilterModel->layer()->actions()->doAction( action->property( "action_id" ).toString(), f );
}
else if ( action->data().toString() == QLatin1String( "map_layer_action" ) )
{
QObject *object = action->property( "action" ).value<QObject *>();
QgsMapLayerAction *layerAction = qobject_cast<QgsMapLayerAction *>( object );
if ( layerAction )
{
layerAction->triggerForFeature( mFilterModel->layer(), f );
}
}
}
void QgsAttributeTableView::columnSizeChanged( int index, int oldWidth, int newWidth )
{
Q_UNUSED( oldWidth )
emit columnResized( index, newWidth );
}
void QgsAttributeTableView::onActionColumnItemPainted( const QModelIndex &index )
{
if ( !indexWidget( index ) )
{
QWidget *widget = createActionWidget( mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
mActionWidgets.insert( index, widget );
setIndexWidget( index, widget );
}
}
void QgsAttributeTableView::recreateActionWidgets()
{
QMap< QModelIndex, QWidget * >::const_iterator it = mActionWidgets.constBegin();
for ( ; it != mActionWidgets.constEnd(); ++it )
{
// ownership of widget was transferred by initial call to setIndexWidget - clearing
// the index widget will delete the old widget safely
// they should then be recreated by onActionColumnItemPainted
setIndexWidget( it.key(), nullptr );
}
mActionWidgets.clear();
}