Skip to content

File tree

4 files changed

+202
-73
lines changed

4 files changed

+202
-73
lines changed
 

‎src/plugins/grass/qgsgrassbrowser.cpp

Lines changed: 146 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ void QgsGrassBrowser::addMap()
201201
mIface->addVectorLayer( uri, name, "grass");
202202
mapSelected = true;
203203
}
204+
else if ( type == QgsGrassModel::Region )
205+
{
206+
struct Cell_head window;
207+
if ( !getItemRegion (*it, &window) ) continue;
208+
writeRegion ( &window );
209+
}
204210
}
205211
}
206212

@@ -246,10 +252,20 @@ void QgsGrassBrowser::copyMap()
246252
typeName = "region";
247253
}
248254

249-
QgsGrassElementDialog *ed = new QgsGrassElementDialog();
255+
QgsGrassElementDialog ed;
250256
bool ok;
251-
QString newName = ed->getItem ( element, map, &ok );
252-
delete ed;
257+
QString source;
258+
QString suggest;
259+
if ( mapset == QgsGrass::getDefaultMapset() )
260+
{
261+
source = map;
262+
}
263+
else
264+
{
265+
suggest = map;
266+
}
267+
QString newName = ed.getItem ( element, "New name",
268+
"New name", suggest, source, &ok );
253269

254270
if ( !ok ) return;
255271

@@ -262,7 +278,7 @@ void QgsGrassBrowser::copyMap()
262278
if ( !process.waitForFinished() )
263279
{
264280
QMessageBox::warning( 0, "Warning", "Cannot copy map "
265-
+ map );
281+
+ map + "@" + mapset );
266282
}
267283
else
268284
{
@@ -277,7 +293,57 @@ void QgsGrassBrowser::renameMap()
277293
std::cerr << "QgsGrassBrowser::renameMap()" << std::endl;
278294
#endif
279295

280-
QMessageBox::warning ( 0, "Warning", "Not yet implemented" );
296+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
297+
298+
QList<QModelIndex>::const_iterator it = indexes.begin();
299+
for (; it != indexes.end(); ++it)
300+
{
301+
int type = mModel->itemType(*it);
302+
QString mapset = mModel->itemMapset(*it);
303+
QString map = mModel->itemMap(*it);
304+
305+
if ( mapset != QgsGrass::getDefaultMapset() ) continue; // should not happen
306+
307+
QString typeName;
308+
QString element;
309+
if ( type == QgsGrassModel::Raster )
310+
{
311+
element = "cell";
312+
typeName = "rast";
313+
}
314+
else if ( type == QgsGrassModel::Vector )
315+
{
316+
element = "vector";
317+
typeName = "vect";
318+
}
319+
else if ( type == QgsGrassModel::Region )
320+
{
321+
element = "windows";
322+
typeName = "region";
323+
}
324+
325+
QgsGrassElementDialog ed;
326+
bool ok;
327+
QString newName = ed.getItem ( element, "New name",
328+
"New name", "", map, &ok );
329+
330+
if ( !ok ) return;
331+
332+
QString module = "g.rename";
333+
#ifdef WIN32
334+
module.append(".exe");
335+
#endif
336+
QProcess process(this);
337+
process.start(module, QStringList( typeName + "=" + map + "," + newName ) );
338+
if ( !process.waitForFinished() )
339+
{
340+
QMessageBox::warning( 0, "Warning", "Cannot rename map " + map );
341+
}
342+
else
343+
{
344+
refresh();
345+
}
346+
}
281347
}
282348

283349
void QgsGrassBrowser::deleteMap()
@@ -336,76 +402,29 @@ void QgsGrassBrowser::setRegion()
336402
#endif
337403

338404
struct Cell_head window;
339-
340-
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
341405

342-
QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
343-
QgsGrass::getDefaultLocation() );
406+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
344407

345408
// TODO multiple selection - extent region to all maps
346409
QList<QModelIndex>::const_iterator it = indexes.begin();
347410
for (; it != indexes.end(); ++it)
348411
{
349-
int type = mModel->itemType(*it);
350-
QString uri = mModel->uri(*it);
351-
QString mapset = mModel->itemMapset(*it);
352-
QString map = mModel->itemMap(*it);
353-
354-
if ( type == QgsGrassModel::Raster )
355-
{
356-
357-
if ( G_get_cellhd ( map.toLocal8Bit().data(),
358-
mapset.toLocal8Bit().data(), &window) < 0 )
359-
{
360-
QMessageBox::warning( 0, "Warning",
361-
"Cannot read raster map region" );
362-
return;
363-
}
364-
}
365-
else if ( type == QgsGrassModel::Vector )
366-
{
367-
G_get_window ( &window ); // get current resolution
368-
369-
struct Map_info Map;
370-
371-
int level = Vect_open_old_head ( &Map,
372-
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());
373-
374-
if ( level < 2 )
375-
{
376-
QMessageBox::warning( 0, "Warning",
377-
"Cannot read vector map region" );
378-
return;
379-
}
380-
381-
BOUND_BOX box;
382-
Vect_get_map_box (&Map, &box );
383-
window.north = box.N;
384-
window.south = box.S;
385-
window.west = box.W;
386-
window.east = box.E;
387-
388-
Vect_close (&Map);
389-
}
390-
else if ( type == QgsGrassModel::Region )
391-
{
392-
if ( G__get_window (&window, "windows",
393-
map.toLocal8Bit().data(),
394-
mapset.toLocal8Bit().data() ) != NULL )
395-
{
396-
QMessageBox::warning( 0, "Warning",
397-
"Cannot read region" );
398-
return;
399-
}
400-
}
412+
if ( !getItemRegion (*it, &window) ) return;
401413
}
414+
writeRegion ( &window );
415+
}
416+
417+
void QgsGrassBrowser::writeRegion(struct Cell_head *window )
418+
{
419+
#ifdef QGISDEBUG
420+
std::cerr << "QgsGrassBrowser::writeRegion()" << std::endl;
421+
#endif
402422

403-
// Reset mapset (selected maps could be in a different one)
404423
QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(),
405424
QgsGrass::getDefaultLocation(),
406425
QgsGrass::getDefaultMapset() );
407426

408-
if ( G_put_window ( &window ) == -1 )
427+
if ( G_put_window ( window ) == -1 )
409428
{
410429
QMessageBox::warning( 0, "Warning",
411430
"Cannot write new region" );
@@ -414,6 +433,70 @@ void QgsGrassBrowser::setRegion()
414433
emit regionChanged();
415434
}
416435

436+
bool QgsGrassBrowser::getItemRegion( QModelIndex index, struct Cell_head *window )
437+
{
438+
#ifdef QGISDEBUG
439+
std::cerr << "QgsGrassBrowser::setRegion()" << std::endl;
440+
#endif
441+
442+
QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
443+
QgsGrass::getDefaultLocation() );
444+
445+
int type = mModel->itemType(index);
446+
QString uri = mModel->uri(index);
447+
QString mapset = mModel->itemMapset(index);
448+
QString map = mModel->itemMap(index);
449+
450+
if ( type == QgsGrassModel::Raster )
451+
{
452+
453+
if ( G_get_cellhd ( map.toLocal8Bit().data(),
454+
mapset.toLocal8Bit().data(), window) < 0 )
455+
{
456+
QMessageBox::warning( 0, "Warning",
457+
"Cannot read raster map region" );
458+
return false;
459+
}
460+
}
461+
else if ( type == QgsGrassModel::Vector )
462+
{
463+
G_get_window ( window ); // get current resolution
464+
465+
struct Map_info Map;
466+
467+
int level = Vect_open_old_head ( &Map,
468+
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());
469+
470+
if ( level < 2 )
471+
{
472+
QMessageBox::warning( 0, "Warning",
473+
"Cannot read vector map region" );
474+
return false;
475+
}
476+
477+
BOUND_BOX box;
478+
Vect_get_map_box (&Map, &box );
479+
window->north = box.N;
480+
window->south = box.S;
481+
window->west = box.W;
482+
window->east = box.E;
483+
484+
Vect_close (&Map);
485+
}
486+
else if ( type == QgsGrassModel::Region )
487+
{
488+
if ( G__get_window (window, "windows",
489+
map.toLocal8Bit().data(),
490+
mapset.toLocal8Bit().data() ) != NULL )
491+
{
492+
QMessageBox::warning( 0, "Warning",
493+
"Cannot read region" );
494+
return false;
495+
}
496+
}
497+
return true;
498+
}
499+
417500
void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
418501
{
419502
#ifdef QGISDEBUG

‎src/plugins/grass/qgsgrassbrowser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public slots:
5656
// Set current region to selected map
5757
void setRegion();
5858

59+
// Get item's region
60+
bool getItemRegion(QModelIndex index, struct Cell_head *window);
61+
62+
// Write region
63+
void writeRegion ( struct Cell_head *window );
64+
5965
// Refresh model
6066
void refresh();
6167

‎src/plugins/grass/qgsgrassutils.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,33 @@ QgsGrassElementDialog::QgsGrassElementDialog() : QObject()
8888
QgsGrassElementDialog::~QgsGrassElementDialog() {}
8989

9090
QString QgsGrassElementDialog::getItem ( QString element,
91-
QString text, bool * ok )
91+
QString title, QString label,
92+
QString text, QString source, bool * ok )
9293
{
9394
#ifdef QGISDEBUG
9495
std::cerr << "QgsGrassElementDialog::getItem" << std::endl;
9596
#endif
96-
*ok = false;
97+
if ( ok ) *ok = false;
9798
mElement = element;
99+
mSource = source;
98100
mDialog = new QDialog ();
101+
mDialog->setWindowTitle(title);
99102
QVBoxLayout *layout = new QVBoxLayout ( mDialog );
100103
QHBoxLayout *buttonLayout = new QHBoxLayout ( );
101104

105+
mLabel = new QLabel ( label );
106+
layout->addWidget( mLabel );
107+
102108
mLineEdit = new QLineEdit ( text );
103109
layout->addWidget( mLineEdit );
104-
mErrorLabel = new QLabel ( );
110+
111+
mErrorLabel = new QLabel ( "X" );
105112
layout->addWidget( mErrorLabel );
113+
// Intention: keep fixed size - but it does not help
114+
mErrorLabel->adjustSize();
115+
mErrorLabel->setMinimumHeight ( mErrorLabel->height()+5 );
106116

107-
mOkButton = new QPushButton ( "Ok" );
117+
mOkButton = new QPushButton ( );
108118
mCancelButton = new QPushButton ( "Cancel" );
109119

110120
layout->insertLayout( -1, buttonLayout );
@@ -116,7 +126,7 @@ QString QgsGrassElementDialog::getItem ( QString element,
116126
connect ( mCancelButton, SIGNAL(clicked()), mDialog, SLOT(reject() ) );
117127

118128
textChanged ();
119-
if ( mDialog->exec() == QDialog::Accepted )
129+
if ( ok && mDialog->exec() == QDialog::Accepted )
120130
{
121131
*ok = true;
122132
}
@@ -133,9 +143,34 @@ void QgsGrassElementDialog::textChanged ()
133143
std::cerr << "QgsGrassElementDialog::textChanged" << std::endl;
134144
#endif
135145

136-
mErrorLabel->setText ( "" );
137-
if ( QgsGrassUtils::itemExists( mElement, mLineEdit->text() ) )
146+
QString text = mLineEdit->text().trimmed();
147+
148+
mErrorLabel->setText ( " " );
149+
mOkButton->setText ("Ok");
150+
mOkButton->setEnabled ( true );
151+
152+
if ( text.length() == 0 )
138153
{
139-
mErrorLabel->setText ( "Exists!" );
154+
mErrorLabel->setText ( "<font color='red'>Enter a name!</font>" );
155+
mOkButton->setEnabled ( false );
156+
return;
157+
}
158+
159+
#ifdef WIN32
160+
if ( !mSource.isNull() && text.toLower() == mSource.toLower() )
161+
#else
162+
if ( !mSource.isNull() && text == mSource )
163+
#endif
164+
{
165+
mErrorLabel->setText ( "<font color='red'>This is name of the source!</font>" );
166+
mOkButton->setEnabled ( false );
167+
return;
168+
}
169+
if ( QgsGrassUtils::itemExists( mElement, text ) )
170+
{
171+
mErrorLabel->setText ( "<font color='red'>Exists!</font>" );
172+
mOkButton->setText ("Overwrite");
173+
return;
140174
}
141175
}
176+

‎src/plugins/grass/qgsgrassutils.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,22 @@ class QgsGrassElementDialog: public QObject
6464
~QgsGrassElementDialog();
6565

6666
public:
67-
// Get a name for new GRASS element (map)
67+
//! Get a name for new GRASS element (map)
68+
// \param source local source
6869
QString getItem ( QString element,
69-
QString text, bool * ok );
70+
QString title, QString label,
71+
QString text, QString source = 0,
72+
bool * ok = 0 );
7073

7174
public slots:
7275
void textChanged();
7376

7477
private:
7578
QString mElement;
79+
QString mSource;
7680
QDialog *mDialog;
7781
QLineEdit *mLineEdit;
82+
QLabel *mLabel;
7883
QLabel *mErrorLabel;
7984
QPushButton *mOkButton;
8085
QPushButton *mCancelButton;

0 commit comments

Comments
 (0)
Please sign in to comment.