2017-03-07 14:03:07 +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.
|
2017-03-07 14:03:07 +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
|
2017-03-07 14:03:07 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/calendar_box.h"
|
2017-03-07 14:03:07 +00:00
|
|
|
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_dialogs.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-07 15:28:45 +00:00
|
|
|
#include "ui/effects/ripple_animation.h"
|
2017-03-07 14:03:07 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kDaysInWeek = 7;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class CalendarBox::Context {
|
|
|
|
public:
|
|
|
|
Context(QDate month, QDate highlighted);
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
_month.setForced(_month.value(), true);
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:40:17 +00:00
|
|
|
void setMinDate(QDate date);
|
|
|
|
void setMaxDate(QDate date);
|
|
|
|
|
|
|
|
int minDayIndex() const {
|
|
|
|
return _minDayIndex;
|
|
|
|
}
|
|
|
|
int maxDayIndex() const {
|
|
|
|
return _maxDayIndex;
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
void skipMonth(int skip);
|
|
|
|
void showMonth(QDate month);
|
|
|
|
|
|
|
|
int highlightedIndex() const {
|
|
|
|
return _highlightedIndex;
|
|
|
|
}
|
|
|
|
int rowsCount() const {
|
|
|
|
return _rowsCount;
|
|
|
|
}
|
|
|
|
int daysShift() const {
|
|
|
|
return _daysShift;
|
|
|
|
}
|
|
|
|
int daysCount() const {
|
|
|
|
return _daysCount;
|
|
|
|
}
|
|
|
|
bool isEnabled(int index) const {
|
2017-03-07 16:40:17 +00:00
|
|
|
return (index >= _minDayIndex) && (index <= _maxDayIndex);
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const base::Variable<QDate> &month() {
|
|
|
|
return _month;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDate dateFromIndex(int index) const;
|
|
|
|
QString labelFromIndex(int index) const;
|
|
|
|
|
|
|
|
private:
|
2017-03-07 16:40:17 +00:00
|
|
|
void applyMonth(const QDate &month, bool forced = false);
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
static int daysShiftForMonth(QDate month);
|
|
|
|
static int rowsCountForMonth(QDate month);
|
|
|
|
|
|
|
|
base::Variable<QDate> _month;
|
2017-03-07 16:40:17 +00:00
|
|
|
QDate _min, _max;
|
2017-03-07 14:03:07 +00:00
|
|
|
QDate _highlighted;
|
|
|
|
|
|
|
|
int _highlightedIndex = 0;
|
2017-03-07 16:40:17 +00:00
|
|
|
int _minDayIndex = 0;
|
|
|
|
int _maxDayIndex = 0;
|
2017-03-07 14:03:07 +00:00
|
|
|
int _daysCount = 0;
|
|
|
|
int _daysShift = 0;
|
|
|
|
int _rowsCount = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
CalendarBox::Context::Context(QDate month, QDate highlighted) : _highlighted(highlighted) {
|
|
|
|
showMonth(month);
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:40:17 +00:00
|
|
|
void CalendarBox::Context::setMinDate(QDate date) {
|
|
|
|
_min = date;
|
|
|
|
applyMonth(_month.value(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Context::setMaxDate(QDate date) {
|
|
|
|
_max = date;
|
|
|
|
applyMonth(_month.value(), true);
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
void CalendarBox::Context::showMonth(QDate month) {
|
|
|
|
if (month.day() != 1) {
|
|
|
|
month = QDate(month.year(), month.month(), 1);
|
|
|
|
}
|
2017-03-07 16:40:17 +00:00
|
|
|
applyMonth(month);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Context::applyMonth(const QDate &month, bool forced) {
|
2017-03-07 14:03:07 +00:00
|
|
|
_daysCount = month.daysInMonth();
|
|
|
|
_daysShift = daysShiftForMonth(month);
|
|
|
|
_rowsCount = rowsCountForMonth(month);
|
|
|
|
auto yearIndex = month.year();
|
|
|
|
auto monthIndex = month.month();
|
|
|
|
_highlightedIndex = month.daysTo(_highlighted);
|
2017-03-07 16:40:17 +00:00
|
|
|
_minDayIndex = _min.isNull() ? INT_MIN : month.daysTo(_min);
|
|
|
|
_maxDayIndex = _max.isNull() ? INT_MAX : month.daysTo(_max);
|
2017-12-08 12:44:52 +00:00
|
|
|
if (forced) {
|
|
|
|
_month.setForced(month, true);
|
|
|
|
} else {
|
|
|
|
_month.set(month, true);
|
|
|
|
}
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Context::skipMonth(int skip) {
|
|
|
|
auto year = _month.value().year();
|
|
|
|
auto month = _month.value().month();
|
|
|
|
month += skip;
|
|
|
|
while (month < 1) {
|
|
|
|
--year;
|
|
|
|
month += 12;
|
|
|
|
}
|
|
|
|
while (month > 12) {
|
|
|
|
++year;
|
|
|
|
month -= 12;
|
|
|
|
}
|
|
|
|
showMonth(QDate(year, month, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
int CalendarBox::Context::daysShiftForMonth(QDate month) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!month.isNull());
|
2017-03-07 14:03:07 +00:00
|
|
|
constexpr auto kMaxRows = 6;
|
|
|
|
auto inMonthIndex = month.day() - 1;
|
|
|
|
auto inWeekIndex = month.dayOfWeek() - 1;
|
|
|
|
return ((kMaxRows * kDaysInWeek) + inWeekIndex - inMonthIndex) % kDaysInWeek;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CalendarBox::Context::rowsCountForMonth(QDate month) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!month.isNull());
|
2017-03-07 14:03:07 +00:00
|
|
|
auto daysShift = daysShiftForMonth(month);
|
|
|
|
auto daysCount = month.daysInMonth();
|
|
|
|
auto cellsCount = daysShift + daysCount;
|
|
|
|
auto result = (cellsCount / kDaysInWeek);
|
|
|
|
if (cellsCount % kDaysInWeek) ++result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDate CalendarBox::Context::dateFromIndex(int index) const {
|
|
|
|
constexpr auto kMonthsCount = 12;
|
|
|
|
auto month = _month.value().month();
|
|
|
|
auto year = _month.value().year();
|
|
|
|
while (index < 0) {
|
|
|
|
if (!--month) {
|
|
|
|
month += kMonthsCount;
|
|
|
|
--year;
|
|
|
|
}
|
|
|
|
index += QDate(year, month, 1).daysInMonth();
|
|
|
|
}
|
|
|
|
for (auto maxIndex = QDate(year, month, 1).daysInMonth(); index >= maxIndex; maxIndex = QDate(year, month, 1).daysInMonth()) {
|
|
|
|
index -= maxIndex;
|
|
|
|
if (month++ == kMonthsCount) {
|
|
|
|
month -= kMonthsCount;
|
|
|
|
++year;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QDate(year, month, index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CalendarBox::Context::labelFromIndex(int index) const {
|
|
|
|
auto day = [this, index] {
|
|
|
|
if (index >= 0 && index < daysCount()) {
|
|
|
|
return index + 1;
|
|
|
|
}
|
|
|
|
return dateFromIndex(index).day();
|
|
|
|
};
|
|
|
|
return QString::number(day());
|
|
|
|
}
|
|
|
|
|
|
|
|
class CalendarBox::Inner : public TWidget, public RPCSender, private base::Subscriber {
|
|
|
|
public:
|
|
|
|
Inner(QWidget *parent, Context *context);
|
|
|
|
|
|
|
|
int countHeight() {
|
|
|
|
auto innerHeight = st::calendarDaysHeight + _context->rowsCount() * st::calendarCellSize.height();
|
|
|
|
return st::calendarPadding.top() + innerHeight + st::calendarPadding.bottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDateChosenCallback(base::lambda<void(QDate)> callback) {
|
|
|
|
_dateChosenCallback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Inner();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void monthChanged(QDate month);
|
2017-12-08 12:44:52 +00:00
|
|
|
void setSelected(int selected);
|
2017-03-07 15:28:45 +00:00
|
|
|
void setPressed(int pressed);
|
2017-03-07 14:03:07 +00:00
|
|
|
|
|
|
|
int rowsLeft() const;
|
|
|
|
int rowsTop() const;
|
|
|
|
void resizeToCurrent();
|
|
|
|
void paintDayNames(Painter &p, QRect clip);
|
|
|
|
void paintRows(Painter &p, QRect clip);
|
|
|
|
|
|
|
|
Context *_context = nullptr;
|
|
|
|
|
2017-03-07 15:28:45 +00:00
|
|
|
std::map<int, std::unique_ptr<Ui::RippleAnimation>> _ripples;
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
base::lambda<void(QDate)> _dateChosenCallback;
|
|
|
|
|
|
|
|
static constexpr auto kEmptySelection = -kDaysInWeek;
|
|
|
|
int _selected = kEmptySelection;
|
|
|
|
int _pressed = kEmptySelection;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-12-08 12:44:52 +00:00
|
|
|
CalendarBox::Inner::Inner(QWidget *parent, Context *context)
|
|
|
|
: TWidget(parent)
|
2017-03-07 14:03:07 +00:00
|
|
|
, _context(context) {
|
|
|
|
setMouseTracking(true);
|
2017-12-08 12:44:52 +00:00
|
|
|
subscribe(context->month(), [this](QDate month) {
|
|
|
|
monthChanged(month);
|
|
|
|
});
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::monthChanged(QDate month) {
|
2017-12-08 12:44:52 +00:00
|
|
|
setSelected(kEmptySelection);
|
2017-03-07 15:28:45 +00:00
|
|
|
_ripples.clear();
|
2017-03-07 14:03:07 +00:00
|
|
|
resizeToCurrent();
|
|
|
|
update();
|
2017-12-08 12:44:52 +00:00
|
|
|
sendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton);
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::resizeToCurrent() {
|
|
|
|
resize(st::boxWideWidth, countHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
auto clip = e->rect();
|
|
|
|
|
|
|
|
paintDayNames(p, clip);
|
|
|
|
paintRows(p, clip);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::paintDayNames(Painter &p, QRect clip) {
|
|
|
|
p.setFont(st::calendarDaysFont);
|
|
|
|
p.setPen(st::calendarDaysFg);
|
|
|
|
auto y = st::calendarPadding.top();
|
2017-03-07 16:40:17 +00:00
|
|
|
auto x = rowsLeft();
|
2017-03-07 14:03:07 +00:00
|
|
|
if (!myrtlrect(x, y, st::calendarCellSize.width() * kDaysInWeek, st::calendarDaysHeight).intersects(clip)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto i = 0; i != kDaysInWeek; ++i, x += st::calendarCellSize.width()) {
|
|
|
|
auto rect = myrtlrect(x, y, st::calendarCellSize.width(), st::calendarDaysHeight);
|
|
|
|
if (!rect.intersects(clip)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p.drawText(rect, langDayOfWeek(i + 1), style::al_top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CalendarBox::Inner::rowsLeft() const {
|
|
|
|
return st::calendarPadding.left();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CalendarBox::Inner::rowsTop() const {
|
|
|
|
return st::calendarPadding.top() + st::calendarDaysHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::paintRows(Painter &p, QRect clip) {
|
|
|
|
p.setFont(st::calendarDaysFont);
|
2017-03-07 15:28:45 +00:00
|
|
|
auto ms = getms();
|
2017-03-07 14:03:07 +00:00
|
|
|
auto y = rowsTop();
|
|
|
|
auto index = -_context->daysShift();
|
|
|
|
auto highlightedIndex = _context->highlightedIndex();
|
|
|
|
for (auto row = 0, rowsCount = _context->rowsCount(), daysCount = _context->daysCount()
|
|
|
|
; row != rowsCount
|
|
|
|
; ++row, y += st::calendarCellSize.height()) {
|
|
|
|
auto x = rowsLeft();
|
|
|
|
if (!myrtlrect(x, y, st::calendarCellSize.width() * kDaysInWeek, st::calendarCellSize.height()).intersects(clip)) {
|
|
|
|
index += kDaysInWeek;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto col = 0; col != kDaysInWeek; ++col, ++index, x += st::calendarCellSize.width()) {
|
|
|
|
auto rect = myrtlrect(x, y, st::calendarCellSize.width(), st::calendarCellSize.height());
|
|
|
|
auto grayedOut = (index < 0 || index >= daysCount || !rect.intersects(clip));
|
|
|
|
auto highlighted = (index == highlightedIndex);
|
|
|
|
auto enabled = _context->isEnabled(index);
|
2017-03-07 15:28:45 +00:00
|
|
|
auto innerLeft = x + (st::calendarCellSize.width() - st::calendarCellInner) / 2;
|
|
|
|
auto innerTop = y + (st::calendarCellSize.height() - st::calendarCellInner) / 2;
|
2017-03-07 14:03:07 +00:00
|
|
|
if (highlighted) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
2017-03-07 15:28:45 +00:00
|
|
|
p.setBrush(grayedOut ? st::windowBgOver : st::dialogsBgActive);
|
|
|
|
p.drawEllipse(myrtlrect(innerLeft, innerTop, st::calendarCellInner, st::calendarCellInner));
|
2017-03-07 14:03:07 +00:00
|
|
|
p.setBrush(Qt::NoBrush);
|
|
|
|
}
|
2017-03-07 15:28:45 +00:00
|
|
|
auto it = _ripples.find(index);
|
|
|
|
if (it != _ripples.cend()) {
|
|
|
|
auto colorOverride = [highlighted, grayedOut] {
|
|
|
|
if (highlighted) {
|
|
|
|
return grayedOut ? st::windowBgRipple : st::dialogsRippleBgActive;
|
|
|
|
}
|
|
|
|
return st::windowBgOver;
|
|
|
|
};
|
|
|
|
it->second->paint(p, innerLeft, innerTop, width(), ms, &(colorOverride()->c));
|
|
|
|
if (it->second->empty()) {
|
|
|
|
_ripples.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 14:03:07 +00:00
|
|
|
if (highlighted) {
|
2017-03-07 15:28:45 +00:00
|
|
|
p.setPen(grayedOut ? st::windowSubTextFg : st::dialogsNameFgActive);
|
2017-03-07 14:03:07 +00:00
|
|
|
} else if (enabled) {
|
|
|
|
p.setPen(grayedOut ? st::windowSubTextFg : st::boxTextFg);
|
|
|
|
} else {
|
|
|
|
p.setPen(st::windowSubTextFg);
|
|
|
|
}
|
|
|
|
p.drawText(rect, _context->labelFromIndex(index), style::al_center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::mouseMoveEvent(QMouseEvent *e) {
|
2017-12-08 12:44:52 +00:00
|
|
|
const auto size = st::calendarCellSize;
|
|
|
|
const auto point = e->pos();
|
|
|
|
const auto inner = QRect(
|
|
|
|
rowsLeft(),
|
|
|
|
rowsTop(),
|
|
|
|
kDaysInWeek * size.width(),
|
|
|
|
_context->rowsCount() * size.height());
|
|
|
|
if (inner.contains(point)) {
|
|
|
|
const auto row = (point.y() - rowsTop()) / size.height();
|
|
|
|
const auto col = (point.x() - rowsLeft()) / size.width();
|
|
|
|
const auto index = row * kDaysInWeek + col - _context->daysShift();
|
|
|
|
setSelected(index);
|
2017-03-07 14:03:07 +00:00
|
|
|
} else {
|
2017-12-08 12:44:52 +00:00
|
|
|
setSelected(kEmptySelection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::setSelected(int selected) {
|
|
|
|
if (selected != kEmptySelection && !_context->isEnabled(selected)) {
|
|
|
|
selected = kEmptySelection;
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
2017-12-08 12:44:52 +00:00
|
|
|
_selected = selected;
|
|
|
|
setCursor((_selected == kEmptySelection)
|
|
|
|
? style::cur_default
|
|
|
|
: style::cur_pointer);
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::mousePressEvent(QMouseEvent *e) {
|
2017-03-07 15:28:45 +00:00
|
|
|
setPressed(_selected);
|
|
|
|
if (_selected != kEmptySelection) {
|
|
|
|
auto index = _selected + _context->daysShift();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(index >= 0);
|
2017-03-07 15:28:45 +00:00
|
|
|
|
|
|
|
auto row = index / kDaysInWeek;
|
|
|
|
auto col = index % kDaysInWeek;
|
|
|
|
auto cell = QRect(rowsLeft() + col * st::calendarCellSize.width(), rowsTop() + row * st::calendarCellSize.height(), st::calendarCellSize.width(), st::calendarCellSize.height());
|
|
|
|
auto it = _ripples.find(_selected);
|
|
|
|
if (it == _ripples.cend()) {
|
|
|
|
auto mask = Ui::RippleAnimation::ellipseMask(QSize(st::calendarCellInner, st::calendarCellInner));
|
|
|
|
auto update = [this, cell] { rtlupdate(cell); };
|
|
|
|
it = _ripples.emplace(_selected, std::make_unique<Ui::RippleAnimation>(st::defaultRippleAnimation, std::move(mask), std::move(update))).first;
|
|
|
|
}
|
|
|
|
auto ripplePosition = QPoint(cell.x() + (st::calendarCellSize.width() - st::calendarCellInner) / 2, cell.y() + (st::calendarCellSize.height() - st::calendarCellInner) / 2);
|
|
|
|
it->second->add(e->pos() - ripplePosition);
|
|
|
|
}
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
|
2017-03-07 15:28:45 +00:00
|
|
|
auto pressed = _pressed;
|
|
|
|
setPressed(kEmptySelection);
|
2017-03-07 14:03:07 +00:00
|
|
|
if (pressed != kEmptySelection && pressed == _selected) {
|
|
|
|
_dateChosenCallback(_context->dateFromIndex(pressed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:28:45 +00:00
|
|
|
void CalendarBox::Inner::setPressed(int pressed) {
|
|
|
|
if (_pressed != pressed) {
|
|
|
|
if (_pressed != kEmptySelection) {
|
|
|
|
auto it = _ripples.find(_pressed);
|
|
|
|
if (it != _ripples.cend()) {
|
|
|
|
it->second->lastStop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_pressed = pressed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
CalendarBox::Inner::~Inner() = default;
|
|
|
|
|
|
|
|
class CalendarBox::Title : public TWidget, private base::Subscriber {
|
|
|
|
public:
|
2017-12-08 12:44:52 +00:00
|
|
|
Title(QWidget *parent, Context *context)
|
|
|
|
: TWidget(parent)
|
|
|
|
, _context(context) {
|
2017-03-07 14:03:07 +00:00
|
|
|
subscribe(_context->month(), [this](QDate date) { monthChanged(date); });
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void monthChanged(QDate month);
|
|
|
|
|
|
|
|
Context *_context = nullptr;
|
|
|
|
|
|
|
|
QString _text;
|
|
|
|
int _textWidth = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void CalendarBox::Title::monthChanged(QDate month) {
|
|
|
|
_text = langMonthOfYearFull(month.month(), month.year());
|
|
|
|
_textWidth = st::calendarTitleFont->width(_text);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::Title::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.setFont(st::calendarTitleFont);
|
|
|
|
p.setPen(st::boxTitleFg);
|
|
|
|
p.drawTextLeft((width() - _textWidth) / 2, (height() - st::calendarTitleFont->height) / 2, width(), _text, _textWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
CalendarBox::CalendarBox(QWidget*, QDate month, QDate highlighted, base::lambda<void(QDate date)> callback)
|
2017-03-07 15:01:08 +00:00
|
|
|
: _context(std::make_unique<Context>(month, highlighted))
|
2017-03-07 14:03:07 +00:00
|
|
|
, _inner(this, _context.get())
|
|
|
|
, _title(this, _context.get())
|
2017-03-07 16:40:17 +00:00
|
|
|
, _previous(this, st::calendarPrevious)
|
|
|
|
, _next(this, st::calendarNext)
|
2017-03-07 14:03:07 +00:00
|
|
|
, _callback(std::move(callback)) {
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:40:17 +00:00
|
|
|
void CalendarBox::setMinDate(QDate date) {
|
|
|
|
_context->setMinDate(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::setMaxDate(QDate date) {
|
|
|
|
_context->setMaxDate(date);
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
void CalendarBox::prepare() {
|
2017-03-07 16:40:17 +00:00
|
|
|
_previous->setClickedCallback([this] {
|
|
|
|
if (isPreviousEnabled()) {
|
|
|
|
_context->skipMonth(-1);
|
|
|
|
}
|
2017-03-07 14:03:07 +00:00
|
|
|
});
|
2017-03-07 16:40:17 +00:00
|
|
|
_next->setClickedCallback([this] {
|
|
|
|
if (isNextEnabled()) {
|
|
|
|
_context->skipMonth(1);
|
|
|
|
}
|
2017-03-07 14:03:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// _inner = setInnerWidget(object_ptr<Inner>(this, _context.get()), st::calendarScroll, st::calendarTitleHeight);
|
|
|
|
_inner->setDateChosenCallback(std::move(_callback));
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_close), [this] { closeBox(); });
|
2017-03-07 14:03:07 +00:00
|
|
|
|
|
|
|
subscribe(_context->month(), [this](QDate month) { monthChanged(month); });
|
|
|
|
|
|
|
|
_context->start();
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:40:17 +00:00
|
|
|
bool CalendarBox::isPreviousEnabled() const {
|
|
|
|
return (_context->minDayIndex() < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CalendarBox::isNextEnabled() const {
|
|
|
|
return (_context->maxDayIndex() >= _context->daysCount());
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
void CalendarBox::monthChanged(QDate month) {
|
|
|
|
setDimensions(st::boxWideWidth, st::calendarTitleHeight + _inner->countHeight());
|
2017-03-07 16:40:17 +00:00
|
|
|
auto previousEnabled = isPreviousEnabled();
|
|
|
|
_previous->setIconOverride(previousEnabled ? nullptr : &st::calendarPreviousDisabled);
|
|
|
|
_previous->setRippleColorOverride(previousEnabled ? nullptr : &st::boxBg);
|
|
|
|
_previous->setCursor(previousEnabled ? style::cur_pointer : style::cur_default);
|
|
|
|
auto nextEnabled = isNextEnabled();
|
|
|
|
_next->setIconOverride(nextEnabled ? nullptr : &st::calendarNextDisabled);
|
|
|
|
_next->setRippleColorOverride(nextEnabled ? nullptr : &st::boxBg);
|
|
|
|
_next->setCursor(nextEnabled ? style::cur_pointer : style::cur_default);
|
2017-03-07 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarBox::resizeEvent(QResizeEvent *e) {
|
2017-03-07 16:40:17 +00:00
|
|
|
_previous->moveToLeft(0, 0);
|
|
|
|
_next->moveToRight(0, 0);
|
|
|
|
_title->setGeometryToLeft(_previous->width(), 0, width() - _previous->width() - _next->width(), st::calendarTitleHeight);
|
2017-03-07 14:03:07 +00:00
|
|
|
_inner->setGeometryToLeft(0, st::calendarTitleHeight, width(), height() - st::calendarTitleHeight);
|
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
CalendarBox::~CalendarBox() = default;
|