26 changes: 22 additions & 4 deletions src/gui/attributetable/qgsattributetableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsattributetablefiltermodel.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayercache.h"
#include "qgsvectorlayerselectionmanager.h"
#include "qgsvectordataprovider.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
Expand All @@ -34,6 +35,7 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
, mMasterModel( NULL )
, mFilterModel( NULL )
, mFeatureSelectionModel( NULL )
, mFeatureSelectionManager( NULL )
, mActionPopup( NULL )
{
QSettings settings;
Expand Down Expand Up @@ -62,7 +64,7 @@ QgsAttributeTableView::~QgsAttributeTableView()
delete mActionPopup;
}
}

#if 0
void QgsAttributeTableView::setCanvasAndLayerCache( QgsMapCanvas *canvas, QgsVectorLayerCache *layerCache )
{
QgsAttributeTableModel* oldModel = mMasterModel;
Expand All @@ -77,15 +79,15 @@ void QgsAttributeTableView::setCanvasAndLayerCache( QgsMapCanvas *canvas, QgsVec
mFilterModel = new QgsAttributeTableFilterModel( canvas, mMasterModel, mMasterModel );
setModel( mFilterModel );
delete mFeatureSelectionModel;
mFeatureSelectionModel = new QgsFeatureSelectionModel( mFilterModel, mFilterModel, layerCache->layer(), mFilterModel );
mFeatureSelectionModel = new QgsFeatureSelectionModel( mFilterModel, mFilterModel, new QgsVectorLayerSelectionManager( layerCache->layer(), mFilterModel ), mFilterModel );
connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ), this, SLOT( repaintRequested( QModelIndexList ) ) );
connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ), this, SLOT( repaintRequested() ) );
setSelectionModel( mFeatureSelectionModel );

delete oldModel;
delete filterModel;
}

#endif
bool QgsAttributeTableView::eventFilter( QObject *object, QEvent *event )
{
if ( object == verticalHeader()->viewport() )
Expand Down Expand Up @@ -124,14 +126,30 @@ void QgsAttributeTableView::setModel( QgsAttributeTableFilterModel* filterModel

if ( filterModel )
{
mFeatureSelectionModel = new QgsFeatureSelectionModel( mFilterModel, mFilterModel, mFilterModel->layer(), mFilterModel );
if ( !mFeatureSelectionManager )
{
mFeatureSelectionManager = new QgsVectorLayerSelectionManager( mFilterModel->layer(), mFilterModel );
}

mFeatureSelectionModel = new QgsFeatureSelectionModel( mFilterModel, mFilterModel, mFeatureSelectionManager, mFilterModel );
setSelectionModel( mFeatureSelectionModel );
mTableDelegate->setFeatureSelectionModel( mFeatureSelectionModel );
connect( mFeatureSelectionModel, SIGNAL( requestRepaint( QModelIndexList ) ), this, SLOT( repaintRequested( QModelIndexList ) ) );
connect( mFeatureSelectionModel, SIGNAL( requestRepaint() ), this, SLOT( repaintRequested() ) );
}
}

void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager )
{
if ( mFeatureSelectionManager )
delete mFeatureSelectionManager;

mFeatureSelectionManager = featureSelectionManager;

if ( mFeatureSelectionModel )
mFeatureSelectionModel->setFeatureSelectionManager( mFeatureSelectionManager );
}

void QgsAttributeTableView::closeEvent( QCloseEvent *e )
{
Q_UNUSED( e );
Expand Down
18 changes: 9 additions & 9 deletions src/gui/attributetable/qgsattributetableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@

#include "qgsfeature.h" // For QgsFeatureIds

class QgsAttributeTableModel;
class QgsAttributeTableDelegate;
class QgsAttributeTableFilterModel;
class QgsVectorLayerCache;
class QgsAttributeTableModel;
class QgsFeatureSelectionModel;
class QgsAttributeTableDelegate;
class QgsIFeatureSelectionManager;
class QgsMapCanvas;
class QgsVectorLayer;
class QgsVectorLayerCache;
class QMenu;
class QProgressDialog;


/**
* @brief
* Provides a table view of features of a @link QgsVectorLayer @endlink.
Expand All @@ -50,13 +52,10 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
virtual void setModel( QgsAttributeTableFilterModel* filterModel );

/**
* Autocreates the models
* @param layerCache The @link QgsVectorLayerCache @endlink to use ( as backend )
* @param canvas The @link QgsMapCanvas @endlink to use ( for the currently visible features filter )
*
* @deprecated
* @brief setFeatureSelectionManager
* @param featureSelectionManager We will take ownership
*/
void setCanvasAndLayerCache( QgsMapCanvas *canvas, QgsVectorLayerCache *layerCache );
void setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager );

/**
* This event filter is installed on the verticalHeader to intercept mouse press and release
Expand Down Expand Up @@ -143,6 +142,7 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
QgsAttributeTableModel* mMasterModel;
QgsAttributeTableFilterModel* mFilterModel;
QgsFeatureSelectionModel* mFeatureSelectionModel;
QgsIFeatureSelectionManager* mFeatureSelectionManager;
QgsAttributeTableDelegate* mTableDelegate;
QAbstractItemModel* mModel; // Most likely the filter model
QMenu *mActionPopup;
Expand Down
44 changes: 30 additions & 14 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
* *
***************************************************************************/

#include "qgsdualview.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayercache.h"
#include "qgsattributetablemodel.h"
#include "qgsfeaturelistmodel.h"
#include "qgsattributedialog.h"
#include "qgsapplication.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsattributeaction.h"
#include "qgsvectordataprovider.h"
#include "qgsmessagelog.h"
#include "qgsattributedialog.h"
#include "qgsattributetablemodel.h"
#include "qgsdualview.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsfeaturelistmodel.h"
#include "qgsifeatureselectionmanager.h"
#include "qgsmapcanvas.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayercache.h"

#include <QDialog>
#include <QMenu>
Expand All @@ -32,8 +34,11 @@

QgsDualView::QgsDualView( QWidget* parent )
: QStackedWidget( parent )
, mEditorContext()
, mMasterModel( NULL )
, mAttributeDialog( NULL )
, mProgressDlg( NULL )
, mFeatureSelectionManager( NULL )
{
setupUi( this );

Expand All @@ -57,9 +62,9 @@ QgsDualView::~QgsDualView()
delete mAttributeDialog;
}

void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDistanceArea myDa, const QgsFeatureRequest &request )
void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsFeatureRequest &request, QgsAttributeEditorContext context )
{
mDistanceArea = myDa;
mEditorContext = context;

connect( mTableView, SIGNAL( willShowContextMenu( QMenu*, QModelIndex ) ), this, SLOT( viewWillShowContextMenu( QMenu*, QModelIndex ) ) );

Expand All @@ -69,7 +74,7 @@ void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDista
mTableView->setModel( mFilterModel );
mFeatureList->setModel( mFeatureListModel );

mAttributeDialog = new QgsAttributeDialog( layer, 0, false, myDa );
mAttributeDialog = new QgsAttributeDialog( layer, NULL, false, NULL, true, mEditorContext );
if ( mAttributeDialog->dialog() )
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );

Expand Down Expand Up @@ -264,7 +269,7 @@ void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
}

mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( feat ), true, mDistanceArea, this, false );
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( feat ), true, this, false, mEditorContext );
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );
mAttributeDialog->dialog()->setVisible( true );

Expand Down Expand Up @@ -419,7 +424,7 @@ void QgsDualView::attributeDeleted( int attribute )

mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );

mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, mDistanceArea, this, false );
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, this, false );
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );

delete oldDialog;
Expand Down Expand Up @@ -459,7 +464,7 @@ void QgsDualView::attributeAdded( int attribute )

mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );

mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, mDistanceArea, this, false );
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, this, false );
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );

delete oldDialog;
Expand Down Expand Up @@ -503,7 +508,7 @@ void QgsDualView::reloadAttribute( const int& idx )

mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );

mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, mDistanceArea, this, false );
mAttributeDialog = new QgsAttributeDialog( mLayerCache->layer(), new QgsFeature( *feat ), true, this, false );
mAttributeEditorLayout->addWidget( mAttributeDialog->dialog() );

delete oldDialog;
Expand All @@ -520,6 +525,17 @@ void QgsDualView::setRequest( const QgsFeatureRequest& request )
mMasterModel->setRequest( request );
}

void QgsDualView::setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager )
{
mTableView->setFeatureSelectionManager( featureSelectionManager );
// mFeatureList->setFeatureSelectionManager( featureSelectionManager );

if ( mFeatureSelectionManager && mFeatureSelectionManager->parent() == this )
delete mFeatureSelectionManager;

mFeatureSelectionManager = featureSelectionManager;
}

void QgsDualView::progress( int i, bool& cancel )
{
if ( !mProgressDlg )
Expand Down
17 changes: 11 additions & 6 deletions src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

#include <QStackedWidget>

#include "qgsdistancearea.h"
#include "ui_qgsdualviewbase.h"

#include "qgsattributeeditorcontext.h"
#include "qgsattributetablefiltermodel.h"
#include "qgscachedfeatureiterator.h"
#include "ui_qgsdualviewbase.h"
#include "qgsdistancearea.h"

class QgsFeatureRequest;
class QgsAttributeDialog;
class QgsFeatureRequest;
class QSignalMapper;

/**
Expand Down Expand Up @@ -74,10 +76,10 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
* @param layer The layer which should be used to fetch features
* @param mapCanvas The mapCanvas (used for the FilterMode
* {@link QgsAttributeTableFilterModel::ShowVisible}
* @param myDa Used for attribute dialog creation
* @param request Use a modified request to limit the shown features
* @param context The context in which this view is shown
*/
void init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDistanceArea myDa, const QgsFeatureRequest& request = QgsFeatureRequest() );
void init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request = QgsFeatureRequest(), QgsAttributeEditorContext context = QgsAttributeEditorContext() );

/**
* Change the current view mode.
Expand Down Expand Up @@ -133,6 +135,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void setRequest( const QgsFeatureRequest& request );

void setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager );

protected:
/**
* Initializes widgets which depend on the attributes of this layer
Expand Down Expand Up @@ -235,6 +239,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
void initLayerCache( QgsVectorLayer *layer );
void initModels( QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request );

QgsAttributeEditorContext mEditorContext;
QgsAttributeTableModel* mMasterModel;
QgsAttributeTableFilterModel* mFilterModel;
QgsFeatureListModel* mFeatureListModel;
Expand All @@ -244,7 +249,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QMenu* mPreviewColumnsMenu;
QgsVectorLayerCache* mLayerCache;
QProgressDialog* mProgressDlg;

QgsIFeatureSelectionManager* mFeatureSelectionManager;
QgsDistanceArea mDistanceArea;

friend class TestQgsDualView;
Expand Down
23 changes: 12 additions & 11 deletions src/gui/attributetable/qgsfeaturelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
* *
***************************************************************************/

#include <QKeyEvent>
#include <QSettings>
#include <QHeaderView>
#include <QKeyEvent>
#include <QMenu>
#include <QSet>
#include <QSettings>

#include "qgsfeaturelistview.h"
#include "qgsattributetablemodel.h"
#include "qgsattributetabledelegate.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfeaturelistviewdelegate.h"
#include "qgsattributetablemodel.h"
#include "qgsfeaturelistmodel.h"
#include "qgsfeaturelistviewdelegate.h"
#include "qgsfeaturelistview.h"
#include "qgsfeatureselectionmodel.h"
#include <QSet>
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerselectionmanager.h"

QgsFeatureListView::QgsFeatureListView( QWidget *parent )
: QListView( parent )
Expand All @@ -52,7 +53,7 @@ void QgsFeatureListView::setModel( QgsFeatureListModel* featureListModel )
mModel = featureListModel;

delete mFeatureSelectionModel;
mFeatureSelectionModel = new QgsFeatureSelectionModel( featureListModel, featureListModel, featureListModel->layerCache()->layer(), this );
mFeatureSelectionModel = new QgsFeatureSelectionModel( featureListModel, featureListModel, new QgsVectorLayerSelectionManager( featureListModel->layerCache()->layer(), this ), this );
setSelectionModel( mFeatureSelectionModel );

mCurrentEditSelectionModel = new QItemSelectionModel( mModel->masterModel(), this );
Expand Down
31 changes: 19 additions & 12 deletions src/gui/attributetable/qgsfeatureselectionmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "qgsattributetablemodel.h"
#include "qgsfeaturemodel.h"
#include "qgsifeatureselectionmanager.h"
#include "qgsfeatureselectionmodel.h"
#include "qgsvectorlayer.h"
#include <qdebug.h>

QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsVectorLayer* layer, QObject* parent )
QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsIFeatureSelectionManager* featureSelectionManager, QObject* parent )
: QItemSelectionModel( model, parent )
, mFeatureModel( featureModel )
, mLayer( layer )
, mSyncEnabled( true )
, mClearAndSelectBuffer( false )
{
connect( mLayer, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
setFeatureSelectionManager( featureSelectionManager );
}

void QgsFeatureSelectionModel::enableSync( bool enable )
Expand All @@ -22,12 +22,12 @@ void QgsFeatureSelectionModel::enableSync( bool enable )
{
if ( mClearAndSelectBuffer )
{
mLayer->setSelectedFeatures( mSelectedBuffer );
mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
}
else
{
mLayer->select( mSelectedBuffer );
mLayer->deselect( mDeselectedBuffer );
mFeatureSelectionManager->select( mSelectedBuffer );
mFeatureSelectionManager->deselect( mDeselectedBuffer );
}

mSelectedBuffer.clear();
Expand All @@ -44,7 +44,7 @@ bool QgsFeatureSelectionModel::isSelected( QgsFeatureId fid )
if ( mDeselectedBuffer.contains( fid ) )
return false;

if ( !mClearAndSelectBuffer && mLayer->selectedFeaturesIds().contains( fid ) )
if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeaturesIds().contains( fid ) )
return true;

return false;
Expand All @@ -66,7 +66,7 @@ void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection,
ids << id;
}

disconnect( mLayer, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
disconnect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );

if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
{
Expand All @@ -83,7 +83,7 @@ void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection,
}
else
{
mLayer->setSelectedFeatures( ids );
mFeatureSelectionManager->setSelectedFeatures( ids );
}
}
else if ( command.testFlag( QItemSelectionModel::Select ) )
Expand All @@ -100,7 +100,7 @@ void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection,
}
else
{
mLayer->select( ids );
mFeatureSelectionManager->select( ids );
}
}
else if ( command.testFlag( QItemSelectionModel::Deselect ) )
Expand All @@ -117,11 +117,11 @@ void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection,
}
else
{
mLayer->deselect( ids );
mFeatureSelectionManager->deselect( ids );
}
}

connect( mLayer, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );

QModelIndexList updatedIndexes;
foreach ( QModelIndex idx, selection.indexes() )
Expand All @@ -132,6 +132,13 @@ void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection,
emit requestRepaint( updatedIndexes );
}

void QgsFeatureSelectionModel::setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager )
{
mFeatureSelectionManager = featureSelectionManager;

connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
}

void QgsFeatureSelectionModel::layerSelectionChanged( QgsFeatureIds selected, QgsFeatureIds deselected, bool clearAndSelect )
{
if ( clearAndSelect )
Expand Down
16 changes: 14 additions & 2 deletions src/gui/attributetable/qgsfeatureselectionmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

class QgsVectorLayer;
class QgsFeatureModel;
class QgsIFeatureSelectionManager;

class GUI_EXPORT QgsFeatureSelectionModel : public QItemSelectionModel
{
Q_OBJECT
public:
explicit QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsVectorLayer* layer, QObject* parent );
explicit QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsIFeatureSelectionManager* featureSelectionHandler, QObject* parent );

/**
* Enables or disables synchronisation to the {@link QgsVectorLayer}
Expand Down Expand Up @@ -81,6 +82,8 @@ class GUI_EXPORT QgsFeatureSelectionModel : public QItemSelectionModel
*/
virtual void selectFeatures( const QItemSelection &selection, SelectionFlags command );

virtual void setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager );

private slots:
virtual void layerSelectionChanged( QgsFeatureIds selected, QgsFeatureIds deselected, bool clearAndSelect );

Expand All @@ -89,10 +92,19 @@ class GUI_EXPORT QgsFeatureSelectionModel : public QItemSelectionModel

private:
QgsFeatureModel* mFeatureModel;
QgsVectorLayer* mLayer;
QgsIFeatureSelectionManager* mFeatureSelectionManager;
bool mSyncEnabled;

//! If sync is disabled
//! Holds a list of newly selected features which will be synced when re-enabled
QgsFeatureIds mSelectedBuffer;

//! If sync is disabled
//! Holds a list of newly deselected features which will be synced when re-enabled
QgsFeatureIds mDeselectedBuffer;

//! If sync is disabled
//! Is set to true, if a clear and select operation should be performed before syncing
bool mClearAndSelectBuffer;
};

Expand Down
58 changes: 58 additions & 0 deletions src/gui/attributetable/qgsgenericfeatureselectionmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/***************************************************************************
qgsgenericfeatureselectionmgr.cpp
--------------------------------------
Date : 11.6.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* 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 "qgsgenericfeatureselectionmanager.h"

QgsGenericFeatureSelectionManager::QgsGenericFeatureSelectionManager( QObject *parent )
: QgsIFeatureSelectionManager( parent )
{
}

QgsGenericFeatureSelectionManager::QgsGenericFeatureSelectionManager( const QgsFeatureIds& initialSelection, QObject* parent )
: QgsIFeatureSelectionManager( parent )
, mSelectedFeatures( initialSelection )
{
}

int QgsGenericFeatureSelectionManager::selectedFeatureCount()
{
return mSelectedFeatures.size();
}

void QgsGenericFeatureSelectionManager::select( const QgsFeatureIds& ids )
{
mSelectedFeatures += ids;
emit selectionChanged( ids, QgsFeatureIds(), false );
}

void QgsGenericFeatureSelectionManager::deselect( const QgsFeatureIds& ids )
{
mSelectedFeatures -= ids;
emit selectionChanged( QgsFeatureIds(), ids, false );
}

void QgsGenericFeatureSelectionManager::setSelectedFeatures( const QgsFeatureIds& ids )
{
QgsFeatureIds selected = mSelectedFeatures - ids;
QgsFeatureIds deselected = ids - mSelectedFeatures;

mSelectedFeatures = ids;
emit selectionChanged( selected, deselected, true );
}

const QgsFeatureIds& QgsGenericFeatureSelectionManager::selectedFeaturesIds() const
{
return mSelectedFeatures;
}
46 changes: 46 additions & 0 deletions src/gui/attributetable/qgsgenericfeatureselectionmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/***************************************************************************
qgsgenericfeatureselectionmgr.h
--------------------------------------
Date : 11.6.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSGENERICFEATURESELECTIONMANAGER_H
#define QGSGENERICFEATURESELECTIONMANAGER_H

#include "qgsfeature.h"
#include "qgsifeatureselectionmanager.h"

/**
* This selection manager synchronizes a local set of selected features with an attribute table.
* If you want to synchronize the attribute table selection with the map canvas selection, you
* should use { @link QgsVectorLayerSelectionManager } instead.
*/
class CORE_EXPORT QgsGenericFeatureSelectionManager : public QgsIFeatureSelectionManager
{
Q_OBJECT

public:
explicit QgsGenericFeatureSelectionManager( QObject *parent = NULL );
QgsGenericFeatureSelectionManager( const QgsFeatureIds& initialSelection, QObject *parent = NULL );

// QgsIFeatureSelection interface
virtual int selectedFeatureCount();
virtual void select( const QgsFeatureIds& ids );
virtual void deselect( const QgsFeatureIds& ids );
virtual void setSelectedFeatures( const QgsFeatureIds& ids );
virtual const QgsFeatureIds& selectedFeaturesIds() const;

private:
QgsFeatureIds mSelectedFeatures;
};

#endif // QGSGENERICFEATURESELECTIONMANAGER_H
86 changes: 86 additions & 0 deletions src/gui/attributetable/qgsifeatureselectionmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/***************************************************************************
qgsifeatureselection.h
--------------------------------------
Date : 6.6.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSIFEATURESELECTIONMANAGER_H
#define QGSIFEATURESELECTIONMANAGER_H

#include <QObject>

#include "qgsfeature.h"

/**
* Is an interface class to abstract feature selection handling.
*
* e.g. { @link QgsVectorLayer } implements this interface to manage its selections.
*/

class CORE_EXPORT QgsIFeatureSelectionManager : public QObject
{
Q_OBJECT

public:
QgsIFeatureSelectionManager( QObject* parent )
: QObject( parent ) {}

/**
* The number of features that are selected in this layer
*
* @return See description
*/
virtual int selectedFeatureCount() = 0;

/**
* Select features
*
* @param ids Feature ids to select
*/
virtual void select( const QgsFeatureIds& ids ) = 0;

/**
* Deselect features
*
* @param ids Feature ids to deselect
*/
virtual void deselect( const QgsFeatureIds& ids ) = 0;

/**
* Change selection to the new set of features. Dismisses the current selection.
* Will emit the { @link selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) } signal with the
* clearAndSelect flag set.
*
* @param ids The ids which will be the new selection
*/
virtual void setSelectedFeatures( const QgsFeatureIds& ids ) = 0;

/**
* Return reference to identifiers of selected features
*
* @return A list of { @link QgsFeatureId } 's
* @see selectedFeatures()
*/
virtual const QgsFeatureIds &selectedFeaturesIds() const = 0;

signals:
/**
* This signal is emitted when selection was changed
*
* @param selected Newly selected feature ids
* @param deselected Ids of all features which have previously been selected but are not any more
* @param clearAndSelect In case this is set to true, the old selection was dismissed and the new selection corresponds to selected
*/
void selectionChanged( const QgsFeatureIds selected, const QgsFeatureIds deselected, const bool clearAndSelect );
};

#endif // QGSIFEATURESELECTIONMANAGER_H
50 changes: 50 additions & 0 deletions src/gui/attributetable/qgsvectorlayerselectionmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/***************************************************************************
qgsvectorlayerselectionmanager.cpp
--------------------------------------
Date : 6.6.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* 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 "qgsvectorlayerselectionmanager.h"

#include "qgsvectorlayer.h"

QgsVectorLayerSelectionManager::QgsVectorLayerSelectionManager( QgsVectorLayer* layer, QObject* parent )
: QgsIFeatureSelectionManager( parent )
, mLayer( layer )
{
connect( mLayer, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
}

int QgsVectorLayerSelectionManager::selectedFeatureCount()
{
return mLayer->selectedFeatureCount();
}

void QgsVectorLayerSelectionManager::select( const QgsFeatureIds& ids )
{
mLayer->select( ids );
}

void QgsVectorLayerSelectionManager::deselect( const QgsFeatureIds& ids )
{
mLayer->deselect( ids );
}

void QgsVectorLayerSelectionManager::setSelectedFeatures( const QgsFeatureIds& ids )
{
mLayer->setSelectedFeatures( ids );
}

const QgsFeatureIds& QgsVectorLayerSelectionManager::selectedFeaturesIds() const
{
return mLayer->selectedFeaturesIds();
}
71 changes: 71 additions & 0 deletions src/gui/attributetable/qgsvectorlayerselectionmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/***************************************************************************
qgsvectorlayerselectionmanager.h
--------------------------------------
Date : 6.6.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSVECTORLAYERSELECTIONMANAGER_H
#define QGSVECTORLAYERSELECTIONMANAGER_H

#include "qgsifeatureselectionmanager.h"

class QgsVectorLayer;

class CORE_EXPORT QgsVectorLayerSelectionManager : public QgsIFeatureSelectionManager
{
Q_OBJECT

public:
explicit QgsVectorLayerSelectionManager( QgsVectorLayer* layer, QObject *parent = 0 );
/**
* The number of features that are selected in this layer
*
* @return See description
*/
virtual int selectedFeatureCount();

/**
* Select features
*
* @param ids Feature ids to select
*/
virtual void select( const QgsFeatureIds& ids );

/**
* Deselect features
*
* @param ids Feature ids to deselect
*/
virtual void deselect( const QgsFeatureIds& ids );

/**
* Change selection to the new set of features. Dismisses the current selection.
* Will emit the { @link selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) } signal with the
* clearAndSelect flag set.
*
* @param ids The ids which will be the new selection
*/
virtual void setSelectedFeatures( const QgsFeatureIds& ids );

/**
* Return reference to identifiers of selected features
*
* @return A list of { @link QgsFeatureIds }
* @see selectedFeatures()
*/
virtual const QgsFeatureIds& selectedFeaturesIds() const;

private:
QgsVectorLayer* mLayer;
};

#endif // QGSVECTORLAYERSELECTIONMANAGER_H
2 changes: 1 addition & 1 deletion tests/src/gui/testqgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void TestQgsDualView::cleanupTestCase()
void TestQgsDualView::init()
{
mDualView = new QgsDualView();
mDualView->init( mPointsLayer, mCanvas, QgsDistanceArea() );
mDualView->init( mPointsLayer, mCanvas );
}

void TestQgsDualView::cleanup()
Expand Down