summaryrefslogtreecommitdiffabout
path: root/korganizer/datenavigator.cpp
blob: b0eac5157a1c3da1c3502600f8bb2696e47f5a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
    This file is part of KOrganizer.
    Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#include "datenavigator.h"

#include "koglobals.h"

#include <kcalendarsystem.h>

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>

using namespace KCal;

DateNavigator::DateNavigator( QObject *parent, const char *name , KOViewManager * v)
  : QObject( parent, name )
{
    mViewManager = v;
    mSelectedDates.append( QDate::currentDate() );
}

DateNavigator::~DateNavigator()
{
}

void DateNavigator::slotMonthSelect( int m )
{
    QDate firstSelected = mSelectedDates.first();
    int weekDay = firstSelected.dayOfWeek();
    int diff  = m - firstSelected.month() ;
    firstSelected = KOGlobals::self()->calendarSystem()->addMonths( firstSelected, diff );
    
    if ( mSelectedDates.first().day() == 1 && mSelectedDates.count() > 27 )
      selectMonthByDate( firstSelected );
  else
      selectWeekByDay( weekDay, firstSelected );
    
}

void DateNavigator::slotDaySelect( QDate d )
{
    QDate firstSelected = mSelectedDates.first();
    int weekDay = firstSelected.dayOfWeek();
    //int diff  = firstSelected.daysTo(  d  ) ;
    firstSelected = firstSelected.addDays( firstSelected .daysTo(  d  ) );
    
    selectWeekByDay( weekDay, firstSelected );
    
}

void DateNavigator::selectMonthByDate(  const QDate & firstSelected )
{
 
    int monthDay = firstSelected.day(); 
    QDate date = firstSelected.addDays( 1-monthDay );
    selectDates( date , date.daysInMonth ());
    
}
void DateNavigator::selectMonth()
{
 
    QDate date =mSelectedDates.first();
    selectMonthByDate( date );
}
void DateNavigator::selectMonthFromMonthview()
{
 
    QDate date =mSelectedDates.first().addDays( 7 );
    selectMonthByDate( date );
}

DateList DateNavigator::selectedDates()
{
  return mSelectedDates;
}

int DateNavigator::datesCount() const
{
  return mSelectedDates.count();
}

void DateNavigator::selectDates( const DateList& dateList )
{
  if (dateList.count() > 0) {
    mSelectedDates = dateList;
    emitSelected();
  }
}

void DateNavigator::selectDate( const QDate &date )
{
  QDate d = date;

  if ( !d.isValid() ) {
    d = QDate::currentDate();
  }

  mSelectedDates.clear();
  mSelectedDates.append( d );

  emitSelected();
}

void DateNavigator::selectDates( int count )
{

    QDate d = mSelectedDates.first();
    selectDates( d, count );
}

void DateNavigator::selectDates( const QDate &d, int count )
{
  DateList dates;

  int i;
  for( i = 0; i < count; ++i ) {
    dates.append( d.addDays( i ) );
  }
  
  mSelectedDates = dates;
  
  emitSelected();
}

void DateNavigator::selectWeekByDay( int weekDay, const QDate &d )
{
    //  qDebug("selectWeekByDay(  %d %s ", weekDay, d.toString().latin1());
  int dateCount = mSelectedDates.count();
  bool weekStart = ( weekDay == KGlobal::locale()->weekStartDay() );
  if ( weekDay == 1 && dateCount == 5 ) selectWorkWeek( d );
  else if ( weekStart && dateCount == 7 ) selectWeek( d );
  else selectDates( d, dateCount );
}

void DateNavigator::selectWeek()
{
    QDate d = mSelectedDates.first();
    selectWeek( d );
}
void DateNavigator::selectWeek( int num )
{
    int year = mSelectedDates.first().year();
    QDate d =  QDate ( year, 1,1);
    while ( d.dayOfWeek() != 4 )
        d = d.addDays( 1 );
    selectWeek( d.addDays ( (num-1) *7 ) );
}
void DateNavigator::selectWeekFromMonthView( int num )
{
    int year = mSelectedDates.first().year();
    if ( mSelectedDates.first().dayOfYear() > 300 && num < 10 )
        ++year;
    if ( mSelectedDates.first().dayOfYear() < 70 && num > 40 )
        --year;
    QDate d =  QDate ( year, 1,1);
    while ( d.dayOfWeek() != 4 )
        d = d.addDays( 1 );
    selectWeek( d.addDays ( (num-1) *7 ) );
}
void DateNavigator::selectWeek( const QDate &d )
{
  int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );

  int weekStart = KGlobal::locale()->weekStartDay();
  QDate firstDate = d.addDays( weekStart - dayOfWeek );

  if ( weekStart != 1 && dayOfWeek < weekStart ) {
      firstDate = firstDate.addDays(-7 );
  }


  selectDates( firstDate, 7 );
}

void DateNavigator::selectWorkWeek()
{ 
    QDate d = mSelectedDates.first();
  selectWorkWeek( d );
}

void DateNavigator::selectWorkWeek( const QDate &d )
{
  int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );

  QDate firstDate = d.addDays( 1 - dayOfWeek );

  int weekStart = KGlobal::locale()->weekStartDay();
  if ( weekStart != 1 && dayOfWeek >= weekStart ) {
    firstDate = firstDate.addDays( 7 );
  }

  selectDates( firstDate, 5 );
}

void DateNavigator::selectTodayMonth()
{
    QDate date = QDate::currentDate().addDays( 1-QDate::currentDate().day() );
    selectDates( date , date.daysInMonth ());

}
void DateNavigator::selectToday()
{
  QDate d = QDate::currentDate();

  int dateCount = mSelectedDates.count();
  if ( dateCount == 5 && d.dayOfWeek() < 6 ) selectWorkWeek( d );
  else if ( dateCount == 7 ) selectWeek( d );
  else selectDates( d, dateCount );
}

void DateNavigator::selectPreviousYear()
{
  QDate firstSelected = mSelectedDates.first();
  int weekDay = firstSelected.dayOfWeek();
  firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, -1 );

  selectWeekByDay( weekDay, firstSelected );
}

void DateNavigator::selectPreviousMonth()
{
  QDate firstSelected = mSelectedDates.first();
  int weekDay = firstSelected.dayOfWeek();
  firstSelected = KOGlobals::self()->calendarSystem()->addMonths( firstSelected, -1 );

  
  if ( mSelectedDates.first().day() == 1 && mSelectedDates.count() > 27 )
      selectMonthByDate( firstSelected );
  else
      selectWeekByDay( weekDay, firstSelected ); 
}

void DateNavigator::selectNextMonth()
{
  QDate firstSelected = mSelectedDates.first();
  int weekDay = firstSelected.dayOfWeek();

  firstSelected = KOGlobals::self()->calendarSystem()->addMonths( firstSelected, 1 );
 
  if ( mSelectedDates.first().day() == 1 && mSelectedDates.count() > 27 )
      selectMonthByDate( firstSelected );
  else
      selectWeekByDay( weekDay, firstSelected ); 
 
}
void DateNavigator::selectPreviousWeek()
{
  selectDates( mSelectedDates.first().addDays( -7 ), datesCount() );
}

void DateNavigator::selectNextWeek()
{
  selectDates( mSelectedDates.first().addDays( 7 ), datesCount() );
}

void DateNavigator::selectNextYear()
{
  QDate firstSelected = mSelectedDates.first();
  int weekDay = firstSelected.dayOfWeek();
  firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, 1 );

  selectWeekByDay( weekDay, firstSelected );
}

void DateNavigator::selectPrevious()
{
  int offset = -7;
  if ( datesCount() == 1 ) {
    offset = -1;
  }
 if ( mViewManager )
      if ( mViewManager->showsNextDays() )
          offset = -datesCount();
  selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
}

void DateNavigator::selectNext()
{
  int offset = 7;
  if ( datesCount() == 1 ) {
    offset = 1;
  }
  if ( mViewManager )
      if ( mViewManager->showsNextDays() )
          offset = datesCount();
  selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
}

void DateNavigator::emitSelected()
{
  emit datesSelected( mSelectedDates );
}

//#include "datenavigator.moc"