Skip to content
Permalink
Browse files

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
  • Loading branch information
rblazek
rblazek committed Mar 17, 2006
1 parent b90a498 commit c83c881bf2f0ada4be95fbdc0299cd1c24fb3cb2
@@ -81,6 +81,14 @@ QgsGrassBrowser::QgsGrassBrowser ( QgisIface *iface,
tb->addAction ( mActionDeleteMap );
connect ( mActionDeleteMap, SIGNAL(triggered()), this, SLOT(deleteMap()) );

mActionSetRegion = new QAction(
QIcon(myIconPath+"grass_set_region.png"),
tr("Set current region to selected map"), this);
mActionSetRegion->setEnabled(false);
ag->addAction ( mActionSetRegion );
tb->addAction ( mActionSetRegion );
connect ( mActionSetRegion, SIGNAL(triggered()), this, SLOT(setRegion()) );

mActionRefresh = new QAction(
QIcon(myIconPath+"grass_refresh.png"),
tr("Refresh"), this);
@@ -238,6 +246,80 @@ void QgsGrassBrowser::deleteMap()
}
}

void QgsGrassBrowser::setRegion()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassBrowser::setRegion()" << std::endl;
#endif

struct Cell_head window;

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

QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation() );

// TODO multiple selection - extent region to all maps
QList<QModelIndex>::const_iterator it = indexes.begin();
for (; it != indexes.end(); ++it)
{
int type = mModel->itemType(*it);
QString uri = mModel->uri(*it);
QString mapset = mModel->itemMapset(*it);
QString map = mModel->itemMap(*it);

if ( type == QgsGrassModel::Raster )
{

if ( G_get_cellhd ( map.toLocal8Bit().data(),
mapset.toLocal8Bit().data(), &window) < 0 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read raster map region" );
return;
}
}
else if ( type == QgsGrassModel::Vector )
{
G_get_window ( &window ); // get current resolution

struct Map_info Map;

int level = Vect_open_old_head ( &Map,
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());

if ( level < 2 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read vector map region" );
return;
}

BOUND_BOX box;
Vect_get_map_box (&Map, &box );
window.north = box.N;
window.south = box.S;
window.west = box.W;
window.east = box.E;

Vect_close (&Map);
}
}

// Reset mapset (selected maps could be in a different one)
QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
QgsGrass::getDefaultMapset() );

if ( G_put_window ( &window ) == -1 )
{
QMessageBox::warning( 0, "Warning",
"Cannot write new region" );
return;
}
emit regionChanged();
}

void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
{
#ifdef QGISDEBUG
@@ -246,6 +328,7 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI

mActionAddMap->setEnabled(false);
mActionDeleteMap->setEnabled(false);
mActionSetRegion->setEnabled(false);

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

@@ -266,6 +349,8 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI
}
if ( type == QgsGrassModel::Raster || type == QgsGrassModel::Vector )
{
mActionSetRegion->setEnabled(true);

QString mapset = mModel->itemMapset(*it);
if ( mapset == QgsGrass::getDefaultMapset() )
{
@@ -47,6 +47,9 @@ public slots:
// Delete selected map
void deleteMap();

// Set current region to selected map
void setRegion();

// Refresh model
void refresh();

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

signals:
// emited when something in GRASS Tools changed region
void regionChanged();

private:
QgisIface *mIface;

@@ -78,6 +85,7 @@ public slots:
//! Actions
QAction *mActionAddMap;
QAction *mActionDeleteMap;
QAction *mActionSetRegion;
QAction *mActionRefresh;
};

@@ -49,6 +49,7 @@
#include <iostream>

#include <qgsproject.h>
#include <qgsrubberband.h>

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

// Create region rubber band
mRegion = new QgsRubberBand(mCanvas, 1);
mRegion->setZ(20);
mRegion->hide();

// Create the action for tool
mOpenMapsetAction = new QAction( "Open mapset", this );
mNewMapsetAction = new QAction( "New mapset", this );
@@ -218,6 +224,8 @@ void QgsGrassPlugin::initGui()
// Init Region symbology
mRegionPen.setColor( QColor ( settings.readEntry ("/GRASS/region/color", "#ff0000" ) ) );
mRegionPen.setWidth( settings.readNumEntry ("/GRASS/region/width", 0 ) );
mRegion->setColor ( mRegionPen.color() );
mRegion->setWidth ( mRegionPen.width() );

mapsetChanged();
}
@@ -228,6 +236,7 @@ void QgsGrassPlugin::mapsetChanged ()
mOpenToolsAction->setEnabled(false);
mRegionAction->setEnabled(false);
mEditRegionAction->setEnabled(false);
mRegion->hide();
mCloseMapsetAction->setEnabled(false);
mNewVectorAction->setEnabled(false);

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

if ( mTools )
{
@@ -395,8 +407,13 @@ void QgsGrassPlugin::addRaster()
// Open tools
void QgsGrassPlugin::openTools()
{
if ( !mTools )
mTools = new QgsGrassTools ( mQgis, qGisInterface, mQgis, 0, Qt::WType_Dialog );
if ( !mTools ) {
mTools = new QgsGrassTools ( mQgis, qGisInterface, mQgis, 0, Qt::WType_Dialog );

std::cout << "connect = " <<
connect( mTools, SIGNAL( regionChanged() ), this, SLOT( redrawRegion()) )
<< "connect" << std::endl;
}

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

if ( QgsGrass::activeMode() && mRegionAction->isEnabled() && mRegionAction->isOn() ) {
displayRegion(painter);
}
}

void QgsGrassPlugin::displayRegion(QPainter *painter)
void QgsGrassPlugin::displayRegion()
{
#ifdef QGISDEBUG
std::cout << "QgsGrassPlugin::displayRegion()" << std::endl;
#endif

mRegion->reset();

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

QgsMapToPixel *transform = mCanvas->getCoordinateTransform();
Q3PointArray pointArray(5);

for ( int i = 0; i < 5; i++ ) {
transform->transform( &(points[i]) );
pointArray.setPoint( i,
static_cast<int>(points[i].x()),
static_cast<int>(points[i].y()) );
for ( int i = 0; i < 5; i++ )
{
mRegion->addPoint( points[i] );
}

painter->setPen ( mRegionPen );
painter->drawPolyline ( pointArray );
}

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

QPixmap *pixmap = mCanvas->canvasPixmap();
QPainter p;
p.begin(pixmap);

if ( on ) {
displayRegion(&p);
displayRegion();
mRegion->show();
} else {
// This is not perfect, but user can see reaction and it is fast
QPen pen = mRegionPen;
mRegionPen.setColor( QColor(255,255,255) ); // TODO: background color
displayRegion(&p);
mRegionPen = pen;
mRegion->hide();
}
}

p.end();
mCanvas->repaint(false);
void QgsGrassPlugin::redrawRegion()
{
#ifdef QGISDEBUG
std::cout << "QgsGrassPlugin::redrawRegion()" << std::endl;
#endif
if ( mRegionAction->isOn() )
{
displayRegion();
}
}

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

mRegion->setColor ( mRegionPen.color() );
mRegion->setWidth ( mRegionPen.width() );

QSettings settings("QuantumGIS", "qgis");
settings.writeEntry ("/GRASS/region/color", mRegionPen.color().name() );
settings.writeEntry ("/GRASS/region/width", (int) mRegionPen.width() );
@@ -24,6 +24,8 @@

#include <vector>

class QgsRubberBand;

class QgsGrassTools;
class QgsGrassNewMapset;
class QToolBar;
@@ -81,11 +83,13 @@ public slots:
//! show the help document
void help();
//! Display current region
void displayRegion(QPainter *painter);
void displayRegion();
//! Switch region on/off
void switchRegion(bool on);
//! Change region
void changeRegion(void);
//! Redraw region
void redrawRegion(void);
//! Post render
void postRender(QPainter *);
//! Open tools
@@ -130,6 +134,8 @@ public slots:
QAction *mRegionAction;
//! Region width
QPen mRegionPen;
// Region rubber band
QgsRubberBand *mRegion;
//! GRASS tools
QgsGrassTools *mTools;
//! Pointer to QgsGrassNewMapset

0 comments on commit c83c881

Please sign in to comment.
You can’t perform that action at this time.