Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony Léonard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "smartlistdelegate.h" |
| 20 | |
| 21 | #include <QApplication> |
| 22 | #include <QSortFilterProxyModel> |
| 23 | #include <QPainter> |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 24 | #include <QPixmap> |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 25 | |
| 26 | #include "itemdataroles.h" |
| 27 | #include "person.h" |
| 28 | #include "recentmodel.h" |
| 29 | #include "call.h" |
Nicolas Jager | 389a229 | 2016-02-03 12:30:09 -0500 | [diff] [blame] | 30 | #include "combar.h" |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 31 | |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 32 | #include "ringthemeutils.h" |
| 33 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 34 | SmartListDelegate::SmartListDelegate(QObject* parent) : |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 35 | QItemDelegate(parent) |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 36 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 40 | SmartListDelegate::paint(QPainter* painter |
| 41 | , const QStyleOptionViewItem& option |
| 42 | , const QModelIndex& index |
| 43 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 44 | { |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 45 | QStyleOptionViewItem opt(option); |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 46 | painter->setRenderHint(QPainter::Antialiasing); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame] | 47 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 48 | // Not having focus removes dotted lines around the item |
| 49 | if (opt.state & QStyle::State_HasFocus) |
| 50 | opt.state ^= QStyle::State_HasFocus; |
| 51 | |
| 52 | // First, we draw the control itself |
| 53 | QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 54 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 55 | |
| 56 | QRect &rect = opt.rect; |
| 57 | |
| 58 | // Avatar drawing |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 59 | opt.decorationSize = QSize(sizeImage_, sizeImage_); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 60 | opt.decorationPosition = QStyleOptionViewItem::Left; |
| 61 | opt.decorationAlignment = Qt::AlignCenter; |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame] | 62 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 63 | QRect rectAvatar(16 + rect.left(), rect.top() + dy_, sizeImage_, sizeImage_); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame] | 64 | drawDecoration(painter, opt, rectAvatar, |
| 65 | QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>())); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 66 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 67 | QFont font(painter->font()); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 68 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 69 | // If there's unread messages, a message count is displayed |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 70 | if (auto messageCount = index.data(static_cast<int>(Ring::Role::UnreadTextMessageCount)).toInt()) { |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 71 | font.setPointSize(8); |
| 72 | QFontMetrics textFontMetrics(font); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 73 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 74 | // If there is more than 10 unread messages, "10+" is displayed |
| 75 | QString messageCountText = (messageCount > 10)? "10+" : QString::number(messageCount); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 76 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 77 | // This QRect is the bounding one containing the message count to be displayed |
| 78 | QRect pastilleRect = textFontMetrics.boundingRect(QRect(rectAvatar.left() + sizeImage_/3, |
| 79 | rectAvatar.bottom() - textFontMetrics.height(), sizeImage_, textFontMetrics.height()), |
| 80 | Qt::AlignCenter, messageCountText); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 81 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 82 | // This one is larger than pastilleRect and is used to prepare the red background |
| 83 | QRect bubbleRect(pastilleRect.left() - 3, pastilleRect.top(), |
| 84 | pastilleRect.width() + 6, pastilleRect.height() + 1); |
| 85 | |
| 86 | // The background is displayed |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 87 | QPainterPath path; |
| 88 | path.addRoundedRect(bubbleRect, 3, 3); |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 89 | QPen pen(RingTheme::red_, 5); |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 90 | painter->setOpacity(0.9); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 91 | painter->setPen(pen); |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 92 | painter->fillPath(path, RingTheme::red_); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 93 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 94 | // Then the message count |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 95 | painter->setPen(Qt::white); |
| 96 | painter->setOpacity(1); |
| 97 | painter->setFont(font); |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 98 | painter->drawText(pastilleRect, Qt::AlignCenter, messageCountText); |
Edric Milaret | 23fd3fa | 2016-01-15 15:37:35 -0500 | [diff] [blame] | 99 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 100 | |
Anthony Léonard | a8d78f9 | 2017-04-28 11:40:29 -0400 | [diff] [blame] | 101 | // Presence indicator |
| 102 | QPainterPath outerCircle, innerCircle; |
| 103 | QPointF center(rectAvatar.left() + sizeImage_/6, (rectAvatar.bottom() - sizeImage_/6) + 1); |
| 104 | qreal outerCRadius = sizeImage_ / 6, innerCRadius = outerCRadius * 0.75; |
| 105 | outerCircle.addEllipse(center, outerCRadius, outerCRadius); |
| 106 | innerCircle.addEllipse(center, innerCRadius, innerCRadius); |
| 107 | if (index.data(static_cast<int>(Ring::Role::IsPresent)).value<bool>()) { |
| 108 | painter->fillPath(outerCircle, Qt::white); |
| 109 | painter->fillPath(innerCircle, RingTheme::green_); |
| 110 | } |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 111 | font.setPointSize(fontSize_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 112 | QPen pen(painter->pen()); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 113 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 114 | // A lightGrey separator is painted at the bottom if the current item is not selected |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 115 | if (not (opt.state & QStyle::State_Selected)) { |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 116 | pen.setColor(RingTheme::lightGrey_); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 117 | painter->setPen(pen); |
| 118 | painter->drawLine(rect.left() + 20, rect.bottom(), |
| 119 | rect.right() - 20, |
| 120 | rect.bottom()); |
| 121 | } |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 122 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 123 | if (index.column() == 0) |
| 124 | { |
| 125 | painter->setPen(pen); |
| 126 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 127 | QRect rectTexts(16 + rect.left() + dx_ + sizeImage_, |
| 128 | rect.top(), |
| 129 | rect.width(), |
| 130 | rect.height() / 2); |
| 131 | |
| 132 | // The name is displayed at the avatar's left |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 133 | QVariant name = index.data(static_cast<int>(Ring::Role::Name)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 134 | if (name.isValid()) |
| 135 | { |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 136 | pen.setColor(RingTheme::lightBlack_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 137 | painter->setPen(pen); |
| 138 | font.setBold(true); |
| 139 | painter->setFont(font); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame] | 140 | QFontMetrics fontMetrics(font); |
Nicolas Jager | 389a229 | 2016-02-03 12:30:09 -0500 | [diff] [blame] | 141 | QString nameStr = fontMetrics.elidedText(name.toString(), Qt::ElideRight |
| 142 | , rectTexts.width()- sizeImage_ - effectiveComBarSize_ - dx_); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame] | 143 | painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, nameStr); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Anthony Léonard | 522c2f1 | 2017-04-21 10:59:06 -0400 | [diff] [blame] | 146 | // Finally, either last interaction date or call state is displayed |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 147 | QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState)); |
Edric Milaret | 4e37099 | 2016-01-20 11:29:32 -0500 | [diff] [blame] | 148 | pen.setColor(RingTheme::grey_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 149 | painter->setPen(pen); |
| 150 | font.setBold(false); |
| 151 | painter->setFont(font); |
| 152 | rectTexts.moveTop(cellHeight_/2); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 153 | if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index))) |
| 154 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 155 | painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_, |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 156 | rect.top() + rect.height()/2, |
| 157 | rect.width(), rect.height()/2), |
| 158 | Qt::AlignTop | Qt::AlignLeft, state.toString()); |
| 159 | } |
| 160 | else |
| 161 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 162 | QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 163 | if (lastUsed.isValid()) |
| 164 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 165 | painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_, |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 166 | rect.top() + rect.height()/2, |
| 167 | rect.width(), rect.height()/2), |
| 168 | Qt::AlignTop | Qt::AlignLeft, lastUsed.toString()); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 174 | QItemDelegate::paint(painter, opt, index); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | QSize |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 179 | SmartListDelegate::sizeHint(const QStyleOptionViewItem& option |
| 180 | , const QModelIndex& index |
| 181 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 182 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 183 | QSize size = QItemDelegate::sizeHint(option, index); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 184 | size.setHeight(cellHeight_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 185 | return size; |
| 186 | } |