Skip to content

Commit 4b4a665

Browse files
author
jef
committed
[FEATURE] recenter map canvas to an entered coordinate
git-svn-id: http://svn.osgeo.org/qgis/trunk@11573 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 92bb2a3 commit 4b4a665

File tree

2 files changed

+74
-17
lines changed

2 files changed

+74
-17
lines changed

src/app/qgisapp.cpp

+67-16
Original file line numberDiff line numberDiff line change
@@ -1473,17 +1473,38 @@ void QgisApp::createStatusBar()
14731473
mToggleExtentsViewButton->setCheckable( true );
14741474
connect( mToggleExtentsViewButton, SIGNAL( toggled( bool ) ), this, SLOT( extentsViewToggled( bool ) ) );
14751475
statusBar()->addPermanentWidget( mToggleExtentsViewButton, 0 );
1476-
//coords status bar widget
1476+
1477+
// add a label to show current scale
14771478
mCoordsLabel = new QLabel( QString(), statusBar() );
1479+
mCoordsLabel->setFont( myFont );
14781480
mCoordsLabel->setMinimumWidth( 10 );
14791481
mCoordsLabel->setMaximumHeight( 20 );
1480-
mCoordsLabel->setFont( myFont );
14811482
mCoordsLabel->setMargin( 3 );
14821483
mCoordsLabel->setAlignment( Qt::AlignCenter );
1483-
mCoordsLabel->setWhatsThis( tr( "Shows the map coordinates at the "
1484-
"current cursor position. The display is continuously updated "
1485-
"as the mouse is moved." ) );
1484+
mCoordsLabel->setFrameStyle( QFrame::NoFrame );
1485+
mCoordsLabel->setText( tr( "Coordinate:" ) );
1486+
mCoordsLabel->setToolTip( tr( "Current map coordinate" ) );
14861487
statusBar()->addPermanentWidget( mCoordsLabel, 0 );
1488+
1489+
//coords status bar widget
1490+
mCoordsEdit = new QLineEdit( QString(), statusBar() );
1491+
mCoordsEdit->setFont( myFont );
1492+
mCoordsEdit->setMinimumWidth( 10 );
1493+
mCoordsEdit->setMaximumWidth( 200 );
1494+
mCoordsEdit->setMaximumHeight( 20 );
1495+
mCoordsEdit->setContentsMargins( 0, 0, 0, 0 );
1496+
mCoordsEdit->setAlignment( Qt::AlignCenter );
1497+
mCoordsEdit->setAlignment( Qt::AlignCenter );
1498+
QRegExp coordValidator( "[+-]?\\d+\\.?\\d*\\s*,\\s*[+-]?\\d+\\.?\\d*" );
1499+
mCoordsEditValidator = new QRegExpValidator( coordValidator, mCoordsEdit );
1500+
mCoordsEdit->setWhatsThis( tr( "Shows the map coordinates at the "
1501+
"current cursor position. The display is continuously updated "
1502+
"as the mouse is moved. It also allows editing to set the canvas "
1503+
"center to a given position." ) );
1504+
mCoordsEdit->setToolTip( tr( "Current map coordinate (formatted as x,y)" ) );
1505+
statusBar()->addPermanentWidget( mCoordsEdit, 0 );
1506+
connect( mCoordsEdit, SIGNAL( editingFinished() ), this, SLOT( userCenter() ) );
1507+
14871508
// add a label to show current scale
14881509
mScaleLabel = new QLabel( QString(), statusBar() );
14891510
mScaleLabel->setFont( myFont );
@@ -4666,11 +4687,10 @@ void QgisApp::showMouseCoordinate( const QgsPoint & p )
46664687
}
46674688
else
46684689
{
4669-
mCoordsLabel->setText( p.toString( mMousePrecisionDecimalPlaces ) );
4670-
// Set minimum necessary width
4671-
if ( mCoordsLabel->width() > mCoordsLabel->minimumWidth() )
4690+
mCoordsEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
4691+
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
46724692
{
4673-
mCoordsLabel->setMinimumWidth( mCoordsLabel->width() );
4693+
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
46744694
}
46754695
}
46764696
}
@@ -4710,6 +4730,33 @@ void QgisApp::userScale()
47104730
}
47114731
}
47124732

4733+
void QgisApp::userCenter()
4734+
{
4735+
QStringList parts = mCoordsEdit->text().split( ',' );
4736+
if ( parts.size() != 2 )
4737+
return;
4738+
4739+
bool xOk;
4740+
double x = parts.at( 0 ).toDouble( &xOk );
4741+
if ( !xOk )
4742+
return;
4743+
4744+
bool yOk;
4745+
double y = parts.at( 1 ).toDouble( &yOk );
4746+
if ( !yOk )
4747+
return;
4748+
4749+
QgsRectangle r = mMapCanvas->extent();
4750+
4751+
mMapCanvas->setExtent(
4752+
QgsRectangle(
4753+
x - r.width() / 2.0, y - r.height() / 2.0,
4754+
x + r.width() / 2.0, y + r.height() / 2.0
4755+
)
4756+
);
4757+
mMapCanvas->refresh();
4758+
}
4759+
47134760

47144761
// toggle overview status
47154762
void QgisApp::isInOverview()
@@ -5366,31 +5413,35 @@ void QgisApp::extentsViewToggled( bool theFlag )
53665413
{
53675414
//extents view mode!
53685415
mToggleExtentsViewButton->setIcon( getThemeIcon( "extents.png" ) );
5369-
mCoordsLabel->setToolTip( tr( "Map coordinates for the current view extents" ) );
5416+
mCoordsEdit->setToolTip( tr( "Map coordinates for the current view extents" ) );
5417+
mCoordsEdit->setEnabled( false );
53705418
showExtents();
53715419
}
53725420
else
53735421
{
53745422
//mouse cursor pos view mode!
53755423
mToggleExtentsViewButton->setIcon( getThemeIcon( "tracking.png" ) );
5376-
mCoordsLabel->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
5424+
mCoordsEdit->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
5425+
mCoordsEdit->setEnabled( true );
5426+
mCoordsLabel->setText( tr( "Coordinate:" ) );
53775427
}
53785428
}
53795429

53805430
void QgisApp::showExtents()
53815431
{
53825432
if ( !mToggleExtentsViewButton->isChecked() )
53835433
{
5384-
//we are in show coords mode so no need to do anything
53855434
return;
53865435
}
5436+
53875437
// update the statusbar with the current extents.
53885438
QgsRectangle myExtents = mMapCanvas->extent();
5389-
mCoordsLabel->setText( tr( "Extents: %1" ).arg( myExtents.toString( true ) ) );
5439+
mCoordsLabel->setText( tr( "Extents:" ) );
5440+
mCoordsEdit->setText( myExtents.toString( true ) );
53905441
//ensure the label is big enough
5391-
if ( mCoordsLabel->width() > mCoordsLabel->minimumWidth() )
5442+
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
53925443
{
5393-
mCoordsLabel->setMinimumWidth( mCoordsLabel->width() );
5444+
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
53945445
}
53955446
} // QgisApp::showExtents
53965447

@@ -6288,7 +6339,7 @@ QPixmap QgisApp::getThemePixmap( const QString theName )
62886339
void QgisApp::updateUndoActions()
62896340
{
62906341
bool canUndo = false, canRedo = false;
6291-
QgsMapLayer* layer = this->activeLayer();
6342+
QgsMapLayer* layer = activeLayer();
62926343
if ( layer )
62936344
{
62946345
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );

src/app/qgisapp.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ class QgisApp : public QMainWindow
399399
void showScale( double theScale );
400400
//! Slot to handle user scale input;
401401
void userScale();
402+
//! Slot to handle user center input;
403+
void userCenter();
402404
//! Remove a layer from the map and legend
403405
void removeLayer();
404406
//! zoom to extent of layer
@@ -872,8 +874,12 @@ class QgisApp : public QMainWindow
872874
QLineEdit * mScaleEdit;
873875
//! The validator for the mScaleEdit
874876
QValidator * mScaleEditValidator;
875-
//! Widget that will live in the statusbar to display coords
877+
//! Widget that will live on the statusbar to display "Coordinate / Extent"
876878
QLabel * mCoordsLabel;
879+
//! Widget that will live in the statusbar to display and edit coords
880+
QLineEdit * mCoordsEdit;
881+
//! The validator for the mCoordsEdit
882+
QValidator * mCoordsEditValidator;
877883
//! Widget that will live in the statusbar to show progress of operations
878884
QProgressBar * mProgressBar;
879885
//! Widget used to suppress rendering

0 commit comments

Comments
 (0)