QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  QFileInfo fi( gridFilePath );
62  settings.setValue( QStringLiteral( "lastTransformGridFolder" ), fi.absolutePath(), QgsSettings::App );
63 
64  const QString baseGridPath = QgsApplication::qgisSettingsDirPath() + QStringLiteral( "proj" );
65  if ( !QDir( baseGridPath ).exists() )
66  QDir().mkdir( baseGridPath );
67 
68  const QString destPath = baseGridPath + '/' + fi.fileName();
69 
70  if ( QFile::copy( gridFilePath, destPath ) )
71  {
72  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 ) );
73  accept();
74  }
75  else
76  {
77  QMessageBox::critical( this, tr( "Install Grid File" ), tr( "Could not copy %1 to %2. Please check folder permissions and retry." ).arg( mGridName, destPath ) );
78  }
79 }
80 
qgsinstallgridshiftdialog.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsSettings::App
@ App
Definition: qgssettings.h:75
qgsgui.h
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:139
QgsApplication::qgisSettingsDirPath
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
Definition: qgsapplication.cpp:993
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
qgssettings.h