2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
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)
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
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/>.
20 #include "eventmodel.h"
28 const int RADIUS = 10;
29 const int SPACER = 10;
31 const double scaleFactor1 = 0.4;
32 const double scaleFactor2 = 0.8;
34 Delegate::Delegate(QTreeView *aParent)
35 : QItemDelegate(aParent)
44 QListIterator<ControlId> i(mControls.keys());
47 delete mControls[i.next()]->image();
51 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
57 QColor bkgrColor = Qt::cyan;
58 //QColor bkgrColor = QColor(0xAA,0xAA,0xAA);
59 QColor conflictColor = Qt::yellow;
61 QPen borderPen(bkgrColor.darker());
64 // entry horisontal layout:
65 // * spacer (aka y position of 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());
71 QFont fontSmall = option.font;
72 fontSmall.setBold(false);
73 fontSmall.setPixelSize(aux*0.2);
74 QFontMetrics fmSmall(fontSmall);
76 QFont fontSmallB = fontSmall;
77 fontSmallB.setBold(true);
78 QFontMetrics fmSmallB(fontSmallB);
81 QFont fontBig = option.font;
82 fontBig.setBold(false);
83 fontBig.setPixelSize(aux*0.33);
84 QFontMetrics fmBig(fontBig);
86 QFont fontBigB = fontBig;
87 fontBigB.setBold(true);
88 QFontMetrics fmBigB(fontBigB);
90 //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
92 //Time conflicts are colored differently
93 if(event->hasTimeConflict())
94 bkgrColor = conflictColor;
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);
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());
113 //painter->setBrush( bkgrColor );
114 painter->setBrush(itemGradient);
115 painter->setPen(borderPen);
116 painter->drawPath(endPath);
118 painter->setFont(option.font);
120 else // middle elements
122 //painter->setBrush( bkgrColor );
123 painter->setBrush(itemGradient);
124 painter->setPen(Qt::NoPen);
125 painter->drawRect(option.rect);
127 painter->setPen(borderPen);
129 painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
130 painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
132 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
134 painter->setFont(option.font);
138 foreach(Control* c, mControls.values()) {
139 c->setEnabled(false);
141 if(event->isFavourite())
142 mControls[FavouriteControlOn]->paint(painter, option.rect);
144 mControls[FavouriteControlOff]->paint(painter, option.rect);
146 if(event->hasAlarm())
147 mControls[AlarmControlOn]->paint(painter, option.rect);
149 mControls[AlarmControlOff]->paint(painter, option.rect);
151 if (event->room()->hasMap())
152 mControls[MapControl]->paint(painter, option.rect);
153 if(event->hasTimeConflict())
154 mControls[WarningControl]->paint(painter, option.rect);
157 // it starts just below the image
158 // ("position of text" is lower-left angle of the first letter,
159 // so the first line is actually at the same height as the image)
160 QPointF titlePointF(option.rect.x() + SPACER,
161 option.rect.y() + SPACER + mControls[FavouriteControlOn]->image()->height());
162 QTime start = event->start().time();
163 painter->setFont(fontBig);
164 painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
166 titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
167 painter->setFont(fontBigB);
168 QString title = event->title();
169 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
171 // chop words from the end
172 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
175 // chop characters one-by-one from the end
176 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
183 painter->drawText(titlePointF,title);
185 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
186 painter->setFont(fontSmall);
187 painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
189 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
190 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
192 else // doesn't have parent - time-groups' elements (top items)
194 QFont fontSmall = option.font;
195 fontSmall.setBold(true);
196 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
197 QFontMetrics fmSmall(fontSmall);
199 QFont fontBig = option.font;
200 fontBig.setBold(true);
201 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
202 QFontMetrics fmBig(fontBig);
204 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
206 QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
207 //titleGradient.setColorAt(0.0, Qt::white);
208 titleGradient.setColorAt(0.0, bkgrColor);
209 titleGradient.setColorAt(0.5, Qt::white);
210 titleGradient.setColorAt(1.0, bkgrColor);
212 QPainterPath titlePath;
213 if(isExpanded(index))
215 titlePath.moveTo(option.rect.bottomLeft());
216 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
217 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
218 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
219 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
220 titlePath.lineTo(option.rect.bottomRight());
221 titlePath.closeSubpath();
225 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
226 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
227 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
228 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
229 titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
230 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
231 titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
232 titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);
233 titlePath.closeSubpath();
236 painter->setBrush(titleGradient);
237 painter->setPen(borderPen);
238 painter->drawPath(titlePath);
241 painter->setFont(fontSmall);
242 QImage *image = mControls[FavouriteControlOn]->image();
244 option.rect.topRight()
246 spacer + image->width(),
247 - option.rect.height()/2 + image->height()/2);
248 painter->drawImage(drawPoint,*image);
249 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
250 QString::number(numberOfFavourities(index)));
252 drawPoint.setX(drawPoint.x() - spacer - image->width());
253 painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
254 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
255 QString::number(numberOfAlarms(index)));
258 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
259 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
260 drawPoint.setY(drawPoint.y()+image->height() - 2);
261 painter->drawText(drawPoint,numEvents);
263 QPointF titlePointF = QPoint(
264 option.rect.x()+SPACER,
265 option.rect.y()+option.rect.height()-fmBig.descent());
266 painter->setFont(fontBig);
268 painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
271 //// HIGHLIGHTING SELECTED ITEM
272 //if (option.state & QStyle::State_Selected)
273 //painter->fillRect(option.rect, option.palette.highlight());
278 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
282 if (index.internalId() == 0) // time group
288 return QSize(100,100);
292 bool Delegate::hasParent( const QModelIndex &index ) const
294 if( index.parent().isValid() )
300 bool Delegate::isLast( const QModelIndex &index ) const
302 if(!hasParent(index))
303 return false; // what should be returned here?
305 if(index.row() >= (index.model()->rowCount(index.parent())-1))
311 bool Delegate::isExpanded( const QModelIndex &index ) const
316 return mViewPtr->isExpanded( index );
319 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
321 if(!hasParent(aIndex)) // time-group item (root item)
324 QListIterator<ControlId> i(mControls.keys());
327 ControlId id = i.next();
328 Control *control = mControls[id];
329 if (control->enabled()
330 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
339 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
341 , mImage(new QImage(aImageName))
342 , mDrawPoint(QPoint(0,0))
346 if (prev_control == NULL) {
347 p = QPoint(0, SPACER);
349 p = prev_control->drawPoint();
351 p.setX(p.x()-image()->width()-SPACER);
355 void Delegate::Control::paint(QPainter* painter, const QRect rect)
357 painter->drawImage(drawPoint(rect),*image());
361 void Delegate::defineControls()
365 mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-onBig.png"), NULL));
367 mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-offBig.png"), NULL));
372 mControls.insert(AlarmControlOn,
373 new Control(AlarmControlOn, QString(":icons/alarm-onBig.png"), mControls[FavouriteControlOn]));
375 mControls.insert(AlarmControlOff,
376 new Control(AlarmControlOff, QString(":icons/alarm-offBig.png"), mControls[FavouriteControlOff]));
379 mControls.insert(MapControl,
380 new Control(MapControl, QString(":icons/compassBig.png"), mControls[AlarmControlOn]));
383 mControls.insert(MapControl,
384 new Control(MapControl, QString(":icons/compassBig.png"), mControls[FavouriteControlOn]));
388 mControls.insert(WarningControl,
389 new Control(WarningControl, QString(":icons/exclamation.png"), mControls[MapControl]));
392 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
394 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
400 int Delegate::numberOfFavourities(const QModelIndex &index) const
402 if(index.parent().isValid()) // it's event, not time-group
406 for(int i=0; i<index.model()->rowCount(index); i++)
407 if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
413 int Delegate::numberOfAlarms(const QModelIndex &index) const
415 if(index.parent().isValid()) // it's event, not time-group
419 for(int i=0; i<index.model()->rowCount(index); i++)
420 if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())