smartlistdelegate.cpp 4.87 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 45 46
    opt.decorationPosition = QStyleOptionViewItem::Left;
    opt.decorationAlignment = Qt::AlignCenter;
    if (opt.state & QStyle::State_HasFocus)
        opt.state ^= QStyle::State_HasFocus;
47

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

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

Edric Milaret's avatar
Edric Milaret committed
54 55 56
    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>()));
57

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

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

    QFont font(painter->font());
    font.setPointSize(fontSize_);
65 66

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

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

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

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

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