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