File: iconbutton.h

package info (click to toggle)
martchus-qtutilities 6.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 780 kB
  • sloc: cpp: 5,908; xml: 37; sh: 25; ansic: 12; makefile: 12
file content (61 lines) | stat: -rw-r--r-- 1,314 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef WIDGETS_ICONBUTTON_H
#define WIDGETS_ICONBUTTON_H

#include "../global.h"

#include <QAbstractButton>
#include <QAction>
#include <QPixmap>
#include <QSize>

#include <cstdint>

namespace QtUtilities {

class QT_UTILITIES_EXPORT IconButton : public QAbstractButton {
    Q_OBJECT
    Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)

public:
    explicit IconButton(QWidget *parent = nullptr);
    ~IconButton() override;

    static IconButton *fromAction(QAction *action, std::uintptr_t id = 0);
    const QPixmap &pixmap() const;
    void setPixmap(const QPixmap &pixmap);
    QSize sizeHint() const override;

    static constexpr auto defaultPixmapSize = QSize(16, 16);

protected:
    void paintEvent(QPaintEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;

private Q_SLOTS:
    void assignDataFromActionChangedSignal();
    void assignDataFromAction(const QAction *action);

private:
    QPixmap m_pixmap;
};

/*!
 * \brief Returns the pixmap.
 */
inline const QPixmap &IconButton::pixmap() const
{
    return m_pixmap;
}

/*!
 * \brief Sets the pixmap.
 */
inline void IconButton::setPixmap(const QPixmap &pixmap)
{
    m_pixmap = pixmap;
    update();
}
} // namespace QtUtilities

#endif // WIDGETS_ICONBUTTON_H