blob: 15ee10a81cd0dd4bfb11df7b34bb864690c8b7b0 [file] [log] [blame]
Anthony Léonardf95f7f62017-04-04 11:01:51 -04001/***************************************************************************
2 * Copyright (C) 2017 by Savoir-faire Linux *
3 * Author: Anthony Léonard <anthony.leonard@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 "contactrequestitemdelegate.h"
20#include "ringthemeutils.h"
21
22#include "accountmodel.h"
23#include "pendingcontactrequestmodel.h"
24#include "contactrequest.h"
25
26#include <QPainter>
27#include <QApplication>
28
29ContactRequestItemDelegate::ContactRequestItemDelegate(QObject* parent) :
30 QItemDelegate(parent)
31{}
32
33void
34ContactRequestItemDelegate::paint(QPainter* painter
35 , const QStyleOptionViewItem& option
36 , const QModelIndex& index
37 ) const
38{
39 painter->setRenderHint(QPainter::Antialiasing);
40
41 QStyleOptionViewItem opt(option);
42
43 // Not having focus removes dotted lines around the item
44 if (opt.state & QStyle::State_HasFocus)
45 opt.state ^= QStyle::State_HasFocus;
46
47 // First, we draw the control itself
48 QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
49 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
50
51 // Then, we print the text
52 QFont font(painter->font());
53 font.setPointSize(10);
54 font.setBold(true);
55 painter->setFont(font);
56
57 QFontMetrics fontMetrics(font);
58
59 QRect rectText(opt.rect);
60 rectText.setLeft(opt.rect.left() + dxText_);
61 rectText.setTop(opt.rect.top() + dyText_);
62 rectText.setBottom(rectText.top() + fontMetrics.height());
63
64 QString text(index.data().toString());
65 text = fontMetrics.elidedText(text, Qt::ElideRight, rectText.width());
66
67 QPen pen(painter->pen());
68
69 pen.setColor(RingTheme::lightBlack_);
70 painter->setPen(pen);
71
72 painter->drawText(rectText,text);
73
74 // Draw a picture
75 // TODO: Draw the incoming CR picture if part of the payload
76 QRect rectPic(opt.rect.left() + dxImage_, opt.rect.top() + dyImage_, sizeImage_, sizeImage_);
77 drawDecoration(painter, opt, rectPic,
78 QPixmap::fromImage(QImage(":/images/user/btn-default-userpic.svg").scaled(QSize(sizeImage_, sizeImage_),
79 Qt::KeepAspectRatio,
80 Qt::SmoothTransformation)));
81
82 // Draw separator when item is not selected
83 if (not (opt.state & QStyle::State_Selected)) {
84 QRect rect(opt.rect);
85 pen.setColor(RingTheme::lightGrey_);
86 painter->setPen(pen);
87 painter->drawLine(rect.left() + separatorYPadding_, rect.bottom(),
88 rect.right() - separatorYPadding_,
89 rect.bottom());
90 }
91}
92
93QSize
94ContactRequestItemDelegate::sizeHint(const QStyleOptionViewItem& option
95 , const QModelIndex& index
96 ) const
97{
98 QSize size = QItemDelegate::sizeHint(option, index);
99 size.setHeight(cellHeight_);
100 return size;
101}