Edric Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Edric Milaret | bab169d | 2016-01-07 15:13:33 -0500 | [diff] [blame^] | 2 | * Copyright (C) 2015-2016 by Savoir-faire Linux * |
Edric Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 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 | |
Edric Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 21 | HistoryDelegate::HistoryDelegate(QObject *parent) : |
| 22 | QStyledItemDelegate(parent) |
| 23 | { |
| 24 | |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | HistoryDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
| 29 | const QModelIndex &index) const |
| 30 | { |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 31 | QStyleOptionViewItem opt = option; |
Edric Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 32 | 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 Milaret | 1235382 | 2015-05-14 14:41:09 -0400 | [diff] [blame] | 37 | Call::Direction direction = index.model()->data(index, static_cast<int>(Call::Role::Direction)).value<Call::Direction>(); |
| 38 | |
Edric Milaret | ed0b280 | 2015-10-01 15:10:02 -0400 | [diff] [blame] | 39 | opt.text.clear(); |
Edric Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 40 | 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 Milaret | 1235382 | 2015-05-14 14:41:09 -0400 | [diff] [blame] | 54 | 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 Milaret | 53f57b6 | 2015-05-11 11:02:17 -0400 | [diff] [blame] | 65 | } else { |
| 66 | painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()), |
| 67 | opt.displayAlignment, name); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | QSize |
| 73 | HistoryDelegate::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 | } |