1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * |
---|
4 | * This file is part of fosdem-schedule. |
---|
5 | * |
---|
6 | * fosdem-schedule is free software: you can redistribute it and/or modify it |
---|
7 | * under the terms of the GNU General Public License as published by the Free |
---|
8 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
9 | * any later version. |
---|
10 | * |
---|
11 | * fosdem-schedule is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
14 | * more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License along with |
---|
17 | * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | #include "delegate.h" |
---|
20 | #include "eventmodel.h" |
---|
21 | #include <track.h> |
---|
22 | |
---|
23 | #include <QDebug> |
---|
24 | #include <QPainter> |
---|
25 | |
---|
26 | #include "room.h" |
---|
27 | |
---|
28 | const int RADIUS = 10; |
---|
29 | const int SPACER = 10; |
---|
30 | |
---|
31 | const double scaleFactor1 = 0.4; |
---|
32 | const double scaleFactor2 = 0.8; |
---|
33 | |
---|
34 | Delegate::Delegate(QTreeView *aParent) |
---|
35 | : QItemDelegate(aParent) |
---|
36 | , mViewPtr(aParent) |
---|
37 | { |
---|
38 | mControls.clear(); |
---|
39 | defineControls(); |
---|
40 | } |
---|
41 | |
---|
42 | Delegate::~Delegate() |
---|
43 | { |
---|
44 | QListIterator<ControlId> i(mControls.keys()); |
---|
45 | while (i.hasNext()) |
---|
46 | { |
---|
47 | delete mControls[i.next()]->image(); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
---|
52 | { |
---|
53 | if(!mViewPtr) |
---|
54 | return; |
---|
55 | |
---|
56 | painter->save(); |
---|
57 | QColor bkgrColor = Qt::cyan; |
---|
58 | //QColor bkgrColor = QColor(0xAA,0xAA,0xAA); |
---|
59 | QColor conflictColor = Qt::yellow; |
---|
60 | |
---|
61 | QPen borderPen(bkgrColor.darker()); |
---|
62 | if(hasParent(index)) |
---|
63 | { |
---|
64 | // entry horisontal layout: |
---|
65 | // * spacer (aka y position of image) |
---|
66 | // * image |
---|
67 | // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track |
---|
68 | int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height(); |
---|
69 | Event *event = static_cast<Event*>(index.internalPointer()); |
---|
70 | // font SMALL |
---|
71 | QFont fontSmall = option.font; |
---|
72 | fontSmall.setBold(false); |
---|
73 | fontSmall.setPixelSize(aux*0.2); |
---|
74 | QFontMetrics fmSmall(fontSmall); |
---|
75 | // font SMALL bold |
---|
76 | QFont fontSmallB = fontSmall; |
---|
77 | fontSmallB.setBold(true); |
---|
78 | QFontMetrics fmSmallB(fontSmallB); |
---|
79 | |
---|
80 | // font BIG |
---|
81 | QFont fontBig = option.font; |
---|
82 | fontBig.setBold(false); |
---|
83 | fontBig.setPixelSize(aux*0.33); |
---|
84 | QFontMetrics fmBig(fontBig); |
---|
85 | // font BIG bold |
---|
86 | QFont fontBigB = fontBig; |
---|
87 | fontBigB.setBold(true); |
---|
88 | QFontMetrics fmBigB(fontBigB); |
---|
89 | |
---|
90 | //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width(); |
---|
91 | |
---|
92 | //Time conflicts are colored differently |
---|
93 | if(event->hasTimeConflict()) |
---|
94 | bkgrColor = conflictColor; |
---|
95 | |
---|
96 | QLinearGradient itemGradient(option.rect.topLeft(), option.rect.bottomLeft()); |
---|
97 | itemGradient.setColorAt(0.0, Qt::white); |
---|
98 | itemGradient.setColorAt(0.25, bkgrColor); |
---|
99 | itemGradient.setColorAt(0.5, bkgrColor); |
---|
100 | itemGradient.setColorAt(0.75, bkgrColor); |
---|
101 | itemGradient.setColorAt(1.0, Qt::white); |
---|
102 | |
---|
103 | if(isLast(index)) |
---|
104 | { |
---|
105 | QPainterPath endPath; |
---|
106 | endPath.moveTo(option.rect.topLeft()); |
---|
107 | endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS)); |
---|
108 | endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90); |
---|
109 | endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0)); |
---|
110 | endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90); |
---|
111 | endPath.lineTo(option.rect.topRight()); |
---|
112 | |
---|
113 | //painter->setBrush( bkgrColor ); |
---|
114 | painter->setBrush(itemGradient); |
---|
115 | painter->setPen(borderPen); |
---|
116 | painter->drawPath(endPath); |
---|
117 | |
---|
118 | painter->setFont(option.font); |
---|
119 | } |
---|
120 | else // middle elements |
---|
121 | { |
---|
122 | //painter->setBrush( bkgrColor ); |
---|
123 | painter->setBrush(itemGradient); |
---|
124 | painter->setPen(Qt::NoPen); |
---|
125 | painter->drawRect(option.rect); |
---|
126 | |
---|
127 | painter->setPen(borderPen); |
---|
128 | // vertical lines |
---|
129 | painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft()); |
---|
130 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); |
---|
131 | // horizontal lines |
---|
132 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); |
---|
133 | |
---|
134 | painter->setFont(option.font); |
---|
135 | } |
---|
136 | |
---|
137 | // draw Controls |
---|
138 | if(event->isFavourite()) |
---|
139 | mControls[FavouriteControlOn]->paint(painter, option.rect); |
---|
140 | else |
---|
141 | mControls[FavouriteControlOff]->paint(painter, option.rect); |
---|
142 | #ifdef MAEMO |
---|
143 | if(event->hasAlarm()) |
---|
144 | mControls[AlarmControlOn]->paint(painter, option.rect); |
---|
145 | else |
---|
146 | mControls[AlarmControlOff]->paint(painter, option.rect); |
---|
147 | #endif |
---|
148 | if (event->room()->hasMap()) |
---|
149 | mControls[MapControl]->paint(painter, option.rect); |
---|
150 | if(event->hasTimeConflict()) |
---|
151 | mControls[WarningControl]->paint(painter, option.rect); |
---|
152 | |
---|
153 | // draw texts |
---|
154 | // it starts just below the image |
---|
155 | // ("position of text" is lower-left angle of the first letter, |
---|
156 | // so the first line is actually at the same height as the image) |
---|
157 | QPointF titlePointF(option.rect.x() + SPACER, |
---|
158 | option.rect.y() + SPACER + mControls[FavouriteControlOn]->image()->height()); |
---|
159 | QTime start = event->start().time(); |
---|
160 | painter->setFont(fontBig); |
---|
161 | painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName()); |
---|
162 | // title |
---|
163 | titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent()); |
---|
164 | painter->setFont(fontBigB); |
---|
165 | QString title = event->title(); |
---|
166 | if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen |
---|
167 | { |
---|
168 | // chop words from the end |
---|
169 | while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty()) |
---|
170 | { |
---|
171 | title.chop(1); |
---|
172 | // chop characters one-by-one from the end |
---|
173 | while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty()) |
---|
174 | { |
---|
175 | title.chop(1); |
---|
176 | } |
---|
177 | } |
---|
178 | title += "..."; |
---|
179 | } |
---|
180 | painter->drawText(titlePointF,title); |
---|
181 | // persons |
---|
182 | titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent()); |
---|
183 | painter->setFont(fontSmall); |
---|
184 | painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and ")); |
---|
185 | // track |
---|
186 | titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent()); |
---|
187 | painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId())); |
---|
188 | } |
---|
189 | else // doesn't have parent - time-groups' elements (top items) |
---|
190 | { |
---|
191 | QFont fontSmall = option.font; |
---|
192 | fontSmall.setBold(true); |
---|
193 | fontSmall.setPixelSize(option.rect.height()*scaleFactor1); |
---|
194 | QFontMetrics fmSmall(fontSmall); |
---|
195 | |
---|
196 | QFont fontBig = option.font; |
---|
197 | fontBig.setBold(true); |
---|
198 | fontBig.setPixelSize(option.rect.height()*scaleFactor2); |
---|
199 | QFontMetrics fmBig(fontBig); |
---|
200 | |
---|
201 | int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width(); |
---|
202 | |
---|
203 | QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight()); |
---|
204 | //titleGradient.setColorAt(0.0, Qt::white); |
---|
205 | titleGradient.setColorAt(0.0, bkgrColor); |
---|
206 | titleGradient.setColorAt(0.5, Qt::white); |
---|
207 | titleGradient.setColorAt(1.0, bkgrColor); |
---|
208 | |
---|
209 | QPainterPath titlePath; |
---|
210 | if(isExpanded(index)) |
---|
211 | { |
---|
212 | titlePath.moveTo(option.rect.bottomLeft()); |
---|
213 | titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS)); |
---|
214 | titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90); |
---|
215 | titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0)); |
---|
216 | titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90); |
---|
217 | titlePath.lineTo(option.rect.bottomRight()); |
---|
218 | titlePath.closeSubpath(); |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS)); |
---|
223 | titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90); |
---|
224 | titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0)); |
---|
225 | titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90); |
---|
226 | titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS)); |
---|
227 | titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90); |
---|
228 | titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0)); |
---|
229 | titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90); |
---|
230 | titlePath.closeSubpath(); |
---|
231 | } |
---|
232 | |
---|
233 | painter->setBrush(titleGradient); |
---|
234 | painter->setPen(borderPen); |
---|
235 | painter->drawPath(titlePath); |
---|
236 | |
---|
237 | // draw icons |
---|
238 | painter->setFont(fontSmall); |
---|
239 | QImage *image = mControls[FavouriteControlOn]->image(); |
---|
240 | QPoint drawPoint = |
---|
241 | option.rect.topRight() |
---|
242 | - QPoint( |
---|
243 | spacer + image->width(), |
---|
244 | - option.rect.height()/2 + image->height()/2); |
---|
245 | painter->drawImage(drawPoint,*image); |
---|
246 | painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2), |
---|
247 | QString::number(numberOfFavourities(index))); |
---|
248 | #ifdef MAEMO |
---|
249 | drawPoint.setX(drawPoint.x() - spacer - image->width()); |
---|
250 | painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image()); |
---|
251 | painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2), |
---|
252 | QString::number(numberOfAlarms(index))); |
---|
253 | #endif |
---|
254 | // draw texts |
---|
255 | QString numEvents = QString::number(index.model()->rowCount(index)).append("/"); |
---|
256 | drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width()); |
---|
257 | drawPoint.setY(drawPoint.y()+image->height() - 2); |
---|
258 | painter->drawText(drawPoint,numEvents); |
---|
259 | |
---|
260 | QPointF titlePointF = QPoint( |
---|
261 | option.rect.x()+SPACER, |
---|
262 | option.rect.y()+option.rect.height()-fmBig.descent()); |
---|
263 | painter->setFont(fontBig); |
---|
264 | |
---|
265 | painter->drawText(titlePointF,qVariantValue<QString>(index.data())); |
---|
266 | } |
---|
267 | |
---|
268 | //// HIGHLIGHTING SELECTED ITEM |
---|
269 | //if (option.state & QStyle::State_Selected) |
---|
270 | //painter->fillRect(option.rect, option.palette.highlight()); |
---|
271 | |
---|
272 | painter->restore(); |
---|
273 | } |
---|
274 | |
---|
275 | QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
---|
276 | { |
---|
277 | Q_UNUSED(option) |
---|
278 | |
---|
279 | if (index.internalId() == 0) // time group |
---|
280 | { |
---|
281 | return QSize(40,40); |
---|
282 | } |
---|
283 | else // event |
---|
284 | { |
---|
285 | return QSize(100,100); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | bool Delegate::hasParent( const QModelIndex &index ) const |
---|
290 | { |
---|
291 | if( index.parent().isValid() ) |
---|
292 | return true; |
---|
293 | else |
---|
294 | return false; |
---|
295 | } |
---|
296 | |
---|
297 | bool Delegate::isLast( const QModelIndex &index ) const |
---|
298 | { |
---|
299 | if(!hasParent(index)) |
---|
300 | return false; // what should be returned here? |
---|
301 | |
---|
302 | if(index.row() >= (index.model()->rowCount(index.parent())-1)) |
---|
303 | return true; |
---|
304 | else |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | bool Delegate::isExpanded( const QModelIndex &index ) const |
---|
309 | { |
---|
310 | if( !mViewPtr ) |
---|
311 | return false; |
---|
312 | else |
---|
313 | return mViewPtr->isExpanded( index ); |
---|
314 | } |
---|
315 | |
---|
316 | Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const |
---|
317 | { |
---|
318 | if(!hasParent(aIndex)) // time-group item (root item) |
---|
319 | return ControlNone; |
---|
320 | |
---|
321 | QListIterator<ControlId> i(mControls.keys()); |
---|
322 | while (i.hasNext()) |
---|
323 | { |
---|
324 | ControlId id = i.next(); |
---|
325 | if(mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint)) |
---|
326 | { |
---|
327 | if(id == WarningControl) |
---|
328 | { |
---|
329 | if(static_cast<Event*>(aIndex.internalPointer())->hasTimeConflict()) |
---|
330 | return id; |
---|
331 | } |
---|
332 | else |
---|
333 | return id; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | return ControlNone; |
---|
338 | } |
---|
339 | |
---|
340 | Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control) |
---|
341 | : mId(aControlId) |
---|
342 | , mImage(new QImage(aImageName)) |
---|
343 | , mDrawPoint(QPoint(0,0)) |
---|
344 | { |
---|
345 | QPoint p; |
---|
346 | if (prev_control == NULL) { |
---|
347 | p = QPoint(0, SPACER); |
---|
348 | } else { |
---|
349 | p = prev_control->drawPoint(); |
---|
350 | } |
---|
351 | p.setX(p.x()-image()->width()-SPACER); |
---|
352 | setDrawPoint(p); |
---|
353 | } |
---|
354 | |
---|
355 | void Delegate::Control::paint(QPainter* painter, const QRect rect) |
---|
356 | { |
---|
357 | painter->drawImage(drawPoint(rect),*image()); |
---|
358 | } |
---|
359 | |
---|
360 | void Delegate::defineControls() |
---|
361 | { |
---|
362 | // FAVOURITE ICONs |
---|
363 | // on |
---|
364 | mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-onBig.png"), NULL)); |
---|
365 | // off |
---|
366 | mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-offBig.png"), NULL)); |
---|
367 | |
---|
368 | #ifdef MAEMO |
---|
369 | // ALARM ICONs |
---|
370 | // on |
---|
371 | mControls.insert(AlarmControlOn, |
---|
372 | new Control(AlarmControlOn, QString(":icons/alarm-onBig.png"), mControls[FavouriteControlOn])); |
---|
373 | // off |
---|
374 | mControls.insert(AlarmControlOff, |
---|
375 | new Control(AlarmControlOff, QString(":icons/alarm-offBig.png"), mControls[FavouriteControlOff])); |
---|
376 | |
---|
377 | // MAP ICON |
---|
378 | mControls.insert(MapControl, |
---|
379 | new Control(MapControl, QString(":icons/compassBig.png"), mControls[AlarmControlOn])); |
---|
380 | #else |
---|
381 | // MAP ICON |
---|
382 | mControls.insert(MapControl, |
---|
383 | new Control(MapControl, QString(":icons/compassBig.png"), mControls[FavouriteControlOn])); |
---|
384 | #endif |
---|
385 | |
---|
386 | // WARNING ICON |
---|
387 | mControls.insert(WarningControl, |
---|
388 | new Control(WarningControl, QString(":icons/exclamation.png"), mControls[MapControl])); |
---|
389 | } |
---|
390 | |
---|
391 | bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const |
---|
392 | { |
---|
393 | if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) ) |
---|
394 | return true; |
---|
395 | |
---|
396 | return false; |
---|
397 | } |
---|
398 | |
---|
399 | int Delegate::numberOfFavourities(const QModelIndex &index) const |
---|
400 | { |
---|
401 | if(index.parent().isValid()) // it's event, not time-group |
---|
402 | return 0; |
---|
403 | |
---|
404 | int nrofFavs = 0; |
---|
405 | for(int i=0; i<index.model()->rowCount(index); i++) |
---|
406 | if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite()) |
---|
407 | nrofFavs++; |
---|
408 | |
---|
409 | return nrofFavs; |
---|
410 | } |
---|
411 | |
---|
412 | int Delegate::numberOfAlarms(const QModelIndex &index) const |
---|
413 | { |
---|
414 | if(index.parent().isValid()) // it's event, not time-group |
---|
415 | return 0; |
---|
416 | |
---|
417 | int nrofAlarms = 0; |
---|
418 | for(int i=0; i<index.model()->rowCount(index); i++) |
---|
419 | if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm()) |
---|
420 | nrofAlarms++; |
---|
421 | |
---|
422 | return nrofAlarms; |
---|
423 | } |
---|
424 | |
---|