SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / korganizer / datenavigator.cpp
blobf95cd9e540c2908bb527cff0c96ea529af0e0b5f
1 /*
2 This file is part of KOrganizer.
3 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
7 Author: Sergio Martins <sergio@kdab.com>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 As a special exception, permission is given to link this program
24 with any edition of Qt, and distribute the resulting executable,
25 without including the source code for Qt in the source distribution.
28 #include "datenavigator.h"
29 #include "koglobals.h"
31 #include "korganizer_debug.h"
32 #include <KCalendarSystem>
34 #include <QDate>
35 #include <KLocalizedString>
36 #include <QLocale>
38 DateNavigator::DateNavigator(QObject *parent) : QObject(parent)
40 mSelectedDates.append(QDate::currentDate());
43 DateNavigator::~DateNavigator()
47 KCalCore::DateList DateNavigator::selectedDates()
49 return mSelectedDates;
52 int DateNavigator::datesCount() const
54 return mSelectedDates.count();
57 void DateNavigator::selectDates(const KCalCore::DateList &dateList, const QDate &preferredMonth)
59 if (!dateList.isEmpty()) {
60 mSelectedDates = dateList;
61 emitSelected(preferredMonth);
65 void DateNavigator::selectDate(const QDate &date)
67 QDate d = date;
69 if (!d.isValid()) {
70 qCDebug(KORGANIZER_LOG) << "an invalid date was passed as a parameter!";
71 d = QDate::currentDate();
73 mSelectedDates.clear();
74 mSelectedDates.append(d);
76 emitSelected();
79 void DateNavigator::selectDates(int count)
81 count = qMin(count, static_cast<int>(MAX_SELECTABLE_DAYS));
82 selectDates(mSelectedDates.first(), count);
85 void DateNavigator::selectDates(const QDate &d, int count,
86 const QDate &preferredMonth)
88 KCalCore::DateList dates;
89 dates.reserve(count);
91 for (int i = 0; i < count; ++i) {
92 dates.append(d.addDays(i));
95 mSelectedDates = dates;
96 emitSelected(preferredMonth);
99 void DateNavigator::selectWeekByDay(int weekDay, const QDate &d, const QDate &preferredMonth)
101 int dateCount = mSelectedDates.count();
102 bool weekStart = (weekDay == QLocale().firstDayOfWeek());
103 if (weekStart && dateCount == 7) {
104 selectWeek(d, preferredMonth);
105 } else {
106 selectDates(d, dateCount, preferredMonth);
110 void DateNavigator::selectWeek()
112 selectWeek(mSelectedDates.first());
115 void DateNavigator::selectWeek(const QDate &d, const QDate &preferredMonth)
117 const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek(d);
118 const int weekStart = QLocale().firstDayOfWeek();
120 QDate firstDate = d.addDays(weekStart - dayOfWeek);
122 if (weekStart != 1 && dayOfWeek < weekStart) {
123 firstDate = firstDate.addDays(-7);
126 selectDates(firstDate, 7, preferredMonth);
129 void DateNavigator::selectWorkWeek()
131 selectWorkWeek(mSelectedDates.first());
134 void DateNavigator::selectWorkWeek(const QDate &d)
136 const int weekStart = QLocale().firstDayOfWeek();
137 const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek(d);
138 QDate currentDate = d.addDays(weekStart - dayOfWeek);
140 if (weekStart != 1 && dayOfWeek < weekStart) {
141 currentDate = currentDate.addDays(-7);
144 mSelectedDates.clear();
145 const int mask = KOGlobals::self()->getWorkWeekMask();
147 for (int i = 0; i < 7; ++i) {
148 if ((1 << ((i + weekStart + 6) % 7)) & (mask)) {
149 mSelectedDates.append(currentDate.addDays(i));
153 emitSelected();
156 void DateNavigator::selectToday()
158 QDate d = QDate::currentDate();
160 int dateCount = mSelectedDates.count();
162 if (dateCount == 7) {
163 selectWeek(d);
164 } else if (dateCount == 5) {
165 selectWorkWeek(d);
166 } else {
167 selectDates(d, dateCount);
171 void DateNavigator::selectPreviousYear()
173 QDate firstSelected = mSelectedDates.first();
174 int weekDay = firstSelected.dayOfWeek();
175 firstSelected = KOGlobals::self()->calendarSystem()->addYears(firstSelected, -1);
177 selectWeekByDay(weekDay, firstSelected);
180 void DateNavigator::selectPreviousMonth(const QDate &currentMonth,
181 const QDate &selectionLowerLimit,
182 const QDate &selectionUpperLimit)
184 shiftMonth(currentMonth,
185 selectionLowerLimit,
186 selectionUpperLimit,
187 -1);
190 void DateNavigator::selectPreviousWeek()
192 QDate firstSelected = mSelectedDates.first();
193 const int weekDay = firstSelected.dayOfWeek();
194 firstSelected = KOGlobals::self()->calendarSystem()->addDays(firstSelected, -7);
196 selectWeekByDay(weekDay, firstSelected);
199 void DateNavigator::selectNextWeek()
201 QDate firstSelected = mSelectedDates.first();
202 const int weekDay = firstSelected.dayOfWeek();
204 firstSelected = KOGlobals::self()->calendarSystem()->addDays(firstSelected, 7);
206 selectWeekByDay(weekDay, firstSelected);
209 void DateNavigator::shiftMonth(const QDate &currentMonth,
210 const QDate &selectionLowerLimit,
211 const QDate &selectionUpperLimit,
212 int offset)
214 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
216 QDate firstSelected = mSelectedDates.first();
217 const int weekDay = firstSelected.dayOfWeek();
218 firstSelected = calSys->addMonths(firstSelected, offset);
220 /* Don't trust firstSelected to calculate the nextMonth. firstSelected
221 can belong to a month other than currentMonth because KDateNavigator
222 displays 7*6 days. firstSelected should only be used for selection
223 purposes */
224 const QDate nextMonth = currentMonth.isValid() ?
225 calSys->addMonths(currentMonth, offset) : firstSelected;
227 /* When firstSelected doesn't belong to currentMonth it can happen
228 that the new selection won't be visible on our KDateNavigators
229 so we must adjust it */
230 if (selectionLowerLimit.isValid() &&
231 firstSelected < selectionLowerLimit) {
232 firstSelected = selectionLowerLimit;
233 } else if (selectionUpperLimit.isValid() &&
234 firstSelected > selectionUpperLimit) {
235 firstSelected = selectionUpperLimit.addDays(-6);
238 selectWeekByDay(weekDay, firstSelected, nextMonth);
241 void DateNavigator::selectNextMonth(const QDate &currentMonth,
242 const QDate &selectionLowerLimit,
243 const QDate &selectionUpperLimit)
245 shiftMonth(currentMonth,
246 selectionLowerLimit,
247 selectionUpperLimit,
251 void DateNavigator::selectNextYear()
253 QDate firstSelected = mSelectedDates.first();
254 int weekDay = firstSelected.dayOfWeek();
255 firstSelected = KOGlobals::self()->calendarSystem()->addYears(firstSelected, 1);
257 selectWeekByDay(weekDay, firstSelected);
260 void DateNavigator::selectPrevious()
262 int offset = -7;
263 if (datesCount() == 1) {
264 offset = -1;
267 selectDates(mSelectedDates.first().addDays(offset), datesCount());
270 void DateNavigator::selectNext()
272 int offset = 7;
273 if (datesCount() == 1) {
274 offset = 1;
277 selectDates(mSelectedDates.first().addDays(offset), datesCount());
280 void DateNavigator::selectMonth(int month)
282 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
284 // always display starting at the first week of the specified month
285 QDate firstSelected = QDate(mSelectedDates.first().year(), month, 1);
287 int day = calSys->day(firstSelected);
288 calSys->setDate(firstSelected, calSys->year(firstSelected), month, 1);
289 int days = calSys->daysInMonth(firstSelected);
290 // As day we use either the selected date, or if the month has less days
291 // than that, we use the max day of that month
292 if (day > days) {
293 day = days;
295 QDate requestedMonth;
296 calSys->setDate(firstSelected, calSys->year(firstSelected), month, day);
297 calSys->setDate(requestedMonth, calSys->year(firstSelected), month, 1);
299 selectWeekByDay(1, firstSelected, requestedMonth);
302 void DateNavigator::selectYear(int year)
304 QDate firstSelected = mSelectedDates.first();
305 const int deltaYear = year - KOGlobals::self()->calendarSystem()->year(firstSelected);
306 firstSelected = KOGlobals::self()->calendarSystem()->addYears(firstSelected, deltaYear);
308 const int weekDay = firstSelected.dayOfWeek();
309 selectWeekByDay(weekDay, firstSelected);
312 void DateNavigator::emitSelected(const QDate &preferredMonth)
314 Q_EMIT datesSelected(mSelectedDates, preferredMonth);