Commit d4d3ed1f authored by Nicolas Fella's avatar Nicolas Fella Committed by Dimitris Kardarakos

Replace Plasma calendar backend with a simple monthmodel

parent 51c23c16
Pipeline #6212 passed with stage
in 5 minutes and 16 seconds
......@@ -34,7 +34,6 @@ find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS DBus Core Quick
find_package(KF5Config)
find_package(KF5Kirigami2)
find_package(KF5Plasma)
find_package(KF5I18n)
find_package(KF5CalendarCore REQUIRED)
find_package(KF5DBusAddons CONFIG REQUIRED)
......
......@@ -12,12 +12,13 @@ set(calindori_SRCS
eventcontroller.cpp
todocontroller.cpp
incidencealarmsmodel.cpp
daysofmonthmodel.cpp
)
qt5_add_resources(RESOURCES resources.qrc)
add_executable(calindori ${calindori_SRCS} ${RESOURCES})
target_link_libraries(calindori Qt5::Core Qt5::Qml Qt5::Quick Qt5::Test Qt5::Svg KF5::ConfigCore KF5::Plasma KF5::I18n KF5::CalendarCore)
target_link_libraries(calindori Qt5::Core Qt5::Qml Qt5::Quick Qt5::Test Qt5::Svg KF5::ConfigCore KF5::I18n KF5::CalendarCore)
install(TARGETS calindori ${KF5_INSTALL_TARGETS_DEFAULT_ARGS})
......
......@@ -21,6 +21,7 @@ import QtQuick 2.0
import QtQuick.Controls 2.4 as Controls2
import QtQuick.Layouts 1.11
import org.kde.kirigami 2.0 as Kirigami
import org.kde.phone.calindori 0.1
/**
* Calendar component that displays:
......@@ -36,11 +37,11 @@ MonthView {
signal goToday
signal refresh
displayedYear: plasmaCalendar.year
displayedMonthName: plasmaCalendar.displayedDateMonthName
displayedYear: mm.year
displayedMonthName: Qt.locale(Qt.locale().uiLanguages[0]).monthName(mm.month-1)
selectedDayTodosCount: todosCount(selectedDate)
selectedDayEventsCount: eventsCount(selectedDate)
daysModel: plasmaCalendar.daysModel
daysModel: mm
Layout.preferredHeight: childrenRect.height
Layout.preferredWidth: childrenRect.width
......@@ -49,28 +50,15 @@ MonthView {
reloadSelectedDate();
}
onNextMonth: {
plasmaCalendar.displayedDate = new Date(plasmaCalendar.displayedDate.setMonth(plasmaCalendar.displayedDate.getMonth() + 1));
}
onPreviousMonth: {
plasmaCalendar.displayedDate = new Date(plasmaCalendar.displayedDate.setMonth(plasmaCalendar.displayedDate.getMonth() -1));
}
onNextMonth: mm.goNextMonth()
onGoToday: {
plasmaCalendar.displayedDate = root.currentDate;
}
// HACK: Added as a temporary model provider, to be replaced with a non-plasma dependant backend
CalendarBackend {
id: plasmaCalendar
onPreviousMonth: mm.goPreviousMonth()
property int displayedDateMonth: Qt.formatDate(plasmaCalendar.displayedDate,"MM")
property string displayedDateMonthName: Qt.locale(Qt.locale().uiLanguages[0]).monthName(displayedDateMonth-1)
onGoToday: mm.goCurrentMonth()
days: root.days
weeks: root.weeks
today: root.currentDate
DaysOfMonthModel {
Please register or sign in to reply
id: mm
year: 2019
month: 8
}
}
import QtQuick 2.0
import org.kde.plasma.calendar 2.0
Calendar {
id: calendarBackend
}
......@@ -46,8 +46,8 @@ Rectangle {
width: childrenRect.width
height: childrenRect.height
opacity:(dayButton.isCurrentDate || highlight ) ? 0.4 : 1
color: dayButton.isCurrentDate ? Kirigami.Theme.textColor : ( highlight ? Kirigami.Theme.selectionBackgroundColor : Kirigami.Theme.backgroundColor )
opacity:(isToday || highlight ) ? 0.4 : 1
color: isToday ? Kirigami.Theme.textColor : ( highlight ? Kirigami.Theme.selectionBackgroundColor : Kirigami.Theme.backgroundColor )
border.color: Kirigami.Theme.disabledTextColor
Item {
......@@ -77,9 +77,6 @@ Rectangle {
Controls2.ToolButton {
id: dayButton
property bool isCurrentDate: ( Qt.formatDate(dayDelegate.currentDate, "yyyy") == model.yearNumber ) && ( Qt.formatDate(dayDelegate.currentDate, "MM") == model.monthNumber ) && ( Qt.formatDate(dayDelegate.currentDate, "dd") == model.dayNumber )
property bool isCurrentMonth: model.monthNumber == Qt.formatDate(plasmaCalendar.displayedDate, "MM")
anchors.fill: parent
enabled: isCurrentMonth
......
/*
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "daysofmonthmodel.h"
#include <QDebug>
void DaysOfMonthModel::update()
{
beginResetModel();
m_dayList = {};
int totalDays = m_daysPerWeek * m_weeks;
int daysBeforeCurrentMonth = 0;
int daysAfterCurrentMonth = 0;
QDate firstDay(m_year, m_month, 1);
if (m_firstDayOfWeek < firstDay.dayOfWeek()) {
daysBeforeCurrentMonth = firstDay.dayOfWeek() - m_firstDayOfWeek;
} else {
daysBeforeCurrentMonth = 7 - (m_firstDayOfWeek - firstDay.dayOfWeek());
}
int daysThusFar = daysBeforeCurrentMonth + firstDay.daysInMonth();
if (daysThusFar < totalDays) {
daysAfterCurrentMonth = totalDays - daysThusFar;
}
if (daysBeforeCurrentMonth > 0) {
QDate previousMonth = firstDay.addMonths(-1);
for (int i = 0; i < daysBeforeCurrentMonth; i++) {
DayData day;
day.isCurrent = false;
day.dayNumber = previousMonth.daysInMonth() - (daysBeforeCurrentMonth - (i + 1));
day.monthNumber = previousMonth.month();
day.yearNumber = previousMonth.year();
m_dayList << day;
}
}
for (int i = 0; i < firstDay.daysInMonth(); i++) {
DayData day;
day.isCurrent = true;
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
day.monthNumber = firstDay.month();
day.yearNumber = firstDay.year();
m_dayList << day;
}
if (daysAfterCurrentMonth > 0) {
for (int i = 0; i < daysAfterCurrentMonth; i++) {
DayData day;
day.isCurrent = false;
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
day.monthNumber = firstDay.addMonths(1).month();
day.yearNumber = firstDay.addMonths(1).year();
m_dayList << day;
}
}
m_dayList[QDate::currentDate().day() + daysBeforeCurrentMonth - 1].isToday = QDate::currentDate().month() == m_month && QDate::currentDate().year() == m_year;
endResetModel();
}
QVariant DaysOfMonthModel::data(const QModelIndex& index, int role) const
{
int row = index.row();
switch (role) {
case CurrentMonthRole:
return m_dayList[row].isCurrent;
case DayNumberRole:
return m_dayList[row].dayNumber;
case MonthNumberRole:
return m_dayList[row].monthNumber;
case YearNumberRole:
return m_dayList[row].yearNumber;
case TodayRole:
return m_dayList[row].isToday;
default:
return QStringLiteral("Deadbeef");
}
}
QHash<int, QByteArray> DaysOfMonthModel::roleNames() const
{
return {
{CurrentMonthRole, "isCurrentMonth"},
{DayNumberRole, "dayNumber"},
{MonthNumberRole, "monthNumber"},
{YearNumberRole, "yearNumber"},
{TodayRole, "isToday"}
};
}
void DaysOfMonthModel::goNextMonth()
{
if (m_month == 12) {
m_month = 1;
m_year++;
} else {
m_month ++;
}
Q_EMIT yearChanged();
Q_EMIT monthChanged();
update();
}
void DaysOfMonthModel::goPreviousMonth()
{
if (m_month == 1) {
m_month = 12;
m_year--;
} else {
m_month--;
}
Q_EMIT yearChanged();
Q_EMIT monthChanged();
update();
}
void DaysOfMonthModel::goCurrentMonth()
{
m_year = QDate::currentDate().year();
m_month = QDate::currentDate().month();
Q_EMIT yearChanged();
Q_EMIT monthChanged();
update();
}
int DaysOfMonthModel::month() const
{
return m_month;
}
void DaysOfMonthModel::setMonth(int month)
{
if (m_month != month) {
m_month = month;
Q_EMIT monthChanged();
update();
}
}
void DaysOfMonthModel::setYear(int year)
{
if (m_year != year) {
m_year = year;
Q_EMIT yearChanged();
update();
}
}
int DaysOfMonthModel::year() const
{
return m_year;
}
int DaysOfMonthModel::daysPerWeek() const
{
return m_daysPerWeek;
}
void DaysOfMonthModel::setDaysPerWeek(int daysPerWeek)
{
if (m_daysPerWeek != daysPerWeek) {
m_daysPerWeek = daysPerWeek;
Q_EMIT daysPerWeekChanged();
update();
}
}
int DaysOfMonthModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return m_weeks * m_daysPerWeek;
}
int DaysOfMonthModel::weeks() const
{
return m_weeks;
}
void DaysOfMonthModel::setWeeks(int weeks)
{
if (m_weeks != weeks) {
m_weeks = weeks;
Q_EMIT weeksChanged();
update();
}
}
/*
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef DAYSOFMONTHMODEL_H
#define DAYSOFMONTHMODEL_H
#include <QAbstractListModel>
#include <QVector>
#include <QDate>
#include <QLocale>
struct DayData
{
bool isCurrent;
int dayNumber;
int monthNumber;
int yearNumber;
bool isToday = false;
};
class DaysOfMonthModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int year READ year WRITE setYear NOTIFY yearChanged)
Q_PROPERTY(int month READ month WRITE setMonth NOTIFY monthChanged)
Q_PROPERTY(int daysPerWeek READ daysPerWeek WRITE setDaysPerWeek NOTIFY daysPerWeekChanged)
Q_PROPERTY(int weeks READ weeks WRITE setWeeks NOTIFY weeksChanged)
public:
enum Roles {
CurrentMonthRole = Qt::UserRole + 1,
DayNumberRole,
MonthNumberRole,
YearNumberRole,
TodayRole
};
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex & index, int role) const override;
int rowCount(const QModelIndex & parent) const override;
int year() const;
void setYear(int year);
int month() const;
void setMonth(int month);
int daysPerWeek() const;
void setDaysPerWeek(int daysPerWeek);
int weeks() const;
void setWeeks(int weeks);
Q_INVOKABLE void goNextMonth();
Q_INVOKABLE void goPreviousMonth();
Q_INVOKABLE void goCurrentMonth();
Q_INVOKABLE void update();
Q_SIGNALS:
void yearChanged();
void monthChanged();
void daysPerWeekChanged();
void weeksChanged();
private:
QVector<DayData> m_dayList;
int m_firstDayOfWeek = QLocale::system().firstDayOfWeek();
int m_year;
int m_month;
int m_daysPerWeek = 7;
int m_weeks = 6;
};
#endif // DAYSOFMONTHMODEL_H
......@@ -30,6 +30,7 @@
#include "eventcontroller.h"
#include "todocontroller.h"
#include "incidencealarmsmodel.h"
#include "daysofmonthmodel.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
......@@ -45,6 +46,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qmlRegisterType<EventController>("org.kde.phone.calindori",0,1,"EventController");
qmlRegisterType<TodoController>("org.kde.phone.calindori",0,1,"TodoController");
qmlRegisterType<IncidenceAlarmsModel>("org.kde.phone.calindori",0,1,"IncidenceAlarmsModel");
qmlRegisterType<DaysOfMonthModel>("org.kde.phone.calindori",0,1,"DaysOfMonthModel");
QQmlApplicationEngine engine;
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
......
<RCC>
<qresource prefix="/">
<file alias="CalendarBackend.qml">contents/ui/backends/CalendarBackend.qml</file>
<file alias="MonthView.qml">contents/ui/kirigami-playground/MonthView.qml</file>
<file alias="DayDelegate.qml">contents/ui/kirigami-playground/DayDelegate.qml</file>
<file alias="Main.qml">contents/ui/Main.qml</file>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment