3
3
Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4
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>
6
9
This program is free software; you can redistribute it and/or modify
7
10
it under the terms of the GNU General Public License as published by
8
11
the Free Software Foundation; either version 2 of the License, or
51
54
return mSelectedDates.count();
54
void DateNavigator::selectDates( const DateList &dateList )
57
void DateNavigator::selectDates( const DateList &dateList, const QDate &preferredMonth )
56
59
if ( dateList.count() > 0 ) {
57
60
mSelectedDates = dateList;
61
emitSelected( preferredMonth );
77
79
void DateNavigator::selectDates( int count )
81
count = qMin( count, static_cast<int>( MAX_SELECTABLE_DAYS ) );
79
82
selectDates( mSelectedDates.first(), count );
82
void DateNavigator::selectDates( const QDate &d, int count )
85
void DateNavigator::selectDates( const QDate &d, int count,
86
const QDate &preferredMonth )
91
95
mSelectedDates = dates;
96
emitSelected( preferredMonth );
95
void DateNavigator::selectWeekByDay( int weekDay, const QDate &d )
99
void DateNavigator::selectWeekByDay( int weekDay, const QDate &d, const QDate &preferredMonth )
97
101
int dateCount = mSelectedDates.count();
98
102
bool weekStart = ( weekDay == KGlobal::locale()->weekStartDay() );
99
103
if ( weekStart && dateCount == 7 ) {
104
selectWeek( d, preferredMonth );
102
selectDates( d, dateCount );
106
selectDates( d, dateCount, preferredMonth );
108
112
selectWeek( mSelectedDates.first() );
111
void DateNavigator::selectWeek( const QDate &d )
115
void DateNavigator::selectWeek( const QDate &d, const QDate &preferredMonth )
113
int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
115
int weekStart = KGlobal::locale()->weekStartDay();
117
const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
118
const int weekStart = KGlobal::locale()->weekStartDay();
117
120
QDate firstDate = d.addDays( weekStart - dayOfWeek );
131
134
void DateNavigator::selectWorkWeek( const QDate &d )
133
int weekStart = KGlobal::locale()->weekStartDay();
134
int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
136
const int weekStart = KGlobal::locale()->weekStartDay();
137
const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
135
138
QDate currentDate = d.addDays( weekStart - dayOfWeek );
137
140
if ( weekStart != 1 && dayOfWeek < weekStart ) {
174
177
selectWeekByDay( weekDay, firstSelected );
177
void DateNavigator::selectPreviousMonth()
180
void DateNavigator::selectPreviousMonth( const QDate ¤tMonth,
181
const QDate &selectionLowerLimit,
182
const QDate &selectionUpperLimit )
179
QDate firstSelected = mSelectedDates.first();
180
int weekDay = firstSelected.dayOfWeek();
181
firstSelected = KOGlobals::self()->calendarSystem()->addMonths( firstSelected, -1 );
183
selectWeekByDay( weekDay, firstSelected );
184
shiftMonth( currentMonth,
186
190
void DateNavigator::selectPreviousWeek()
188
192
QDate firstSelected = mSelectedDates.first();
189
int weekDay = firstSelected.dayOfWeek();
193
const int weekDay = firstSelected.dayOfWeek();
190
194
firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, -7 );
192
196
selectWeekByDay( weekDay, firstSelected );
195
199
void DateNavigator::selectNextWeek()
197
201
QDate firstSelected = mSelectedDates.first();
198
int weekDay = firstSelected.dayOfWeek();
202
const int weekDay = firstSelected.dayOfWeek();
200
204
firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, 7 );
202
206
selectWeekByDay( weekDay, firstSelected );
205
void DateNavigator::selectNextMonth()
209
void DateNavigator::shiftMonth( const QDate ¤tMonth,
210
const QDate &selectionLowerLimit,
211
const QDate &selectionUpperLimit,
214
const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
207
216
QDate firstSelected = mSelectedDates.first();
208
int weekDay = firstSelected.dayOfWeek();
210
firstSelected = KOGlobals::self()->calendarSystem()->addMonths( firstSelected, 1 );
212
selectWeekByDay( weekDay, firstSelected );
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
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 ¤tMonth,
242
const QDate &selectionLowerLimit,
243
const QDate &selectionUpperLimit )
245
shiftMonth( currentMonth,
215
251
void DateNavigator::selectNextYear()
244
280
void DateNavigator::selectMonth( int month )
282
const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
246
284
// always display starting at the first week of the specified month
248
285
QDate firstSelected = QDate( mSelectedDates.first().year(), month, 1 );
250
const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
251
287
int day = calSys->day( firstSelected );
252
288
calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, 1 );
253
289
int days = calSys->daysInMonth( firstSelected );
256
292
if ( day > days ) {
295
QDate requestedMonth;
259
296
calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, day );
297
calSys->setYMD( requestedMonth, calSys->year( firstSelected ), month, 1 );
261
selectWeekByDay( 1, firstSelected );
299
selectWeekByDay( 1, firstSelected, requestedMonth );
264
302
void DateNavigator::selectYear( int year )
266
304
QDate firstSelected = mSelectedDates.first();
267
int deltaYear = year - KOGlobals::self()->calendarSystem()->year( firstSelected );
305
const int deltaYear = year - KOGlobals::self()->calendarSystem()->year( firstSelected );
268
306
firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, deltaYear );
270
int weekDay = firstSelected.dayOfWeek();
308
const int weekDay = firstSelected.dayOfWeek();
271
309
selectWeekByDay( weekDay, firstSelected );
274
void DateNavigator::emitSelected()
312
void DateNavigator::emitSelected( const QDate &preferredMonth )
276
emit datesSelected( mSelectedDates );
314
emit datesSelected( mSelectedDates, preferredMonth );
279
317
#include "datenavigator.moc"