API Documentation
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: Button
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/TranslatedString.h>
35#include <NDEVR/RGBColor.h>
36#include <NDEVR/TimeSpan.h>
37#include <QToolButton>
38#include <functional>
39#include <QEvent>
40#include <QPointer>
41#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
42typedef QEvent ButtonEnterEvent;
43#else
44typedef QEnterEvent ButtonEnterEvent;
45#endif
46class QStylePainter;
47class QStyleOptionToolButton;
48class QPushButton;
49namespace NDEVR
50{
51 class ButtonDialog;
52 class QTResourceListener;
53 enum class TooltipType;
54 template<class t_type>
55 class Resource;
56 template<class t_type>
58 struct GenericOption;
59 /**--------------------------------------------------------------------------------------------------
60 \brief A core widget that allows the user to click one of many button types.
61
62 Perhaps the most commonly used widget in any graphical user interface. Push (click) a button to
63 command the computer to perform some action, or to answer a question.
64 **/
65 class NDEVR_WIDGETS_API Button : public QToolButton
66 {
67 friend class HoverButtonIconWatch;
68 Q_OBJECT
69 public:
71 {
72 e_large
73 , e_medium
74 , e_medium_centered
75 , e_small
76 , e_circular
77 , e_circular_small
78 , e_push_button
80 };
81 public:
82 Button(QWidget* parent = nullptr);
83 Button(ButtonState state, QWidget* parent = nullptr);
84 Button(const TranslatedString& name, const String& icon = "", QWidget* parent = nullptr);
85 Button(const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
86 Button(const TranslatedString& name, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
87 Button(const TranslatedString& name, const String& icon, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
88 Button(const TranslatedString& name, const String& icon, TooltipType tooltip, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
90 void paintEvent(QPaintEvent *event) override;
91 void setAlignment(Qt::Alignment m_alignment);
92 void setup(const TranslatedString& name, const String& icon, const std::function<void(Button*)>& on_click);
93 void setup(const TranslatedString& name, const String& icon, TooltipType tooltip, const std::function<void(Button*)>& on_click);
94 void setup(const TranslatedString& name, const String& icon, Resource<bool>& checked_var);
95 void setup(GenericOption& checked_var);
96 void setup(ApplicationOption<bool>& checked_var);
97 void setup(const TranslatedString& name, const String& icon, ApplicationOption<bool>& checked_var);
99 int scaledIconSize() const;
101 void setToolTip(const QString& tip);
103 void showPopupMenu(QWidget* widget, bool is_popup = true);
105 void requestFixedState(bool fixed_state);
106 ButtonState buttonState() const { return m_state; }
107 void setFunction(const std::function<void(Button*)>& on_click) { m_on_click = on_click; }
108 void setHoverFunction(const std::function<void(Button*, bool hovering)>& on_hover) { m_on_hover = on_hover; }
109 void setPressFunction(const std::function<void(Button*, bool hovering)>& on_press) { m_on_press = on_press; }
110 void enterEvent(ButtonEnterEvent* e) override;
111 void leaveEvent(QEvent* e) override;
112 void setMargin(uint04 margin);
113 void setIconSize(int size);
115 bool event(QEvent* e) override;
116 void setIconSizeMultiplier(fltp04 icon_size_multiplier);
117 void setHoverIconSizeMultiplier(fltp04 icon_size_multiplier);
118 void setTitle(const TranslatedString& title);
119 void setText(const QString &text);
120 const TranslatedString& title() const;
121 const String& iconName() const;
122 void setIconName(const String& icon);
123 void setIcon(const QIcon& icon);
124 void setHoverIconName(const String& icon);
125 void setCheckedIconName(const String& icon);
126 bool eventFilter(QObject* watched, QEvent* event) override;
127 void setToolTip(const TranslatedString& tooltip);
129 void setToolTip(const TranslatedString& enabled_tooltip, const TranslatedString& disabled_tooltip);
130 void setFlat(bool is_flat);
132 void setSound(const String& sound);
133 void setIgnoreClicks(bool ignore_clicks) { m_ignore_clicks = ignore_clicks; }
134 void copyButtonFrom(Button* button);
135 void setBackgroundOpacity(fltp04 background_opacity);
136 void setMenuDialog(QWidget* widget);
137 void setIconColor(const RGBColor& color);
138 void setBackgroundColor(const RGBColor& color, bool update_foreground = true);
140 void setFocusOnPress(bool focus_on_press);
141 void setAutoDefault(bool is_auto_default);
142 void setDefault(bool is_default);
143 void setCheckable(bool checkable);
144 void setChecked(bool checked);
145 QSize sizeHint() const override;
146 QSize minimumSizeHint() const override;
147 void virtualMouseEvent(QEvent::Type event);
148 void setFontMultiplier(fltp04 font_multiplier);
149 void setBold(bool is_bold);
150 void setMenu(QMenu* menu);
151 void setClickCooldown(const TimeSpan& span);
152 QMenu* menu() const;
153 protected:
154 void mouseReleaseEvent(QMouseEvent*) override;
155 void mousePressEvent(QMouseEvent*) override;
156 protected slots:
160 private:
161 QString autoWrapText(const QFontMetrics& metrics);
162 void updateTooltip();
163 void updateText();
164 bool ignorePress() const;
165 void setup();
166 void _setIcon(const String & icon, bool force = false);
167 void updateFont();
168 void _defaultRender(QStylePainter* p, QStyleOptionToolButton* tool_button, QIcon::State state, QIcon::Mode mode, bool draw_backgound, bool is_default);
169 protected:
170 std::function<void(Button*)> m_on_click = nullptr;
171 std::function<void(Button*, bool hovering)> m_on_hover = nullptr;
172 std::function<void(Button*, bool pressing)> m_on_press = nullptr;
173 QTResourceListener* m_icon_theme_listener = nullptr;
174 ButtonDialog* m_dialog = nullptr;
175 QPointer<QWidget> m_menu_dialog = nullptr;
184 Time m_last_click_time = Time(0);
185 TimeSpan m_click_cooldown = TimeSpan(0.1);
186 fltp04 m_icon_size_multiplier = 1.0f;
188 fltp04 m_hover_icon_size_multiplier = Constant<fltp04>::Invalid;
190 fltp04 m_background_opacity = 1.0f;
191 ButtonState m_state = ButtonState::e_large;
192 bool m_ignore_clicks = false;
193 bool m_fixed_state_requested = false;
194 bool m_has_custom_background_color = false;
195 bool m_focus_on_press = true;
196 Qt::Alignment m_alignment;
198
199 QPushButton* m_push_button = nullptr;
200 bool m_is_default_button = false;
201 bool m_is_auto_default_button = false;
202 fltp04 m_font_multiplier = 1.0f;
203 bool m_is_bold = false;
204 public:
206 };
207}
QEnterEvent ButtonEnterEvent
Definition Button.h:44
#define NDEVR_WIDGETS_API
Definition DLLInfo.h:59
The default object to store data of any type that should persist through sessions of the application.
Definition Button.h:57
Typically used for dialogs which have any number of buttons associated with it that are toggled on wh...
Definition ButtonDialog.h:46
A core widget that allows the user to click one of many button types.
Definition Button.h:66
void setText(const QString &text)
void setDisabledToolTip(const TranslatedString &tooltip)
QSize minimumSizeHint() const override
void setChecked(bool checked)
Button(const TranslatedString &name, const String &icon, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
void setup(const TranslatedString &name, const String &icon, const std::function< void(Button *)> &on_click)
RGBColor m_icon_color
Definition Button.h:197
void requestFixedState(bool fixed_state)
void setFunction(const std::function< void(Button *)> &on_click)
Definition Button.h:107
void enterEvent(ButtonEnterEvent *e) override
void setHoverIconName(const String &icon)
void setMenuDialog(QWidget *widget)
QSize sizeHint() const override
void onButtonReleaseSlot()
void setSound(const String &sound)
void setIconSizeMultiplier(fltp04 icon_size_multiplier)
void setToolTip(const QString &tip)
void setToolTip(const TranslatedString &enabled_tooltip, const TranslatedString &disabled_tooltip)
TranslatedString m_enabled_tooltip
Definition Button.h:178
String m_hover_icon
Definition Button.h:181
void mousePressEvent(QMouseEvent *) override
Button(QWidget *parent=nullptr)
void setTitle(const TranslatedString &title)
Qt::Alignment m_alignment
Definition Button.h:196
void virtualMouseEvent(QEvent::Type event)
void clearBackgroundColor()
bool requestButtonState(ButtonState state)
void leaveEvent(QEvent *e) override
void setMenu(QMenu *menu)
void setIcon(const QIcon &icon)
TranslatedString m_title
Definition Button.h:176
void setBackgroundOpacity(fltp04 background_opacity)
Button(ButtonState state, QWidget *parent=nullptr)
void setToolTip(TooltipType type)
String m_icon
Definition Button.h:179
void setFlat(bool is_flat)
void setToolTip(const TranslatedString &tooltip)
void copyButtonFrom(Button *button)
int scaledIconSize() const
int styleDefaultIconSize() const
void setup(const TranslatedString &name, const String &icon, Resource< bool > &checked_var)
void setCheckable(bool checkable)
void setIconName(const String &icon)
bool eventFilter(QObject *watched, QEvent *event) override
void setPressFunction(const std::function< void(Button *, bool hovering)> &on_press)
Definition Button.h:109
void setIgnoreClicks(bool ignore_clicks)
Definition Button.h:133
Button(const TranslatedString &name, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
Button(const TranslatedString &name, const String &icon, TooltipType tooltip, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
String m_checked_icon
Definition Button.h:182
bool event(QEvent *e) override
void setHoverFunction(const std::function< void(Button *, bool hovering)> &on_hover)
Definition Button.h:108
void onButtonPressSlot()
ButtonState buttonState() const
Definition Button.h:106
void setAlignment(Qt::Alignment m_alignment)
void setDefault(bool is_default)
void setButtonDialog(ButtonDialog *dialog)
ButtonState
Definition Button.h:71
void setup(const TranslatedString &name, const String &icon, ApplicationOption< bool > &checked_var)
void setClickCooldown(const TimeSpan &span)
void paintEvent(QPaintEvent *event) override
Button(const TranslatedString &name, const String &icon="", QWidget *parent=nullptr)
void setup(ApplicationOption< bool > &checked_var)
TranslatedString m_disabled_tooltip
Definition Button.h:177
void setIconSize(int size)
void setup(GenericOption &checked_var)
const TranslatedString & title() const
void setIconColor(const RGBColor &color)
static String s_default_sound
Definition Button.h:205
void setHoverIconSizeMultiplier(fltp04 icon_size_multiplier)
void setFocusOnPress(bool focus_on_press)
void mouseReleaseEvent(QMouseEvent *) override
Button(const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
String m_current_icon
Definition Button.h:180
const String & iconName() const
void setProgressPercent(fltp04 progress)
QMenu * menu() const
void setBackgroundColor(const RGBColor &color, bool update_foreground=true)
String m_sound
Definition Button.h:183
void setMargin(uint04 margin)
void setCheckedIconName(const String &icon)
void showPopupMenu(QWidget *widget, bool is_popup=true)
void setButtonState(ButtonState state)
void setBold(bool is_bold)
void setFontMultiplier(fltp04 font_multiplier)
void setAutoDefault(bool is_auto_default)
void setup(const TranslatedString &name, const String &icon, TooltipType tooltip, const std::function< void(Button *)> &on_click)
void onButtonClickSlot()
Definition QTTools.h:168
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:54
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Toggle.h:41
The core String class for the NDEVR API.
Definition String.h:69
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
Definition ACIColor.h:37
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
TooltipType
Enums for program-generated tooltips.
Definition TooltipTextManager.h:44
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Stores a generic option of any type as well as some information about how the user might interact wit...
Definition GenericOptions.h:57