Edric Milaret | db76aa8 | 2015-05-11 16:01:00 -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 "contactdelegate.h" |
| 20 | |
| 21 | #include "person.h" |
| 22 | #include "contactmethod.h" |
| 23 | |
| 24 | ContactDelegate::ContactDelegate(QObject *parent) |
| 25 | : QStyledItemDelegate(parent) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | |
| 30 | void |
| 31 | ContactDelegate::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) { |
| 38 | QString name = index.model()->data(index, Qt::DisplayRole).toString(); |
| 39 | opt.text = ""; |
| 40 | QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); |
| 41 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); |
| 42 | QRect rect = opt.rect; |
| 43 | QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? |
| 44 | QPalette::Normal : QPalette::Disabled; |
| 45 | if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) |
| 46 | cg = QPalette::Inactive; |
| 47 | painter->setPen(opt.palette.color(cg, QPalette::Text)); |
| 48 | painter->setOpacity(1.0); |
| 49 | painter->drawText(QRect(rect.left()+sizeImage_+5, rect.top(), |
| 50 | rect.width(), rect.height()/2), |
| 51 | opt.displayAlignment, name); |
| 52 | |
| 53 | QVariant var_c = index.child(0,0).data( |
| 54 | static_cast<int>(Person::Role::Object)); |
| 55 | if (var_c.isValid()) { |
| 56 | Person *c = var_c.value<Person *>(); |
| 57 | QVariant var_p = c->photo(); |
| 58 | painter->drawRect(QRect(rect.left(), rect.top(), |
| 59 | sizeImage_+1, sizeImage_+1)); |
| 60 | if (var_p.isValid()) { |
| 61 | painter->drawImage(QRect(rect.left()+1, rect.top()+1, |
| 62 | sizeImage_, sizeImage_), |
| 63 | var_p.value<QImage>()); |
| 64 | } else { |
| 65 | QImage defaultImage(":images/account.png"); |
| 66 | painter->drawImage(QRect(rect.left(), rect.top(), |
| 67 | sizeImage_, sizeImage_), |
| 68 | defaultImage); |
| 69 | } |
| 70 | switch (c->phoneNumbers().size()) { |
| 71 | case 0: |
| 72 | break; |
| 73 | case 1: |
| 74 | { |
| 75 | QString number; |
| 76 | QVariant var_n = |
| 77 | c->phoneNumbers().first()->roleData(Qt::DisplayRole); |
| 78 | if (var_n.isValid()) |
| 79 | number = var_n.value<QString>(); |
| 80 | |
| 81 | painter->drawText(QRect(rect.left()+sizeImage_+5, |
| 82 | rect.top() + rect.height()/2, |
| 83 | rect.width(), rect.height()/2), |
| 84 | opt.displayAlignment, number); |
| 85 | break; |
| 86 | } |
| 87 | default: |
| 88 | /* more than one, for now don't show any of the contact methods */ |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | QSize |
| 96 | ContactDelegate::sizeHint(const QStyleOptionViewItem &option, |
| 97 | const QModelIndex &index) const |
| 98 | { |
| 99 | QSize result = QStyledItemDelegate::sizeHint(option, index); |
| 100 | result.setHeight((result.height()*2)+2); |
| 101 | return result; |
| 102 | } |