smartlistdelegate.cpp 6.6 KB
Newer Older
1
/***************************************************************************
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

#include "itemdataroles.h"
#include "person.h"
#include "recentmodel.h"
#include "call.h"
30
#include "combar.h"
31

32 33
#include "ringthemeutils.h"

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

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

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

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

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

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

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

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

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

    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);
91
        QPen pen(RingTheme::red_, 5);
92
        painter->setPen(pen);
93
        painter->fillPath(path, RingTheme::red_);
94 95 96 97 98 99 100
        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
101
    font.setPointSize(fontSize_);
102 103

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

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

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

120
        QVariant name = index.data(static_cast<int>(Ring::Role::Name));
121 122
        if (name.isValid())
        {
123
            pen.setColor(RingTheme::lightBlack_);
124 125 126
            painter->setPen(pen);
            font.setBold(true);
            painter->setFont(font);
127
            QFontMetrics fontMetrics(font);
128 129
            QString nameStr = fontMetrics.elidedText(name.toString(), Qt::ElideRight
                                                                    , rectTexts.width()- sizeImage_ - effectiveComBarSize_ - dx_);
130
            painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, nameStr);
131 132 133
        }

        QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
134
        pen.setColor(RingTheme::grey_);
135 136 137 138 139 140
        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
141
            painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
142 143 144 145 146 147
                               rect.top() + rect.height()/2,
                               rect.width(), rect.height()/2),
                               Qt::AlignTop | Qt::AlignLeft, state.toString());
        }
        else
        {
148
            QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
149 150
            if (lastUsed.isValid())
            {
Edric Milaret's avatar
Edric Milaret committed
151
                painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
152 153 154
                                   rect.top() + rect.height()/2,
                                   rect.width(), rect.height()/2),
                                   Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
155 156
            }
        }
157 158 159
    }
    else
    {
Edric Milaret's avatar
Edric Milaret committed
160
        QItemDelegate::paint(painter, opt, index);
161 162 163 164
    }
}

QSize
165 166 167
SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
                           , const QModelIndex& index
                           ) const
168
{
Edric Milaret's avatar
Edric Milaret committed
169
    QSize size = QItemDelegate::sizeHint(option, index);
170 171 172
    size.setHeight(cellHeight_);
    return size;
}