blob: 695ff6b265dd2afd735685961c4c51ef845f2dad [file] [log] [blame]
Edric Milaretdb76aa82015-05-11 16:01:00 -04001/***************************************************************************
Edric Milaretbab169d2016-01-07 15:13:33 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Edric Milaretdb76aa82015-05-11 16:01:00 -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 "contactdelegate.h"
20
21#include "person.h"
22#include "contactmethod.h"
23
24ContactDelegate::ContactDelegate(QObject *parent)
25 : QStyledItemDelegate(parent)
26{
27}
28
29
30void
31ContactDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
32 const QModelIndex &index) const
33{
Edric Milareted0b2802015-10-01 15:10:02 -040034 QStyleOptionViewItem opt = option;
Edric Milaretdb76aa82015-05-11 16:01:00 -040035 initStyleOption(&opt, index);
36
37 if (index.column() == 0) {
Edric Milareted0b2802015-10-01 15:10:02 -040038 opt.text.clear();
Edric Milaretdb76aa82015-05-11 16:01:00 -040039 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
40 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
41 QRect rect = opt.rect;
42 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ?
43 QPalette::Normal : QPalette::Disabled;
44 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
45 cg = QPalette::Inactive;
46 painter->setPen(opt.palette.color(cg, QPalette::Text));
47 painter->setOpacity(1.0);
Edric Milaretdb76aa82015-05-11 16:01:00 -040048 QVariant var_c = index.child(0,0).data(
49 static_cast<int>(Person::Role::Object));
50 if (var_c.isValid()) {
51 Person *c = var_c.value<Person *>();
Edric Milaret43f3c1e2015-07-16 17:52:47 -040052 painter->drawText(QRect(rect.left()+sizeImage_+5, rect.top(),
53 rect.width(), rect.height()/2),
54 opt.displayAlignment, c->formattedName());
Edric Milaretdb76aa82015-05-11 16:01:00 -040055 QVariant var_p = c->photo();
Edric Milaretdb76aa82015-05-11 16:01:00 -040056 if (var_p.isValid()) {
57 painter->drawImage(QRect(rect.left()+1, rect.top()+1,
58 sizeImage_, sizeImage_),
59 var_p.value<QImage>());
60 } else {
61 QImage defaultImage(":images/account.png");
62 painter->drawImage(QRect(rect.left(), rect.top(),
63 sizeImage_, sizeImage_),
64 defaultImage);
65 }
66 switch (c->phoneNumbers().size()) {
67 case 0:
68 break;
69 case 1:
70 {
71 QString number;
72 QVariant var_n =
73 c->phoneNumbers().first()->roleData(Qt::DisplayRole);
74 if (var_n.isValid())
75 number = var_n.value<QString>();
76
77 painter->drawText(QRect(rect.left()+sizeImage_+5,
78 rect.top() + rect.height()/2,
79 rect.width(), rect.height()/2),
80 opt.displayAlignment, number);
81 break;
82 }
83 default:
Edric Milaret2afd2bf2015-07-21 17:12:25 -040084 painter->drawText(QRect(rect.left()+sizeImage_+5,
85 rect.top() + rect.height()/2,
86 rect.width(), rect.height()/2),
Edric Milaret53ac6e52015-09-14 13:37:06 -040087 opt.displayAlignment, tr("<Multiple contact methods>"));
Edric Milaretdb76aa82015-05-11 16:01:00 -040088 break;
89 }
90 }
91 }
92}
93
94QSize
95ContactDelegate::sizeHint(const QStyleOptionViewItem &option,
96 const QModelIndex &index) const
97{
98 QSize result = QStyledItemDelegate::sizeHint(option, index);
Edric Milareta0ebd062016-01-13 12:18:23 -050099 auto height = (result.height()*2)+2;
100 if (height < sizeImage_)
101 height = sizeImage_;
102 result.setHeight(height);
Edric Milaretdb76aa82015-05-11 16:01:00 -0400103 return result;
104}