[ca90cb1] | 1 | /* |
---|
| 2 | * Copyright (C) 2010 Ixonos Plc. |
---|
[4a87a3b] | 3 | * Copyright (C) 2011-2015 Philipp Spitzer, gregor herrmann, Stefan Stahl |
---|
[ca90cb1] | 4 | * |
---|
[6df32f2] | 5 | * This file is part of ConfClerk. |
---|
[ca90cb1] | 6 | * |
---|
[6df32f2] | 7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
[ca90cb1] | 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) |
---|
| 10 | * any later version. |
---|
| 11 | * |
---|
[6df32f2] | 12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
[ca90cb1] | 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 |
---|
| 15 | * more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License along with |
---|
[6df32f2] | 18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
[ca90cb1] | 19 | */ |
---|
[66327a0] | 20 | #include "delegate.h" |
---|
| 21 | #include "eventmodel.h" |
---|
[7680536] | 22 | #include "track.h" |
---|
[66327a0] | 23 | |
---|
| 24 | #include <QDebug> |
---|
| 25 | #include <QPainter> |
---|
| 26 | |
---|
[508de33] | 27 | #include "room.h" |
---|
| 28 | |
---|
[66327a0] | 29 | const int RADIUS = 10; |
---|
[b1a201a] | 30 | const int SPACER = 10; |
---|
[5d7c9e5] | 31 | |
---|
| 32 | const double scaleFactor1 = 0.4; |
---|
| 33 | const double scaleFactor2 = 0.8; |
---|
[66327a0] | 34 | |
---|
| 35 | Delegate::Delegate(QTreeView *aParent) |
---|
| 36 | : QItemDelegate(aParent) |
---|
| 37 | , mViewPtr(aParent) |
---|
| 38 | { |
---|
| 39 | mControls.clear(); |
---|
| 40 | defineControls(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | Delegate::~Delegate() |
---|
| 44 | { |
---|
| 45 | QListIterator<ControlId> i(mControls.keys()); |
---|
| 46 | while (i.hasNext()) |
---|
| 47 | { |
---|
| 48 | delete mControls[i.next()]->image(); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
---|
| 53 | { |
---|
[be12b9f] | 54 | if (!mViewPtr) |
---|
[66327a0] | 55 | return; |
---|
| 56 | |
---|
| 57 | painter->save(); |
---|
[699d5b8] | 58 | |
---|
[268d58f] | 59 | QColor textColor = option.palette.color(QPalette::Text); |
---|
[be12b9f] | 60 | |
---|
[66327a0] | 61 | if(hasParent(index)) |
---|
| 62 | { |
---|
[1343ea4] | 63 | Event *event = static_cast<Event*>(index.internalPointer()); |
---|
| 64 | |
---|
| 65 | // determine severity of conflict |
---|
| 66 | Favourite eventTimeConflict = event->timeConflict(); // cache value as event->timeConflict is expensive |
---|
| 67 | enum ConflictSeverity {csNone, csWeak, csStrong} conflictSeverity; |
---|
| 68 | switch (event->favourite()) { |
---|
| 69 | case Favourite_strong: |
---|
| 70 | conflictSeverity = (eventTimeConflict == Favourite_strong) ? csStrong : csNone; |
---|
| 71 | break; |
---|
| 72 | case Favourite_weak: |
---|
| 73 | conflictSeverity = (eventTimeConflict == Favourite_no) ? csNone : csWeak; |
---|
| 74 | break; |
---|
| 75 | case Favourite_no: |
---|
| 76 | conflictSeverity = csNone; |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | |
---|
[268d58f] | 80 | // entry horizontal layout: |
---|
[525448c] | 81 | // * spacer (aka y position of image) |
---|
| 82 | // * image |
---|
| 83 | // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track |
---|
[7b3cd0e] | 84 | int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height(); |
---|
[be12b9f] | 85 | |
---|
[b1a201a] | 86 | // font SMALL |
---|
| 87 | QFont fontSmall = option.font; |
---|
| 88 | fontSmall.setBold(false); |
---|
| 89 | fontSmall.setPixelSize(aux*0.2); |
---|
| 90 | QFontMetrics fmSmall(fontSmall); |
---|
| 91 | // font SMALL bold |
---|
| 92 | QFont fontSmallB = fontSmall; |
---|
| 93 | fontSmallB.setBold(true); |
---|
| 94 | |
---|
| 95 | // font BIG |
---|
| 96 | QFont fontBig = option.font; |
---|
| 97 | fontBig.setBold(false); |
---|
| 98 | fontBig.setPixelSize(aux*0.33); |
---|
| 99 | QFontMetrics fmBig(fontBig); |
---|
[be12b9f] | 100 | |
---|
[b1a201a] | 101 | // font BIG bold |
---|
| 102 | QFont fontBigB = fontBig; |
---|
| 103 | fontBigB.setBold(true); |
---|
| 104 | QFontMetrics fmBigB(fontBigB); |
---|
| 105 | |
---|
[be12b9f] | 106 | // background (in case of time conflicts) |
---|
[1343ea4] | 107 | if (conflictSeverity != csNone) { |
---|
| 108 | painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow")); |
---|
[66327a0] | 109 | painter->setPen(Qt::NoPen); |
---|
| 110 | painter->drawRect(option.rect); |
---|
[be12b9f] | 111 | } |
---|
[66327a0] | 112 | |
---|
[be12b9f] | 113 | // background (without time conflicts) |
---|
| 114 | else { |
---|
| 115 | QStyleOption styleOption; |
---|
| 116 | styleOption.rect = option.rect; |
---|
| 117 | qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr); |
---|
[66327a0] | 118 | } |
---|
| 119 | |
---|
| 120 | // draw Controls |
---|
[a6a9e0b] | 121 | foreach(Control* c, mControls.values()) { |
---|
| 122 | c->setEnabled(false); |
---|
| 123 | } |
---|
[7b3cd0e] | 124 | switch (event->favourite()) { |
---|
| 125 | case Favourite_strong: |
---|
| 126 | mControls[FavouriteControlStrong]->paint(painter, option.rect); |
---|
| 127 | break; |
---|
| 128 | case Favourite_weak: |
---|
| 129 | mControls[FavouriteControlWeak]->paint(painter, option.rect); |
---|
| 130 | break; |
---|
| 131 | case Favourite_no: |
---|
| 132 | mControls[FavouriteControlNo]->paint(painter, option.rect); |
---|
| 133 | break; |
---|
| 134 | } |
---|
[54a377c] | 135 | |
---|
[508de33] | 136 | if(event->hasAlarm()) |
---|
[525448c] | 137 | mControls[AlarmControlOn]->paint(painter, option.rect); |
---|
[b6cd05c] | 138 | else |
---|
[525448c] | 139 | mControls[AlarmControlOff]->paint(painter, option.rect); |
---|
[54a377c] | 140 | |
---|
[1343ea4] | 141 | if(eventTimeConflict != Favourite_no) |
---|
[525448c] | 142 | mControls[WarningControl]->paint(painter, option.rect); |
---|
[5d7c9e5] | 143 | |
---|
| 144 | // draw texts |
---|
[525448c] | 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) |
---|
[1343ea4] | 148 | painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor)); |
---|
[525448c] | 149 | QPointF titlePointF(option.rect.x() + SPACER, |
---|
[7b3cd0e] | 150 | option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height()); |
---|
[b1a201a] | 151 | QTime start = event->start().time(); |
---|
| 152 | painter->setFont(fontBig); |
---|
[0d995ed] | 153 | painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName()); |
---|
[be12b9f] | 154 | |
---|
[b1a201a] | 155 | // title |
---|
| 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 |
---|
| 160 | { |
---|
| 161 | // chop words from the end |
---|
| 162 | while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty()) |
---|
| 163 | { |
---|
| 164 | title.chop(1); |
---|
| 165 | // chop characters one-by-one from the end |
---|
| 166 | while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty()) |
---|
| 167 | { |
---|
| 168 | title.chop(1); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | title += "..."; |
---|
| 172 | } |
---|
| 173 | painter->drawText(titlePointF,title); |
---|
[be12b9f] | 174 | |
---|
[b1a201a] | 175 | // persons |
---|
| 176 | titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent()); |
---|
| 177 | painter->setFont(fontSmall); |
---|
[d785a93] | 178 | QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters"; |
---|
| 179 | painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and ")); |
---|
[be12b9f] | 180 | |
---|
[b1a201a] | 181 | // track |
---|
| 182 | titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent()); |
---|
[005e2b7] | 183 | painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId())); |
---|
[66327a0] | 184 | } |
---|
[be12b9f] | 185 | |
---|
[66327a0] | 186 | else // doesn't have parent - time-groups' elements (top items) |
---|
| 187 | { |
---|
[be12b9f] | 188 | int numFav = numberOfFavourities(index); |
---|
[7947db3] | 189 | int numAlarm = numberOfAlarms(index); |
---|
[be12b9f] | 190 | |
---|
| 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); |
---|
| 198 | |
---|
[b1a201a] | 199 | QFont fontSmall = option.font; |
---|
| 200 | fontSmall.setBold(true); |
---|
| 201 | fontSmall.setPixelSize(option.rect.height()*scaleFactor1); |
---|
| 202 | QFontMetrics fmSmall(fontSmall); |
---|
| 203 | |
---|
| 204 | QFont fontBig = option.font; |
---|
| 205 | fontBig.setBold(true); |
---|
| 206 | fontBig.setPixelSize(option.rect.height()*scaleFactor2); |
---|
| 207 | QFontMetrics fmBig(fontBig); |
---|
| 208 | |
---|
| 209 | int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width(); |
---|
| 210 | |
---|
[268d58f] | 211 | // draw icons |
---|
[be12b9f] | 212 | painter->setPen(QPen(textColor)); |
---|
[5d7c9e5] | 213 | painter->setFont(fontSmall); |
---|
[7b3cd0e] | 214 | QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image(); |
---|
[4be95b8] | 215 | QPoint drawPoint = |
---|
| 216 | option.rect.topRight() |
---|
| 217 | - QPoint( |
---|
[b0a3bc7] | 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), |
---|
[be12b9f] | 222 | QString::number(numFav)); |
---|
[54a377c] | 223 | |
---|
[b0a3bc7] | 224 | drawPoint.setX(drawPoint.x() - spacer - image->width()); |
---|
[7947db3] | 225 | image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image(); |
---|
| 226 | painter->drawImage(drawPoint,*image); |
---|
[b0a3bc7] | 227 | painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2), |
---|
[7947db3] | 228 | QString::number(numAlarm)); |
---|
[54a377c] | 229 | |
---|
[4be95b8] | 230 | // draw texts |
---|
[5d7c9e5] | 231 | QString numEvents = QString::number(index.model()->rowCount(index)).append("/"); |
---|
| 232 | drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width()); |
---|
[b0a3bc7] | 233 | drawPoint.setY(drawPoint.y()+image->height() - 2); |
---|
[5d7c9e5] | 234 | painter->drawText(drawPoint,numEvents); |
---|
| 235 | |
---|
| 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,qVariantValue<QString>(index.data())); |
---|
[66327a0] | 241 | } |
---|
| 242 | |
---|
| 243 | painter->restore(); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
---|
| 247 | { |
---|
| 248 | Q_UNUSED(option) |
---|
| 249 | |
---|
| 250 | if (index.internalId() == 0) // time group |
---|
| 251 | { |
---|
[a3f6b00] | 252 | return QSize(40,40); |
---|
[66327a0] | 253 | } |
---|
| 254 | else // event |
---|
| 255 | { |
---|
| 256 | return QSize(100,100); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | bool Delegate::hasParent( const QModelIndex &index ) const |
---|
| 261 | { |
---|
| 262 | if( index.parent().isValid() ) |
---|
| 263 | return true; |
---|
| 264 | else |
---|
| 265 | return false; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | bool Delegate::isLast( const QModelIndex &index ) const |
---|
| 269 | { |
---|
| 270 | if(!hasParent(index)) |
---|
| 271 | return false; // what should be returned here? |
---|
| 272 | |
---|
| 273 | if(index.row() >= (index.model()->rowCount(index.parent())-1)) |
---|
| 274 | return true; |
---|
| 275 | else |
---|
| 276 | return false; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | bool Delegate::isExpanded( const QModelIndex &index ) const |
---|
| 280 | { |
---|
| 281 | if( !mViewPtr ) |
---|
| 282 | return false; |
---|
| 283 | else |
---|
| 284 | return mViewPtr->isExpanded( index ); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const |
---|
| 288 | { |
---|
| 289 | if(!hasParent(aIndex)) // time-group item (root item) |
---|
| 290 | return ControlNone; |
---|
| 291 | |
---|
| 292 | QListIterator<ControlId> i(mControls.keys()); |
---|
| 293 | while (i.hasNext()) |
---|
| 294 | { |
---|
| 295 | ControlId id = i.next(); |
---|
[900bcf1] | 296 | Control *control = mControls[id]; |
---|
| 297 | if (control->enabled() |
---|
| 298 | and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint)) |
---|
[e7340e1] | 299 | { |
---|
[900bcf1] | 300 | return id; |
---|
[3f3e22d] | 301 | } |
---|
[66327a0] | 302 | } |
---|
| 303 | |
---|
| 304 | return ControlNone; |
---|
| 305 | } |
---|
| 306 | |
---|
[525448c] | 307 | Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control) |
---|
| 308 | : mId(aControlId) |
---|
| 309 | , mImage(new QImage(aImageName)) |
---|
| 310 | , mDrawPoint(QPoint(0,0)) |
---|
[a6a9e0b] | 311 | , mEnabled(false) |
---|
[525448c] | 312 | { |
---|
| 313 | QPoint p; |
---|
| 314 | if (prev_control == NULL) { |
---|
| 315 | p = QPoint(0, SPACER); |
---|
| 316 | } else { |
---|
| 317 | p = prev_control->drawPoint(); |
---|
| 318 | } |
---|
| 319 | p.setX(p.x()-image()->width()-SPACER); |
---|
| 320 | setDrawPoint(p); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | void Delegate::Control::paint(QPainter* painter, const QRect rect) |
---|
| 324 | { |
---|
| 325 | painter->drawImage(drawPoint(rect),*image()); |
---|
[a6a9e0b] | 326 | setEnabled(true); |
---|
[525448c] | 327 | } |
---|
| 328 | |
---|
[66327a0] | 329 | void Delegate::defineControls() |
---|
| 330 | { |
---|
[680a4da] | 331 | // FAVOURITE ICONs |
---|
[7b3cd0e] | 332 | // strong |
---|
| 333 | mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-on.png"), NULL)); |
---|
| 334 | // weak |
---|
| 335 | mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL)); |
---|
| 336 | // no |
---|
| 337 | mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-off.png"), NULL)); |
---|
[680a4da] | 338 | |
---|
| 339 | // ALARM ICONs |
---|
| 340 | // on |
---|
[525448c] | 341 | mControls.insert(AlarmControlOn, |
---|
[7b3cd0e] | 342 | new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong])); |
---|
[680a4da] | 343 | // off |
---|
[525448c] | 344 | mControls.insert(AlarmControlOff, |
---|
[7b3cd0e] | 345 | new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo])); |
---|
[ec813bb] | 346 | // WARNING ICON |
---|
| 347 | mControls.insert(WarningControl, |
---|
| 348 | new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff])); |
---|
[54a377c] | 349 | } |
---|
[66327a0] | 350 | |
---|
| 351 | bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const |
---|
| 352 | { |
---|
| 353 | if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) ) |
---|
| 354 | return true; |
---|
| 355 | |
---|
| 356 | return false; |
---|
| 357 | } |
---|
| 358 | |
---|
[4be95b8] | 359 | int Delegate::numberOfFavourities(const QModelIndex &index) const |
---|
| 360 | { |
---|
| 361 | if(index.parent().isValid()) // it's event, not time-group |
---|
| 362 | return 0; |
---|
| 363 | |
---|
| 364 | int nrofFavs = 0; |
---|
| 365 | for(int i=0; i<index.model()->rowCount(index); i++) |
---|
[7b3cd0e] | 366 | if(static_cast<Event*>(index.child(i,0).internalPointer())->favourite() != Favourite_no) |
---|
[4be95b8] | 367 | nrofFavs++; |
---|
| 368 | |
---|
| 369 | return nrofFavs; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | int Delegate::numberOfAlarms(const QModelIndex &index) const |
---|
| 373 | { |
---|
| 374 | if(index.parent().isValid()) // it's event, not time-group |
---|
| 375 | return 0; |
---|
| 376 | |
---|
| 377 | int nrofAlarms = 0; |
---|
| 378 | for(int i=0; i<index.model()->rowCount(index); i++) |
---|
| 379 | if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm()) |
---|
| 380 | nrofAlarms++; |
---|
| 381 | |
---|
| 382 | return nrofAlarms; |
---|
| 383 | } |
---|
| 384 | |
---|