2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
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 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
58 QColor textColor = option.palette.color(QPalette::Text);
62 Event *event = static_cast<Event*>(index.internalPointer());
64 // determine severity of conflict
65 Favourite eventTimeConflict = event->timeConflict(); // cache value as event->timeConflict is expensive
66 enum ConflictSeverity {csNone, csWeak, csStrong} conflictSeverity = csNone;
67 switch (event->favourite()) {
68 case Favourite_strong:
69 conflictSeverity = (eventTimeConflict == Favourite_strong) ? csStrong : csNone;
72 conflictSeverity = (eventTimeConflict == Favourite_no) ? csNone : csWeak;
75 conflictSeverity = csNone;
79 // entry horizontal layout:
80 // * spacer (aka y position of image)
82 // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
83 int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height();
86 QFont fontSmall = option.font;
87 fontSmall.setBold(false);
88 fontSmall.setPixelSize(aux*0.2);
89 QFontMetrics fmSmall(fontSmall);
91 QFont fontSmallB = fontSmall;
92 fontSmallB.setBold(true);
95 QFont fontBig = option.font;
96 fontBig.setBold(false);
97 fontBig.setPixelSize(aux*0.33);
98 QFontMetrics fmBig(fontBig);
101 QFont fontBigB = fontBig;
102 fontBigB.setBold(true);
103 QFontMetrics fmBigB(fontBigB);
105 // background (in case of time conflicts)
106 if (conflictSeverity != csNone) {
107 painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow"));
108 painter->setPen(Qt::NoPen);
109 painter->drawRect(option.rect);
112 // background (without time conflicts)
114 QStyleOption styleOption;
115 styleOption.rect = option.rect;
116 qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
120 foreach(Control* c, mControls.values()) {
121 c->setEnabled(false);
123 switch (event->favourite()) {
124 case Favourite_strong:
125 mControls[FavouriteControlStrong]->paint(painter, option.rect);
128 mControls[FavouriteControlWeak]->paint(painter, option.rect);
131 mControls[FavouriteControlNo]->paint(painter, option.rect);
135 if(event->hasAlarm())
136 mControls[AlarmControlOn]->paint(painter, option.rect);
138 mControls[AlarmControlOff]->paint(painter, option.rect);
140 if(eventTimeConflict != Favourite_no)
141 mControls[WarningControl]->paint(painter, option.rect);
144 // it starts just below the image
145 // ("position of text" is lower-left angle of the first letter,
146 // so the first line is actually at the same height as the image)
147 painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor));
148 QPointF titlePointF(option.rect.x() + SPACER,
149 option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
150 QTime start = event->start().time();
151 painter->setFont(fontBig);
152 painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
155 titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
156 painter->setFont(fontBigB);
157 QString title = event->title();
158 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
160 // chop words from the end
161 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
164 // chop characters one-by-one from the end
165 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
172 painter->drawText(titlePointF,title);
175 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
176 painter->setFont(fontSmall);
177 QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
178 painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
181 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
182 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
185 else // doesn't have parent - time-groups' elements (top items)
187 int numFav = numberOfFavourites(index);
188 int numAlarm = numberOfAlarms(index);
190 QStyleOptionButton styleOptionButton;
191 styleOptionButton.rect = option.rect;
192 if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
193 // styleOptionButton.text = qVariantValue<QString>(index.data());
194 qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
195 // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
196 // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
198 QFont fontSmall = option.font;
199 fontSmall.setBold(true);
200 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
201 QFontMetrics fmSmall(fontSmall);
203 QFont fontBig = option.font;
204 fontBig.setBold(true);
205 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
206 QFontMetrics fmBig(fontBig);
208 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
211 painter->setPen(QPen(textColor));
212 painter->setFont(fontSmall);
213 QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
215 option.rect.topRight()
217 spacer + image->width(),
218 - option.rect.height()/2 + image->height()/2);
219 painter->drawImage(drawPoint,*image);
220 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
221 QString::number(numFav));
223 drawPoint.setX(drawPoint.x() - spacer - image->width());
224 image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
225 painter->drawImage(drawPoint,*image);
226 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
227 QString::number(numAlarm));
230 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
231 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
232 drawPoint.setY(drawPoint.y()+image->height() - 2);
233 painter->drawText(drawPoint,numEvents);
235 QPointF titlePointF = QPoint(
236 option.rect.x()+SPACER,
237 option.rect.y()+option.rect.height()-fmBig.descent());
238 painter->setFont(fontBig);
239 painter->drawText(titlePointF,index.data().value<QString>());
245 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
249 if (index.internalId() == 0) // time group
255 return QSize(100,100);
259 bool Delegate::hasParent( const QModelIndex &index ) const
261 if( index.parent().isValid() )
267 bool Delegate::isLast( const QModelIndex &index ) const
269 if(!hasParent(index))
270 return false; // what should be returned here?
272 if(index.row() >= (index.model()->rowCount(index.parent())-1))
278 bool Delegate::isExpanded( const QModelIndex &index ) const
283 return mViewPtr->isExpanded( index );
286 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
288 if(!hasParent(aIndex)) // time-group item (root item)
291 QListIterator<ControlId> i(mControls.keys());
294 ControlId id = i.next();
295 Control *control = mControls[id];
296 if (control->enabled()
297 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
306 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
308 , mImage(new QImage(aImageName))
309 , mDrawPoint(QPoint(0,0))
313 if (prev_control == NULL) {
314 p = QPoint(0, SPACER);
316 p = prev_control->drawPoint();
318 p.setX(p.x()-image()->width()-SPACER);
322 void Delegate::Control::paint(QPainter* painter, const QRect rect)
324 painter->drawImage(drawPoint(rect),*image());
328 void Delegate::defineControls()
332 mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
334 mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
336 mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
340 mControls.insert(AlarmControlOn,
341 new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
343 mControls.insert(AlarmControlOff,
344 new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
346 mControls.insert(WarningControl,
347 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
350 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
352 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
358 int Delegate::numberOfFavourites(const QModelIndex &index) const
360 if(index.parent().isValid()) // it's event, not time-group
364 for(int i=0; i<index.model()->rowCount(index); i++)
365 if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->favourite() != Favourite_no)
371 int Delegate::numberOfAlarms(const QModelIndex &index) const
373 if(index.parent().isValid()) // it's event, not time-group
377 for(int i=0; i<index.model()->rowCount(index); i++)
378 if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->hasAlarm())