smartlistdelegate.cpp 5.67 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 24 25 26 27 28 29
 * 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>

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

30 31
SmartListDelegate::SmartListDelegate(QObject* parent) :
    QStyledItemDelegate(parent)
32 33 34 35
{
}

void
36 37 38 39
SmartListDelegate::paint(QPainter* painter
                        , const QStyleOptionViewItem& option
                        , const QModelIndex& index
                        ) const
40
{
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    QStyleOptionViewItem opt(option);

    QFont font(painter->font());
    font.setPointSize(10);

    const int currentRow = index.row();

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

    QRect rectTexts(16 + rect.left() + dx+sizeImage_, rect.top(), rect.width(), rect.height()/2);
    QRect rectAvatar(16 + rect.left(), rect.top() + dy, sizeImage_, sizeImage_);

    QLinearGradient linearGradient(0, 0, 100, 100);
    linearGradient.setColorAt(0.0, Qt::white);
    linearGradient.setColorAt(1.0, Qt::black);

    if (currentRow == rowHighlighted_)
    {
        painter->fillRect(opt.rect, QColor(242, 242, 242));
        emit rowSelected(opt.rect);
    }

    QPen pen(painter->pen());
    pen.setColor(QColor(242, 242, 242));
    painter->setPen(pen);
    painter->drawLine(rect.left() + 20, rect.bottom(),
                      rect.right() - 20,
                      rect.bottom());
    if (index.column() == 0)
    {
        painter->setPen(pen);

74
        QVariant name = index.data(static_cast<int>(Ring::Role::Name));
75 76 77 78 79 80 81
        if (name.isValid())
        {
            pen.setColor(QColor(63, 63, 63));
            painter->setPen(pen);
            font.setBold(true);
            painter->setFont(font);
            painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, name.toString());
82 83 84
        }

        QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        pen.setColor(QColor(192, 192, 192));
        painter->setPen(pen);
        font.setBold(false);
        painter->setFont(font);
        rectTexts.moveTop(cellHeight_/2);

        if (state.isValid() && RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(index)))
        {
            painter->drawText(QRect(16 + rect.left() + dx + sizeImage_,
                               rect.top() + rect.height()/2,
                               rect.width(), rect.height()/2),
                               Qt::AlignTop | Qt::AlignLeft, state.toString());
        }
        else
        {
101
            QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
102 103 104 105 106 107
            if (lastUsed.isValid())
            {
                painter->drawText(QRect(16 + rect.left() + dx + sizeImage_,
                                   rect.top() + rect.height()/2,
                                   rect.width(), rect.height()/2),
                                   Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
108 109
            }
        }
110 111

        painter->setPen(pen);
112 113
        QVariant var_p = index.data(static_cast<int>(Ring::Role::Object));
        auto person = var_p.value<Person*>();
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        if (person && not person->photo().value<QImage>().isNull())
        {
            painter->drawImage(rectAvatar, person->photo().value<QImage>());
        }
        else
        {
            QImage defaultImage(":images/user/btn-default-userpic.svg");
            painter->drawImage(rectAvatar, defaultImage);
        }

        if (currentRow == rowHighlighted_)
        {
            QLinearGradient gradient(opt.rect.topLeft(),opt.rect.topRight());
            gradient.setColorAt(0.65,    QColor(242, 242, 242, 0));
            gradient.setColorAt(0.75,    QColor(242, 242, 242, 255));

            painter->fillRect(opt.rect, gradient);
131
        }
132 133 134 135 136 137 138 139 140 141 142
        else
        {
            QLinearGradient gradient(opt.rect.topLeft(),opt.rect.topRight());
            gradient.setColorAt(0.8,    QColor(255, 255, 255, 0));
            gradient.setColorAt(1.0,    QColor(255, 255, 255, 255));

            painter->fillRect(opt.rect, gradient);
        }
    }
    else
    {
143 144 145 146 147
        QStyledItemDelegate::paint(painter, option, index);
    }
}

QSize
148 149 150
SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
                           , const QModelIndex& index
                           ) const
151
{
152 153 154
    QSize size = QStyledItemDelegate::sizeHint(option, index);
    size.setHeight(cellHeight_);
    size.setWidth(cellWidth_);
155

156 157
    return size;
}