Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-faire Linux * |
| 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> |
| 24 | |
| 25 | #include "itemdataroles.h" |
| 26 | #include "person.h" |
| 27 | #include "recentmodel.h" |
| 28 | #include "call.h" |
| 29 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 30 | SmartListDelegate::SmartListDelegate(QObject* parent) : |
| 31 | QStyledItemDelegate(parent) |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 32 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 36 | SmartListDelegate::paint(QPainter* painter |
| 37 | , const QStyleOptionViewItem& option |
| 38 | , const QModelIndex& index |
| 39 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 40 | { |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 41 | QStyleOptionViewItem opt(option); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 42 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 43 | QFont font(painter->font()); |
| 44 | font.setPointSize(10); |
| 45 | |
| 46 | const int currentRow = index.row(); |
| 47 | |
| 48 | QRect rect = opt.rect; |
| 49 | rect.setLeft(0); |
| 50 | |
| 51 | QRect rectTexts(16 + rect.left() + dx+sizeImage_, rect.top(), rect.width(), rect.height()/2); |
| 52 | QRect rectAvatar(16 + rect.left(), rect.top() + dy, sizeImage_, sizeImage_); |
| 53 | |
| 54 | QLinearGradient linearGradient(0, 0, 100, 100); |
| 55 | linearGradient.setColorAt(0.0, Qt::white); |
| 56 | linearGradient.setColorAt(1.0, Qt::black); |
| 57 | |
| 58 | if (currentRow == rowHighlighted_) |
| 59 | { |
| 60 | painter->fillRect(opt.rect, QColor(242, 242, 242)); |
| 61 | emit rowSelected(opt.rect); |
| 62 | } |
| 63 | |
| 64 | QPen pen(painter->pen()); |
| 65 | pen.setColor(QColor(242, 242, 242)); |
| 66 | painter->setPen(pen); |
| 67 | painter->drawLine(rect.left() + 20, rect.bottom(), |
| 68 | rect.right() - 20, |
| 69 | rect.bottom()); |
| 70 | if (index.column() == 0) |
| 71 | { |
| 72 | painter->setPen(pen); |
| 73 | |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 74 | QVariant name = index.data(static_cast<int>(Ring::Role::Name)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 75 | if (name.isValid()) |
| 76 | { |
| 77 | pen.setColor(QColor(63, 63, 63)); |
| 78 | painter->setPen(pen); |
| 79 | font.setBold(true); |
| 80 | painter->setFont(font); |
| 81 | painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, name.toString()); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 85 | |
| 86 | pen.setColor(QColor(192, 192, 192)); |
| 87 | painter->setPen(pen); |
| 88 | font.setBold(false); |
| 89 | painter->setFont(font); |
| 90 | rectTexts.moveTop(cellHeight_/2); |
| 91 | |
| 92 | if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index))) |
| 93 | { |
| 94 | painter->drawText(QRect(16 + rect.left() + dx + sizeImage_, |
| 95 | rect.top() + rect.height()/2, |
| 96 | rect.width(), rect.height()/2), |
| 97 | Qt::AlignTop | Qt::AlignLeft, state.toString()); |
| 98 | } |
| 99 | else |
| 100 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 101 | QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed)); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 102 | if (lastUsed.isValid()) |
| 103 | { |
| 104 | painter->drawText(QRect(16 + rect.left() + dx + sizeImage_, |
| 105 | rect.top() + rect.height()/2, |
| 106 | rect.width(), rect.height()/2), |
| 107 | Qt::AlignTop | Qt::AlignLeft, lastUsed.toString()); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 110 | |
| 111 | painter->setPen(pen); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 112 | QVariant var_p = index.data(static_cast<int>(Ring::Role::Object)); |
| 113 | auto person = var_p.value<Person*>(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 114 | if (person && not person->photo().value<QImage>().isNull()) |
| 115 | { |
| 116 | painter->drawImage(rectAvatar, person->photo().value<QImage>()); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 117 | } |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 118 | else |
| 119 | { |
| 120 | QImage defaultImage(":images/user/btn-default-userpic.svg"); |
| 121 | painter->drawImage(rectAvatar, defaultImage); |
| 122 | } |
| 123 | |
| 124 | if (currentRow == rowHighlighted_) |
| 125 | { |
| 126 | QLinearGradient gradient(opt.rect.topLeft(),opt.rect.topRight()); |
| 127 | gradient.setColorAt(0.65, QColor(242, 242, 242, 0)); |
| 128 | gradient.setColorAt(0.75, QColor(242, 242, 242, 255)); |
| 129 | |
| 130 | painter->fillRect(opt.rect, gradient); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | QLinearGradient gradient(opt.rect.topLeft(),opt.rect.topRight()); |
| 135 | gradient.setColorAt(0.8, QColor(255, 255, 255, 0)); |
| 136 | gradient.setColorAt(1.0, QColor(255, 255, 255, 255)); |
| 137 | |
| 138 | painter->fillRect(opt.rect, gradient); |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 143 | QStyledItemDelegate::paint(painter, option, index); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | QSize |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 148 | SmartListDelegate::sizeHint(const QStyleOptionViewItem& option |
| 149 | , const QModelIndex& index |
| 150 | ) const |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 151 | { |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 152 | QSize size = QStyledItemDelegate::sizeHint(option, index); |
| 153 | size.setHeight(cellHeight_); |
| 154 | size.setWidth(cellWidth_); |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 155 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 156 | return size; |
| 157 | } |