Skip to content

Commit 127c882

Browse files
committed
create scale widget (scale combo box + button to set scale from map canvas)
1 parent 8f70664 commit 127c882

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

python/gui/gui.sip

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
%Include qgsrubberband.sip
101101
%Include qgsscalecombobox.sip
102102
%Include qgsscalerangewidget.sip
103+
%Include qgsscalewidget.sip
103104
%Include qgsscalevisibilitydialog.sip
104105
%Include qgssearchquerybuilder.sip
105106
%Include qgstextannotationitem.sip

python/gui/qgsscalewidget.sip

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/** A combobox which lets the user select map scale from predefined list
3+
* and highlights nearest to current scale value
4+
*/
5+
class QgsScaleWidget : QWidget
6+
{
7+
%TypeHeaderCode
8+
#include <qgsscalewidget.h>
9+
%End
10+
11+
public:
12+
QgsScaleWidget( QWidget* parent = 0 );
13+
~QgsScaleWidget();
14+
15+
//! shows a button to set the scale to the current scale of the map canvas next to the combobox
16+
//! @note the map canvas must be defined to show the button
17+
void setShowCurrentScaleButton( bool showCurrentScaleButton );
18+
bool showCurrentScaleButton();
19+
20+
//! set the map canvas associated to the current button
21+
void setMapCanvas( QgsMapCanvas* canvas );
22+
23+
//! Function to read the selected scale as text
24+
QString scaleString();
25+
//! Function to set the selected scale from text
26+
bool setScaleString( QString scaleTxt );
27+
//! Function to read the selected scale as double
28+
double scale();
29+
//! Function to set the selected scale from double
30+
void setScale( double scale );
31+
32+
//! Helper function to convert a double to scale string
33+
// Performs rounding, so an exact representation is not to
34+
// be expected.
35+
static QString toString( double scale );
36+
//! Helper function to convert a scale string to double
37+
static double toDouble( QString scaleString, bool *ok = NULL );
38+
39+
signals:
40+
//! Signal is emitted when *user* has finished editing/selecting a new scale.
41+
void scaleChanged();
42+
43+
public slots:
44+
void updateScales( const QStringList &scales = QStringList() );
45+
46+
};
47+

src/gui/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ qgsrubberband.cpp
215215
qgsscalecombobox.cpp
216216
qgsscalerangewidget.cpp
217217
qgsscalevisibilitydialog.cpp
218+
qgsscalewidget.cpp
218219
qgssearchquerybuilder.cpp
219220
qgsslider.cpp
220221
qgssublayersdialog.cpp
@@ -412,6 +413,7 @@ qgsrelationmanagerdialog.h
412413
qgsscalecombobox.h
413414
qgsscalerangewidget.h
414415
qgsscalevisibilitydialog.h
416+
qgsscalewidget.h
415417
qgssearchquerybuilder.h
416418
qgsslider.h
417419
qgssublayersdialog.h
@@ -491,6 +493,7 @@ qgsrubberband.h
491493
qgsscalecombobox.h
492494
qgsscalerangewidget.h
493495
qgsscalevisibilitydialog.h
496+
qgsscalewidget.h
494497
qgssearchquerybuilder.h
495498
qgsslider.h
496499
qgssublayersdialog.h

src/gui/qgsscalewidget.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/***************************************************************************
2+
qgsscalewidget.cpp
3+
--------------------------------------
4+
Date : 08.01.2015
5+
Copyright : (C) 2014 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 <QHBoxLayout>
17+
18+
#include "qgsapplication.h"
19+
#include "qgsscalewidget.h"
20+
#include "qgsmapcanvas.h"
21+
22+
QgsScaleWidget::QgsScaleWidget( QWidget *parent )
23+
: QWidget( parent )
24+
, mShowCurrentScaleButton( false )
25+
, mCanvas( NULL )
26+
{
27+
QHBoxLayout* layout = new QHBoxLayout( this );
28+
layout->setContentsMargins( 0, 0, 0, 0 );
29+
layout->setSpacing( 2 );
30+
31+
mScaleComboBox = new QgsScaleComboBox( this );
32+
layout->addWidget( mScaleComboBox );
33+
34+
mCurrentScaleButton = new QToolButton( this );
35+
mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( "/mActionMapIdentification.svg" ) );
36+
layout->addWidget( mCurrentScaleButton );
37+
mCurrentScaleButton->hide();
38+
39+
connect( mScaleComboBox, SIGNAL( scaleChanged() ), this, SIGNAL( scaleChanged() ) );
40+
connect( mCurrentScaleButton, SIGNAL( clicked() ), this, SLOT( setScaleFromCanvas() ) );
41+
}
42+
43+
44+
QgsScaleWidget::~QgsScaleWidget()
45+
{
46+
}
47+
48+
void QgsScaleWidget::setShowCurrentScaleButton( bool showCurrentScaleButton )
49+
{
50+
mShowCurrentScaleButton = showCurrentScaleButton;
51+
mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
52+
}
53+
54+
void QgsScaleWidget::setMapCanvas( QgsMapCanvas* canvas )
55+
{
56+
mCanvas = canvas;
57+
mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
58+
}
59+
60+
void QgsScaleWidget::setScaleFromCanvas()
61+
{
62+
if ( !mCanvas )
63+
return;
64+
65+
setScale( mCanvas->scale() );
66+
}
67+
68+
69+

src/gui/qgsscalewidget.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/***************************************************************************
2+
qgsscalewidget.h
3+
--------------------------------------
4+
Date : 08.01.2015
5+
Copyright : (C) 2014 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 QGSSCALEWIDGET_H
17+
#define QGSSCALEWIDGET_H
18+
19+
#include <QWidget>
20+
#include <QToolButton>
21+
22+
23+
#include "qgsscalecombobox.h"
24+
25+
class QgsMapCanvas;
26+
27+
/** \ingroup gui
28+
* A combobox which lets the user select map scale from predefined list
29+
* and highlights nearest to current scale value
30+
**/
31+
class GUI_EXPORT QgsScaleWidget : public QWidget
32+
{
33+
Q_OBJECT
34+
Q_PROPERTY( bool showCurrentScaleButton READ showCurrentScaleButton WRITE setShowCurrentScaleButton )
35+
36+
public:
37+
explicit QgsScaleWidget(QWidget *parent = 0);
38+
39+
virtual ~QgsScaleWidget();
40+
41+
//! shows a button to set the scale to the current scale of the map canvas next to the combobox
42+
//! @note the map canvas must be defined to show the button
43+
void setShowCurrentScaleButton( bool showCurrentScaleButton );
44+
bool showCurrentScaleButton(){ return mShowCurrentScaleButton;}
45+
46+
//! set the map canvas associated to the current button
47+
void setMapCanvas( QgsMapCanvas* canvas );
48+
49+
//! Function to read the selected scale as text
50+
QString scaleString(){return mScaleComboBox->scaleString();}
51+
//! Function to set the selected scale from text
52+
bool setScaleString( QString scaleTxt ){return mScaleComboBox->setScaleString(scaleTxt);}
53+
//! Function to read the selected scale as double
54+
double scale(){return mScaleComboBox->scale();}
55+
//! Function to set the selected scale from double
56+
void setScale( double scale ){return mScaleComboBox->setScale(scale);}
57+
58+
//! Helper function to convert a double to scale string
59+
// Performs rounding, so an exact representation is not to
60+
// be expected.
61+
static QString toString( double scale ){return QgsScaleComboBox::toString(scale);}
62+
//! Helper function to convert a scale string to double
63+
static double toDouble( QString scaleString, bool *ok = NULL ){return QgsScaleComboBox::toDouble(scaleString, ok);}
64+
65+
public slots:
66+
void updateScales( const QStringList &scales = QStringList() ){return mScaleComboBox->updateScales(scales);}
67+
68+
//! assign the current scale from the map canvas
69+
void setScaleFromCanvas();
70+
71+
signals:
72+
//! Signal is emitted when *user* has finished editing/selecting a new scale.
73+
void scaleChanged();
74+
75+
private:
76+
QgsScaleComboBox* mScaleComboBox;
77+
QToolButton* mCurrentScaleButton;
78+
QgsMapCanvas* mCanvas;
79+
bool mShowCurrentScaleButton;
80+
};
81+
82+
#endif // QGSSCALEWIDGET_H

0 commit comments

Comments
 (0)