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(borderPen);
115 painter->drawPath(endPath);
117 painter->setFont(option.font);
119 else // middle elements
121 //painter->setBrush( bkgrColor );
122 painter->setBrush(itemGradient);
123 painter->setPen(Qt::NoPen);
124 painter->drawRect(option.rect);
126 painter->setPen(borderPen);
128 painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
129 painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
131 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
133 painter->setFont(option.font);
137 foreach(Control* c, mControls.values()) {
138 c->setEnabled(false);
140 if(event->isFavourite())
141 mControls[FavouriteControlOn]->paint(painter, option.rect);
143 mControls[FavouriteControlOff]->paint(painter, option.rect);
145 if(event->hasAlarm())
146 mControls[AlarmControlOn]->paint(painter, option.rect);
148 mControls[AlarmControlOff]->paint(painter, option.rect);
150 if(event->hasTimeConflict())
151 mControls[WarningControl]->paint(painter, option.rect);
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());
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
168 // chop words from the end
169 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
172 // chop characters one-by-one from the end
173 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
180 painter->drawText(titlePointF,title);
182 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
183 painter->setFont(fontSmall);
184 painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
186 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
187 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
189 else // doesn't have parent - time-groups' elements (top items)
191 QFont fontSmall = option.font;
192 fontSmall.setBold(true);
193 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
194 QFontMetrics fmSmall(fontSmall);
196 QFont fontBig = option.font;
197 fontBig.setBold(true);
198 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
199 QFontMetrics fmBig(fontBig);
201 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
203 QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
204 bkgrColor = option.palette.color(QPalette::Highlight);
205 textColor = option.palette.color(QPalette::HighlightedText);
206 titleGradient.setColorAt(0.0, bkgrColor);
207 titleGradient.setColorAt(1.0, bkgrColor);
209 QPainterPath titlePath;
210 if(isExpanded(index))
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();
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();
233 painter->setBrush(titleGradient);
234 painter->setPen(borderPen);
235 painter->drawPath(titlePath);
238 borderPen.setColor(textColor);
239 painter->setPen(borderPen);
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())