blob: 8f9602a32edcc4ac99277be1098553cf452d271c [file] [log] [blame]
Edric Milaret53f57b62015-05-11 11:02:17 -04001/***************************************************************************
Edric Milaretbab169d2016-01-07 15:13:33 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Edric Milaret53f57b62015-05-11 11:02:17 -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 "historydelegate.h"
20
Edric Milaret53f57b62015-05-11 11:02:17 -040021HistoryDelegate::HistoryDelegate(QObject *parent) :
22 QStyledItemDelegate(parent)
23{
24
25}
26
27void
28HistoryDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
29 const QModelIndex &index) const
30{
Edric Milareted0b2802015-10-01 15:10:02 -040031 QStyleOptionViewItem opt = option;
Edric Milaret53f57b62015-05-11 11:02:17 -040032 initStyleOption(&opt, index);
33
34 if (index.column() == 0) {
35 auto name = index.model()->data(index, Qt::DisplayRole).toString();
36 auto number = index.model()->data(index, static_cast<int>(Call::Role::Number)).toString();
Edric Milaret12353822015-05-14 14:41:09 -040037 Call::Direction direction = index.model()->data(index, static_cast<int>(Call::Role::Direction)).value<Call::Direction>();
38
Edric Milareted0b2802015-10-01 15:10:02 -040039 opt.text.clear();
Edric Milaret53f57b62015-05-11 11:02:17 -040040 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
41 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
42 auto rect = opt.rect;
43 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? 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);
48 if (not number.isEmpty()) {
49 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()/2),
50 opt.displayAlignment, name);
51 painter->setOpacity(0.7);
52 painter->drawText(QRect(rect.left(), rect.top() + rect.height()/2, rect.width(), rect.height()/2),
53 opt.displayAlignment, number);
Edric Milaret12353822015-05-14 14:41:09 -040054 painter->setOpacity(1.0);
55 QImage arrow;
56 switch (direction) {
57 case Call::Direction::INCOMING:
58 arrow.load("://images/arrow-down.png");
59 break;
60 case Call::Direction::OUTGOING:
61 arrow.load("://images/arrow-up.png");
62 break;
63 }
64 painter->drawImage(QRect(rect.left() -imgSize_, rect.top() + (rect.height()-imgSize_)/2, imgSize_, imgSize_), arrow);
Edric Milaret53f57b62015-05-11 11:02:17 -040065 } else {
66 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()),
67 opt.displayAlignment, name);
68 }
69 }
70}
71
72QSize
73HistoryDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const
74{
75 QSize result = QStyledItemDelegate::sizeHint(option, index);
76 if (not index.model()->data(index, static_cast<int>(Call::Role::Number)).toString().isEmpty()) {
77 result.setHeight(result.height()*2);
78 } else {
79 result.setHeight(result.height());
80 }
81 return result;
82}