blob: 7632e99fee8025438a82c889fbab7bbbd537408a [file] [log] [blame]
Edric Milaret53f57b62015-05-11 11:02:17 -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 "historydelegate.h"
20
21#include <QDebug>
22
23HistoryDelegate::HistoryDelegate(QObject *parent) :
24 QStyledItemDelegate(parent)
25{
26
27}
28
29void
30HistoryDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
31 const QModelIndex &index) const
32{
33 QStyleOptionViewItemV4 opt = option;
34 initStyleOption(&opt, index);
35
36 if (index.column() == 0) {
37 auto name = index.model()->data(index, Qt::DisplayRole).toString();
38 auto number = index.model()->data(index, static_cast<int>(Call::Role::Number)).toString();
Edric Milaret12353822015-05-14 14:41:09 -040039 Call::Direction direction = index.model()->data(index, static_cast<int>(Call::Role::Direction)).value<Call::Direction>();
40
Edric Milaret53f57b62015-05-11 11:02:17 -040041 opt.text = "";
42 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
43 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
44 auto rect = opt.rect;
45 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
46 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
47 cg = QPalette::Inactive;
48 painter->setPen(opt.palette.color(cg, QPalette::Text));
49 painter->setOpacity(1.0);
50 if (not number.isEmpty()) {
51 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()/2),
52 opt.displayAlignment, name);
53 painter->setOpacity(0.7);
54 painter->drawText(QRect(rect.left(), rect.top() + rect.height()/2, rect.width(), rect.height()/2),
55 opt.displayAlignment, number);
Edric Milaret12353822015-05-14 14:41:09 -040056 painter->setOpacity(1.0);
57 QImage arrow;
58 switch (direction) {
59 case Call::Direction::INCOMING:
60 arrow.load("://images/arrow-down.png");
61 break;
62 case Call::Direction::OUTGOING:
63 arrow.load("://images/arrow-up.png");
64 break;
65 }
66 painter->drawImage(QRect(rect.left() -imgSize_, rect.top() + (rect.height()-imgSize_)/2, imgSize_, imgSize_), arrow);
Edric Milaret53f57b62015-05-11 11:02:17 -040067 } else {
68 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()),
69 opt.displayAlignment, name);
70 }
71 }
72}
73
74QSize
75HistoryDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const
76{
77 QSize result = QStyledItemDelegate::sizeHint(option, index);
78 if (not index.model()->data(index, static_cast<int>(Call::Role::Number)).toString().isEmpty()) {
79 result.setHeight(result.height()*2);
80 } else {
81 result.setHeight(result.height());
82 }
83 return result;
84}