2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-02 22:16:35 +03:00
|
|
|
#include "ui/widgets/buttons.h"
|
2019-04-02 13:13:30 +04:00
|
|
|
#include "ui/effects/animations.h"
|
2016-11-15 14:56:49 +03:00
|
|
|
#include "styles/style_widgets.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2016-10-28 12:20:24 +03:00
|
|
|
namespace Ui {
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
class AbstractCheckView {
|
|
|
|
public:
|
2018-06-04 18:35:11 +03:00
|
|
|
AbstractCheckView(int duration, bool checked, Fn<void()> updateCallback);
|
2017-07-07 14:16:37 +03:00
|
|
|
|
2018-09-26 14:28:16 +03:00
|
|
|
void setChecked(bool checked, anim::type animated);
|
2017-09-30 22:20:40 +03:00
|
|
|
void finishAnimating();
|
2018-06-04 18:35:11 +03:00
|
|
|
void setUpdateCallback(Fn<void()> updateCallback);
|
2017-07-07 14:16:37 +03:00
|
|
|
bool checked() const {
|
|
|
|
return _checked;
|
|
|
|
}
|
2018-04-13 14:10:09 +04:00
|
|
|
void update();
|
2019-04-02 13:13:30 +04:00
|
|
|
float64 currentAnimationValue();
|
2018-09-26 14:28:16 +03:00
|
|
|
bool animating() const;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
2018-12-06 19:47:28 +04:00
|
|
|
auto checkedChanges() const {
|
|
|
|
return _checks.events();
|
|
|
|
}
|
2017-09-27 15:04:19 +03:00
|
|
|
auto checkedValue() const {
|
2017-09-13 20:01:23 +03:00
|
|
|
return _checks.events_starting_with(checked());
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:06:27 +03:00
|
|
|
virtual QSize getSize() const = 0;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
2019-04-02 13:13:30 +04:00
|
|
|
virtual void paint(Painter &p, int left, int top, int outerWidth) = 0;
|
2017-07-09 18:06:27 +03:00
|
|
|
virtual QImage prepareRippleMask() const = 0;
|
|
|
|
virtual bool checkRippleStartPosition(QPoint position) const = 0;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
virtual ~AbstractCheckView() = default;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
private:
|
2018-09-26 14:28:16 +03:00
|
|
|
virtual void checkedChangedHook(anim::type animated) {
|
|
|
|
}
|
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
int _duration = 0;
|
|
|
|
bool _checked = false;
|
2018-06-04 18:35:11 +03:00
|
|
|
Fn<void()> _updateCallback;
|
2019-04-02 13:13:30 +04:00
|
|
|
Ui::Animations::Simple _toggleAnimation;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
2017-09-13 20:01:23 +03:00
|
|
|
rpl::event_stream<bool> _checks;
|
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CheckView : public AbstractCheckView {
|
2015-10-06 22:49:23 +03:00
|
|
|
public:
|
2018-04-13 14:10:09 +04:00
|
|
|
CheckView(
|
|
|
|
const style::Check &st,
|
|
|
|
bool checked,
|
2018-06-04 18:35:11 +03:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
void setStyle(const style::Check &st);
|
|
|
|
|
2017-07-09 18:06:27 +03:00
|
|
|
QSize getSize() const override;
|
2019-04-02 13:13:30 +04:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth) override;
|
2017-07-09 18:06:27 +03:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
2018-04-13 14:10:09 +04:00
|
|
|
void setUntoggledOverride(
|
2018-09-21 19:28:46 +03:00
|
|
|
std::optional<QColor> untoggledOverride);
|
2018-04-13 14:10:09 +04:00
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
private:
|
2017-07-09 18:06:27 +03:00
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 11:31:24 +03:00
|
|
|
not_null<const style::Check*> _st;
|
2018-09-21 19:28:46 +03:00
|
|
|
std::optional<QColor> _untoggledOverride;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class RadioView : public AbstractCheckView {
|
|
|
|
public:
|
2018-04-13 14:10:09 +04:00
|
|
|
RadioView(
|
|
|
|
const style::Radio &st,
|
|
|
|
bool checked,
|
2018-06-04 18:35:11 +03:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
void setStyle(const style::Radio &st);
|
|
|
|
|
2018-09-26 14:28:16 +03:00
|
|
|
void setToggledOverride(std::optional<QColor> toggledOverride);
|
2018-09-21 19:28:46 +03:00
|
|
|
void setUntoggledOverride(std::optional<QColor> untoggledOverride);
|
2018-04-13 14:10:09 +04:00
|
|
|
|
2017-07-09 18:06:27 +03:00
|
|
|
QSize getSize() const override;
|
2019-04-02 13:13:30 +04:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth) override;
|
2017-07-09 18:06:27 +03:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
private:
|
2017-07-09 18:06:27 +03:00
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 11:31:24 +03:00
|
|
|
not_null<const style::Radio*> _st;
|
2018-09-26 14:28:16 +03:00
|
|
|
std::optional<QColor> _toggledOverride;
|
2018-09-21 19:28:46 +03:00
|
|
|
std::optional<QColor> _untoggledOverride;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ToggleView : public AbstractCheckView {
|
|
|
|
public:
|
2018-04-13 14:10:09 +04:00
|
|
|
ToggleView(
|
|
|
|
const style::Toggle &st,
|
|
|
|
bool checked,
|
2018-06-04 18:35:11 +03:00
|
|
|
Fn<void()> updateCallback = nullptr);
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
void setStyle(const style::Toggle &st);
|
|
|
|
|
2017-07-09 18:06:27 +03:00
|
|
|
QSize getSize() const override;
|
2019-04-02 13:13:30 +04:00
|
|
|
void paint(Painter &p, int left, int top, int outerWidth) override;
|
2017-07-09 18:06:27 +03:00
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
bool checkRippleStartPosition(QPoint position) const override;
|
2019-01-21 14:22:18 +04:00
|
|
|
void setLocked(bool locked);
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
private:
|
2017-07-09 18:06:27 +03:00
|
|
|
void paintXV(Painter &p, int left, int top, int outerWidth, float64 toggled, const QBrush &brush);
|
|
|
|
QSize rippleSize() const;
|
|
|
|
|
2017-08-17 11:31:24 +03:00
|
|
|
not_null<const style::Toggle*> _st;
|
2019-01-21 14:22:18 +04:00
|
|
|
bool _locked = false;
|
2017-07-07 14:16:37 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Checkbox : public RippleButton {
|
|
|
|
public:
|
2018-04-13 14:10:09 +04: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 22:49:23 +03:00
|
|
|
|
2019-05-22 16:29:02 +02:00
|
|
|
void setText(const QString &text, bool rich = false);
|
2017-09-21 22:21:33 +03:00
|
|
|
void setCheckAlignment(style::align alignment);
|
2019-05-22 16:29:02 +02:00
|
|
|
void setAllowMultiline(bool allow);
|
2017-04-18 20:37:14 +03:00
|
|
|
|
2015-10-06 22:49:23 +03:00
|
|
|
bool checked() const;
|
2018-12-05 13:55:56 +04:00
|
|
|
rpl::producer<bool> checkedChanges() const;
|
|
|
|
rpl::producer<bool> checkedValue() const;
|
2016-06-01 16:07:03 +03:00
|
|
|
enum class NotifyAboutChange {
|
|
|
|
Notify,
|
|
|
|
DontNotify,
|
|
|
|
};
|
2018-12-05 13:55:56 +04:00
|
|
|
void setChecked(
|
|
|
|
bool checked,
|
|
|
|
NotifyAboutChange notify = NotifyAboutChange::Notify);
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-09-30 22:20:40 +03:00
|
|
|
void finishAnimating();
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2016-12-02 22:16:35 +03:00
|
|
|
QMargins getMargins() const override {
|
|
|
|
return _st.margin;
|
|
|
|
}
|
2016-08-22 19:16:21 +02:00
|
|
|
int naturalWidth() const override;
|
|
|
|
|
2017-09-21 22:21:33 +03:00
|
|
|
void updateCheck() {
|
|
|
|
rtlupdate(checkRect());
|
|
|
|
}
|
|
|
|
QRect checkRect() const;
|
|
|
|
|
2016-08-22 19:16:21 +02:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2016-12-05 14:01:08 +03:00
|
|
|
void onStateChanged(State was, StateChangeSource source) override;
|
2016-12-02 22:16:35 +03:00
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
QImage prepareRippleMask() const override;
|
|
|
|
QPoint prepareRippleStartPosition() const override;
|
2016-11-11 16:46:04 +03:00
|
|
|
|
2017-07-18 20:11:44 +03:00
|
|
|
virtual void handlePress();
|
|
|
|
|
2015-10-06 22:49:23 +03:00
|
|
|
private:
|
2017-04-18 20:37:14 +03:00
|
|
|
void resizeToText();
|
2017-07-14 12:47:17 +03:00
|
|
|
QPixmap grabCheckCache() const;
|
2019-06-01 12:44:10 +03:00
|
|
|
int countTextMinWidth() const;
|
2017-04-18 20:37:14 +03:00
|
|
|
|
2015-10-06 22:49:23 +03:00
|
|
|
const style::Checkbox &_st;
|
2017-07-07 14:16:37 +03:00
|
|
|
std::unique_ptr<AbstractCheckView> _check;
|
2018-12-05 13:55:56 +04:00
|
|
|
rpl::event_stream<bool> _checkedChanges;
|
2017-07-14 12:47:17 +03:00
|
|
|
QPixmap _checkCache;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2019-06-12 15:26:04 +02:00
|
|
|
Text::String _text;
|
2017-09-21 22:21:33 +03:00
|
|
|
style::align _checkAlignment = style::al_left;
|
2019-05-22 16:29:02 +02:00
|
|
|
bool _allowMultiline = false;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-18 14:55:04 +03:00
|
|
|
class Radiobutton;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-03-18 14:55:04 +03:00
|
|
|
class RadiobuttonGroup {
|
2015-10-06 22:49:23 +03:00
|
|
|
public:
|
2017-03-18 14:55:04 +03:00
|
|
|
RadiobuttonGroup() = default;
|
|
|
|
RadiobuttonGroup(int value) : _value(value), _hasValue(true) {
|
|
|
|
}
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2018-06-04 18:35:11 +03:00
|
|
|
void setChangedCallback(Fn<void(int value)> callback) {
|
2017-03-18 14:55:04 +03:00
|
|
|
_changedCallback = std::move(callback);
|
|
|
|
}
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-03-18 14:55:04 +03:00
|
|
|
bool hasValue() const {
|
|
|
|
return _hasValue;
|
|
|
|
}
|
|
|
|
int value() const {
|
2015-10-06 22:49:23 +03:00
|
|
|
return _value;
|
|
|
|
}
|
2017-03-18 14:55:04 +03:00
|
|
|
void setValue(int value);
|
|
|
|
|
2017-03-19 00:06:10 +03:00
|
|
|
private:
|
|
|
|
friend class Radiobutton;
|
2017-03-18 14:55:04 +03: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 18:35:11 +03:00
|
|
|
Fn<void(int value)> _changedCallback;
|
2017-03-18 14:55:04 +03:00
|
|
|
std::vector<Radiobutton*> _buttons;
|
|
|
|
|
|
|
|
};
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2018-12-05 13:55:56 +04:00
|
|
|
class Radiobutton : public Checkbox {
|
2017-03-18 14:55:04 +03:00
|
|
|
public:
|
2018-04-13 14:10:09 +04: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 22:49:23 +03:00
|
|
|
~Radiobutton();
|
|
|
|
|
2016-11-11 16:46:04 +03:00
|
|
|
protected:
|
2017-07-18 20:11:44 +03:00
|
|
|
void handlePress() override;
|
2016-11-11 16:46:04 +03:00
|
|
|
|
2015-10-06 22:49:23 +03:00
|
|
|
private:
|
2017-07-07 14:16:37 +03:00
|
|
|
// Hide the names from Checkbox.
|
|
|
|
bool checked() const;
|
2018-12-05 13:55:56 +04:00
|
|
|
void checkedChanges() const;
|
|
|
|
void checkedValue() const;
|
2017-07-07 14:16:37 +03:00
|
|
|
void setChecked(bool checked, NotifyAboutChange notify);
|
2018-12-05 13:55:56 +04:00
|
|
|
|
2017-07-07 14:16:37 +03:00
|
|
|
Checkbox *checkbox() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
const Checkbox *checkbox() const {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-18 14:55:04 +03:00
|
|
|
friend class RadiobuttonGroup;
|
|
|
|
void handleNewGroupValue(int value);
|
|
|
|
|
|
|
|
std::shared_ptr<RadiobuttonGroup> _group;
|
|
|
|
int _value = 0;
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
};
|
2016-10-28 12:20:24 +03:00
|
|
|
|
2017-03-19 00:06:10 +03: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 14:10:09 +04: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-19 00:06:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-28 12:20:24 +03:00
|
|
|
} // namespace Ui
|