summaryrefslogtreecommitdiffabout
path: root/kaddressbook/jumpbuttonbar.cpp
authorMichael Krelin <hacker@klever.net>2007-07-04 11:23:42 (UTC)
committer Michael Krelin <hacker@klever.net>2007-07-04 11:23:42 (UTC)
commita08aff328d4393031d5ba7d622c2b05705a89d73 (patch) (unidiff)
tree8ee90d686081c52e7c69b5ce946e9b1a7d690001 /kaddressbook/jumpbuttonbar.cpp
parent11edc920afe4f274c0964436633aa632c8288a40 (diff)
downloadkdepimpi-a08aff328d4393031d5ba7d622c2b05705a89d73.zip
kdepimpi-a08aff328d4393031d5ba7d622c2b05705a89d73.tar.gz
kdepimpi-a08aff328d4393031d5ba7d622c2b05705a89d73.tar.bz2
initial public commit of qt4 portp1
Diffstat (limited to 'kaddressbook/jumpbuttonbar.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/jumpbuttonbar.cpp13
1 files changed, 8 insertions, 5 deletions
diff --git a/kaddressbook/jumpbuttonbar.cpp b/kaddressbook/jumpbuttonbar.cpp
index aebf8a6..740b7db 100644
--- a/kaddressbook/jumpbuttonbar.cpp
+++ b/kaddressbook/jumpbuttonbar.cpp
@@ -1,257 +1,260 @@
1/* 1/*
2 This file is part of KAddressBook. 2 This file is part of KAddressBook.
3 Copyright (c) 2002 Mike Pilone <mpilone@slac.com> 3 Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 13 GNU General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 18
19 As a special exception, permission is given to link this program 19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable, 20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution. 21 without including the source code for Qt in the source distribution.
22*/ 22*/
23 23
24#include <qevent.h> 24#include <qevent.h>
25#include <qlayout.h> 25#include <qlayout.h>
26#include <qpushbutton.h> 26#include <qpushbutton.h>
27#include <qstring.h> 27#include <qstring.h>
28#include <qtl.h> 28#include <q3tl.h>
29#include <qapplication.h> 29#include <qapplication.h>
30#include <QDesktopWidget>
31//Added by qt3to4:
32#include <Q3GridLayout>
30 33
31#include <kabc/addressbook.h> 34#include <kabc/addressbook.h>
32#include <kabc/field.h> 35#include <kabc/field.h>
33#include <kdebug.h> 36#include <kdebug.h>
34#include <klocale.h> 37#include <klocale.h>
35 38
36#include "kabcore.h" 39#include "kabcore.h"
37 40
38#include "jumpbuttonbar.h" 41#include "jumpbuttonbar.h"
39 42
40class JumpButton : public QPushButton 43class JumpButton : public QPushButton
41{ 44{
42 public: 45 public:
43 JumpButton( const QString &text, QWidget *parent, 46 JumpButton( const QString &text, QWidget *parent,
44 const QString &character ); 47 const QString &character );
45 48
46 void setCharacter( const QString &character ); 49 void setCharacter( const QString &character );
47 QString character() const; 50 QString character() const;
48 51
49 private: 52 private:
50 QString mCharacter; 53 QString mCharacter;
51}; 54};
52 55
53JumpButton::JumpButton( const QString &text, QWidget *parent, 56JumpButton::JumpButton( const QString &text, QWidget *parent,
54 const QString &character ) 57 const QString &character )
55 : QPushButton( text, parent ) 58 : QPushButton( text, parent )
56{ 59{
57 mCharacter = character; 60 mCharacter = character;
58} 61}
59 62
60void JumpButton::setCharacter( const QString &character ) 63void JumpButton::setCharacter( const QString &character )
61{ 64{
62 mCharacter = character; 65 mCharacter = character;
63 setText(mCharacter.upper() ); 66 setText(mCharacter.upper() );
64} 67}
65 68
66QString JumpButton::character() const 69QString JumpButton::character() const
67{ 70{
68 return mCharacter; 71 return mCharacter;
69} 72}
70 73
71JumpButtonBar::JumpButtonBar( KABCore *core, QWidget *parent, const char *name ) 74JumpButtonBar::JumpButtonBar( KABCore *core, QWidget *parent, const char *name )
72 : QWidget( parent, name ), mCore( core ) 75 : QWidget( parent, name ), mCore( core )
73{ 76{
74 if ( QApplication::desktop()->width() < 480 ) 77 if ( QApplication::desktop()->width() < 480 )
75 78
76 mButtonLayout = new QGridLayout( this, 1, 18 ); 79 mButtonLayout = new Q3GridLayout( this, 1, 18 );
77 else 80 else
78 mButtonLayout = new QGridLayout( this, 1, 20 ); 81 mButtonLayout = new Q3GridLayout( this, 1, 20 );
79 mButtonLayout->setAlignment( Qt::AlignTop ); 82 mButtonLayout->setAlignment( Qt::AlignTop );
80 83
81 recreateButtons(); 84 recreateButtons();
82} 85}
83 86
84JumpButtonBar::~JumpButtonBar() 87JumpButtonBar::~JumpButtonBar()
85{ 88{
86} 89}
87 90
88QSizePolicy JumpButtonBar::sizePolicy() const 91QSizePolicy JumpButtonBar::sizePolicy() const
89{ 92{
90#ifndef KAB_EMBEDDED 93#ifndef KAB_EMBEDDED
91 return QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Minimum, 94 return QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Minimum,
92 QSizePolicy::Vertically ); 95 QSizePolicy::Vertically );
93#else //KAB_EMBEDDED 96#else //KAB_EMBEDDED
94 return QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Minimum); 97 return QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Minimum);
95#endif //KAB_EMBEDDED 98#endif //KAB_EMBEDDED
96} 99}
97 100
98void JumpButtonBar::letterClicked() 101void JumpButtonBar::letterClicked()
99{ 102{
100 JumpButton *button = (JumpButton*)sender(); 103 JumpButton *button = (JumpButton*)sender();
101 QString character = button->character(); 104 QString character = button->character();
102 if ( character.length() == 2 ) 105 if ( character.length() == 2 )
103 character = character.left(1) +"-"+character.right(1); 106 character = character.left(1) +"-"+character.right(1);
104 if ( !character.isNull() ) 107 if ( !character.isNull() )
105 emit jumpToLetter( character ); 108 emit jumpToLetter( character );
106 //qDebug("emit *%s* ",character.latin1()); 109 //qDebug("emit *%s* ",character.latin1());
107} 110}
108 111
109void JumpButtonBar::recreateButtons() 112void JumpButtonBar::recreateButtons()
110{ 113{
111 // the easiest way to remove all buttons ;) 114 // the easiest way to remove all buttons ;)
112 //mButtons.setAutoDelete( true ); 115 //mButtons.setAutoDelete( true );
113 //mButtons.clear(); 116 //mButtons.clear();
114 //mButtons.setAutoDelete( false ); 117 //mButtons.setAutoDelete( false );
115 mCharacters.clear(); 118 mCharacters.clear();
116 119
117 QString character; 120 QString character;
118 121
119 KABC::AddressBook *ab = mCore->addressBook(); 122 KABC::AddressBook *ab = mCore->addressBook();
120 KABC::AddressBook::Iterator it; 123 KABC::AddressBook::Iterator it;
121 KABC::Field *field = mCore->currentSearchField(); 124 KABC::Field *field = mCore->currentSearchField();
122 if ( field ) { 125 if ( field ) {
123 setEnabled( true ); 126 setEnabled( true );
124 } else { 127 } else {
125 setEnabled( false ); 128 setEnabled( false );
126 return; 129 return;
127 } 130 }
128 mCharacters.append( "*"); 131 mCharacters.append( "*");
129 for ( it = ab->begin(); it != ab->end(); ++it ) { 132 for ( it = ab->begin(); it != ab->end(); ++it ) {
130 if ( !field->value( *it ).isEmpty() ) 133 if ( !field->value( *it ).isEmpty() )
131 character = field->value( *it )[ 0 ].lower(); 134 character = field->value( *it )[ 0 ].lower();
132 if ( character != "!" ) { 135 if ( character != "!" ) {
133 if ( !character.isEmpty() && !mCharacters.contains( character ) && character.at( 0 ).isLetter ()) 136 if ( !character.isEmpty() && !mCharacters.contains( character ) && character.at( 0 ).isLetter ())
134 mCharacters.append( character ); 137 mCharacters.append( character );
135 } 138 }
136 } 139 }
137 if ( mCharacters.count() == 0 ) { 140 if ( mCharacters.count() == 0 ) {
138 setEnabled( false ); 141 setEnabled( false );
139 return; 142 return;
140 } 143 }
141 144
142 int maxRows = mCharacters.count() / 2; // we use 2 columns 145 int maxRows = mCharacters.count() / 2; // we use 2 columns
143 if ( mCharacters.count() % 2 ) 146 if ( mCharacters.count() % 2 )
144 maxRows++; 147 maxRows++;
145 sortListLocaleAware( mCharacters ); 148 sortListLocaleAware( mCharacters );
146 bool skip2 = false; 149 bool skip2 = false;
147 int skipcount = 0; 150 int skipcount = 0;
148 int maxHei = 0; 151 int maxHei = 0;
149#ifdef DESKTOP_VERSION 152#ifdef DESKTOP_VERSION
150 int maxChar = 16; 153 int maxChar = 16;
151 int heightDiff = 200; 154 int heightDiff = 200;
152 int fixwid = 36; 155 int fixwid = 36;
153#else 156#else
154 //default for 240x320 diaplay: 157 //default for 240x320 diaplay:
155 int maxChar = 16; 158 int maxChar = 16;
156 int heightDiff = 64; 159 int heightDiff = 64;
157 int fixwid = 20; 160 int fixwid = 20;
158 161
159 if ( QApplication::desktop()->height() == 480 ) { 162 if ( QApplication::desktop()->height() == 480 ) {
160 maxChar = 16; 163 maxChar = 16;
161 heightDiff = 135; 164 heightDiff = 135;
162 fixwid = 34; 165 fixwid = 34;
163 } else if ( QApplication::desktop()->height() >= 640 ) { 166 } else if ( QApplication::desktop()->height() >= 640 ) {
164 maxChar = 20; 167 maxChar = 20;
165 heightDiff = 135; 168 heightDiff = 135;
166 fixwid = 34; 169 fixwid = 34;
167 } 170 }
168#endif 171#endif
169 172
170 if ( mCharacters.count() > maxChar ) 173 if ( mCharacters.count() > maxChar )
171 skipcount = mCharacters.count()- maxChar; 174 skipcount = mCharacters.count()- maxChar;
172 maxHei = (QApplication::desktop()->height()-heightDiff)/(maxChar); 175 maxHei = (QApplication::desktop()->height()-heightDiff)/(maxChar);
173 176
174 177
175 maxRows = 28; 178 maxRows = 28;
176 bool skipcurrent = false; 179 bool skipcurrent = false;
177 bool state = isUpdatesEnabled(); 180 bool state = isUpdatesEnabled();
178 setUpdatesEnabled( false ); 181 setUpdatesEnabled( false );
179 //qDebug("cc %d ",mCharacters.count() ); 182 //qDebug("cc %d ",mCharacters.count() );
180 JumpButton *button = 0; 183 JumpButton *button = 0;
181 int row = 0, col = 0; 184 int row = 0, col = 0;
182 JumpButton* cur = mButtons.first(); 185 JumpButton* cur = mButtons.first();
183 for ( uint i = 0; i < mCharacters.count(); ++i ) { 186 for ( uint i = 0; i < mCharacters.count(); ++i ) {
184 if ( skipcount > 0 && skipcurrent ) { 187 if ( skipcount > 0 && skipcurrent ) {
185 --skipcount; 188 --skipcount;
186 if ( button ) { 189 if ( button ) {
187 button->setCharacter( button->character() + mCharacters[ i ]); 190 button->setCharacter( button->character() + mCharacters[ i ]);
188 } 191 }
189 } else { 192 } else {
190 if ( cur ) { 193 if ( cur ) {
191 button = cur ; 194 button = cur ;
192 cur = mButtons.next(); 195 cur = mButtons.next();
193 button->setCharacter(mCharacters[ i ]); 196 button->setCharacter(mCharacters[ i ]);
194 } else { 197 } else {
195 button = new JumpButton( mCharacters[ i ].upper(), this, mCharacters[ i ] ); 198 button = new JumpButton( mCharacters[ i ].upper(), this, mCharacters[ i ] );
196 if ( fixwid ) 199 if ( fixwid )
197 button->setFixedWidth( fixwid ); 200 button->setFixedWidth( fixwid );
198 mButtons.append( button ); 201 mButtons.append( button );
199 connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) ); 202 connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) );
200 mButtonLayout->addWidget( button, row, col ); 203 mButtonLayout->addWidget( button, row, col );
201 } 204 }
202 if ( maxHei ) 205 if ( maxHei )
203 button->setMaximumHeight( maxHei ); 206 button->setMaximumHeight( maxHei );
204 button->show(); 207 button->show();
205 208
206 if ( col == maxRows ) { 209 if ( col == maxRows ) {
207 row = 0; 210 row = 0;
208 col++; 211 col++;
209 } else 212 } else
210 row++; 213 row++;
211 } 214 }
212 if ( i > 0 ) 215 if ( i > 0 )
213 skipcurrent = !skipcurrent; 216 skipcurrent = !skipcurrent;
214 } 217 }
215 while ( cur ) { 218 while ( cur ) {
216 cur->hide(); 219 cur->hide();
217 cur = mButtons.next(); 220 cur = mButtons.next();
218 } 221 }
219 222
220 mButtonLayout->activate(); 223 mButtonLayout->activate();
221 setUpdatesEnabled( state ); 224 setUpdatesEnabled( state );
222 update(); 225 update();
223} 226}
224 227
225void JumpButtonBar::sortListLocaleAware( QStringList &list ) 228void JumpButtonBar::sortListLocaleAware( QStringList &list )
226{ 229{
227 QStringList::Iterator beginIt = list.begin(); 230 QStringList::Iterator beginIt = list.begin();
228 QStringList::Iterator endIt = list.end(); 231 QStringList::Iterator endIt = list.end();
229 232
230 --endIt; 233 --endIt;
231 if ( beginIt == endIt ) // don't need sorting 234 if ( beginIt == endIt ) // don't need sorting
232 return; 235 return;
233 236
234 QStringList::Iterator walkBackIt = endIt; 237 QStringList::Iterator walkBackIt = endIt;
235 while ( beginIt != endIt ) { 238 while ( beginIt != endIt ) {
236 QStringList::Iterator j1 = list.begin(); 239 QStringList::Iterator j1 = list.begin();
237 QStringList::Iterator j2 = j1; 240 QStringList::Iterator j2 = j1;
238 ++j2; 241 ++j2;
239 while ( j1 != walkBackIt ) { 242 while ( j1 != walkBackIt ) {
240#ifndef KAB_EMBEDDED 243#ifndef KAB_EMBEDDED
241 if ( QString::localeAwareCompare( *j2, *j1 ) < 0 ) 244 if ( QString::localeAwareCompare( *j2, *j1 ) < 0 )
242#else //KAB_EMBEDDED 245#else //KAB_EMBEDDED
243 if ( QString::compare( *j2, *j1 ) < 0 ) 246 if ( QString::compare( *j2, *j1 ) < 0 )
244#endif //KAB_EMBEDDED 247#endif //KAB_EMBEDDED
245 qSwap( *j1, *j2 ); 248 qSwap( *j1, *j2 );
246 249
247 ++j1; 250 ++j1;
248 ++j2; 251 ++j2;
249 } 252 }
250 ++beginIt; 253 ++beginIt;
251 --walkBackIt; 254 --walkBackIt;
252 } 255 }
253} 256}
254 257
255#ifndef KAB_EMBEDDED 258#ifndef KAB_EMBEDDED_
256#include "jumpbuttonbar.moc" 259#include "moc_jumpbuttonbar.cpp"
257#endif //KAB_EMBEDDED 260#endif //KAB_EMBEDDED