smartlistdelegate.cpp 4.98 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 44
    painter->setRenderHint(QPainter::Antialiasing);
    opt.decorationSize = QSize(sizeImage_, sizeImage_);
Edric Milaret's avatar
Edric Milaret committed
45 46 47 48
    opt.decorationPosition = QStyleOptionViewItem::Left;
    opt.decorationAlignment = Qt::AlignCenter;
    if (opt.state & QStyle::State_HasFocus)
        opt.state ^= QStyle::State_HasFocus;
49

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

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

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

Edric Milaret's avatar
Edric Milaret committed
60
    const int currentRow = index.row();
61 62 63

    if (currentRow == rowHighlighted_)
        emit rowSelected(opt.rect);
Edric Milaret's avatar
Edric Milaret committed
64 65 66

    QFont font(painter->font());
    font.setPointSize(fontSize_);
67 68

    QPen pen(painter->pen());
Edric Milaret's avatar
Edric Milaret committed
69 70 71 72 73 74 75 76

    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());
    }
77 78 79 80
    if (index.column() == 0)
    {
        painter->setPen(pen);

81
        QVariant name = index.data(static_cast<int>(Ring::Role::Name));
82 83
        if (name.isValid())
        {
Edric Milaret's avatar
Edric Milaret committed
84
            pen.setColor(lightBlack_);
85 86 87 88
            painter->setPen(pen);
            font.setBold(true);
            painter->setFont(font);
            painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, name.toString());
89 90 91
        }

        QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
Edric Milaret's avatar
Edric Milaret committed
92
        pen.setColor(grey_);
93 94 95 96 97 98
        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
99
            painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
100 101 102 103 104 105
                               rect.top() + rect.height()/2,
                               rect.width(), rect.height()/2),
                               Qt::AlignTop | Qt::AlignLeft, state.toString());
        }
        else
        {
106
            QVariant lastUsed = index.data(static_cast<int>(Ring::Role::FormattedLastUsed));
107 108
            if (lastUsed.isValid())
            {
Edric Milaret's avatar
Edric Milaret committed
109
                painter->drawText(QRect(16 + rect.left() + dx_ + sizeImage_,
110 111 112
                                   rect.top() + rect.height()/2,
                                   rect.width(), rect.height()/2),
                                   Qt::AlignTop | Qt::AlignLeft, lastUsed.toString());
113 114
            }
        }
115 116 117
    }
    else
    {
Edric Milaret's avatar
Edric Milaret committed
118
        QItemDelegate::paint(painter, opt, index);
119 120 121 122
    }
}

QSize
123 124 125
SmartListDelegate::sizeHint(const QStyleOptionViewItem& option
                           , const QModelIndex& index
                           ) const
126
{
Edric Milaret's avatar
Edric Milaret committed
127
    QSize size = QItemDelegate::sizeHint(option, index);
128 129 130 131
    size.setHeight(cellHeight_);
    size.setWidth(cellWidth_);
    return size;
}