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 = option.palette.color(QPalette::Background);
59 //QColor bkgrColor = QColor(0xAA,0xAA,0xAA);
60 QColor conflictColor = Qt::yellow;
62 QColor textColor = option.palette.color(QPalette::Text);
63 QPen borderPen(textColor);
66 // entry horizontal layout:
67 // * spacer (aka y position of image)
69 // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
70 int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height();
71 Event *event = static_cast<Event*>(index.internalPointer());
73 QFont fontSmall = option.font;
74 fontSmall.setBold(false);
75 fontSmall.setPixelSize(aux*0.2);
76 QFontMetrics fmSmall(fontSmall);
78 QFont fontSmallB = fontSmall;
79 fontSmallB.setBold(true);
80 QFontMetrics fmSmallB(fontSmallB);
83 QFont fontBig = option.font;
84 fontBig.setBold(false);
85 fontBig.setPixelSize(aux*0.33);
86 QFontMetrics fmBig(fontBig);
88 QFont fontBigB = fontBig;
89 fontBigB.setBold(true);
90 QFontMetrics fmBigB(fontBigB);
92 //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
94 //Time conflicts are colored differently
95 if(event->hasTimeConflict())
96 bkgrColor = conflictColor;
98 QLinearGradient itemGradient(option.rect.topLeft(), option.rect.bottomLeft());
99 itemGradient.setColorAt(0.0, bkgrColor);
100 itemGradient.setColorAt(1.0, bkgrColor);
104 QPainterPath endPath;
105 endPath.moveTo(option.rect.topLeft());
106 endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
107 endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
108 endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
109 endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
110 endPath.lineTo(option.rect.topRight());
112 //painter->setBrush( bkgrColor );
113 painter->setBrush(itemGradient);
114 painter->setPen(Qt::NoPen);
115 painter->drawPath(endPath);
116 painter->setPen(borderPen);
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);
130 painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
131 painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
134 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
136 painter->setFont(option.font);
140 foreach(Control* c, mControls.values()) {
141 c->setEnabled(false);
143 if(event->isFavourite())
144 mControls[FavouriteControlOn]->paint(painter, option.rect);
146 mControls[FavouriteControlOff]->paint(painter, option.rect);
148 if(event->hasAlarm())
149 mControls[AlarmControlOn]->paint(painter, option.rect);
151 mControls[AlarmControlOff]->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 QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
188 painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
190 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
191 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
193 else // doesn't have parent - time-groups' elements (top items)
195 QFont fontSmall = option.font;
196 fontSmall.setBold(true);
197 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
198 QFontMetrics fmSmall(fontSmall);
200 QFont fontBig = option.font;
201 fontBig.setBold(true);
202 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
203 QFontMetrics fmBig(fontBig);
205 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
207 QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
208 bkgrColor = option.palette.color(QPalette::Highlight);
209 textColor = option.palette.color(QPalette::HighlightedText);
210 titleGradient.setColorAt(0.0, bkgrColor);
211 titleGradient.setColorAt(1.0, bkgrColor);
213 QPainterPath titlePath;
214 if(isExpanded(index))
216 titlePath.moveTo(option.rect.bottomLeft());
217 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
218 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
219 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
220 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
221 titlePath.lineTo(option.rect.bottomRight());
222 titlePath.closeSubpath();
226 titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
227 titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
228 titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
229 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
230 titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
231 titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
232 titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
233 titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);
234 titlePath.closeSubpath();
237 painter->setBrush(titleGradient);
238 painter->setPen(borderPen);
239 painter->drawPath(titlePath);
242 borderPen.setColor(textColor);
243 painter->setPen(borderPen);
244 painter->setFont(fontSmall);
245 QImage *image = mControls[FavouriteControlOn]->image();
247 option.rect.topRight()
249 spacer + image->width(),
250 - option.rect.height()/2 + image->height()/2);
251 painter->drawImage(drawPoint,*image);
252 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
253 QString::number(numberOfFavourities(index)));
255 drawPoint.setX(drawPoint.x() - spacer - image->width());
256 painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
257 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
258 QString::number(numberOfAlarms(index)));
261 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
262 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
263 drawPoint.setY(drawPoint.y()+image->height() - 2);
264 painter->drawText(drawPoint,numEvents);
266 QPointF titlePointF = QPoint(
267 option.rect.x()+SPACER,
268 option.rect.y()+option.rect.height()-fmBig.descent());
269 painter->setFont(fontBig);
271 painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
274 //// HIGHLIGHTING SELECTED ITEM
275 //if (option.state & QStyle::State_Selected)
276 //painter->fillRect(option.rect, option.palette.highlight());
281 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
285 if (index.internalId() == 0) // time group
291 return QSize(100,100);
295 bool Delegate::hasParent( const QModelIndex &index ) const
297 if( index.parent().isValid() )
303 bool Delegate::isLast( const QModelIndex &index ) const
305 if(!hasParent(index))
306 return false; // what should be returned here?
308 if(index.row() >= (index.model()->rowCount(index.parent())-1))
314 bool Delegate::isExpanded( const QModelIndex &index ) const
319 return mViewPtr->isExpanded( index );
322 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
324 if(!hasParent(aIndex)) // time-group item (root item)
327 QListIterator<ControlId> i(mControls.keys());
330 ControlId id = i.next();
331 Control *control = mControls[id];
332 if (control->enabled()
333 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
342 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
344 , mImage(new QImage(aImageName))
345 , mDrawPoint(QPoint(0,0))
349 if (prev_control == NULL) {
350 p = QPoint(0, SPACER);
352 p = prev_control->drawPoint();
354 p.setX(p.x()-image()->width()-SPACER);
358 void Delegate::Control::paint(QPainter* painter, const QRect rect)
360 painter->drawImage(drawPoint(rect),*image());
364 void Delegate::defineControls()
368 mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/emblem-new.png"), NULL));
370 mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/emblem-new-off.png"), NULL));
375 mControls.insert(AlarmControlOn,
376 new Control(AlarmControlOn, QString(":icons/appointment-soon.png"), mControls[FavouriteControlOn]));
378 mControls.insert(AlarmControlOff,
379 new Control(AlarmControlOff, QString(":icons/appointment-soon-off.png"), mControls[FavouriteControlOff]));
383 mControls.insert(WarningControl,
384 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[FavouriteControlOn]));
387 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
389 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
395 int Delegate::numberOfFavourities(const QModelIndex &index) const
397 if(index.parent().isValid()) // it's event, not time-group
401 for(int i=0; i<index.model()->rowCount(index); i++)
402 if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
408 int Delegate::numberOfAlarms(const QModelIndex &index) const
410 if(index.parent().isValid()) // it's event, not time-group
414 for(int i=0; i<index.model()->rowCount(index); i++)
415 if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())