pixbufmanipulator.cpp 5.24 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1 2 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 30 31
/***************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * 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 "pixbufmanipulator.h"

#include <QtCore/QSize>
#include <QtCore/QMetaType>

#include <person.h>
#include <memory>
#include <call.h>
#include <contactmethod.h>

namespace Interfaces {

PixbufManipulator::PixbufManipulator()
Edric Milaret's avatar
Edric Milaret committed
32
    : fallbackAvatar_(QImage(":/images/user/btn-default-userpic.svg"))
Edric Milaret's avatar
Edric Milaret committed
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{}

QImage
PixbufManipulator::scaleAndFrame(const QImage photo, const QSize& size)
{
    return photo.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

QVariant
PixbufManipulator::callPhoto(Call* c, const QSize& size, bool displayPresence)
{
    if (!c || c->type() == Call::Type::CONFERENCE)
        return QVariant::fromValue(scaleAndFrame(fallbackAvatar_, size));
    return callPhoto(c->peerContactMethod(), size, displayPresence);
}

QVariant
PixbufManipulator::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence)
{
    if (n && n->contact()) {
        return contactPhoto(n->contact(), size, displayPresence);
    } else {
        return QVariant::fromValue(scaleAndFrame(fallbackAvatar_, size));
    }
}

QVariant
PixbufManipulator::contactPhoto(Person* c, const QSize& size, bool displayPresence)
{
    Q_UNUSED(displayPresence);

    /**
     * try to get the photo
     * otherwise use the fallback avatar
     */

    QImage photo;

    if (c->photo().isValid())
        photo = c->photo().value<QImage>();
    else
        photo = fallbackAvatar_;

    return QVariant::fromValue(scaleAndFrame(photo, size));
}

QVariant PixbufManipulator::personPhoto(const QByteArray& data, const QString& type)
{
    Q_UNUSED(type);
    Q_UNUSED(data);
    return QVariant();
}

QVariant
PixbufManipulator::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent)
{
    Q_UNUSED(p)
    Q_UNUSED(size)
    Q_UNUSED(displayPresence)
    Q_UNUSED(isPresent)
    return QVariant();
}

QVariant
PixbufManipulator::securityIssueIcon(const QModelIndex& index)
{
    Q_UNUSED(index)
    return QVariant();
}

QByteArray
PixbufManipulator::toByteArray(const QVariant& pxm)
{
    Q_UNUSED(pxm);
    return QByteArray();
}

QVariant
PixbufManipulator::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const
{
    Q_UNUSED(interface)
    Q_UNUSED(hint)
    return QVariant();
}
QVariant
PixbufManipulator::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const
{
    Q_UNUSED(level)
    return QVariant();
}
QVariant
PixbufManipulator::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const
{
    Q_UNUSED(cat)
    return QVariant();
}
QVariant
PixbufManipulator::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const
{
    Q_UNUSED(cat)
    return QVariant();
}
QVariant
PixbufManipulator::userActionIcon(const UserActionElement& state) const
{
    Q_UNUSED(state)
    return QVariant();
}

QVariant PixbufManipulator::decorationRole(const QModelIndex& index)
{
    Q_UNUSED(index)
    return QVariant();
}

QVariant PixbufManipulator::decorationRole(const Call* c)
{
Edric Milaret's avatar
Edric Milaret committed
150
    QImage photo;
Edric Milaret's avatar
Edric Milaret committed
151 152 153
    if (c && c->peerContactMethod()
            && c->peerContactMethod()->contact()
            && c->peerContactMethod()->contact()->photo().isValid()) {
Edric Milaret's avatar
Edric Milaret committed
154
        photo =  c->peerContactMethod()->contact()->photo().value<QImage>();
Edric Milaret's avatar
Edric Milaret committed
155
    }
Edric Milaret's avatar
Edric Milaret committed
156 157 158
    else
        photo = fallbackAvatar_;
    return QVariant::fromValue(scaleAndFrame(photo, imgSize_));
Edric Milaret's avatar
Edric Milaret committed
159 160 161 162
}

QVariant PixbufManipulator::decorationRole(const ContactMethod* cm)
{
Edric Milaret's avatar
Edric Milaret committed
163
    QImage photo;
Edric Milaret's avatar
Edric Milaret committed
164
    if (cm && cm->contact() && cm->contact()->photo().isValid())
Edric Milaret's avatar
Edric Milaret committed
165 166 167 168
        photo = cm->contact()->photo().value<QImage>();
    else
        photo = fallbackAvatar_;
    return QVariant::fromValue(scaleAndFrame(photo, imgSize_));
Edric Milaret's avatar
Edric Milaret committed
169 170 171 172
}

QVariant PixbufManipulator::decorationRole(const Person* p)
{
Edric Milaret's avatar
Edric Milaret committed
173
    QImage photo;
Edric Milaret's avatar
Edric Milaret committed
174
    if (p && p->photo().isValid())
Edric Milaret's avatar
Edric Milaret committed
175 176 177 178
        photo = p->photo().value<QImage>();
    else
        photo = fallbackAvatar_;
    return QVariant::fromValue(scaleAndFrame(photo, imgSize_));
Edric Milaret's avatar
Edric Milaret committed
179 180 181
}

} // namespace Interfaces