KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
buttonlabel.h
1#ifndef BUTTONLABEL_H
2#define BUTTONLABEL_H
3
4#include <QWidget>
5#include <Kanoop/gui/libkanoopgui.h>
6
7class QHBoxLayout;
8class QToolButton;
9class QLabel;
10
11/**
12 * @brief Composite widget combining a QLabel with a QToolButton.
13 *
14 * ButtonLabel places a text label and a tool button side-by-side. The button
15 * can carry separate active and inactive icons and fires a clicked() signal.
16 * Colors and alignment are customizable.
17 */
18class LIBKANOOPGUI_EXPORT ButtonLabel : public QWidget
19{
20 Q_OBJECT
21public:
22 /**
23 * @brief Construct an empty ButtonLabel.
24 * @param parent Optional QWidget parent
25 */
26 explicit ButtonLabel(QWidget *parent = nullptr);
27
28 /**
29 * @brief Construct with initial label text.
30 * @param text Initial label text
31 * @param parent Optional QWidget parent
32 */
33 explicit ButtonLabel(const QString& text, QWidget *parent = nullptr);
34
35 /**
36 * @brief Return the current label text.
37 * @return Label text string
38 */
39 QString text() const { return _text; }
40
41 /**
42 * @brief Set the label text.
43 * @param text New label text
44 */
45 void setText(const QString& text);
46
47 /**
48 * @brief Set the button icons for active and inactive states.
49 * @param activeIcon Icon shown when isActive() is true
50 * @param inactiveIcon Icon shown when isActive() is false (optional)
51 */
52 void setIcon(const QIcon& activeIcon, const QIcon& inactiveIcon = QIcon());
53
54 /**
55 * @brief Set the horizontal alignment of the button within the layout.
56 * @param alignment Qt alignment flags
57 */
58 void setButtonAlignment(Qt::Alignment alignment);
59
60 /**
61 * @brief Set the active state and update the displayed icon.
62 * @param active true to show the active icon
63 */
64 void setActive(bool active);
65
66 /**
67 * @brief Return whether the button is in the active state.
68 * @return true if active
69 */
70 bool isActive() const { return _active; }
71
72 /**
73 * @brief Set the foreground (text) color.
74 * @param color Foreground color
75 */
76 void setForegroundColor(const QColor& color);
77
78 /**
79 * @brief Set the background color.
80 * @param color Background color
81 */
82 void setBackgroundColor(const QColor& color);
83
84private:
85 void commonInit();
86 void makeStyleSheet();
87 void relayout();
88
89 QHBoxLayout* _layout = nullptr;
90 QLabel* _label = nullptr;
91 QToolButton* _button = nullptr;
92 QIcon _activeIcon;
93 QIcon _inactiveIcon;
94 bool _active = false;
95 Qt::Alignment _buttonAlignment = Qt::AlignRight;
96 QString _text;
97 QColor _backgroundColor;
98 QColor _foregroundColor;
99
100signals:
101 /** @brief Emitted when the tool button is clicked. */
102 void clicked();
103 /** @brief Emitted when the active state changes. */
105
106private slots:
107 void onButtonClicked();
108};
109
110#endif // BUTTONLABEL_H
Composite widget combining a QLabel with a QToolButton.
Definition buttonlabel.h:19
void setBackgroundColor(const QColor &color)
Set the background color.
bool isActive() const
Return whether the button is in the active state.
Definition buttonlabel.h:70
ButtonLabel(const QString &text, QWidget *parent=nullptr)
Construct with initial label text.
void setButtonAlignment(Qt::Alignment alignment)
Set the horizontal alignment of the button within the layout.
void setActive(bool active)
Set the active state and update the displayed icon.
void setText(const QString &text)
Set the label text.
void activeChanged()
Emitted when the active state changes.
QString text() const
Return the current label text.
Definition buttonlabel.h:39
ButtonLabel(QWidget *parent=nullptr)
Construct an empty ButtonLabel.
void clicked()
Emitted when the tool button is clicked.
void setForegroundColor(const QColor &color)
Set the foreground (text) color.
void setIcon(const QIcon &activeIcon, const QIcon &inactiveIcon=QIcon())
Set the button icons for active and inactive states.