2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2021 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/>.
24 #include "eventmodel.h"
26 #include "conference.h"
28 #include "application.h"
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);
93 QFont fontSmallB = fontSmall;
94 fontSmallB.setBold(true);
97 QFont fontBig = option.font;
98 fontBig.setBold(false);
99 fontBig.setPixelSize(aux*0.33);
100 QFontMetrics fmBig(fontBig);
103 QFont fontBigB = fontBig;
104 fontBigB.setBold(true);
105 QFontMetrics fmBigB(fontBigB);
107 // background (in case of time conflicts)
108 if (conflictSeverity != csNone) {
109 painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow"));
110 painter->setPen(Qt::NoPen);
111 painter->drawRect(option.rect);
114 // background (without time conflicts)
116 QStyleOption styleOption;
117 styleOption.rect = option.rect;
118 qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
122 foreach(Control* c, mControls.values()) {
123 c->setEnabled(false);
125 switch (event->favourite()) {
126 case Favourite_strong:
127 mControls[FavouriteControlStrong]->paint(painter, option.rect);
130 mControls[FavouriteControlWeak]->paint(painter, option.rect);
133 mControls[FavouriteControlNo]->paint(painter, option.rect);
137 if(event->hasAlarm())
138 mControls[AlarmControlOn]->paint(painter, option.rect);
140 mControls[AlarmControlOff]->paint(painter, option.rect);
142 if(eventTimeConflict != Favourite_no)
143 mControls[WarningControl]->paint(painter, option.rect);
146 // it starts just below the image
147 // ("position of text" is lower-left angle of the first letter,
148 // so the first line is actually at the same height as the image)
149 painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor));
150 QPointF titlePointF(option.rect.x() + SPACER,
151 option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
152 Conference& conference = ((Application*) qApp)->activeConference();
153 QTime start = conference.shiftTime(event->start().time());
154 painter->setFont(fontBig);
155 painter->drawText(titlePointF, start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
158 titlePointF.setY(titlePointF.y() + fmBig.lineSpacing());
159 painter->setFont(fontBigB);
160 QString title = event->title();
161 if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
163 // chop words from the end
164 while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
167 // chop characters one-by-one from the end
168 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
175 painter->drawText(titlePointF, title);
178 titlePointF.setY(titlePointF.y() + fmBigB.descent() + fmSmall.lineSpacing());
179 painter->setFont(fontSmall);
180 QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
181 painter->drawText(titlePointF, presenterPrefix + ": " + event->persons().join(" and "));
184 titlePointF.setY(titlePointF.y() + fmSmall.lineSpacing());
185 painter->drawText(titlePointF, "Track: " + Track::retrieveTrackName(event->trackId()));
188 else // doesn't have parent - time-groups' elements (top items)
190 int numFav = numberOfFavourites(index);
191 int numAlarm = numberOfAlarms(index);
193 QStyleOptionButton styleOptionButton;
194 styleOptionButton.rect = option.rect;
195 if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
196 // styleOptionButton.text = qVariantValue<QString>(index.data());
197 qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
198 // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
199 // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
201 QFont fontSmall = option.font;
202 fontSmall.setBold(true);
203 fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
204 QFontMetrics fmSmall(fontSmall);
206 QFont fontBig = option.font;
207 fontBig.setBold(true);
208 fontBig.setPixelSize(option.rect.height()*scaleFactor2);
209 QFontMetrics fmBig(fontBig);
211 int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
214 painter->setPen(QPen(textColor));
215 painter->setFont(fontSmall);
216 QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
218 option.rect.topRight()
220 spacer + image->width(),
221 - option.rect.height()/2 + image->height()/2);
222 painter->drawImage(drawPoint,*image);
223 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
224 QString::number(numFav));
226 drawPoint.setX(drawPoint.x() - spacer - image->width());
227 image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
228 painter->drawImage(drawPoint,*image);
229 painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
230 QString::number(numAlarm));
233 QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
234 drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
235 drawPoint.setY(drawPoint.y()+image->height() - 2);
236 painter->drawText(drawPoint,numEvents);
238 QPointF titlePointF = QPoint(
239 option.rect.x()+SPACER,
240 option.rect.y()+option.rect.height()-fmBig.descent());
241 painter->setFont(fontBig);
242 painter->drawText(titlePointF,index.data().value<QString>());
248 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
252 if (index.internalId() == 0) // time group
258 return QSize(100,100);
262 bool Delegate::hasParent( const QModelIndex &index ) const
264 if( index.parent().isValid() )
270 bool Delegate::isLast( const QModelIndex &index ) const
272 if(!hasParent(index))
273 return false; // what should be returned here?
275 if(index.row() >= (index.model()->rowCount(index.parent())-1))
281 bool Delegate::isExpanded( const QModelIndex &index ) const
286 return mViewPtr->isExpanded( index );
289 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
291 if(!hasParent(aIndex)) // time-group item (root item)
294 QListIterator<ControlId> i(mControls.keys());
297 ControlId id = i.next();
298 Control *control = mControls[id];
299 if (control->enabled()
300 and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
309 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
311 , mImage(new QImage(aImageName))
312 , mDrawPoint(QPoint(0,0))
316 if (prev_control == NULL) {
317 p = QPoint(0, SPACER);
319 p = prev_control->drawPoint();
321 p.setX(p.x()-image()->width()-SPACER);
325 void Delegate::Control::paint(QPainter* painter, const QRect rect)
327 painter->drawImage(drawPoint(rect),*image());
331 void Delegate::defineControls()
335 mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
337 mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
339 mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
343 mControls.insert(AlarmControlOn,
344 new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
346 mControls.insert(AlarmControlOff,
347 new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
349 mControls.insert(WarningControl,
350 new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
353 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
355 if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
361 int Delegate::numberOfFavourites(const QModelIndex &index) const
363 if(index.parent().isValid()) // it's event, not time-group
367 for(int i=0; i<index.model()->rowCount(index); i++)
368 if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->favourite() != Favourite_no)
374 int Delegate::numberOfAlarms(const QModelIndex &index) const
376 if(index.parent().isValid()) // it's event, not time-group
380 for(int i=0; i<index.model()->rowCount(index); i++)
381 if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->hasAlarm())