1
/***************************************************************************
2
qgsgrassselect.cpp - Select GRASS layer dialog
5
copyright : (C) 2004 by Radim Blazek
7
***************************************************************************/
8
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
***************************************************************************/
19
#include <QFileDialog>
20
#include <qsettings.h>
22
#include <q3listbox.h>
23
#include <qstringlist.h>
26
#include <qmessagebox.h>
27
#include <qinputdialog.h>
28
#include <qsettings.h>
31
#include <grass/gis.h>
32
#include <grass/Vect.h>
35
#include "../../src/providers/grass/qgsgrass.h"
36
#include "qgsgrassselect.h"
38
QgsGrassSelect::QgsGrassSelect(int type):QgsGrassSelectBase()
41
std::cerr << "QgsGrassSelect() type = " << type << std::endl;
47
if ( QgsGrass::activeMode() ) {
48
lastGisdbase = QgsGrass::getDefaultGisdbase();
49
lastLocation = QgsGrass::getDefaultLocation();
50
lastMapset = QgsGrass::getDefaultMapset();
53
lastGisdbase = settings.readEntry("/GRASS/lastGisdbase");
54
//check we got something from qsettings otherwise default to users home dir
55
if (lastGisdbase.isEmpty())
57
QDir home = QDir::home();
58
lastGisdbase = QString( home.path() );
63
QgsGrassSelect::type = type;
67
case QgsGrassSelect::VECTOR:
68
setCaption ( tr("Select GRASS Vector Layer") );
71
case QgsGrassSelect::RASTER:
72
/* Remove layer combo box */
75
setCaption ( tr("Select GRASS Raster Layer") );
78
case QgsGrassSelect::MAPCALC:
79
/* Remove layer combo box */
82
setCaption ( tr("Select GRASS mapcalc schema") );
85
case QgsGrassSelect::MAPSET:
90
setCaption ( tr("Select GRASS Mapset") );
94
egisdbase->setText(lastGisdbase);
101
QgsGrassSelect::~QgsGrassSelect()
103
saveWindowLocation();
106
void QgsGrassSelect::restorePosition()
108
optionsFrame->adjustSize ();
112
int ww = settings.readNumEntry("/GRASS/windows/select/w", 500);
113
int wh = settings.readNumEntry("/GRASS/windows/select/h", 100);
114
int wx = settings.readNumEntry("/GRASS/windows/select/x", 100);
115
int wy = settings.readNumEntry("/GRASS/windows/select/y", 100);
120
void QgsGrassSelect::saveWindowLocation()
123
QPoint p = this->pos();
124
QSize s = this->size();
125
settings.writeEntry("/GRASS/windows/select/x", p.x());
126
settings.writeEntry("/GRASS/windows/select/y", p.y());
127
settings.writeEntry("/GRASS/windows/select/w", s.width());
128
settings.writeEntry("/GRASS/windows/select/h", s.height());
131
bool QgsGrassSelect::first = true;
132
QString QgsGrassSelect::lastGisdbase;
133
QString QgsGrassSelect::lastLocation;
134
QString QgsGrassSelect::lastMapset;
135
QString QgsGrassSelect::lastVectorMap;
136
QString QgsGrassSelect::lastRasterMap;
137
QString QgsGrassSelect::lastLayer;
138
QString QgsGrassSelect::lastMapcalc;
140
void QgsGrassSelect::setLocations()
147
QDir d = QDir( egisdbase->text() );
151
// Add all subdirs containing PERMANENT/DEFAULT_WIND
152
for ( unsigned int i = 0; i < d.count(); i++ ) {
153
if ( d[i] == "." || d[i] == ".." ) continue;
155
QString ldpath = egisdbase->text() + "/" + d[i];
157
/* TODO: G_is_location() was added to GRASS 6.1 06-05-24,
158
enable its use after some period (others do update) */
160
if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 )
162
if ( !G_is_location( ldpath.toLocal8Bit().constData() ) ) continue;
167
QString chf = egisdbase->text() + "/" + d[i] + "/PERMANENT/DEFAULT_WIND";
168
if ( !QFile::exists ( chf ) ) continue;
171
// if type is MAPSET check also if at least one mapset owned by user exists
172
if ( QgsGrassSelect::type == QgsGrassSelect::MAPSET )
176
QDir ld = QDir( ldpath );
178
for ( unsigned int j = 0; j < ld.count(); j++ )
180
if ( !QgsGrass::isMapset( ldpath + "/" + ld[j] ) ) continue;
182
QFileInfo info ( ldpath + "/" + ld[j] );
183
if ( !info.isWritable() ) continue;
185
// TODO: check if owner == user: how to get uer name in QT
191
if ( !exists ) continue;
194
elocation->insertItem ( QString ( d[i] ), -1 );
195
if ( QString ( d[i] ) == lastLocation ) {
201
elocation->setCurrentItem(sel);
208
void QgsGrassSelect::setMapsets()
211
std::cerr << "setMapsets()" << std::endl;
218
if ( elocation->count() < 1 ) return;
220
// Location directory
221
QString ldpath = egisdbase->text() + "/" + elocation->currentText();
222
QDir ld = QDir( ldpath );
227
// Go through all subdirs and add all subdirs from vector/
228
for ( unsigned int i = 0; i < ld.count(); i++ )
230
if ( QgsGrass::isMapset( ldpath + "/" + ld[i] ) )
232
emapset->insertItem ( ld[i], -1 );
233
if ( ld[i] == lastMapset ) {
240
emapset->setCurrentItem(sel);
246
void QgsGrassSelect::setMaps()
249
std::cerr << "setMaps()" << std::endl;
252
// Replaced by text box to enable wild cards
256
if ( emapset->count() < 1 ) return;
259
QString ldpath = egisdbase->text() + "/" + elocation->currentText() + "/" + emapset->currentText();
260
QDir ld = QDir( ldpath );
265
if (type == VECTOR ) // vector
267
QStringList list = QgsGrass::vectors ( egisdbase->text(),
268
elocation->currentText(), emapset->currentText() );
270
for ( int j = 0; j < list.count(); j++ )
272
emap->insertItem ( list[j], -1 );
273
if ( list[j] == lastVectorMap ) sel = idx;
278
else if ( type == RASTER )
281
QStringList list = QgsGrass::rasters ( egisdbase->text(),
282
elocation->currentText(), emapset->currentText() );
284
for ( int j = 0; j < list.count(); j++ )
286
emap->insertItem ( list[j], -1 );
287
if ( list[j] == lastRasterMap ) sel = idx;
292
// TODO add QgsGrass::groups ( use G_list( G_ELEMENT_GROUP) )
293
QDir md = QDir( ldpath + "/group/" );
294
md.setFilter (QDir::Dirs);
296
for ( unsigned int j = 0; j < md.count(); j++ ) {
297
if ( md[j] == "." || md[j] == ".." ) continue;
299
QString m = QString( md[j] + " (GROUP)" );
300
emap->insertItem ( m, -1 );
301
if ( m == lastRasterMap ) {
307
else if (type == MAPCALC )
309
QDir md = QDir( ldpath + "/mapcalc/" );
310
md.setFilter (QDir::Files);
312
for ( unsigned int j = 0; j < md.count(); j++ ) {
313
QString m = QString( md[j] );
314
emap->insertItem ( m, -1 );
315
if ( m == lastMapcalc ) {
322
emap->setCurrentItem(sel);
327
emap->clearEdit(); // set box line empty
334
void QgsGrassSelect::setLayers()
337
std::cerr << "setLayers()" << std::endl;
342
if (type != VECTOR ) return;
343
if ( emap->count() < 1 ) return;
345
QStringList layers = vectorLayers ( egisdbase->text(),
346
elocation->currentText(), emapset->currentText(),
347
emap->currentText().ascii() );
351
for ( int i = 0; i < layers.count(); i++ )
353
elayer->insertItem ( layers[i], -1 );
354
if ( layers[i] == lastLayer ) sel = idx;
358
// if last used layer has not been found
359
// make default a map from layer 1
362
for ( int j = 0; j < layers.count(); j++ )
364
if (layers[j].left(1) == "1")
373
elayer->setCurrentItem(sel);
375
elayer->clearEdit(); // set box line empty
378
if ( elayer->count() == 1 ) {
379
elayer->setDisabled(true);
381
elayer->setDisabled(false);
385
QStringList QgsGrassSelect::vectorLayers ( QString gisdbase,
386
QString location, QString mapset, QString mapName )
391
QgsGrass::setLocation ( gisdbase, location);
394
QgsGrass::resetError();
395
Vect_set_open_level (2);
397
int level = Vect_open_old_head (&map, (char *) mapName.ascii(),
398
(char *) mapset.ascii());
400
if ( QgsGrass::getError() == QgsGrass::FATAL ) {
401
std::cerr << "Cannot open GRASS vector: " << QgsGrass::getErrorMessage().toLocal8Bit().data() << std::endl;
406
std::cerr << "Cannot open vector on level 2" << std::endl;
407
QMessageBox::warning( 0, tr("Warning"), tr("Cannot open vector on level 2 (topology not available).") );
412
std::cerr << "GRASS vector successfully opened" << std::endl;
417
int ncidx = Vect_cidx_get_num_fields ( &map );
419
for ( int i = 0; i < ncidx; i++ ) {
420
int field = Vect_cidx_get_field_number ( &map, i);
422
fs.sprintf("%d",field);
425
std::cerr << "i = " << i << " layer = " << field << std::endl;
429
int npoints = Vect_cidx_get_type_count ( &map, field, GV_POINT);
431
QString l = fs + "_point";
436
/* Lines without category appears in layer 0, but not boundaries */
441
tp = GV_LINE | GV_BOUNDARY;
443
int nlines = Vect_cidx_get_type_count ( &map, field, tp);
445
QString l = fs + "_line";
450
int nareas = Vect_cidx_get_type_count ( &map, field, GV_AREA);
452
QString l = fs + "_polygon";
461
void QgsGrassSelect::on_GisdbaseBrowse_clicked()
464
QString Gisdbase = QFileDialog::getExistingDirectory( this,
465
tr("Choose existing GISDBASE"), egisdbase->text() );
467
if ( !Gisdbase.isNull() )
469
egisdbase->setText ( Gisdbase );
473
void QgsGrassSelect::on_ok_clicked()
475
saveWindowLocation();
477
gisdbase = egisdbase->text();
478
lastGisdbase = QString( gisdbase );
480
if ( elocation->count() == 0 ) {
481
QString msg = tr("Wrong GISDBASE, no locations available.");
482
QMessageBox::warning(this, tr("Wrong GISDBASE"), msg);
486
//write to qgsettings as gisdbase seems to be valid
488
settings.writeEntry("/GRASS/lastGisdbase",lastGisdbase );
490
location = elocation->currentText();
491
lastLocation = location;
493
mapset = emapset->currentText();
496
map = emap->currentText().stripWhiteSpace();
498
if ( type != QgsGrassSelect::MAPSET && map.isEmpty() ) {
499
QString msg = tr("Select a map.");
500
QMessageBox::warning(0, tr("No map"), msg);
504
if ( type == QgsGrassSelect::VECTOR )
506
if ( elayer->count() == 0 )
508
QMessageBox::warning(0, tr("No layer"),
509
tr("No layers available in this map"));
513
layer = elayer->currentText().stripWhiteSpace();
515
} else if ( type == QgsGrassSelect::RASTER) {
517
if ( map.find(" (GROUP)") != -1 ) {
518
map.remove ( " (GROUP)" );
519
selectedType = QgsGrassSelect::GROUP;
521
selectedType = QgsGrassSelect::RASTER;
523
} else if ( type == QgsGrassSelect::MAPCALC ) {
529
void QgsGrassSelect::on_cancel_clicked()
531
saveWindowLocation();