~ubuntu-branches/ubuntu/quantal/gwenview/quantal-proposed

« back to all changes in this revision

Viewing changes to lib/graphicshudbutton.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac, Philip Muškovac, Scott Kitterman
  • Date: 2011-12-24 18:54:55 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111224185455-zxffs6i6m5cwwuze
Tags: 4:4.7.95-0ubuntu1
[ Philip Muškovac ]
* New upstream release candiate

[ Scott Kitterman ]
* Fix Ubuntu Vcs- header in debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2011 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "graphicshudbutton.moc"
 
23
 
 
24
// Local
 
25
#include <fullscreentheme.h>
 
26
 
 
27
// KDE
 
28
#include <KDebug>
 
29
#include <KGlobalSettings>
 
30
#include <KIconLoader>
 
31
 
 
32
// Qt
 
33
#include <QFontMetrics>
 
34
#include <QGraphicsSceneEvent>
 
35
#include <QIcon>
 
36
#include <QPainter>
 
37
#include <QStyle>
 
38
#include <QStyleOptionGraphicsItem>
 
39
 
 
40
namespace Gwenview
 
41
{
 
42
 
 
43
struct LayoutInfo
 
44
{
 
45
    QRect iconRect;
 
46
    QRect textRect;
 
47
    QSize size;
 
48
};
 
49
 
 
50
struct GraphicsHudButtonPrivate
 
51
{
 
52
    QIcon mIcon;
 
53
    QString mText;
 
54
 
 
55
    bool mIsDown;
 
56
 
 
57
    void initLayoutInfo(LayoutInfo* info)
 
58
    {
 
59
        FullScreenTheme::RenderInfo renderInfo = FullScreenTheme::renderInfo(FullScreenTheme::ButtonWidget);
 
60
        const int padding = renderInfo.padding;
 
61
 
 
62
        QSize sh;
 
63
        if (!mIcon.isNull()) {
 
64
            int size = KIconLoader::global()->currentSize(KIconLoader::Small);
 
65
            info->iconRect = QRect(padding, padding, size, size);
 
66
        }
 
67
        if (!mText.isEmpty()) {
 
68
            QFont font = KGlobalSettings::generalFont();
 
69
            QFontMetrics fm(font);
 
70
            QSize size = fm.size(0, mText);
 
71
            info->textRect = QRect(padding, padding, size.width(), size.height());
 
72
            if (!info->iconRect.isNull()) {
 
73
                info->textRect.translate(info->iconRect.right(), 0);
 
74
            }
 
75
        }
 
76
 
 
77
        QRectF rect = info->iconRect | info->textRect;
 
78
        info->size = QSize(rect.right() + padding, rect.bottom() + padding);
 
79
    }
 
80
};
 
81
 
 
82
GraphicsHudButton::GraphicsHudButton(QGraphicsItem* parent)
 
83
: QGraphicsWidget(parent)
 
84
, d(new GraphicsHudButtonPrivate)
 
85
{
 
86
    d->mIsDown = false;
 
87
    setCursor(Qt::ArrowCursor);
 
88
    setAcceptHoverEvents(true);
 
89
}
 
90
 
 
91
GraphicsHudButton::~GraphicsHudButton()
 
92
{
 
93
    delete d;
 
94
}
 
95
 
 
96
void GraphicsHudButton::setIcon(const QIcon& icon)
 
97
{
 
98
    d->mIcon = icon;
 
99
    updateGeometry();
 
100
}
 
101
 
 
102
void GraphicsHudButton::setText(const QString& text)
 
103
{
 
104
    d->mText = text;
 
105
    updateGeometry();
 
106
}
 
107
 
 
108
void GraphicsHudButton::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget*)
 
109
{
 
110
    FullScreenTheme::State state;
 
111
    if (option->state.testFlag(QStyle::State_MouseOver)) {
 
112
        state = d->mIsDown ? FullScreenTheme::DownState : FullScreenTheme::MouseOverState;
 
113
    } else {
 
114
        state = FullScreenTheme::NormalState;
 
115
    }
 
116
    FullScreenTheme::RenderInfo renderInfo = FullScreenTheme::renderInfo(FullScreenTheme::ButtonWidget, state);
 
117
 
 
118
    painter->setPen(renderInfo.borderPen);
 
119
    painter->setBrush(renderInfo.bgBrush);
 
120
    painter->setRenderHint(QPainter::Antialiasing);
 
121
    painter->drawRoundedRect(boundingRect().adjusted(.5, .5, -.5, -.5), renderInfo.borderRadius, renderInfo.borderRadius);
 
122
 
 
123
    LayoutInfo info;
 
124
    d->initLayoutInfo(&info);
 
125
 
 
126
    if (!d->mIcon.isNull()) {
 
127
        painter->drawPixmap(
 
128
            info.iconRect.topLeft(),
 
129
            d->mIcon.pixmap(info.iconRect.size())
 
130
        );
 
131
    }
 
132
    if (!d->mText.isEmpty()) {
 
133
        painter->setPen(renderInfo.textPen);
 
134
        painter->drawText(
 
135
            info.textRect,
 
136
            Qt::AlignCenter,
 
137
            d->mText);
 
138
    }
 
139
}
 
140
 
 
141
QSizeF GraphicsHudButton::sizeHint(Qt::SizeHint /*which*/, const QSizeF& /*constraint*/) const
 
142
{
 
143
    LayoutInfo info;
 
144
    d->initLayoutInfo(&info);
 
145
    return info.size;
 
146
}
 
147
 
 
148
void GraphicsHudButton::mousePressEvent(QGraphicsSceneMouseEvent*)
 
149
{
 
150
    d->mIsDown = true;
 
151
    update();
 
152
}
 
153
 
 
154
void GraphicsHudButton::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
 
155
{
 
156
    d->mIsDown = false;
 
157
    update();
 
158
    if (boundingRect().contains(event->pos())) {
 
159
        clicked();
 
160
    }
 
161
}
 
162
 
 
163
} // namespace