Skip to content

Commit

Permalink
Use a proper model for gcp list, fix use of delegates, fix gcp
Browse files Browse the repository at this point in the history
points don't move on canvas when edited in list

(cherry picked from commit ca2dbef)
  • Loading branch information
nyalldawson committed Feb 14, 2022
1 parent 3d1bff5 commit b380a0c
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 262 deletions.
311 changes: 191 additions & 120 deletions src/app/georeferencer/qgsgcplistmodel.cpp

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions src/app/georeferencer/qgsgcplistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
#ifndef QGSGCP_LIST_TABLE_VIEW_H
#define QGSGCP_LIST_TABLE_VIEW_H

#include <QTreeView>
#include <QAbstractTableModel>
#include "qgis_app.h"
#include "qgsunittypes.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransformcontext.h"

class QgsGeorefDataPoint;
class QgsGeorefTransform;
class QgsGCPList;

class QgsGCPListModel : public QAbstractTableModel
class APP_EXPORT QgsGCPListModel : public QAbstractTableModel
{
Q_OBJECT

Expand Down Expand Up @@ -53,16 +55,33 @@ class QgsGCPListModel : public QAbstractTableModel
void setGCPList( QgsGCPList *theGCPList );
void setGeorefTransform( QgsGeorefTransform *georefTransform );

/**
* Sets the target (output) CRS for the georeferencing.
*/
void setTargetCrs( const QgsCoordinateReferenceSystem &targetCrs, const QgsCoordinateTransformContext &context );

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;

void updateModel();
/**
* Recalculates the residual values.
*/
void updateResiduals();

signals:

void pointEnabled( QgsGeorefDataPoint *pnt, int i );

private:
QgsUnitTypes::RenderUnit residualUnit() const;

QgsCoordinateReferenceSystem mTargetCrs;
QgsCoordinateTransformContext mTransformContext;

QgsGCPList *mGCPList = nullptr;
QgsGeorefTransform *mGeorefTransform = nullptr;
};
Expand Down
105 changes: 14 additions & 91 deletions src/app/georeferencer/qgsgcplistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
QgsGCPListWidget::QgsGCPListWidget( QWidget *parent )
: QTableView( parent )
, mGCPListModel( new QgsGCPListModel( this ) )
, mNonEditableDelegate( new QgsNonEditableDelegate( this ) )
, mDmsAndDdDelegate( new QgsDmsAndDdDelegate( this ) )
, mCoordDelegate( new QgsCoordDelegate( this ) )
{
Expand All @@ -48,14 +47,10 @@ QgsGCPListWidget::QgsGCPListWidget( QWidget *parent )
setAlternatingRowColors( true );

// set delegates for items
setItemDelegateForColumn( 1, mNonEditableDelegate ); // id
setItemDelegateForColumn( 2, mCoordDelegate ); // srcX
setItemDelegateForColumn( 3, mCoordDelegate ); // srcY
setItemDelegateForColumn( 4, mDmsAndDdDelegate ); // dstX
setItemDelegateForColumn( 5, mDmsAndDdDelegate ); // dstY
setItemDelegateForColumn( 6, mNonEditableDelegate ); // dX
setItemDelegateForColumn( 7, mNonEditableDelegate ); // dY
setItemDelegateForColumn( 8, mNonEditableDelegate ); // residual

connect( this, &QAbstractItemView::doubleClicked,
this, &QgsGCPListWidget::itemDoubleClicked );
Expand All @@ -64,10 +59,12 @@ QgsGCPListWidget::QgsGCPListWidget( QWidget *parent )
connect( this, &QWidget::customContextMenuRequested,
this, &QgsGCPListWidget::showContextMenu );

connect( mDmsAndDdDelegate, &QAbstractItemDelegate::closeEditor,
this, &QgsGCPListWidget::updateItemCoords );
connect( mCoordDelegate, &QAbstractItemDelegate::closeEditor,
this, &QgsGCPListWidget::updateItemCoords );
connect( mGCPListModel, &QgsGCPListModel::pointEnabled, this, [ = ]( QgsGeorefDataPoint * point, int row )
{
emit pointEnabled( point, row );
adjustTableContent();
return;
} );
}

void QgsGCPListWidget::setGCPList( QgsGCPList *theGCPList )
Expand All @@ -84,9 +81,15 @@ void QgsGCPListWidget::setGeorefTransform( QgsGeorefTransform *georefTransform )
adjustTableContent();
}

void QgsGCPListWidget::updateGCPList()
void QgsGCPListWidget::setTargetCrs( const QgsCoordinateReferenceSystem &targetCrs, const QgsCoordinateTransformContext &context )
{
mGCPListModel->setTargetCrs( targetCrs, context );
adjustTableContent();
}

void QgsGCPListWidget::updateResiduals()
{
mGCPListModel->updateModel();
mGCPListModel->updateResiduals();
adjustTableContent();
}

Expand All @@ -109,26 +112,6 @@ void QgsGCPListWidget::itemClicked( const QModelIndex &index )
{
const QModelIndex sourceIndex = static_cast<const QSortFilterProxyModel *>( model() )->mapToSource( index );

#if 0
QStandardItem *item = mGCPListModel->item( sourceIndex.row(), sourceIndex.column() );
if ( item->isCheckable() )
{
QgsGeorefDataPoint *p = mGCPList->at( sourceIndex.row() );
if ( item->checkState() == Qt::Checked )
{
p->setEnabled( true );
}
else // Qt::Unchecked
{
p->setEnabled( false );
}

mGCPListModel->updateModel();
emit pointEnabled( p, sourceIndex.row() );
adjustTableContent();
}
#endif

mPrevRow = sourceIndex.row();
mPrevColumn = sourceIndex.column();
}
Expand All @@ -148,22 +131,6 @@ void QgsGCPListWidget::keyPressEvent( QKeyEvent *e )
}
}
}
else if ( e->key() == Qt::Key_Space )
{
const QModelIndex index = currentIndex();
if ( index.isValid() )
{
const QModelIndex sourceIndex = static_cast<const QSortFilterProxyModel *>( model() )->mapToSource( index );
QgsGeorefDataPoint *p = mGCPList->at( sourceIndex.row() );
p->setEnabled( !p->isEnabled() );

mGCPListModel->updateModel();
emit pointEnabled( p, sourceIndex.row() );
adjustTableContent();
setCurrentIndex( model()->index( index.row(), index.column() ) );
return;
}
}
else if ( e->key() == Qt::Key_Up )
{
const QModelIndex index = currentIndex();
Expand Down Expand Up @@ -203,45 +170,6 @@ void QgsGCPListWidget::keyPressEvent( QKeyEvent *e )
e->ignore();
}

void QgsGCPListWidget::updateItemCoords( QWidget *editor )
{
QLineEdit *lineEdit = qobject_cast<QLineEdit *>( editor );
QgsGeorefDataPoint *dataPoint = mGCPList->at( mPrevRow );
if ( lineEdit )
{
const double value = lineEdit->text().toDouble();
QgsPointXY newMapCoords( dataPoint->destinationPoint() );

QgsPointXY newSourceCoords( dataPoint->sourcePoint() );
if ( mPrevColumn == 2 ) // srcX
{
newSourceCoords.setX( value );
}
else if ( mPrevColumn == 3 ) // srcY
{
newSourceCoords.setY( value );
}
else if ( mPrevColumn == 4 ) // dstX
{
newMapCoords.setX( value );
}
else if ( mPrevColumn == 5 ) // dstY
{
newMapCoords.setY( value );
}
else
{
return;
}

dataPoint->setSourcePoint( newSourceCoords );
dataPoint->setDestinationPoint( newMapCoords );
}

dataPoint->updateCoords();
updateGCPList();
}

void QgsGCPListWidget::showContextMenu( QPoint p )
{
if ( !mGCPList || 0 == mGCPList->count() )
Expand Down Expand Up @@ -277,11 +205,6 @@ void QgsGCPListWidget::removeRow()
emit deleteDataPoint( index.row() );
}

void QgsGCPListWidget::editCell()
{
edit( currentIndex() );
}

void QgsGCPListWidget::jumpToSourcePoint( const QModelIndex &modelIndex )
{
const QgsPointXY sourcePoint = mGCPListModel->data( modelIndex, static_cast< int >( QgsGCPListModel::Role::SourcePointRole ) ).value< QgsPointXY >();
Expand Down
19 changes: 14 additions & 5 deletions src/app/georeferencer/qgsgcplistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <QTableView>

class QgsDoubleSpinBoxDelegate;
class QgsNonEditableDelegate;
class QgsDmsAndDdDelegate;
class QgsCoordDelegate;

Expand All @@ -27,6 +26,8 @@ class QgsGCPListModel;
class QgsGeorefTransform;
class QgsGeorefDataPoint;
class QgsPointXY;
class QgsCoordinateReferenceSystem;
class QgsCoordinateTransformContext;

class QgsGCPListWidget : public QTableView
{
Expand All @@ -36,8 +37,19 @@ class QgsGCPListWidget : public QTableView

void setGCPList( QgsGCPList *theGCPList );
void setGeorefTransform( QgsGeorefTransform *georefTransform );

/**
* Sets the target (output) CRS for the georeferencing.
*/
void setTargetCrs( const QgsCoordinateReferenceSystem &targetCrs, const QgsCoordinateTransformContext &context );

QgsGCPList *gcpList() { return mGCPList; }
void updateGCPList();

/**
* Recalculates the residual values.
*/
void updateResiduals();

void closeEditors();

void keyPressEvent( QKeyEvent *e ) override;
Expand All @@ -53,10 +65,8 @@ class QgsGCPListWidget : public QTableView
void deleteDataPoint( int index );

private slots:
void updateItemCoords( QWidget *editor );
void showContextMenu( QPoint );
void removeRow();
void editCell();
void jumpToSourcePoint( const QModelIndex &modelIndex );

private:
Expand All @@ -67,7 +77,6 @@ class QgsGCPListWidget : public QTableView
QgsGCPList *mGCPList = nullptr;
QgsGCPListModel *mGCPListModel = nullptr;

QgsNonEditableDelegate *mNonEditableDelegate = nullptr;
QgsDmsAndDdDelegate *mDmsAndDdDelegate = nullptr;
QgsCoordDelegate *mCoordDelegate = nullptr;

Expand Down
12 changes: 2 additions & 10 deletions src/app/georeferencer/qgsgeorefdatapoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,13 @@ QgsGeorefDataPoint::~QgsGeorefDataPoint()
void QgsGeorefDataPoint::setSourcePoint( const QgsPointXY &p )
{
mGcpPoint.setSourcePoint( p );
mGCPSourceItem->update();
mGCPDestinationItem->update();
updateCoords();
}

void QgsGeorefDataPoint::setDestinationPoint( const QgsPointXY &p )
{
mGcpPoint.setDestinationPoint( p );
if ( mGCPSourceItem )
{
mGCPSourceItem->update();
}
if ( mGCPDestinationItem )
{
mGCPDestinationItem->update();
}
updateCoords();
}

QgsPointXY QgsGeorefDataPoint::transformedDestinationPoint( const QgsCoordinateReferenceSystem &targetCrs, const QgsCoordinateTransformContext &context ) const
Expand Down
10 changes: 0 additions & 10 deletions src/app/georeferencer/qgsgeorefdelegates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
#include "qgsgeorefdelegates.h"
#include <cmath>

// ------------------------ QgsNonEditableDelegate ------------------------- //
QgsNonEditableDelegate::QgsNonEditableDelegate( QWidget *parent )
: QStyledItemDelegate( parent )
{
}

// ------------------------- QgsDmsAndDdDelegate --------------------------- //
QgsDmsAndDdDelegate::QgsDmsAndDdDelegate( QWidget *parent )
: QStyledItemDelegate( parent )
Expand Down Expand Up @@ -63,8 +57,6 @@ void QgsDmsAndDdDelegate::setModelData( QWidget *editor, QAbstractItemModel *mod
value = stringValue.toDouble();

model->setData( index, value, Qt::EditRole );
model->setData( index, value, Qt::DisplayRole );
model->setData( index, value, Qt::ToolTipRole );
}

void QgsDmsAndDdDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -121,8 +113,6 @@ void QgsCoordDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QString stringValue = lineEdit->text();
const double value = stringValue.toDouble();
model->setData( index, value, Qt::EditRole );
model->setData( index, value, Qt::DisplayRole );
model->setData( index, value, Qt::ToolTipRole );
}

void QgsCoordDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
Expand Down
17 changes: 0 additions & 17 deletions src/app/georeferencer/qgsgeorefdelegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@

#include <QStyledItemDelegate>

class QgsNonEditableDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
explicit QgsNonEditableDelegate( QWidget *parent = nullptr );

QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index ) const override
{
Q_UNUSED( parent )
Q_UNUSED( option )
Q_UNUSED( index )
return nullptr;
}
};

class QgsDmsAndDdDelegate : public QStyledItemDelegate
{
Q_OBJECT
Expand Down

0 comments on commit b380a0c

Please sign in to comment.