1
// vim: set tabstop=4 shiftwidth=4 expandtab:
3
Gwenview: an image viewer
4
Copyright 2011 Aurélien Gâteau <agateau@kde.org>
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.
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.
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.
22
#include "graphicshudbutton.moc"
25
#include <fullscreentheme.h>
29
#include <KGlobalSettings>
30
#include <KIconLoader>
33
#include <QFontMetrics>
34
#include <QGraphicsSceneEvent>
38
#include <QStyleOptionGraphicsItem>
50
struct GraphicsHudButtonPrivate
57
void initLayoutInfo(LayoutInfo* info)
59
FullScreenTheme::RenderInfo renderInfo = FullScreenTheme::renderInfo(FullScreenTheme::ButtonWidget);
60
const int padding = renderInfo.padding;
63
if (!mIcon.isNull()) {
64
int size = KIconLoader::global()->currentSize(KIconLoader::Small);
65
info->iconRect = QRect(padding, padding, size, size);
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);
77
QRectF rect = info->iconRect | info->textRect;
78
info->size = QSize(rect.right() + padding, rect.bottom() + padding);
82
GraphicsHudButton::GraphicsHudButton(QGraphicsItem* parent)
83
: QGraphicsWidget(parent)
84
, d(new GraphicsHudButtonPrivate)
87
setCursor(Qt::ArrowCursor);
88
setAcceptHoverEvents(true);
91
GraphicsHudButton::~GraphicsHudButton()
96
void GraphicsHudButton::setIcon(const QIcon& icon)
102
void GraphicsHudButton::setText(const QString& text)
108
void GraphicsHudButton::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget*)
110
FullScreenTheme::State state;
111
if (option->state.testFlag(QStyle::State_MouseOver)) {
112
state = d->mIsDown ? FullScreenTheme::DownState : FullScreenTheme::MouseOverState;
114
state = FullScreenTheme::NormalState;
116
FullScreenTheme::RenderInfo renderInfo = FullScreenTheme::renderInfo(FullScreenTheme::ButtonWidget, state);
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);
124
d->initLayoutInfo(&info);
126
if (!d->mIcon.isNull()) {
128
info.iconRect.topLeft(),
129
d->mIcon.pixmap(info.iconRect.size())
132
if (!d->mText.isEmpty()) {
133
painter->setPen(renderInfo.textPen);
141
QSizeF GraphicsHudButton::sizeHint(Qt::SizeHint /*which*/, const QSizeF& /*constraint*/) const
144
d->initLayoutInfo(&info);
148
void GraphicsHudButton::mousePressEvent(QGraphicsSceneMouseEvent*)
154
void GraphicsHudButton::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
158
if (boundingRect().contains(event->pos())) {