2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-02 19:16:35 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_widgets.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
#include <rpl/event_stream.h>
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
namespace Ui {
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
class AbstractCheckView {
|
|
|
|
public:
|
2018-06-04 15:35:11 +00:00
|
|
|
AbstractCheckView(int duration, bool checked, Fn<void()> updateCallback);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
2018-09-26 11:28:16 +00:00
|
|
|
void setChecked(bool checked, anim::type animated);
|
2017-09-30 19:20:40 +00:00
|
|
|
void finishAnimating();
|
2018-06-04 15:35:11 +00:00
|
|
|
void setUpdateCallback(Fn<void()> updateCallback);
|
2017-07-07 11:16:37 +00:00
|
|
|
bool checked() const {
|
|
|
|
return _checked;
|
|
|
|
}
|
2018-04-13 10:10:09 +00:00
|
|
|
void update();
|
2019-02-19 06:57:53 +00:00
|
|
|
float64 currentAnimationValue(crl::time ms);
|
2018-09-26 11:28:16 +00:00
|
|
|
bool animating() const;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
2018-12-06 15:47:28 +00:00
|
|
|
auto checkedChanges() const {
|
|
|
|
return _checks.events();
|
|
|
|
}
|
2017-09-27 12:04:19 +00:00
|
|
|
auto checkedValue() const {
|
2017-09-13 17:01:23 +00:00
|
|
|
return _checks.events_starting_with(checked());
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
virtual QSize getSize() const = 0;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
// Zero instead of ms value means that animation was already updated for this time.
|
|
|
|
// It can be passed to currentAnimationValue() safely.
|
2019-02-19 06:57:53 +00:00
|
|
|
virtual void paint(Painter &p, int left, int top, int outerWidth, crl::time ms) = 0;
|
2017-07-09 15:06:27 +00:00
|
|
|
virtual QImage prepareRippleMask() const = 0;
|
|
|
|
virtual bool checkRippleStartPosition(QPoint position) const = 0;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
void paint(Painter &p, int left, int top, int outerWidth) {
|
|
|
|
// Pass zero in ms if the animation was already updated for this time.
|
|
|
|
paint(p, left, top, outerWidth, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~AbstractCheckView() = default;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
private:
|
2018-09-26 11:28:16 +00:00
|
|
|
virtual void checkedChangedHook(anim::type animated) {
|
|
|
|
}
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
int _duration = 0;
|
|
|
|
bool _checked = false;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> _updateCallback;
|
2017-07-07 11:16:37 +00:00
|
|
|
Animation _toggleAnimation;
|
|
|
|
|
2017-09-13 17:01:23 +00:00
|
|
|
rpl::event_stream<bool> _checks;
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CheckView : public AbstractCheckView {
|
2015-10-06 19:49:23 +00:00
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
CheckView(
|
|
|
|
const style::Check &st,
|
|
|
|
bool checked,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
void setStyle(const style::Check &st);
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize getSize() const override;
|
2019-02-19 06:57:53 +00:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth, crl::time ms) override;
|
2017-07-09 15:06:27 +00:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
2018-04-13 10:10:09 +00:00
|
|
|
void setUntoggledOverride(
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<QColor> untoggledOverride);
|
2018-04-13 10:10:09 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
private:
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<const style::Check*> _st;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<QColor> _untoggledOverride;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class RadioView : public AbstractCheckView {
|
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
RadioView(
|
|
|
|
const style::Radio &st,
|
|
|
|
bool checked,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
void setStyle(const style::Radio &st);
|
|
|
|
|
2018-09-26 11:28:16 +00:00
|
|
|
void setToggledOverride(std::optional<QColor> toggledOverride);
|
2018-09-21 16:28:46 +00:00
|
|
|
void setUntoggledOverride(std::optional<QColor> untoggledOverride);
|
2018-04-13 10:10:09 +00:00
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize getSize() const override;
|
2019-02-19 06:57:53 +00:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth, crl::time ms) override;
|
2017-07-09 15:06:27 +00:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<const style::Radio*> _st;
|
2018-09-26 11:28:16 +00:00
|
|
|
std::optional<QColor> _toggledOverride;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<QColor> _untoggledOverride;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ToggleView : public AbstractCheckView {
|
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
ToggleView(
|
|
|
|
const style::Toggle &st,
|
|
|
|
bool checked,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
void setStyle(const style::Toggle &st);
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize getSize() const override;
|
2019-02-19 06:57:53 +00:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth, crl::time ms) override;
|
2017-07-09 15:06:27 +00:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2019-01-21 10:22:18 +00:00
|
|
|
void setLocked(bool locked);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-09 15:06:27 +00:00
|
|
|
void paintXV(Painter &p, int left, int top, int outerWidth, float64 toggled, const QBrush &brush);
|
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<const style::Toggle*> _st;
|
2019-01-21 10:22:18 +00:00
|
|
|
bool _locked = false;
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Checkbox : public RippleButton {
|
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
Checkbox(
|
|
|
|
QWidget *parent,
|
|
|
|
const QString &text,
|
|
|
|
bool checked = false,
|
|
|
|
const style::Checkbox &st = st::defaultCheckbox,
|
|
|
|
const style::Check &checkSt = st::defaultCheck);
|
|
|
|
Checkbox(
|
|
|
|
QWidget *parent,
|
|
|
|
const QString &text,
|
|
|
|
bool checked,
|
|
|
|
const style::Checkbox &st,
|
|
|
|
const style::Toggle &toggleSt);
|
|
|
|
Checkbox(
|
|
|
|
QWidget *parent,
|
|
|
|
const QString &text,
|
|
|
|
const style::Checkbox &st,
|
|
|
|
std::unique_ptr<AbstractCheckView> check);
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
void setText(const QString &text);
|
2017-09-21 19:21:33 +00:00
|
|
|
void setCheckAlignment(style::align alignment);
|
2017-04-18 17:37:14 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
bool checked() const;
|
2018-12-05 09:55:56 +00:00
|
|
|
rpl::producer<bool> checkedChanges() const;
|
|
|
|
rpl::producer<bool> checkedValue() const;
|
2016-06-01 13:07:03 +00:00
|
|
|
enum class NotifyAboutChange {
|
|
|
|
Notify,
|
|
|
|
DontNotify,
|
|
|
|
};
|
2018-12-05 09:55:56 +00:00
|
|
|
void setChecked(
|
|
|
|
bool checked,
|
|
|
|
NotifyAboutChange notify = NotifyAboutChange::Notify);
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-09-30 19:20:40 +00:00
|
|
|
void finishAnimating();
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-12-02 19:16:35 +00:00
|
|
|
QMargins getMargins() const override {
|
|
|
|
return _st.margin;
|
|
|
|
}
|
2016-08-22 17:16:21 +00:00
|
|
|
int naturalWidth() const override;
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
void updateCheck() {
|
|
|
|
rtlupdate(checkRect());
|
|
|
|
}
|
|
|
|
QRect checkRect() const;
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void onStateChanged(State was, StateChangeSource source) override;
|
2016-12-02 19:16:35 +00:00
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
QPoint prepareRippleStartPosition() const override;
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2017-07-18 17:11:44 +00:00
|
|
|
virtual void handlePress();
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
private:
|
2017-04-18 17:37:14 +00:00
|
|
|
void resizeToText();
|
2017-07-14 09:47:17 +00:00
|
|
|
QPixmap grabCheckCache() const;
|
2017-04-18 17:37:14 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
const style::Checkbox &_st;
|
2017-07-07 11:16:37 +00:00
|
|
|
std::unique_ptr<AbstractCheckView> _check;
|
2018-12-05 09:55:56 +00:00
|
|
|
rpl::event_stream<bool> _checkedChanges;
|
2017-07-14 09:47:17 +00:00
|
|
|
QPixmap _checkCache;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
Text _text;
|
2017-09-21 19:21:33 +00:00
|
|
|
style::align _checkAlignment = style::al_left;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
class Radiobutton;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
class RadiobuttonGroup {
|
2015-10-06 19:49:23 +00:00
|
|
|
public:
|
2017-03-18 11:55:04 +00:00
|
|
|
RadiobuttonGroup() = default;
|
|
|
|
RadiobuttonGroup(int value) : _value(value), _hasValue(true) {
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setChangedCallback(Fn<void(int value)> callback) {
|
2017-03-18 11:55:04 +00:00
|
|
|
_changedCallback = std::move(callback);
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
bool hasValue() const {
|
|
|
|
return _hasValue;
|
|
|
|
}
|
|
|
|
int value() const {
|
2015-10-06 19:49:23 +00:00
|
|
|
return _value;
|
|
|
|
}
|
2017-03-18 11:55:04 +00:00
|
|
|
void setValue(int value);
|
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
private:
|
|
|
|
friend class Radiobutton;
|
2017-03-18 11:55:04 +00:00
|
|
|
void registerButton(Radiobutton *button) {
|
|
|
|
if (!base::contains(_buttons, button)) {
|
|
|
|
_buttons.push_back(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void unregisterButton(Radiobutton *button) {
|
|
|
|
_buttons.erase(std::remove(_buttons.begin(), _buttons.end(), button), _buttons.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
int _value = 0;
|
|
|
|
bool _hasValue = false;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(int value)> _changedCallback;
|
2017-03-18 11:55:04 +00:00
|
|
|
std::vector<Radiobutton*> _buttons;
|
|
|
|
|
|
|
|
};
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2018-12-05 09:55:56 +00:00
|
|
|
class Radiobutton : public Checkbox {
|
2017-03-18 11:55:04 +00:00
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
Radiobutton(
|
|
|
|
QWidget *parent,
|
|
|
|
const std::shared_ptr<RadiobuttonGroup> &group,
|
|
|
|
int value,
|
|
|
|
const QString &text,
|
|
|
|
const style::Checkbox &st = st::defaultCheckbox,
|
|
|
|
const style::Radio &radioSt = st::defaultRadio);
|
|
|
|
Radiobutton(
|
|
|
|
QWidget *parent,
|
|
|
|
const std::shared_ptr<RadiobuttonGroup> &group,
|
|
|
|
int value,
|
|
|
|
const QString &text,
|
|
|
|
const style::Checkbox &st,
|
|
|
|
std::unique_ptr<AbstractCheckView> check);
|
2015-10-06 19:49:23 +00:00
|
|
|
~Radiobutton();
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
protected:
|
2017-07-18 17:11:44 +00:00
|
|
|
void handlePress() override;
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
private:
|
2017-07-07 11:16:37 +00:00
|
|
|
// Hide the names from Checkbox.
|
|
|
|
bool checked() const;
|
2018-12-05 09:55:56 +00:00
|
|
|
void checkedChanges() const;
|
|
|
|
void checkedValue() const;
|
2017-07-07 11:16:37 +00:00
|
|
|
void setChecked(bool checked, NotifyAboutChange notify);
|
2018-12-05 09:55:56 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
Checkbox *checkbox() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
const Checkbox *checkbox() const {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
friend class RadiobuttonGroup;
|
|
|
|
void handleNewGroupValue(int value);
|
|
|
|
|
|
|
|
std::shared_ptr<RadiobuttonGroup> _group;
|
|
|
|
int _value = 0;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
2016-10-28 09:20:24 +00:00
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
template <typename Enum>
|
|
|
|
class Radioenum;
|
|
|
|
|
|
|
|
template <typename Enum>
|
|
|
|
class RadioenumGroup {
|
|
|
|
public:
|
|
|
|
RadioenumGroup() = default;
|
|
|
|
RadioenumGroup(Enum value) : _group(static_cast<int>(value)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
|
|
|
void setChangedCallback(Callback &&callback) {
|
|
|
|
_group.setChangedCallback([callback](int value) {
|
|
|
|
callback(static_cast<Enum>(value));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasValue() const {
|
|
|
|
return _group.hasValue();
|
|
|
|
}
|
|
|
|
Enum value() const {
|
|
|
|
return static_cast<Enum>(_group.value());
|
|
|
|
}
|
|
|
|
void setValue(Enum value) {
|
|
|
|
_group.setValue(static_cast<int>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename OtherEnum>
|
|
|
|
friend class Radioenum;
|
|
|
|
|
|
|
|
RadiobuttonGroup _group;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Enum>
|
|
|
|
class Radioenum : public Radiobutton {
|
|
|
|
public:
|
2018-04-13 10:10:09 +00:00
|
|
|
Radioenum(
|
|
|
|
QWidget *parent,
|
|
|
|
const std::shared_ptr<RadioenumGroup<Enum>> &group,
|
|
|
|
Enum value,
|
|
|
|
const QString &text,
|
|
|
|
const style::Checkbox &st = st::defaultCheckbox)
|
|
|
|
: Radiobutton(
|
|
|
|
parent,
|
|
|
|
std::shared_ptr<RadiobuttonGroup>(group, &group->_group),
|
|
|
|
static_cast<int>(value),
|
|
|
|
text,
|
|
|
|
st) {
|
|
|
|
}
|
|
|
|
Radioenum(
|
|
|
|
QWidget *parent,
|
|
|
|
const std::shared_ptr<RadioenumGroup<Enum>> &group,
|
|
|
|
Enum value,
|
|
|
|
const QString &text,
|
|
|
|
const style::Checkbox &st,
|
|
|
|
std::unique_ptr<AbstractCheckView> check)
|
|
|
|
: Radiobutton(
|
|
|
|
parent,
|
|
|
|
std::shared_ptr<RadiobuttonGroup>(group, &group->_group),
|
|
|
|
static_cast<int>(value),
|
|
|
|
text,
|
|
|
|
st,
|
|
|
|
std::move(check)) {
|
2017-03-18 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
} // namespace Ui
|