@@ -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
283349void 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+
417500void QgsGrassBrowser::selectionChanged (const QItemSelection & selected, const QItemSelection & deselected)
418501{
419502 #ifdef QGISDEBUG
0 commit comments