Kirigami-addons

infinitecalendarviewmodel.h
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <QAbstractItemModel>
7#include <QCalendar>
8#include <QDateTime>
9#include <QLocale>
10#include <QQmlParserStatus>
11#include <qqmlregistration.h>
12
13class InfiniteCalendarViewModel : public QAbstractListModel, public QQmlParserStatus
14{
16 QML_ELEMENT
17
19
20 // Amount of dates to add each time the model adds more dates
21 Q_PROPERTY(int datesToAdd READ datesToAdd WRITE setDatesToAdd NOTIFY datesToAddChanged)
22 Q_PROPERTY(int scale READ scale WRITE setScale NOTIFY scaleChanged)
23 Q_PROPERTY(QDateTime currentDate READ currentDate WRITE setCurrentDate NOTIFY currentDateChanged)
24 Q_PROPERTY(QDateTime minimumDate READ minimumDate WRITE setMinimumDate NOTIFY minimumDateChanged)
25 Q_PROPERTY(QDateTime maximumDate READ maximumDate WRITE setMaximumDate NOTIFY maximumDateChanged)
26
27public:
28 // The decade scale is designed to be used in a 4x3 grid, so shows 12 years at a time
29 enum Scale { WeekScale, MonthScale, YearScale, DecadeScale };
30 Q_ENUM(Scale);
31
32 enum Roles {
33 StartDateRole = Qt::UserRole + 1,
34 FirstDayOfMonthRole,
35 SelectedMonthRole,
36 SelectedYearRole,
37 };
38 Q_ENUM(Roles);
39
40 explicit InfiniteCalendarViewModel(QObject *parent = nullptr);
41 ~InfiniteCalendarViewModel() = default;
42
43 void setup();
44 QVariant data(const QModelIndex &idx, int role) const override;
45 QHash<int, QByteArray> roleNames() const override;
46 int rowCount(const QModelIndex &parent = {}) const override;
47
48 void classBegin() override;
49 void componentComplete() override;
50
51 QDateTime currentDate() const;
52 void setCurrentDate(const QDateTime &currentDate);
53
54 QDateTime minimumDate() const;
55 void setMinimumDate(const QDateTime &minimumDate);
56
57 QDateTime maximumDate() const;
58 void setMaximumDate(const QDateTime &maximumDate);
59
60 Q_INVOKABLE void addDates(bool atEnd, const QDateTime startFrom = {});
61
62 int datesToAdd() const;
63 void setDatesToAdd(int datesToAdd);
64
65 int scale();
66 void setScale(int scale);
67
69 void datesToAddChanged();
70 void scaleChanged();
71 void currentDateChanged();
72 void minimumDateChanged();
73 void maximumDateChanged();
74
75private:
76 void addWeekDates(bool atEnd, const QDateTime &startFrom);
77 void addMonthDates(bool atEnd, const QDateTime &startFrom);
78 void addYearDates(bool atEnd, const QDateTime &startFrom);
79 void addDecadeDates(bool atEnd, const QDateTime &startFrom);
80
81 QDateTime m_currentDate;
82 QDateTime m_minimumDate;
83 QDateTime m_maximumDate;
84 QVector<QDateTime> m_startDates;
85 QVector<QDateTime> m_firstDayOfMonthDates;
86 QLocale m_locale;
87 int m_datesToAdd = 10;
88 int m_scale = MonthScale;
89 bool m_isCompleted = false;
90};
Q_INTERFACES(...)
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
UserRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.