Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Edric Milaret | bab169d | 2016-01-07 15:13:33 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 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" |
| 30 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 31 | SmartListDelegate::SmartListDelegate(QObject* parent) : |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 32 | QItemDelegate(parent) |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 33 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 37 | SmartListDelegate::paint(QPainter* painter |
| 38 | , const QStyleOptionViewItem& option |
| 39 | , const QModelIndex& index |
| 40 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 41 | { |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 42 | QStyleOptionViewItem opt(option); |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 43 | painter->setRenderHint(QPainter::Antialiasing); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame^] | 44 | |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 45 | opt.decorationSize = QSize(sizeImage_, sizeImage_); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 46 | opt.decorationPosition = QStyleOptionViewItem::Left; |
| 47 | opt.decorationAlignment = Qt::AlignCenter; |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame^] | 48 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 49 | if (opt.state & QStyle::State_HasFocus) |
| 50 | opt.state ^= QStyle::State_HasFocus; |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 51 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 52 | QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 53 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 54 | |
| 55 | QRect rect = opt.rect; |
| 56 | rect.setLeft(0); |
| 57 | |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame^] | 58 | QRect rectTexts(16 + rect.left() + dx_ + sizeImage_, |
| 59 | rect.top(), |
| 60 | rect.width(), |
| 61 | rect.height() / 2); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 62 | QRect rectAvatar(16 + rect.left(), rect.top() + dy_, sizeImage_, sizeImage_); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame^] | 63 | drawDecoration(painter, opt, rectAvatar, |
| 64 | QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>())); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 65 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 66 | const int currentRow = index.row(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 67 | if (currentRow == rowHighlighted_) |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 68 | emit rowSelected(opt.rect); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 69 | |
| 70 | QFont font(painter->font()); |
| 71 | font.setPointSize(fontSize_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 72 | |
| 73 | QPen pen(painter->pen()); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 74 | |
| 75 | if (not (opt.state & QStyle::State_Selected)) { |
| 76 | pen.setColor(lightGrey_); |
| 77 | painter->setPen(pen); |
| 78 | painter->drawLine(rect.left() + 20, rect.bottom(), |
| 79 | rect.right() - 20, |
| 80 | rect.bottom()); |
| 81 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 82 | if (index.column() == 0) |
| 83 | { |
| 84 | painter->setPen(pen); |
| 85 | |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 86 | QVariant name = index.data(static_cast<int>(Ring::Role::Name)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 87 | if (name.isValid()) |
| 88 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 89 | pen.setColor(lightBlack_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 90 | painter->setPen(pen); |
| 91 | font.setBold(true); |
| 92 | painter->setFont(font); |
Edric Milaret | 645e4c3 | 2016-01-18 15:55:08 -0500 | [diff] [blame^] | 93 | QFontMetrics fontMetrics(font); |
| 94 | QString nameStr = name.toString(); |
| 95 | auto realRect = fontMetrics.boundingRect(rect, Qt::AlignBottom | Qt::AlignLeft, nameStr); |
| 96 | if (realRect.width() > (rect.width() - rectTexts.left() - 30)) { |
| 97 | /* 30 here is the size of the video button */ |
| 98 | auto charToChop = (realRect.width() - (rect.width() - rectTexts.left())) / fontMetrics.averageCharWidth(); |
| 99 | nameStr.chop(charToChop + 8); |
| 100 | nameStr.append(QStringLiteral("...")); |
| 101 | } |
| 102 | painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, nameStr); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState)); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 106 | pen.setColor(grey_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 107 | painter->setPen(pen); |
| 108 | font.setBold(false); |
| 109 | painter->setFont(font); |
| 110 | rectTexts.moveTop(cellHeight_/2); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 111 | if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index))) |
| 112 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 113 | painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_, |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 114 | rect.top() + rect.height()/2, |
| 115 | rect.width(), rect.height()/2), |
| 116 | Qt::AlignTop | Qt::AlignLeft, state.toString()); |
| 117 | } |
| 118 | else |
| 119 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 120 | QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 121 | if (lastUsed.isValid()) |
| 122 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 123 | painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_, |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 124 | rect.top() + rect.height()/2, |
| 125 | rect.width(), rect.height()/2), |
| 126 | Qt::AlignTop | Qt::AlignLeft, lastUsed.toString()); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 132 | QItemDelegate::paint(painter, opt, index); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | QSize |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 137 | SmartListDelegate::sizeHint(const QStyleOptionViewItem& option |
| 138 | , const QModelIndex& index |
| 139 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 140 | { |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 141 | QSize size = QItemDelegate::sizeHint(option, index); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 142 | size.setHeight(cellHeight_); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 143 | return size; |
| 144 | } |