void FontGrid::mousePressEvent(QMouseEvent *e) { int letter = getLetter(e->pos()); if(letter < _letterCountH*_letterCountV) { setLetter(letter); emit letterClicked(letter); } setFocus(); }
void FontGrid::keyPressEvent(QKeyEvent *e) { int letter; switch(e->key()) { case Qt::Key_Left: letter = _letter - 1; if(letter >= 0) { setLetter(letter); emit letterClicked(letter); } break; case Qt::Key_Right: letter = _letter + 1; if(letter < _letterCountH*_letterCountV) { setLetter(letter); emit letterClicked(letter); } break; case Qt::Key_Up: letter = _letter - _letterCountH; if(letter >= 0) { setLetter(letter); emit letterClicked(letter); } break; case Qt::Key_Down: letter = _letter + _letterCountH; if(letter < _letterCountH*_letterCountV) { setLetter(letter); emit letterClicked(letter); } break; } QWidget::keyPressEvent(e); }
void JumpButtonBar::updateButtons() { int currentButton = mGroupBox->selectedId(); // the easiest way to remove all buttons ;) mButtons.setAutoDelete( true ); mButtons.clear(); mButtons.setAutoDelete( false ); QStringList characters; // calculate how many buttons are possible QFontMetrics fm = fontMetrics(); QPushButton *btn = new QPushButton( "", this ); btn->hide(); QSize buttonSize = style().sizeFromContents( QStyle::CT_PushButton, btn, fm.size( ShowPrefix, "X - X") ). expandedTo( QApplication::globalStrut() ); delete btn; int buttonHeight = buttonSize.height() + 8; uint possibleButtons = (height() / buttonHeight) - 1; QString character; KABC::AddressBook *ab = mCore->addressBook(); KABC::AddressBook::Iterator it; for ( it = ab->begin(); it != ab->end(); ++it ) { KABC::Field *field = 0; field = mCore->currentSortField(); if ( field ) { setEnabled( true ); if ( !field->value( *it ).isEmpty() ) character = field->value( *it )[ 0 ].lower(); } else { setEnabled( false ); return; } if ( !character.isEmpty() && !characters.contains( character ) ) characters.append( character ); } sortListLocaleAware( characters ); if ( characters.count() <= possibleButtons ) { // at first the easy case: all buttons fits in window for ( uint i = 0; i < characters.count(); ++i ) { JumpButton *button = new JumpButton( characters[ i ], QString::null, mGroupBox ); connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) ); mButtons.append( button ); button->show(); } } else { if ( possibleButtons == 0 ) // to avoid crashes on startup return; int offset = characters.count() / possibleButtons; int odd = characters.count() % possibleButtons; if ( odd ) offset++; int current = 0; for ( uint i = 0; i < possibleButtons; ++i ) { if ( characters.count() - current == 0 ) continue; if ( characters.count() - current <= possibleButtons - i ) { JumpButton *button = new JumpButton( characters[ current ], QString::null, mGroupBox ); connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) ); mButtons.append( button ); button->show(); current++; } else { int pos = ( current + offset >= (int)characters.count() ? characters.count() - 1 : current + offset - 1 ); QString range; for ( int j = current; j < pos + 1; ++j ) range.append( characters[ j ] ); JumpButton *button = new JumpButton( characters[ current ], characters[ pos ], mGroupBox ); connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) ); mButtons.append( button ); button->show(); current = ( i + 1 ) * offset; } } } if ( currentButton != -1 ) mGroupBox->setButton( currentButton ); else mGroupBox->setButton( 0 ); int maxWidth = 0; QPushButton *button; for ( button = mButtons.first(); button; button = mButtons.next() ) maxWidth = QMAX( maxWidth, button->sizeHint().width() ); setFixedWidth( maxWidth ); }