File indexing completed on 2024-04-28 04:40:44

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QCalendar>
0008 #include <QDate>
0009 #include <QLocale>
0010 #include <memory>
0011 
0012 /// Month model exposing month days and events to a QML view.
0013 class MonthModel : public QAbstractListModel
0014 {
0015     Q_OBJECT
0016     /// The year number of the month.
0017     Q_PROPERTY(int year READ year WRITE setYear NOTIFY yearChanged)
0018     /// The month number of the month.
0019     Q_PROPERTY(int month READ month WRITE setMonth NOTIFY monthChanged)
0020     /// The translated week days.
0021     Q_PROPERTY(QStringList weekDays READ weekDays CONSTANT)
0022     /// Set the selected date.
0023     Q_PROPERTY(QDate selected READ selected WRITE setSelected NOTIFY selectedChanged)
0024 public:
0025     enum Roles {
0026         // Day roles
0027         DayNumber = Qt::UserRole, ///< Day numbers, usually from 1 to 31.
0028         SameMonth, ///< True iff this day is in the same month as the one displayed.
0029         Date, ///< Date of the day.
0030         IsSelected, ///< Date is equal the selected date.
0031         IsToday ///< Date is today.
0032     };
0033 
0034 public:
0035     explicit MonthModel(QObject *parent = nullptr);
0036     ~MonthModel() override;
0037 
0038     int year() const;
0039     void setYear(int year);
0040     int month() const;
0041     void setMonth(int month);
0042     QDate selected() const;
0043     void setSelected(const QDate &selected);
0044 
0045     QStringList weekDays() const;
0046 
0047     /// Go to the next month.
0048     Q_INVOKABLE void next();
0049     /// Go to the previous month.
0050     Q_INVOKABLE void previous();
0051     /// Go to the currentDate.
0052     Q_INVOKABLE void goToday();
0053 
0054     // QAbstractItemModel overrides
0055     QHash<int, QByteArray> roleNames() const override;
0056     QVariant data(const QModelIndex &index, int role) const override;
0057     int rowCount(const QModelIndex &parent) const override;
0058 
0059 Q_SIGNALS:
0060     void yearChanged();
0061     void monthChanged();
0062     void selectedChanged();
0063 
0064 private:
0065     class Private;
0066     QLocale m_locale;
0067     std::unique_ptr<Private> d;
0068 };