NDEVR
API Documentation
Button.h
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;
53 enum class TooltipType;
54 template<class t_type>
55 class Resource;
56 template<class t_type>
58 struct GenericOption;
67 class NDEVR_WIDGETS_API Button : public QToolButton
68 {
69 friend class HoverButtonIconWatch;
70 Q_OBJECT
71 public:
86 public:
91 Button(QWidget* parent = nullptr);
97 Button(ButtonState state, QWidget* parent = nullptr);
104 Button(const TranslatedString& name, const StringView& icon = StringView(), QWidget* parent = nullptr);
110 Button(const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
117 Button(const TranslatedString& name, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
125 Button(const TranslatedString& name, const StringView& icon, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
134 Button(const TranslatedString& name, const StringView& icon, TooltipType tooltip, const std::function<void(Button*)>& on_click, QWidget* parent = nullptr);
143 void paintEvent(QPaintEvent *event) override;
148 void setAlignment(Qt::Alignment m_alignment);
155 void setup(const TranslatedString& name, const StringView& icon, const std::function<void(Button*)>& on_click);
163 void setup(const TranslatedString& name, const StringView& icon, TooltipType tooltip, const std::function<void(Button*)>& on_click);
170 void setup(const TranslatedString& name, const StringView& icon, Resource<bool>& checked_var);
175 void setup(GenericOption& checked_var);
180 void setup(ApplicationOption<bool>& checked_var);
187 void setup(const TranslatedString& name, const StringView& icon, ApplicationOption<bool>& checked_var);
197 int scaledIconSize() const;
207 void setToolTip(const QString& tip);
212 void setToolTip(const TranslatedString& tooltip);
218 void setToolTip(const TranslatedString& enabled_tooltip, const TranslatedString& disabled_tooltip);
233 void setLogOutput(bool log_output);
239 void showPopupMenu(QWidget* widget, bool is_popup = true);
250 void requestFixedState(bool fixed_state);
255 ButtonState buttonState() const { return m_state; }
260 void setFunction(const std::function<void(Button*)>& on_click) { m_on_click = on_click; }
265 void setHoverFunction(const std::function<void(Button*, bool hovering)>& on_hover) { m_on_hover = on_hover; }
270 void setPressFunction(const std::function<void(Button*, bool hovering)>& on_press) { m_on_press = on_press; }
275 void enterEvent(ButtonEnterEvent* e) override;
280 void leaveEvent(QEvent* e) override;
285 void setMargin(uint04 margin);
290 void setIconSize(int size);
301 bool event(QEvent* e) override;
306 void setIconSizeMultiplier(fltp04 icon_size_multiplier);
311 void setHoverIconSizeMultiplier(fltp04 icon_size_multiplier);
321 void setText(const QString &text);
326 const TranslatedString& title() const;
331 const String& iconName() const;
336 void setIconName(const StringView& icon);
341 void setIcon(const QIcon& icon);
346 void setHoverIconName(const StringView& icon);
358 bool eventFilter(QObject* watched, QEvent* event) override;
363 void setFlat(bool is_flat);
373 void setSound(const StringView& sound);
378 void setIgnoreClicks(bool ignore_clicks) { m_ignore_clicks = ignore_clicks; }
383 void copyButtonFrom(Button* button);
388 void setBackgroundOpacity(fltp04 background_opacity);
393 void setMenuDialog(QWidget* widget);
398 void setIconColor(const RGBColor& color);
404 void setBackgroundColor(const RGBColor& color, bool update_foreground = true);
414 void setForErrorOrSuccess(bool is_error, bool is_success);
419 void setFocusOnPress(bool focus_on_press);
424 void setAutoDefault(bool is_auto_default);
429 void setDefault(bool is_default);
434 void setCheckable(bool checkable);
439 void setChecked(bool checked);
444 QSize sizeHint() const override;
449 QSize minimumSizeHint() const override;
454 void virtualMouseEvent(QEvent::Type event);
459 void setFontMultiplier(fltp04 font_multiplier);
464 void setBold(bool is_bold);
469 void setMenu(QMenu* menu);
473 void showToolTip() const;
478 void setClickCooldown(const TimeSpan& span);
483 QMenu* menu() const;
484 protected:
488 void mouseReleaseEvent(QMouseEvent*) override;
492 void mousePressEvent(QMouseEvent*) override;
493 protected slots:
506 private:
512 QString autoWrapText(const QFontMetrics& metrics);
517 String getTooltip() const;
521 void updateText();
526 bool ignorePress() const;
530 void setup();
536 void _setIcon(const StringView& icon, bool force = false);
540 void updateFont();
550 void _defaultRender(QStylePainter* p, QStyleOptionToolButton* tool_button, QIcon::State state, QIcon::Mode mode, bool draw_backgound, bool is_default);
551 protected:
552 std::function<void(Button*)> m_on_click = nullptr;
553 std::function<void(Button*, bool hovering)> m_on_hover = nullptr;
554 std::function<void(Button*, bool pressing)> m_on_press = nullptr;
557 QPointer<QWidget> m_menu_dialog = nullptr;
569 uint04 m_margin = Constant<uint04>::Invalid;
570 fltp04 m_hover_icon_size_multiplier = Constant<fltp04>::Invalid;
571 fltp04 m_progress = Constant<fltp04>::Invalid;
574 bool m_ignore_clicks = false;
577 bool m_focus_on_press = true;
578 Qt::Alignment m_alignment;
580
581 QPushButton* m_push_button = nullptr;
582 bool m_is_default_button = false;
585 bool m_is_bold = false;
586 bool m_log_output = true;
587 public:
589 };
590}
The default object to store data of any type that should persist through sessions of the application.
Typically used for dialogs which have any number of buttons associated with it that are toggled on wh...
void setup(const TranslatedString &name, const StringView &icon, const std::function< void(Button *)> &on_click)
Sets up the button with a name, icon, and click callback.
std::function< void(Button *)> m_on_click
Callback invoked on button click.
Definition Button.h:552
const TranslatedString & title() const
Returns the translated title of the button.
void setHoverFunction(const std::function< void(Button *, bool hovering)> &on_hover)
Sets a callback function invoked when the mouse enters or leaves the button.
Definition Button.h:265
void enterEvent(ButtonEnterEvent *e) override
Handles the mouse enter event.
bool m_is_bold
Whether the button text is bold.
Definition Button.h:585
void onButtonClickSlot()
Slot invoked when the button is clicked, executing the click callback.
void setHoverIconSizeMultiplier(fltp04 icon_size_multiplier)
Sets a multiplier for the icon size when the button is hovered.
void setIgnoreClicks(bool ignore_clicks)
Sets whether click events should be ignored.
Definition Button.h:378
void onButtonPressSlot()
Slot invoked when the button is pressed down.
void setFunction(const std::function< void(Button *)> &on_click)
Sets the callback function invoked when the button is clicked.
Definition Button.h:260
void setIconColor(const RGBColor &color)
Sets a custom color for the button icon.
bool m_is_auto_default_button
Whether this is an auto-default button in a dialog.
Definition Button.h:583
bool event(QEvent *e) override
Processes a generic event, handling theme and style changes.
void setToolTip(TooltipType type)
Sets the tooltip from a predefined tooltip type.
void setSound(const StringView &sound)
Sets the sound played when the button is clicked.
void mousePressEvent(QMouseEvent *) override
Handles the mouse press event.
void setup(const TranslatedString &name, const StringView &icon, TooltipType tooltip, const std::function< void(Button *)> &on_click)
Sets up the button with a name, icon, tooltip type, and click callback.
void setClickCooldown(const TimeSpan &span)
Sets the minimum time between successive click events.
ButtonState buttonState() const
Returns the current visual state of the button.
Definition Button.h:255
Button(const TranslatedString &name, const StringView &icon, TooltipType tooltip, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
Constructs a Button with a title, icon, tooltip type, and click callback.
Time m_last_click_time
Timestamp of the most recent click event.
Definition Button.h:566
Button(const TranslatedString &name, const StringView &icon, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
Constructs a Button with a title, icon, and click callback.
void showToolTip() const
Programmatically shows the button's tooltip at the button's position.
void setChecked(bool checked)
Sets the checked state of the button.
void setAutoDefault(bool is_auto_default)
Sets whether the button is an auto-default button in dialogs.
std::function< void(Button *, bool hovering)> m_on_hover
Callback invoked on hover state change.
Definition Button.h:553
String m_checked_icon
Icon resource name displayed when checked.
Definition Button.h:564
Button(ButtonState state, QWidget *parent=nullptr)
Constructs a Button with a specified visual state.
Button(QWidget *parent=nullptr)
Constructs a Button with an optional parent widget.
void setToolTip(const QString &tip)
Sets the tooltip text from a QString.
String m_sound
Sound resource name played on click.
Definition Button.h:565
~Button()
Destroys the Button and releases resources.
void paintEvent(QPaintEvent *event) override
Handles the paint event, rendering the button with its current style and state.
void setPressFunction(const std::function< void(Button *, bool hovering)> &on_press)
Sets a callback function invoked when the button is pressed or released.
Definition Button.h:270
QSize minimumSizeHint() const override
Returns the minimum recommended size for the button.
void setFocusOnPress(bool focus_on_press)
Sets whether the button should receive focus when pressed.
int styleDefaultIconSize() const
Returns the default icon size as determined by the current style.
TranslatedString m_disabled_tooltip
Tooltip shown when the button is disabled.
Definition Button.h:559
void setButtonDialog(ButtonDialog *dialog)
Associates a ButtonDialog with this button for toggle-controlled dialog visibility.
void setup(ApplicationOption< bool > &checked_var)
Sets up the button as a checkable toggle bound to an ApplicationOption<bool>.
void copyButtonFrom(Button *button)
Copies all visual properties and settings from another Button.
void setMenu(QMenu *menu)
Sets a dropdown QMenu to display when the button is clicked.
void setup(const TranslatedString &name, const StringView &icon, Resource< bool > &checked_var)
Sets up the button as a checkable toggle bound to a bool Resource.
void setToolTip(const TranslatedString &enabled_tooltip, const TranslatedString &disabled_tooltip)
Sets separate tooltips for the enabled and disabled states.
bool requestButtonState(ButtonState state)
Requests that the button adopt the specified state, returning whether it was accepted.
void setIcon(const QIcon &icon)
Sets the icon directly from a QIcon object.
void setIconSize(int size)
Sets the icon size in pixels, overriding automatic scaling.
fltp04 m_hover_icon_size_multiplier
Icon size multiplier when hovered, or Invalid for default.
Definition Button.h:570
void requestFixedState(bool fixed_state)
Requests that the button maintain a fixed state, preventing automatic state changes.
String m_hover_icon
Icon resource name displayed on hover.
Definition Button.h:563
void setBold(bool is_bold)
Sets whether the button text is rendered in bold.
void setText(const QString &text)
Sets the display text of the button (Qt override).
void setFontMultiplier(fltp04 font_multiplier)
Sets a multiplier for the button's font size.
void setIconSizeMultiplier(fltp04 icon_size_multiplier)
Sets a multiplier for the default icon size.
bool m_fixed_state_requested
Whether the button state has been locked.
Definition Button.h:575
QTResourceListener * m_icon_theme_listener
Listener for icon theme changes.
Definition Button.h:555
String m_current_icon
The currently displayed icon resource name.
Definition Button.h:562
Button(const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
Constructs a Button with a click callback.
void leaveEvent(QEvent *e) override
Handles the mouse leave event.
void setDefault(bool is_default)
Sets whether the button is the default button in dialogs.
void setCheckedIconName(const StringView &icon)
Sets an alternate icon displayed when the button is checked.
uint04 m_margin
Custom margin around button content, or Invalid for default.
Definition Button.h:569
void setBackgroundColor(const RGBColor &color, bool update_foreground=true)
Sets a custom background color for the button.
bool m_is_default_button
Whether this is the default button in a dialog.
Definition Button.h:582
const String & iconName() const
Returns the current icon resource name.
static String s_default_sound
The default sound played by all buttons on click.
Definition Button.h:588
void setDisabledToolTip(const TranslatedString &tooltip)
Sets the tooltip shown when the button is disabled.
std::function< void(Button *, bool pressing)> m_on_press
Callback invoked on press state change.
Definition Button.h:554
TimeSpan m_click_cooldown
Minimum duration between successive clicks.
Definition Button.h:567
void setToolTip(const TranslatedString &tooltip)
Sets the tooltip text from a TranslatedString.
void setBackgroundOpacity(fltp04 background_opacity)
Sets the opacity of the button background (0.0 = transparent, 1.0 = opaque).
fltp04 m_icon_size_multiplier
Multiplier applied to the default icon size.
Definition Button.h:568
Button(const TranslatedString &name, const std::function< void(Button *)> &on_click, QWidget *parent=nullptr)
Constructs a Button with a title and click callback.
Qt::Alignment m_alignment
Text alignment within the button.
Definition Button.h:578
RGBColor m_icon_color
Custom icon color override.
Definition Button.h:579
bool m_focus_on_press
Whether the button receives focus when pressed.
Definition Button.h:577
String m_icon
The requested icon resource name.
Definition Button.h:561
void setCheckable(bool checkable)
Sets whether the button supports a checked/unchecked toggle state.
void setButtonState(ButtonState state)
Sets the visual state/style of the button.
void setIconName(const StringView &icon)
Sets the icon by resource name, triggering an icon update.
void showPopupMenu(QWidget *widget, bool is_popup=true)
Shows a popup menu widget anchored to this button.
ButtonState m_state
The current visual state/style of the button.
Definition Button.h:573
TranslatedString m_title
The display title of the button.
Definition Button.h:558
fltp04 m_background_opacity
Opacity of the button background.
Definition Button.h:572
void setLogOutput(bool log_output)
Enables or disables logging of button click events.
bool m_has_custom_background_color
Whether a custom background color has been set.
Definition Button.h:576
void mouseReleaseEvent(QMouseEvent *) override
Handles the mouse release event for click detection.
QMenu * menu() const
Returns the dropdown menu attached to this button, if any.
void setup(const TranslatedString &name, const StringView &icon, ApplicationOption< bool > &checked_var)
Sets up the button with a name, icon, and ApplicationOption<bool> binding.
int scaledIconSize() const
Returns the icon size after applying the current size multiplier.
TranslatedString m_enabled_tooltip
Tooltip shown when the button is enabled.
Definition Button.h:560
void setMargin(uint04 margin)
Sets the margin around the button content in pixels.
void onButtonReleaseSlot()
Slot invoked when the button is released.
bool eventFilter(QObject *watched, QEvent *event) override
Filters events for watched objects, used for push button event forwarding.
bool m_log_output
Whether click events are logged.
Definition Button.h:586
QSize sizeHint() const override
Returns the recommended size for the button.
void virtualMouseEvent(QEvent::Type event)
Simulates a mouse event of the given type on the button.
void setFlat(bool is_flat)
Sets whether the button has a flat (borderless) appearance.
void setTitle(const TranslatedString &title)
Sets the display title of the button.
QPointer< QWidget > m_menu_dialog
Widget shown as a popup menu on click.
Definition Button.h:557
fltp04 m_progress
Progress overlay value (0.0-1.0), or Invalid to hide.
Definition Button.h:571
QPushButton * m_push_button
Internal QPushButton used for push button state rendering.
Definition Button.h:581
bool m_ignore_clicks
Whether to ignore click events.
Definition Button.h:574
void setMenuDialog(QWidget *widget)
Sets a widget to be shown as a popup menu when the button is clicked.
void clearBackgroundColor()
Clears any custom background color, reverting to the theme default.
ButtonState
Defines the visual size and style of the button.
Definition Button.h:76
@ e_none
No specific style applied.
Definition Button.h:84
@ e_circular_small
Small circular button.
Definition Button.h:82
@ e_circular
Circular button.
Definition Button.h:81
@ e_medium
Medium button with icon and text beside it.
Definition Button.h:78
@ e_small
Small button with icon only.
Definition Button.h:80
@ e_medium_centered
Medium button with centered icon and text.
Definition Button.h:79
@ e_push_button
Standard push button style.
Definition Button.h:83
@ e_large
Large button with icon and text below.
Definition Button.h:77
void setHoverIconName(const StringView &icon)
Sets an alternate icon displayed when the button is hovered.
Button(const TranslatedString &name, const StringView &icon=StringView(), QWidget *parent=nullptr)
Constructs a Button with a title and optional icon.
void setProgressPercent(fltp04 progress)
Sets a progress percentage to display as an overlay on the button.
void setup(GenericOption &checked_var)
Sets up the button from a GenericOption, using its name, icon, and value.
void setForErrorOrSuccess(bool is_error, bool is_success)
Sets the button's visual state to indicate an error or success condition.
fltp04 m_font_multiplier
Multiplier applied to the default font size.
Definition Button.h:584
ButtonDialog * m_dialog
Associated ButtonDialog for toggle-controlled visibility.
Definition Button.h:556
void setAlignment(Qt::Alignment m_alignment)
Sets the text alignment within the button.
A ResourceListener which will always be executed on the main UI thread and can be tied to a Qt Object...
Definition QTTools.h:493
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
TooltipType
Enums for program-generated tooltips.
Stores a generic option of any type as well as some information about how the user might interact wit...