Skip to content

Commit c6d0bef

Browse files
committed
[FEATURE][composer] Add zoom actual size action to composer, zoom indicator and combobox to status bar
1 parent ed3e04d commit c6d0bef

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

src/app/composer/qgscomposer.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
256256
viewMenu->addAction( mActionZoomIn );
257257
viewMenu->addAction( mActionZoomOut );
258258
viewMenu->addAction( mActionZoomAll );
259+
viewMenu->addAction( mActionZoomActual );
259260
viewMenu->addSeparator();
260261
viewMenu->addAction( mActionRefreshView );
261262
viewMenu->addSeparator();
@@ -331,6 +332,26 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
331332
setMouseTracking( true );
332333
mViewFrame->setMouseTracking( true );
333334

335+
mStatusZoomCombo = new QComboBox( mStatusBar );
336+
mStatusZoomCombo->setEditable( true );
337+
mStatusZoomCombo->setInsertPolicy( QComboBox::NoInsert );
338+
mStatusZoomCombo->setCompleter( 0 );
339+
mStatusZoomCombo->setMinimumWidth( 100 );
340+
//zoom combo box accepts decimals in the range 1-9999, with an optional decimal point and "%" sign
341+
QRegExp zoomRx( "\\s*\\d{1,4}(\\.\\d?)?\\s*%?" );
342+
QValidator *zoomValidator = new QRegExpValidator( zoomRx, mStatusZoomCombo );
343+
mStatusZoomCombo->lineEdit()->setValidator( zoomValidator );
344+
345+
//add some nice zoom levels to the zoom combobox
346+
mStatusZoomLevelsList << 0.125 << 0.25 << 0.5 << 1.0 << 2.0 << 4.0 << 8.0;
347+
QList<double>::iterator zoom_it;
348+
for ( zoom_it = mStatusZoomLevelsList.begin(); zoom_it != mStatusZoomLevelsList.end(); ++zoom_it )
349+
{
350+
mStatusZoomCombo->insertItem( 0, QString( tr( "%1\%" ) ).arg(( *zoom_it ) * 100 ) );
351+
}
352+
connect( mStatusZoomCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( on_mStatusZoomCombo_currentIndexChanged( int ) ) );
353+
connect( mStatusZoomCombo->lineEdit(), SIGNAL( returnPressed() ), this, SLOT( on_mStatusZoomCombo_zoomEntered() ) );
354+
334355
//create status bar labels
335356
mStatusCursorXLabel = new QLabel( mStatusBar );
336357
mStatusCursorXLabel->setMinimumWidth( 100 );
@@ -339,9 +360,11 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
339360
mStatusCursorPageLabel = new QLabel( mStatusBar );
340361
mStatusCursorPageLabel->setMinimumWidth( 100 );
341362
mStatusCompositionLabel = new QLabel( mStatusBar );
363+
342364
mStatusBar->addWidget( mStatusCursorXLabel );
343365
mStatusBar->addWidget( mStatusCursorYLabel );
344366
mStatusBar->addWidget( mStatusCursorPageLabel );
367+
mStatusBar->addWidget( mStatusZoomCombo );
345368
mStatusBar->addWidget( mStatusCompositionLabel );
346369

347370
//create composer view and layout with rulers
@@ -480,6 +503,7 @@ void QgsComposer::setupTheme()
480503
mActionZoomAll->setIcon( QgsApplication::getThemeIcon( "/mActionZoomFullExtent.svg" ) );
481504
mActionZoomIn->setIcon( QgsApplication::getThemeIcon( "/mActionZoomIn.svg" ) );
482505
mActionZoomOut->setIcon( QgsApplication::getThemeIcon( "/mActionZoomOut.svg" ) );
506+
mActionZoomActual->setIcon( QgsApplication::getThemeIcon( "/mActionZoomActual.svg" ) );
483507
mActionMouseZoom->setIcon( QgsApplication::getThemeIcon( "/mActionZoomToSelected.svg" ) );
484508
mActionRefreshView->setIcon( QgsApplication::getThemeIcon( "/mActionDraw.svg" ) );
485509
mActionUndo->setIcon( QgsApplication::getThemeIcon( "/mActionUndo.png" ) );
@@ -547,6 +571,9 @@ void QgsComposer::connectSlots()
547571
//also listen out for position updates from the horizontal/vertical rulers
548572
connect( mHorizontalRuler, SIGNAL( cursorPosChanged( QPointF ) ), this, SLOT( updateStatusCursorPos( QPointF ) ) );
549573
connect( mVerticalRuler, SIGNAL( cursorPosChanged( QPointF ) ), this, SLOT( updateStatusCursorPos( QPointF ) ) );
574+
//listen out for zoom updates
575+
connect( this, SIGNAL( zoomLevelChanged() ), this, SLOT( updateStatusZoom() ) );
576+
connect( mView, SIGNAL( zoomLevelChanged() ), this, SLOT( updateStatusZoom() ) );
550577
//listen out to status bar updates from the composition
551578
connect( mComposition, SIGNAL( statusMsgChanged( QString ) ), this, SLOT( updateStatusCompositionMsg( QString ) ) );
552579
}
@@ -631,6 +658,48 @@ void QgsComposer::updateStatusCursorPos( QPointF cursorPosition )
631658
mStatusCursorPageLabel->setText( QString( tr( "page: %3" ) ).arg( currentPage ) );
632659
}
633660

661+
void QgsComposer::updateStatusZoom()
662+
{
663+
double dpi = QgsApplication::desktop()->logicalDpiX();
664+
//monitor dpi is not always correct - so make sure the value is sane
665+
if (( dpi < 60 ) || ( dpi > 250 ) )
666+
dpi = 72;
667+
668+
//pixel width for 1mm on screen
669+
double scale100 = dpi / 25.4;
670+
//current zoomLevel
671+
double zoomLevel = mView->transform().m11() * 100 / scale100;
672+
673+
mStatusZoomCombo->blockSignals( true );
674+
mStatusZoomCombo->lineEdit()->setText( QString( tr( "%1\%" ) ).arg( QString::number( zoomLevel, 'f', 1 ) ) );
675+
mStatusZoomCombo->blockSignals( false );
676+
}
677+
678+
void QgsComposer::on_mStatusZoomCombo_currentIndexChanged( int index )
679+
{
680+
double selectedZoom = mStatusZoomLevelsList[ mStatusZoomLevelsList.count() - index - 1 ];
681+
if ( mView )
682+
{
683+
mView->setZoomLevel( selectedZoom );
684+
//update zoom combobox text for correct format (one decimal place, trailing % sign)
685+
mStatusZoomCombo->blockSignals( true );
686+
mStatusZoomCombo->lineEdit()->setText( QString( tr( "%1\%" ) ).arg( QString::number( selectedZoom * 100, 'f', 1 ) ) );
687+
mStatusZoomCombo->blockSignals( false );
688+
}
689+
}
690+
691+
void QgsComposer::on_mStatusZoomCombo_zoomEntered()
692+
{
693+
if ( !mView )
694+
{
695+
return;
696+
}
697+
698+
//need to remove spaces and "%" characters from input text
699+
QString zoom = mStatusZoomCombo->currentText().remove( QChar( '%' ) ).trimmed();
700+
mView->setZoomLevel( zoom.toDouble() / 100 );
701+
}
702+
634703
void QgsComposer::updateStatusCompositionMsg( QString message )
635704
{
636705
mStatusCompositionLabel->setText( message );
@@ -715,6 +784,11 @@ void QgsComposer::on_mActionZoomOut_triggered()
715784
emit zoomLevelChanged();
716785
}
717786

787+
void QgsComposer::on_mActionZoomActual_triggered()
788+
{
789+
mView->setZoomLevel( 1.0 );
790+
}
791+
718792
void QgsComposer::on_mActionMouseZoom_triggered()
719793
{
720794
if ( mView )

src/app/composer/qgscomposer.h

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
126126
//! Zoom out
127127
void on_mActionZoomOut_triggered();
128128

129+
//! Zoom actual
130+
void on_mActionZoomActual_triggered();
131+
129132
//! Refresh view
130133
void on_mActionRefreshView_triggered();
131134

@@ -365,6 +368,13 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
365368
//! Updates cursor position in status bar
366369
void updateStatusCursorPos( QPointF position );
367370

371+
//! Updates zoom level in status bar
372+
void updateStatusZoom();
373+
374+
void on_mStatusZoomCombo_currentIndexChanged( int index );
375+
376+
void on_mStatusZoomCombo_zoomEntered();
377+
368378
//! Updates status bar composition message
369379
void updateStatusCompositionMsg( QString message );
370380

@@ -414,6 +424,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
414424
QLabel* mStatusCursorXLabel;
415425
QLabel* mStatusCursorYLabel;
416426
QLabel* mStatusCursorPageLabel;
427+
/**Combobox in status bar which shows/adjusts current zoom level*/
428+
QComboBox* mStatusZoomCombo;
429+
QList<double> mStatusZoomLevelsList;
417430
/**Label in status bar which shows messages from the composition*/
418431
QLabel* mStatusCompositionLabel;
419432

src/gui/qgscomposerview.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QMimeData>
2424
#include <QGridLayout>
2525

26+
#include "qgsapplication.h"
2627
#include "qgscomposerview.h"
2728
#include "qgscomposerarrow.h"
2829
#include "qgscomposerframe.h"
@@ -1313,6 +1314,7 @@ void QgsComposerView::wheelZoom( QWheelEvent * event )
13131314
}
13141315

13151316
//update composition for new zoom
1317+
emit zoomLevelChanged();
13161318
updateRulers();
13171319
update();
13181320
//redraw cached map items
@@ -1328,6 +1330,22 @@ void QgsComposerView::wheelZoom( QWheelEvent * event )
13281330
}
13291331
}
13301332

1333+
void QgsComposerView::setZoomLevel( double zoomLevel )
1334+
{
1335+
double dpi = QgsApplication::desktop()->logicalDpiX();
1336+
//monitor dpi is not always correct - so make sure the value is sane
1337+
if (( dpi < 60 ) || ( dpi > 250 ) )
1338+
dpi = 72;
1339+
1340+
//desired pixel width for 1mm on screen
1341+
double scale = zoomLevel * dpi / 25.4;
1342+
setTransform( QTransform::fromScale( scale , scale ) );
1343+
1344+
updateRulers();
1345+
update();
1346+
emit zoomLevelChanged();
1347+
}
1348+
13311349
void QgsComposerView::paintEvent( QPaintEvent* event )
13321350
{
13331351
if ( mPaintingEnabled )
@@ -1356,6 +1374,7 @@ void QgsComposerView::showEvent( QShowEvent* e )
13561374
void QgsComposerView::resizeEvent( QResizeEvent* event )
13571375
{
13581376
QGraphicsView::resizeEvent( event );
1377+
emit zoomLevelChanged();
13591378
updateRulers();
13601379
}
13611380

src/gui/qgscomposerview.h

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
136136
void setHorizontalRuler( QgsComposerRuler* r ) { mHorizontalRuler = r; }
137137
void setVerticalRuler( QgsComposerRuler* r ) { mVerticalRuler = r; }
138138

139+
/**Set zoom level, where a zoom level of 1.0 corresponds to 100%*/
140+
void setZoomLevel( double zoomLevel );
141+
139142
protected:
140143
void mousePressEvent( QMouseEvent* );
141144
void mouseReleaseEvent( QMouseEvent* );
@@ -220,6 +223,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
220223
void actionFinished();
221224
/**Is emitted when mouse cursor coordinates change*/
222225
void cursorPosChanged( QPointF );
226+
/**Is emitted when the view zoom changes*/
227+
void zoomLevelChanged();
223228

224229
/**Emitted before composerview is shown*/
225230
void composerViewShow( QgsComposerView* );

src/ui/qgscomposerbase.ui

+16
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<bool>true</bool>
7777
</attribute>
7878
<addaction name="mActionZoomAll"/>
79+
<addaction name="mActionZoomActual"/>
7980
<addaction name="mActionZoomIn"/>
8081
<addaction name="mActionZoomOut"/>
8182
<addaction name="mActionRefreshView"/>
@@ -179,6 +180,21 @@
179180
<string>Ctrl+-</string>
180181
</property>
181182
</action>
183+
<action name="mActionZoomActual">
184+
<property name="icon">
185+
<iconset resource="../../images/images.qrc">
186+
<normaloff>:/images/themes/default/mActionZoomActual.svg</normaloff>:/images/themes/default/mActionZoomActual.svg</iconset>
187+
</property>
188+
<property name="text">
189+
<string>Zoom to 100%</string>
190+
</property>
191+
<property name="toolTip">
192+
<string>Zoom to 100%</string>
193+
</property>
194+
<property name="shortcut">
195+
<string>Ctrl+1</string>
196+
</property>
197+
</action>
182198
<action name="mActionMouseZoom">
183199
<property name="icon">
184200
<iconset resource="../../images/images.qrc">

0 commit comments

Comments
 (0)