lib

KoZoomAction.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2004 Ariya Hidayat <ariya@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#include <KoZoomAction.h>
20
21#include <tqstring.h>
22#include <tqstringlist.h>
23#include <tqregexp.h>
24#include <tqvaluelist.h>
25
26#include <tdelocale.h>
27
28KoZoomAction::KoZoomAction( const TQString& text, const TQIconSet& pix,
29 const TDEShortcut& cut, TQObject* parent, const char* name ):
30 TDESelectAction( text, pix, cut, parent, name )
31{
32 init();
33}
34
35KoZoomAction::KoZoomAction( const TQString& text, const TQString& pix,
36 const TDEShortcut& cut, TQObject* parent, const char* name ):
37 TDESelectAction( text, pix, cut, parent, name )
38{
39 init();
40
41}
42
43void KoZoomAction::setZoom( const TQString& text )
44{
45 bool ok = false;
46 TQString t = text;
47 int zoom = t.remove( '%' ).toInt( &ok );
48
49 // where we'll store sorted new zoom values
50 TQValueList<int> list;
51 if( zoom > 10 ) list.append( zoom );
52
53 // "Captured" non-empty sequence of digits
54 TQRegExp regexp("(\\d+)");
55
56 const TQStringList itemsList( items() );
57 for( TQStringList::ConstIterator it = itemsList.begin(); it != itemsList.end(); ++it )
58 {
59 regexp.search( *it );
60 const int val=regexp.cap(1).toInt( &ok );
61
62 //zoom : limit inferior=10
63 if( ok && val>9 && list.contains( val )==0 )
64 list.append( val );
65 }
66
67 qHeapSort( list );
68
69 // update items with new sorted zoom values
70 TQStringList values;
71 for (TQValueList<int>::Iterator it = list.begin(); it != list.end(); ++it )
72 values.append( i18n("%1%").arg(*it) );
73 setItems( values );
74
75 TQString zoomStr = i18n("%1%").arg( zoom );
76 setCurrentItem( values.findIndex( zoomStr ) );
77}
78
79void KoZoomAction::setZoom( int zoom )
80{
81 setZoom( TQString::number( zoom ) );
82}
83
84void KoZoomAction::activated( const TQString& text )
85{
86 setZoom( text );
87 emit zoomChanged( text );
88}
89
90void KoZoomAction::init()
91{
92 setEditable( true );
93
94 TQStringList values;
95 values << i18n("%1%").arg("33");
96 values << i18n("%1%").arg("50");
97 values << i18n("%1%").arg("75");
98 values << i18n("%1%").arg("100");
99 values << i18n("%1%").arg("125");
100 values << i18n("%1%").arg("150");
101 values << i18n("%1%").arg("200");
102 values << i18n("%1%").arg("250");
103 values << i18n("%1%").arg("350");
104 values << i18n("%1%").arg("400");
105 values << i18n("%1%").arg("450");
106 values << i18n("%1%").arg("500");
107 setItems( values );
108
109 setCurrentItem( values.findIndex( i18n("%1%").arg( 100 ) ) );
110
111 connect( this, TQ_SIGNAL( activated( const TQString& ) ),
112 TQ_SLOT( activated( const TQString& ) ) );
113}
114
115#include "KoZoomAction.moc"
void zoomChanged(const TQString &zoom)
Signal zoomChanged is triggered when user changes the zoom value, either by choosing it from the list...
void setZoom(const TQString &zoom)
Sets the zoom.
KoZoomAction(const TQString &text, const TQIconSet &pix, const TDEShortcut &cut=TDEShortcut(), TQObject *parent=0, const char *name=0)
Creates a new zoom action.
virtual TQString text() const
void activated()
virtual void setEditable(bool)
virtual void setCurrentItem(int index)
virtual TQStringList items() const
virtual void setItems(const TQStringList &lst)
TDESelectAction(const TQString &text, const TDEShortcut &cut=TDEShortcut(), TQObject *parent=0, const char *name=0)