QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfeaturelistview.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 "qgsfeaturelistview.h"
22 #include "qgsattributetablemodel.h"
25 #include "qgsvectorlayer.h"
26 #include "qgsvectordataprovider.h"
27 #include "qgslogger.h"
28 #include "qgsmapcanvas.h"
30 #include "qgsfeaturelistmodel.h"
32 #include <QSet>
33 
35  : QListView( parent )
36  , mCurrentEditSelectionModel( NULL )
37  , mFeatureSelectionModel( NULL )
38  , mItemDelegate( NULL )
39  , mEditSelectionDrag( false )
40 {
41  setSelectionMode( QAbstractItemView::ExtendedSelection );
42 }
43 
45 {
46  return mModel->layerCache();
47 }
48 
50 {
51  QListView::setModel( featureListModel );
53 
55  mFeatureSelectionModel = new QgsFeatureSelectionModel( featureListModel, featureListModel, featureListModel->layerCache()->layer(), this );
56  setSelectionModel( mFeatureSelectionModel );
57 
58  mCurrentEditSelectionModel = new QItemSelectionModel( mModel->masterModel(), this );
59 
60  if ( mItemDelegate && mItemDelegate->parent() == this )
61  {
62  delete mItemDelegate;
63  }
64 
67  setItemDelegate( mItemDelegate );
68 
70  connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ), this, SLOT( repaintRequested( QModelIndexList ) ) );
71  connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ), this, SLOT( repaintRequested() ) );
72 
73  connect( mCurrentEditSelectionModel, SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), SLOT( editSelectionChanged( QItemSelection, QItemSelection ) ) );
74 }
75 
76 bool QgsFeatureListView::setDisplayExpression( const QString expression )
77 {
78  if ( mModel->setDisplayExpression( expression ) )
79  {
80  emit displayExpressionChanged( expression );
81  return true;
82  }
83  else
84  {
85  return false;
86  }
87 }
88 
90 {
91  return mModel->displayExpression();
92 }
93 
95 {
96  return mModel->parserErrorString();
97 }
98 
99 void QgsFeatureListView::mousePressEvent( QMouseEvent *event )
100 {
101  QPoint pos = event->pos();
102 
103  QModelIndex index = indexAt( pos );
104 
106  {
107  mEditSelectionDrag = true;
108  mCurrentEditSelectionModel->select( mModel->mapToMaster( index ), QItemSelectionModel::ClearAndSelect );
109  }
110  else
111  {
113  selectRow( index, true );
115  }
116 }
117 
118 void QgsFeatureListView::editSelectionChanged( QItemSelection deselected, QItemSelection selected )
119 {
120  if ( isVisible() && updatesEnabled() )
121  {
122  QItemSelection localDeselected = mModel->mapSelectionFromMaster( deselected );
123  QItemSelection localSelected = mModel->mapSelectionFromMaster( selected );
124  viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
125  }
126 
127  QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
128  if ( currentSelection.size() == 1 )
129  {
130  QgsFeature feat;
131  mModel->featureByIndex( mModel->mapFromMaster( currentSelection.indexes().first() ), feat );
132 
133  emit currentEditSelectionChanged( feat );
134  }
135 }
136 
138 {
139  QItemSelection selection;
140  selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->rowCount() - 1, 0 ) ) );
141 
142  mFeatureSelectionModel->selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
143 }
144 
146 {
147  QItemSelection selection;
148 
149  foreach ( QgsFeatureId fid, fids )
150  {
151  selection.append( QItemSelectionRange( mModel->mapToMaster( mModel->fidToIdx( fid ) ) ) );
152  }
153 
154  mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
155 }
156 
157 void QgsFeatureListView::repaintRequested( QModelIndexList indexes )
158 {
159  foreach ( const QModelIndex index, indexes )
160  {
161  update( index );
162  }
163 }
164 
166 {
167  setDirtyRegion( viewport()->rect() );
168 }
169 
176 void QgsFeatureListView::mouseMoveEvent( QMouseEvent *event )
177 {
178  QPoint pos = event->pos();
179 
180  QModelIndex index = indexAt( pos );
181 
182  if ( mEditSelectionDrag )
183  {
184  mCurrentEditSelectionModel->select( mModel->mapToMaster( index ), QItemSelectionModel::ClearAndSelect );
185  }
186  else
187  {
188  selectRow( index, false );
189  }
190 }
191 
199 void QgsFeatureListView::mouseReleaseEvent( QMouseEvent *event )
200 {
201  Q_UNUSED( event );
202 
203  if ( mEditSelectionDrag )
204  {
205  mEditSelectionDrag = false;
206  }
207  else
208  {
210  }
211 }
212 
213 void QgsFeatureListView::keyPressEvent( QKeyEvent *event )
214 {
215  if ( Qt::Key_Up == event->key() || Qt::Key_Down == event->key() )
216  {
217  int currentRow = 0;
218  if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
219  {
220  QModelIndex localIndex = mModel->mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
221  currentRow = localIndex.row();
222  }
223 
224  QModelIndex newLocalIndex;
225  QModelIndex newIndex;
226 
227  switch ( event->key() )
228  {
229  case Qt::Key_Up:
230  newLocalIndex = mModel->index( currentRow - 1, 0 );
231  newIndex = mModel->mapToMaster( newLocalIndex );
232  if ( newIndex.isValid() )
233  {
234  mCurrentEditSelectionModel->select( newIndex, QItemSelectionModel::ClearAndSelect );
235  scrollTo( newLocalIndex );
236  }
237  break;
238 
239  case Qt::Key_Down:
240  newLocalIndex = mModel->index( currentRow + 1, 0 );
241  newIndex = mModel->mapToMaster( newLocalIndex );
242  if ( newIndex.isValid() )
243  {
244  mCurrentEditSelectionModel->select( newIndex, QItemSelectionModel::ClearAndSelect );
245  scrollTo( newLocalIndex );
246  }
247  break;
248 
249  default:
250  break;
251  }
252  }
253  else
254  {
255  QListView::keyPressEvent( event );
256  }
257 }
258 
259 void QgsFeatureListView::selectRow( const QModelIndex& index, bool anchor )
260 {
261  QItemSelectionModel::SelectionFlags command = selectionCommand( index );
262  int row = index.row();
263 
264  if ( anchor )
265  mRowAnchor = row;
266 
267  if ( selectionMode() != QListView::SingleSelection
268  && command.testFlag( QItemSelectionModel::Toggle ) )
269  {
270  if ( anchor )
272  ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
273  command &= ~QItemSelectionModel::Toggle;
274  command |= mCtrlDragSelectionFlag;
275  if ( !anchor )
276  command |= QItemSelectionModel::Current;
277  }
278 
279  QModelIndex tl = model()->index( qMin( mRowAnchor, row ), 0 );
280  QModelIndex br = model()->index( qMax( mRowAnchor, row ), model()->columnCount() - 1 );
281 
282  mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
283 }