1 #include "DoubleCalendarDialog.h"
2
3 #include "Gui/Utils/Style.h"
4 #include "Gui/Utils/Widgets/CalendarWidget.h"
5 #include "Utils/Language/LanguageUtils.h"
6
7 #include <QDialogButtonBox>
8 #include <QVBoxLayout>
9 #include <QLabel>
10 #include <QDate>
11 #include <QLocale>
12
13 using Gui::DoubleCalendarDialog;
14
15 struct DoubleCalendarDialog::Private
16 {
17 Gui::CalendarWidget* calendarFrom=nullptr;
18 Gui::CalendarWidget* calendarTo=nullptr;
19
createCalendarDoubleCalendarDialog::Private20 static Gui::CalendarWidget* createCalendar(QWidget* parent)
21 {
22 auto* calendar = new Gui::CalendarWidget(parent);
23
24 calendar->setSelectedDate(QDate::currentDate());
25 calendar->setDateEditEnabled(false);
26 calendar->setGridVisible(true);
27 calendar->setFirstDayOfWeek(Qt::DayOfWeek::Monday);
28 calendar->setLocale(Util::Language::getCurrentLocale());
29 calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat::ShortDayNames);
30 calendar->setMaximumDate(QDate::currentDate());
31 calendar->showToday();
32 calendar->resize(800, 600);
33
34
35 return calendar;
36 }
37 };
38
DoubleCalendarDialog(QWidget * parent)39 DoubleCalendarDialog::DoubleCalendarDialog(QWidget* parent) :
40 Gui::Dialog(parent)
41 {
42 m = Pimpl::make<Private>();
43
44 this->setWindowTitle(tr("Select date range"));
45 this->setLayout( new QVBoxLayout() );
46 this->setStyleSheet(Style::currentStyle());
47
48 m->calendarFrom = Private::createCalendar(this);
49 m->calendarTo = Private::createCalendar(this);
50
51 m->calendarFrom->showToday();
52 m->calendarTo->setMinimumDate(QDate::currentDate());
53 m->calendarTo->showToday();
54
55 { // calendard layout
56 auto* horLayout = new QHBoxLayout();
57 horLayout->setSpacing(10);
58
59 {
60 auto* verLayout = new QVBoxLayout();
61 verLayout->addWidget(new QLabel(tr("Start date"), this));
62 verLayout->addWidget(m->calendarFrom);
63 horLayout->addItem(verLayout);
64 }
65
66 {
67 auto* verLayout = new QVBoxLayout();
68 verLayout->addWidget(new QLabel(tr("End date"), this));
69 verLayout->addWidget(m->calendarTo);
70 horLayout->addItem(verLayout);
71 }
72
73 this->layout()->addItem(horLayout);
74 }
75
76 { //
77 auto* buttonBox = new QDialogButtonBox(this);
78 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
79
80 connect(buttonBox, &QDialogButtonBox::accepted, this, &DoubleCalendarDialog::sigAccepted);
81 connect(buttonBox, &QDialogButtonBox::rejected, this, &DoubleCalendarDialog::sigRejected);
82 connect(buttonBox, &QDialogButtonBox::accepted, this, &DoubleCalendarDialog::close);
83 connect(buttonBox, &QDialogButtonBox::rejected, this, &DoubleCalendarDialog::close);
84
85 this->layout()->addWidget(buttonBox);
86 }
87
88 connect(m->calendarFrom, &QCalendarWidget::clicked, this, &DoubleCalendarDialog::startDateSelected);
89 connect(m->calendarTo, &QCalendarWidget::clicked, this, &DoubleCalendarDialog::endDateSelected);
90 }
91
92 DoubleCalendarDialog::~DoubleCalendarDialog() = default;
93
startDate() const94 QDate DoubleCalendarDialog::startDate() const
95 {
96 return m->calendarFrom->selectedDate();
97 }
98
endDate() const99 QDate DoubleCalendarDialog::endDate() const
100 {
101 return m->calendarTo->selectedDate();
102 }
103
startDateSelected(const QDate & date)104 void DoubleCalendarDialog::startDateSelected(const QDate& date)
105 {
106 m->calendarTo->setMinimumDate(m->calendarFrom->selectedDate());
107 m->calendarTo->setCurrentPage(date.year(), date.month());
108 m->calendarTo->setSelectedDate(date);
109 }
110
endDateSelected(const QDate & date)111 void DoubleCalendarDialog::endDateSelected(const QDate& date)
112 {
113 Q_UNUSED(date)
114 }
115