calendar_box.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/layers/box_content.h"
  9. #include "ui/widgets/tooltip.h"
  10. #include "base/required.h"
  11. #include "base/timer.h"
  12. #include <QtCore/QDate>
  13. namespace style {
  14. struct CalendarSizes;
  15. struct CalendarColors;
  16. } // namespace style
  17. namespace st {
  18. extern const style::CalendarSizes &defaultCalendarSizes;
  19. extern const style::CalendarColors &defaultCalendarColors;
  20. } // namespace st
  21. namespace Ui {
  22. class IconButton;
  23. class ScrollArea;
  24. class CalendarBox;
  25. struct CalendarBoxArgs {
  26. template <typename T>
  27. using required = base::required<T>;
  28. required<QDate> month;
  29. required<QDate> highlighted;
  30. required<Fn<void(QDate date)>> callback;
  31. FnMut<void(not_null<CalendarBox*>)> finalize;
  32. const style::CalendarSizes &st = st::defaultCalendarSizes;
  33. QDate minDate;
  34. QDate maxDate;
  35. bool allowsSelection = false;
  36. Fn<void(
  37. not_null<Ui::CalendarBox*>,
  38. std::optional<int>)> selectionChanged;
  39. const style::CalendarColors &stColors = st::defaultCalendarColors;
  40. };
  41. class CalendarBox final : public BoxContent, private AbstractTooltipShower {
  42. public:
  43. CalendarBox(QWidget*, CalendarBoxArgs &&args);
  44. ~CalendarBox();
  45. void toggleSelectionMode(bool enabled);
  46. [[nodiscard]] QDate selectedFirstDate() const;
  47. [[nodiscard]] QDate selectedLastDate() const;
  48. protected:
  49. void prepare() override;
  50. void keyPressEvent(QKeyEvent *e) override;
  51. void resizeEvent(QResizeEvent *e) override;
  52. private:
  53. void monthChanged(QDate month);
  54. bool isPreviousEnabled() const;
  55. bool isNextEnabled() const;
  56. void goPreviousMonth();
  57. void goNextMonth();
  58. void setExactScroll();
  59. void processScroll();
  60. void createButtons();
  61. void showJumpTooltip(not_null<IconButton*> button);
  62. void jumpAfterDelay(not_null<IconButton*> button);
  63. void jump(QPointer<IconButton> button);
  64. QString tooltipText() const override;
  65. QPoint tooltipPos() const override;
  66. bool tooltipWindowActive() const override;
  67. const style::CalendarSizes &_st;
  68. const style::CalendarColors &_styleColors;
  69. class Context;
  70. std::unique_ptr<Context> _context;
  71. std::unique_ptr<ScrollArea> _scroll;
  72. class Inner;
  73. not_null<Inner*> _inner;
  74. class FloatingDate;
  75. std::unique_ptr<FloatingDate> _floatingDate;
  76. class Title;
  77. object_ptr<Title> _title;
  78. object_ptr<IconButton> _previous;
  79. object_ptr<IconButton> _next;
  80. bool _previousEnabled = false;
  81. bool _nextEnabled = false;
  82. Fn<void(QDate date)> _callback;
  83. FnMut<void(not_null<CalendarBox*>)> _finalize;
  84. bool _watchScroll = false;
  85. QPointer<IconButton> _tooltipButton;
  86. QPointer<IconButton> _jumpButton;
  87. base::Timer _jumpTimer;
  88. bool _selectionMode = false;
  89. Fn<void(
  90. not_null<Ui::CalendarBox*>,
  91. std::optional<int>)> _selectionChanged;
  92. };
  93. } // namespace Ui