QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include "qgsapplication.h"
21#include "qgsgui.h"
22#include "qgssettings.h"
23
24#include <QFileDialog>
25#include <QMessageBox>
26#include <QString>
27
28#include "moc_qgsinstallgridshiftdialog.cpp"
29
30using namespace Qt::StringLiterals;
31
33
34QgsInstallGridShiftFileDialog::QgsInstallGridShiftFileDialog( const QString &gridName, QWidget *parent )
35 : QDialog( parent )
36 , mGridName( gridName )
37{
38 setupUi( this );
40
41 mInstallButton->setText( tr( "Install %1 from Folder…" ).arg( mGridName ) );
42
43 connect( mInstallButton, &QPushButton::clicked, this, &QgsInstallGridShiftFileDialog::installFromFile );
44}
45
46void QgsInstallGridShiftFileDialog::setDescription( const QString &html )
47{
48 mSummaryLabel->setHtml( html );
49}
50
51void QgsInstallGridShiftFileDialog::setDownloadMessage( const QString &message )
52{
53 mDownloadLabel->setText( message );
54}
55
56void QgsInstallGridShiftFileDialog::installFromFile()
57{
58 QgsSettings settings;
59 const QString initialDir = settings.value( u"lastTransformGridFolder"_s, QDir::homePath(), QgsSettings::App ).toString();
60 const QString gridFilePath = QFileDialog::getOpenFileName( nullptr, tr( "Install %1" ).arg( mGridName ), initialDir, u"%1 (%1);;"_s.arg( mGridName ) + tr( "Grid Shift Files" ) + u" (*.gsb *.GSB *.tif);;"_s + QObject::tr( "All files" ) + " (*)" );
61
62 if ( gridFilePath.isEmpty() )
63 {
64 return; //canceled by the user
65 }
66
67 const QFileInfo fi( gridFilePath );
68 settings.setValue( u"lastTransformGridFolder"_s, fi.absolutePath(), QgsSettings::App );
69
70 const QString baseGridPath = QgsApplication::qgisSettingsDirPath() + u"proj"_s;
71
72 const QString destFilePath = baseGridPath + '/' + mGridName;
73 const QString destPath = QFileInfo( destFilePath ).absolutePath();
74
75 if ( !QDir( destPath ).exists() )
76 QDir().mkpath( destPath );
77
78 if ( QFile::copy( gridFilePath, destFilePath ) )
79 {
80 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 ) );
81 accept();
82 }
83 else
84 {
85 QMessageBox::critical( this, tr( "Install Grid File" ), tr( "Could not copy %1 to %2. Please check folder permissions and retry." ).arg( mGridName, destFilePath ) );
86 }
87}
88
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:224
Stores settings for use within QGIS.
Definition qgssettings.h:68
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.