smartlistdelegate.cpp 5.58 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
SmartListDelegate::SmartListDelegate(QObject* parent) :
Edric Milaret's avatar
Edric Milaret committed
32
    QItemDelegate(parent)
33 34 35 36
{
}

void
37 38 39 40
SmartListDelegate::paint(QPainter* painter
                        , const QStyleOptionViewItem& option
                        , const QModelIndex& index
                        ) const
41
{
42
    QStyleOptionViewItem opt(option);
Edric Milaret's avatar
Edric Milaret committed
43
    painter->setRenderHint(QPainter::Antialiasing);
44

Edric Milaret's avatar
Edric Milaret committed
45
    opt.decorationSize = QSize(sizeImage_, sizeImage_);
Edric Milaret's avatar
Edric Milaret committed
46 47
    opt.decorationPosition = QStyleOptionViewItem::Left;
    opt.decorationAlignment = Qt::AlignCenter;
48

Edric Milaret's avatar
Edric Milaret committed
49 50
    if (opt.state & QStyle::State_HasFocus)
        opt.state ^= QStyle::State_HasFocus;
51

Edric Milaret's avatar
Edric Milaret committed
52 53
    QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
54 55 56 57

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

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

Edric Milaret's avatar
Edric Milaret committed
66
    const int currentRow = index.row();
67 68
    if (currentRow == rowHighlighted_)
        emit rowSelected(opt.rect);
Edric Milaret's avatar
Edric Milaret committed
69 70 71

    QFont font(painter->font());
    font.setPointSize(fontSize_);
72 73

    QPen pen(painter->pen());
Edric Milaret's avatar
Edric Milaret committed
74 75 76 77 78 79 80 81

    if (not (opt.state & QStyle::State_Selected)) {
        pen.setColor(lightGrey_);
        painter->setPen(pen);
        painter->drawLine(rect.left() + 20, rect.bottom(),
                          rect.right() - 20,
                          rect.bottom());
    }
82 83 84 85
    if (index.column() == 0)
    {
        painter->setPen(pen);

86
        QVariant name = index.data(static_cast<int>(Ring::Role::Name));
87 88
        if (name.isValid())
        {
Edric Milaret's avatar
Edric Milaret committed
89
            pen.setColor(lightBlack_);
90 91 92
            painter->setPen(pen);
            font.setBold(true);
            painter->setFont(font);
93 94 95 96 97 98 99 100 101 102
            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);
103 104 105
        }

        QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
Edric Milaret's avatar
Edric Milaret committed
106
        pen.setColor(grey_);
107 108 109 110 111 112
        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
113
            painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
114 115 116 117 118 119
                               rect.top() + rect.height()/2,
                               rect.width(), rect.height()/2),
                               Qt::AlignTop | Qt::AlignLeft, state.toString());
        }
        else
        {
120
            QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
121 122
            if (lastUsed.isValid())
            {
Edric Milaret's avatar
Edric Milaret committed
123
                painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
124 125 126
                                   rect.top() + rect.height()/2,
                                   rect.width(), rect.height()/2),
                                   Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
127 128
            }
        }
129 130 131
    }
    else
    {
Edric Milaret's avatar
Edric Milaret committed
132
        QItemDelegate::paint(painter, opt, index);
133 134 135 136
    }
}

QSize
137 138 139
SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
                           , const QModelIndex& index
                           ) const
140
{
Edric Milaret's avatar
Edric Milaret committed
141
    QSize size = QItemDelegate::sizeHint(option, index);
142 143 144
    size.setHeight(cellHeight_);
    return size;
}