| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtOrganizer module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 or version 3 as published by the Free |
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
| 22 | ** following information to ensure the GNU Lesser General Public License |
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 25 | ** |
| 26 | ** As a special exception, The Qt Company gives you certain additional |
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception |
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 29 | ** |
| 30 | ** $QT_END_LICENSE$ |
| 31 | ** |
| 32 | ****************************************************************************/ |
| 33 | |
| 34 | #include "qdeclarativeorganizerrecurrencerule_p.h" |
| 35 | |
| 36 | #include <QtQml/qqmlinfo.h> |
| 37 | |
| 38 | QTORGANIZER_USE_NAMESPACE |
| 39 | |
| 40 | QT_BEGIN_NAMESPACE |
| 41 | |
| 42 | /*! |
| 43 | \qmltype RecurrenceRule |
| 44 | \instantiates QDeclarativeOrganizerRecurrenceRule |
| 45 | \brief The RecurrenceRule element represents a rule by which a organizer item repeats. |
| 46 | \inqmlmodule QtOrganizer |
| 47 | \ingroup qml-organizer-main |
| 48 | */ |
| 49 | |
| 50 | /*! |
| 51 | \qmlsignal RecurrenceRule::onRecurrenceRuleChanged() |
| 52 | |
| 53 | This signal is emitted, when any of the RecurrenceRule's properties have been changed. |
| 54 | */ |
| 55 | |
| 56 | /*! |
| 57 | \internal |
| 58 | */ |
| 59 | QDeclarativeOrganizerRecurrenceRule::QDeclarativeOrganizerRecurrenceRule(QObject *parent) |
| 60 | : QObject(parent) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /*! |
| 65 | \qmlproperty enumeration RecurrenceRule::frequency |
| 66 | |
| 67 | This property holds the frequency with which the item recurs, the value can be one of: |
| 68 | \list |
| 69 | \li RecurrenceRule.Invalid - (default). |
| 70 | \li RecurrenceRule.Daily |
| 71 | \li RecurrenceRule.Weekly |
| 72 | \li RecurrenceRule.Monthly |
| 73 | \li RecurrenceRule.Yearly |
| 74 | \endlist |
| 75 | */ |
| 76 | void QDeclarativeOrganizerRecurrenceRule::setFrequency(Frequency freq) |
| 77 | { |
| 78 | if (freq != frequency()) { |
| 79 | m_rule.setFrequency(static_cast<QOrganizerRecurrenceRule::Frequency>(freq)); |
| 80 | emit recurrenceRuleChanged(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | QDeclarativeOrganizerRecurrenceRule::Frequency QDeclarativeOrganizerRecurrenceRule::frequency() const |
| 85 | { |
| 86 | return static_cast<Frequency>(m_rule.frequency()); |
| 87 | } |
| 88 | |
| 89 | /*! |
| 90 | \qmlproperty variant RecurrenceRule::limit |
| 91 | |
| 92 | This property holds the limit condition of the recurrence rule, the value can be a limit date |
| 93 | and time or a limit count. The default is no limit. |
| 94 | */ |
| 95 | void QDeclarativeOrganizerRecurrenceRule::setLimit(const QVariant &value) |
| 96 | { |
| 97 | if (!value.isValid() || |
| 98 | (static_cast<QMetaType::Type>(value.type()) == QMetaType::VoidStar && value.value<void *>() == 0)) { |
| 99 | if (m_rule.limitType() != QOrganizerRecurrenceRule::NoLimit) { |
| 100 | m_rule.clearLimit(); |
| 101 | emit recurrenceRuleChanged(); |
| 102 | } |
| 103 | } else if (value.type() == QVariant::DateTime) { |
| 104 | QDate v = value.toDateTime().toUTC().date(); |
| 105 | if (v != m_rule.limitDate()) { |
| 106 | m_rule.setLimit(v); |
| 107 | emit recurrenceRuleChanged(); |
| 108 | } |
| 109 | } else if (value.type() == QVariant::Date) { |
| 110 | QDate v = value.value<QDate>(); |
| 111 | if (v != m_rule.limitDate()) { |
| 112 | m_rule.setLimit(v); |
| 113 | emit recurrenceRuleChanged(); |
| 114 | } |
| 115 | } else if ((value.type() == QVariant::Int) || (value.type() == QVariant::Double)) { |
| 116 | int v = value.value<int>(); |
| 117 | if (v != m_rule.limitCount()) { |
| 118 | m_rule.setLimit(v); |
| 119 | emit recurrenceRuleChanged(); |
| 120 | } |
| 121 | } else { |
| 122 | // TODO throw an error event |
| 123 | qmlInfo(me: this) << tr(s: "Invalid recurrence rule limit; value ," ) << value << tr(s: ", did not match one of the types: date, integer or double" ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | QVariant QDeclarativeOrganizerRecurrenceRule::limit() const |
| 128 | { |
| 129 | if (m_rule.limitType() == QOrganizerRecurrenceRule::CountLimit) |
| 130 | return QVariant::fromValue(value: m_rule.limitCount()); |
| 131 | else if (m_rule.limitType() == QOrganizerRecurrenceRule::DateLimit) |
| 132 | return QVariant::fromValue(value: m_rule.limitDate()); |
| 133 | |
| 134 | //NoLimit |
| 135 | return QVariant(); |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | \qmlproperty int RecurrenceRule::interval |
| 140 | |
| 141 | This property holds the interval of recurrence. The default interval is 1. |
| 142 | */ |
| 143 | void QDeclarativeOrganizerRecurrenceRule::setInterval(int interval) |
| 144 | { |
| 145 | if (interval != m_rule.interval()) { |
| 146 | m_rule.setInterval(interval); |
| 147 | emit recurrenceRuleChanged(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int QDeclarativeOrganizerRecurrenceRule::interval() const |
| 152 | { |
| 153 | return m_rule.interval(); |
| 154 | } |
| 155 | |
| 156 | /*! |
| 157 | \qmlproperty list<variant> RecurrenceRule::daysOfWeek |
| 158 | |
| 159 | This property holds a list of the days of week that the item should recur on. |
| 160 | */ |
| 161 | void QDeclarativeOrganizerRecurrenceRule::setDaysOfWeek(const QVariantList &days) |
| 162 | { |
| 163 | QSet<Qt::DayOfWeek> saved; |
| 164 | foreach (const QVariant &day, days) |
| 165 | saved << static_cast<Qt::DayOfWeek>(day.value<int>()); |
| 166 | if (saved != m_rule.daysOfWeek()) { |
| 167 | m_rule.setDaysOfWeek(saved); |
| 168 | emit recurrenceRuleChanged(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | QVariantList QDeclarativeOrganizerRecurrenceRule::daysOfWeek() const |
| 173 | { |
| 174 | QVariantList days; |
| 175 | foreach (Qt::DayOfWeek day, m_rule.daysOfWeek()) |
| 176 | days << day; |
| 177 | return days; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | \qmlproperty list<variant> RecurrenceRule::daysOfMonth |
| 182 | |
| 183 | This property holds a list of the days of the month that the item should recur on. |
| 184 | */ |
| 185 | void QDeclarativeOrganizerRecurrenceRule::setDaysOfMonth(const QVariantList &days) |
| 186 | { |
| 187 | QSet<int> saved; |
| 188 | foreach (const QVariant &day, days) |
| 189 | saved << day.value<int>(); |
| 190 | if (saved != m_rule.daysOfMonth()) { |
| 191 | m_rule.setDaysOfMonth(saved); |
| 192 | emit recurrenceRuleChanged(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | QVariantList QDeclarativeOrganizerRecurrenceRule::daysOfMonth() const |
| 197 | { |
| 198 | QVariantList days; |
| 199 | foreach (int day, m_rule.daysOfMonth()) |
| 200 | days << day; |
| 201 | return days; |
| 202 | } |
| 203 | |
| 204 | /*! |
| 205 | \qmlproperty list<variant> RecurrenceRule::daysOfYear |
| 206 | |
| 207 | This property holds a list of the days of the year that the item should recur on. |
| 208 | */ |
| 209 | void QDeclarativeOrganizerRecurrenceRule::setDaysOfYear(const QVariantList &days) |
| 210 | { |
| 211 | QSet<int> saved; |
| 212 | foreach (const QVariant &day, days) |
| 213 | saved << day.value<int>(); |
| 214 | if (saved != m_rule.daysOfYear()) { |
| 215 | m_rule.setDaysOfYear(saved); |
| 216 | emit recurrenceRuleChanged(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | QVariantList QDeclarativeOrganizerRecurrenceRule::daysOfYear() const |
| 221 | { |
| 222 | QVariantList days; |
| 223 | foreach (int day, m_rule.daysOfYear()) |
| 224 | days << day; |
| 225 | return days; |
| 226 | } |
| 227 | |
| 228 | /*! |
| 229 | \qmlproperty list<int> RecurrenceRule::monthsOfYear |
| 230 | |
| 231 | This property holds a list of the months that the item should recur on, the list element value can be one of: |
| 232 | \list |
| 233 | \li RecurrenceRule.January |
| 234 | \li RecurrenceRule.February |
| 235 | \li RecurrenceRule.March |
| 236 | \li RecurrenceRule.April |
| 237 | \li RecurrenceRule.May |
| 238 | \li RecurrenceRule.June |
| 239 | \li RecurrenceRule.July |
| 240 | \li RecurrenceRule.August |
| 241 | \li RecurrenceRule.September |
| 242 | \li RecurrenceRule.October |
| 243 | \li RecurrenceRule.November |
| 244 | \li RecurrenceRule.December |
| 245 | \endlist |
| 246 | */ |
| 247 | void QDeclarativeOrganizerRecurrenceRule::setMonthsOfYear(const QVariantList &months) |
| 248 | { |
| 249 | QSet<QOrganizerRecurrenceRule::Month> saved; |
| 250 | foreach (const QVariant &day, months) |
| 251 | saved << static_cast<QOrganizerRecurrenceRule::Month>(day.value<int>()); |
| 252 | if (saved != m_rule.monthsOfYear()) { |
| 253 | m_rule.setMonthsOfYear(saved); |
| 254 | emit recurrenceRuleChanged(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | QVariantList QDeclarativeOrganizerRecurrenceRule::monthsOfYear() const |
| 259 | { |
| 260 | QVariantList ms; |
| 261 | foreach (int m, m_rule.monthsOfYear()) |
| 262 | ms << m; |
| 263 | return ms; |
| 264 | } |
| 265 | |
| 266 | /*! |
| 267 | \qmlproperty list<int> RecurrenceRule::weeksOfYear |
| 268 | |
| 269 | This property holds a list of the weeks of the year that the item should recur on. |
| 270 | */ |
| 271 | void QDeclarativeOrganizerRecurrenceRule::setWeeksOfYear(const QVariantList &weeks) |
| 272 | { |
| 273 | QSet<int> saved; |
| 274 | foreach (const QVariant &week, weeks) |
| 275 | saved << week.value<int>(); |
| 276 | if (saved != m_rule.weeksOfYear()) { |
| 277 | m_rule.setWeeksOfYear(saved); |
| 278 | emit recurrenceRuleChanged(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | QVariantList QDeclarativeOrganizerRecurrenceRule::weeksOfYear() const |
| 283 | { |
| 284 | QVariantList weeks; |
| 285 | foreach (int week, m_rule.weeksOfYear()) |
| 286 | weeks << week; |
| 287 | return weeks; |
| 288 | } |
| 289 | |
| 290 | /*! |
| 291 | \qmlproperty list<int> RecurrenceRule::positions |
| 292 | |
| 293 | This property holds the position-list of the recurrence rule. |
| 294 | */ |
| 295 | void QDeclarativeOrganizerRecurrenceRule::setPositions(const QVariantList &pos) |
| 296 | { |
| 297 | if (pos != positions()) { |
| 298 | QSet<int> saved; |
| 299 | foreach (const QVariant &p, pos) |
| 300 | saved << p.value<int>(); |
| 301 | m_rule.setPositions(saved); |
| 302 | emit recurrenceRuleChanged(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | QVariantList QDeclarativeOrganizerRecurrenceRule::positions() const |
| 307 | { |
| 308 | QVariantList pos; |
| 309 | foreach (int p, m_rule.positions()) |
| 310 | pos << p; |
| 311 | return pos; |
| 312 | } |
| 313 | |
| 314 | /*! |
| 315 | \qmlproperty enumeration RecurrenceRule::firstDayOfWeek |
| 316 | |
| 317 | This property holds the day that the week starts on. If not set, this is Monday. The value can be one of: |
| 318 | \list |
| 319 | \li Qt.Monday |
| 320 | \li Qt.Tuesday |
| 321 | \li Qt.Wednesday |
| 322 | \li Qt.Thursday |
| 323 | \li Qt.Friday |
| 324 | \li Qt.Saturday |
| 325 | \li Qt.Sunday |
| 326 | \endlist |
| 327 | */ |
| 328 | void QDeclarativeOrganizerRecurrenceRule::setFirstDayOfWeek(Qt::DayOfWeek day) |
| 329 | { |
| 330 | if (day != firstDayOfWeek()) { |
| 331 | m_rule.setFirstDayOfWeek(day); |
| 332 | emit recurrenceRuleChanged(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | Qt::DayOfWeek QDeclarativeOrganizerRecurrenceRule::firstDayOfWeek() const |
| 337 | { |
| 338 | return m_rule.firstDayOfWeek(); |
| 339 | } |
| 340 | |
| 341 | /*! |
| 342 | \internal |
| 343 | */ |
| 344 | QOrganizerRecurrenceRule QDeclarativeOrganizerRecurrenceRule::rule() const |
| 345 | { |
| 346 | return m_rule; |
| 347 | } |
| 348 | |
| 349 | /*! |
| 350 | \internal |
| 351 | */ |
| 352 | void QDeclarativeOrganizerRecurrenceRule::setRule(const QOrganizerRecurrenceRule &rule) |
| 353 | { |
| 354 | m_rule = rule; |
| 355 | } |
| 356 | |
| 357 | #include "moc_qdeclarativeorganizerrecurrencerule_p.cpp" |
| 358 | |
| 359 | QT_END_NAMESPACE |
| 360 | |