QGIS API Documentation  3.25.0-Master (dec16ba68b)
qgsinstallgridshiftdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsinstallgridshiftdialog.cpp
3  -------------------
4  begin : September 2019
5  copyright : (C) 2019 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsgui.h"
20 #include "qgssettings.h"
21 #include "qgsapplication.h"
22 
23 #include <QFileDialog>
24 #include <QMessageBox>
25 
27 
28 QgsInstallGridShiftFileDialog::QgsInstallGridShiftFileDialog( const QString &gridName, QWidget *parent )
29  : QDialog( parent )
30  , mGridName( gridName )
31 {
32  setupUi( this );
34 
35  mInstallButton->setText( tr( "Install %1 from Folder…" ).arg( mGridName ) );
36 
37  connect( mInstallButton, &QPushButton::clicked, this, &QgsInstallGridShiftFileDialog::installFromFile );
38 }
39 
40 void QgsInstallGridShiftFileDialog::setDescription( const QString &html )
41 {
42  mSummaryLabel->setHtml( html );
43 }
44 
45 void QgsInstallGridShiftFileDialog::setDownloadMessage( const QString &message )
46 {
47  mDownloadLabel->setText( message );
48 }
49 
50 void QgsInstallGridShiftFileDialog::installFromFile()
51 {
52  QgsSettings settings;
53  const QString initialDir = settings.value( QStringLiteral( "lastTransformGridFolder" ), QDir::homePath(), QgsSettings::App ).toString();
54  const QString gridFilePath = QFileDialog::getOpenFileName( nullptr, tr( "Install %1" ).arg( mGridName ), initialDir, QStringLiteral( "%1 (%1);;" ).arg( mGridName ) + tr( "Grid Shift Files" ) + QStringLiteral( " (*.gsb *.GSB *.tif);;" ) + QObject::tr( "All files" ) + " (*)" );
55 
56  if ( gridFilePath.isEmpty() )
57  {
58  return; //canceled by the user
59  }
60 
61  const QFileInfo fi( gridFilePath );
62  settings.setValue( QStringLiteral( "lastTransformGridFolder" ), fi.absolutePath(), QgsSettings::App );
63 
64  const QString baseGridPath = QgsApplication::qgisSettingsDirPath() + QStringLiteral( "proj" );
65 
66  const QString destFilePath = baseGridPath + '/' + mGridName;
67  const QString destPath = QFileInfo( destFilePath ).absolutePath();
68 
69  if ( !QDir( destPath ).exists() )
70  QDir().mkpath( destPath );
71 
72  if ( QFile::copy( gridFilePath, destFilePath ) )
73  {
74  QMessageBox::information( this, tr( "Install Grid File" ), tr( "The %1 grid shift file has been successfully installed. Please restart QGIS for this change to take effect." ).arg( mGridName ) );
75  accept();
76  }
77  else
78  {
79  QMessageBox::critical( this, tr( "Install Grid File" ), tr( "Could not copy %1 to %2. Please check folder permissions and retry." ).arg( mGridName, destFilePath ) );
80  }
81 }
82 
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:180
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.