matrixion/src/ui/FlatButton.h

184 lines
5.8 KiB
C
Raw Normal View History

#pragma once
2017-04-06 02:06:42 +03:00
#include <QPushButton>
#include <QStateMachine>
#include "Theme.h"
2017-10-28 15:46:39 +03:00
class RippleOverlay;
2017-04-06 02:06:42 +03:00
class FlatButton;
class FlatButtonStateMachine : public QStateMachine
{
2017-09-10 12:59:21 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Q_PROPERTY(qreal overlayOpacity WRITE setOverlayOpacity READ overlayOpacity)
Q_PROPERTY(
qreal checkedOverlayProgress WRITE setCheckedOverlayProgress READ checkedOverlayProgress)
2017-04-06 02:06:42 +03:00
public:
2017-09-10 12:59:21 +03:00
explicit FlatButtonStateMachine(FlatButton *parent);
~FlatButtonStateMachine();
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
void setOverlayOpacity(qreal opacity);
void setCheckedOverlayProgress(qreal opacity);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
inline qreal overlayOpacity() const;
inline qreal checkedOverlayProgress() const;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
void startAnimations();
void setupProperties();
void updateCheckedStatus();
2017-04-06 02:06:42 +03:00
signals:
2017-09-10 12:59:21 +03:00
void buttonPressed();
void buttonChecked();
void buttonUnchecked();
2017-04-06 02:06:42 +03:00
protected:
2017-09-10 12:59:21 +03:00
bool eventFilter(QObject *watched, QEvent *event) override;
2017-04-06 02:06:42 +03:00
private:
2017-09-10 12:59:21 +03:00
void addTransition(QObject *object, const char *signal, QState *fromState, QState *toState);
void addTransition(QObject *object,
QEvent::Type eventType,
QState *fromState,
QState *toState);
void addTransition(QAbstractTransition *transition, QState *fromState, QState *toState);
FlatButton *const button_;
QState *const top_level_state_;
QState *const config_state_;
QState *const checkable_state_;
QState *const checked_state_;
QState *const unchecked_state_;
QState *const neutral_state_;
QState *const neutral_focused_state_;
QState *const hovered_state_;
QState *const hovered_focused_state_;
QState *const pressed_state_;
qreal overlay_opacity_;
qreal checked_overlay_progress_;
bool was_checked_;
2017-04-06 02:06:42 +03:00
};
2017-08-20 13:47:22 +03:00
inline qreal
FlatButtonStateMachine::overlayOpacity() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return overlay_opacity_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline qreal
FlatButtonStateMachine::checkedOverlayProgress() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return checked_overlay_progress_;
2017-04-06 02:06:42 +03:00
}
class FlatButton : public QPushButton
{
2017-09-10 12:59:21 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Q_PROPERTY(QColor foregroundColor WRITE setForegroundColor READ foregroundColor)
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
Q_PROPERTY(QColor overlayColor WRITE setOverlayColor READ overlayColor)
Q_PROPERTY(QColor disabledForegroundColor WRITE setDisabledForegroundColor READ
disabledForegroundColor)
Q_PROPERTY(QColor disabledBackgroundColor WRITE setDisabledBackgroundColor READ
disabledBackgroundColor)
Q_PROPERTY(qreal fontSize WRITE setFontSize READ fontSize)
2017-04-06 02:06:42 +03:00
public:
2017-09-10 12:59:21 +03:00
explicit FlatButton(QWidget *parent = 0,
ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
explicit FlatButton(const QString &text,
QWidget *parent = 0,
ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
FlatButton(const QString &text,
ui::Role role,
QWidget *parent = 0,
ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
~FlatButton();
void applyPreset(ui::ButtonPreset preset);
void setBackgroundColor(const QColor &color);
void setBackgroundMode(Qt::BGMode mode);
void setBaseOpacity(qreal opacity);
void setCheckable(bool value);
void setCornerRadius(qreal radius);
void setDisabledBackgroundColor(const QColor &color);
void setDisabledForegroundColor(const QColor &color);
void setFixedRippleRadius(qreal radius);
void setFontSize(qreal size);
void setForegroundColor(const QColor &color);
void setHasFixedRippleRadius(bool value);
void setIconPlacement(ui::ButtonIconPlacement placement);
void setOverlayColor(const QColor &color);
void setOverlayStyle(ui::OverlayStyle style);
void setRippleStyle(ui::RippleStyle style);
void setRole(ui::Role role);
QColor foregroundColor() const;
QColor backgroundColor() const;
QColor overlayColor() const;
QColor disabledForegroundColor() const;
QColor disabledBackgroundColor() const;
qreal fontSize() const;
qreal cornerRadius() const;
qreal baseOpacity() const;
bool hasFixedRippleRadius() const;
ui::Role role() const;
ui::OverlayStyle overlayStyle() const;
ui::RippleStyle rippleStyle() const;
ui::ButtonIconPlacement iconPlacement() const;
Qt::BGMode backgroundMode() const;
QSize sizeHint() const override;
2017-04-06 02:06:42 +03:00
protected:
2017-09-10 12:59:21 +03:00
int IconPadding = 0;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
void checkStateSet() override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void paintEvent(QPaintEvent *event) override;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
virtual void paintBackground(QPainter *painter);
virtual void paintForeground(QPainter *painter);
virtual void updateClipPath();
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
void init();
2017-04-06 02:06:42 +03:00
private:
2017-09-10 12:59:21 +03:00
RippleOverlay *ripple_overlay_;
FlatButtonStateMachine *state_machine_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
ui::Role role_;
ui::RippleStyle ripple_style_;
ui::ButtonIconPlacement icon_placement_;
ui::OverlayStyle overlay_style_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Qt::BGMode bg_mode_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QColor background_color_;
QColor foreground_color_;
QColor overlay_color_;
QColor disabled_color_;
QColor disabled_background_color_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
qreal fixed_ripple_radius_;
qreal corner_radius_;
qreal base_opacity_;
qreal font_size_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
bool use_fixed_ripple_radius_;
2017-04-06 02:06:42 +03:00
};