QGIS API Documentation  2.2.0-Valmiera
 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 );
164  setSelectionMode( QAbstractItemView::ExtendedSelection );
165 }
166 
168 {
169  setSelectionMode( QAbstractItemView::NoSelection );
171  setSelectionMode( QAbstractItemView::ExtendedSelection );
172 }
173 
174 void QgsAttributeTableView::mouseMoveEvent( QMouseEvent *event )
175 {
176  setSelectionMode( QAbstractItemView::NoSelection );
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 }