Diffstat (limited to 'core/pim/datebook/datebookweeklst.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/pim/datebook/datebookweeklst.cpp | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/core/pim/datebook/datebookweeklst.cpp b/core/pim/datebook/datebookweeklst.cpp new file mode 100644 index 0000000..05e36be --- a/dev/null +++ b/core/pim/datebook/datebookweeklst.cpp | |||
@@ -0,0 +1,314 @@ | |||
1 | #include "datebookweeklst.h" | ||
2 | |||
3 | #include "datebookweekheaderimpl.h" | ||
4 | |||
5 | #include <qpe/calendar.h> | ||
6 | #include <qpe/datebookdb.h> | ||
7 | #include <qpe/event.h> | ||
8 | #include <qpe/qpeapplication.h> | ||
9 | #include <qpe/timestring.h> | ||
10 | #include <qpe/datebookmonth.h> | ||
11 | |||
12 | #include <qdatetime.h> | ||
13 | #include <qheader.h> | ||
14 | #include <qlabel.h> | ||
15 | #include <qlayout.h> | ||
16 | #include <qpainter.h> | ||
17 | #include <qpopupmenu.h> | ||
18 | #include <qtimer.h> | ||
19 | #include <qstyle.h> | ||
20 | #include <qtoolbutton.h> | ||
21 | #include <qvbox.h> | ||
22 | #include <qsizepolicy.h> | ||
23 | #include <qabstractlayout.h> | ||
24 | #include <qtl.h> | ||
25 | |||
26 | bool calcWeek(const QDate &d, int &week, int &year, | ||
27 | bool startOnMonday = false); | ||
28 | |||
29 | DateBookWeekLstHeader::DateBookWeekLstHeader(bool onM, QWidget* parent, | ||
30 | const char* name, WFlags fl) | ||
31 | : DateBookWeekLstHeaderBase(parent, name, fl) | ||
32 | { | ||
33 | setBackgroundMode( PaletteButton ); | ||
34 | labelDate->setBackgroundMode( PaletteButton ); | ||
35 | labelWeek->setBackgroundMode( PaletteButton ); | ||
36 | forward->setBackgroundMode( PaletteButton ); | ||
37 | back->setBackgroundMode( PaletteButton ); | ||
38 | DateBookWeekLstHeaderBaseLayout->setSpacing(0); | ||
39 | DateBookWeekLstHeaderBaseLayout->setMargin(0); | ||
40 | //setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding)); | ||
41 | setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed)); | ||
42 | |||
43 | connect(back, SIGNAL(clicked()), this, SLOT(prevWeek())); | ||
44 | connect(forward, SIGNAL(clicked()), this, SLOT(nextWeek())); | ||
45 | connect(labelWeek, SIGNAL(clicked()), this, SLOT(pickDate())); | ||
46 | onMonday=onM; | ||
47 | } | ||
48 | DateBookWeekLstHeader::~DateBookWeekLstHeader(){} | ||
49 | void DateBookWeekLstHeader::setDate(QDate d) { | ||
50 | date=d; | ||
51 | |||
52 | int year,week; | ||
53 | calcWeek(d,week,year,onMonday); | ||
54 | labelWeek->setText("W: " + QString::number(week)); | ||
55 | |||
56 | QDate start=date; | ||
57 | QDate stop=start.addDays(6); | ||
58 | labelDate->setText( QString::number(start.day()) + " " + | ||
59 | start.monthName(start.month()) + " - " + | ||
60 | QString::number(stop.day()) + " " + | ||
61 | start.monthName(stop.month()) ); | ||
62 | emit dateChanged(year,week); | ||
63 | } | ||
64 | void DateBookWeekLstHeader::pickDate() { | ||
65 | static QPopupMenu *m1 = 0; | ||
66 | static DateBookMonth *picker = 0; | ||
67 | if ( !m1 ) { | ||
68 | m1 = new QPopupMenu( this ); | ||
69 | picker = new DateBookMonth( m1, 0, TRUE ); | ||
70 | m1->insertItem( picker ); | ||
71 | connect( picker, SIGNAL( dateClicked( int, int, int ) ), | ||
72 | this, SLOT( setDate( int, int, int ) ) ); | ||
73 | //connect( m1, SIGNAL( aboutToHide() ), | ||
74 | //this, SLOT( gotHide() ) ); | ||
75 | } | ||
76 | picker->setDate( date.year(), date.month(), date.day() ); | ||
77 | m1->popup(mapToGlobal(labelWeek->pos()+QPoint(0,labelWeek->height()))); | ||
78 | picker->setFocus(); | ||
79 | } | ||
80 | void DateBookWeekLstHeader::setDate(int y, int m, int d) { | ||
81 | QDate new_date(y,m,d); | ||
82 | setDate(new_date); | ||
83 | } | ||
84 | |||
85 | void DateBookWeekLstHeader::nextWeek() { | ||
86 | setDate(date.addDays(7)); | ||
87 | } | ||
88 | void DateBookWeekLstHeader::prevWeek() { | ||
89 | setDate(date.addDays(-7)); | ||
90 | } | ||
91 | |||
92 | DateBookWeekLstDayHdr::DateBookWeekLstDayHdr(const QDate &d, bool onM, | ||
93 | QWidget* parent = 0, | ||
94 | const char* name = 0, | ||
95 | WFlags fl = 0 ) | ||
96 | : DateBookWeekLstDayHdrBase(parent, name, fl) { | ||
97 | |||
98 | date=d; | ||
99 | |||
100 | static const char *wdays="MTWTFSS"; | ||
101 | char day=wdays[d.dayOfWeek()-1]; | ||
102 | |||
103 | label->setText( QString(QChar(day)) + " " + | ||
104 | QString::number(d.day()) ); | ||
105 | add->setText("+"); | ||
106 | |||
107 | if (d == QDate::currentDate()) { | ||
108 | QPalette pal=label->palette(); | ||
109 | pal.setColor(QColorGroup::Foreground, QColor(0,0,255)); | ||
110 | label->setPalette(pal); | ||
111 | |||
112 | /* | ||
113 | QFont f=label->font(); | ||
114 | f.setItalic(true); | ||
115 | label->setFont(f); | ||
116 | label->setPalette(QPalette(QColor(0,0,255),label->backgroundColor())); | ||
117 | */ | ||
118 | } else if (d.dayOfWeek() == 7) { // FIXME: Match any holiday | ||
119 | QPalette pal=label->palette(); | ||
120 | pal.setColor(QColorGroup::Foreground, QColor(255,0,0)); | ||
121 | label->setPalette(pal); | ||
122 | } | ||
123 | |||
124 | |||
125 | connect (label, SIGNAL(clicked()), this, SLOT(showDay())); | ||
126 | connect (add, SIGNAL(clicked()), this, SLOT(newEvent())); | ||
127 | } | ||
128 | |||
129 | void DateBookWeekLstDayHdr::showDay() { | ||
130 | emit showDate(date.year(), date.month(), date.day()); | ||
131 | } | ||
132 | void DateBookWeekLstDayHdr::newEvent() { | ||
133 | QDateTime start, stop; | ||
134 | start=stop=date; | ||
135 | start.setTime(QTime(10,0)); | ||
136 | stop.setTime(QTime(12,0)); | ||
137 | |||
138 | emit addEvent(start,stop,""); | ||
139 | } | ||
140 | DateBookWeekLstEvent::DateBookWeekLstEvent(const EffectiveEvent &ev, | ||
141 | QWidget* parent = 0, | ||
142 | const char* name = 0, | ||
143 | WFlags fl = 0) : | ||
144 | ClickableLabel(parent,name,fl), | ||
145 | event(ev) | ||
146 | { | ||
147 | char s[10]; | ||
148 | if ( ev.startDate() != ev.date() ) { // multiday event (not first day) | ||
149 | if ( ev.endDate() == ev.date() ) { // last day | ||
150 | strcpy(s, "__|__"); | ||
151 | } else { | ||
152 | strcpy(s, " |---"); | ||
153 | } | ||
154 | } else { | ||
155 | sprintf(s,"%.2d:%.2d",ev.start().hour(),ev.start().minute()); | ||
156 | } | ||
157 | setText(QString(s) + " " + ev.description()); | ||
158 | connect(this, SIGNAL(clicked()), this, SLOT(editMe())); | ||
159 | } | ||
160 | void DateBookWeekLstEvent::editMe() { | ||
161 | emit editEvent(event.event()); | ||
162 | } | ||
163 | |||
164 | |||
165 | DateBookWeekLstView::DateBookWeekLstView(QValueList<EffectiveEvent> &ev, | ||
166 | QDate &d, bool onM, | ||
167 | QWidget* parent, | ||
168 | const char* name, WFlags fl) | ||
169 | : QWidget( parent, name, fl ) | ||
170 | { | ||
171 | onMonday=onM; | ||
172 | setPalette(white); | ||
173 | setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding)); | ||
174 | |||
175 | QVBoxLayout *layout = new QVBoxLayout( this ); | ||
176 | |||
177 | qBubbleSort(ev); | ||
178 | QValueListIterator<EffectiveEvent> it; | ||
179 | it=ev.begin(); | ||
180 | |||
181 | int dayOrder[7]; | ||
182 | if (onMonday) | ||
183 | for (int d=0; d<7; d++) dayOrder[d]=d+1; | ||
184 | else { | ||
185 | for (int d=0; d<7; d++) dayOrder[d]=d; | ||
186 | dayOrder[0]=7; | ||
187 | } | ||
188 | |||
189 | for (int i=0; i<7; i++) { | ||
190 | // Header | ||
191 | DateBookWeekLstDayHdr *hdr=new DateBookWeekLstDayHdr(d.addDays(i), | ||
192 | onMonday,this); | ||
193 | connect(hdr, SIGNAL(showDate(int,int,int)), | ||
194 | this, SIGNAL(showDate(int,int,int))); | ||
195 | connect(hdr, SIGNAL(addEvent(const QDateTime &, | ||
196 | const QDateTime &, | ||
197 | const QString &)), | ||
198 | this, SIGNAL(addEvent(const QDateTime &, | ||
199 | const QDateTime &, | ||
200 | const QString &))); | ||
201 | layout->addWidget(hdr); | ||
202 | |||
203 | // Events | ||
204 | while ( (*it).date().dayOfWeek() == dayOrder[i] && it!=ev.end() ) { | ||
205 | DateBookWeekLstEvent *l=new DateBookWeekLstEvent(*it,this); | ||
206 | layout->addWidget(l); | ||
207 | connect (l, SIGNAL(editEvent(const Event &)), | ||
208 | this, SIGNAL(editEvent(const Event &))); | ||
209 | it++; | ||
210 | } | ||
211 | |||
212 | layout->addItem(new QSpacerItem(1,1, QSizePolicy::Minimum, QSizePolicy::Expanding)); | ||
213 | } | ||
214 | } | ||
215 | DateBookWeekLstView::~DateBookWeekLstView(){} | ||
216 | void DateBookWeekLstView::keyPressEvent(QKeyEvent *e) {e->ignore();} | ||
217 | |||
218 | DateBookWeekLst::DateBookWeekLst( bool ap, bool onM, DateBookDB *newDB, | ||
219 | QWidget *parent, | ||
220 | const char *name ) | ||
221 | : QWidget( parent, name ), | ||
222 | db( newDB ), | ||
223 | startTime( 0 ), | ||
224 | ampm( ap ), | ||
225 | onMonday(onM) | ||
226 | { | ||
227 | setFocusPolicy(StrongFocus); | ||
228 | layout = new QVBoxLayout( this ); | ||
229 | layout->setMargin(0); | ||
230 | |||
231 | header=new DateBookWeekLstHeader(onM, this); | ||
232 | layout->addWidget( header ); | ||
233 | connect(header, SIGNAL(dateChanged(int,int)), this, SLOT(dateChanged(int,int))); | ||
234 | |||
235 | scroll=new QScrollView(this); | ||
236 | //scroll->setVScrollBarMode(QScrollView::AlwaysOn); | ||
237 | //scroll->setHScrollBarMode(QScrollView::AlwaysOff); | ||
238 | scroll->setResizePolicy(QScrollView::AutoOneFit); | ||
239 | layout->addWidget(scroll); | ||
240 | |||
241 | view=NULL; | ||
242 | } | ||
243 | |||
244 | |||
245 | void DateBookWeekLst::setDate( QDate &d ) { | ||
246 | int w,y; | ||
247 | calcWeek(d,w,y,onMonday); | ||
248 | year=y; | ||
249 | _week=w; | ||
250 | header->setDate(date()); | ||
251 | } | ||
252 | void DateBookWeekLst::redraw() {getEvents();} | ||
253 | |||
254 | QDate DateBookWeekLst::date() const { | ||
255 | QDate d; | ||
256 | d.setYMD(year,1,1); | ||
257 | |||
258 | int dow= d.dayOfWeek(); | ||
259 | if (!onMonday) | ||
260 | if (dow==7) dow=1; | ||
261 | else dow++; | ||
262 | |||
263 | d=d.addDays( (_week-1)*7 - dow + 1 ); | ||
264 | return d; | ||
265 | } | ||
266 | |||
267 | void DateBookWeekLst::getEvents() { | ||
268 | QDate start = date(); | ||
269 | QDate stop = start.addDays(6); | ||
270 | QValueList<EffectiveEvent> el = db->getEffectiveEvents(start, stop); | ||
271 | |||
272 | if (view) delete view; | ||
273 | view=new DateBookWeekLstView(el,start,onMonday,scroll); | ||
274 | |||
275 | connect (view, SIGNAL(editEvent(const Event &)), | ||
276 | this, SIGNAL(editEvent(const Event &))); | ||
277 | connect (view, SIGNAL(showDate(int,int,int)), | ||
278 | this, SIGNAL(showDate(int,int,int))); | ||
279 | connect (view, SIGNAL(addEvent(const QDateTime &, const QDateTime &, | ||
280 | const QString &)), | ||
281 | this, SIGNAL(addEvent(const QDateTime &, const QDateTime &, | ||
282 | const QString &))); | ||
283 | |||
284 | scroll->addChild(view); | ||
285 | view->show(); | ||
286 | scroll->updateScrollBars(); | ||
287 | } | ||
288 | |||
289 | void DateBookWeekLst::dateChanged(int y, int w) { | ||
290 | year=y; | ||
291 | _week=w; | ||
292 | getEvents(); | ||
293 | } | ||
294 | |||
295 | void DateBookWeekLst::keyPressEvent(QKeyEvent *e) | ||
296 | { | ||
297 | switch(e->key()) { | ||
298 | case Key_Up: | ||
299 | scroll->scrollBy(0, -20); | ||
300 | break; | ||
301 | case Key_Down: | ||
302 | scroll->scrollBy(0, 20); | ||
303 | break; | ||
304 | case Key_Left: | ||
305 | header->prevWeek(); | ||
306 | break; | ||
307 | case Key_Right: | ||
308 | header->nextWeek(); | ||
309 | break; | ||
310 | default: | ||
311 | e->ignore(); | ||
312 | } | ||
313 | } | ||
314 | |||