Skip to content

Commit c83c881

Browse files
author
rblazek
committedMar 17, 2006
set region for map from browser; use rubber band for region
git-svn-id: http://svn.osgeo.org/qgis/trunk@5047 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b90a498 commit c83c881

File tree

4 files changed

+140
-30
lines changed

4 files changed

+140
-30
lines changed
 

‎src/plugins/grass/qgsgrassbrowser.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ QgsGrassBrowser::QgsGrassBrowser ( QgisIface *iface,
8181
tb->addAction ( mActionDeleteMap );
8282
connect ( mActionDeleteMap, SIGNAL(triggered()), this, SLOT(deleteMap()) );
8383

84+
mActionSetRegion = new QAction(
85+
QIcon(myIconPath+"grass_set_region.png"),
86+
tr("Set current region to selected map"), this);
87+
mActionSetRegion->setEnabled(false);
88+
ag->addAction ( mActionSetRegion );
89+
tb->addAction ( mActionSetRegion );
90+
connect ( mActionSetRegion, SIGNAL(triggered()), this, SLOT(setRegion()) );
91+
8492
mActionRefresh = new QAction(
8593
QIcon(myIconPath+"grass_refresh.png"),
8694
tr("Refresh"), this);
@@ -238,6 +246,80 @@ void QgsGrassBrowser::deleteMap()
238246
}
239247
}
240248

249+
void QgsGrassBrowser::setRegion()
250+
{
251+
#ifdef QGISDEBUG
252+
std::cerr << "QgsGrassBrowser::setRegion()" << std::endl;
253+
#endif
254+
255+
struct Cell_head window;
256+
257+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
258+
259+
QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
260+
QgsGrass::getDefaultLocation() );
261+
262+
// TODO multiple selection - extent region to all maps
263+
QList<QModelIndex>::const_iterator it = indexes.begin();
264+
for (; it != indexes.end(); ++it)
265+
{
266+
int type = mModel->itemType(*it);
267+
QString uri = mModel->uri(*it);
268+
QString mapset = mModel->itemMapset(*it);
269+
QString map = mModel->itemMap(*it);
270+
271+
if ( type == QgsGrassModel::Raster )
272+
{
273+
274+
if ( G_get_cellhd ( map.toLocal8Bit().data(),
275+
mapset.toLocal8Bit().data(), &window) < 0 )
276+
{
277+
QMessageBox::warning( 0, "Warning",
278+
"Cannot read raster map region" );
279+
return;
280+
}
281+
}
282+
else if ( type == QgsGrassModel::Vector )
283+
{
284+
G_get_window ( &window ); // get current resolution
285+
286+
struct Map_info Map;
287+
288+
int level = Vect_open_old_head ( &Map,
289+
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());
290+
291+
if ( level < 2 )
292+
{
293+
QMessageBox::warning( 0, "Warning",
294+
"Cannot read vector map region" );
295+
return;
296+
}
297+
298+
BOUND_BOX box;
299+
Vect_get_map_box (&Map, &box );
300+
window.north = box.N;
301+
window.south = box.S;
302+
window.west = box.W;
303+
window.east = box.E;
304+
305+
Vect_close (&Map);
306+
}
307+
}
308+
309+
// Reset mapset (selected maps could be in a different one)
310+
QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(),
311+
QgsGrass::getDefaultLocation(),
312+
QgsGrass::getDefaultMapset() );
313+
314+
if ( G_put_window ( &window ) == -1 )
315+
{
316+
QMessageBox::warning( 0, "Warning",
317+
"Cannot write new region" );
318+
return;
319+
}
320+
emit regionChanged();
321+
}
322+
241323
void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
242324
{
243325
#ifdef QGISDEBUG
@@ -246,6 +328,7 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI
246328

247329
mActionAddMap->setEnabled(false);
248330
mActionDeleteMap->setEnabled(false);
331+
mActionSetRegion->setEnabled(false);
249332

250333
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
251334

@@ -266,6 +349,8 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI
266349
}
267350
if ( type == QgsGrassModel::Raster || type == QgsGrassModel::Vector )
268351
{
352+
mActionSetRegion->setEnabled(true);
353+
269354
QString mapset = mModel->itemMapset(*it);
270355
if ( mapset == QgsGrass::getDefaultMapset() )
271356
{

‎src/plugins/grass/qgsgrassbrowser.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public slots:
4747
// Delete selected map
4848
void deleteMap();
4949

50+
// Set current region to selected map
51+
void setRegion();
52+
5053
// Refresh model
5154
void refresh();
5255

@@ -57,6 +60,10 @@ public slots:
5760
// Double click
5861
void doubleClicked(const QModelIndex & index);
5962

63+
signals:
64+
// emited when something in GRASS Tools changed region
65+
void regionChanged();
66+
6067
private:
6168
QgisIface *mIface;
6269

@@ -78,6 +85,7 @@ public slots:
7885
//! Actions
7986
QAction *mActionAddMap;
8087
QAction *mActionDeleteMap;
88+
QAction *mActionSetRegion;
8189
QAction *mActionRefresh;
8290
};
8391

‎src/plugins/grass/qgsgrassplugin.cpp

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <iostream>
5050

5151
#include <qgsproject.h>
52+
#include <qgsrubberband.h>
5253

5354
extern "C" {
5455
#include <grass/gis.h>
@@ -143,6 +144,11 @@ void QgsGrassPlugin::initGui()
143144
connect( mQgis, SIGNAL( projectRead() ), this, SLOT( projectRead()));
144145
connect( mQgis, SIGNAL( newProject() ), this, SLOT(newProject()));
145146

147+
// Create region rubber band
148+
mRegion = new QgsRubberBand(mCanvas, 1);
149+
mRegion->setZ(20);
150+
mRegion->hide();
151+
146152
// Create the action for tool
147153
mOpenMapsetAction = new QAction( "Open mapset", this );
148154
mNewMapsetAction = new QAction( "New mapset", this );
@@ -218,6 +224,8 @@ void QgsGrassPlugin::initGui()
218224
// Init Region symbology
219225
mRegionPen.setColor( QColor ( settings.readEntry ("/GRASS/region/color", "#ff0000" ) ) );
220226
mRegionPen.setWidth( settings.readNumEntry ("/GRASS/region/width", 0 ) );
227+
mRegion->setColor ( mRegionPen.color() );
228+
mRegion->setWidth ( mRegionPen.width() );
221229

222230
mapsetChanged();
223231
}
@@ -228,6 +236,7 @@ void QgsGrassPlugin::mapsetChanged ()
228236
mOpenToolsAction->setEnabled(false);
229237
mRegionAction->setEnabled(false);
230238
mEditRegionAction->setEnabled(false);
239+
mRegion->hide();
231240
mCloseMapsetAction->setEnabled(false);
232241
mNewVectorAction->setEnabled(false);
233242

@@ -247,6 +256,9 @@ void QgsGrassPlugin::mapsetChanged ()
247256
QSettings settings("QuantumGIS", "qgis");
248257
bool on = settings.readBoolEntry ("/GRASS/region/on", true );
249258
mRegionAction->setOn(on);
259+
if ( on ) {
260+
mRegion->show();
261+
}
250262

251263
if ( mTools )
252264
{
@@ -395,8 +407,13 @@ void QgsGrassPlugin::addRaster()
395407
// Open tools
396408
void QgsGrassPlugin::openTools()
397409
{
398-
if ( !mTools )
399-
mTools = new QgsGrassTools ( mQgis, qGisInterface, mQgis, 0, Qt::WType_Dialog );
410+
if ( !mTools ) {
411+
mTools = new QgsGrassTools ( mQgis, qGisInterface, mQgis, 0, Qt::WType_Dialog );
412+
413+
std::cout << "connect = " <<
414+
connect( mTools, SIGNAL( regionChanged() ), this, SLOT( redrawRegion()) )
415+
<< "connect" << std::endl;
416+
}
400417

401418
mTools->show();
402419
}
@@ -540,17 +557,15 @@ void QgsGrassPlugin::postRender(QPainter *painter)
540557
std::cout << "QgsGrassPlugin::postRender()" << std::endl;
541558
#endif
542559

543-
if ( QgsGrass::activeMode() && mRegionAction->isEnabled() && mRegionAction->isOn() ) {
544-
displayRegion(painter);
545-
}
546560
}
547561

548-
void QgsGrassPlugin::displayRegion(QPainter *painter)
562+
void QgsGrassPlugin::displayRegion()
549563
{
550564
#ifdef QGISDEBUG
551565
std::cout << "QgsGrassPlugin::displayRegion()" << std::endl;
552566
#endif
553567

568+
mRegion->reset();
554569

555570
// Display region of current mapset if in active mode
556571
if ( !QgsGrass::activeMode() ) return;
@@ -584,18 +599,10 @@ void QgsGrassPlugin::displayRegion(QPainter *painter)
584599
points[3].setX(window.west); points[3].setY(window.north);
585600
points[4].setX(window.west); points[4].setY(window.south);
586601

587-
QgsMapToPixel *transform = mCanvas->getCoordinateTransform();
588-
Q3PointArray pointArray(5);
589-
590-
for ( int i = 0; i < 5; i++ ) {
591-
transform->transform( &(points[i]) );
592-
pointArray.setPoint( i,
593-
static_cast<int>(points[i].x()),
594-
static_cast<int>(points[i].y()) );
602+
for ( int i = 0; i < 5; i++ )
603+
{
604+
mRegion->addPoint( points[i] );
595605
}
596-
597-
painter->setPen ( mRegionPen );
598-
painter->drawPolyline ( pointArray );
599606
}
600607

601608
void QgsGrassPlugin::switchRegion(bool on)
@@ -607,22 +614,23 @@ void QgsGrassPlugin::switchRegion(bool on)
607614
QSettings settings("QuantumGIS", "qgis");
608615
settings.writeEntry ("/GRASS/region/on", on );
609616

610-
QPixmap *pixmap = mCanvas->canvasPixmap();
611-
QPainter p;
612-
p.begin(pixmap);
613-
614617
if ( on ) {
615-
displayRegion(&p);
618+
displayRegion();
619+
mRegion->show();
616620
} else {
617-
// This is not perfect, but user can see reaction and it is fast
618-
QPen pen = mRegionPen;
619-
mRegionPen.setColor( QColor(255,255,255) ); // TODO: background color
620-
displayRegion(&p);
621-
mRegionPen = pen;
621+
mRegion->hide();
622622
}
623+
}
623624

624-
p.end();
625-
mCanvas->repaint(false);
625+
void QgsGrassPlugin::redrawRegion()
626+
{
627+
#ifdef QGISDEBUG
628+
std::cout << "QgsGrassPlugin::redrawRegion()" << std::endl;
629+
#endif
630+
if ( mRegionAction->isOn() )
631+
{
632+
displayRegion();
633+
}
626634
}
627635

628636
void QgsGrassPlugin::changeRegion(void)
@@ -651,6 +659,9 @@ void QgsGrassPlugin::setRegionPen(QPen & pen)
651659
{
652660
mRegionPen = pen;
653661

662+
mRegion->setColor ( mRegionPen.color() );
663+
mRegion->setWidth ( mRegionPen.width() );
664+
654665
QSettings settings("QuantumGIS", "qgis");
655666
settings.writeEntry ("/GRASS/region/color", mRegionPen.color().name() );
656667
settings.writeEntry ("/GRASS/region/width", (int) mRegionPen.width() );

‎src/plugins/grass/qgsgrassplugin.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include <vector>
2626

27+
class QgsRubberBand;
28+
2729
class QgsGrassTools;
2830
class QgsGrassNewMapset;
2931
class QToolBar;
@@ -81,11 +83,13 @@ public slots:
8183
//! show the help document
8284
void help();
8385
//! Display current region
84-
void displayRegion(QPainter *painter);
86+
void displayRegion();
8587
//! Switch region on/off
8688
void switchRegion(bool on);
8789
//! Change region
8890
void changeRegion(void);
91+
//! Redraw region
92+
void redrawRegion(void);
8993
//! Post render
9094
void postRender(QPainter *);
9195
//! Open tools
@@ -130,6 +134,8 @@ public slots:
130134
QAction *mRegionAction;
131135
//! Region width
132136
QPen mRegionPen;
137+
// Region rubber band
138+
QgsRubberBand *mRegion;
133139
//! GRASS tools
134140
QgsGrassTools *mTools;
135141
//! Pointer to QgsGrassNewMapset

0 commit comments

Comments
 (0)
Please sign in to comment.