summaryrefslogtreecommitdiff
path: root/core/pim/datebook/datebookweekheaderimpl.cpp
blob: e7c720841cc0055b378e0e0ed21ff0020e12e3cf (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "datebookweekheaderimpl.h"
#include <qlabel.h>
#include <qspinbox.h>
#include <qdatetime.h>

/*
 *  Constructs a DateBookWeekHeader which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 */
DateBookWeekHeader::DateBookWeekHeader( bool startOnMonday, QWidget* parent,
					const char* name, WFlags fl )
    : DateBookWeekHeaderBase( parent, name, fl ),
      bStartOnMonday( startOnMonday )
{
    setBackgroundMode( PaletteButton );
    labelDate->setBackgroundMode( PaletteButton );
}

/*
 *  Destroys the object and frees any allocated resources
 */
DateBookWeekHeader::~DateBookWeekHeader()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 * public slot
 */
void DateBookWeekHeader::yearChanged( int y )
{
    setDate( y, week );
}
/*
 * public slot
 */
void DateBookWeekHeader::nextWeek()
{
    if ( week < 52 )
	week++;
    setDate( year, week );
}
/*
 * public slot
 */
void DateBookWeekHeader::prevWeek()
{
    if ( week > 1 )
	week--;
    setDate( year, week );
}
/*
 * public slot
 */
void DateBookWeekHeader::weekChanged( int w )
{
    setDate( year, w );
}

void DateBookWeekHeader::setDate( int y, int w )
{
    year = y;
    week = w;
    spinYear->setValue( y );
    spinWeek->setValue( w );

    QDate d = dateFromWeek( week, year, bStartOnMonday );

    QString s = QString::number( d.day() ) + ". " + d.monthName( d.month() )
		+ "-";
    d = d.addDays( 6 );
    s += QString::number( d.day() ) + ". " + d.monthName( d.month() );
    labelDate->setText( s );

    emit dateChanged( y, w );
}

void DateBookWeekHeader::setStartOfWeek( bool onMonday )
{
    bStartOnMonday = onMonday;
    setDate( year, week );
}

// dateFromWeek
// compute the date from the week in the year

QDate dateFromWeek( int week, int year, bool startOnMonday )
{
    QDate d;
    d.setYMD( year, 1, 1 );
    int dayOfWeek = d.dayOfWeek();
    if ( startOnMonday ) {
	if ( dayOfWeek <= 4 ) {
	    d = d.addDays( ( week - 1 ) * 7 - dayOfWeek + 1 );
	} else {
	    d = d.addDays( (week) * 7 - dayOfWeek + 1 );
	}
    } else {
	if ( dayOfWeek <= 4 || dayOfWeek == 7) {
	    d = d.addDays( ( week - 1 ) * 7 - dayOfWeek % 7 );
	} else {
	    d = d.addDays( ( week ) * 7 - dayOfWeek % 7 );
	}
    }
    return d;
}