QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsattributetableview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableView.cpp
3  --------------------------------------
4  Date : Feb 2009
5  Copyright : (C) 2009 Vita Cizek
6  Email : weetya (at) gmail.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QKeyEvent>
17 #include <QSettings>
18 #include <QHeaderView>
19 #include <QMenu>
20 
21 #include "qgsattributetableview.h"
22 #include "qgsattributetablemodel.h"
25 #include "qgsvectorlayer.h"
26 #include "qgsvectorlayercache.h"
28 #include "qgsvectordataprovider.h"
29 #include "qgslogger.h"
30 #include "qgsmapcanvas.h"
32 
34  : QTableView( parent )
35  , mMasterModel( NULL )
36  , mFilterModel( NULL )
37  , mFeatureSelectionModel( NULL )
38  , mFeatureSelectionManager( NULL )
39  , mActionPopup( NULL )
40 {
41  QSettings settings;
42  restoreGeometry( settings.value( "/BetterAttributeTable/geometry" ).toByteArray() );
43 
44  verticalHeader()->setDefaultSectionSize( 20 );
45  horizontalHeader()->setHighlightSections( false );
46 
48  setItemDelegate( mTableDelegate );
49 
50  setSelectionBehavior( QAbstractItemView::SelectRows );
51  setSelectionMode( QAbstractItemView::ExtendedSelection );
52  setSortingEnabled( true );
53 
54  verticalHeader()->viewport()->installEventFilter( this );
55 
56  connect( verticalHeader(), SIGNAL( sectionPressed( int ) ), this, SLOT( selectRow( int ) ) );
57  connect( verticalHeader(), SIGNAL( sectionEntered( int ) ), this, SLOT( _q_selectRow( int ) ) );
58 }
59 
61 {
62  if ( mActionPopup )
63  {
64  delete mActionPopup;
65  }
66 }
67 #if 0
68 void QgsAttributeTableView::setCanvasAndLayerCache( QgsMapCanvas *canvas, QgsVectorLayerCache *layerCache )
69 {
72 
73  mMasterModel = new QgsAttributeTableModel( layerCache, this );
74 
75  mLayerCache = layerCache;
76 
78 
83  connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ), this, SLOT( repaintRequested( QModelIndexList ) ) );
84  connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ), this, SLOT( repaintRequested() ) );
85  setSelectionModel( mFeatureSelectionModel );
86 
87  delete oldModel;
88  delete filterModel;
89 }
90 #endif
91 bool QgsAttributeTableView::eventFilter( QObject *object, QEvent *event )
92 {
93  if ( object == verticalHeader()->viewport() )
94  {
95  switch ( event->type() )
96  {
97  case QEvent::MouseButtonPress:
99  break;
100 
101  case QEvent::MouseButtonRelease:
103  break;
104 
105  default:
106  break;
107  }
108  }
109  return false;
110 }
111 
113 {
114  if ( mFilterModel )
115  {
116  // Cleanup old model stuff if present
117  disconnect( mFilterModel, SIGNAL( filterAboutToBeInvalidated() ), this, SLOT( onFilterAboutToBeInvalidated() ) );
118  disconnect( mFilterModel, SIGNAL( filterInvalidated() ), this, SLOT( onFilterInvalidated() ) );
119  }
120 
121  mFilterModel = filterModel;
122  QTableView::setModel( filterModel );
123 
124  delete mFeatureSelectionModel;
125  mFeatureSelectionModel = NULL;
126 
127  if ( filterModel )
128  {
130  {
132  }
133 
135  setSelectionModel( mFeatureSelectionModel );
137  connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ), this, SLOT( repaintRequested( QModelIndexList ) ) );
138  connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ), this, SLOT( repaintRequested() ) );
139  }
140 }
141 
143 {
146 
147  mFeatureSelectionManager = featureSelectionManager;
148 
151 }
152 
153 void QgsAttributeTableView::closeEvent( QCloseEvent *e )
154 {
155  Q_UNUSED( e );
156  QSettings settings;
157  settings.setValue( "/BetterAttributeTable/geometry", QVariant( saveGeometry() ) );
158 }
159 
160 void QgsAttributeTableView::mousePressEvent( QMouseEvent *event )
161 {
162  setSelectionMode( QAbstractItemView::NoSelection );
163  QTableView::mousePressEvent( event );
164  setSelectionMode( QAbstractItemView::ExtendedSelection );
165 }
166 
168 {
169  setSelectionMode( QAbstractItemView::NoSelection );
170  QTableView::mouseReleaseEvent( event );
171  setSelectionMode( QAbstractItemView::ExtendedSelection );
172 }
173 
174 void QgsAttributeTableView::mouseMoveEvent( QMouseEvent *event )
175 {
176  setSelectionMode( QAbstractItemView::NoSelection );
177  QTableView::mouseMoveEvent( event );
178  setSelectionMode( QAbstractItemView::ExtendedSelection );
179 }
180 
181 void QgsAttributeTableView::keyPressEvent( QKeyEvent *event )
182 {
183  switch ( event->key() )
184  {
185 
186  // Default Qt behavior would be to change the selection.
187  // We don't make it that easy for the user to trash his selection.
188  case Qt::Key_Up:
189  case Qt::Key_Down:
190  case Qt::Key_Left:
191  case Qt::Key_Right:
192  setSelectionMode( QAbstractItemView::NoSelection );
193  QTableView::keyPressEvent( event );
194  setSelectionMode( QAbstractItemView::ExtendedSelection );
195  break;
196 
197  default:
198  QTableView::keyPressEvent( event );
199  break;
200  }
201 }
202 
203 void QgsAttributeTableView::repaintRequested( QModelIndexList indexes )
204 {
205  foreach ( const QModelIndex index, indexes )
206  {
207  update( index );
208  }
209 }
210 
212 {
213  setDirtyRegion( viewport()->rect() );
214 }
215 
217 {
218  QItemSelection selection;
219  selection.append( QItemSelectionRange( mFilterModel->index( 0, 0 ), mFilterModel->index( mFilterModel->rowCount() - 1, 0 ) ) );
220  mFeatureSelectionModel->selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
221 }
222 
223 void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent* event )
224 {
225  if ( mActionPopup )
226  {
227  delete mActionPopup;
228  mActionPopup = 0;
229  }
230 
231  QModelIndex idx = indexAt( event->pos() );
232  if ( !idx.isValid() )
233  {
234  return;
235  }
236 
237  QgsVectorLayer *vlayer = mFilterModel->layer();
238  if ( !vlayer )
239  return;
240 
241  mActionPopup = new QMenu();
242 
243  mActionPopup->addAction( tr( "Select All" ), this, SLOT( selectAll() ), QKeySequence::SelectAll );
244 
245  // let some other parts of the application add some actions
246  emit willShowContextMenu( mActionPopup, idx );
247 
248  if ( mActionPopup->actions().count() > 0 )
249  {
250  mActionPopup->popup( event->globalPos() );
251  }
252 }
253 
255 {
256  selectRow( row, true );
257 }
258 
260 {
261  selectRow( row, false );
262 }
263 
264 void QgsAttributeTableView::selectRow( int row, bool anchor )
265 {
266  if ( selectionBehavior() == QTableView::SelectColumns
267  || ( selectionMode() == QTableView::SingleSelection
268  && selectionBehavior() == QTableView::SelectItems ) )
269  return;
270 
271  if ( row >= 0 && row < model()->rowCount() )
272  {
273  int column = horizontalHeader()->logicalIndexAt( isRightToLeft() ? viewport()->width() : 0 );
274  QModelIndex index = model()->index( row, column );
275  QItemSelectionModel::SelectionFlags command = selectionCommand( index );
276  selectionModel()->setCurrentIndex( index, QItemSelectionModel::NoUpdate );
277  if (( anchor && !( command & QItemSelectionModel::Current ) )
278  || ( selectionMode() == QTableView::SingleSelection ) )
279  mRowSectionAnchor = row;
280 
281  if ( selectionMode() != QTableView::SingleSelection
282  && command.testFlag( QItemSelectionModel::Toggle ) )
283  {
284  if ( anchor )
286  ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
287  command &= ~QItemSelectionModel::Toggle;
288  command |= mCtrlDragSelectionFlag;
289  if ( !anchor )
290  command |= QItemSelectionModel::Current;
291  }
292 
293  QModelIndex tl = model()->index( qMin( mRowSectionAnchor, row ), 0 );
294  QModelIndex br = model()->index( qMax( mRowSectionAnchor, row ), model()->columnCount() - 1 );
295  if ( verticalHeader()->sectionsMoved() && tl.row() != br.row() )
296  setSelection( visualRect( tl ) | visualRect( br ), command );
297  else
298  mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
299  }
300 }
QgsAttributeTableFilterModel * mFilterModel
static unsigned index
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
virtual void loadLayer()
Loads the layer into the model Preferably to be called, before basing any other models on this model...
QgsAttributeTableModel * mMasterModel
QItemSelectionModel::SelectionFlag mCtrlDragSelectionFlag
void mouseMoveEvent(QMouseEvent *event)
Called for mouse move events on a table cell.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
virtual bool eventFilter(QObject *object, QEvent *event)
This event filter is installed on the verticalHeader to intercept mouse press and release events...
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
void enableSync(bool enable)
Enables or disables synchronisation to the QgsVectorLayer When synchronisation is disabled...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
void mousePressEvent(QMouseEvent *event)
Called for mouse press events on a table cell.
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QgsVectorLayerCache * mLayerCache
QgsFeatureSelectionModel * mFeatureSelectionModel
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QgsIFeatureSelectionManager * mFeatureSelectionManager
QgsAttributeTableView(QWidget *parent=0)
void keyPressEvent(QKeyEvent *event)
Called for key press events Disables selection change by only pressing an arrow key.
virtual void selectFeatures(const QItemSelection &selection, SelectionFlags command)
Select features on this table.
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
QgsAttributeTableDelegate * mTableDelegate
virtual void setModel(QgsAttributeTableFilterModel *filterModel)
void willShowContextMenu(QMenu *menu, QModelIndex atIndex)
Is emitted, in order to provide a hook to add aditional menu entries to the context menu...
This class caches features of a given QgsVectorLayer.
void closeEvent(QCloseEvent *event)
Saves geometry to the settings on close.
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
setFeatureSelectionManager
void mouseReleaseEvent(QMouseEvent *event)
Called for mouse release events on a table cell.
A delegate item class for QgsAttributeTable (see Qt documentation for QItemDelegate).
virtual void selectRow(int row)
virtual void _q_selectRow(int row)
Is an interface class to abstract feature selection handling.
Represents a vector layer which manages a vector based data sets.
void contextMenuEvent(QContextMenuEvent *event)
Is called when the context menu will be shown.
#define tr(sourceText)