QmlMaterial 0.1.0
Loading...
Searching...
No Matches
calendar_month_model.hpp
1#pragma once
2
3#include <QAbstractListModel>
4#include <QDateTime>
5#include <QLocale>
6#include <qqmlregistration.h>
7#include <array>
8#include "qml_material/export.hpp"
9
10namespace qml_material
11{
12class QML_MATERIAL_API CalendarMonthModel : public QAbstractListModel {
13 Q_OBJECT
14 QML_ELEMENT
15 Q_PROPERTY(int month READ month WRITE setMonth NOTIFY monthChanged FINAL)
16 Q_PROPERTY(int year READ year WRITE setYear NOTIFY yearChanged FINAL)
17 Q_PROPERTY(QLocale locale READ locale WRITE setLocale NOTIFY localeChanged FINAL)
18 Q_PROPERTY(QVariantList weekDays READ weekDays NOTIFY localeChanged FINAL)
19 Q_PROPERTY(QStringList monthNames READ monthNames NOTIFY localeChanged FINAL)
20 Q_PROPERTY(QDateTime firstDate READ firstDate NOTIFY firstDateChanged FINAL)
21public:
22 enum Role
23 {
24 DateRole = Qt::UserRole + 1,
25 DayRole,
26 TodayRole,
27 WeekNumberRole,
28 MonthRole,
29 YearRole
30 };
31 Q_ENUM(Role)
32 explicit CalendarMonthModel(QObject* parent = nullptr);
33 int month() const { return m_month; }
34 int year() const { return m_year; }
35 QLocale locale() const { return m_locale; }
36 QVariantList weekDays() const;
37 QStringList monthNames() const;
38 void setMonth(int);
39 void setYear(int);
40 void setLocale(const QLocale&);
41 int rowCount(const QModelIndex& parent = {}) const override;
42 QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const override;
43 QHash<int, QByteArray> roleNames() const override;
44 Q_INVOKABLE QDateTime dateAt(int index) const;
45 QDateTime firstDate() const;
46 Q_SIGNAL void firstDateChanged();
47 Q_SIGNAL void monthChanged();
48 Q_SIGNAL void yearChanged();
49 Q_SIGNAL void localeChanged();
50
51private:
52 void populate();
53 int m_month;
54 int m_year;
55 QLocale m_locale;
56 QDate m_today;
57 std::array<QDate, 42> m_dates;
58 std::array<QDateTime, 42> m_date_times;
59};
60} // namespace qml_material