Quantum GIS API Documentation  1.8
src/gui/qgsscalecombobox.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgsscalecombobox.h
00003                               ------------------------
00004   begin                : January 7, 2012
00005   copyright            : (C) 2012 by Alexander Bruy
00006   email                : alexander dot bruy at gmail dot com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsscalecombobox.h"
00019 
00020 #include <QAbstractItemView>
00021 
00022 QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent )
00023 {
00024   // make combobox editable and populate with predefined scales
00025   setEditable( true );
00026   addItem( "1:1000000" );
00027   addItem( "1:500000" );
00028   addItem( "1:250000" );
00029   addItem( "1:100000" );
00030   addItem( "1:50000" );
00031   addItem( "1:25000" );
00032   addItem( "1:10000" );
00033   addItem( "1:5000" );
00034   addItem( "1:2500" );
00035   addItem( "1:1000" );
00036   addItem( "1:500" );
00037 
00038   setInsertPolicy( QComboBox::NoInsert );
00039 }
00040 
00041 QgsScaleComboBox::~QgsScaleComboBox()
00042 {
00043 }
00044 
00045 void QgsScaleComboBox::showPopup()
00046 {
00047   QComboBox::showPopup();
00048 
00049   QStringList parts = currentText().split( ':' );
00050   bool ok;
00051   int idx = 0;
00052   int min = 999999;
00053   long currScale = parts.at( 1 ).toLong( &ok );
00054   long nextScale, delta;
00055   for ( int i = 0; i < count(); i++ )
00056   {
00057     parts = itemText( i ).split( ':' );
00058     nextScale = parts.at( 1 ).toLong( &ok );
00059     delta = qAbs( currScale - nextScale );
00060     if ( delta < min )
00061     {
00062       min = delta;
00063       idx = i;
00064     }
00065   }
00066 
00067   blockSignals( true );
00068   view()->setCurrentIndex( model()->index( idx, 0 ) );
00069   blockSignals( false );
00070 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines