File indexing completed on 2024-06-23 04:42:36

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 <QDate>
0008 #include <QLocale>
0009 #include <QQmlEngine>
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     QML_ELEMENT
0017     
0018     /// The year number of the month.
0019     Q_PROPERTY(int year READ year WRITE setYear NOTIFY yearChanged)
0020     /// The month number of the month.
0021     Q_PROPERTY(int month READ month WRITE setMonth NOTIFY monthChanged)
0022     /// The translated week days.
0023     Q_PROPERTY(QStringList weekDays READ weekDays CONSTANT)
0024     /// Set the selected date.
0025     Q_PROPERTY(QDate selected READ selected WRITE setSelected NOTIFY selectedChanged)
0026 public:
0027     enum Roles {
0028         // Day roles
0029         DayNumber = Qt::UserRole, ///< Day numbers, usually from 1 to 31.
0030         SameMonth, ///< True iff this day is in the same month as the one displayed.
0031         Date, ///< Date of the day.
0032         IsSelected, ///< Date is equal the selected date.
0033         IsToday ///< Date is today.
0034     };
0035 
0036 public:
0037     explicit MonthModel(QObject *parent = nullptr);
0038     ~MonthModel() override;
0039 
0040     int year() const;
0041     void setYear(int year);
0042     int month() const;
0043     void setMonth(int month);
0044     QDate selected() const;
0045     void setSelected(const QDate &selected);
0046 
0047     QStringList weekDays() const;
0048 
0049     /// Go to the next month.
0050     Q_INVOKABLE void next();
0051     /// Go to the previous month.
0052     Q_INVOKABLE void previous();
0053     /// Go to the currentDate.
0054     Q_INVOKABLE void goToday();
0055 
0056     // QAbstractItemModel overrides
0057     QHash<int, QByteArray> roleNames() const override;
0058     QVariant data(const QModelIndex &index, int role) const override;
0059     int rowCount(const QModelIndex &parent) const override;
0060 
0061     Q_INVOKABLE static QString monthName(int month);
0062 
0063 Q_SIGNALS:
0064     void yearChanged();
0065     void monthChanged();
0066     void selectedChanged();
0067 
0068 private:
0069     struct Private;
0070     QLocale m_locale;
0071     std::unique_ptr<Private> d;
0072 };