Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] recenter map canvas to an entered coordinate
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11573 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 5, 2009
1 parent d4d122b commit 3ca628c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
83 changes: 67 additions & 16 deletions src/app/qgisapp.cpp
Expand Up @@ -1473,17 +1473,38 @@ void QgisApp::createStatusBar()
mToggleExtentsViewButton->setCheckable( true );
connect( mToggleExtentsViewButton, SIGNAL( toggled( bool ) ), this, SLOT( extentsViewToggled( bool ) ) );
statusBar()->addPermanentWidget( mToggleExtentsViewButton, 0 );
//coords status bar widget

// add a label to show current scale
mCoordsLabel = new QLabel( QString(), statusBar() );
mCoordsLabel->setFont( myFont );
mCoordsLabel->setMinimumWidth( 10 );
mCoordsLabel->setMaximumHeight( 20 );
mCoordsLabel->setFont( myFont );
mCoordsLabel->setMargin( 3 );
mCoordsLabel->setAlignment( Qt::AlignCenter );
mCoordsLabel->setWhatsThis( tr( "Shows the map coordinates at the "
"current cursor position. The display is continuously updated "
"as the mouse is moved." ) );
mCoordsLabel->setFrameStyle( QFrame::NoFrame );
mCoordsLabel->setText( tr( "Coordinate:" ) );
mCoordsLabel->setToolTip( tr( "Current map coordinate" ) );
statusBar()->addPermanentWidget( mCoordsLabel, 0 );

//coords status bar widget
mCoordsEdit = new QLineEdit( QString(), statusBar() );
mCoordsEdit->setFont( myFont );
mCoordsEdit->setMinimumWidth( 10 );
mCoordsEdit->setMaximumWidth( 200 );
mCoordsEdit->setMaximumHeight( 20 );
mCoordsEdit->setContentsMargins( 0, 0, 0, 0 );
mCoordsEdit->setAlignment( Qt::AlignCenter );
mCoordsEdit->setAlignment( Qt::AlignCenter );
QRegExp coordValidator( "[+-]?\\d+\\.?\\d*\\s*,\\s*[+-]?\\d+\\.?\\d*" );
mCoordsEditValidator = new QRegExpValidator( coordValidator, mCoordsEdit );
mCoordsEdit->setWhatsThis( tr( "Shows the map coordinates at the "
"current cursor position. The display is continuously updated "
"as the mouse is moved. It also allows editing to set the canvas "
"center to a given position." ) );
mCoordsEdit->setToolTip( tr( "Current map coordinate (formatted as x,y)" ) );
statusBar()->addPermanentWidget( mCoordsEdit, 0 );
connect( mCoordsEdit, SIGNAL( editingFinished() ), this, SLOT( userCenter() ) );

// add a label to show current scale
mScaleLabel = new QLabel( QString(), statusBar() );
mScaleLabel->setFont( myFont );
Expand Down Expand Up @@ -4666,11 +4687,10 @@ void QgisApp::showMouseCoordinate( const QgsPoint & p )
}
else
{
mCoordsLabel->setText( p.toString( mMousePrecisionDecimalPlaces ) );
// Set minimum necessary width
if ( mCoordsLabel->width() > mCoordsLabel->minimumWidth() )
mCoordsEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
{
mCoordsLabel->setMinimumWidth( mCoordsLabel->width() );
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
}
}
}
Expand Down Expand Up @@ -4710,6 +4730,33 @@ void QgisApp::userScale()
}
}

void QgisApp::userCenter()
{
QStringList parts = mCoordsEdit->text().split( ',' );
if ( parts.size() != 2 )
return;

bool xOk;
double x = parts.at( 0 ).toDouble( &xOk );
if ( !xOk )
return;

bool yOk;
double y = parts.at( 1 ).toDouble( &yOk );
if ( !yOk )
return;

QgsRectangle r = mMapCanvas->extent();

mMapCanvas->setExtent(
QgsRectangle(
x - r.width() / 2.0, y - r.height() / 2.0,
x + r.width() / 2.0, y + r.height() / 2.0
)
);
mMapCanvas->refresh();
}


// toggle overview status
void QgisApp::isInOverview()
Expand Down Expand Up @@ -5366,31 +5413,35 @@ void QgisApp::extentsViewToggled( bool theFlag )
{
//extents view mode!
mToggleExtentsViewButton->setIcon( getThemeIcon( "extents.png" ) );
mCoordsLabel->setToolTip( tr( "Map coordinates for the current view extents" ) );
mCoordsEdit->setToolTip( tr( "Map coordinates for the current view extents" ) );
mCoordsEdit->setEnabled( false );
showExtents();
}
else
{
//mouse cursor pos view mode!
mToggleExtentsViewButton->setIcon( getThemeIcon( "tracking.png" ) );
mCoordsLabel->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
mCoordsEdit->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
mCoordsEdit->setEnabled( true );
mCoordsLabel->setText( tr( "Coordinate:" ) );
}
}

void QgisApp::showExtents()
{
if ( !mToggleExtentsViewButton->isChecked() )
{
//we are in show coords mode so no need to do anything
return;
}

// update the statusbar with the current extents.
QgsRectangle myExtents = mMapCanvas->extent();
mCoordsLabel->setText( tr( "Extents: %1" ).arg( myExtents.toString( true ) ) );
mCoordsLabel->setText( tr( "Extents:" ) );
mCoordsEdit->setText( myExtents.toString( true ) );
//ensure the label is big enough
if ( mCoordsLabel->width() > mCoordsLabel->minimumWidth() )
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
{
mCoordsLabel->setMinimumWidth( mCoordsLabel->width() );
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
}
} // QgisApp::showExtents

Expand Down Expand Up @@ -6288,7 +6339,7 @@ QPixmap QgisApp::getThemePixmap( const QString theName )
void QgisApp::updateUndoActions()
{
bool canUndo = false, canRedo = false;
QgsMapLayer* layer = this->activeLayer();
QgsMapLayer* layer = activeLayer();
if ( layer )
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
Expand Down
8 changes: 7 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -399,6 +399,8 @@ class QgisApp : public QMainWindow
void showScale( double theScale );
//! Slot to handle user scale input;
void userScale();
//! Slot to handle user center input;
void userCenter();
//! Remove a layer from the map and legend
void removeLayer();
//! zoom to extent of layer
Expand Down Expand Up @@ -872,8 +874,12 @@ class QgisApp : public QMainWindow
QLineEdit * mScaleEdit;
//! The validator for the mScaleEdit
QValidator * mScaleEditValidator;
//! Widget that will live in the statusbar to display coords
//! Widget that will live on the statusbar to display "Coordinate / Extent"
QLabel * mCoordsLabel;
//! Widget that will live in the statusbar to display and edit coords
QLineEdit * mCoordsEdit;
//! The validator for the mCoordsEdit
QValidator * mCoordsEditValidator;
//! Widget that will live in the statusbar to show progress of operations
QProgressBar * mProgressBar;
//! Widget used to suppress rendering
Expand Down

0 comments on commit 3ca628c

Please sign in to comment.