File indexing completed on 2022-12-06 18:50:32

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "worksheettoolbutton.h"
0007 #include <QPainter>
0008 #include <QGraphicsSceneMouseEvent>
0009 #include <QCursor>
0010 
0011 WorksheetToolButton::WorksheetToolButton(QGraphicsItem* parent)
0012     : QGraphicsObject(parent)
0013 {
0014     m_size = QSize(16, 16);
0015     setCursor(QCursor(Qt::ArrowCursor));
0016     m_scale = 0;
0017 }
0018 
0019 void WorksheetToolButton::setIcon(const QIcon& icon)
0020 {
0021     m_icon = icon;
0022 }
0023 
0024 qreal WorksheetToolButton::width()
0025 {
0026     return m_size.width();
0027 }
0028 
0029 qreal WorksheetToolButton::height()
0030 {
0031     return m_size.height();
0032 }
0033 
0034 QRectF WorksheetToolButton::boundingRect() const
0035 {
0036     return QRectF(0, 0, m_size.width(), m_size.height());
0037 }
0038 
0039 void WorksheetToolButton::setIconScale(qreal scale)
0040 {
0041     m_scale = scale;
0042     m_pixmap = m_icon.pixmap(m_size * m_scale);
0043 }
0044 
0045 void WorksheetToolButton::paint(QPainter* painter,
0046                                 const QStyleOptionGraphicsItem* option,
0047                                 QWidget* widget)
0048 {
0049     Q_UNUSED(option);
0050     Q_UNUSED(widget);
0051     if (m_scale == 0)
0052         setIconScale(1);
0053     QRectF rect(QPointF(0,0), m_size);
0054     painter->drawPixmap(rect, m_pixmap, m_pixmap.rect());
0055 }
0056 
0057 void WorksheetToolButton::mousePressEvent(QGraphicsSceneMouseEvent* event)
0058 {
0059     Q_UNUSED(event);
0060 
0061     emit pressed();
0062 }
0063 
0064 void WorksheetToolButton::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
0065 {
0066     if (boundingRect().contains(event->pos()))
0067         emit clicked();
0068 }