Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 18 | */ |
| 19 | #import "ImageManipulationDelegate.h" |
| 20 | |
| 21 | #import <Cocoa/Cocoa.h> |
| 22 | #import <Foundation/Foundation.h> |
| 23 | |
| 24 | //Qt |
| 25 | #import <QSize> |
| 26 | #import <QBuffer> |
| 27 | #import <QtGui/QColor> |
| 28 | #import <QtGui/QPainter> |
Alexandre Lision | 4f26462 | 2016-05-08 17:08:56 -0400 | [diff] [blame] | 29 | #import <QHash> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 30 | #import <QtGui/QBitmap> |
| 31 | #import <QtWidgets/QApplication> |
| 32 | #import <QtGui/QImage> |
| 33 | #import <QtMacExtras/qmacfunctions.h> |
| 34 | #import <QtGui/QPalette> |
| 35 | |
| 36 | //Ring |
| 37 | #import <person.h> |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 38 | #import <profilemodel.h> |
| 39 | #import <profile.h> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 40 | #import <contactmethod.h> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 41 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 42 | namespace Interfaces { |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 4f26462 | 2016-05-08 17:08:56 -0400 | [diff] [blame] | 44 | ImageManipulationDelegate::ImageManipulationDelegate() {} |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 45 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 46 | QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) { |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 47 | const int radius = size.height() / 2; |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 48 | QPixmap pxm; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 49 | if (c && c->photo().isValid()) { |
Alexandre Lision | 196545b | 2016-05-13 17:05:13 -0400 | [diff] [blame] | 50 | // Check cache |
| 51 | auto index = QStringLiteral("%1%2%3").arg(size.width()) |
| 52 | .arg(size.height()) |
| 53 | .arg(QString::fromUtf8(c->uid())); |
| 54 | |
| 55 | if (m_hContactsPixmap.contains(index)) { |
| 56 | return m_hContactsPixmap.value(index).second; |
| 57 | } |
| 58 | |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 59 | QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding, |
| 60 | Qt::SmoothTransformation)); |
| 61 | |
| 62 | QPixmap finalImg; |
| 63 | if (contactPhoto.size() != size) { |
| 64 | finalImg = crop(contactPhoto, size); |
| 65 | } else |
| 66 | finalImg = contactPhoto; |
| 67 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 68 | pxm = QPixmap(size); |
| 69 | pxm.fill(Qt::transparent); |
| 70 | QPainter painter(&pxm); |
| 71 | |
| 72 | //Clear the pixmap |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 73 | painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 74 | painter.setCompositionMode(QPainter::CompositionMode_Clear); |
| 75 | painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white)); |
| 76 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 77 | |
| 78 | //Add corner radius to the Pixmap |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 79 | QRect pxRect = finalImg.rect(); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 80 | QBitmap mask(pxRect.size()); |
| 81 | QPainter customPainter(&mask); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 82 | customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 83 | customPainter.fillRect (pxRect , Qt::white ); |
| 84 | customPainter.setBackground (Qt::black ); |
| 85 | customPainter.setBrush (Qt::black ); |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 86 | customPainter.drawRoundedRect(pxRect,radius,radius ); |
| 87 | finalImg.setMask (mask ); |
| 88 | painter.drawPixmap (0,0,finalImg ); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 89 | painter.setBrush (Qt::NoBrush ); |
| 90 | painter.setPen (Qt::black ); |
| 91 | painter.setCompositionMode (QPainter::CompositionMode_SourceIn); |
| 92 | painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 93 | |
Alexandre Lision | 196545b | 2016-05-13 17:05:13 -0400 | [diff] [blame] | 94 | // Save in cache |
| 95 | QPair<QMetaObject::Connection, QPixmap> toInsert; |
| 96 | toInsert.first = QObject::connect(c, |
| 97 | &Person::changed, |
| 98 | [=]() { |
| 99 | if (c) { |
| 100 | auto index = QStringLiteral("%1%2%3").arg(size.width()) |
| 101 | .arg(size.height()) |
| 102 | .arg(QString::fromUtf8(c->uid())); |
| 103 | if (m_hContactsPixmap.contains(index)) { |
| 104 | QObject::disconnect(m_hContactsPixmap.value(index).first); |
| 105 | m_hContactsPixmap.remove(index); |
| 106 | } |
| 107 | } |
| 108 | }); |
| 109 | toInsert.second = pxm; |
| 110 | m_hContactsPixmap.insert(index, toInsert); |
| 111 | |
| 112 | } else { |
| 113 | return drawDefaultUserPixmap(size); |
| 114 | } |
Alexandre Lision | afa56dc | 2016-05-08 17:30:20 -0400 | [diff] [blame] | 115 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 116 | return pxm; |
| 117 | } |
| 118 | |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 119 | QPixmap |
| 120 | ImageManipulationDelegate::crop(QPixmap& photo, const QSize& destSize) |
| 121 | { |
| 122 | auto initSize = photo.size(); |
| 123 | float leftDelta = 0; |
| 124 | float topDelta = 0; |
| 125 | |
| 126 | if (destSize.height() == initSize.height()) { |
| 127 | leftDelta = (destSize.width() - initSize.width()) / 2; |
| 128 | } else { |
| 129 | topDelta = (destSize.height() - initSize.height()) / 2; |
| 130 | } |
| 131 | |
| 132 | float xScale = (float)destSize.width() / initSize.width(); |
| 133 | float yScale = (float)destSize.height() / initSize.height(); |
| 134 | |
| 135 | QRectF destRect(leftDelta, topDelta, |
| 136 | initSize.width() - leftDelta, initSize.height() - topDelta); |
| 137 | |
| 138 | destRect.setLeft(leftDelta * xScale); |
| 139 | destRect.setTop(topDelta * yScale); |
| 140 | |
| 141 | destRect.setWidth((initSize.width() - leftDelta) * xScale); |
| 142 | destRect.setHeight((initSize.height() - topDelta) * yScale); |
| 143 | |
| 144 | return photo.copy(destRect.toRect()); |
| 145 | } |
| 146 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 147 | QVariant |
| 148 | ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence) |
| 149 | { |
| 150 | return callPhoto(c->peerContactMethod(), size, displayPresence); |
| 151 | } |
| 152 | |
| 153 | QVariant |
| 154 | ImageManipulationDelegate::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence) |
| 155 | { |
| 156 | if (n->contact()) { |
| 157 | return contactPhoto(n->contact(), size, displayPresence); |
| 158 | } else { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 159 | return drawDefaultUserPixmap(size); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type) |
| 164 | { |
| 165 | QImage image; |
| 166 | //For now, ENCODING is only base64 and image type PNG or JPG |
| 167 | const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1()); |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 168 | if (!ret) { |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 169 | qDebug() << "vCard image loading failed"; |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 170 | return drawDefaultUserPixmap(decorationSize); |
| 171 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 172 | |
| 173 | return QPixmap::fromImage(image); |
| 174 | } |
| 175 | |
| 176 | QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm) |
| 177 | { |
| 178 | //Preparation of our QPixmap |
| 179 | QByteArray bArray; |
| 180 | QBuffer buffer(&bArray); |
| 181 | buffer.open(QIODevice::WriteOnly); |
| 182 | |
| 183 | //PNG ? |
| 184 | (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG"); |
| 185 | buffer.close(); |
| 186 | |
| 187 | return bArray; |
| 188 | } |
| 189 | |
| 190 | QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) { |
Alexandre Lision | 4f26462 | 2016-05-08 17:08:56 -0400 | [diff] [blame] | 191 | |
| 192 | auto index = QStringLiteral("%1%2").arg(size.width()).arg(size.height()); |
| 193 | if (m_hDefaultUserPixmap.contains(index)) { |
| 194 | return m_hDefaultUserPixmap.value(index); |
| 195 | } |
| 196 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 197 | // create the image somehow, load from file, draw into it... |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 198 | auto sourceImgRef = CGImageSourceCreateWithData((__bridge CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 199 | auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 200 | auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size)); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 201 | CFRelease(sourceImgRef); |
| 202 | CFRelease(imgRef); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 203 | |
Alexandre Lision | 4f26462 | 2016-05-08 17:08:56 -0400 | [diff] [blame] | 204 | m_hDefaultUserPixmap.insert(index, finalpxm); |
| 205 | |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 206 | return finalpxm; |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 207 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 208 | |
| 209 | CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) { |
| 210 | // create context, keeping original image properties |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 211 | CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(), |
| 212 | CGImageGetBitsPerComponent(image), |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 213 | CGImageGetBytesPerRow(image), |
| 214 | CGImageGetColorSpace(image), |
| 215 | kCGImageAlphaPremultipliedLast); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 216 | |
| 217 | if(context == NULL) |
| 218 | return nil; |
| 219 | |
| 220 | // draw image to context (resizing it) |
| 221 | CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image); |
| 222 | // extract resulting image from context |
| 223 | CGImageRef imgRef = CGBitmapContextCreateImage(context); |
| 224 | CGContextRelease(context); |
| 225 | |
| 226 | return imgRef; |
| 227 | } |
| 228 | |
| 229 | QVariant |
| 230 | ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent) |
| 231 | { |
| 232 | Q_UNUSED(p) |
| 233 | Q_UNUSED(size) |
| 234 | Q_UNUSED(displayPresence) |
| 235 | Q_UNUSED(isPresent) |
| 236 | return QVariant(); |
| 237 | } |
| 238 | |
| 239 | QVariant |
| 240 | ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index) |
| 241 | { |
| 242 | Q_UNUSED(index) |
| 243 | return QVariant(); |
| 244 | } |
| 245 | |
| 246 | QVariant |
| 247 | ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const |
| 248 | { |
| 249 | Q_UNUSED(interface) |
| 250 | Q_UNUSED(hint) |
| 251 | return QVariant(); |
| 252 | } |
| 253 | QVariant |
| 254 | ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const |
| 255 | { |
| 256 | Q_UNUSED(level) |
| 257 | return QVariant(); |
| 258 | } |
| 259 | QVariant |
| 260 | ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const |
| 261 | { |
| 262 | Q_UNUSED(cat) |
| 263 | return QVariant(); |
| 264 | } |
| 265 | QVariant |
| 266 | ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const |
| 267 | { |
| 268 | Q_UNUSED(cat) |
| 269 | return QVariant(); |
| 270 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 271 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 272 | QVariant |
| 273 | ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const |
| 274 | { |
| 275 | Q_UNUSED(state) |
| 276 | return QVariant(); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 277 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 278 | |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 279 | QVariant ImageManipulationDelegate::decorationRole(const QModelIndex& index) |
| 280 | { |
| 281 | Q_UNUSED(index) |
| 282 | return QVariant(); |
| 283 | } |
| 284 | |
| 285 | QVariant ImageManipulationDelegate::decorationRole(const Call* c) |
| 286 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 287 | if (c && c->peerContactMethod() |
| 288 | && c->peerContactMethod()->contact()) { |
| 289 | return contactPhoto(c->peerContactMethod()->contact(), decorationSize); |
| 290 | } else |
| 291 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | QVariant ImageManipulationDelegate::decorationRole(const ContactMethod* cm) |
| 295 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 296 | QImage photo; |
| 297 | if (cm && cm->contact() && cm->contact()->photo().isValid()) |
| 298 | return contactPhoto(cm->contact(), decorationSize); |
| 299 | else |
| 300 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | QVariant ImageManipulationDelegate::decorationRole(const Person* p) |
| 304 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 305 | return contactPhoto(const_cast<Person*>(p), decorationSize); |
| 306 | } |
| 307 | |
| 308 | QVariant ImageManipulationDelegate::decorationRole(const Account* acc) |
| 309 | { |
| 310 | Q_UNUSED(acc) |
| 311 | if (auto pro = ProfileModel::instance().selectedProfile()) |
| 312 | return contactPhoto(pro->person(), decorationSize); |
| 313 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 316 | } // namespace Interfaces |