1
/* This file is part of the KDE project
2
Copyright (C) 2006-2008 Jarosław Staniek <staniek@kde.org>
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this program; see the file COPYING. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "kexidropdownbutton.h"
23
#include <QStyleOption>
25
#include <QApplication>
27
#include <QStyleOptionToolButton>
31
#include <kexi_global.h>
32
#include <kexiutils/styleproxy.h>
34
//! @internal A style that removes menu indicator from KexiDropDownButton.
35
class KexiDropDownButtonStyle : public KexiUtils::StyleProxy
38
KexiDropDownButtonStyle(QStyle *parentStyle)
39
: KexiUtils::StyleProxy(parentStyle)
42
virtual ~KexiDropDownButtonStyle() {}
44
virtual void drawComplexControl( ComplexControl control, const QStyleOptionComplex * option,
45
QPainter * painter, const QWidget * widget = 0 ) const
47
if (control == CC_ToolButton && qstyleoption_cast<const QStyleOptionToolButton *>(option)) {
48
QStyleOptionToolButton newOption(*qstyleoption_cast<const QStyleOptionToolButton *>(option));
49
newOption.features &= ~QStyleOptionToolButton::HasMenu;
51
StyleProxy::drawComplexControl(control, &newOption, painter, widget);
54
StyleProxy::drawComplexControl(control, option, painter, widget);
57
virtual int styleHint( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
59
if (hint == QStyle::SH_ToolButton_PopupDelay) {
62
return StyleProxy::styleHint(hint, option, widget, returnData);
67
KexiDropDownButton::KexiDropDownButton(QWidget *parent)
70
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
71
KexiDropDownButtonStyle *s = new KexiDropDownButtonStyle(style());
74
//! @todo get this from a KStyle
75
// setFixedWidth(QMAX(18, qApp->globalStrut().width()));
79
#warning TODO use subControlRect
81
#pragma WARNING( TODO use subControlRect )
84
if (style()->objectName().toLower()=="thinkeramik")
85
fixedWidth = 18; //typical width as in "windows" style
87
fixedWidth = style()->subControlRect( QStyle::CC_ComboBox, 0,
88
QStyle::SC_ComboBoxArrow ).width();
89
setFixedWidth( fixedWidth );
93
KexiDropDownButton::~KexiDropDownButton()
97
QSize KexiDropDownButton::sizeHint() const
99
return QSize(fontMetrics().maxWidth() + 2*2, fontMetrics().height()*2 + 2*2);
102
void KexiDropDownButton::paintEvent(QPaintEvent *e)
104
QToolButton::paintEvent(e);
106
QStyleOptionToolButton option;
107
option.initFrom(this);
108
//option.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
109
style()->drawPrimitive(QStyle::PE_IndicatorButtonDropDown, &option, &p);
111
//! @todo use tableview's appearance parameters for color
113
QPen linePen(Qt::black);
116
p.drawLine(r.topLeft(), r.topRight());
119
void KexiDropDownButton::keyPressEvent(QKeyEvent * e)
121
const int k = e->key();
122
const bool dropDown =
123
(e->modifiers() == Qt::NoButton
124
&& (k == Qt::Key_Space || k == Qt::Key_Enter || k == Qt::Key_Return || k == Qt::Key_F2
127
|| (e->modifiers() == Qt::AltButton && k == Qt::Key_Down);
132
QMouseEvent me(QEvent::MouseButtonPress, QPoint(2, 2), Qt::LeftButton, Qt::NoButton,
134
QApplication::sendEvent(this, &me);
137
QToolButton::keyPressEvent(e);
140
#include "kexidropdownbutton.moc"