blob: 755c261000164bbbe2b646be54fda927f320ca3c [file] [log] [blame]
Edric Milareted0b2802015-10-01 15:10:02 -04001/***************************************************************************
Edric Milaretbab169d2016-01-07 15:13:33 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Edric Milareted0b2802015-10-01 15:10:02 -04003 * 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 Milareta0ebd062016-01-13 12:18:23 -050024#include <QPixmap>
Edric Milareted0b2802015-10-01 15:10:02 -040025
26#include "itemdataroles.h"
27#include "person.h"
28#include "recentmodel.h"
29#include "call.h"
30
Nicolas Jager97a21b42015-12-03 16:55:45 -050031SmartListDelegate::SmartListDelegate(QObject* parent) :
Edric Milareta0ebd062016-01-13 12:18:23 -050032 QItemDelegate(parent)
Edric Milareted0b2802015-10-01 15:10:02 -040033{
Edric Milareted0b2802015-10-01 15:10:02 -040034}
35
36void
Nicolas Jager97a21b42015-12-03 16:55:45 -050037SmartListDelegate::paint(QPainter* painter
38 , const QStyleOptionViewItem& option
39 , const QModelIndex& index
40 ) const
Edric Milareted0b2802015-10-01 15:10:02 -040041{
Nicolas Jager97a21b42015-12-03 16:55:45 -050042 QStyleOptionViewItem opt(option);
Edric Milareta13b48c2016-01-18 14:42:35 -050043 painter->setRenderHint(QPainter::Antialiasing);
Edric Milaret645e4c32016-01-18 15:55:08 -050044
Edric Milareta13b48c2016-01-18 14:42:35 -050045 opt.decorationSize = QSize(sizeImage_, sizeImage_);
Edric Milareta0ebd062016-01-13 12:18:23 -050046 opt.decorationPosition = QStyleOptionViewItem::Left;
47 opt.decorationAlignment = Qt::AlignCenter;
Edric Milaret645e4c32016-01-18 15:55:08 -050048
Edric Milareta0ebd062016-01-13 12:18:23 -050049 if (opt.state & QStyle::State_HasFocus)
50 opt.state ^= QStyle::State_HasFocus;
Edric Milareted0b2802015-10-01 15:10:02 -040051
Edric Milareta0ebd062016-01-13 12:18:23 -050052 QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
53 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
Nicolas Jager97a21b42015-12-03 16:55:45 -050054
55 QRect rect = opt.rect;
56 rect.setLeft(0);
57
Edric Milaret645e4c32016-01-18 15:55:08 -050058 QRect rectTexts(16 + rect.left() + dx_ + sizeImage_,
59 rect.top(),
60 rect.width(),
61 rect.height() / 2);
Edric Milareta0ebd062016-01-13 12:18:23 -050062 QRect rectAvatar(16 + rect.left(), rect.top() + dy_, sizeImage_, sizeImage_);
Edric Milaret645e4c32016-01-18 15:55:08 -050063 drawDecoration(painter, opt, rectAvatar,
64 QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>()));
Nicolas Jager97a21b42015-12-03 16:55:45 -050065
Edric Milareta0ebd062016-01-13 12:18:23 -050066 const int currentRow = index.row();
Nicolas Jager97a21b42015-12-03 16:55:45 -050067 if (currentRow == rowHighlighted_)
Nicolas Jager97a21b42015-12-03 16:55:45 -050068 emit rowSelected(opt.rect);
Edric Milareta0ebd062016-01-13 12:18:23 -050069
70 QFont font(painter->font());
71 font.setPointSize(fontSize_);
Nicolas Jager97a21b42015-12-03 16:55:45 -050072
73 QPen pen(painter->pen());
Edric Milareta0ebd062016-01-13 12:18:23 -050074
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 Jager97a21b42015-12-03 16:55:45 -050082 if (index.column() == 0)
83 {
84 painter->setPen(pen);
85
Edric Milareted0b2802015-10-01 15:10:02 -040086 QVariant name = index.data(static_cast<int>(Ring::Role::Name));
Nicolas Jager97a21b42015-12-03 16:55:45 -050087 if (name.isValid())
88 {
Edric Milareta0ebd062016-01-13 12:18:23 -050089 pen.setColor(lightBlack_);
Nicolas Jager97a21b42015-12-03 16:55:45 -050090 painter->setPen(pen);
91 font.setBold(true);
92 painter->setFont(font);
Edric Milaret645e4c32016-01-18 15:55:08 -050093 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 Milareted0b2802015-10-01 15:10:02 -0400103 }
104
105 QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
Edric Milareta0ebd062016-01-13 12:18:23 -0500106 pen.setColor(grey_);
Nicolas Jager97a21b42015-12-03 16:55:45 -0500107 painter->setPen(pen);
108 font.setBold(false);
109 painter->setFont(font);
110 rectTexts.moveTop(cellHeight_/2);
Nicolas Jager97a21b42015-12-03 16:55:45 -0500111 if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index)))
112 {
Edric Milareta0ebd062016-01-13 12:18:23 -0500113 painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
Nicolas Jager97a21b42015-12-03 16:55:45 -0500114 rect.top() + rect.height()/2,
115 rect.width(), rect.height()/2),
116 Qt::AlignTop | Qt::AlignLeft, state.toString());
117 }
118 else
119 {
Edric Milareted0b2802015-10-01 15:10:02 -0400120 QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
Nicolas Jager97a21b42015-12-03 16:55:45 -0500121 if (lastUsed.isValid())
122 {
Edric Milareta0ebd062016-01-13 12:18:23 -0500123 painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
Nicolas Jager97a21b42015-12-03 16:55:45 -0500124 rect.top() + rect.height()/2,
125 rect.width(), rect.height()/2),
126 Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
Edric Milareted0b2802015-10-01 15:10:02 -0400127 }
128 }
Nicolas Jager97a21b42015-12-03 16:55:45 -0500129 }
130 else
131 {
Edric Milareta0ebd062016-01-13 12:18:23 -0500132 QItemDelegate::paint(painter, opt, index);
Edric Milareted0b2802015-10-01 15:10:02 -0400133 }
134}
135
136QSize
Nicolas Jager97a21b42015-12-03 16:55:45 -0500137SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
138 , const QModelIndex& index
139 ) const
Edric Milareted0b2802015-10-01 15:10:02 -0400140{
Edric Milareta0ebd062016-01-13 12:18:23 -0500141 QSize size = QItemDelegate::sizeHint(option, index);
Nicolas Jager97a21b42015-12-03 16:55:45 -0500142 size.setHeight(cellHeight_);
Nicolas Jager97a21b42015-12-03 16:55:45 -0500143 return size;
144}