blob: 29d47b5dd397144c587f31fb5aff833b8ba877bf [file] [log] [blame]
Edric Milareted0b2802015-10-01 15:10:02 -04001/***************************************************************************
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 Jager97a21b42015-12-03 16:55:45 -050030SmartListDelegate::SmartListDelegate(QObject* parent) :
31 QStyledItemDelegate(parent)
Edric Milareted0b2802015-10-01 15:10:02 -040032{
Edric Milareted0b2802015-10-01 15:10:02 -040033}
34
35void
Nicolas Jager97a21b42015-12-03 16:55:45 -050036SmartListDelegate::paint(QPainter* painter
37 , const QStyleOptionViewItem& option
38 , const QModelIndex& index
39 ) const
Edric Milareted0b2802015-10-01 15:10:02 -040040{
Nicolas Jager97a21b42015-12-03 16:55:45 -050041 QStyleOptionViewItem opt(option);
Edric Milareted0b2802015-10-01 15:10:02 -040042
Nicolas Jager97a21b42015-12-03 16:55:45 -050043 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 Milareted0b2802015-10-01 15:10:02 -040074 QVariant name = index.data(static_cast<int>(Ring::Role::Name));
Nicolas Jager97a21b42015-12-03 16:55:45 -050075 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 Milareted0b2802015-10-01 15:10:02 -040082 }
83
84 QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
Nicolas Jager97a21b42015-12-03 16:55:45 -050085
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 Milareted0b2802015-10-01 15:10:02 -0400101 QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
Nicolas Jager97a21b42015-12-03 16:55:45 -0500102 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 Milareted0b2802015-10-01 15:10:02 -0400108 }
109 }
Nicolas Jager97a21b42015-12-03 16:55:45 -0500110
111 painter->setPen(pen);
Edric Milareted0b2802015-10-01 15:10:02 -0400112 QVariant var_p = index.data(static_cast<int>(Ring::Role::Object));
113 auto person = var_p.value<Person*>();
Nicolas Jager97a21b42015-12-03 16:55:45 -0500114 if (person && not person->photo().value<QImage>().isNull())
115 {
116 painter->drawImage(rectAvatar, person->photo().value<QImage>());
Edric Milareted0b2802015-10-01 15:10:02 -0400117 }
Nicolas Jager97a21b42015-12-03 16:55:45 -0500118 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 Milareted0b2802015-10-01 15:10:02 -0400143 QStyledItemDelegate::paint(painter, option, index);
144 }
145}
146
147QSize
Nicolas Jager97a21b42015-12-03 16:55:45 -0500148SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
149 , const QModelIndex& index
150 ) const
Edric Milareted0b2802015-10-01 15:10:02 -0400151{
Nicolas Jager97a21b42015-12-03 16:55:45 -0500152 QSize size = QStyledItemDelegate::sizeHint(option, index);
153 size.setHeight(cellHeight_);
154 size.setWidth(cellWidth_);
Edric Milareted0b2802015-10-01 15:10:02 -0400155
Nicolas Jager97a21b42015-12-03 16:55:45 -0500156 return size;
157}