blob: 9ba05a7ece1acfbb0b8e89e600ec7971a3470d4e [file] [log] [blame]
Edric Milaret83b248c2015-06-02 11:42:23 -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 "imdelegate.h"
20
21#include "media/text.h"
22#include "media/textrecording.h"
23
24ImDelegate::ImDelegate(QObject *parent)
25 : QStyledItemDelegate(parent), showDate_(false), showAuthor_(false)
26{}
27
28void ImDelegate::setDisplayOptions(ImDelegate::DisplayOptions opt)
29{
30 qDebug() << opt;
31 showAuthor_ = opt & DisplayOptions::AUTHOR;
32 showDate_ = opt & DisplayOptions::DATE;
33}
34
35void
36ImDelegate::paint(QPainter *painter,
37 const QStyleOptionViewItem &option,
38 const QModelIndex &index) const
39{
40 QStyleOptionViewItemV4 opt = option;
41 initStyleOption(&opt, index);
42
43 if (index.isValid()) {
44
45 auto msg = index.model()->data(index, Qt::DisplayRole).toString();
46 opt.text = "";
47 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
48 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
49 auto rect = opt.rect;
50 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
51 ? QPalette::Normal : QPalette::Disabled;
52 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
53 cg = QPalette::Inactive;
54 painter->setPen(opt.palette.color(cg, QPalette::Text));
55 painter->setOpacity(1.0);
56 auto dir = index.model()->data(
57 index,
58 static_cast<int>(Media::TextRecording::Role::Direction))
59 .value<Media::Media::Direction>() == Media::Media::Direction::IN
60 ? Qt::AlignLeft : Qt::AlignRight;
61
62 if (showAuthor_) {
63 auto author = index.model()->
64 data(index,
65 static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).toString();
66 msg = QString("(%1) %2").arg(author, msg);
67 }
68 if (showDate_) {
69 auto formattedDate = index.model()->
70 data(index,
71 static_cast<int>(Media::TextRecording::Role::FormattedDate)).toString();
72 msg = QString("[%1] %2").arg(formattedDate, msg);
73 }
74 painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()),
75 dir, msg);
76 }
77}
78
79QSize
80ImDelegate::sizeHint(const QStyleOptionViewItem &option,
81 const QModelIndex &index) const
82{
83 QSize result = QStyledItemDelegate::sizeHint(option, index);
84 result.setHeight(result.height());
85 return result;
86}
87