2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-12-02 19:16:35 +00:00
|
|
|
#include "ui/effects/ripple_animation.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
namespace Ui {
|
|
|
|
namespace {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
TextParseOptions _checkboxOptions = {
|
|
|
|
TextParseMultiline, // flags
|
|
|
|
0, // maxw
|
|
|
|
0, // maxh
|
|
|
|
Qt::LayoutDirectionAuto, // dir
|
|
|
|
};
|
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
AbstractCheckView::AbstractCheckView(int duration, bool checked, base::lambda<void()> updateCallback)
|
|
|
|
: _duration(duration)
|
|
|
|
, _checked(checked)
|
|
|
|
, _updateCallback(std::move(updateCallback)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCheckView::setCheckedFast(bool checked) {
|
|
|
|
_checked = checked;
|
2017-09-30 19:20:40 +00:00
|
|
|
finishAnimating();
|
2017-07-07 11:16:37 +00:00
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCheckView::setUpdateCallback(base::lambda<void()> updateCallback) {
|
|
|
|
_updateCallback = std::move(updateCallback);
|
|
|
|
if (_toggleAnimation.animating()) {
|
|
|
|
_toggleAnimation.setUpdateCallback(_updateCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractCheckView::setCheckedAnimated(bool checked) {
|
|
|
|
if (_checked != checked) {
|
|
|
|
_checked = checked;
|
|
|
|
_toggleAnimation.start(_updateCallback, _checked ? 0. : 1., _checked ? 1. : 0., _duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 19:20:40 +00:00
|
|
|
void AbstractCheckView::finishAnimating() {
|
2017-07-07 11:16:37 +00:00
|
|
|
_toggleAnimation.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 AbstractCheckView::currentAnimationValue(TimeMs ms) {
|
|
|
|
return ms ? _toggleAnimation.current(ms, _checked ? 1. : 0.) : _toggleAnimation.current(_checked ? 1. : 0.);
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
ToggleView::ToggleView(
|
|
|
|
const style::Toggle &st,
|
|
|
|
bool checked,
|
|
|
|
base::lambda<void()> updateCallback)
|
|
|
|
: AbstractCheckView(st.duration, checked, std::move(updateCallback))
|
2017-07-07 11:16:37 +00:00
|
|
|
, _st(&st) {
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize ToggleView::getSize() const {
|
|
|
|
return QSize(2 * _st->border + _st->diameter + _st->width, 2 * _st->border + _st->diameter);
|
2017-07-07 11:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToggleView::setStyle(const style::Toggle &st) {
|
|
|
|
_st = &st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToggleView::paint(Painter &p, int left, int top, int outerWidth, TimeMs ms) {
|
2017-07-09 15:06:27 +00:00
|
|
|
left += _st->border;
|
|
|
|
top += _st->border;
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
auto toggled = currentAnimationValue(ms);
|
|
|
|
auto fullWidth = _st->diameter + _st->width;
|
|
|
|
auto innerDiameter = _st->diameter - 2 * _st->shift;
|
|
|
|
auto innerRadius = float64(innerDiameter) / 2.;
|
2017-07-09 15:06:27 +00:00
|
|
|
auto toggleLeft = left + anim::interpolate(0, fullWidth - _st->diameter, toggled);
|
2017-07-07 11:16:37 +00:00
|
|
|
auto bgRect = rtlrect(left + _st->shift, top + _st->shift, fullWidth - 2 * _st->shift, innerDiameter, outerWidth);
|
2017-07-09 15:06:27 +00:00
|
|
|
auto fgRect = rtlrect(toggleLeft, top, _st->diameter, _st->diameter, outerWidth);
|
|
|
|
auto fgBrush = anim::brush(_st->untoggledFg, _st->toggledFg, toggled);
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
2017-07-09 15:06:27 +00:00
|
|
|
p.setBrush(fgBrush);
|
2017-07-07 11:16:37 +00:00
|
|
|
p.drawRoundedRect(bgRect, innerRadius, innerRadius);
|
|
|
|
|
|
|
|
auto pen = anim::pen(_st->untoggledFg, _st->toggledFg, toggled);
|
|
|
|
pen.setWidth(_st->border);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.setBrush(anim::brush(_st->untoggledBg, _st->toggledBg, toggled));
|
|
|
|
p.drawEllipse(fgRect);
|
2017-07-09 15:06:27 +00:00
|
|
|
|
|
|
|
if (_st->xsize > 0) {
|
|
|
|
paintXV(p, toggleLeft, top, outerWidth, toggled, fgBrush);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToggleView::paintXV(Painter &p, int left, int top, int outerWidth, float64 toggled, const QBrush &brush) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_st->vsize > 0);
|
|
|
|
Assert(_st->stroke > 0);
|
2017-07-09 15:06:27 +00:00
|
|
|
static const auto sqrt2 = sqrt(2.);
|
|
|
|
auto stroke = (0. + _st->stroke) / sqrt2;
|
|
|
|
if (toggled < 1) {
|
|
|
|
// Just X or X->V.
|
|
|
|
auto xSize = 0. + _st->xsize;
|
|
|
|
auto xLeft = left + (_st->diameter - xSize) / 2.;
|
|
|
|
auto xTop = top + (_st->diameter - xSize) / 2.;
|
|
|
|
QPointF pathX[] = {
|
|
|
|
{ xLeft, xTop + stroke },
|
|
|
|
{ xLeft + stroke, xTop },
|
|
|
|
{ xLeft + (xSize / 2.), xTop + (xSize / 2.) - stroke },
|
|
|
|
{ xLeft + xSize - stroke, xTop },
|
|
|
|
{ xLeft + xSize, xTop + stroke },
|
|
|
|
{ xLeft + (xSize / 2.) + stroke, xTop + (xSize / 2.) },
|
|
|
|
{ xLeft + xSize, xTop + xSize - stroke },
|
|
|
|
{ xLeft + xSize - stroke, xTop + xSize },
|
|
|
|
{ xLeft + (xSize / 2.), xTop + (xSize / 2.) + stroke },
|
|
|
|
{ xLeft + stroke, xTop + xSize },
|
|
|
|
{ xLeft, xTop + xSize - stroke },
|
|
|
|
{ xLeft + (xSize / 2.) - stroke, xTop + (xSize / 2.) },
|
|
|
|
};
|
|
|
|
for (auto &point : pathX) {
|
|
|
|
point = rtlpoint(point, outerWidth);
|
|
|
|
}
|
|
|
|
if (toggled > 0) {
|
|
|
|
// X->V.
|
|
|
|
auto vSize = 0. + _st->vsize;
|
|
|
|
auto fSize = (xSize + vSize - 2. * stroke);
|
|
|
|
auto vLeft = left + (_st->diameter - fSize) / 2.;
|
|
|
|
auto vTop = 0. + xTop + _st->vshift;
|
|
|
|
QPointF pathV[] = {
|
|
|
|
{ vLeft, vTop + xSize - vSize + stroke },
|
|
|
|
{ vLeft + stroke, vTop + xSize - vSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize - 2 * stroke },
|
|
|
|
{ vLeft + fSize - stroke, vTop },
|
|
|
|
{ vLeft + fSize, vTop + stroke },
|
|
|
|
{ vLeft + vSize, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - 2 * stroke, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize - 2 * stroke, vTop + xSize - stroke },
|
|
|
|
};
|
|
|
|
for (auto &point : pathV) {
|
|
|
|
point = rtlpoint(point, outerWidth);
|
|
|
|
}
|
|
|
|
p.fillPath(anim::interpolate(pathX, pathV, toggled), brush);
|
|
|
|
} else {
|
|
|
|
// Just X.
|
|
|
|
p.fillPath(anim::path(pathX), brush);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just V.
|
|
|
|
auto xSize = 0. + _st->xsize;
|
|
|
|
auto xTop = top + (_st->diameter - xSize) / 2.;
|
|
|
|
auto vSize = 0. + _st->vsize;
|
|
|
|
auto fSize = (xSize + vSize - 2. * stroke);
|
|
|
|
auto vLeft = left + (_st->diameter - (_st->xsize + _st->vsize - 2. * stroke)) / 2.;
|
|
|
|
auto vTop = 0. + xTop + _st->vshift;
|
|
|
|
QPointF pathV[] = {
|
|
|
|
{ vLeft, vTop + xSize - vSize + stroke },
|
|
|
|
{ vLeft + stroke, vTop + xSize - vSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize - 2 * stroke },
|
|
|
|
{ vLeft + fSize - stroke, vTop },
|
|
|
|
{ vLeft + fSize, vTop + stroke },
|
|
|
|
{ vLeft + vSize, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - stroke, vTop + xSize },
|
|
|
|
{ vLeft + vSize - 2 * stroke, vTop + xSize - stroke },
|
|
|
|
{ vLeft + vSize - 2 * stroke, vTop + xSize - stroke },
|
|
|
|
};
|
|
|
|
|
|
|
|
p.fillPath(anim::path(pathV), brush);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize ToggleView::rippleSize() const {
|
|
|
|
return getSize() + 2 * QSize(_st->rippleAreaPadding, _st->rippleAreaPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage ToggleView::prepareRippleMask() const {
|
|
|
|
auto size = rippleSize();
|
|
|
|
return RippleAnimation::roundRectMask(size, size.height() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToggleView::checkRippleStartPosition(QPoint position) const {
|
|
|
|
return QRect(QPoint(0, 0), rippleSize()).contains(position);
|
2017-07-07 11:16:37 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
CheckView::CheckView(const style::Check &st, bool checked, base::lambda<void()> updateCallback) : AbstractCheckView(st.duration, checked, std::move(updateCallback))
|
|
|
|
, _st(&st) {
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize CheckView::getSize() const {
|
2017-07-07 11:16:37 +00:00
|
|
|
return QSize(_st->diameter, _st->diameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckView::setStyle(const style::Check &st) {
|
|
|
|
_st = &st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckView::paint(Painter &p, int left, int top, int outerWidth, TimeMs ms) {
|
|
|
|
auto toggled = currentAnimationValue(ms);
|
|
|
|
auto pen = anim::pen(_st->untoggledFg, _st->toggledFg, toggled);
|
|
|
|
pen.setWidth(_st->thickness);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.setBrush(anim::brush(_st->bg, anim::color(_st->untoggledFg, _st->toggledFg, toggled), toggled));
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawRoundedRect(rtlrect(QRectF(left, top, _st->diameter, _st->diameter).marginsRemoved(QMarginsF(_st->thickness / 2., _st->thickness / 2., _st->thickness / 2., _st->thickness / 2.)), outerWidth), st::buttonRadius - (_st->thickness / 2.), st::buttonRadius - (_st->thickness / 2.));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toggled > 0) {
|
|
|
|
_st->icon.paint(p, QPoint(left, top), outerWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize CheckView::rippleSize() const {
|
|
|
|
return getSize() + 2 * QSize(_st->rippleAreaPadding, _st->rippleAreaPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage CheckView::prepareRippleMask() const {
|
|
|
|
return RippleAnimation::ellipseMask(rippleSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckView::checkRippleStartPosition(QPoint position) const {
|
|
|
|
return QRect(QPoint(0, 0), rippleSize()).contains(position);
|
|
|
|
}
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
RadioView::RadioView(const style::Radio &st, bool checked, base::lambda<void()> updateCallback) : AbstractCheckView(st.duration, checked, std::move(updateCallback))
|
|
|
|
, _st(&st) {
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize RadioView::getSize() const {
|
2017-07-07 11:16:37 +00:00
|
|
|
return QSize(_st->diameter, _st->diameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioView::setStyle(const style::Radio &st) {
|
|
|
|
_st = &st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioView::paint(Painter &p, int left, int top, int outerWidth, TimeMs ms) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
|
|
|
auto toggled = currentAnimationValue(ms);
|
|
|
|
auto pen = anim::pen(_st->untoggledFg, _st->toggledFg, toggled);
|
|
|
|
pen.setWidth(_st->thickness);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.setBrush(_st->bg);
|
|
|
|
//int32 skip = qCeil(_st->thickness / 2.);
|
|
|
|
//p.drawEllipse(_checkRect.marginsRemoved(QMargins(skip, skip, skip, skip)));
|
|
|
|
p.drawEllipse(rtlrect(QRectF(left, top, _st->diameter, _st->diameter).marginsRemoved(QMarginsF(_st->thickness / 2., _st->thickness / 2., _st->thickness / 2., _st->thickness / 2.)), outerWidth));
|
|
|
|
|
|
|
|
if (toggled > 0) {
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(anim::brush(_st->untoggledFg, _st->toggledFg, toggled));
|
|
|
|
|
|
|
|
auto skip0 = _st->diameter / 2., skip1 = _st->skip / 10., checkSkip = skip0 * (1. - toggled) + skip1 * toggled;
|
|
|
|
p.drawEllipse(rtlrect(QRectF(left, top, _st->diameter, _st->diameter).marginsRemoved(QMarginsF(checkSkip, checkSkip, checkSkip, checkSkip)), outerWidth));
|
|
|
|
//int32 fskip = qFloor(checkSkip), cskip = qCeil(checkSkip);
|
|
|
|
//if (2 * fskip < _checkRect.width()) {
|
|
|
|
// if (fskip != cskip) {
|
|
|
|
// p.setOpacity(float64(cskip) - checkSkip);
|
|
|
|
// p.drawEllipse(_checkRect.marginsRemoved(QMargins(fskip, fskip, fskip, fskip)));
|
|
|
|
// p.setOpacity(1.);
|
|
|
|
// }
|
|
|
|
// if (2 * cskip < _checkRect.width()) {
|
|
|
|
// p.drawEllipse(_checkRect.marginsRemoved(QMargins(cskip, cskip, cskip, cskip)));
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:27 +00:00
|
|
|
QSize RadioView::rippleSize() const {
|
|
|
|
return getSize() + 2 * QSize(_st->rippleAreaPadding, _st->rippleAreaPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage RadioView::prepareRippleMask() const {
|
|
|
|
return RippleAnimation::ellipseMask(rippleSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioView::checkRippleStartPosition(QPoint position) const {
|
|
|
|
return QRect(QPoint(0, 0), rippleSize()).contains(position);
|
|
|
|
}
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
Checkbox::Checkbox(QWidget *parent, const QString &text, bool checked, const style::Checkbox &st, const style::Check &checkSt) : Checkbox(parent, text, st, std::make_unique<CheckView>(checkSt, checked, [this] { updateCheck(); })) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Checkbox::Checkbox(QWidget *parent, const QString &text, bool checked, const style::Checkbox &st, const style::Toggle &toggleSt) : Checkbox(parent, text, st, std::make_unique<ToggleView>(toggleSt, checked, [this] { updateCheck(); })) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Checkbox::Checkbox(QWidget *parent, const QString &text, const style::Checkbox &st, std::unique_ptr<AbstractCheckView> check) : RippleButton(parent, st.ripple)
|
|
|
|
, _st(st)
|
|
|
|
, _check(std::move(check))
|
|
|
|
, _text(_st.style, text, _checkboxOptions) {
|
|
|
|
resizeToText();
|
2015-10-06 19:49:23 +00:00
|
|
|
setCursor(style::cur_pointer);
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
QRect Checkbox::checkRect() const {
|
|
|
|
auto size = _check->getSize();
|
|
|
|
return QRect({
|
|
|
|
(_checkAlignment & Qt::AlignHCenter)
|
|
|
|
? (width() - size.width()) / 2
|
|
|
|
: (_checkAlignment & Qt::AlignRight)
|
|
|
|
? (width() - _st.checkPosition.x() - size.width())
|
|
|
|
: _st.checkPosition.x(),
|
|
|
|
(_checkAlignment & Qt::AlignVCenter)
|
|
|
|
? (height() - size.height()) / 2
|
|
|
|
: (_checkAlignment & Qt::AlignBottom)
|
|
|
|
? (height() - _st.checkPosition.y() - size.height())
|
|
|
|
: _st.checkPosition.y()
|
|
|
|
}, size);
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
void Checkbox::setText(const QString &text) {
|
|
|
|
_text.setText(_st.style, text, _checkboxOptions);
|
|
|
|
resizeToText();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
void Checkbox::setCheckAlignment(style::align alignment) {
|
|
|
|
if (_checkAlignment != alignment) {
|
|
|
|
_checkAlignment = alignment;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
bool Checkbox::checked() const {
|
2017-07-07 11:16:37 +00:00
|
|
|
return _check->checked();
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
void Checkbox::resizeToText() {
|
|
|
|
if (_st.width <= 0) {
|
|
|
|
resizeToWidth(_text.maxWidth() - _st.width);
|
|
|
|
} else {
|
|
|
|
resizeToWidth(_st.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 13:07:03 +00:00
|
|
|
void Checkbox::setChecked(bool checked, NotifyAboutChange notify) {
|
2017-07-07 11:16:37 +00:00
|
|
|
if (_check->checked() != checked) {
|
|
|
|
_check->setCheckedAnimated(checked);
|
2016-06-01 13:07:03 +00:00
|
|
|
if (notify == NotifyAboutChange::Notify) {
|
2017-07-07 11:16:37 +00:00
|
|
|
checkedChanged.notify(checked, true);
|
2016-06-01 13:07:03 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 19:20:40 +00:00
|
|
|
void Checkbox::finishAnimating() {
|
|
|
|
_check->finishAnimating();
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
int Checkbox::naturalWidth() const {
|
2017-09-21 19:21:33 +00:00
|
|
|
if (_st.width > 0) {
|
|
|
|
return _st.width;
|
|
|
|
}
|
|
|
|
auto result = _st.checkPosition.x() + _check->getSize().width();
|
|
|
|
if (!_text.isEmpty()) {
|
|
|
|
result += _st.textPosition.x() + _text.maxWidth();
|
|
|
|
}
|
|
|
|
return result - _st.width;
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
void Checkbox::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
auto check = checkRect();
|
2016-12-02 19:16:35 +00:00
|
|
|
auto ms = getms();
|
2017-07-09 15:06:27 +00:00
|
|
|
if (isDisabled()) {
|
|
|
|
p.setOpacity(_st.disabledOpacity);
|
|
|
|
} else {
|
|
|
|
auto active = _check->currentAnimationValue(ms);
|
|
|
|
auto color = anim::color(_st.rippleBg, _st.rippleBgActive, active);
|
2017-09-21 19:21:33 +00:00
|
|
|
paintRipple(
|
|
|
|
p,
|
|
|
|
check.x() + _st.rippleAreaPosition.x(),
|
|
|
|
check.y() + _st.rippleAreaPosition.y(),
|
|
|
|
ms,
|
|
|
|
&color);
|
2017-07-09 15:06:27 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
auto realCheckRect = myrtlrect(check);
|
2017-07-07 11:16:37 +00:00
|
|
|
if (realCheckRect.intersects(e->rect())) {
|
2017-07-14 09:47:17 +00:00
|
|
|
if (isDisabled()) {
|
2017-09-21 19:21:33 +00:00
|
|
|
p.drawPixmapLeft(check.left(), check.top(), width(), _checkCache);
|
2017-07-14 09:47:17 +00:00
|
|
|
} else {
|
2017-09-21 19:21:33 +00:00
|
|
|
_check->paint(p, check.left(), check.top(), width());
|
2017-07-14 09:47:17 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2017-07-07 11:16:37 +00:00
|
|
|
if (realCheckRect.contains(e->rect())) return;
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
auto leftSkip = _st.checkPosition.x()
|
|
|
|
+ check.width()
|
|
|
|
+ _st.textPosition.x();
|
|
|
|
auto availableTextWidth = qMax(width() - leftSkip, 1);
|
|
|
|
|
|
|
|
if (!_text.isEmpty()) {
|
|
|
|
Assert(!(_checkAlignment & Qt::AlignHCenter));
|
|
|
|
p.setPen(_st.textFg);
|
|
|
|
auto textSkip = _st.checkPosition.x()
|
|
|
|
+ check.width()
|
|
|
|
+ _st.textPosition.x();
|
|
|
|
auto textTop = _st.margin.top() + _st.textPosition.y();
|
|
|
|
if (_checkAlignment & Qt::AlignLeft) {
|
|
|
|
_text.drawLeftElided(
|
|
|
|
p,
|
|
|
|
textSkip,
|
|
|
|
textTop,
|
|
|
|
availableTextWidth,
|
|
|
|
width());
|
|
|
|
} else {
|
|
|
|
_text.drawRightElided(
|
|
|
|
p,
|
|
|
|
textSkip,
|
|
|
|
textTop,
|
|
|
|
availableTextWidth,
|
|
|
|
width());
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 09:47:17 +00:00
|
|
|
QPixmap Checkbox::grabCheckCache() const {
|
2017-09-21 19:21:33 +00:00
|
|
|
auto checkSize = _check->getSize();
|
|
|
|
auto image = QImage(checkSize * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
2017-07-14 09:47:17 +00:00
|
|
|
image.fill(Qt::transparent);
|
|
|
|
image.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
{
|
|
|
|
Painter p(&image);
|
2017-09-21 19:21:33 +00:00
|
|
|
_check->paint(p, 0, 0, checkSize.width());
|
2017-07-14 09:47:17 +00:00
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std::move(image));
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void Checkbox::onStateChanged(State was, StateChangeSource source) {
|
|
|
|
RippleButton::onStateChanged(was, source);
|
2016-12-02 19:16:35 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
if (isDisabled() && !(was & StateFlag::Disabled)) {
|
2015-10-06 19:49:23 +00:00
|
|
|
setCursor(style::cur_default);
|
2017-09-30 19:20:40 +00:00
|
|
|
finishAnimating();
|
2017-07-14 09:47:17 +00:00
|
|
|
_checkCache = grabCheckCache();
|
2016-12-05 11:01:08 +00:00
|
|
|
} else if (!isDisabled() && (was & StateFlag::Disabled)) {
|
2015-10-06 19:49:23 +00:00
|
|
|
setCursor(style::cur_pointer);
|
2017-07-14 09:47:17 +00:00
|
|
|
_checkCache = QPixmap();
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2017-07-07 11:16:37 +00:00
|
|
|
|
|
|
|
auto now = state();
|
|
|
|
if (!isDisabled() && (was & StateFlag::Over) && (now & StateFlag::Over)) {
|
|
|
|
if ((was & StateFlag::Down) && !(now & StateFlag::Down)) {
|
2017-07-18 17:11:44 +00:00
|
|
|
handlePress();
|
2017-07-07 11:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 17:11:44 +00:00
|
|
|
void Checkbox::handlePress() {
|
|
|
|
setChecked(!checked());
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:16:35 +00:00
|
|
|
int Checkbox::resizeGetHeight(int newWidth) {
|
2017-07-12 13:53:42 +00:00
|
|
|
return _check->getSize().height();
|
2016-12-02 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage Checkbox::prepareRippleMask() const {
|
2017-07-09 15:06:27 +00:00
|
|
|
return _check->prepareRippleMask();
|
2016-12-02 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPoint Checkbox::prepareRippleStartPosition() const {
|
2017-07-09 15:06:27 +00:00
|
|
|
if (isDisabled()) {
|
|
|
|
return DisabledRippleStartPosition();
|
2016-12-02 19:16:35 +00:00
|
|
|
}
|
2017-09-21 19:21:33 +00:00
|
|
|
auto position = myrtlpoint(mapFromGlobal(QCursor::pos()))
|
|
|
|
- checkRect().topLeft()
|
|
|
|
- _st.rippleAreaPosition;
|
|
|
|
return _check->checkRippleStartPosition(position)
|
|
|
|
? position
|
|
|
|
: DisabledRippleStartPosition();
|
2016-12-02 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
void RadiobuttonGroup::setValue(int value) {
|
|
|
|
if (_hasValue && _value == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_hasValue = true;
|
|
|
|
_value = value;
|
|
|
|
for (auto button : _buttons) {
|
|
|
|
button->handleNewGroupValue(_value);
|
|
|
|
}
|
|
|
|
if (_changedCallback) {
|
|
|
|
_changedCallback(_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 11:16:37 +00:00
|
|
|
Radiobutton::Radiobutton(QWidget *parent, const std::shared_ptr<RadiobuttonGroup> &group, int value, const QString &text, const style::Checkbox &st, const style::Radio &radioSt) : Checkbox(parent, text, st, std::make_unique<RadioView>(radioSt, (group->hasValue() && group->value() == value), [this] { updateCheck(); }))
|
2017-03-18 11:55:04 +00:00
|
|
|
, _group(group)
|
2017-07-07 11:16:37 +00:00
|
|
|
, _value(value) {
|
2017-03-18 11:55:04 +00:00
|
|
|
_group->registerButton(this);
|
2017-07-07 11:16:37 +00:00
|
|
|
subscribe(checkbox()->checkedChanged, [this](bool checked) {
|
|
|
|
if (checked) {
|
|
|
|
_group->setValue(_value);
|
|
|
|
}
|
|
|
|
});
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 11:55:04 +00:00
|
|
|
void Radiobutton::handleNewGroupValue(int value) {
|
|
|
|
auto checked = (value == _value);
|
2017-07-07 11:16:37 +00:00
|
|
|
if (checkbox()->checked() != checked) {
|
|
|
|
checkbox()->setChecked(checked, Ui::Checkbox::NotifyAboutChange::DontNotify);
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 17:11:44 +00:00
|
|
|
void Radiobutton::handlePress() {
|
|
|
|
if (!checkbox()->checked()) {
|
|
|
|
checkbox()->setChecked(true);
|
2017-03-18 11:55:04 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Radiobutton::~Radiobutton() {
|
2017-03-18 11:55:04 +00:00
|
|
|
_group->unregisterButton(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-10-28 09:20:24 +00:00
|
|
|
|
|
|
|
} // namespace Ui
|