• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

korganizer

  • sources
  • kde-4.12
  • kdepim
  • korganizer
datenavigator.cpp
Go to the documentation of this file.
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>
5 
6  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
7  Author: Sergio Martins <sergio@kdab.com>
8 
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.
13 
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.
18 
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.
22 
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.
26 */
27 
28 #include "datenavigator.h"
29 #include "koglobals.h"
30 
31 #include <KDebug>
32 #include <KCalendarSystem>
33 #include <KGlobal>
34 #include <KLocale>
35 
36 DateNavigator::DateNavigator( QObject *parent ) : QObject( parent )
37 {
38  mSelectedDates.append( QDate::currentDate() );
39 }
40 
41 DateNavigator::~DateNavigator()
42 {
43 }
44 
45 KCalCore::DateList DateNavigator::selectedDates()
46 {
47  return mSelectedDates;
48 }
49 
50 int DateNavigator::datesCount() const
51 {
52  return mSelectedDates.count();
53 }
54 
55 void DateNavigator::selectDates( const KCalCore::DateList &dateList, const QDate &preferredMonth )
56 {
57  if ( dateList.count() > 0 ) {
58  mSelectedDates = dateList;
59  emitSelected( preferredMonth );
60  }
61 }
62 
63 void DateNavigator::selectDate( const QDate &date )
64 {
65  QDate d = date;
66 
67  if ( !d.isValid() ) {
68  kDebug() << "an invalid date was passed as a parameter!";
69  d = QDate::currentDate();
70  }
71  mSelectedDates.clear();
72  mSelectedDates.append( d );
73 
74  emitSelected();
75 }
76 
77 void DateNavigator::selectDates( int count )
78 {
79  count = qMin( count, static_cast<int>( MAX_SELECTABLE_DAYS ) );
80  selectDates( mSelectedDates.first(), count );
81 }
82 
83 void DateNavigator::selectDates( const QDate &d, int count,
84  const QDate &preferredMonth )
85 {
86  KCalCore::DateList dates;
87 
88  int i;
89  for ( i = 0; i < count; ++i ) {
90  dates.append( d.addDays( i ) );
91  }
92 
93  mSelectedDates = dates;
94  emitSelected( preferredMonth );
95 }
96 
97 void DateNavigator::selectWeekByDay( int weekDay, const QDate &d, const QDate &preferredMonth )
98 {
99  int dateCount = mSelectedDates.count();
100  bool weekStart = ( weekDay == KGlobal::locale()->weekStartDay() );
101  if ( weekStart && dateCount == 7 ) {
102  selectWeek( d, preferredMonth );
103  } else {
104  selectDates( d, dateCount, preferredMonth );
105  }
106 }
107 
108 void DateNavigator::selectWeek()
109 {
110  selectWeek( mSelectedDates.first() );
111 }
112 
113 void DateNavigator::selectWeek( const QDate &d, const QDate &preferredMonth )
114 {
115  const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
116  const int weekStart = KGlobal::locale()->weekStartDay();
117 
118  QDate firstDate = d.addDays( weekStart - dayOfWeek );
119 
120  if ( weekStart != 1 && dayOfWeek < weekStart ) {
121  firstDate = firstDate.addDays( -7 );
122  }
123 
124  selectDates( firstDate, 7, preferredMonth );
125 }
126 
127 void DateNavigator::selectWorkWeek()
128 {
129  selectWorkWeek( mSelectedDates.first() );
130 }
131 
132 void DateNavigator::selectWorkWeek( const QDate &d )
133 {
134  const int weekStart = KGlobal::locale()->weekStartDay();
135  const int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
136  QDate currentDate = d.addDays( weekStart - dayOfWeek );
137 
138  if ( weekStart != 1 && dayOfWeek < weekStart ) {
139  currentDate = currentDate.addDays( -7 );
140  }
141 
142  mSelectedDates.clear();
143  const int mask = KOGlobals::self()->getWorkWeekMask();
144 
145  for ( int i = 0; i < 7; ++i ) {
146  if ( ( 1 << ( ( i + weekStart + 6 ) % 7 ) ) & (mask) ) {
147  mSelectedDates.append( currentDate.addDays( i ) );
148  }
149  }
150 
151  emitSelected();
152 }
153 
154 void DateNavigator::selectToday()
155 {
156  QDate d = QDate::currentDate();
157 
158  int dateCount = mSelectedDates.count();
159 
160  if ( dateCount == 7 ) {
161  selectWeek( d );
162  } else if ( dateCount == 5 ) {
163  selectWorkWeek( d );
164  } else {
165  selectDates( d, dateCount );
166  }
167 }
168 
169 void DateNavigator::selectPreviousYear()
170 {
171  QDate firstSelected = mSelectedDates.first();
172  int weekDay = firstSelected.dayOfWeek();
173  firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, -1 );
174 
175  selectWeekByDay( weekDay, firstSelected );
176 }
177 
178 void DateNavigator::selectPreviousMonth( const QDate &currentMonth,
179  const QDate &selectionLowerLimit,
180  const QDate &selectionUpperLimit )
181 {
182  shiftMonth( currentMonth,
183  selectionLowerLimit,
184  selectionUpperLimit,
185  -1 );
186 }
187 
188 void DateNavigator::selectPreviousWeek()
189 {
190  QDate firstSelected = mSelectedDates.first();
191  const int weekDay = firstSelected.dayOfWeek();
192  firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, -7 );
193 
194  selectWeekByDay( weekDay, firstSelected );
195 }
196 
197 void DateNavigator::selectNextWeek()
198 {
199  QDate firstSelected = mSelectedDates.first();
200  const int weekDay = firstSelected.dayOfWeek();
201 
202  firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, 7 );
203 
204  selectWeekByDay( weekDay, firstSelected );
205 }
206 
207 void DateNavigator::shiftMonth( const QDate &currentMonth,
208  const QDate &selectionLowerLimit,
209  const QDate &selectionUpperLimit,
210  int offset )
211 {
212  const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
213 
214  QDate firstSelected = mSelectedDates.first();
215  const int weekDay = firstSelected.dayOfWeek();
216  firstSelected = calSys->addMonths( firstSelected, offset );
217 
218  /* Don't trust firstSelected to calculate the nextMonth. firstSelected
219  can belong to a month other than currentMonth because KDateNavigator
220  displays 7*6 days. firstSelected should only be used for selection
221  purposes */
222  const QDate nextMonth = currentMonth.isValid() ?
223  calSys->addMonths( currentMonth, offset ) : firstSelected;
224 
225  /* When firstSelected doesn't belong to currentMonth it can happen
226  that the new selection won't be visible on our KDateNavigators
227  so we must adjust it */
228  if ( selectionLowerLimit.isValid() &&
229  firstSelected < selectionLowerLimit ) {
230  firstSelected = selectionLowerLimit;
231  } else if ( selectionUpperLimit.isValid() &&
232  firstSelected > selectionUpperLimit ) {
233  firstSelected = selectionUpperLimit.addDays( -6 );
234  }
235 
236  selectWeekByDay( weekDay, firstSelected, nextMonth );
237 }
238 
239 void DateNavigator::selectNextMonth( const QDate &currentMonth,
240  const QDate &selectionLowerLimit,
241  const QDate &selectionUpperLimit )
242 {
243  shiftMonth( currentMonth,
244  selectionLowerLimit,
245  selectionUpperLimit,
246  1 );
247 }
248 
249 void DateNavigator::selectNextYear()
250 {
251  QDate firstSelected = mSelectedDates.first();
252  int weekDay = firstSelected.dayOfWeek();
253  firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, 1 );
254 
255  selectWeekByDay( weekDay, firstSelected );
256 }
257 
258 void DateNavigator::selectPrevious()
259 {
260  int offset = -7;
261  if ( datesCount() == 1 ) {
262  offset = -1;
263  }
264 
265  selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
266 }
267 
268 void DateNavigator::selectNext()
269 {
270  int offset = 7;
271  if ( datesCount() == 1 ) {
272  offset = 1;
273  }
274 
275  selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
276 }
277 
278 void DateNavigator::selectMonth( int month )
279 {
280  const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
281 
282  // always display starting at the first week of the specified month
283  QDate firstSelected = QDate( mSelectedDates.first().year(), month, 1 );
284 
285  int day = calSys->day( firstSelected );
286  calSys->setDate( firstSelected, calSys->year( firstSelected ), month, 1 );
287  int days = calSys->daysInMonth( firstSelected );
288  // As day we use either the selected date, or if the month has less days
289  // than that, we use the max day of that month
290  if ( day > days ) {
291  day = days;
292  }
293  QDate requestedMonth;
294  calSys->setDate( firstSelected, calSys->year( firstSelected ), month, day );
295  calSys->setDate( requestedMonth, calSys->year( firstSelected ), month, 1 );
296 
297  selectWeekByDay( 1, firstSelected, requestedMonth );
298 }
299 
300 void DateNavigator::selectYear( int year )
301 {
302  QDate firstSelected = mSelectedDates.first();
303  const int deltaYear = year - KOGlobals::self()->calendarSystem()->year( firstSelected );
304  firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, deltaYear );
305 
306  const int weekDay = firstSelected.dayOfWeek();
307  selectWeekByDay( weekDay, firstSelected );
308 }
309 
310 void DateNavigator::emitSelected( const QDate &preferredMonth )
311 {
312  emit datesSelected( mSelectedDates, preferredMonth );
313 }
314 
315 #include "datenavigator.moc"
koglobals.h
DateNavigator::selectToday
void selectToday()
Definition: datenavigator.cpp:154
DateNavigator::selectDate
void selectDate(const QDate &)
Definition: datenavigator.cpp:63
DateNavigator::selectMonth
void selectMonth(int month)
Definition: datenavigator.cpp:278
DateNavigator::selectPreviousYear
void selectPreviousYear()
Definition: datenavigator.cpp:169
QObject
DateNavigator::selectNextWeek
void selectNextWeek()
Definition: datenavigator.cpp:197
datenavigator.h
DateNavigator::selectWeekByDay
void selectWeekByDay(int weekDay, const QDate &, const QDate &preferredMonth=QDate())
Definition: datenavigator.cpp:97
DateNavigator::selectWeek
void selectWeek()
Definition: datenavigator.cpp:108
DateNavigator::selectDates
void selectDates(const KCalCore::DateList &, const QDate &preferredMonth=QDate())
Definition: datenavigator.cpp:55
DateNavigator::selectedDates
KCalCore::DateList selectedDates()
Definition: datenavigator.cpp:45
DateNavigator::selectPreviousWeek
void selectPreviousWeek()
Definition: datenavigator.cpp:188
DateNavigator::selectPreviousMonth
void selectPreviousMonth(const QDate &currentMonth=QDate(), const QDate &selectionLowerLimit=QDate(), const QDate &selectionUpperLimit=QDate())
Definition: datenavigator.cpp:178
DateNavigator::selectPrevious
void selectPrevious()
Definition: datenavigator.cpp:258
DateNavigator::selectNextMonth
void selectNextMonth(const QDate &currentMonth=QDate(), const QDate &selectionLowerLimit=QDate(), const QDate &selectionUpperLimit=QDate())
Definition: datenavigator.cpp:239
KOGlobals::calendarSystem
const KCalendarSystem * calendarSystem() const
Definition: koglobals.cpp:65
DateNavigator::datesCount
int datesCount() const
Definition: datenavigator.cpp:50
KOGlobals::self
static KOGlobals * self()
Definition: koglobals.cpp:43
DateNavigator::~DateNavigator
~DateNavigator()
Definition: datenavigator.cpp:41
DateNavigator::DateNavigator
DateNavigator(QObject *parent=0)
Definition: datenavigator.cpp:36
DateNavigator::emitSelected
void emitSelected(const QDate &preferredMonth=QDate())
Definition: datenavigator.cpp:310
DateNavigator::datesSelected
void datesSelected(const KCalCore::DateList &, const QDate &preferredMonth)
DateNavigator::selectYear
void selectYear(int year)
Definition: datenavigator.cpp:300
DateNavigator::selectNext
void selectNext()
Definition: datenavigator.cpp:268
KOGlobals::getWorkWeekMask
int getWorkWeekMask()
Definition: koglobals.cpp:125
DateNavigator::selectWorkWeek
void selectWorkWeek()
Definition: datenavigator.cpp:127
DateNavigator::selectNextYear
void selectNextYear()
Definition: datenavigator.cpp:249
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

korganizer

Skip menu "korganizer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal