~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kicker/kicker/ui/hidebutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-31 19:16:54 UTC
  • Revision ID: james.westby@ubuntu.com-20071031191654-xuof6e1jg6uxqaze
Tags: 3.95.0-0ubuntu1~gutsy1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (C) 2003-2004 Nadeem Hasan <nhasan@kde.org>
3
 
   Copyright (C) 2004 Aaron J. Seigo <aseigo@kde.org>
4
 
 
5
 
   This program is free software; you can redistribute it and/or
6
 
   modify it under the terms of the GNU General Public
7
 
   License as published by the Free Software Foundation; either
8
 
   version 2 of the License, or (at your option) any later version.
9
 
 
10
 
   This program is distributed in the hope that it will be useful,
11
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
   General Public License for more details.
14
 
 
15
 
   You should have received a copy of the GNU General Public License
16
 
   along with this program; see the file COPYING.  If not, write to
17
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
 
   Boston, MA 02110-1301, USA.
19
 
*/
20
 
 
21
 
#include "hidebutton.h"
22
 
 
23
 
#include <QPainter>
24
 
#include <QPixmap>
25
 
#include <QResizeEvent>
26
 
#include <QEvent>
27
 
 
28
 
#include <kcursor.h>
29
 
#include <kglobalsettings.h>
30
 
#include <kiconeffect.h>
31
 
#include <kiconloader.h>
32
 
#include <kicontheme.h>
33
 
#include <kstandarddirs.h>
34
 
 
35
 
HideButton::HideButton(QWidget *parent)
36
 
    : QAbstractButton(parent),
37
 
      m_highlight(false),
38
 
      m_arrow(Qt::LeftArrow)
39
 
{
40
 
    setBackgroundOrigin(AncestorOrigin);
41
 
 
42
 
    connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), SLOT(slotSettingsChanged(int)));
43
 
    connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)), SLOT(slotIconChanged(int)));
44
 
 
45
 
    slotSettingsChanged(KGlobalSettings::SETTINGS_MOUSE);
46
 
}
47
 
 
48
 
void HideButton::paintEvent(QPaintEvent*)
49
 
{
50
 
    QPainter painter( this );
51
 
    drawButton( &painter );
52
 
}
53
 
 
54
 
void HideButton::drawButton(QPainter *p)
55
 
{
56
 
    if (m_arrow == Qt::LeftArrow)
57
 
    {
58
 
        p->setPen(palette().color(QPalette::Mid));
59
 
        p->drawLine(width()-1, 0, width()-1, height());
60
 
    }
61
 
    else if (m_arrow == Qt::RightArrow)
62
 
    {
63
 
        p->setPen(palette().color(QPalette::Mid));
64
 
        p->drawLine(0, 0, 0, height());
65
 
    }
66
 
    else if (m_arrow == Qt::UpArrow)
67
 
    {
68
 
        p->setPen(palette().color(QPalette::Mid));
69
 
        p->drawLine(0, height()-1, width(), height()-1);
70
 
    }
71
 
    else if (m_arrow == Qt::DownArrow)
72
 
    {
73
 
        p->setPen(palette().color(QPalette::Mid));
74
 
        p->drawLine(0, 0, width(), 0);
75
 
    }
76
 
 
77
 
    drawButtonLabel(p);
78
 
}
79
 
 
80
 
void HideButton::drawButtonLabel(QPainter *p)
81
 
{
82
 
    QPixmap pix = m_highlight ? m_activeIcon : m_normalIcon;
83
 
 
84
 
    if (isChecked() || isDown())
85
 
    {
86
 
        p->translate(2, 2);
87
 
    }
88
 
 
89
 
    QPoint origin(2, 2);
90
 
    int w = width();
91
 
    int h = height();
92
 
    int pw = pix.width();
93
 
    int ph = pix.height();
94
 
 
95
 
    if (ph < (h - 4))
96
 
    {
97
 
        origin.setY(origin.y() + ((h - ph) / 2));
98
 
    }
99
 
 
100
 
    if (pw < (w - 4))
101
 
    {
102
 
        origin.setX(origin.x() + ((w - pw) / 2));
103
 
    }
104
 
 
105
 
    p->drawPixmap(origin, pix);
106
 
}
107
 
 
108
 
void HideButton::setPixmap(const QPixmap &pix)
109
 
{
110
 
    setIcon(pix);
111
 
    generateIcons();
112
 
}
113
 
 
114
 
void HideButton::setArrowType(Qt::ArrowType arrow)
115
 
{
116
 
    m_arrow = arrow;
117
 
    switch (arrow)
118
 
    {
119
 
        case Qt::LeftArrow:
120
 
            setPixmap(SmallIcon("arrow-left"));
121
 
        break;
122
 
 
123
 
        case Qt::RightArrow:
124
 
            setPixmap(SmallIcon("arrow-right"));
125
 
        break;
126
 
 
127
 
        case Qt::UpArrow:
128
 
            setPixmap(SmallIcon("arrow-up"));
129
 
        break;
130
 
 
131
 
        case Qt::DownArrow:
132
 
        default:
133
 
            setPixmap(SmallIcon("arrow-down"));
134
 
        break;
135
 
    }
136
 
}
137
 
 
138
 
void HideButton::generateIcons()
139
 
{
140
 
    if (icon().isNull())
141
 
    {
142
 
        m_normalIcon = QPixmap();
143
 
        m_activeIcon = QPixmap();
144
 
        return;
145
 
    }
146
 
 
147
 
    QPixmap pix = icon().pixmap();
148
 
    pix = pix.scaled(size() - QSize(4, 4), Qt::KeepAspectRatio, Qt::SmoothTransformation);
149
 
 
150
 
    KIconEffect effect;
151
 
    m_normalIcon = effect.apply(pix, KIconLoader::Panel, KIconLoader::DefaultState);
152
 
    m_activeIcon = effect.apply(pix, KIconLoader::Panel, KIconLoader::ActiveState);
153
 
}
154
 
 
155
 
void HideButton::slotSettingsChanged(int category)
156
 
{
157
 
    if (category != KGlobalSettings::SETTINGS_MOUSE)
158
 
    {
159
 
        return;
160
 
    }
161
 
 
162
 
    bool changeCursor = KGlobalSettings::changeCursorOverIcon();
163
 
 
164
 
    if (changeCursor)
165
 
    {
166
 
        setCursor(Qt::PointingHandCursor);
167
 
    }
168
 
    else
169
 
    {
170
 
        unsetCursor();
171
 
    }
172
 
}
173
 
 
174
 
void HideButton::slotIconChanged(int group)
175
 
{
176
 
    if (group != KIconLoader::Panel)
177
 
    {
178
 
        return;
179
 
    }
180
 
 
181
 
    generateIcons();
182
 
    repaint();
183
 
}
184
 
 
185
 
void HideButton::enterEvent(QEvent *e)
186
 
{
187
 
    m_highlight = true;
188
 
 
189
 
    repaint();
190
 
    QAbstractButton::enterEvent(e);
191
 
}
192
 
 
193
 
void HideButton::leaveEvent(QEvent *e)
194
 
{
195
 
    m_highlight = false;
196
 
 
197
 
    repaint();
198
 
    QAbstractButton::enterEvent(e);
199
 
}
200
 
 
201
 
void HideButton::resizeEvent(QResizeEvent *)
202
 
{
203
 
    generateIcons();
204
 
}
205
 
 
206
 
#include "hidebutton.moc"
207
 
 
208
 
// vim:ts=4:sw=4:et