18 #include <QHeaderView>
34 : QTableView( parent )
35 , mMasterModel( NULL )
36 , mFilterModel( NULL )
37 , mFeatureSelectionModel( NULL )
38 , mFeatureSelectionManager( NULL )
40 , mActionPopup( NULL )
42 , mRowSectionAnchor( 0 )
43 , mCtrlDragSelectionFlag( QItemSelectionModel::Select )
46 restoreGeometry( settings.value(
"/BetterAttributeTable/geometry" ).toByteArray() );
48 verticalHeader()->setDefaultSectionSize( 20 );
49 horizontalHeader()->setHighlightSections(
false );
52 setItemDelegate( mTableDelegate );
54 setSelectionBehavior( QAbstractItemView::SelectRows );
55 setSelectionMode( QAbstractItemView::ExtendedSelection );
56 setSortingEnabled(
true );
58 verticalHeader()->viewport()->installEventFilter(
this );
60 connect( verticalHeader(), SIGNAL( sectionPressed(
int ) ),
this, SLOT(
selectRow(
int ) ) );
61 connect( verticalHeader(), SIGNAL( sectionEntered(
int ) ),
this, SLOT(
_q_selectRow(
int ) ) );
76 mLayerCache = layerCache;
82 delete mFeatureSelectionModel;
84 connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ),
this, SLOT(
repaintRequested( QModelIndexList ) ) );
85 connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ),
this, SLOT(
repaintRequested() ) );
86 setSelectionModel( mFeatureSelectionModel );
94 if (
object == verticalHeader()->viewport() )
96 switch ( event->type() )
98 case QEvent::MouseButtonPress:
102 case QEvent::MouseButtonRelease:
118 disconnect( mFilterModel, SIGNAL( filterAboutToBeInvalidated() ),
this, SLOT( onFilterAboutToBeInvalidated() ) );
119 disconnect( mFilterModel, SIGNAL( filterInvalidated() ),
this, SLOT( onFilterInvalidated() ) );
122 mFilterModel = filterModel;
125 connect( mFilterModel, SIGNAL( destroyed() ),
this, SLOT( modelDeleted() ) );
127 delete mFeatureSelectionModel;
128 mFeatureSelectionModel = 0;
132 if ( !mFeatureSelectionManager )
137 mFeatureSelectionModel =
new QgsFeatureSelectionModel( mFilterModel, mFilterModel, mFeatureSelectionManager, mFilterModel );
138 setSelectionModel( mFeatureSelectionModel );
140 connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ),
this, SLOT(
repaintRequested( QModelIndexList ) ) );
141 connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ),
this, SLOT(
repaintRequested() ) );
147 if ( mFeatureSelectionManager )
148 delete mFeatureSelectionManager;
150 mFeatureSelectionManager = featureSelectionManager;
152 if ( mFeatureSelectionModel )
160 settings.setValue(
"/BetterAttributeTable/geometry", QVariant( saveGeometry() ) );
165 setSelectionMode( QAbstractItemView::NoSelection );
167 setSelectionMode( QAbstractItemView::ExtendedSelection );
172 setSelectionMode( QAbstractItemView::NoSelection );
174 setSelectionMode( QAbstractItemView::ExtendedSelection );
179 setSelectionMode( QAbstractItemView::NoSelection );
181 setSelectionMode( QAbstractItemView::ExtendedSelection );
186 switch ( event->key() )
195 setSelectionMode( QAbstractItemView::NoSelection );
197 setSelectionMode( QAbstractItemView::ExtendedSelection );
208 foreach (
const QModelIndex
index, indexes )
216 setDirtyRegion( viewport()->rect() );
221 QItemSelection selection;
222 selection.append( QItemSelectionRange( mFilterModel->index( 0, 0 ), mFilterModel->index( mFilterModel->rowCount() - 1, 0 ) ) );
223 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
231 QModelIndex idx = indexAt( event->pos() );
232 if ( !idx.isValid() )
241 mActionPopup =
new QMenu();
243 mActionPopup->addAction(
tr(
"Select All" ),
this, SLOT(
selectAll() ), QKeySequence::SelectAll );
248 if ( mActionPopup->actions().count() > 0 )
250 mActionPopup->popup( event->globalPos() );
264 void QgsAttributeTableView::modelDeleted()
267 mFeatureSelectionManager = 0;
268 mFeatureSelectionModel = 0;
273 if ( selectionBehavior() == QTableView::SelectColumns
274 || ( selectionMode() == QTableView::SingleSelection
275 && selectionBehavior() == QTableView::SelectItems ) )
278 if ( row >= 0 && row < model()->rowCount() )
280 int column = horizontalHeader()->logicalIndexAt( isRightToLeft() ? viewport()->width() : 0 );
281 QModelIndex
index = model()->index( row, column );
282 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
283 selectionModel()->setCurrentIndex( index, QItemSelectionModel::NoUpdate );
284 if (( anchor && !( command & QItemSelectionModel::Current ) )
285 || ( selectionMode() == QTableView::SingleSelection ) )
286 mRowSectionAnchor = row;
288 if ( selectionMode() != QTableView::SingleSelection
289 && command.testFlag( QItemSelectionModel::Toggle ) )
292 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
293 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
294 command &= ~QItemSelectionModel::Toggle;
295 command |= mCtrlDragSelectionFlag;
297 command |= QItemSelectionModel::Current;
300 QModelIndex tl = model()->index( qMin( mRowSectionAnchor, row ), 0 );
301 QModelIndex br = model()->index( qMax( mRowSectionAnchor, row ), model()->columnCount() - 1 );
302 if ( verticalHeader()->sectionsMoved() && tl.row() != br.row() )
303 setSelection( visualRect( tl ) | visualRect( br ), command );
305 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );