Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++
2.2 KB
Raw Blame
/*
 *  SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "KisStretchedIconButton.h"

#include <QIcon>
#include <QLabel>
#include <QHBoxLayout>
#include <QAction>
#include <QWindow>

#include <kis_debug.h>

KisStretchedIconButton::KisStretchedIconButton(QWidget *parent)
    : QToolButton(parent),
      m_label(new QLabel(this))
{
    m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(m_label);
    setLayout(layout);
    slotActionChanged();
}

KisStretchedIconButton::~KisStretchedIconButton()
{
}

void KisStretchedIconButton::setStretchedIcon(const QIcon &icon)
{
    m_stretchedIcon = icon;
    updateLabelIcon();
}

QIcon KisStretchedIconButton::stretchedIcon() const
{
    return m_stretchedIcon;
}

void KisStretchedIconButton::setAssociatedAction(QAction *action)
{
    m_actionConnections.clear();
    m_action = action;
    if (action) {
        m_actionConnections.addConnection(action, &QAction::changed,
                                          this, &KisStretchedIconButton::slotActionChanged);
        m_actionConnections.addConnection(this, &KisStretchedIconButton::clicked,
                                          action, &QAction::trigger);
    }
    slotActionChanged();
}

QAction *KisStretchedIconButton::associatedAction() const
{
    return m_action;
}

void KisStretchedIconButton::resizeEvent(QResizeEvent *event)
{
    QToolButton::resizeEvent(event);
    updateLabelIcon();
}

void KisStretchedIconButton::updateLabelIcon()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    m_label->setPixmap(m_stretchedIcon.pixmap(this->windowHandle(), m_label->size()));
#else
    m_label->setPixmap(m_stretchedIcon.pixmap(m_label->size(), this->window()->devicePixelRatio()));
#endif
}

void KisStretchedIconButton::slotActionChanged()
{
    setStretchedIcon(m_action ? m_action->icon() : QIcon());
    setText("");
    setToolTip(m_action ? m_action->toolTip() : "");
    setStatusTip(m_action ? m_action->statusTip() : "");
    setWhatsThis(m_action ? m_action->whatsThis() : "");
    setEnabled(m_action && m_action->isEnabled());
    setVisible(m_action && m_action->isVisible());
}