Skip to content

Commit

Permalink
define default transforms in global options
Browse files Browse the repository at this point in the history
adds QgsCoordinateTransformContext::readSettings()/writeSettings to read/write from settings
  • Loading branch information
3nids authored and nyalldawson committed Dec 15, 2017
1 parent 8798cf0 commit 44ed7bd
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 172 deletions.
14 changes: 14 additions & 0 deletions python/core/qgscoordinatetransformcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@




class QgsCoordinateTransformContext
{
%Docstring
Expand Down Expand Up @@ -149,6 +150,19 @@ class QgsCoordinateTransformContext
%End


void readSettings();
%Docstring
Reads the context's state from application settings.
.. seealso:: readSettings()
%End

void writeSettings();
%Docstring
Write the context's state to application settings.
.. seealso:: writeSettings()
%End


};


Expand Down
159 changes: 62 additions & 97 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "qgsattributetablefiltermodel.h"
#include "qgsrasterformatsaveoptionswidget.h"
#include "qgsrasterpyramidsoptionswidget.h"
#include "qgsdatumtransformdialog.h"
#include "qgsdialog.h"
#include "qgscomposer.h"
#include "qgscolorschemeregistry.h"
Expand Down Expand Up @@ -454,56 +455,21 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
leProjectGlobalCrs->setCrs( mDefaultCrs );
leProjectGlobalCrs->setOptionVisible( QgsProjectionSelectionWidget::DefaultCrs, false );

//default datum transformations
mSettings->beginGroup( QStringLiteral( "/Projections" ) );

mShowDatumTransformDialogCheckBox->setChecked( mSettings->value( QStringLiteral( "/Projections/showDatumTransformDialog" ), false ).toBool() );

QStringList projectionKeys = mSettings->allKeys();

//collect src and dest entries that belong together
QMap< QPair< QString, QString >, QPair< int, int > > transforms;
QStringList::const_iterator pkeyIt = projectionKeys.constBegin();
for ( ; pkeyIt != projectionKeys.constEnd(); ++pkeyIt )
{
if ( pkeyIt->contains( QLatin1String( "srcTransform" ) ) || pkeyIt->contains( QLatin1String( "destTransform" ) ) )
{
QStringList split = pkeyIt->split( '/' );
QString srcAuthId, destAuthId;
if ( ! split.isEmpty() )
{
srcAuthId = split.at( 0 );
}
if ( split.size() > 1 )
{
destAuthId = split.at( 1 ).split( '_' ).at( 0 );
}

if ( pkeyIt->contains( QLatin1String( "srcTransform" ) ) )
{
transforms[ qMakePair( srcAuthId, destAuthId )].first = mSettings->value( *pkeyIt ).toInt();
}
else if ( pkeyIt->contains( QLatin1String( "destTransform" ) ) )
{
transforms[ qMakePair( srcAuthId, destAuthId )].second = mSettings->value( *pkeyIt ).toInt();
}
}
}
mSettings->endGroup();
// Datum transforms
QgsCoordinateTransformContext context;
context.readSettings();
mDefaultDatumTransformTableModel->setTransformContext( context );
mDefaultDatumTransformTableView->setModel( mDefaultDatumTransformTableModel );
mDefaultDatumTransformTableView->resizeColumnToContents( 0 );
mDefaultDatumTransformTableView->horizontalHeader()->show();
mDefaultDatumTransformTableView->setSelectionMode( QAbstractItemView::SingleSelection );
mDefaultDatumTransformTableView->setSelectionBehavior( QAbstractItemView::SelectRows );
connect( mAddDefaultTransformButton, &QToolButton::clicked, this, &QgsOptions::addDefaultDatumTransform );
connect( mRemoveDefaultTransformButton, &QToolButton::clicked, this, &QgsOptions::removeDefaultDatumTransform );
connect( mEditDefaultTransformButton, &QToolButton::clicked, this, &QgsOptions::editDefaultDatumTransform );

QMap< QPair< QString, QString >, QPair< int, int > >::const_iterator transformIt = transforms.constBegin();
for ( ; transformIt != transforms.constEnd(); ++transformIt )
{
const QPair< int, int > &v = transformIt.value();
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText( 0, transformIt.key().first );
item->setText( 1, transformIt.key().second );
item->setText( 2, QString::number( v.first ) );
item->setText( 3, QString::number( v.second ) );
mDefaultDatumTransformTreeWidget->addTopLevelItem( item );
}
connect( mAddDefaultTransformButton, &QAbstractButton::clicked, this, &QgsOptions::addDefaultTransformation );
connect( mRemoveDefaultTransformButton, &QAbstractButton::clicked, this, &QgsOptions::removeDefaultTransformation );

// Set the units for measuring
mDistanceUnitsComboBox->addItem( tr( "Meters" ), QgsUnitTypes::DistanceMeters );
Expand Down Expand Up @@ -1560,7 +1526,7 @@ void QgsOptions::saveOptions()
mStyleSheetBuilder->saveToSettings( mStyleSheetNewOpts );
}

saveDefaultDatumTransformations();
mDefaultDatumTransformTableModel->transformContext().writeSettings();

mLocatorOptionsWidget->commitChanges();

Expand Down Expand Up @@ -2255,72 +2221,71 @@ void QgsOptions::saveMinMaxLimits( QComboBox *cbox, const QString &name )
mSettings->setValue( "/Raster/defaultContrastEnhancementLimits/" + name, value );
}

void QgsOptions::removeDefaultTransformation()
void QgsOptions::addDefaultDatumTransform()
{
QList<QTreeWidgetItem *> items = mDefaultDatumTransformTreeWidget->selectedItems();
for ( int i = 0; i < items.size(); ++i )
QgsDatumTransformDialog *dlg = new QgsDatumTransformDialog();
if ( dlg->exec() )
{
int idx = mDefaultDatumTransformTreeWidget->indexOfTopLevelItem( items.at( i ) );
if ( idx >= 0 )
{
delete mDefaultDatumTransformTreeWidget->takeTopLevelItem( idx );
}
QPair< QPair<QgsCoordinateReferenceSystem, int>, QPair<QgsCoordinateReferenceSystem, int > > dt = dlg->selectedDatumTransforms();
QgsCoordinateTransformContext context = mDefaultDatumTransformTableModel->transformContext();
context.addSourceDestinationDatumTransform( dt.first.first, dt.second.first, dt.first.second, dt.second.second );
mDefaultDatumTransformTableModel->setTransformContext( context );
}
}

void QgsOptions::addDefaultTransformation()
void QgsOptions::removeDefaultDatumTransform()
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText( 0, QLatin1String( "" ) );
item->setText( 1, QLatin1String( "" ) );
item->setText( 2, QLatin1String( "" ) );
item->setText( 3, QLatin1String( "" ) );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mDefaultDatumTransformTreeWidget->addTopLevelItem( item );
}

void QgsOptions::saveDefaultDatumTransformations()
{
QgsSettings s;
s.beginGroup( QStringLiteral( "/Projections" ) );
QStringList groupKeys = s.allKeys();
QStringList::const_iterator groupKeyIt = groupKeys.constBegin();
for ( ; groupKeyIt != groupKeys.constEnd(); ++groupKeyIt )
QModelIndexList selectedIndexes = mDefaultDatumTransformTableView->selectionModel()->selectedIndexes();
if ( selectedIndexes.count() > 0 )
{
if ( groupKeyIt->contains( QLatin1String( "srcTransform" ) ) || groupKeyIt->contains( QLatin1String( "destTransform" ) ) )
{
s.remove( *groupKeyIt );
}
mDefaultDatumTransformTableModel->removeTransform( selectedIndexes );
}
}

int nDefaultTransforms = mDefaultDatumTransformTreeWidget->topLevelItemCount();
for ( int i = 0; i < nDefaultTransforms; ++i )
void QgsOptions::editDefaultDatumTransform()
{
QModelIndexList selectedIndexes = mDefaultDatumTransformTableView->selectionModel()->selectedIndexes();
if ( selectedIndexes.count() > 0 )
{
QTreeWidgetItem *item = mDefaultDatumTransformTreeWidget->topLevelItem( i );
QString srcAuthId = item->text( 0 );
QString destAuthId = item->text( 1 );
if ( srcAuthId.isEmpty() || destAuthId.isEmpty() )
{
continue;
}

bool conversionOk;
int srcDatumTransform = item->text( 2 ).toInt( &conversionOk );
if ( conversionOk )
QgsCoordinateReferenceSystem sourceCrs;
QgsCoordinateReferenceSystem destinationCrs;
int sourceTransform = -1;
int destinationTransform = -1;
for ( QModelIndexList::const_iterator it = selectedIndexes.constBegin(); it != selectedIndexes.constEnd(); it ++ )
{
s.setValue( srcAuthId + "//" + destAuthId + "_srcTransform", srcDatumTransform );
if ( it->column() == QgsDatumTransformTableModel::SourceCrsColumn )
{
sourceCrs = QgsCoordinateReferenceSystem( mDefaultDatumTransformTableModel->data( *it, Qt::DisplayRole ).toString() );
}
if ( it->column() == QgsDatumTransformTableModel::DestinationCrsColumn )
{
destinationCrs = QgsCoordinateReferenceSystem( mDefaultDatumTransformTableModel->data( *it, Qt::DisplayRole ).toString() );
}
if ( it->column() == QgsDatumTransformTableModel::SourceTransformColumn )
{
sourceTransform = mDefaultDatumTransformTableModel->data( *it, Qt::UserRole ).toInt();
}
if ( it->column() == QgsDatumTransformTableModel::DestinationTransformColumn )
{
destinationTransform = mDefaultDatumTransformTableModel->data( *it, Qt::UserRole ).toInt();
}
}
int destDatumTransform = item->text( 3 ).toInt( &conversionOk );
if ( conversionOk )
if ( sourceCrs.isValid() && destinationCrs.isValid() &&
( sourceTransform != -1 || destinationTransform != -1 ) )
{
s.setValue( srcAuthId + "//" + destAuthId + "_destTransform", destDatumTransform );
QgsDatumTransformDialog *dlg = new QgsDatumTransformDialog( sourceCrs, destinationCrs, qMakePair( sourceTransform, destinationTransform ) );
if ( dlg->exec() )
{
QPair< QPair<QgsCoordinateReferenceSystem, int>, QPair<QgsCoordinateReferenceSystem, int > > dt = dlg->selectedDatumTransforms();
QgsCoordinateTransformContext context = mDefaultDatumTransformTableModel->transformContext();
// QMap::insert takes care of replacing existing value
context.addSourceDestinationDatumTransform( sourceCrs, destinationCrs, dt.first.second, dt.second.second );
mDefaultDatumTransformTableModel->setTransformContext( context );
}
}
}

s.endGroup();
}


void QgsOptions::addColor()
{
QColor newColor = QgsColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select color" ), true );
Expand Down
17 changes: 11 additions & 6 deletions src/app/qgsoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgshelp.h"

#include "qgscoordinatereferencesystem.h"
#include "qgsdatumtransformtablemodel.h"

#include <QList>
#include "qgis_app.h"
Expand Down Expand Up @@ -227,11 +228,17 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
/* Save the list of which gdal drivers should be used. */
void saveGdalDriverList();

void removeDefaultTransformation();
void addDefaultTransformation();

void addColor();

//! add a new datum transform
void addDefaultDatumTransform();

//! remove currently selected datum transform
void removeDefaultDatumTransform();

//! edit currently selected datum transform
void editDefaultDatumTransform();

private:
QgsSettings *mSettings = nullptr;
QStringList i18nList();
Expand All @@ -248,8 +255,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
//! Generate table row for custom environment variables
void addCustomEnvVarRow( const QString &varName, const QString &varVal, const QString &varApply = QString() );

void saveDefaultDatumTransformations();

void showHelp();

QListWidgetItem *addScaleToScaleList( const QString &newScale );
Expand All @@ -267,7 +272,7 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

QList< QgsOptionsPageWidget * > mAdditionalOptionWidgets;
QgsLocatorOptionsWidget *mLocatorOptionsWidget = nullptr;

QgsDatumTransformTableModel *mDefaultDatumTransformTableModel = new QgsDatumTransformTableModel( this );
};

#endif // #ifndef QGSOPTIONS_H
1 change: 0 additions & 1 deletion src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,6 @@ void QgsProjectProperties::editDatumTransform()
}
}
}

}

void QgsProjectProperties::cbxWFSPubliedStateChanged( int aIdx )
Expand Down
Loading

0 comments on commit 44ed7bd

Please sign in to comment.