kdelibs/plasma/widgets/toolbutton.cpp
Ivailo Monev b8325fec7e plasma: drop support for setting image instead of icon for widgets
mixing the usual icons with svg icons makes things look different, does it
not?

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
2024-05-23 10:10:13 +03:00

348 lines
9.4 KiB
C++

/*
* Copyright 2008 Marco Martin <notmart@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "toolbutton.h"
#include "animator.h"
#include "framesvg.h"
#include "private/actionwidgetinterface_p.h"
#include "private/themedwidgetinterface_p.h"
#include <QPropertyAnimation>
#include <QStyleOptionToolButton>
#include <QToolButton>
#include <kcolorutils.h>
namespace Plasma
{
class ToolButtonPrivate : public ActionWidgetInterface<ToolButton>
{
public:
ToolButtonPrivate(ToolButton *toolButton)
: ActionWidgetInterface<ToolButton>(toolButton),
background(nullptr),
animation(nullptr),
opacity(1.0),
underMouse(false)
{
}
void syncActiveRect();
void syncBorders();
void animationUpdate(qreal progress);
FrameSvg *background;
QPropertyAnimation *animation;
qreal opacity;
QRectF activeRect;
bool underMouse;
};
void ToolButtonPrivate::syncActiveRect()
{
background->setElementPrefix("normal");
qreal left = 0.0;
qreal top = 0.0;
qreal right = 0.0;
qreal bottom = 0.0;
background->getMargins(left, top, right, bottom);
background->setElementPrefix("active");
qreal activeLeft, activeTop, activeRight, activeBottom;
background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
activeRect = QRectF(QPointF(0, 0), q->size());
activeRect.adjust(left - activeLeft, top - activeTop,
-(right - activeRight), -(bottom - activeBottom));
background->setElementPrefix("normal");
}
void ToolButtonPrivate::syncBorders()
{
//set margins from the normal element
qreal left = 0.0;
qreal top = 0.0;
qreal right = 0.0;
qreal bottom = 0.0;
background->setElementPrefix("normal");
background->getMargins(left, top, right, bottom);
q->setContentsMargins(left, top, right, bottom);
// calc the rect for the over effect
syncActiveRect();
}
void ToolButtonPrivate::animationUpdate(qreal progress)
{
opacity = progress;
// explicit update
q->update();
}
ToolButton::ToolButton(QGraphicsWidget *parent)
: QGraphicsProxyWidget(parent),
d(new ToolButtonPrivate(this))
{
d->background = new FrameSvg(this);
d->background->setImagePath("widgets/button");
d->background->setCacheAllRenderedFrames(true);
d->background->setElementPrefix("normal");
QToolButton *native = new QToolButton();
connect(native, SIGNAL(clicked()), this, SIGNAL(clicked()));
connect(native, SIGNAL(pressed()), this, SIGNAL(pressed()));
connect(native, SIGNAL(released()), this, SIGNAL(released()));
setWidget(native);
native->setWindowIcon(QIcon());
native->setAttribute(Qt::WA_NoSystemBackground);
native->setAutoRaise(true);
d->syncBorders();
setAcceptHoverEvents(true);
connect(d->background, SIGNAL(repaintNeeded()), SLOT(syncBorders()));
d->animation = new QPropertyAnimation(this, "animationUpdate");
d->animation->setStartValue(0);
d->animation->setEndValue(1);
d->initTheming();
}
ToolButton::~ToolButton()
{
delete d->animation;
delete d;
}
void ToolButton::setAnimationUpdate(qreal progress)
{
d->animationUpdate(progress);
}
qreal ToolButton::animationUpdate() const
{
return d->opacity;
}
void ToolButton::setAction(QAction *action)
{
d->setAction(action);
}
QAction *ToolButton::action() const
{
return d->action;
}
void ToolButton::setAutoRaise(bool raise)
{
nativeWidget()->setAutoRaise(raise);
}
bool ToolButton::autoRaise() const
{
return nativeWidget()->autoRaise();
}
void ToolButton::setText(const QString &text)
{
nativeWidget()->setText(text);
updateGeometry();
}
QString ToolButton::text() const
{
return nativeWidget()->text();
}
void ToolButton::setIcon(const QIcon &icon)
{
nativeWidget()->setIcon(icon);
}
QIcon ToolButton::icon() const
{
return nativeWidget()->icon();
}
void ToolButton::setDown(bool down)
{
nativeWidget()->setDown(down);
}
bool ToolButton::isDown() const
{
return nativeWidget()->isDown();
}
QToolButton *ToolButton::nativeWidget() const
{
return static_cast<QToolButton*>(widget());
}
void ToolButton::resizeEvent(QGraphicsSceneResizeEvent *event)
{
if (d->background) {
// resize all four panels
d->background->setElementPrefix("pressed");
d->background->resizeFrame(size());
d->background->setElementPrefix("focus");
d->background->resizeFrame(size());
d->syncActiveRect();
d->background->setElementPrefix("active");
d->background->resizeFrame(d->activeRect.size());
d->background->setElementPrefix("normal");
d->background->resizeFrame(size());
}
QGraphicsProxyWidget::resizeEvent(event);
}
void ToolButton::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
if (Theme::defaultTheme()->useNativeWidgetStyle()) {
QGraphicsProxyWidget::paint(painter, option, widget);
return;
}
QToolButton *button = nativeWidget();
QStyleOptionToolButton buttonOpt;
buttonOpt.initFrom(button);
buttonOpt.icon = button->icon();
buttonOpt.text = button->text();
buttonOpt.iconSize = button->iconSize();
buttonOpt.toolButtonStyle = button->toolButtonStyle();
bool animationState = (d->animation->state() == QAbstractAnimation::Running ? true : false);
if (button->isEnabled() && (animationState || !button->autoRaise() || d->underMouse || (buttonOpt.state & QStyle::State_On) || button->isChecked() || button->isDown())) {
if (button->isDown() || (buttonOpt.state & QStyle::State_On) || button->isChecked()) {
d->background->setElementPrefix("pressed");
} else {
d->background->setElementPrefix("normal");
}
d->background->resizeFrame(size());
if (animationState) {
QPixmap buffer = d->background->framePixmap();
QPainter bufferPainter(&buffer);
bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
QColor alphaColor(Qt::black);
alphaColor.setAlphaF(qMin(qreal(0.95), d->opacity));
bufferPainter.fillRect(buffer.rect(), alphaColor);
bufferPainter.end();
painter->drawPixmap(QPoint(0,0), buffer);
buttonOpt.palette.setColor(QPalette::ButtonText, KColorUtils::mix(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor), Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor), 1-d->opacity));
} else {
d->background->paintFrame(painter);
buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
}
} else {
buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
}
buttonOpt.font = font();
painter->setFont(buttonOpt.font);
button->style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, painter, button);
}
void ToolButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
d->underMouse = true;
if (nativeWidget()->isDown() || !nativeWidget()->autoRaise()) {
return;
}
const int FadeInDuration = 75;
if (d->animation->state() != QAbstractAnimation::Stopped) {
d->animation->stop();
}
d->animation->setDuration(FadeInDuration);
d->animation->setDirection(QAbstractAnimation::Forward);
d->animation->start();
d->background->setElementPrefix("active");
QGraphicsProxyWidget::hoverEnterEvent(event);
}
void ToolButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
d->underMouse = false;
if (nativeWidget()->isDown() || !nativeWidget()->autoRaise()) {
return;
}
const int FadeOutDuration = 150;
if (d->animation->state() != QAbstractAnimation::Stopped) {
d->animation->stop();
}
d->animation->setDuration(FadeOutDuration);
d->animation->setDirection(QAbstractAnimation::Backward);
d->animation->start();
d->background->setElementPrefix("active");
QGraphicsProxyWidget::hoverLeaveEvent(event);
}
void ToolButton::changeEvent(QEvent *event)
{
d->changeEvent(event);
if (event->type() == QEvent::EnabledChange && !isEnabled()) {
d->underMouse = false;
}
QGraphicsProxyWidget::changeEvent(event);
}
QVariant ToolButton::itemChange(GraphicsItemChange change, const QVariant &value)
{
//If the widget is hidden while it's hovered and then we show it again
//we have to disable the hover otherwise it will remain hovered.
if (change == ItemVisibleHasChanged){
d->underMouse = false;
}
return QGraphicsProxyWidget::itemChange(change, value);
}
} // namespace Plasma
#include "moc_toolbutton.cpp"