-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsopenrasterdialog.cpp
165 lines (136 loc) · 6.06 KB
/
qgsopenrasterdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/***************************************************************************
qgsopenrasterdialog.cpp
--------------------------------------
Date : 14-Feb-2010
Copyright : (C) 2010 by Jack R, Maxim Dubinin (GIS-Lab)
Email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include "qgsrasterlayer.h"
#include "qgsproject.h"
#include "qgsopenrasterdialog.h"
QgsOpenRasterDialog::QgsOpenRasterDialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
QPushButton *okPushButton = buttonBox->button(QDialogButtonBox::Ok);
okPushButton->setEnabled( false );
}
// ------------------------------- public ---------------------------------- //
void QgsOpenRasterDialog::getRasterOptions(QString &rasterFileName, QString &modifiedFileName, QString &worldFileName)
{
rasterFileName = leRasterFileName->text();
modifiedFileName = leModifiedRasterFileName->text();
worldFileName = mWorldFileName;
}
// ------------------------------ protected -------------------------------- //
void QgsOpenRasterDialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
// --------------------------- private slots ------------------------------- //
void QgsOpenRasterDialog::on_tbnSelectRaster_clicked()
{
QSettings settings;
QString dir = settings.value( "/Plugin-GeoReferencer/rasterdirectory" ).toString();
if ( dir.isEmpty() )
dir = ".";
QString lastUsedFilter = settings.value("/Plugin-GeoReferencer/lastusedfilter").toString();
QString filters;
QgsRasterLayer::buildSupportedRasterFileFilter( filters );
filters.prepend("(*.*);;");
QString rasterFileName = QFileDialog::getOpenFileName( this, tr( "Choose a name of the raster" ), dir,
filters, &lastUsedFilter );
if ( rasterFileName.isEmpty() )
{
return;
}
leRasterFileName->setText( rasterFileName );
// do we think that this is a valid raster?
if ( !QgsRasterLayer::isValidRasterFileName( rasterFileName ) )
{
QMessageBox::critical( this, tr( "Error" ),
tr( "The selected file is not a valid raster file." ) );
return;
}
QFileInfo fileInfo( rasterFileName );
settings.setValue( "/Plugin-GeoReferencer/rasterdirectory", fileInfo.path() );
settings.setValue("/Plugin-GeoReferencer/lastusedfilter", lastUsedFilter);
QString modifiedFileName = generateModifiedRasterFileName();
leModifiedRasterFileName->setText(modifiedFileName);
// What DOING this code?
QgsProject* prj = QgsProject::instance();
QString projBehaviour = settings.value( "/Projections/defaultBehaviour" ).toString();
QString projectCRS = prj->readEntry( "SpatialRefSys", "/ProjectCRSProj4String" );
int projectCrsId = prj->readNumEntry( "SpatialRefSys", "/ProjectCrsId" );
settings.setValue( "/Projections/defaultBehaviour", "useProject" );
prj->writeEntry( "SpatialRefSys", "/ProjectCRSProj4String", GEOPROJ4 );
prj->writeEntry( "SpatialRefSys", "/ProjectCrsId", int( GEOCRS_ID ) );
settings.setValue( "/Projections/defaultBehaviour", projBehaviour );
prj->writeEntry( "SpatialRefSys", "/ProjectCRSProj4String", projectCRS );
prj->writeEntry( "SpatialRefSys", "/ProjectCrsId", projectCrsId );
}
void QgsOpenRasterDialog::on_tbnSelectModifiedRaster_clicked()
{
QSettings settings;
QString dir = settings.value("/Plugin-GeoReferencer/rasterdirectory").toString();
if (dir.isEmpty())
dir = ".";
QString modifiedFileName = QFileDialog::getSaveFileName( this, tr( "Choose a name for the modified raster" ), dir );
if ( modifiedFileName.right( 4 ) != ".tif" )
modifiedFileName += ".tif";
// do we think that this is a valid raster?
if ( !QgsRasterLayer::isValidRasterFileName( modifiedFileName ) )
{
QMessageBox::critical( this, tr( "Error" ),
tr( "The selected file is not a valid raster file." ) );
return;
}
QFileInfo fileInfo( modifiedFileName );
settings.setValue( "/Plugin-GeoReferencer/rasterdirectory", fileInfo.path() );
leModifiedRasterFileName->setText( modifiedFileName );
}
void QgsOpenRasterDialog::on_leModifiedRasterFileName_textChanged(const QString name)
{
mWorldFileName = guessWorldFileName(name);
bool enable = (leModifiedRasterFileName->text().size() != 0 && leRasterFileName->text().size() != 0);
QPushButton *okPushButton = buttonBox->button(QDialogButtonBox::Ok);
okPushButton->setEnabled( enable );
}
// ------------------------------ private ---------------------------------- //
QString QgsOpenRasterDialog::generateModifiedRasterFileName()
{
QString modifiedFileName = leRasterFileName->text();
QFileInfo modifiedFileInfo(modifiedFileName);
int pos = modifiedFileName.size() - modifiedFileInfo.suffix().size() - 1;
modifiedFileName.insert( pos, tr( "-modified", "Georeferencer:QgsOpenRasterDialog.cpp - used to modify a user given file name" ) );
pos = modifiedFileName.size() - modifiedFileInfo.suffix().size();
modifiedFileName.replace( pos, modifiedFileName.size(), "tif" );
return modifiedFileName;
}
QString QgsOpenRasterDialog::guessWorldFileName( const QString rasterFileName )
{
QString worldFileName = "";
int point = rasterFileName.lastIndexOf( '.' );
if ( point != -1 && point != rasterFileName.length() - 1 )
worldFileName = rasterFileName.left( point + 1 ) + "wld";
return worldFileName;
}