lib

KoZoomAction.cpp

00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2004 Ariya Hidayat <ariya@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <KoZoomAction.h>
00020 
00021 #include <qstring.h>
00022 #include <qstringlist.h>
00023 #include <qregexp.h>
00024 #include <qvaluelist.h>
00025 
00026 #include <klocale.h>
00027 
00028 KoZoomAction::KoZoomAction( const QString& text, const QIconSet& pix, 
00029   const KShortcut& cut, QObject* parent, const char* name ):
00030   KSelectAction( text, pix, cut, parent, name )
00031 {
00032   init();
00033 }
00034       
00035 KoZoomAction::KoZoomAction( const QString& text, const QString& pix, 
00036   const KShortcut& cut, QObject* parent, const char* name ):
00037   KSelectAction( text, pix, cut, parent, name )
00038 {
00039   init();
00040   
00041 }
00042 
00043 void KoZoomAction::setZoom( const QString& text )
00044 {
00045   bool ok = false;
00046   QString t = text;
00047   int zoom = t.remove( '%' ).toInt( &ok );
00048   
00049   // where we'll store sorted new zoom values
00050   QValueList<int> list;
00051   if( zoom > 10 ) list.append( zoom );
00052   
00053   // "Captured" non-empty sequence of digits
00054   QRegExp regexp("(\\d+)"); 
00055   
00056   const QStringList itemsList( items() );
00057   for( QStringList::ConstIterator it = itemsList.begin(); it != itemsList.end(); ++it )
00058   {
00059     regexp.search( *it );
00060     const int val=regexp.cap(1).toInt( &ok );
00061     
00062     //zoom : limit inferior=10
00063     if( ok && val>9 && list.contains( val )==0 )
00064       list.append( val );
00065   }
00066   
00067   qHeapSort( list );
00068 
00069   // update items with new sorted zoom values
00070   QStringList values;
00071   for (QValueList<int>::Iterator it = list.begin(); it != list.end(); ++it )
00072     values.append( i18n("%1%").arg(*it) );
00073   setItems( values );
00074   
00075   QString zoomStr = i18n("%1%").arg( zoom );
00076   setCurrentItem( values.findIndex( zoomStr ) );
00077 }
00078 
00079 void KoZoomAction::setZoom( int zoom )
00080 {
00081   setZoom( QString::number( zoom ) );
00082 }
00083 
00084 void KoZoomAction::activated( const QString& text )
00085 {
00086   setZoom( text );
00087   emit zoomChanged( text );
00088 }
00089 
00090 void KoZoomAction::init()
00091 {
00092   setEditable( true );
00093     
00094   QStringList values;
00095   values << i18n("%1%").arg("33");
00096   values << i18n("%1%").arg("50");
00097   values << i18n("%1%").arg("75");
00098   values << i18n("%1%").arg("100");
00099   values << i18n("%1%").arg("125");
00100   values << i18n("%1%").arg("150");
00101   values << i18n("%1%").arg("200");
00102   values << i18n("%1%").arg("250");
00103   values << i18n("%1%").arg("350");
00104   values << i18n("%1%").arg("400");
00105   values << i18n("%1%").arg("450");
00106   values << i18n("%1%").arg("500");
00107   setItems( values );
00108   
00109   setCurrentItem( values.findIndex( i18n("%1%").arg( 100 ) ) );
00110   
00111   connect( this, SIGNAL( activated( const QString& ) ), 
00112     SLOT( activated( const QString& ) ) );
00113 }
00114 
00115 #include "KoZoomAction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys