Commit b459ff95 authored by Chupligin Sergey's avatar Chupligin Sergey

[CalendarModel] Add fill model function

parent b2394a98
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
import QtQuick 2.6 import QtQuick 2.6
import QtQuick.Controls.Nemo 1.0 import QtQuick.Controls.Nemo 1.0
import Nemo.Models 1.0
Item { Item {
id: datePicker id: datePicker
...@@ -203,7 +205,7 @@ Item { ...@@ -203,7 +205,7 @@ Item {
cellWidth: width / 7 - 1 cellWidth: width / 7 - 1
cellHeight: width / 6 cellHeight: width / 6
model: dateModel model: calendarModel
delegate: Item{ delegate: Item{
id: dayCell id: dayCell
...@@ -263,138 +265,16 @@ Item { ...@@ -263,138 +265,16 @@ Item {
} }
} }
} }
Component.onCompleted: {
dateModel.currentDate = currentDate
dateModel.fillModel()
}
} }
onMonthChanged: { onMonthChanged: {
dateModel.selectedDate = datePicker.currentDate calendarModel.selectedDate = datePicker.currentDate
dateModel.clear() calendarModel.month = currentDate.getMonth()+1;
dateModel.fillModel() calendarModel.year = currentDate.getFullYear();
}
ListModel {
id: dateModel
signal monthChanged()
property int firstDayOffset: 0
property date selectedDate: new Date()
property date currentDate: new Date()
//public:
function setEvent(eventDate, enable) {
if (eventDate.getMonth() !== selectedDate.getMonth() && eventDate.getFullYear() !== selectedDate.getFullYear())
return
setProperty(eventDate.getDate() + firstDayOffset, "hasEventDay", enable)
}
function getMonthYearString() {
return Qt.formatDate(selectedDate, "MMMM yyyy")
}
function changeModel(_selectedDate) {
clear()
selectedDate = _selectedDate
fillModel()
monthChanged()
} }
function showNext() { CalendarModel{
showOtherMonth(selectedDate.getMonth() + 1) id: calendarModel
}
function showPrevious() {
showOtherMonth(selectedDate.getMonth() - 1)
}
//private:
function fillModel() {
firstDayOffset = getFirstDayOffset(selectedDate)
for(var i = 0; i < 6 * 7; ++i) {
var objectDate = selectedDate;
objectDate.setDate(selectedDate.getDate() - (selectedDate.getDate() - 1 + firstDayOffset - i))
appendDayObject(objectDate)
}
}
function appendDayObject(dateOfDay) {
append({
"dateOfDay" : dateOfDay,
"isCurrentDay" : dateOfDay.getDate() === currentDate.getDate() &&
dateOfDay.getMonth() === currentDate.getMonth() &&
dateOfDay.getFullYear() === currentDate.getFullYear(),
"isOtherMonthDay" : dateOfDay.getMonth() !== selectedDate.getMonth(),
"hasEventDay" : false
})
}
function showOtherMonth(month) {
var newDate = selectedDate
var currentDay = selectedDate.getDate()
currentDay = getValidDayByMonthAndDay(month, currentDay, isLeapYear(selectedDate.getFullYear()));
newDate.setMonth(month, currentDay)
changeModel(newDate)
}
function getFirstDayOffset(currentDate) {
var tmpDate = currentDate
tmpDate.setDate(currentDate.getDate() - (currentDate.getDate() - 1))
var firstDayWeekDay = tmpDate.getDay()
if (firstDayWeekDay === 0)
firstDayWeekDay = 6
else
firstDayWeekDay--
return firstDayWeekDay
}
function getValidDayByMonthAndDay(month, day, leapYear) {
if (month === 12)
month = 0
if (month === -1)
month = 11
if (month === 0 ||
month === 2 ||
month === 4 ||
month === 6 ||
month === 7 ||
month === 9 ||
month === 11)
return day
if (month !== 1) {
if (day < 31)
return day
return 30
}
if (day < 29)
return day
if (leapYear)
return 29
return 28
}
function isLeapYear(year) {
if(year % 4 === 0) {
if(year % 100 === 0) {
if(year % 400 === 0) {
return true;
}
else
return false;
}
else
return true;
}
return false;
}
} }
} }
...@@ -68,7 +68,7 @@ QVariant CalendarModel::data(const QModelIndex &index, int role) const ...@@ -68,7 +68,7 @@ QVariant CalendarModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
} }
dateItem item = m_dateList.at(index.row()); DateItem item = m_dateList.at(index.row());
switch (role) switch (role)
{ {
case Qt::UserRole: case Qt::UserRole:
...@@ -94,7 +94,7 @@ QVariant CalendarModel::get(const int idx) const ...@@ -94,7 +94,7 @@ QVariant CalendarModel::get(const int idx) const
} }
QMap<QString, QVariant> itemData; QMap<QString, QVariant> itemData;
dateItem item = m_dateList.at(idx); DateItem item = m_dateList.at(idx);
itemData.insert("isOtherMonthDay",item.isOtherMonthDay); itemData.insert("isOtherMonthDay",item.isOtherMonthDay);
itemData.insert("isCurrentDay",item.isCurrentDay); itemData.insert("isCurrentDay",item.isCurrentDay);
...@@ -136,5 +136,48 @@ void CalendarModel::setYear(int year) ...@@ -136,5 +136,48 @@ void CalendarModel::setYear(int year)
void CalendarModel::fill() void CalendarModel::fill()
{ {
m_dateList.clear();
QDate firstDayOfSelectedMonth = QDate(m_year,m_month,1);
int startWeekDay = firstDayOfSelectedMonth.dayOfWeek();
/*If first dayof moth not Monday add form preview month*/
if(startWeekDay != 1)
{
int needToAdd = 1-startWeekDay;
for(int n=needToAdd; n<0; n++)
{
m_dateList.append(createDateItem(firstDayOfSelectedMonth.addDays(n),true));
}
}
for(int n=0; n < firstDayOfSelectedMonth.daysInMonth();n++)
{
QDate date = firstDayOfSelectedMonth.addDays(n);
m_dateList.append(createDateItem(date,false,date == m_currentDate));
}
/*if last day of moth not Sunday add from next mont*/
QDate lastDayOfSelectedMonth = QDate(m_year,m_month,firstDayOfSelectedMonth.daysInMonth());
int endWeekDay = lastDayOfSelectedMonth.dayOfWeek();
if(endWeekDay != 7)
{
int needToAdd = 7-endWeekDay;
for(int n=1;n<=needToAdd;n++)
{
m_dateList.append(createDateItem(lastDayOfSelectedMonth.addDays(n),true));
}
}
dataChanged(createIndex(0, 0), createIndex(rowCount()-1, 0));
}
CalendarModel::DateItem CalendarModel::createDateItem(QDate dateOfDay, bool isOtherMonthDay, bool isCurrentDay, bool isSelectedDay, bool hasEventDay)
{
DateItem dateItem;
dateItem.dateOfDay = dateOfDay;
dateItem.hasEventDay = hasEventDay;
dateItem.isCurrentDay = isCurrentDay;
dateItem.isOtherMonthDay = isOtherMonthDay;
dateItem.isSelectedDay = isSelectedDay;
return dateItem;
} }
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
class CalendarModel : public QAbstractListModel class CalendarModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
struct dateItem{ struct DateItem{
bool isOtherMonthDay; bool isOtherMonthDay;
bool isCurrentDay; bool isCurrentDay;
bool isSelectedDay; bool isSelectedDay;
...@@ -76,9 +76,14 @@ signals: ...@@ -76,9 +76,14 @@ signals:
private: private:
QHash<int,QByteArray> m_hash; QHash<int,QByteArray> m_hash;
QList<dateItem> m_dateList; QList<DateItem> m_dateList;
void fill(); void fill();
DateItem createDateItem(QDate dateOfDay,
bool isOtherMonthDay = false,
bool isCurrentDay = false,
bool isSelectedDay = false,
bool hasEventDay = false);
QDate m_currentDate; QDate m_currentDate;
QDate m_selectedDate; QDate m_selectedDate;
......
TEMPLATE = lib TEMPLATE = lib
TARGET = nemomodelsplugin TARGET = nemomodels
PLUGIN_IMPORT_PATH = Nemo/Models
QT -= gui
QT += qml QT += qml
CONFIG += qt plugin hide_symbols CONFIG += qt plugin hide_symbols
SOURCES += \ SOURCES += \
...@@ -11,9 +15,13 @@ SOURCES += \ ...@@ -11,9 +15,13 @@ SOURCES += \
HEADERS += \ HEADERS += \
calendarmodel.h calendarmodel.h
target.path = /usr/lib/qt5/qml/org/nemomobile/models/ target.path = $$[QT_INSTALL_QML]/$$PLUGIN_IMPORT_PATH
qmlfiles.files =\
qmldir
qmlfiles.path = $$[QT_INSTALL_QML]/$$PLUGIN_IMPORT_PATH
INSTALLS += target INSTALLS += target qmlfiles
DISTFILES += \ DISTFILES += \
qmldir qmldir
...@@ -27,18 +27,19 @@ ...@@ -27,18 +27,19 @@
class Q_DECL_EXPORT NemoSettingsPlugin : public QQmlExtensionPlugin class Q_DECL_EXPORT NemoSettingsPlugin : public QQmlExtensionPlugin
{ {
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.nemomobile.models") Q_PLUGIN_METADATA(IID "Nemo.Models")
public: public:
virtual ~NemoSettingsPlugin() { } virtual ~NemoSettingsPlugin() { }
void initializeEngine(QQmlEngine *, const char *uri) void initializeEngine(QQmlEngine *, const char *uri)
{ {
Q_ASSERT(uri == QLatin1String("org.nemomobile.models")); Q_ASSERT(uri == QLatin1String("Nemo.Models") || uri == QLatin1String("org.nemomobile.models"));
} }
void registerTypes(const char *uri) void registerTypes(const char *uri)
{ {
Q_ASSERT(uri == QLatin1String("org.nemomobile.models")); Q_ASSERT(uri == QLatin1String("Nemo.Models") || uri == QLatin1String("org.nemomobile.models"));
qmlRegisterType<CalendarModel>(uri, 1, 0, "CalendarModel"); qmlRegisterType<CalendarModel>(uri, 1, 0, "CalendarModel");
} }
}; };
......
module org.nemomobile.models module Nemo.Models
plugin nemomodels plugin nemomodels
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