stylecontactlistdelegate.cpp 4.88 KB
Newer Older
Konrad Czapla's avatar
Konrad Czapla committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
  This file is part of KAddressBook.

  SPDX-FileCopyrightText: 2020 Konrad Czapla <kondzio89dev@gmail.com>

  SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "stylecontactlistdelegate.h"
#include "contactinfoproxymodel.h"

#include <Akonadi/Contact/ContactsTreeModel>
#include <KLocalizedString>

#include <QApplication>
#include <QPainter>
#include <QPainterPath>
#include <QImage>

StyleContactListDelegate::StyleContactListDelegate(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
21 22
    : QStyledItemDelegate(parent)
    , mKImageSize(50, 50)
Konrad Czapla's avatar
Konrad Czapla committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 74 75 76 77 78 79 80 81 82
{
}

void StyleContactListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_ASSERT(index.isValid());

    if (Akonadi::ContactsTreeModel::Column::FullName == index.column()) {
        QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);

        const QRectF optionRect = option.rect.marginsRemoved(QMargins() + static_cast<int>(mKMargin));

        QRectF pictureRect = QRectF(optionRect.topLeft(), mKImageSize);
        if (mKImageSize.width() > optionRect.size().width()) {
            const qreal width = option.rect.size().width();
            const qreal height = option.rect.size().height();
            const QMargins fitMargins(0, (qMax(width, height) - qMin(width, height)) / qreal(2),
                                      0, (qMax(width, height) - qMin(width, height)) / qreal(2));

            pictureRect = optionRect.marginsRemoved(fitMargins);
        }
        QRectF nameTextRect(optionRect.topLeft(), QSize(optionRect.width(), optionRect.height() / qreal(2)));
        QRectF descriptionTextRect = nameTextRect;
        descriptionTextRect.moveBottomLeft(optionRect.bottomLeft());

        QMargins textMargin;
        switch (static_cast<int>(option.widget->layoutDirection())) {
        case Qt::LayoutDirection::LeftToRight:
            textMargin.setLeft(mKMargin);

            nameTextRect.setLeft(pictureRect.bottomRight().x());
            nameTextRect = nameTextRect.marginsRemoved(textMargin);

            descriptionTextRect.setLeft(pictureRect.bottomRight().x());
            descriptionTextRect = descriptionTextRect.marginsRemoved(textMargin);
            break;
        case Qt::LayoutDirection::RightToLeft:
            pictureRect.moveRight(optionRect.bottomRight().x());
            textMargin.setRight(mKMargin);

            nameTextRect.setRight(pictureRect.bottomLeft().x());
            nameTextRect = nameTextRect.marginsRemoved(textMargin);

            descriptionTextRect.setRight(pictureRect.bottomLeft().x());
            descriptionTextRect = descriptionTextRect.marginsRemoved(textMargin);
            break;
        }

        QPainterPath path;
        path.addEllipse(pictureRect);
        painter->save();
        painter->setClipPath(path);
        painter->setPen(QPen(Qt::darkGray, qreal(4)));
        painter->setRenderHint(QPainter::Antialiasing);
        painter->drawPath(path);
        painter->setFont(QFont(option.font.family(), 12, QFont::Bold, true));

        if (index.data(ContactInfoProxyModel::Roles::PictureRole).value<QImage>().isNull()) {
            const QString initials = index.data(ContactInfoProxyModel::Roles::InitialsRole).value<QString>();
            painter->drawText(pictureRect, Qt::AlignCenter, painter->fontMetrics().elidedText(initials,
Laurent Montel's avatar
Laurent Montel committed
83
                                                                                              Qt::ElideRight, pictureRect.width() - qreal(10)));
Konrad Czapla's avatar
Konrad Czapla committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        } else {
            const qreal dpr = qApp->devicePixelRatio();
            QImage image(index.data(ContactInfoProxyModel::Roles::PictureRole).value<QImage>());
            image = image.scaled(mKImageSize * dpr, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
            image.setDevicePixelRatio(dpr);
            painter->drawImage(pictureRect, image);
        }

        painter->restore();

        painter->setFont(QFont(option.font.family(), 11));
        const QString name = index.data(Qt::DisplayRole).value<QString>();
        painter->drawText(nameTextRect, Qt::AlignLeft | Qt::AlignVCenter,
                          painter->fontMetrics().elidedText(name, Qt::ElideRight, nameTextRect.width()));

        const QString description = index.data(ContactInfoProxyModel::Roles::DescriptionRole).value<QString>();
100 101 102 103 104
        if (!description.isEmpty()) {
            painter->setFont(QFont(option.font.family(), 8, -1, true));
            painter->drawText(descriptionTextRect, Qt::AlignLeft | Qt::AlignVCenter,
                              painter->fontMetrics().elidedText(description, Qt::ElideRight, descriptionTextRect.width()));
        }
Konrad Czapla's avatar
Konrad Czapla committed
105 106 107 108 109 110 111 112 113 114 115 116
        return;
    }
    QStyledItemDelegate::paint(painter, option, index);
}

QSize StyleContactListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = QStyledItemDelegate::sizeHint(option, index);
    size.setHeight(mKImageSize.height() + (qreal(2) * mKMargin));

    return size;
}