~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/widget/utils/kexidropdownbutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2006-2008 Jarosław Staniek <staniek@kde.org>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include "kexidropdownbutton.h"
 
21
 
 
22
#include <QStyle>
 
23
#include <QStyleOption>
 
24
#include <QPainter>
 
25
#include <QApplication>
 
26
#include <QKeyEvent>
 
27
#include <QStyleOptionToolButton>
 
28
 
 
29
#include <KDebug>
 
30
 
 
31
#include <kexi_global.h>
 
32
#include <kexiutils/styleproxy.h>
 
33
 
 
34
//! @internal A style that removes menu indicator from KexiDropDownButton.
 
35
class KexiDropDownButtonStyle : public KexiUtils::StyleProxy
 
36
{
 
37
public:
 
38
    KexiDropDownButtonStyle(QStyle *parentStyle)
 
39
            : KexiUtils::StyleProxy(parentStyle)
 
40
    {
 
41
    }
 
42
    virtual ~KexiDropDownButtonStyle() {}
 
43
 
 
44
    virtual void drawComplexControl( ComplexControl control, const QStyleOptionComplex * option,
 
45
        QPainter * painter, const QWidget * widget = 0 ) const
 
46
    {
 
47
        if (control == CC_ToolButton && qstyleoption_cast<const QStyleOptionToolButton *>(option)) {
 
48
            QStyleOptionToolButton newOption(*qstyleoption_cast<const QStyleOptionToolButton *>(option));
 
49
            newOption.features &= ~QStyleOptionToolButton::HasMenu;
 
50
 
 
51
            StyleProxy::drawComplexControl(control, &newOption, painter, widget);
 
52
            return;
 
53
        }
 
54
        StyleProxy::drawComplexControl(control, option, painter, widget);
 
55
    }
 
56
 
 
57
    virtual int styleHint( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const
 
58
    {
 
59
        if (hint == QStyle::SH_ToolButton_PopupDelay) {
 
60
            return 0;
 
61
        }
 
62
        return StyleProxy::styleHint(hint, option, widget, returnData);
 
63
    }
 
64
};
 
65
 
 
66
 
 
67
KexiDropDownButton::KexiDropDownButton(QWidget *parent)
 
68
        : QToolButton(parent)
 
69
{
 
70
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
 
71
    KexiDropDownButtonStyle *s = new KexiDropDownButtonStyle(style());
 
72
    s->setParent(this);
 
73
    setStyle(s);
 
74
//! @todo get this from a KStyle
 
75
// setFixedWidth(QMAX(18, qApp->globalStrut().width()));
 
76
//    int fixedWidth;
 
77
    //hack
 
78
#ifdef __GNUC__
 
79
#warning TODO use subControlRect
 
80
#else
 
81
#pragma WARNING( TODO use subControlRect )
 
82
#endif
 
83
    /*TODO
 
84
      if (style()->objectName().toLower()=="thinkeramik")
 
85
        fixedWidth = 18; //typical width as in "windows" style
 
86
      else
 
87
        fixedWidth = style()->subControlRect( QStyle::CC_ComboBox, 0,
 
88
          QStyle::SC_ComboBoxArrow ).width();
 
89
      setFixedWidth( fixedWidth );
 
90
      */
 
91
}
 
92
 
 
93
KexiDropDownButton::~KexiDropDownButton()
 
94
{
 
95
}
 
96
 
 
97
QSize KexiDropDownButton::sizeHint() const
 
98
{
 
99
    return QSize(fontMetrics().maxWidth() + 2*2, fontMetrics().height()*2 + 2*2);
 
100
}
 
101
 
 
102
void KexiDropDownButton::paintEvent(QPaintEvent *e)
 
103
{
 
104
    QToolButton::paintEvent(e);
 
105
    QPainter p(this);
 
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);
 
110
 
 
111
    //! @todo use tableview's appearance parameters for color
 
112
    QRect r = rect();
 
113
    QPen linePen(Qt::black);
 
114
    linePen.setWidth(1);
 
115
    p.setPen(linePen);
 
116
    p.drawLine(r.topLeft(), r.topRight());
 
117
}
 
118
 
 
119
void KexiDropDownButton::keyPressEvent(QKeyEvent * e)
 
120
{
 
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
 
125
             || k == Qt::Key_F4)
 
126
        )
 
127
        || (e->modifiers() == Qt::AltButton && k == Qt::Key_Down);
 
128
 
 
129
    if (dropDown) {
 
130
        e->accept();
 
131
        animateClick();
 
132
        QMouseEvent me(QEvent::MouseButtonPress, QPoint(2, 2), Qt::LeftButton, Qt::NoButton,
 
133
                       Qt::NoModifier);
 
134
        QApplication::sendEvent(this, &me);
 
135
        return;
 
136
    }
 
137
    QToolButton::keyPressEvent(e);
 
138
}
 
139
 
 
140
#include "kexidropdownbutton.moc"