Skip to content
Permalink
Browse files

update model [vertex editor] (#9212) (#9238)

* [vertex editor] update model

instead of erasing/creating each time

* do not save layer in model since it's already in the selected feature
  • Loading branch information
3nids committed Feb 22, 2019
1 parent 87c9e18 commit 006ddf5d909fd4993e1ae52522e5543840a37e1c
Showing with 44 additions and 50 deletions.
  1. +35 −38 src/app/vertextool/qgsvertexeditor.cpp
  2. +9 −12 src/app/vertextool/qgsvertexeditor.h
@@ -38,39 +38,38 @@
static const int MIN_RADIUS_ROLE = Qt::UserRole + 1;


QgsVertexEditorModel::QgsVertexEditorModel( QgsVectorLayer *layer, QgsSelectedFeature *selectedFeature, QgsMapCanvas *canvas, QObject *parent )
QgsVertexEditorModel::QgsVertexEditorModel( QgsMapCanvas *canvas, QObject *parent )
: QAbstractTableModel( parent )
, mLayer( layer )
, mSelectedFeature( selectedFeature )
, mCanvas( canvas )
, mHasZ( false )
, mHasM( false )
, mHasR( true ) //always show for now - avoids scanning whole feature for curves TODO - avoid this
, mZCol( -1 )
, mMCol( -1 )
, mRCol( -1 )
{
QWidget *parentWidget = dynamic_cast< QWidget * >( parent );
if ( parentWidget )
mWidgetFont = parentWidget->font();
}

QgsWkbTypes::Type layerWKBType = mLayer->wkbType();
void QgsVertexEditorModel::setFeature( QgsSelectedFeature *selectedFeature )
{
beginResetModel();

mHasZ = QgsWkbTypes::hasZ( layerWKBType );
mHasM = QgsWkbTypes::hasM( layerWKBType );
mSelectedFeature = selectedFeature;
if ( mSelectedFeature && mSelectedFeature->layer() )
{
QgsWkbTypes::Type layerWKBType = mSelectedFeature->layer()->wkbType();

if ( mHasZ )
mZCol = 2;
mHasZ = QgsWkbTypes::hasZ( layerWKBType );
mHasM = QgsWkbTypes::hasM( layerWKBType );

if ( mHasM )
mMCol = 2 + ( mHasZ ? 1 : 0 );
if ( mHasZ )
mZCol = 2;

if ( mHasR )
mRCol = 2 + ( mHasZ ? 1 : 0 ) + ( mHasM ? 1 : 0 );
if ( mHasM )
mMCol = 2 + ( mHasZ ? 1 : 0 );

QWidget *parentWidget = dynamic_cast< QWidget * >( parent );
if ( parentWidget )
{
mWidgetFont = parentWidget->font();
if ( mHasR )
mRCol = 2 + ( mHasZ ? 1 : 0 ) + ( mHasM ? 1 : 0 );
}

endResetModel();
}

int QgsVertexEditorModel::rowCount( const QModelIndex &parent ) const
@@ -84,7 +83,10 @@ int QgsVertexEditorModel::rowCount( const QModelIndex &parent ) const
int QgsVertexEditorModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent );
return 2 + ( mHasZ ? 1 : 0 ) + ( mHasM ? 1 : 0 ) + ( mHasR ? 1 : 0 );
if ( !mSelectedFeature )
return 0;
else
return 2 + ( mHasZ ? 1 : 0 ) + ( mHasM ? 1 : 0 ) + ( mHasR ? 1 : 0 );
}

QVariant QgsVertexEditorModel::data( const QModelIndex &index, int role ) const
@@ -207,7 +209,7 @@ bool QgsVertexEditorModel::setData( const QModelIndex &index, const QVariant &va
{
return false;
}
if ( !mSelectedFeature || index.row() >= mSelectedFeature->vertexMap().count() )
if ( !mSelectedFeature || !mSelectedFeature->layer() || index.row() >= mSelectedFeature->vertexMap().count() )
{
return false;
}
@@ -247,10 +249,10 @@ bool QgsVertexEditorModel::setData( const QModelIndex &index, const QVariant &va
double m = ( index.column() == mMCol ? value.toDouble() : mSelectedFeature->vertexMap().at( index.row() )->point().m() );
QgsPoint p( QgsWkbTypes::PointZM, x, y, z, m );

mLayer->beginEditCommand( QObject::tr( "Moved vertices" ) );
mLayer->moveVertex( p, mSelectedFeature->featureId(), index.row() );
mLayer->endEditCommand();
mLayer->triggerRepaint();
mSelectedFeature->layer()->beginEditCommand( QObject::tr( "Moved vertices" ) );
mSelectedFeature->layer()->moveVertex( p, mSelectedFeature->featureId(), index.row() );
mSelectedFeature->layer()->endEditCommand();
mSelectedFeature->layer()->triggerRepaint();

return false;
}
@@ -296,6 +298,7 @@ bool QgsVertexEditorModel::calcR( int row, double &r, double &minRadius ) const

QgsVertexEditor::QgsVertexEditor( QgsMapCanvas *canvas )
: mCanvas( canvas )
, mVertexModel( new QgsVertexEditorModel( mCanvas, this ) )
{
setWindowTitle( tr( "Vertex Editor" ) );
setObjectName( QStringLiteral( "VertexEditor" ) );
@@ -320,6 +323,8 @@ QgsVertexEditor::QgsVertexEditor( QgsMapCanvas *canvas )
mTableView->setItemDelegateForColumn( 3, new CoordinateItemDelegate( this ) );
mTableView->setItemDelegateForColumn( 4, new CoordinateItemDelegate( this ) );
mTableView->setVisible( false );
mTableView->setModel( mVertexModel );
connect( mTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVertexEditor::updateVertexSelection );

layout->addWidget( mTableView );
layout->addWidget( mHintLabel );
@@ -329,24 +334,16 @@ QgsVertexEditor::QgsVertexEditor( QgsMapCanvas *canvas )

void QgsVertexEditor::updateEditor( QgsSelectedFeature *selectedFeature )
{
if ( mSelectedFeature )
{
delete mVertexModel;
mVertexModel = nullptr;
}

mSelectedFeature = selectedFeature;

mVertexModel->setFeature( mSelectedFeature );

if ( mSelectedFeature )
{
// TODO We really should just update the model itself.
mVertexModel = new QgsVertexEditorModel( mSelectedFeature->layer(), mSelectedFeature, mCanvas, this );
mTableView->setModel( mVertexModel );

mHintLabel->setVisible( false );
mTableView->setVisible( true );

connect( mTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVertexEditor::updateVertexSelection );
connect( mSelectedFeature, &QgsSelectedFeature::selectionChanged, this, &QgsVertexEditor::updateTableSelection );
}
else
@@ -25,7 +25,6 @@
#include <QStyledItemDelegate>

class QgsMapCanvas;
class QgsRubberBand;
class QgsSelectedFeature;
class QgsVectorLayer;

@@ -37,9 +36,9 @@ class QgsVertexEditorModel : public QAbstractTableModel
Q_OBJECT
public:

QgsVertexEditorModel( QgsVectorLayer *layer,
QgsSelectedFeature *selectedFeature,
QgsMapCanvas *canvas, QObject *parent = nullptr );
QgsVertexEditorModel( QgsMapCanvas *canvas, QObject *parent = nullptr );

void setFeature( QgsSelectedFeature *selectedFeature );

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
@@ -49,18 +48,16 @@ class QgsVertexEditorModel : public QAbstractTableModel
Qt::ItemFlags flags( const QModelIndex &index ) const override;

private:

QgsVectorLayer *mLayer = nullptr;
QgsSelectedFeature *mSelectedFeature = nullptr;
QgsMapCanvas *mCanvas = nullptr;

bool mHasZ;
bool mHasM;
bool mHasR;
bool mHasZ = false;
bool mHasM = false;
bool mHasR = true; //always show for now - avoids scanning whole feature for curves TODO - avoid this

int mZCol;
int mMCol;
int mRCol;
int mZCol = -1;
int mMCol = -1;
int mRCol = -1;

QFont mWidgetFont;

0 comments on commit 006ddf5

Please sign in to comment.
You can’t perform that action at this time.