24 #include <tqapplication.h>
25 #include <tqbuttongroup.h>
28 #include <tqpushbutton.h>
32 #include <tdeabc/addressbook.h>
33 #include <tdeabc/field.h>
36 #include <tdelocale.h>
40 #include "jumpbuttonbar.h"
42 class JumpButton :
public TQPushButton
45 JumpButton(
const TQString &firstChar,
const TQString &lastChar,
48 TQString firstChar()
const {
return mChar; }
54 JumpButton::JumpButton(
const TQString &firstChar,
const TQString &lastChar,
56 : TQPushButton(
"", parent ), mChar( firstChar )
58 setToggleButton(
true );
59 if ( !lastChar.isEmpty() )
60 setText( TQString(
"%1 - %2" ).arg( firstChar.upper() ).arg( lastChar.upper() ) );
62 setText( firstChar.upper() );
65 JumpButtonBar::JumpButtonBar( KAB::Core *core, TQWidget *parent,
const char *name )
66 : TQWidget( parent, name ), mCore( core )
68 setMinimumSize( 1, 1 );
70 TQVBoxLayout *layout =
new TQVBoxLayout(
this, 0, 0 );
71 layout->setAlignment( TQt::AlignTop );
72 layout->setAutoAdd(
true );
73 layout->setResizeMode( TQLayout::FreeResize );
75 mGroupBox =
new TQButtonGroup( 1, TQt::Horizontal,
this );
76 mGroupBox->setExclusive(
true );
77 mGroupBox->layout()->setSpacing( 0 );
78 mGroupBox->layout()->setMargin( 0 );
79 mGroupBox->setFrameStyle( TQFrame::NoFrame );
82 JumpButtonBar::~JumpButtonBar()
86 void JumpButtonBar::updateButtons()
88 int currentButton = mGroupBox->selectedId();
91 mButtons.setAutoDelete(
true );
93 mButtons.setAutoDelete(
false );
95 TQStringList characters;
98 TQFontMetrics fm = fontMetrics();
99 TQPushButton *btn =
new TQPushButton(
"",
this );
101 TQSize buttonSize = style().sizeFromContents( TQStyle::CT_PushButton, btn,
102 fm.size( ShowPrefix,
"X - X") ).
103 expandedTo( TQApplication::globalStrut() );
106 int buttonHeight = buttonSize.height() + 8;
107 uint possibleButtons = (height() / buttonHeight) - 1;
110 TDEABC::AddressBook *ab = mCore->addressBook();
111 TDEABC::AddressBook::Iterator it;
112 for ( it = ab->begin(); it != ab->end(); ++it ) {
113 TDEABC::Field *field = 0;
114 field = mCore->currentSortField();
117 if ( !field->value( *it ).isEmpty() )
118 character = field->value( *it )[ 0 ].lower();
124 if ( !character.isEmpty() && !characters.contains( character ) )
125 characters.append( character );
128 sortListLocaleAware( characters );
130 if ( characters.count() <= possibleButtons ) {
132 for ( uint i = 0; i < characters.count(); ++i ) {
133 JumpButton *button =
new JumpButton( characters[ i ], TQString(),
135 connect( button, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( letterClicked() ) );
136 mButtons.append( button );
140 if ( possibleButtons == 0 )
142 int offset = characters.count() / possibleButtons;
143 int odd = characters.count() % possibleButtons;
148 for ( uint i = 0; i < possibleButtons; ++i ) {
149 if ( characters.count() - current == 0 )
151 if ( characters.count() - current <= possibleButtons - i ) {
152 JumpButton *button =
new JumpButton( characters[ current ],
153 TQString(), mGroupBox );
154 connect( button, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( letterClicked() ) );
155 mButtons.append( button );
159 int pos = ( current + offset >= (int)characters.count() ?
160 characters.count() - 1 : current + offset - 1 );
162 for (
int j = current; j < pos + 1; ++j )
163 range.append( characters[ j ] );
164 JumpButton *button =
new JumpButton( characters[ current ],
165 characters[ pos ], mGroupBox );
166 connect( button, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( letterClicked() ) );
167 mButtons.append( button );
169 current = ( i + 1 ) * offset;
174 if ( currentButton != -1 )
175 mGroupBox->setButton( currentButton );
177 mGroupBox->setButton( 0 );
180 TQPushButton *button;
181 for ( button = mButtons.first(); button; button = mButtons.next() )
182 maxWidth = TQMAX( maxWidth, button->sizeHint().width() );
184 setFixedWidth( maxWidth );
187 void JumpButtonBar::letterClicked()
189 JumpButton *button = (JumpButton*)sender();
190 TQString character = button->firstChar();
195 void JumpButtonBar::resizeEvent( TQResizeEvent* )
204 SortContainer(
const TQString &
string )
209 bool operator< (
const SortContainer &cnt )
211 return ( TQString::localeAwareCompare( mString, cnt.mString ) < 0 );
214 TQString data()
const
223 void JumpButtonBar::sortListLocaleAware( TQStringList &list )
225 TQValueList<SortContainer> sortList;
227 TQStringList::ConstIterator it;
228 for ( it = list.begin(); it != list.end(); ++it )
229 sortList.append( SortContainer( *it ) );
231 qHeapSort( sortList );
234 TQValueList<SortContainer>::ConstIterator sortIt;
235 for ( sortIt = sortList.begin(); sortIt != sortList.end(); ++sortIt )
236 list.append( (*sortIt).data() );
239 #include "jumpbuttonbar.moc"