Skip to content

Commit

Permalink
Add delete feature possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 31, 2016
1 parent 81d4f61 commit 920c334
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 13 deletions.
3 changes: 3 additions & 0 deletions images/images.qrc
Expand Up @@ -28,4 +28,7 @@
<file alias="cancel.png">themes/holodark/cancel.png</file>
<file alias="edit.png">themes/holodark/edit.png</file>
</qresource>
<qresource prefix="/">
<file>themes/holodark/delete_forever.svg</file>
</qresource>
</RCC>
63 changes: 63 additions & 0 deletions images/themes/holodark/delete_forever.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/feature.cpp
Expand Up @@ -40,6 +40,9 @@ void Feature::setGeometry( const QgsGeometry& geom )

void Feature::create()
{
if ( !mLayer )
return;

mLayer->addFeature( mFeature );
}

Expand All @@ -53,5 +56,18 @@ QString Feature::displayText() const

bool Feature::readOnly() const
{
if ( !mLayer )
return true;

return mLayer->readOnly();
}

bool Feature::remove()
{
if ( !mLayer )
return false;

mLayer->startEditing();
mLayer->deleteFeature( mFeature.id() );
return mLayer->commitChanges();
}
2 changes: 2 additions & 0 deletions src/feature.h
Expand Up @@ -78,6 +78,8 @@ class Feature

Q_INVOKABLE bool readOnly() const;

Q_INVOKABLE bool remove();

private:
QgsFeature mFeature;
QgsVectorLayer* mLayer;
Expand Down
25 changes: 25 additions & 0 deletions src/featurelistmodel.cpp
Expand Up @@ -18,6 +18,8 @@
#include "featurelistmodel.h"

#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>

#include <QDebug>

FeatureListModel::FeatureListModel( QObject *parent )
Expand Down Expand Up @@ -46,6 +48,7 @@ void FeatureListModel::setFeatures( const QList<QgsMapToolIdentify::IdentifyResu
Feature* f = new Feature( res.mFeature, qobject_cast<QgsVectorLayer*>( res.mLayer ) );
mFeatures.append( f );
connect( f->layer(), SIGNAL( layerDeleted() ), this, SLOT( layerDeleted() ), Qt::UniqueConnection );
connect( f->layer(), SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ), Qt::UniqueConnection );
}

endResetModel();
Expand Down Expand Up @@ -82,6 +85,7 @@ QHash<int, QByteArray> FeatureListModel::roleNames() const
roleNames[FeatureIdRole] = "featureId";
roleNames[FeatureRole] = "feature";
roleNames[LayerNameRole] = "layerName";
roleNames[DeleteFeatureRole] = "deleteFeatureCapability";

return roleNames;
}
Expand Down Expand Up @@ -135,6 +139,10 @@ QVariant FeatureListModel::data( const QModelIndex& index, int role ) const

case LayerNameRole:
return feature->layer()->name();

case DeleteFeatureRole:
bool a = !feature->layer()->readOnly() && ( feature->layer()->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures );
return a;
}

return QVariant();
Expand Down Expand Up @@ -207,3 +215,20 @@ void FeatureListModel::layerDeleted()

removeRows( firstRowToRemove, count );
}

void FeatureListModel::featureDeleted( QgsFeatureId fid )
{
QgsVectorLayer* l = qobject_cast<QgsVectorLayer*>( sender() );
Q_ASSERT( l );

int i = 0;
Q_FOREACH( Feature* f, mFeatures )
{
if ( f->layer() == l && f->id() == fid )
{
removeRows( i, 1 );
break;
}
++i;
}
}
5 changes: 4 additions & 1 deletion src/featurelistmodel.h
Expand Up @@ -35,7 +35,8 @@ class FeatureListModel : public QAbstractItemModel
{
FeatureIdRole = Qt::UserRole + 1,
FeatureRole,
LayerNameRole
LayerNameRole,
DeleteFeatureRole
};

explicit FeatureListModel( QObject *parent = 0 );
Expand Down Expand Up @@ -78,6 +79,8 @@ class FeatureListModel : public QAbstractItemModel
private slots:
void layerDeleted();

void featureDeleted( QgsFeatureId fid );

private:
inline Feature* toFeature( const QModelIndex& index ) const
{
Expand Down
1 change: 1 addition & 0 deletions src/featurelistmodelhighlight.cpp
Expand Up @@ -52,6 +52,7 @@ void FeatureListModelHighlight::onDataChanged()
if ( mModel )
{
connect( mModel, SIGNAL( modelReset() ), this, SLOT( onModelDataChanged() ) );
connect( mModel, SIGNAL( rowsRemoved( QModelIndex,int,int ) ), this, SLOT( onModelDataChanged() ) );
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/qml/Button.qml
Expand Up @@ -8,6 +8,7 @@ Item {
property alias iconSource: button.iconSource
property alias checked: button.checked
property alias checkable: button.checkable
property string style: "dark"

signal pressAndHold
signal clicked
Expand All @@ -24,8 +25,8 @@ Item {
implicitWidth: 100
implicitHeight: 25
border.width: control.activeFocus ? 2 : 1
border.color: "#888"
color: "#cc555555"
border.color: style === "dark" ? "#888" : "#ccbbbbbb"
color: style === "dark" ? "#cc555555" : "#ccbbbbbb"
}
}

Expand Down
39 changes: 29 additions & 10 deletions src/qml/FeatureListForm.qml
Expand Up @@ -16,7 +16,7 @@
***************************************************************************/

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls 1.4 as Controls
import QtQuick.Layouts 1.1
import org.qgis 1.0

Expand Down Expand Up @@ -125,7 +125,7 @@ Rectangle {

Text {
id: featureText
anchors { leftMargin: 10; left: parent.left; right: parent.right; verticalCenter: parent.verticalCenter }
anchors { leftMargin: 10; left: parent.left; right: deleteButton.left; verticalCenter: parent.verticalCenter }
font.bold: true
text: display
}
Expand All @@ -143,14 +143,6 @@ Rectangle {
}
}

/* bottom border */
Rectangle {
anchors.bottom: parent.bottom
height: 1
color: "lightGray"
width: parent.width
}

MouseArea {
anchors.fill: parent

Expand All @@ -164,6 +156,33 @@ Rectangle {
featureForm.selection.selection = index
}
}

Button {
id: deleteButton

width: 48*dp
height: 48*dp

visible: deleteFeatureCapability

anchors { top: parent.top; right: parent.right }

iconSource: "/themes/holodark/delete_forever.svg"

style: "light"

onClicked: {
feature.remove()
}
}

/* bottom border */
Rectangle {
anchors.bottom: parent.bottom
height: 1
color: "lightGray"
width: parent.width
}
}

/* bottom border */
Expand Down

0 comments on commit 920c334

Please sign in to comment.