25 #include "datenavigator.h"
27 #include "koglobals.h"
29 #include <kcalendarsystem.h>
32 #include <tdeglobal.h>
33 #include <tdelocale.h>
37 DateNavigator::DateNavigator( TQObject *parent,
const char *name )
38 : TQObject( parent, name )
40 mSelectedDates.append( TQDate::currentDate() );
43 DateNavigator::~DateNavigator()
47 DateList DateNavigator::selectedDates()
49 return mSelectedDates;
52 int DateNavigator::datesCount()
const
54 return mSelectedDates.count();
57 void DateNavigator::selectDates(
const DateList &dateList )
59 if ( dateList.count() > 0 ) {
60 mSelectedDates = dateList;
66 void DateNavigator::selectDate(
const TQDate &date )
71 kdDebug(5850) <<
"DateNavigator::selectDates(TQDate): an invalid date was passed as a parameter!" << endl;
72 d = TQDate::currentDate();
75 mSelectedDates.clear();
76 mSelectedDates.append( d );
81 void DateNavigator::selectDates(
int count )
83 selectDates( mSelectedDates.first(), count );
86 void DateNavigator::selectDates(
const TQDate &d,
int count,
const TQDate &preferredMonth )
88 if ( count > MAX_SELECTABLE_DAYS ) {
89 count = MAX_SELECTABLE_DAYS;
95 for( i = 0; i < count; ++i ) {
96 dates.append( d.addDays( i ) );
99 mSelectedDates = dates;
101 emitSelected( preferredMonth );
104 void DateNavigator::selectWeekByDay(
int weekDay,
const TQDate &d,
const TQDate &preferredMonth )
106 int dateCount = mSelectedDates.count();
107 bool weekStart = ( weekDay == TDEGlobal::locale()->weekStartDay() );
108 if ( weekStart && dateCount == 7 ) {
109 selectWeek( d, preferredMonth );
111 selectDates( d, dateCount, preferredMonth );
115 void DateNavigator::selectWeek()
117 selectWeek( mSelectedDates.first() );
120 void DateNavigator::selectWeek(
const TQDate &d,
const TQDate &preferredMonth )
122 int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
124 int weekStart = TDEGlobal::locale()->weekStartDay();
126 TQDate firstDate = d.addDays( weekStart - dayOfWeek );
128 if ( weekStart != 1 && dayOfWeek < weekStart ) {
129 firstDate = firstDate.addDays( -7 );
132 selectDates( firstDate, 7, preferredMonth );
135 void DateNavigator::selectWorkWeek()
137 selectWorkWeek( mSelectedDates.first() );
140 void DateNavigator::selectWorkWeek(
const TQDate &d )
142 int weekStart = TDEGlobal::locale()->weekStartDay();
144 int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
146 TQDate currentDate = d.addDays( weekStart - dayOfWeek );
148 if ( weekStart != 1 && dayOfWeek < weekStart ) {
149 currentDate = currentDate.addDays( -7 );
152 mSelectedDates.clear();
153 int mask = KOGlobals::self()->getWorkWeekMask();
155 for (
int i = 0; i < 7; ++i ) {
156 if( (1<< ((i + weekStart + 6) % 7)) & (mask) ) {
157 mSelectedDates.append( currentDate.addDays(i) );
164 void DateNavigator::selectToday()
166 TQDate d = TQDate::currentDate();
168 int dateCount = mSelectedDates.count();
170 if ( dateCount == 7 ) {
172 }
else if ( dateCount == 5 ) {
175 selectDates( d, dateCount );
179 void DateNavigator::selectPreviousYear()
181 TQDate firstSelected = mSelectedDates.first();
182 int weekDay = firstSelected.dayOfWeek();
183 firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, -1 );
185 selectWeekByDay( weekDay, firstSelected );
188 void DateNavigator::selectPreviousMonth(
const TQDate ¤tMonth,
189 const TQDate &selectionLowerLimit,
190 const TQDate &selectionUpperLimit )
192 shiftMonth( currentMonth,
198 void DateNavigator::selectPreviousWeek()
200 TQDate firstSelected = mSelectedDates.first();
201 int weekDay = firstSelected.dayOfWeek();
202 firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, -7 );
204 selectWeekByDay( weekDay, firstSelected );
207 void DateNavigator::selectNextWeek()
209 TQDate firstSelected = mSelectedDates.first();
210 int weekDay = firstSelected.dayOfWeek();
212 firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, 7 );
214 selectWeekByDay( weekDay, firstSelected );
217 void DateNavigator::shiftMonth(
const TQDate ¤tMonth,
218 const TQDate &selectionLowerLimit,
219 const TQDate &selectionUpperLimit,
222 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
224 TQDate firstSelected = mSelectedDates.first();
225 int weekDay = firstSelected.dayOfWeek();
226 firstSelected = calSys->addMonths( firstSelected, offset );
232 const TQDate nextMonth = currentMonth.isValid() ?
233 calSys->addMonths( currentMonth, offset ) : firstSelected;
238 if ( selectionLowerLimit.isValid() &&
239 firstSelected < selectionLowerLimit ) {
240 firstSelected = selectionLowerLimit;
241 }
else if ( selectionUpperLimit.isValid() &&
242 firstSelected > selectionUpperLimit ) {
243 firstSelected = selectionUpperLimit.addDays( -6 );
246 selectWeekByDay( weekDay, firstSelected, nextMonth );
249 void DateNavigator::selectNextMonth(
const TQDate ¤tMonth,
250 const TQDate &selectionLowerLimit,
251 const TQDate &selectionUpperLimit )
253 shiftMonth( currentMonth,
259 void DateNavigator::selectNextYear()
261 TQDate firstSelected = mSelectedDates.first();
262 int weekDay = firstSelected.dayOfWeek();
263 firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, 1 );
265 selectWeekByDay( weekDay, firstSelected );
268 void DateNavigator::selectPrevious()
271 if ( datesCount() == 1 ) {
275 selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
278 void DateNavigator::selectNext()
281 if ( datesCount() == 1 ) {
285 selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
288 void DateNavigator::selectMonth(
int month )
290 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
292 TQDate firstSelected = mSelectedDates.first();
293 int weekDay = firstSelected.dayOfWeek();
295 int day = calSys->day( firstSelected );
296 calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, 1 );
297 int days = calSys->daysInMonth( firstSelected );
303 TQDate requestedMonth;
304 calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, day );
305 calSys->setYMD( requestedMonth, calSys->year( firstSelected ), month, 1 );
307 selectWeekByDay( weekDay, firstSelected, requestedMonth );
310 void DateNavigator::selectYear(
int year )
312 TQDate firstSelected = mSelectedDates.first();
313 int deltaYear = year - KOGlobals::self()->calendarSystem()->year( firstSelected );
314 firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, deltaYear );
316 int weekDay = firstSelected.dayOfWeek();
317 selectWeekByDay( weekDay, firstSelected );
320 void DateNavigator::emitSelected(
const TQDate &preferredMonth )
322 emit datesSelected( mSelectedDates, preferredMonth );
325 #include "datenavigator.moc"