blob: 46516f8396ae0d60b3f0d6563e9ee72d64431408 [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 Milareta0ebd062016-01-13 12:18:23 -050040 opt.decorationSize = QSize(imgSize_,imgSize_);
41 opt.decorationPosition = QStyleOptionViewItem::Left;
42 opt.decorationAlignment = Qt::AlignCenter;
Edric Milaret53f57b62015-05-11 11:02:17 -040043 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
44 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
45 auto rect = opt.rect;
Edric Milaret74474f52016-05-16 13:36:23 -040046 QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled;
Edric Milaret53f57b62015-05-11 11:02:17 -040047 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
48 cg = QPalette::Inactive;
49 painter->setPen(opt.palette.color(cg, QPalette::Text));
50 painter->setOpacity(1.0);
51 if (not number.isEmpty()) {
Edric Milareta0ebd062016-01-13 12:18:23 -050052 painter->drawText(QRect(rect.left() +imgSize_ + 5, rect.top(), rect.width(), rect.height()/2),
53 Qt::AlignBottom, name);
Edric Milaret53f57b62015-05-11 11:02:17 -040054 painter->setOpacity(0.7);
Edric Milareta0ebd062016-01-13 12:18:23 -050055 painter->drawText(QRect(rect.left()+imgSize_ + 5, rect.top() + rect.height()/2, rect.width(), rect.height()/2),
56 Qt::AlignTop, number);
Edric Milaret12353822015-05-14 14:41:09 -040057 painter->setOpacity(1.0);
58 QImage arrow;
59 switch (direction) {
60 case Call::Direction::INCOMING:
61 arrow.load("://images/arrow-down.png");
62 break;
63 case Call::Direction::OUTGOING:
64 arrow.load("://images/arrow-up.png");
65 break;
66 }
67 painter->drawImage(QRect(rect.left() -imgSize_, rect.top() + (rect.height()-imgSize_)/2, imgSize_, imgSize_), arrow);
Edric Milaret53f57b62015-05-11 11:02:17 -040068 } else {
69 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()),
70 opt.displayAlignment, name);
71 }
72 }
73}
74
75QSize
76HistoryDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const
77{
78 QSize result = QStyledItemDelegate::sizeHint(option, index);
Edric Milaret53f57b62015-05-11 11:02:17 -040079 return result;
80}