blob: b94d53a23f45ffae17fafedb39bfd81490afe7d5 [file] [log] [blame]
Edric Milaretdb76aa82015-05-11 16:01:00 -04001/***************************************************************************
Edric Milaret5f316da2015-09-28 11:57:42 -04002 * Copyright (C) 2015 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{
34 QStyleOptionViewItemV4 opt = option;
35 initStyleOption(&opt, index);
36
37 if (index.column() == 0) {
Edric Milaretdb76aa82015-05-11 16:01:00 -040038 opt.text = "";
39 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();
56 painter->drawRect(QRect(rect.left(), rect.top(),
57 sizeImage_+1, sizeImage_+1));
58 if (var_p.isValid()) {
59 painter->drawImage(QRect(rect.left()+1, rect.top()+1,
60 sizeImage_, sizeImage_),
61 var_p.value<QImage>());
62 } else {
63 QImage defaultImage(":images/account.png");
64 painter->drawImage(QRect(rect.left(), rect.top(),
65 sizeImage_, sizeImage_),
66 defaultImage);
67 }
68 switch (c->phoneNumbers().size()) {
69 case 0:
70 break;
71 case 1:
72 {
73 QString number;
74 QVariant var_n =
75 c->phoneNumbers().first()->roleData(Qt::DisplayRole);
76 if (var_n.isValid())
77 number = var_n.value<QString>();
78
79 painter->drawText(QRect(rect.left()+sizeImage_+5,
80 rect.top() + rect.height()/2,
81 rect.width(), rect.height()/2),
82 opt.displayAlignment, number);
83 break;
84 }
85 default:
Edric Milaret2afd2bf2015-07-21 17:12:25 -040086 painter->drawText(QRect(rect.left()+sizeImage_+5,
87 rect.top() + rect.height()/2,
88 rect.width(), rect.height()/2),
Edric Milaret53ac6e52015-09-14 13:37:06 -040089 opt.displayAlignment, tr("<Multiple contact methods>"));
Edric Milaretdb76aa82015-05-11 16:01:00 -040090 break;
91 }
92 }
93 }
94}
95
96QSize
97ContactDelegate::sizeHint(const QStyleOptionViewItem &option,
98 const QModelIndex &index) const
99{
100 QSize result = QStyledItemDelegate::sizeHint(option, index);
101 result.setHeight((result.height()*2)+2);
102 return result;
103}