gqtbuttonswidget
Loading...
Searching...
No Matches
gQtButtonsWidget.h
Go to the documentation of this file.
1#pragma once
2
3// C++
4#include <string>
5#include <vector>
6
7// qt
8#include <QWidget>
9#include <QListWidget>
10#include <QPushButton>
11#include <QVector>
12#include <QIcon>
13#include <QPalette>
14#include <QSize>
15#include <QEvent>
16#include <QShowEvent>
17
37{
44 explicit ButtonInfo(const std::string& icon);
45
55 QListWidgetItem* thisButton;
56
77 QIcon iconForState(int state, const QSize& iconSize, const QPalette& palette) const;
78
79private:
80 std::string buttonName;
81};
82
100class GQTButtonsWidget : public QWidget
101{
102 Q_OBJECT
103
104public:
121 explicit GQTButtonsWidget(double h, double v,
122 const std::vector<std::string>& bicons,
123 bool vertical = true,
124 QWidget* parent = nullptr);
125
126 ~GQTButtonsWidget() override = default;
127
138 QListWidget* buttonsWidget;
139
149 int button_pressed() const { return buttonsWidget->currentRow(); }
150
160 void press_button(int i);
161
169 void reset_buttons();
170
171private:
179 void refresh_icons();
180
181protected:
182 void changeEvent(QEvent* event) override;
183 void showEvent(QShowEvent* event) override;
184
185private:
192 std::vector<ButtonInfo*> buttons;
193
194private slots:
205 void buttonWasPressed(QListWidgetItem* item);
206};
207
221class GQTToggleButtonWidget : public QWidget
222{
223 Q_OBJECT
224
225public:
236 explicit GQTToggleButtonWidget(int buttonWidth, int buttonHeight, int borderRadius,
237 const std::vector<std::string>& titles,
238 bool vertical = true,
239 QWidget* parent = nullptr);
240
249 int buttonPressed() const { return buttonPressedIndex; }
250
259 bool lastButtonState() const;
260
266 void toggleButton(int index);
267
274 bool buttonStatus(int index) const;
275
283 void reset_buttons();
284
296 void setSvgButtonIcon(int index, const QString& svgResourcePath, const QSize& iconSize = QSize());
297
298signals:
305
306protected:
307 void changeEvent(QEvent* event) override;
308 void showEvent(QShowEvent* event) override;
309
310private:
311 struct SvgIcon { QString path; QSize size; };
312
313 int buttonPressedIndex;
314 QVector<QPushButton*> buttons;
315 QVector<SvgIcon> svgIcons;
316
317 void refresh_svg_icons();
318
319private slots:
325 void setButtonPressed(int index);
326};
void changeEvent(QEvent *event) override
void reset_buttons()
Reset all buttons to the "normal" icon state.
void showEvent(QShowEvent *event) override
QListWidget * buttonsWidget
Underlying QListWidget used to render the icons.
void press_button(int i)
Programmatically press a button.
GQTButtonsWidget(double h, double v, const std::vector< std::string > &bicons, bool vertical=true, QWidget *parent=nullptr)
Constructs a GQTButtonsWidget.
int button_pressed() const
Returns the index of the currently selected (pressed) button.
~GQTButtonsWidget() override=default
void toggleButton(int index)
Toggle the checked state of a button.
void setSvgButtonIcon(int index, const QString &svgResourcePath, const QSize &iconSize=QSize())
Assign an SVG resource to a button so it re-renders on every palette/theme change.
void showEvent(QShowEvent *event) override
GQTToggleButtonWidget(int buttonWidth, int buttonHeight, int borderRadius, const std::vector< std::string > &titles, bool vertical=true, QWidget *parent=nullptr)
Constructs a GQTToggleButtonWidget.
void changeEvent(QEvent *event) override
void reset_buttons()
Uncheck all buttons.
int buttonPressed() const
Returns the last pressed button index.
void buttonPressedIndexChanged(int index)
Emitted whenever the last pressed button index changes.
bool buttonStatus(int index) const
Returns the checked state for a specific button.
bool lastButtonState() const
Returns the checked state of the last pressed button.
event
QIcon iconForState(int state, const QSize &iconSize, const QPalette &palette) const
Returns the icon for a given interaction state.
QListWidgetItem * thisButton
The UI item representing this logical button.
ButtonInfo(const std::string &icon)
Constructs a ButtonInfo for a given base icon name.