transmission/qt/triconpushbutton.cc

62 lines
1.6 KiB
C++
Raw Normal View History

/*
* This file Copyright (C) 2009-2014 Mnemosyne LLC
*
* It may be used under the GNU Public License v2 or v3 licenses,
* or any future license endorsed by Mnemosyne LLC.
*
* $Id$
*/
#include <iostream>
#include <QIcon>
#include <QStyleOption>
#include <QStyleOptionButton>
#include <QStylePainter>
#include "hig.h"
#include "triconpushbutton.h"
TrIconPushButton::TrIconPushButton (QWidget * parent):
2013-09-14 22:45:04 +00:00
QPushButton (parent)
{
}
TrIconPushButton::TrIconPushButton (const QIcon& icon, QWidget * parent):
2013-09-14 22:45:04 +00:00
QPushButton (parent)
{
2013-09-14 22:45:04 +00:00
setIcon (icon);
}
QSize
TrIconPushButton::sizeHint () const
{
2013-09-14 22:45:04 +00:00
QSize s = iconSize ();
s.rwidth() += HIG::PAD_SMALL*2;
return s;
}
void
TrIconPushButton::paintEvent (QPaintEvent *)
{
2013-09-14 22:45:04 +00:00
QStylePainter p (this);
QStyleOptionButton opt;
initStyleOption (&opt);
2013-09-14 22:45:04 +00:00
QIcon::Mode mode = opt.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
if ((mode == QIcon::Normal) && (opt.state & QStyle::State_HasFocus))
mode = QIcon::Active;
QPixmap pixmap = opt.icon.pixmap (opt.iconSize, QIcon::Active, QIcon::On);
QRect iconRect (opt.rect.x() + HIG::PAD_SMALL,
opt.rect.y() + (opt.rect.height() - pixmap.height())/2,
pixmap.width(),
pixmap.height());
if (opt.state & (QStyle::State_On | QStyle::State_Sunken))
iconRect.translate (style()->pixelMetric (QStyle::PM_ButtonShiftHorizontal, &opt, this),
style()->pixelMetric (QStyle::PM_ButtonShiftVertical, &opt, this));
2013-09-14 22:45:04 +00:00
p.drawPixmap(iconRect, pixmap);
2013-09-14 22:45:04 +00:00
if (opt.state & QStyle::State_HasFocus)
p.drawPrimitive (QStyle::PE_FrameFocusRect, opt);
}