2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
21 #include "eventmodel.h"
29 const int RADIUS = 10;
30 const int SPACER = 10;
32 const double scaleFactor1 = 0.4;
33 const double scaleFactor2 = 0.8;
35 Delegate::Delegate(QTreeView *aParent)
36 : QItemDelegate(aParent)
45 QListIterator<ControlId> i(mControls.keys());
48 delete mControls[i.next()]->image();
52 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
58 QColor bkgrColor = Qt::cyan;
59 //QColor bkgrColor = QColor(0xAA,0xAA,0xAA);
60 QColor conflictColor = Qt::yellow;
62 QPen borderPen(bkgrColor.darker());
65 // entry horisontal layout:
66 // * spacer (aka y position of image)
68 // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
69 int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height();
70 Event *event = static_cast<Event*>(index.internalPointer());
72 QFont fontSmall = option.font;
73 fontSmall.setBold(false);
74 fontSmall.setPixelSize(aux*0.2);
75 QFontMetrics fmSmall(fontSmall);
77 QFont fontSmallB = fontSmall;
78 fontSmallB.setBold(true);
79 QFontMetrics fmSmallB(fontSmallB);
82 QFont fontBig = option.font;
83 fontBig.setBold(false);
84 fontBig.setPixelSize(aux*0.33);
85 QFontMetrics fmBig(fontBig);
87 QFont fontBigB = fontBig;
88 fontBigB.setBold(true);
89 QFontMetrics fmBigB(fontBigB);
91 //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
93 //Time conflicts are colored differently
94 if(event->hasTimeConflict())
95 bkgrColor = conflictColor;
97 QLinearGradient itemGradient(option.rect.topLeft(), option.rect.bottomLeft());
98 itemGradient.setColorAt(0.0, Qt::white);
99 itemGradient.setColorAt(0.25, bkgrColor);
100 itemGradient.setColorAt(0.5, bkgrColor);
101 itemGradient.setColorAt(0.75, bkgrColor);
102 itemGradient.setColorAt(1.0, Qt::white);
106 QPainterPath endPath;
107 endPath.moveTo(option.rect.topLeft());
108 endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
109 endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
110 endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
111 endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
112 endPath.lineTo(option.rect.topRight());
114 //painter->setBrush( bkgrColor );
115 painter->setBrush(itemGradient);
116 painter->setPen(borderPen);
117 painter->drawPath(endPath);
119 painter->setFont(option.font);
121 else // middle elements
123 //painter->setBrush( bkgrColor );
124 painter->setBrush(itemGradient);
125 painter->setPen(Qt::NoPen);
126 painter->drawRect(option.rect);
128 painter->setPen(borderPen);
130 painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
131 painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
133 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
135 painter->setFont(option.font);
139 foreach(Control* c, mControls.values()) {
140 c->setEnabled(false);
142 if(event->isFavourite())
143 mControls[FavouriteControlOn]->paint(painter, option.rect);
145 mControls[FavouriteControlOff]->paint(painter, option.rect);
147 if(event->hasAlarm())
148 mControls[AlarmControlOn]->paint(painter, option.rect);
150 mControls[AlarmControlOff]->paint(painter, option.rect);
152 if(event->hasTimeConflict())
153 mControls[WarningControl]->paint(painter, option.rect);
156 // it starts just below the image
157 // ("position of text" is lower-left angle of the first letter,
158 // so the first line is actually at the same height as the image)
159 QPointF titlePointF(option.rect.x() + SPACER,
160 option.rect.y() + SPACER + mControls[FavouriteControlOn]->image()->height());
161 QTime start = event->start().time();
162 painter->setFont(fontBig);
163 painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
165 titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
166 painter->setFont(fontBigB);
167 QString title = event->title();
168 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
170 // chop words from the end
171 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
174 // chop characters one-by-one from the end
175 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
182 painter->drawText(titlePointF,title);
184 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
185 painter->setFont(fontSmall);
186 painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
188 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
189 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
191 else // doesn't have parent - time-groups' elements (top items)
193 QFont fontSmall = option.font;
194 fontSmall.setBold(true);
195 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
196 QFontMetrics fmSmall(fontSmall);
198 QFont fontBig = option.font;
199 fontBig.setBold(true);
200 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
201 QFontMetrics fmBig(fontBig);
203 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
205 QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
206 //titleGradient.setColorAt(0.0, Qt::white);
207 titleGradient.setColorAt(0.0, bkgrColor);
208 titleGradient.setColorAt(0.5, Qt::white);
209 titleGradient.setColorAt(1.0, bkgrColor);
211 QPainterPath titlePath;
212 if(isExpanded(index))
214 titlePath.moveTo(option.rect.bottomLeft());
215 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
216 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
217 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
218 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
219 titlePath.lineTo(option.rect.bottomRight());
220 titlePath.closeSubpath();
224 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
225 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
226 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
227 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
228 titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
229 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
230 titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
231 titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);
232 titlePath.closeSubpath();
235 painter->setBrush(titleGradient);
236 painter->setPen(borderPen);
237 painter->drawPath(titlePath);
240 painter->setFont(fontSmall);
241 QImage *image = mControls[FavouriteControlOn]->image();
243 option.rect.topRight()
245 spacer + image->width(),
246 - option.rect.height()/2 + image->height()/2);
247 painter->drawImage(drawPoint,*image);
248 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
249 QString::number(numberOfFavourities(index)));
251 drawPoint.setX(drawPoint.x() - spacer - image->width());
252 painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
253 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
254 QString::number(numberOfAlarms(index)));
257 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
258 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
259 drawPoint.setY(drawPoint.y()+image->height() - 2);
260 painter->drawText(drawPoint,numEvents);
262 QPointF titlePointF = QPoint(
263 option.rect.x()+SPACER,
264 option.rect.y()+option.rect.height()-fmBig.descent());
265 painter->setFont(fontBig);
267 painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
270 //// HIGHLIGHTING SELECTED ITEM
271 //if (option.state & QStyle::State_Selected)
272 //painter->fillRect(option.rect, option.palette.highlight());
277 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
281 if (index.internalId() == 0) // time group
287 return QSize(100,100);
291 bool Delegate::hasParent( const QModelIndex &index ) const
293 if( index.parent().isValid() )
299 bool Delegate::isLast( const QModelIndex &index ) const
301 if(!hasParent(index))
302 return false; // what should be returned here?
304 if(index.row() >= (index.model()->rowCount(index.parent())-1))
310 bool Delegate::isExpanded( const QModelIndex &index ) const
315 return mViewPtr->isExpanded( index );
318 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
320 if(!hasParent(aIndex)) // time-group item (root item)
323 QListIterator<ControlId> i(mControls.keys());
326 ControlId id = i.next();
327 Control *control = mControls[id];
328 if (control->enabled()
329 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
338 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
340 , mImage(new QImage(aImageName))
341 , mDrawPoint(QPoint(0,0))
345 if (prev_control == NULL) {
346 p = QPoint(0, SPACER);
348 p = prev_control->drawPoint();
350 p.setX(p.x()-image()->width()-SPACER);
354 void Delegate::Control::paint(QPainter* painter, const QRect rect)
356 painter->drawImage(drawPoint(rect),*image());
360 void Delegate::defineControls()
364 mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/emblem-new.png"), NULL));
366 mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/emblem-new-off.png"), NULL));
371 mControls.insert(AlarmControlOn,
372 new Control(AlarmControlOn, QString(":icons/appointment-soon.png"), mControls[FavouriteControlOn]));
374 mControls.insert(AlarmControlOff,
375 new Control(AlarmControlOff, QString(":icons/appointment-soon-off.png"), mControls[FavouriteControlOff]));
379 mControls.insert(WarningControl,
380 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[FavouriteControlOn]));
383 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
385 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
391 int Delegate::numberOfFavourities(const QModelIndex &index) const
393 if(index.parent().isValid()) // it's event, not time-group
397 for(int i=0; i<index.model()->rowCount(index); i++)
398 if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
404 int Delegate::numberOfAlarms(const QModelIndex &index) const
406 if(index.parent().isValid()) // it's event, not time-group
410 for(int i=0; i<index.model()->rowCount(index); i++)
411 if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())