Split startDate property into today and displayedDate in calendar component
Basically splits the Calendar::m_startDate into 'today' and 'displayedDate', where displayedDate is the date that is displayed (it controls the days model etc) and can be manipulated by the user by eg. changing months in the plasmoid, and today is the current day, populated by our dataengine (which means it auto-updates with no need for a timer). This allows for greater flexibility and things like "Go back to today" when eg. the plasmoid is hidden or when the user have browsed too far in the calendar and just wants to get back to today (the button to do that pending). Also this fixes a problem where the time dataengine is being polled every 30secs for the clock and would reset the calendar view as the startDate is currently bound to the dataengine and the view resets on that change. REVIEW: 118668 CCBUG: 336304
This commit is contained in:
parent
bcde055673
commit
3188b417f4
@ -38,20 +38,49 @@ Calendar::Calendar(QObject *parent)
|
|||||||
// connect(m_dayHelper, SIGNAL(calendarChanged()), this, SLOT(updateData()));
|
// connect(m_dayHelper, SIGNAL(calendarChanged()), this, SLOT(updateData()));
|
||||||
}
|
}
|
||||||
|
|
||||||
QDate Calendar::startDate() const
|
QDate Calendar::displayedDate() const
|
||||||
{
|
{
|
||||||
return m_startDate;
|
return m_displayedDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calendar::setStartDate(const QDate &dateTime)
|
void Calendar::setDisplayedDate(const QDate &dateTime)
|
||||||
{
|
{
|
||||||
if (m_startDate == dateTime) {
|
if (m_displayedDate == dateTime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_startDate = dateTime;
|
m_displayedDate = dateTime;
|
||||||
// m_dayHelper->setDate(m_startDate.year(), m_startDate.month());
|
// m_dayHelper->setDate(m_displayedDate.year(), m_displayedDate.month());
|
||||||
updateData();
|
updateData();
|
||||||
emit startDateChanged();
|
emit displayedDateChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
QDate Calendar::today() const
|
||||||
|
{
|
||||||
|
return m_today;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Calendar::setToday(const QDate &dateTime)
|
||||||
|
{
|
||||||
|
if (dateTime == m_today) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_today = dateTime;
|
||||||
|
if (m_displayedDate.isNull()) {
|
||||||
|
resetToToday();
|
||||||
|
} else {
|
||||||
|
// the else is to prevent calling updateData() twice
|
||||||
|
// if the resetToToday() was called
|
||||||
|
updateData();
|
||||||
|
}
|
||||||
|
emit todayChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Calendar::resetToToday()
|
||||||
|
{
|
||||||
|
m_displayedDate = m_today;
|
||||||
|
updateData();
|
||||||
|
emit displayedDateChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Calendar::types() const
|
int Calendar::types() const
|
||||||
@ -148,13 +177,13 @@ QString Calendar::monthName() const
|
|||||||
// look across locales, we need to capitalize it ourselves
|
// look across locales, we need to capitalize it ourselves
|
||||||
//
|
//
|
||||||
// see https://bugreports.qt-project.org/browse/QTBUG-35100
|
// see https://bugreports.qt-project.org/browse/QTBUG-35100
|
||||||
QString tmp = QDate::longMonthName(m_startDate.month(), QDate::StandaloneFormat);
|
QString tmp = QDate::longMonthName(m_displayedDate.month(), QDate::StandaloneFormat);
|
||||||
return tmp.left(1).toUpper() + tmp.mid(1);
|
return tmp.left(1).toUpper() + tmp.mid(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Calendar::year() const
|
int Calendar::year() const
|
||||||
{
|
{
|
||||||
return m_startDate.year();
|
return m_displayedDate.year();
|
||||||
}
|
}
|
||||||
|
|
||||||
QAbstractListModel *Calendar::daysModel() const
|
QAbstractListModel *Calendar::daysModel() const
|
||||||
@ -180,7 +209,7 @@ void Calendar::updateData()
|
|||||||
int daysBeforeCurrentMonth;
|
int daysBeforeCurrentMonth;
|
||||||
int daysAfterCurrentMonth;
|
int daysAfterCurrentMonth;
|
||||||
|
|
||||||
QDate firstDay(m_startDate.year(), m_startDate.month(), 1);
|
QDate firstDay(m_displayedDate.year(), m_displayedDate.month(), 1);
|
||||||
|
|
||||||
// If the first day is the same as the starting day then we add a complete row before it.
|
// If the first day is the same as the starting day then we add a complete row before it.
|
||||||
if (m_firstDayOfWeek < firstDay.dayOfWeek()) {
|
if (m_firstDayOfWeek < firstDay.dayOfWeek()) {
|
||||||
@ -189,14 +218,14 @@ void Calendar::updateData()
|
|||||||
daysBeforeCurrentMonth = days() - (m_firstDayOfWeek - firstDay.dayOfWeek());
|
daysBeforeCurrentMonth = days() - (m_firstDayOfWeek - firstDay.dayOfWeek());
|
||||||
}
|
}
|
||||||
|
|
||||||
int daysThusFar = daysBeforeCurrentMonth + m_startDate.daysInMonth();
|
int daysThusFar = daysBeforeCurrentMonth + m_displayedDate.daysInMonth();
|
||||||
if (daysThusFar < totalDays) {
|
if (daysThusFar < totalDays) {
|
||||||
daysAfterCurrentMonth = totalDays - daysThusFar;
|
daysAfterCurrentMonth = totalDays - daysThusFar;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (daysBeforeCurrentMonth > 0) {
|
if (daysBeforeCurrentMonth > 0) {
|
||||||
QDate previousMonth = m_startDate.addMonths(-1);
|
QDate previousMonth = m_displayedDate.addMonths(-1);
|
||||||
//QDate previousMonth(m_startDate.year(), m_startDate.month() - 1, 1);
|
//QDate previousMonth(m_displayedDate.year(), m_displayedDate.month() - 1, 1);
|
||||||
for (int i = 0; i < daysBeforeCurrentMonth; i++) {
|
for (int i = 0; i < daysBeforeCurrentMonth; i++) {
|
||||||
DayData day;
|
DayData day;
|
||||||
day.isCurrentMonth = false;
|
day.isCurrentMonth = false;
|
||||||
@ -210,15 +239,15 @@ void Calendar::updateData()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < m_startDate.daysInMonth(); i++) {
|
for (int i = 0; i < m_displayedDate.daysInMonth(); i++) {
|
||||||
DayData day;
|
DayData day;
|
||||||
day.isCurrentMonth = true;
|
day.isCurrentMonth = true;
|
||||||
day.isNextMonth = false;
|
day.isNextMonth = false;
|
||||||
day.isPreviousMonth = false;
|
day.isPreviousMonth = false;
|
||||||
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
|
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
|
||||||
// day.containsEventItems = m_dayHelper->containsEventItems(i + 1);
|
// day.containsEventItems = m_dayHelper->containsEventItems(i + 1);
|
||||||
day.monthNumber = m_startDate.month();
|
day.monthNumber = m_displayedDate.month();
|
||||||
day.yearNumber = m_startDate.year();
|
day.yearNumber = m_displayedDate.year();
|
||||||
m_dayList << day;
|
m_dayList << day;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -231,8 +260,8 @@ void Calendar::updateData()
|
|||||||
day.isPreviousMonth = false;
|
day.isPreviousMonth = false;
|
||||||
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
|
day.dayNumber = i + 1; // +1 to go form 0 based index to 1 based calendar dates
|
||||||
// day.containsEventItems = false;
|
// day.containsEventItems = false;
|
||||||
day.monthNumber = m_startDate.addMonths(1).month();
|
day.monthNumber = m_displayedDate.addMonths(1).month();
|
||||||
day.yearNumber = m_startDate.addMonths(1).year();
|
day.yearNumber = m_displayedDate.addMonths(1).year();
|
||||||
m_dayList << day;
|
m_dayList << day;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,46 +276,46 @@ void Calendar::updateData()
|
|||||||
m_daysModel->update();
|
m_daysModel->update();
|
||||||
|
|
||||||
// qDebug() << "---------------------------------------------------------------";
|
// qDebug() << "---------------------------------------------------------------";
|
||||||
// qDebug() << "Date obj: " << m_startDate;
|
// qDebug() << "Date obj: " << m_displayedDate;
|
||||||
// qDebug() << "Month: " << m_startDate.month();
|
// qDebug() << "Month: " << m_displayedDate.month();
|
||||||
// qDebug() << "m_days: " << m_days;
|
// qDebug() << "m_days: " << m_days;
|
||||||
// qDebug() << "m_weeks: " << m_weeks;
|
// qDebug() << "m_weeks: " << m_weeks;
|
||||||
// qDebug() << "Days before this month: " << daysBeforeCurrentMonth;
|
// qDebug() << "Days before this month: " << daysBeforeCurrentMonth;
|
||||||
// qDebug() << "Days after this month: " << daysAfterCurrentMonth;
|
// qDebug() << "Days after this month: " << daysAfterCurrentMonth;
|
||||||
// qDebug() << "Days in current month: " << m_startDate.daysInMonth();
|
// qDebug() << "Days in current month: " << m_displayedDate.daysInMonth();
|
||||||
// qDebug() << "m_dayList size: " << m_dayList.count();
|
// qDebug() << "m_dayList size: " << m_dayList.count();
|
||||||
// qDebug() << "---------------------------------------------------------------";
|
// qDebug() << "---------------------------------------------------------------";
|
||||||
}
|
}
|
||||||
void Calendar::nextYear()
|
void Calendar::nextYear()
|
||||||
{
|
{
|
||||||
m_startDate = m_startDate.addYears(1);
|
m_displayedDate = m_displayedDate.addYears(1);
|
||||||
updateData();
|
updateData();
|
||||||
emit startDateChanged();
|
emit displayedDateChanged();
|
||||||
emit yearChanged();
|
emit yearChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calendar::previousYear()
|
void Calendar::previousYear()
|
||||||
{
|
{
|
||||||
m_startDate = m_startDate.addYears(-1);
|
m_displayedDate = m_displayedDate.addYears(-1);
|
||||||
updateData();
|
updateData();
|
||||||
emit startDateChanged();
|
emit displayedDateChanged();
|
||||||
emit yearChanged();
|
emit yearChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calendar::nextMonth()
|
void Calendar::nextMonth()
|
||||||
{
|
{
|
||||||
m_startDate = m_startDate.addMonths(1);
|
m_displayedDate = m_displayedDate.addMonths(1);
|
||||||
updateData();
|
updateData();
|
||||||
emit startDateChanged();
|
emit displayedDateChanged();
|
||||||
emit monthNameChanged();
|
emit monthNameChanged();
|
||||||
emit yearChanged();
|
emit yearChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calendar::previousMonth()
|
void Calendar::previousMonth()
|
||||||
{
|
{
|
||||||
m_startDate = m_startDate.addMonths(-1);
|
m_displayedDate = m_displayedDate.addMonths(-1);
|
||||||
updateData();
|
updateData();
|
||||||
emit startDateChanged();
|
emit displayedDateChanged();
|
||||||
emit monthNameChanged();
|
emit monthNameChanged();
|
||||||
emit yearChanged();
|
emit yearChanged();
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,16 @@ class Calendar : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
/**
|
/**
|
||||||
* This property is used to determine which data from which month to show. One should
|
* This property is used to determine which data from which month to show, it ensures
|
||||||
* set it's Year, Month and Day although the Day is of less importance. Set it using
|
* the day passed in the QDate is visible
|
||||||
* YYYY-MM-DD format.
|
|
||||||
*/
|
*/
|
||||||
Q_PROPERTY(QDate startDate READ startDate WRITE setStartDate NOTIFY startDateChanged)
|
Q_PROPERTY(QDate displayedDate READ displayedDate WRITE setDisplayedDate NOTIFY displayedDateChanged)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This property is used to determine which data from which month to show, it ensures
|
||||||
|
* the day passed in the QDate is visible
|
||||||
|
*/
|
||||||
|
Q_PROPERTY(QDate today READ today WRITE setToday NOTIFY todayChanged)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This determines which kind of data types should be contained in
|
* This determines which kind of data types should be contained in
|
||||||
@ -125,9 +130,13 @@ public:
|
|||||||
|
|
||||||
explicit Calendar(QObject *parent = 0);
|
explicit Calendar(QObject *parent = 0);
|
||||||
|
|
||||||
// Start date
|
// Displayed date
|
||||||
QDate startDate() const;
|
QDate displayedDate() const;
|
||||||
void setStartDate(const QDate &dateTime);
|
void setDisplayedDate(const QDate &dateTime);
|
||||||
|
|
||||||
|
// The day that represents "today"
|
||||||
|
QDate today() const;
|
||||||
|
void setToday(const QDate &dateTime);
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
int types() const;
|
int types() const;
|
||||||
@ -163,9 +172,11 @@ public:
|
|||||||
Q_INVOKABLE void previousYear();
|
Q_INVOKABLE void previousYear();
|
||||||
Q_INVOKABLE QString dayName(int weekday) const;
|
Q_INVOKABLE QString dayName(int weekday) const;
|
||||||
Q_INVOKABLE int currentWeek() const;
|
Q_INVOKABLE int currentWeek() const;
|
||||||
|
Q_INVOKABLE void resetToToday();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void startDateChanged();
|
void displayedDateChanged();
|
||||||
|
void todayChanged();
|
||||||
void typesChanged();
|
void typesChanged();
|
||||||
void daysChanged();
|
void daysChanged();
|
||||||
void weeksChanged();
|
void weeksChanged();
|
||||||
@ -178,7 +189,8 @@ public Q_SLOTS:
|
|||||||
void updateData();
|
void updateData();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QDate m_startDate;
|
QDate m_displayedDate;
|
||||||
|
QDate m_today;
|
||||||
Types m_types;
|
Types m_types;
|
||||||
QList<DayData> m_dayList;
|
QList<DayData> m_dayList;
|
||||||
DaysModel *m_daysModel;
|
DaysModel *m_daysModel;
|
||||||
|
@ -26,53 +26,53 @@ PlasmaComponents.Menu {
|
|||||||
|
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(0))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(0))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 0, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 0, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(1))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(1))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 1, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 1, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(2))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(2))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 2, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 2, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(3))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(3))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 3, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 3, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(4))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(4))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 4, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 4, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(5))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(5))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 5, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 5, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(6))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(6))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 6, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 6, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(7))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(7))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 7, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 7, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(8))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(8))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 8, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 8, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(9))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(9))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 9, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 9, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(10))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(10))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 10, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 10, 1)
|
||||||
}
|
}
|
||||||
PlasmaComponents.MenuItem {
|
PlasmaComponents.MenuItem {
|
||||||
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(11))
|
text: capitalizeFirstLetter(Qt.locale().standaloneMonthName(11))
|
||||||
onClicked: monthCalendar.startDate = new Date(today.getFullYear(), 11, 1)
|
onClicked: monthCalendar.displayedDate = new Date(today.getFullYear(), 11, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because some locales don't have it in standaloneMonthNames,
|
// Because some locales don't have it in standaloneMonthNames,
|
||||||
|
@ -41,7 +41,6 @@ Item {
|
|||||||
property int mHeight: theme.mSize(theme.defaultFont).height
|
property int mHeight: theme.mSize(theme.defaultFont).height
|
||||||
property int borderWidth: 1
|
property int borderWidth: 1
|
||||||
|
|
||||||
property alias startDate: monthCalendar.startDate
|
|
||||||
|
|
||||||
property int columns: monthCalendar.days
|
property int columns: monthCalendar.days
|
||||||
property int rows: monthCalendar.weeks
|
property int rows: monthCalendar.weeks
|
||||||
@ -93,6 +92,10 @@ Item {
|
|||||||
return Qt.formatDate(d, "dddd dd MMM yyyy");
|
return Qt.formatDate(d, "dddd dd MMM yyyy");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resetToToday() {
|
||||||
|
monthCalendar.resetToToday();
|
||||||
|
}
|
||||||
|
|
||||||
PlasmaExtras.Heading {
|
PlasmaExtras.Heading {
|
||||||
id: monthHeading
|
id: monthHeading
|
||||||
|
|
||||||
@ -105,7 +108,7 @@ Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
level: 1
|
level: 1
|
||||||
text: root.startDate.getFullYear() == new Date().getFullYear() ? root.selectedMonth : root.selectedMonth + ", " + root.selectedYear
|
text: monthCalendar.displayedDate.getFullYear() == new Date().getFullYear() ? root.selectedMonth : root.selectedMonth + ", " + root.selectedYear
|
||||||
elide: Text.ElideRight
|
elide: Text.ElideRight
|
||||||
|
|
||||||
Loader {
|
Loader {
|
||||||
@ -135,7 +138,6 @@ Item {
|
|||||||
days: 7
|
days: 7
|
||||||
weeks: 6
|
weeks: 6
|
||||||
firstDayOfWeek: Qt.locale().firstDayOfWeek
|
firstDayOfWeek: Qt.locale().firstDayOfWeek
|
||||||
startDate: today;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DaysCalendar {
|
DaysCalendar {
|
||||||
@ -192,6 +194,7 @@ Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
today: root.today;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user