kexi

kexidropdownbutton.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexidropdownbutton.h"
00021 
00022 #include <kpopupmenu.h>
00023 #include <kdebug.h>
00024 
00025 #include <qstyle.h>
00026 #include <qapplication.h>
00027 
00028 KexiDropDownButton::KexiDropDownButton(QWidget *parent)
00029  : QToolButton(parent, "KexiDBImageBox::Button")
00030 {
00031     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
00033 //  setFixedWidth(QMAX(18, qApp->globalStrut().width()));
00034     int fixedWidth;
00035     //hack
00036     if (qstricmp(style().name(),"thinkeramik")==0)
00037         fixedWidth = 18; //typical width as in "windows" style
00038     else
00039         fixedWidth = style().querySubControlMetrics( QStyle::CC_ComboBox, 
00040             this, QStyle::SC_ComboBoxArrow ).width();
00041     setFixedWidth( fixedWidth );
00042     setPopupDelay(10/*ms*/);
00043 }
00044 
00045 KexiDropDownButton::~KexiDropDownButton()
00046 {
00047 }
00048 
00049 void KexiDropDownButton::drawButton( QPainter *p )
00050 {
00051     QToolButton::drawButton(p);
00052     QStyle::SFlags arrowFlags = QStyle::Style_Default;
00053     if (isDown() || state()==On)
00054         arrowFlags |= QStyle::Style_Down;
00055     if (isEnabled())
00056         arrowFlags |= QStyle::Style_Enabled;
00057     style().drawPrimitive(QStyle::PE_ArrowDown, p,
00058         QRect((width()-7)/2, height()-9, 7, 7), colorGroup(),
00059         arrowFlags, QStyleOption() );
00060 }
00061 
00062 QSize KexiDropDownButton::sizeHint () const
00063 {
00064     return QSize( fontMetrics().maxWidth() + 2*2, fontMetrics().height()*2 + 2*2 );
00065 }
00066 
00067 void KexiDropDownButton::keyPressEvent( QKeyEvent * e ) 
00068 {
00069     const int k = e->key();
00070     const bool dropDown = (e->state() == Qt::NoButton && (k==Qt::Key_Space || k==Qt::Key_Enter || k==Qt::Key_Return || k==Qt::Key_F2 || k==Qt::Key_F4))
00071       || (e->state() == Qt::AltButton && k==Qt::Key_Down);
00072     if (dropDown) {
00073         e->accept();
00074         animateClick();
00075         QMouseEvent me( QEvent::MouseButtonPress, QPoint(2,2), Qt::LeftButton, Qt::NoButton );
00076         QApplication::sendEvent( this, &me );
00077         return;
00078     }
00079     QToolButton::keyPressEvent(e);
00080 }
00081 
00082 #include "kexidropdownbutton.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys