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 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 Event *event = static_cast<Event*>(index.internalPointer());
65 // determine severity of conflict
66 Favourite eventTimeConflict = event->timeConflict(); // cache value as event->timeConflict is expensive
67 enum ConflictSeverity {csNone, csWeak, csStrong} conflictSeverity = csNone;
68 switch (event->favourite()) {
69 case Favourite_strong:
70 conflictSeverity = (eventTimeConflict == Favourite_strong) ? csStrong : csNone;
73 conflictSeverity = (eventTimeConflict == Favourite_no) ? csNone : csWeak;
76 conflictSeverity = csNone;
80 // entry horizontal layout:
81 // * spacer (aka y position of image)
83 // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
84 int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height();
87 QFont fontSmall = option.font;
88 fontSmall.setBold(false);
89 fontSmall.setPixelSize(aux*0.2);
90 QFontMetrics fmSmall(fontSmall);
92 QFont fontSmallB = fontSmall;
93 fontSmallB.setBold(true);
96 QFont fontBig = option.font;
97 fontBig.setBold(false);
98 fontBig.setPixelSize(aux*0.33);
99 QFontMetrics fmBig(fontBig);
102 QFont fontBigB = fontBig;
103 fontBigB.setBold(true);
104 QFontMetrics fmBigB(fontBigB);
106 // background (in case of time conflicts)
107 if (conflictSeverity != csNone) {
108 painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow"));
109 painter->setPen(Qt::NoPen);
110 painter->drawRect(option.rect);
113 // background (without time conflicts)
115 QStyleOption styleOption;
116 styleOption.rect = option.rect;
117 qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
121 foreach(Control* c, mControls.values()) {
122 c->setEnabled(false);
124 switch (event->favourite()) {
125 case Favourite_strong:
126 mControls[FavouriteControlStrong]->paint(painter, option.rect);
129 mControls[FavouriteControlWeak]->paint(painter, option.rect);
132 mControls[FavouriteControlNo]->paint(painter, option.rect);
136 if(event->hasAlarm())
137 mControls[AlarmControlOn]->paint(painter, option.rect);
139 mControls[AlarmControlOff]->paint(painter, option.rect);
141 if(eventTimeConflict != Favourite_no)
142 mControls[WarningControl]->paint(painter, option.rect);
145 // it starts just below the image
146 // ("position of text" is lower-left angle of the first letter,
147 // so the first line is actually at the same height as the image)
148 painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor));
149 QPointF titlePointF(option.rect.x() + SPACER,
150 option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
151 QTime start = event->start().time();
152 painter->setFont(fontBig);
153 painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
156 titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
157 painter->setFont(fontBigB);
158 QString title = event->title();
159 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
161 // chop words from the end
162 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
165 // chop characters one-by-one from the end
166 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
173 painter->drawText(titlePointF,title);
176 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
177 painter->setFont(fontSmall);
178 QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
179 painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
182 titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
183 painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
186 else // doesn't have parent - time-groups' elements (top items)
188 int numFav = numberOfFavourities(index);
189 int numAlarm = numberOfAlarms(index);
191 QStyleOptionButton styleOptionButton;
192 styleOptionButton.rect = option.rect;
193 if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
194 // styleOptionButton.text = qVariantValue<QString>(index.data());
195 qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
196 // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
197 // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
199 QFont fontSmall = option.font;
200 fontSmall.setBold(true);
201 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
202 QFontMetrics fmSmall(fontSmall);
204 QFont fontBig = option.font;
205 fontBig.setBold(true);
206 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
207 QFontMetrics fmBig(fontBig);
209 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
212 painter->setPen(QPen(textColor));
213 painter->setFont(fontSmall);
214 QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
216 option.rect.topRight()
218 spacer + image->width(),
219 - option.rect.height()/2 + image->height()/2);
220 painter->drawImage(drawPoint,*image);
221 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
222 QString::number(numFav));
224 drawPoint.setX(drawPoint.x() - spacer - image->width());
225 image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
226 painter->drawImage(drawPoint,*image);
227 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
228 QString::number(numAlarm));
231 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
232 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
233 drawPoint.setY(drawPoint.y()+image->height() - 2);
234 painter->drawText(drawPoint,numEvents);
236 QPointF titlePointF = QPoint(
237 option.rect.x()+SPACER,
238 option.rect.y()+option.rect.height()-fmBig.descent());
239 painter->setFont(fontBig);
240 painter->drawText(titlePointF,index.data().value<QString>());
246 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
250 if (index.internalId() == 0) // time group
256 return QSize(100,100);
260 bool Delegate::hasParent( const QModelIndex &index ) const
262 if( index.parent().isValid() )
268 bool Delegate::isLast( const QModelIndex &index ) const
270 if(!hasParent(index))
271 return false; // what should be returned here?
273 if(index.row() >= (index.model()->rowCount(index.parent())-1))
279 bool Delegate::isExpanded( const QModelIndex &index ) const
284 return mViewPtr->isExpanded( index );
287 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
289 if(!hasParent(aIndex)) // time-group item (root item)
292 QListIterator<ControlId> i(mControls.keys());
295 ControlId id = i.next();
296 Control *control = mControls[id];
297 if (control->enabled()
298 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
307 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
309 , mImage(new QImage(aImageName))
310 , mDrawPoint(QPoint(0,0))
314 if (prev_control == NULL) {
315 p = QPoint(0, SPACER);
317 p = prev_control->drawPoint();
319 p.setX(p.x()-image()->width()-SPACER);
323 void Delegate::Control::paint(QPainter* painter, const QRect rect)
325 painter->drawImage(drawPoint(rect),*image());
329 void Delegate::defineControls()
333 mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
335 mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
337 mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
341 mControls.insert(AlarmControlOn,
342 new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
344 mControls.insert(AlarmControlOff,
345 new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
347 mControls.insert(WarningControl,
348 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
351 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
353 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
359 int Delegate::numberOfFavourities(const QModelIndex &index) const
361 if(index.parent().isValid()) // it's event, not time-group
365 for(int i=0; i<index.model()->rowCount(index); i++)
366 if(static_cast<Event*>(index.child(i,0).internalPointer())->favourite() != Favourite_no)
372 int Delegate::numberOfAlarms(const QModelIndex &index) const
374 if(index.parent().isValid()) // it's event, not time-group
378 for(int i=0; i<index.model()->rowCount(index); i++)
379 if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())