smartlistdelegate.cpp 6.88 KB
Newer Older
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "smartlistdelegate.h"

#include <QApplication>
#include <QSortFilterProxyModel>
#include <QPainter>
Edric Milaret's avatar
Edric Milaret committed
24
#include <QPixmap>
25 26 27 28 29 30

#include "itemdataroles.h"
#include "person.h"
#include "recentmodel.h"
#include "call.h"

31 32
#include "ringthemeutils.h"

33
SmartListDelegate::SmartListDelegate(QObject* parent) :
Edric Milaret's avatar
Edric Milaret committed
34
    QItemDelegate(parent)
35 36 37 38
{
}

void
39 40 41 42
SmartListDelegate::paint(QPainter* painter
                        , const QStyleOptionViewItem& option
                        , const QModelIndex& index
                        ) const
43
{
44 45
    painter->setRenderHint(QPainter::Antialiasing);

46
    QStyleOptionViewItem opt(option);
Edric Milaret's avatar
Edric Milaret committed
47
    painter->setRenderHint(QPainter::Antialiasing);
48

Edric Milaret's avatar
Edric Milaret committed
49
    opt.decorationSize = QSize(sizeImage_, sizeImage_);
Edric Milaret's avatar
Edric Milaret committed
50 51
    opt.decorationPosition = QStyleOptionViewItem::Left;
    opt.decorationAlignment = Qt::AlignCenter;
52

Edric Milaret's avatar
Edric Milaret committed
53 54
    if (opt.state & QStyle::State_HasFocus)
        opt.state ^= QStyle::State_HasFocus;
55

Edric Milaret's avatar
Edric Milaret committed
56 57
    QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
58 59 60 61

    QRect rect = opt.rect;
    rect.setLeft(0);

62 63 64 65
    QRect rectTexts(16 + rect.left() + dx_ + sizeImage_,
                    rect.top(),
                    rect.width(),
                    rect.height() / 2);
Edric Milaret's avatar
Edric Milaret committed
66
    QRect rectAvatar(16 + rect.left(), rect.top() + dy_, sizeImage_, sizeImage_);
67 68
    drawDecoration(painter, opt, rectAvatar,
                   QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>()));
69

Edric Milaret's avatar
Edric Milaret committed
70
    QFont font(painter->font());
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    if (auto messageCount = index.data(static_cast<int>(Ring::Role::UnreadTextMessageCount)).toInt()) {

        font.setPointSize(8);
        QFontMetrics textFontMetrics(font);
        QString messageCountText = QString::number(messageCount);

        QRect pastilleRect;
        QRect(rectAvatar.right() - 7, rectAvatar.bottom() - 7, pinSize_, pinSize_);
        pastilleRect = textFontMetrics.boundingRect(QRect(rectAvatar.left() + sizeImage_/3,
                                                          rectAvatar.bottom() - 6, sizeImage_, 0),
                                                    Qt::AlignCenter, messageCountText);

        painter->setOpacity(0.9);
        QRect bubbleRect(pastilleRect.left(), pastilleRect.top(),
                         pastilleRect.width() + 3, pastilleRect.height());

        QPainterPath path;
        path.addRoundedRect(bubbleRect, 3, 3);
90
        QPen pen(RingTheme::red_, 5);
91
        painter->setPen(pen);
92
        painter->fillPath(path, RingTheme::red_);
93 94 95 96 97 98 99
        painter->drawPath(path);

        painter->setPen(Qt::white);
        painter->setOpacity(1);
        painter->setFont(font);
        painter->drawText(bubbleRect, Qt::AlignCenter, messageCountText);
    }
Edric Milaret's avatar
Edric Milaret committed
100
    font.setPointSize(fontSize_);
101 102

    QPen pen(painter->pen());
Edric Milaret's avatar
Edric Milaret committed
103

104 105 106 107
    const int currentRow = index.row();
    if (currentRow == rowHighlighted_)
        emit rowSelected(opt.rect);

Edric Milaret's avatar
Edric Milaret committed
108
    if (not (opt.state & QStyle::State_Selected)) {
109
        pen.setColor(RingTheme::lightGrey_);
Edric Milaret's avatar
Edric Milaret committed
110 111 112 113 114
        painter->setPen(pen);
        painter->drawLine(rect.left() + 20, rect.bottom(),
                          rect.right() - 20,
                          rect.bottom());
    }
115 116 117 118
    if (index.column() == 0)
    {
        painter->setPen(pen);

119
        QVariant name = index.data(static_cast<int>(Ring::Role::Name));
120 121
        if (name.isValid())
        {
122
            pen.setColor(RingTheme::lightBlack_);
123 124 125
            painter->setPen(pen);
            font.setBold(true);
            painter->setFont(font);
126 127 128 129 130 131 132 133 134 135
            QFontMetrics fontMetrics(font);
            QString nameStr = name.toString();
            auto realRect = fontMetrics.boundingRect(rect, Qt::AlignBottom | Qt::AlignLeft, nameStr);
            if (realRect.width() > (rect.width() - rectTexts.left() - 30)) {
                /* 30 here is the size of the video button */
                auto charToChop = (realRect.width() - (rect.width() - rectTexts.left())) / fontMetrics.averageCharWidth();
                nameStr.chop(charToChop + 8);
                nameStr.append(QStringLiteral("..."));
            }
            painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, nameStr);
136 137 138
        }

        QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
139
        pen.setColor(RingTheme::grey_);
140 141 142 143 144 145
        painter->setPen(pen);
        font.setBold(false);
        painter->setFont(font);
        rectTexts.moveTop(cellHeight_/2);
        if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index)))
        {
Edric Milaret's avatar
Edric Milaret committed
146
            painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
147 148 149 150 151 152
                               rect.top() + rect.height()/2,
                               rect.width(), rect.height()/2),
                               Qt::AlignTop | Qt::AlignLeft, state.toString());
        }
        else
        {
153
            QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
154 155
            if (lastUsed.isValid())
            {
Edric Milaret's avatar
Edric Milaret committed
156
                painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
157 158 159
                                   rect.top() + rect.height()/2,
                                   rect.width(), rect.height()/2),
                                   Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
160 161
            }
        }
162 163 164
    }
    else
    {
Edric Milaret's avatar
Edric Milaret committed
165
        QItemDelegate::paint(painter, opt, index);
166 167 168 169
    }
}

QSize
170 171 172
SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
                           , const QModelIndex& index
                           ) const
173
{
Edric Milaret's avatar
Edric Milaret committed
174
    QSize size = QItemDelegate::sizeHint(option, index);
175 176 177
    size.setHeight(cellHeight_);
    return size;
}