2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2012 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
59 QColor textColor = option.palette.color(QPalette::Text);
63 // entry horizontal layout:
64 // * spacer (aka y position of image)
66 // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
67 int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height();
68 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);
80 QFont fontBig = option.font;
81 fontBig.setBold(false);
82 fontBig.setPixelSize(aux*0.33);
83 QFontMetrics fmBig(fontBig);
86 QFont fontBigB = fontBig;
87 fontBigB.setBold(true);
88 QFontMetrics fmBigB(fontBigB);
90 // background (in case of time conflicts)
91 if(event->hasTimeConflict()) {
92 painter->setBrush(Qt::yellow);
93 painter->setPen(Qt::NoPen);
94 painter->drawRect(option.rect);
97 // background (without time conflicts)
99 QStyleOption styleOption;
100 styleOption.rect = option.rect;
101 qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
105 foreach(Control* c, mControls.values()) {
106 c->setEnabled(false);
108 if(event->isFavourite())
109 mControls[FavouriteControlOn]->paint(painter, option.rect);
111 mControls[FavouriteControlOff]->paint(painter, option.rect);
113 if(event->hasAlarm())
114 mControls[AlarmControlOn]->paint(painter, option.rect);
116 mControls[AlarmControlOff]->paint(painter, option.rect);
118 if(event->hasTimeConflict())
119 mControls[WarningControl]->paint(painter, option.rect);
122 // it starts just below the image
123 // ("position of text" is lower-left angle of the first letter,
124 // so the first line is actually at the same height as the image)
125 painter->setPen(QPen(event->hasTimeConflict() ? Qt::black : textColor));
126 QPointF titlePointF(option.rect.x() + SPACER,
127 option.rect.y() + SPACER + mControls[FavouriteControlOn]->image()->height());
128 QTime start = event->start().time();
129 painter->setFont(fontBig);
130 painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
133 titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
134 painter->setFont(fontBigB);
135 QString title = event->title();
136 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
138 // chop words from the end
139 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
142 // chop characters one-by-one from the end
143 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
150 painter->drawText(titlePointF,title);
153 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
154 painter->setFont(fontSmall);
155 QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
156 painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
159 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
160 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
163 else // doesn't have parent - time-groups' elements (top items)
165 int numFav = numberOfFavourities(index);
167 QStyleOptionButton styleOptionButton;
168 styleOptionButton.rect = option.rect;
169 if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
170 // styleOptionButton.text = qVariantValue<QString>(index.data());
171 qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
172 // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
173 // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
175 QFont fontSmall = option.font;
176 fontSmall.setBold(true);
177 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
178 QFontMetrics fmSmall(fontSmall);
180 QFont fontBig = option.font;
181 fontBig.setBold(true);
182 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
183 QFontMetrics fmBig(fontBig);
185 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
188 painter->setPen(QPen(textColor));
189 painter->setFont(fontSmall);
190 QImage *image = mControls[numFav ? FavouriteControlOn : FavouriteControlOff]->image();
192 option.rect.topRight()
194 spacer + image->width(),
195 - option.rect.height()/2 + image->height()/2);
196 painter->drawImage(drawPoint,*image);
197 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
198 QString::number(numFav));
200 drawPoint.setX(drawPoint.x() - spacer - image->width());
201 painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
202 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
203 QString::number(numberOfAlarms(index)));
206 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
207 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
208 drawPoint.setY(drawPoint.y()+image->height() - 2);
209 painter->drawText(drawPoint,numEvents);
211 QPointF titlePointF = QPoint(
212 option.rect.x()+SPACER,
213 option.rect.y()+option.rect.height()-fmBig.descent());
214 painter->setFont(fontBig);
215 painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
221 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
225 if (index.internalId() == 0) // time group
231 return QSize(100,100);
235 bool Delegate::hasParent( const QModelIndex &index ) const
237 if( index.parent().isValid() )
243 bool Delegate::isLast( const QModelIndex &index ) const
245 if(!hasParent(index))
246 return false; // what should be returned here?
248 if(index.row() >= (index.model()->rowCount(index.parent())-1))
254 bool Delegate::isExpanded( const QModelIndex &index ) const
259 return mViewPtr->isExpanded( index );
262 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
264 if(!hasParent(aIndex)) // time-group item (root item)
267 QListIterator<ControlId> i(mControls.keys());
270 ControlId id = i.next();
271 Control *control = mControls[id];
272 if (control->enabled()
273 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
282 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
284 , mImage(new QImage(aImageName))
285 , mDrawPoint(QPoint(0,0))
289 if (prev_control == NULL) {
290 p = QPoint(0, SPACER);
292 p = prev_control->drawPoint();
294 p.setX(p.x()-image()->width()-SPACER);
298 void Delegate::Control::paint(QPainter* painter, const QRect rect)
300 painter->drawImage(drawPoint(rect),*image());
304 void Delegate::defineControls()
308 mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-on.png"), NULL));
310 mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-off.png"), NULL));
315 mControls.insert(AlarmControlOn,
316 new Control(AlarmControlOn, QString(":icons/appointment-soon.png"), mControls[FavouriteControlOn]));
318 mControls.insert(AlarmControlOff,
319 new Control(AlarmControlOff, QString(":icons/appointment-soon-off.png"), mControls[FavouriteControlOff]));
321 mControls.insert(WarningControl,
322 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
325 mControls.insert(WarningControl,
326 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[FavouriteControlOn]));
331 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
333 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
339 int Delegate::numberOfFavourities(const QModelIndex &index) const
341 if(index.parent().isValid()) // it's event, not time-group
345 for(int i=0; i<index.model()->rowCount(index); i++)
346 if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
352 int Delegate::numberOfAlarms(const QModelIndex &index) const
354 if(index.parent().isValid()) // it's event, not time-group
358 for(int i=0; i<index.model()->rowCount(index); i++)
359 if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())