Skip to content

Commit 71a6a3d

Browse files
committedNov 7, 2015
move coordinates line edit to a dedicated class
* originally in QgisApp * keep funny stuff (dizzy, retro) in there * paste coordinates with space separator (not only comma)
1 parent 04f3216 commit 71a6a3d

File tree

6 files changed

+389
-217
lines changed

6 files changed

+389
-217
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SET(QGIS_APP_SRCS
3030
qgsdecorationgriddialog.cpp
3131
qgsdxfexportdialog.cpp
3232
qgsformannotationdialog.cpp
33+
qgsguivectorlayertools.cpp
3334
qgshtmlannotationdialog.cpp
3435
qgsdelattrdialog.cpp
3536
qgsdiagramproperties.cpp
@@ -48,10 +49,10 @@ SET(QGIS_APP_SRCS
4849
qgsmaplayerstyleguiutils.cpp
4950
qgsrulebasedlabelingwidget.cpp
5051
qgssavestyletodbdialog.cpp
51-
qgsguivectorlayertools.cpp
52+
qgsstatusbarcoordinateswidget.cpp
53+
qgsversioninfo.cpp
5254
qgswelcomepageitemsmodel.cpp
5355
qgswelcomepage.cpp
54-
qgsversioninfo.cpp
5556

5657
qgsmaptooladdfeature.cpp
5758
qgsmaptooladdpart.cpp
@@ -176,6 +177,7 @@ SET (QGIS_APP_MOC_HDRS
176177
qgsjoindialog.h
177178
qgsaddtaborgroup.h
178179
qgsannotationwidget.h
180+
qgsapplayertreeviewmenuprovider.h
179181
qgsattributeactiondialog.h
180182
qgsattributetypedialog.h
181183
qgsattributetabledialog.h
@@ -202,6 +204,7 @@ SET (QGIS_APP_MOC_HDRS
202204
qgsfieldcalculator.h
203205
qgsfieldsproperties.h
204206
qgsformannotationdialog.h
207+
qgsguivectorlayertools.h
205208
qgshtmlannotationdialog.h
206209
qgsidentifyresultsdialog.h
207210
qgslabeldialog.h
@@ -214,11 +217,10 @@ SET (QGIS_APP_MOC_HDRS
214217
qgsrulebasedlabelingwidget.h
215218
qgssavestyletodbdialog.h
216219
qgsshortcutsmanager.h
217-
qgsapplayertreeviewmenuprovider.h
218-
qgsguivectorlayertools.h
220+
qgsstatusbarcoordinateswidget.h
221+
qgsversioninfo.h
219222
qgswelcomepageitemsmodel.h
220223
qgswelcomepage.h
221-
qgsversioninfo.h
222224

223225
qgsmaptooladdfeature.h
224226
qgsmaptoolcircularstringradius.h

‎src/app/qgisapp.cpp

Lines changed: 13 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#include "qgscomposer.h"
123123
#include "qgscomposermanager.h"
124124
#include "qgscomposerview.h"
125+
#include "qgsstatusbarcoordinateswidget.h"
125126
#include "qgsconfigureshortcutsdialog.h"
126127
#include "qgscoordinatetransform.h"
127128
#include "qgscredentialdialog.h"
@@ -502,9 +503,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
502503
, mScaleLabel( 0 )
503504
, mScaleEdit( 0 )
504505
, mScaleEditValidator( 0 )
505-
, mCoordsLabel( 0 )
506506
, mCoordsEdit( 0 )
507-
, mCoordsEditValidator( 0 )
508507
, mRotationLabel( 0 )
509508
, mRotationEdit( 0 )
510509
, mRotationEditValidator( 0 )
@@ -521,7 +520,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
521520
, mToolPopupDisplay( 0 )
522521
, mLayerTreeCanvasBridge( 0 )
523522
, mSplash( splash )
524-
, mMousePrecisionDecimalPlaces( 0 )
525523
, mInternalClipboard( 0 )
526524
, mShowProjectionTab( false )
527525
, mPythonUtils( 0 )
@@ -944,15 +942,12 @@ QgisApp::QgisApp()
944942
, mScaleLabel( 0 )
945943
, mScaleEdit( 0 )
946944
, mScaleEditValidator( 0 )
947-
, mCoordsLabel( 0 )
948945
, mCoordsEdit( 0 )
949-
, mCoordsEditValidator( 0 )
950946
, mRotationLabel( 0 )
951947
, mRotationEdit( 0 )
952948
, mRotationEditValidator( 0 )
953949
, mProgressBar( 0 )
954950
, mRenderSuppressionCBox( 0 )
955-
, mToggleExtentsViewButton( 0 )
956951
, mOnTheFlyProjectionStatusLabel( 0 )
957952
, mOnTheFlyProjectionStatusButton( 0 )
958953
, mMessageButton( 0 )
@@ -971,11 +966,9 @@ QgisApp::QgisApp()
971966
, mMapWindow( 0 )
972967
, mQgisInterface( 0 )
973968
, mSplash( 0 )
974-
, mMousePrecisionDecimalPlaces( 0 )
975969
, mInternalClipboard( 0 )
976970
, mShowProjectionTab( false )
977971
, mpMapTipsTimer( 0 )
978-
, mDizzyTimer( 0 )
979972
, mpMaptip( 0 )
980973
, mMapTipsVisible( false )
981974
, mFullScreenMode( false )
@@ -1941,52 +1934,13 @@ void QgisApp::createStatusBar()
19411934
// small on some platforms. A point size of 9 still provides
19421935
// plenty of display space on 1024x768 resolutions
19431936
QFont myFont( "Arial", 9 );
1944-
19451937
statusBar()->setFont( myFont );
1946-
//toggle to switch between mouse pos and extents display in status bar widget
1947-
mToggleExtentsViewButton = new QToolButton( statusBar() );
1948-
mToggleExtentsViewButton->setObjectName( "mToggleExtentsViewButton" );
1949-
mToggleExtentsViewButton->setMaximumWidth( 20 );
1950-
//mToggleExtentsViewButton->setMaximumHeight( 20 );
1951-
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "tracking.png" ) );
1952-
mToggleExtentsViewButton->setToolTip( tr( "Toggle extents and mouse position display" ) );
1953-
mToggleExtentsViewButton->setCheckable( true );
1954-
connect( mToggleExtentsViewButton, SIGNAL( toggled( bool ) ), this, SLOT( extentsViewToggled( bool ) ) );
1955-
statusBar()->addPermanentWidget( mToggleExtentsViewButton, 0 );
1956-
1957-
// add a label to show current position
1958-
mCoordsLabel = new QLabel( QString(), statusBar() );
1959-
mCoordsLabel->setObjectName( "mCoordsLabel" );
1960-
mCoordsLabel->setFont( myFont );
1961-
mCoordsLabel->setMinimumWidth( 10 );
1962-
//mCoordsLabel->setMaximumHeight( 20 );
1963-
mCoordsLabel->setMargin( 3 );
1964-
mCoordsLabel->setAlignment( Qt::AlignCenter );
1965-
mCoordsLabel->setFrameStyle( QFrame::NoFrame );
1966-
mCoordsLabel->setText( tr( "Coordinate:" ) );
1967-
mCoordsLabel->setToolTip( tr( "Current map coordinate" ) );
1968-
statusBar()->addPermanentWidget( mCoordsLabel, 0 );
19691938

19701939
//coords status bar widget
1971-
mCoordsEdit = new QLineEdit( QString(), statusBar() );
1972-
mCoordsEdit->setObjectName( "mCoordsEdit" );
1940+
mCoordsEdit = new QgsStatusBarCoordinatesWidget( statusBar() );
1941+
mCoordsEdit->setMapCanvas( mMapCanvas );
19731942
mCoordsEdit->setFont( myFont );
1974-
mCoordsEdit->setMinimumWidth( 10 );
1975-
mCoordsEdit->setMaximumWidth( 300 );
1976-
//mCoordsEdit->setMaximumHeight( 20 );
1977-
mCoordsEdit->setContentsMargins( 0, 0, 0, 0 );
1978-
mCoordsEdit->setAlignment( Qt::AlignCenter );
1979-
QRegExp coordValidator( "[+-]?\\d+\\.?\\d*\\s*,\\s*[+-]?\\d+\\.?\\d*" );
1980-
mCoordsEditValidator = new QRegExpValidator( coordValidator, mCoordsEdit );
1981-
mCoordsEdit->setWhatsThis( tr( "Shows the map coordinates at the "
1982-
"current cursor position. The display is continuously updated "
1983-
"as the mouse is moved. It also allows editing to set the canvas "
1984-
"center to a given position. The format is lat,lon or east,north" ) );
1985-
mCoordsEdit->setToolTip( tr( "Current map coordinate (lat,lon or east,north)" ) );
19861943
statusBar()->addPermanentWidget( mCoordsEdit, 0 );
1987-
connect( mCoordsEdit, SIGNAL( returnPressed() ), this, SLOT( userCenter() ) );
1988-
mDizzyTimer = new QTimer( this );
1989-
connect( mDizzyTimer, SIGNAL( timeout() ), this, SLOT( dizzy() ) );
19901944

19911945
// add a label to show current scale
19921946
mScaleLabel = new QLabel( QString(), statusBar() );
@@ -2312,9 +2266,9 @@ void QgisApp::setupConnections()
23122266

23132267
// signal when mouse moved over window (coords display in status bar)
23142268
connect( mMapCanvas, SIGNAL( xyCoordinates( const QgsPoint & ) ),
2315-
this, SLOT( showMouseCoordinate( const QgsPoint & ) ) );
2269+
this, SLOT( saveLastMousePosition( const QgsPoint & ) ) );
23162270
connect( mMapCanvas, SIGNAL( extentsChanged() ),
2317-
this, SLOT( showExtents() ) );
2271+
this, SLOT( extentChanged() ) );
23182272
connect( mMapCanvas, SIGNAL( scaleChanged( double ) ),
23192273
this, SLOT( showScale( double ) ) );
23202274
connect( mMapCanvas, SIGNAL( rotationChanged( double ) ),
@@ -7334,7 +7288,7 @@ void QgisApp::layerSubsetString()
73347288
delete qb;
73357289
}
73367290

7337-
void QgisApp::showMouseCoordinate( const QgsPoint & p )
7291+
void QgisApp::saveLastMousePosition( const QgsPoint & p )
73387292
{
73397293
if ( mMapTipsVisible )
73407294
{
@@ -7352,43 +7306,6 @@ void QgisApp::showMouseCoordinate( const QgsPoint & p )
73527306
//QgsDebugMsg("Started maptips timer");
73537307
}
73547308
}
7355-
if ( mToggleExtentsViewButton->isChecked() )
7356-
{
7357-
//we are in show extents mode so no need to do anything
7358-
return;
7359-
}
7360-
else
7361-
{
7362-
if ( mMapCanvas->mapUnits() == QGis::Degrees )
7363-
{
7364-
if ( !mMapCanvas->mapSettings().destinationCrs().isValid() )
7365-
return;
7366-
7367-
QgsPoint geo = p;
7368-
if ( !mMapCanvas->mapSettings().destinationCrs().geographicFlag() )
7369-
{
7370-
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem( GEOSRID ) );
7371-
geo = ct.transform( p );
7372-
}
7373-
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );
7374-
7375-
if ( format == "DM" )
7376-
mCoordsEdit->setText( geo.toDegreesMinutes( mMousePrecisionDecimalPlaces ) );
7377-
else if ( format == "DMS" )
7378-
mCoordsEdit->setText( geo.toDegreesMinutesSeconds( mMousePrecisionDecimalPlaces ) );
7379-
else
7380-
mCoordsEdit->setText( geo.toString( mMousePrecisionDecimalPlaces ) );
7381-
}
7382-
else
7383-
{
7384-
mCoordsEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
7385-
}
7386-
7387-
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
7388-
{
7389-
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
7390-
}
7391-
}
73927309
}
73937310

73947311

@@ -7410,58 +7327,6 @@ void QgisApp::userScale()
74107327
mMapCanvas->zoomScale( 1.0 / mScaleEdit->scale() );
74117328
}
74127329

7413-
void QgisApp::dizzy()
7414-
{
7415-
// constants should go to options so that people can customize them to their taste
7416-
int d = 10; // max. translational dizziness offset
7417-
int r = 4; // max. rotational dizzines angle
7418-
QRectF rect = mMapCanvas->sceneRect();
7419-
if ( rect.x() < -d || rect.x() > d || rect.y() < -d || rect.y() > d )
7420-
return; // do not affect panning
7421-
rect.moveTo(( qrand() % ( 2 * d ) ) - d, ( qrand() % ( 2 * d ) ) - d );
7422-
mMapCanvas->setSceneRect( rect );
7423-
QTransform matrix;
7424-
matrix.rotate(( qrand() % ( 2 * r ) ) - r );
7425-
mMapCanvas->setTransform( matrix );
7426-
}
7427-
7428-
void QgisApp::userCenter()
7429-
{
7430-
if ( mCoordsEdit->text() == "dizzy" )
7431-
{
7432-
// sometimes you may feel a bit dizzy...
7433-
if ( mDizzyTimer->isActive() )
7434-
{
7435-
mDizzyTimer->stop();
7436-
mMapCanvas->setSceneRect( mMapCanvas->viewport()->rect() );
7437-
mMapCanvas->setTransform( QTransform() );
7438-
}
7439-
else
7440-
mDizzyTimer->start( 100 );
7441-
}
7442-
else if ( mCoordsEdit->text() == "retro" )
7443-
{
7444-
mMapCanvas->setProperty( "retro", !mMapCanvas->property( "retro" ).toBool() );
7445-
refreshMapCanvas();
7446-
}
7447-
7448-
QStringList parts = mCoordsEdit->text().split( ',' );
7449-
if ( parts.size() != 2 )
7450-
return;
7451-
7452-
bool xOk;
7453-
double x = parts.at( 0 ).toDouble( &xOk );
7454-
if ( !xOk )
7455-
return;
7456-
7457-
bool yOk;
7458-
double y = parts.at( 1 ).toDouble( &yOk );
7459-
if ( !yOk )
7460-
return;
7461-
7462-
mMapCanvas->setCenter( QgsPoint( x, y ) );
7463-
mMapCanvas->refresh();
7464-
}
74657330

74667331
void QgisApp::userRotation()
74677332
{
@@ -7470,7 +7335,6 @@ void QgisApp::userRotation()
74707335
mMapCanvas->refresh();
74717336
}
74727337

7473-
74747338
// toggle overview status
74757339
void QgisApp::isInOverview()
74767340
{
@@ -9234,32 +9098,18 @@ void QgisApp::showMapCanvas()
92349098
mCentralContainer->setCurrentIndex( 0 );
92359099
}
92369100

9237-
void QgisApp::extentsViewToggled( bool theFlag )
9238-
{
9239-
if ( theFlag )
9240-
{
9241-
//extents view mode!
9242-
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "extents.png" ) );
9243-
mCoordsEdit->setToolTip( tr( "Map coordinates for the current view extents" ) );
9244-
mCoordsEdit->setReadOnly( true );
9245-
showExtents();
9246-
}
9247-
else
9248-
{
9249-
//mouse cursor pos view mode!
9250-
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "tracking.png" ) );
9251-
mCoordsEdit->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
9252-
mCoordsEdit->setReadOnly( false );
9253-
mCoordsLabel->setText( tr( "Coordinate:" ) );
9254-
}
9255-
}
9256-
92579101
void QgisApp::markDirty()
92589102
{
92599103
// notify the project that there was a change
92609104
QgsProject::instance()->dirty( true );
92619105
}
92629106

9107+
void QgisApp::extentChanged()
9108+
{
9109+
// allow symbols in the legend update their preview if they use map units
9110+
mLayerTreeView->layerTreeModel()->setLegendMapViewData( mMapCanvas->mapUnitsPerPixel(), mMapCanvas->mapSettings().outputDpi(), mMapCanvas->scale() );
9111+
}
9112+
92639113
void QgisApp::layersWereAdded( const QList<QgsMapLayer *>& theLayers )
92649114
{
92659115
for ( int i = 0; i < theLayers.size(); ++i )
@@ -9303,27 +9153,6 @@ void QgisApp::layersWereAdded( const QList<QgsMapLayer *>& theLayers )
93039153
}
93049154
}
93059155

9306-
void QgisApp::showExtents()
9307-
{
9308-
// allow symbols in the legend update their preview if they use map units
9309-
mLayerTreeView->layerTreeModel()->setLegendMapViewData( mMapCanvas->mapUnitsPerPixel(), mMapCanvas->mapSettings().outputDpi(), mMapCanvas->scale() );
9310-
9311-
if ( !mToggleExtentsViewButton->isChecked() )
9312-
{
9313-
return;
9314-
}
9315-
9316-
// update the statusbar with the current extents.
9317-
QgsRectangle myExtents = mMapCanvas->extent();
9318-
mCoordsLabel->setText( tr( "Extents:" ) );
9319-
mCoordsEdit->setText( myExtents.toString( true ) );
9320-
//ensure the label is big enough
9321-
if ( mCoordsEdit->width() > mCoordsEdit->minimumWidth() )
9322-
{
9323-
mCoordsEdit->setMinimumWidth( mCoordsEdit->width() );
9324-
}
9325-
} // QgisApp::showExtents
9326-
93279156
void QgisApp::showRotation()
93289157
{
93299158
// update the statusbar with the current rotation.
@@ -9358,7 +9187,7 @@ void QgisApp::updateMouseCoordinatePrecision()
93589187
if ( dp < 0 )
93599188
dp = 0;
93609189

9361-
mMousePrecisionDecimalPlaces = dp;
9190+
mCoordsEdit->setMouseCoordinatesPrecision( dp );
93629191
}
93639192

93649193
void QgisApp::showStatusMessage( const QString& theMessage )

‎src/app/qgisapp.h

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class QgsClipboard;
4444
class QgsComposer;
4545
class QgsComposerManager;
4646
class QgsComposerView;
47+
class QgsStatusBarCoordinatesWidget;
4748
class QgsContrastEnhancement;
4849
class QgsCustomLayerOrderWidget;
4950
class QgsDoubleSpinBox;
@@ -718,14 +719,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
718719
//#endif
719720
//! toggles whether the current selected layer is in overview or not
720721
void isInOverview();
721-
//! Slot to show the map coordinate position of the mouse cursor
722-
void showMouseCoordinate( const QgsPoint & );
722+
//! Store the position for map tool tip
723+
void saveLastMousePosition( const QgsPoint & );
723724
//! Slot to show current map scale;
724725
void showScale( double theScale );
725726
//! Slot to handle user scale input;
726727
void userScale();
727-
//! Slot to handle user center input;
728-
void userCenter();
729728
//! Slot to handle user rotation input;
730729
//! @note added in 2.8
731730
void userRotation();
@@ -1073,8 +1072,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
10731072
void selectionChanged( QgsMapLayer *layer );
10741073

10751074
void showProgress( int theProgress, int theTotalSteps );
1076-
void extentsViewToggled( bool theFlag );
1077-
void showExtents();
1075+
void extentChanged();
10781076
void showRotation();
10791077
void showStatusMessage( const QString& theMessage );
10801078
void displayMapToolMessage( const QString& message, QgsMessageBar::MessageLevel level = QgsMessageBar::INFO );
@@ -1248,12 +1246,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
12481246
* @note added in 2.3 */
12491247
void activateDeuteranopePreview();
12501248

1251-
void toggleFilterLegendByExpression(bool);
1249+
void toggleFilterLegendByExpression( bool );
12521250
void updateFilterLegend();
12531251

1254-
/** Make the user feel dizzy */
1255-
void dizzy();
1256-
12571252
/** Shows the statistical summary dock widget and brings it to the foreground
12581253
*/
12591254
void showStatisticsDockWidget();
@@ -1544,12 +1539,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15441539
QgsScaleComboBox *mScaleEdit;
15451540
//! The validator for the mScaleEdit
15461541
QValidator * mScaleEditValidator;
1547-
//! Widget that will live on the statusbar to display "Coordinate / Extent"
1548-
QLabel *mCoordsLabel;
1542+
15491543
//! Widget that will live in the statusbar to display and edit coords
1550-
QLineEdit *mCoordsEdit;
1551-
//! The validator for the mCoordsEdit
1552-
QValidator *mCoordsEditValidator;
1544+
QgsStatusBarCoordinatesWidget *mCoordsEdit;
1545+
15531546
//! Widget that will live on the statusbar to display "Rotation"
15541547
QLabel *mRotationLabel;
15551548
//! Widget that will live in the statusbar to display and edit rotation
@@ -1560,8 +1553,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15601553
QProgressBar *mProgressBar;
15611554
//! Widget used to suppress rendering
15621555
QCheckBox *mRenderSuppressionCBox;
1563-
//! A toggle to switch between mouse coords and view extents display
1564-
QToolButton *mToggleExtentsViewButton;
15651556
//! Widget in status bar used to show current project CRS
15661557
QLabel *mOnTheFlyProjectionStatusLabel;
15671558
//! Widget in status bar used to show status of on the fly projection
@@ -1606,8 +1597,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
16061597
QList<QgsWelcomePageItemsModel::RecentProjectData> mRecentProjects;
16071598
//! Print composers of this project, accessible by id string
16081599
QSet<QgsComposer*> mPrintComposers;
1609-
//! The number of decimal places to use if not automatic
1610-
unsigned int mMousePrecisionDecimalPlaces;
16111600
/** QGIS-internal vector feature clipboard */
16121601
QgsClipboard *mInternalClipboard;
16131602
//! Flag to indicate how the project properties dialog was summoned
@@ -1624,9 +1613,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
16241613
//! Timer for map tips
16251614
QTimer *mpMapTipsTimer;
16261615

1627-
//! Helps to make people dizzy
1628-
QTimer *mDizzyTimer;
1629-
16301616
//! Point of last mouse position in map coordinates (used with MapTips)
16311617
QgsPoint mLastMapPosition;
16321618

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
/***************************************************************************
2+
qgsstatusbarcoordinateswidget.cpp
3+
--------------------------------------
4+
Date : 05.08.2015
5+
Copyright : (C) 2015 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QFont>
17+
#include <QHBoxLayout>
18+
#include <QLabel>
19+
#include <QLineEdit>
20+
#include <QRegExpValidator>
21+
#include <QSpacerItem>
22+
#include <QTimer>
23+
#include <QToolButton>
24+
25+
#include "qgsstatusbarcoordinateswidget.h"
26+
#include "qgsapplication.h"
27+
#include "qgsmapcanvas.h"
28+
#include "qgsproject.h"
29+
30+
31+
QgsStatusBarCoordinatesWidget::QgsStatusBarCoordinatesWidget( QWidget *parent )
32+
: QWidget( parent )
33+
, mDizzyTimer( 0 )
34+
, mMapCanvas( 0 )
35+
, mMousePrecisionDecimalPlaces( 0 )
36+
{
37+
// add a label to show current position
38+
mLabel = new QLabel( QString(), this );
39+
mLabel->setObjectName( "mCoordsLabel" );
40+
mLabel->setMinimumWidth( 10 );
41+
//mCoordsLabel->setMaximumHeight( 20 );
42+
mLabel->setMargin( 3 );
43+
mLabel->setAlignment( Qt::AlignCenter );
44+
mLabel->setFrameStyle( QFrame::NoFrame );
45+
mLabel->setText( tr( "Coordinate:" ) );
46+
mLabel->setToolTip( tr( "Current map coordinate" ) );
47+
48+
mLineEdit = new QLineEdit( this );
49+
mLineEdit->setMinimumWidth( 10 );
50+
mLineEdit->setMaximumWidth( 300 );
51+
//mLineEdit->setMaximumHeight( 20 );
52+
mLineEdit->setContentsMargins( 0, 0, 0, 0 );
53+
mLineEdit->setAlignment( Qt::AlignCenter );
54+
connect( mLineEdit, SIGNAL( returnPressed() ), this, SLOT( validateCoordinates() ) );
55+
56+
QRegExp coordValidator( "[+-]?\\d+\\.?\\d*\\s*,\\s*[+-]?\\d+\\.?\\d*" );
57+
mCoordsEditValidator = new QRegExpValidator( coordValidator, this );
58+
mLineEdit->setWhatsThis( tr( "Shows the map coordinates at the "
59+
"current cursor position. The display is continuously updated "
60+
"as the mouse is moved. It also allows editing to set the canvas "
61+
"center to a given position. The format is lat,lon or east,north" ) );
62+
mLineEdit->setToolTip( tr( "Current map coordinate (lat,lon or east,north)" ) );
63+
64+
//toggle to switch between mouse pos and extents display in status bar widget
65+
mToggleExtentsViewButton = new QToolButton( this );
66+
mToggleExtentsViewButton->setMaximumWidth( 20 );
67+
//mToggleExtentsViewButton->setMaximumHeight( 20 );
68+
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "tracking.png" ) );
69+
mToggleExtentsViewButton->setToolTip( tr( "Toggle extents and mouse position display" ) );
70+
mToggleExtentsViewButton->setCheckable( true );
71+
connect( mToggleExtentsViewButton, SIGNAL( toggled( bool ) ), this, SLOT( extentsViewToggled( bool ) ) );
72+
73+
QHBoxLayout* layout = new QHBoxLayout( this );
74+
setLayout( layout );
75+
layout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ) );
76+
layout->addWidget( mLabel );
77+
layout->addWidget( mLineEdit );
78+
layout->addWidget( mToggleExtentsViewButton );
79+
layout->setContentsMargins( 0, 0, 0, 0 );
80+
layout->setAlignment( Qt::AlignRight );
81+
layout->setSpacing( 0 );
82+
83+
// When you feel dizzy
84+
mDizzyTimer = new QTimer( this );
85+
connect( mDizzyTimer, SIGNAL( timeout() ), this, SLOT( dizzy() ) );
86+
}
87+
88+
void QgsStatusBarCoordinatesWidget::setMapCanvas( QgsMapCanvas *mapCanvas )
89+
{
90+
if ( mMapCanvas )
91+
{
92+
disconnect( mMapCanvas, SIGNAL( xyCoordinates( const QgsPoint & ) ), this, SLOT( showMouseCoordinates( const QgsPoint & ) ) );
93+
disconnect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( showExtent() ) );
94+
}
95+
96+
mMapCanvas = mapCanvas;
97+
connect( mMapCanvas, SIGNAL( xyCoordinates( const QgsPoint & ) ), this, SLOT( showMouseCoordinates( const QgsPoint & ) ) );
98+
connect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( showExtent() ) );
99+
}
100+
101+
void QgsStatusBarCoordinatesWidget::setFont( QFont myFont )
102+
{
103+
mLineEdit->setFont( myFont );
104+
mLabel->setFont( myFont );
105+
}
106+
107+
void QgsStatusBarCoordinatesWidget::setMouseCoordinatesPrecision( unsigned int precision )
108+
{
109+
mMousePrecisionDecimalPlaces = precision;
110+
}
111+
112+
113+
void QgsStatusBarCoordinatesWidget::validateCoordinates()
114+
{
115+
if ( !mMapCanvas )
116+
{
117+
return;
118+
}
119+
if ( mLineEdit->text() == "dizzy" )
120+
{
121+
// sometimes you may feel a bit dizzy...
122+
if ( mDizzyTimer->isActive() )
123+
{
124+
mDizzyTimer->stop();
125+
mMapCanvas->setSceneRect( mMapCanvas->viewport()->rect() );
126+
mMapCanvas->setTransform( QTransform() );
127+
}
128+
else
129+
{
130+
mDizzyTimer->start( 100 );
131+
}
132+
return;
133+
}
134+
else if ( mLineEdit->text() == "retro" )
135+
{
136+
mMapCanvas->setProperty( "retro", !mMapCanvas->property( "retro" ).toBool() );
137+
refreshMapCanvas();
138+
return;
139+
}
140+
141+
bool xOk = false;
142+
bool yOk = false;
143+
double x, y;
144+
QString coordText = mLineEdit->text();
145+
coordText.replace( QRegExp( " {2,}" ), " " );
146+
147+
QStringList parts = coordText.split( ',' );
148+
if ( parts.size() == 2 )
149+
{
150+
x = parts.at( 0 ).toDouble( &xOk );
151+
y = parts.at( 1 ).toDouble( &yOk );
152+
}
153+
154+
if ( !xOk || !yOk )
155+
{
156+
parts = coordText.split( ' ' );
157+
if ( parts.size() == 2 )
158+
{
159+
x = parts.at( 0 ).toDouble( &xOk );
160+
y = parts.at( 1 ).toDouble( &yOk );
161+
}
162+
}
163+
164+
if ( !xOk || !yOk )
165+
return;
166+
167+
mMapCanvas->setCenter( QgsPoint( x, y ) );
168+
mMapCanvas->refresh();
169+
}
170+
171+
172+
void QgsStatusBarCoordinatesWidget::dizzy()
173+
{
174+
if ( !mMapCanvas )
175+
{
176+
return;
177+
}
178+
// constants should go to options so that people can customize them to their taste
179+
int d = 10; // max. translational dizziness offset
180+
int r = 4; // max. rotational dizzines angle
181+
QRectF rect = mMapCanvas->sceneRect();
182+
if ( rect.x() < -d || rect.x() > d || rect.y() < -d || rect.y() > d )
183+
return; // do not affect panning
184+
rect.moveTo(( qrand() % ( 2 * d ) ) - d, ( qrand() % ( 2 * d ) ) - d );
185+
mMapCanvas->setSceneRect( rect );
186+
QTransform matrix;
187+
matrix.rotate(( qrand() % ( 2 * r ) ) - r );
188+
mMapCanvas->setTransform( matrix );
189+
}
190+
191+
void QgsStatusBarCoordinatesWidget::extentsViewToggled( bool theFlag )
192+
{
193+
if ( theFlag )
194+
{
195+
//extents view mode!
196+
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "extents.png" ) );
197+
mLineEdit->setToolTip( tr( "Map coordinates for the current view extents" ) );
198+
mLineEdit->setReadOnly( true );
199+
showExtent();
200+
}
201+
else
202+
{
203+
//mouse cursor pos view mode!
204+
mToggleExtentsViewButton->setIcon( QgsApplication::getThemeIcon( "tracking.png" ) );
205+
mLineEdit->setToolTip( tr( "Map coordinates at mouse cursor position" ) );
206+
mLineEdit->setReadOnly( false );
207+
mLabel->setText( tr( "Coordinate:" ) );
208+
}
209+
}
210+
211+
void QgsStatusBarCoordinatesWidget::refreshMapCanvas()
212+
{
213+
if ( !mMapCanvas )
214+
return;
215+
216+
//stop any current rendering
217+
mMapCanvas->stopRendering();
218+
mMapCanvas->refreshAllLayers();
219+
}
220+
221+
void QgsStatusBarCoordinatesWidget::showMouseCoordinates( const QgsPoint & p )
222+
{
223+
if ( !mMapCanvas || mToggleExtentsViewButton->isChecked() )
224+
{
225+
return;
226+
}
227+
228+
if ( mMapCanvas->mapUnits() == QGis::Degrees )
229+
{
230+
if ( !mMapCanvas->mapSettings().destinationCrs().isValid() )
231+
return;
232+
233+
QgsPoint geo = p;
234+
if ( !mMapCanvas->mapSettings().destinationCrs().geographicFlag() )
235+
{
236+
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem( GEOSRID ) );
237+
geo = ct.transform( p );
238+
}
239+
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );
240+
241+
if ( format == "DM" )
242+
mLineEdit->setText( geo.toDegreesMinutes( mMousePrecisionDecimalPlaces ) );
243+
else if ( format == "DMS" )
244+
mLineEdit->setText( geo.toDegreesMinutesSeconds( mMousePrecisionDecimalPlaces ) );
245+
else
246+
mLineEdit->setText( geo.toString( mMousePrecisionDecimalPlaces ) );
247+
}
248+
else
249+
{
250+
mLineEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
251+
}
252+
253+
if ( mLineEdit->width() > mLineEdit->minimumWidth() )
254+
{
255+
mLineEdit->setMinimumWidth( mLineEdit->width() );
256+
}
257+
}
258+
259+
260+
void QgsStatusBarCoordinatesWidget::showExtent()
261+
{
262+
if ( !mToggleExtentsViewButton->isChecked() )
263+
{
264+
return;
265+
}
266+
267+
// update the statusbar with the current extents.
268+
QgsRectangle myExtents = mMapCanvas->extent();
269+
mLabel->setText( tr( "Extents:" ) );
270+
mLineEdit->setText( myExtents.toString( true ) );
271+
//ensure the label is big enough
272+
if ( mLineEdit->width() > mLineEdit->minimumWidth() )
273+
{
274+
mLineEdit->setMinimumWidth( mLineEdit->width() );
275+
}
276+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/***************************************************************************
2+
qgsstatusbarcoordinateswidget.h
3+
--------------------------------------
4+
Date : 05.08.2015
5+
Copyright : (C) 2015 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSSTATUSBARCOORDINATESWIDGET_H
17+
#define QGSSTATUSBARCOORDINATESWIDGET_H
18+
19+
20+
class QFont;
21+
class QLabel;
22+
class QLineEdit;
23+
class QTimer;
24+
class QToolButton;
25+
class QValidator;
26+
27+
class QgsMapCanvas;
28+
class QgsPoint;
29+
30+
#include <QWidget>
31+
32+
class APP_EXPORT QgsStatusBarCoordinatesWidget : public QWidget
33+
{
34+
Q_OBJECT
35+
36+
enum CrsMode
37+
{
38+
MapCanvas,
39+
Custom
40+
};
41+
42+
public:
43+
QgsStatusBarCoordinatesWidget( QWidget *parent );
44+
45+
//! define the map canvas associated to the widget
46+
void setMapCanvas( QgsMapCanvas* mapCanvas );
47+
48+
void setFont( QFont myFont );
49+
50+
void setMouseCoordinatesPrecision( unsigned int precision );
51+
52+
signals:
53+
void coordinatesChanged();
54+
55+
private slots:
56+
void showMouseCoordinates( const QgsPoint &p );
57+
void extentsViewToggled( bool theFlag );
58+
void validateCoordinates();
59+
void dizzy();
60+
void showExtent();
61+
62+
private:
63+
void refreshMapCanvas();
64+
65+
QLineEdit *mLineEdit;
66+
QToolButton *mToggleExtentsViewButton;
67+
//! Widget that will live on the statusbar to display "Coordinate / Extent"
68+
QLabel *mLabel;
69+
70+
QValidator *mCoordsEditValidator;
71+
QTimer *mDizzyTimer;
72+
QgsMapCanvas* mMapCanvas;
73+
74+
//! The number of decimal places to use if not automatic
75+
unsigned int mMousePrecisionDecimalPlaces;
76+
77+
};
78+
79+
#endif // QGSSTATUSBARCOORDINATESWIDGET_H

‎src/gui/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,18 @@ SET(QGIS_GUI_SRCS
158158
qgsbusyindicatordialog.cpp
159159
qgscharacterselectdialog.cpp
160160
qgsbrowsertreeview.cpp
161-
qgscollapsiblegroupbox.cpp
162-
qgscolorbutton.cpp
163-
qgscolorbuttonv2.cpp
164-
qgscolordialog.cpp
165-
qgscolorswatchgrid.cpp
166-
qgscolorschemelist.cpp
167161
qgscolorwidgets.cpp
168162
qgscodeeditor.cpp
169163
qgscodeeditorpython.cpp
170164
qgscodeeditorsql.cpp
171165
qgscodeeditorhtml.cpp
172166
qgscodeeditorcss.cpp
167+
qgscollapsiblegroupbox.cpp
168+
qgscolorbutton.cpp
169+
qgscolorbuttonv2.cpp
170+
qgscolordialog.cpp
171+
qgscolorswatchgrid.cpp
172+
qgscolorschemelist.cpp
173173
qgscomposerruler.cpp
174174
qgscomposerview.cpp
175175
qgsprevieweffect.cpp

0 commit comments

Comments
 (0)
Please sign in to comment.