Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony Léonard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
Olivier SOLDANO | 47aa97f | 2017-04-04 10:40:00 -0400 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> * |
| 5 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> * |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 6 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> * |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 3 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 20 | **************************************************************************/ |
| 21 | |
| 22 | #include "pixbufmanipulator.h" |
| 23 | |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 24 | #include <QSize> |
| 25 | #include <QMetaType> |
| 26 | #include <QImage> |
| 27 | #include <QIODevice> |
| 28 | #include <QByteArray> |
| 29 | #include <QBuffer> |
Anthony Léonard | 3d920d8 | 2017-07-31 13:51:16 -0400 | [diff] [blame] | 30 | #include <QPainter> |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 31 | #include <QCryptographicHash> |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 32 | |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 33 | #include "person.h" |
| 34 | #include "call.h" |
| 35 | #include "contactmethod.h" |
| 36 | #include "profilemodel.h" |
| 37 | #include "profile.h" |
| 38 | |
| 39 | #include "utils.h" |
Anthony Léonard | 3d920d8 | 2017-07-31 13:51:16 -0400 | [diff] [blame] | 40 | #include "ringthemeutils.h" |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 41 | #undef interface |
| 42 | |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 43 | static const QSize IMAGE_SIZE {128, 128}; |
| 44 | |
| 45 | QColor |
| 46 | getAvatarColor(const QString& canonicalUri) { |
| 47 | if (canonicalUri.isEmpty()) { |
| 48 | return RingTheme::defaultAvatarColor_; |
| 49 | } |
| 50 | auto h = QString(QCryptographicHash::hash(canonicalUri.toLocal8Bit(), QCryptographicHash::Md5).toHex()); |
| 51 | if (h.isEmpty() || h.isNull()) { |
| 52 | return RingTheme::defaultAvatarColor_; |
| 53 | } |
| 54 | uint8_t colorsLength = sizeof(RingTheme::avatarColors_) / sizeof(QColor); |
| 55 | bool ok; |
| 56 | auto colorIndex = QString(h.at(0)).toUInt(&ok, colorsLength); |
| 57 | return RingTheme::avatarColors_[colorIndex]; |
| 58 | } |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 59 | |
| 60 | // |
| 61 | // Generate a QImage representing a dummy user avatar, when user doesn't provide it. |
| 62 | // Current rendering is a flat colored circle with a centered letter. |
| 63 | // The color of the letter is computed from the circle color to be visible whaterver be the circle color. |
| 64 | // |
| 65 | // \param color circle color |
| 66 | // \param letter centerer letter |
| 67 | // |
| 68 | static QImage |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 69 | fallbackAvatar(const QSize size, const QString& canonicalUriStr, const QString& letterStr = QString()) |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 70 | { |
| 71 | // We start with a transparent avatar |
| 72 | QImage avatar(size, QImage::Format_ARGB32); |
| 73 | avatar.fill(Qt::transparent); |
| 74 | |
| 75 | // We pick a color based on the passed character |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 76 | QColor avColor = getAvatarColor(canonicalUriStr); |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 77 | |
| 78 | // We draw a circle with this color |
| 79 | QPainter painter(&avatar); |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 80 | painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 81 | painter.setPen(Qt::transparent); |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 82 | painter.setBrush(avColor.lighter(110)); |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 83 | painter.drawEllipse(avatar.rect()); |
| 84 | |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 85 | // If a letter was passed, then we paint a letter in the circle, |
| 86 | // otherwise we draw the default avatar icon |
| 87 | if (!letterStr.isEmpty()) { |
| 88 | auto letter = letterStr.at(0).toUpper().toLatin1(); |
| 89 | QFont font("Arial", avatar.height() / 2.66667, QFont::Medium); |
| 90 | painter.setFont(font); |
| 91 | painter.setPen(Qt::white); |
| 92 | painter.drawText(avatar.rect(), QString(letter), QTextOption(Qt::AlignCenter)); |
| 93 | } else { |
| 94 | QRect overlayRect = avatar.rect(); |
| 95 | qreal margin = (0.05 * overlayRect.width()); |
| 96 | overlayRect.moveLeft(overlayRect.left() + margin * 0.5); |
| 97 | overlayRect.moveTop(overlayRect.top() + margin * 0.5); |
| 98 | overlayRect.setWidth(overlayRect.width() - margin); |
| 99 | overlayRect.setHeight(overlayRect.height() - margin); |
| 100 | painter.drawPixmap(overlayRect, QPixmap(":/images/default-avatar-overlay.svg")); |
| 101 | } |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 102 | |
| 103 | return avatar; |
| 104 | } |
| 105 | |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 106 | QImage |
| 107 | fallbackAvatar |
| 108 | (const QSize size, const ContactMethod* cm) |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 109 | { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 110 | if (cm == nullptr) { |
| 111 | return QImage(); |
| 112 | } |
| 113 | QImage image; |
| 114 | auto letterStr = QString(); |
| 115 | if (cm->uri().userinfo() != cm->bestName()) { |
| 116 | letterStr = cm->bestName(); |
| 117 | } |
| 118 | image = fallbackAvatar( size, |
| 119 | cm->uri().full(), |
| 120 | letterStr); |
| 121 | return image; |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 124 | /*Namespace Interfaces collide with QBuffer*/ |
| 125 | QByteArray QImageToByteArray(QImage image) |
| 126 | { |
| 127 | QByteArray ba; |
| 128 | QBuffer buffer(&ba); |
| 129 | buffer.open(QIODevice::WriteOnly); |
| 130 | image.save(&buffer, "PNG"); |
| 131 | return ba; |
| 132 | } |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 133 | |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 134 | QImage |
| 135 | PixbufManipulator::scaleAndFrame(const QImage photo, const QSize& size) |
| 136 | { |
| 137 | return photo.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 138 | } |
| 139 | |
| 140 | QVariant |
| 141 | PixbufManipulator::callPhoto(Call* c, const QSize& size, bool displayPresence) |
| 142 | { |
Olivier SOLDANO | ad0fabb | 2016-11-25 09:08:01 -0500 | [diff] [blame] | 143 | if (!c || c->type() == Call::Type::CONFERENCE){ |
Anthony Léonard | 3d920d8 | 2017-07-31 13:51:16 -0400 | [diff] [blame] | 144 | return QVariant::fromValue(fallbackAvatar(size, |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 145 | c->peerContactMethod()->uri().full(), |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 146 | c->peerContactMethod()->bestName())); |
Olivier SOLDANO | ad0fabb | 2016-11-25 09:08:01 -0500 | [diff] [blame] | 147 | } |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 148 | return callPhoto(c->peerContactMethod(), size, displayPresence); |
| 149 | } |
| 150 | |
| 151 | QVariant |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 152 | PixbufManipulator::callPhoto(const ContactMethod* cm, const QSize& size, bool displayPresence) |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 153 | { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 154 | if (cm && cm->contact()) { |
| 155 | return contactPhoto(cm->contact(), size, displayPresence); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 156 | } else { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 157 | return QVariant::fromValue(fallbackAvatar(size, cm)); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | QVariant |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 162 | PixbufManipulator::contactPhoto(Person* p, const QSize& size, bool displayPresence) |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 163 | { |
| 164 | Q_UNUSED(displayPresence); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 165 | QImage photo; |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 166 | if (p->photo().isValid()) { |
| 167 | photo = p->photo().value<QImage>(); |
Olivier SOLDANO | ad0fabb | 2016-11-25 09:08:01 -0500 | [diff] [blame] | 168 | } else { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 169 | photo = fallbackAvatar(IMAGE_SIZE, p->phoneNumbers().at(0)); |
Olivier SOLDANO | ad0fabb | 2016-11-25 09:08:01 -0500 | [diff] [blame] | 170 | } |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 171 | return QVariant::fromValue(scaleAndFrame(photo, size)); |
| 172 | } |
| 173 | |
| 174 | QVariant PixbufManipulator::personPhoto(const QByteArray& data, const QString& type) |
| 175 | { |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 176 | QImage avatar; |
| 177 | QByteArray ba = type.toLatin1(); |
| 178 | const char* c_str2 = ba.data(); |
| 179 | if (avatar.loadFromData(data.fromBase64(data), c_str2)) |
| 180 | return Utils::getCirclePhoto(avatar, avatar.size().width()); |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 181 | return fallbackAvatar(IMAGE_SIZE, QString()); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | QVariant |
| 185 | PixbufManipulator::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent) |
| 186 | { |
| 187 | Q_UNUSED(p) |
| 188 | Q_UNUSED(size) |
| 189 | Q_UNUSED(displayPresence) |
| 190 | Q_UNUSED(isPresent) |
| 191 | return QVariant(); |
| 192 | } |
| 193 | |
| 194 | QVariant |
| 195 | PixbufManipulator::securityIssueIcon(const QModelIndex& index) |
| 196 | { |
| 197 | Q_UNUSED(index) |
| 198 | return QVariant(); |
| 199 | } |
| 200 | |
| 201 | QByteArray |
| 202 | PixbufManipulator::toByteArray(const QVariant& pxm) |
| 203 | { |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 204 | auto image = pxm.value<QImage>(); |
| 205 | QByteArray ba = QImageToByteArray(image); |
| 206 | return ba; |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | QVariant |
Olivier SOLDANO | 41e61ab | 2017-08-04 16:47:16 -0400 | [diff] [blame] | 210 | PixbufManipulator::collectionIcon(const CollectionInterface* colItf, PixmapManipulatorI::CollectionIconHint hint) const |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 211 | { |
Olivier SOLDANO | 41e61ab | 2017-08-04 16:47:16 -0400 | [diff] [blame] | 212 | Q_UNUSED(colItf) |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 213 | Q_UNUSED(hint) |
| 214 | return QVariant(); |
| 215 | } |
| 216 | QVariant |
| 217 | PixbufManipulator::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const |
| 218 | { |
| 219 | Q_UNUSED(level) |
| 220 | return QVariant(); |
| 221 | } |
| 222 | QVariant |
| 223 | PixbufManipulator::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const |
| 224 | { |
| 225 | Q_UNUSED(cat) |
| 226 | return QVariant(); |
| 227 | } |
| 228 | QVariant |
| 229 | PixbufManipulator::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const |
| 230 | { |
| 231 | Q_UNUSED(cat) |
| 232 | return QVariant(); |
| 233 | } |
| 234 | QVariant |
| 235 | PixbufManipulator::userActionIcon(const UserActionElement& state) const |
| 236 | { |
| 237 | Q_UNUSED(state) |
| 238 | return QVariant(); |
| 239 | } |
| 240 | |
| 241 | QVariant PixbufManipulator::decorationRole(const QModelIndex& index) |
| 242 | { |
| 243 | Q_UNUSED(index) |
| 244 | return QVariant(); |
| 245 | } |
| 246 | |
| 247 | QVariant PixbufManipulator::decorationRole(const Call* c) |
| 248 | { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 249 | QImage photo; |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 250 | if (c && c->peerContactMethod() |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 251 | && c->peerContactMethod()->contact() |
| 252 | && c->peerContactMethod()->contact()->photo().isValid()) { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 253 | photo = c->peerContactMethod()->contact()->photo().value<QImage>(); |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 254 | } else { |
| 255 | fallbackAvatar(IMAGE_SIZE, c->peerContactMethod()); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 256 | } |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 257 | return QVariant::fromValue(scaleAndFrame(photo, IMAGE_SIZE)); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | QVariant PixbufManipulator::decorationRole(const ContactMethod* cm) |
| 261 | { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 262 | QImage photo; |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 263 | if (cm && cm->contact() |
| 264 | && cm->contact()->photo().isValid()) { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 265 | photo = cm->contact()->photo().value<QImage>(); |
Olivier SOLDANO | 6c46d35 | 2017-08-24 10:54:55 -0400 | [diff] [blame] | 266 | } else { |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 267 | photo = fallbackAvatar(IMAGE_SIZE, cm); |
Olivier SOLDANO | 6c46d35 | 2017-08-24 10:54:55 -0400 | [diff] [blame] | 268 | } |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 269 | return QVariant::fromValue(scaleAndFrame(photo, IMAGE_SIZE)); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | QVariant PixbufManipulator::decorationRole(const Person* p) |
| 273 | { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 274 | QImage photo; |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 275 | if (p && p->photo().isValid()) { |
Edric Milaret | a13b48c | 2016-01-18 14:42:35 -0500 | [diff] [blame] | 276 | photo = p->photo().value<QImage>(); |
Andreas Traczyk | 072291f | 2018-08-16 16:48:58 -0400 | [diff] [blame^] | 277 | } else { |
| 278 | photo = fallbackAvatar(IMAGE_SIZE, p->phoneNumbers().at(0)); |
| 279 | } |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 280 | return QVariant::fromValue(scaleAndFrame(photo, IMAGE_SIZE)); |
Edric Milaret | a0ebd06 | 2016-01-13 12:18:23 -0500 | [diff] [blame] | 281 | } |
| 282 | |
Edric Milaret | a7cf865 | 2016-04-19 15:37:51 -0400 | [diff] [blame] | 283 | QVariant PixbufManipulator::decorationRole(const Account* acc) |
| 284 | { |
| 285 | Q_UNUSED(acc) |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 286 | return Utils::getCirclePhoto(ProfileModel::instance(). |
Guillaume Roguez | 79cab80 | 2017-08-25 09:59:13 -0400 | [diff] [blame] | 287 | selectedProfile()->person()->photo().value<QImage>(), |
| 288 | IMAGE_SIZE.width()); |
Anthony Léonard | 3d920d8 | 2017-07-31 13:51:16 -0400 | [diff] [blame] | 289 | } |