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> |
| 29 | #import <QtGui/QBitmap> |
| 30 | #import <QtWidgets/QApplication> |
| 31 | #import <QtGui/QImage> |
| 32 | #import <QtMacExtras/qmacfunctions.h> |
| 33 | #import <QtGui/QPalette> |
| 34 | |
| 35 | //Ring |
| 36 | #import <person.h> |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 37 | #import <profilemodel.h> |
| 38 | #import <profile.h> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 39 | #import <contactmethod.h> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 40 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 41 | namespace Interfaces { |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 42 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 43 | ImageManipulationDelegate::ImageManipulationDelegate() |
| 44 | { |
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 | } |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 47 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 48 | QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) { |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 49 | const int radius = size.height() / 2; |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 50 | |
| 51 | QPixmap pxm; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 52 | if (c && c->photo().isValid()) { |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 53 | QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding, |
| 54 | Qt::SmoothTransformation)); |
| 55 | |
| 56 | QPixmap finalImg; |
| 57 | if (contactPhoto.size() != size) { |
| 58 | finalImg = crop(contactPhoto, size); |
| 59 | } else |
| 60 | finalImg = contactPhoto; |
| 61 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 62 | pxm = QPixmap(size); |
| 63 | pxm.fill(Qt::transparent); |
| 64 | QPainter painter(&pxm); |
| 65 | |
| 66 | //Clear the pixmap |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 67 | painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 68 | painter.setCompositionMode(QPainter::CompositionMode_Clear); |
| 69 | painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white)); |
| 70 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 71 | |
| 72 | //Add corner radius to the Pixmap |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 73 | QRect pxRect = finalImg.rect(); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 74 | QBitmap mask(pxRect.size()); |
| 75 | QPainter customPainter(&mask); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 76 | customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 77 | customPainter.fillRect (pxRect , Qt::white ); |
| 78 | customPainter.setBackground (Qt::black ); |
| 79 | customPainter.setBrush (Qt::black ); |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 80 | customPainter.drawRoundedRect(pxRect,radius,radius ); |
| 81 | finalImg.setMask (mask ); |
| 82 | painter.drawPixmap (0,0,finalImg ); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 83 | painter.setBrush (Qt::NoBrush ); |
| 84 | painter.setPen (Qt::black ); |
| 85 | painter.setCompositionMode (QPainter::CompositionMode_SourceIn); |
| 86 | painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 87 | } |
| 88 | else { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 89 | pxm = drawDefaultUserPixmap(size); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return pxm; |
| 93 | } |
| 94 | |
Alexandre Lision | f02a32b | 2016-04-19 15:01:07 -0400 | [diff] [blame] | 95 | QPixmap |
| 96 | ImageManipulationDelegate::crop(QPixmap& photo, const QSize& destSize) |
| 97 | { |
| 98 | auto initSize = photo.size(); |
| 99 | float leftDelta = 0; |
| 100 | float topDelta = 0; |
| 101 | |
| 102 | if (destSize.height() == initSize.height()) { |
| 103 | leftDelta = (destSize.width() - initSize.width()) / 2; |
| 104 | } else { |
| 105 | topDelta = (destSize.height() - initSize.height()) / 2; |
| 106 | } |
| 107 | |
| 108 | float xScale = (float)destSize.width() / initSize.width(); |
| 109 | float yScale = (float)destSize.height() / initSize.height(); |
| 110 | |
| 111 | QRectF destRect(leftDelta, topDelta, |
| 112 | initSize.width() - leftDelta, initSize.height() - topDelta); |
| 113 | |
| 114 | destRect.setLeft(leftDelta * xScale); |
| 115 | destRect.setTop(topDelta * yScale); |
| 116 | |
| 117 | destRect.setWidth((initSize.width() - leftDelta) * xScale); |
| 118 | destRect.setHeight((initSize.height() - topDelta) * yScale); |
| 119 | |
| 120 | return photo.copy(destRect.toRect()); |
| 121 | } |
| 122 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 123 | QVariant |
| 124 | ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence) |
| 125 | { |
| 126 | return callPhoto(c->peerContactMethod(), size, displayPresence); |
| 127 | } |
| 128 | |
| 129 | QVariant |
| 130 | ImageManipulationDelegate::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence) |
| 131 | { |
| 132 | if (n->contact()) { |
| 133 | return contactPhoto(n->contact(), size, displayPresence); |
| 134 | } else { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 135 | return drawDefaultUserPixmap(size); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type) |
| 140 | { |
| 141 | QImage image; |
| 142 | //For now, ENCODING is only base64 and image type PNG or JPG |
| 143 | const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1()); |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 144 | if (!ret) { |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 145 | qDebug() << "vCard image loading failed"; |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 146 | return drawDefaultUserPixmap(decorationSize); |
| 147 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 148 | |
| 149 | return QPixmap::fromImage(image); |
| 150 | } |
| 151 | |
| 152 | QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm) |
| 153 | { |
| 154 | //Preparation of our QPixmap |
| 155 | QByteArray bArray; |
| 156 | QBuffer buffer(&bArray); |
| 157 | buffer.open(QIODevice::WriteOnly); |
| 158 | |
| 159 | //PNG ? |
| 160 | (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG"); |
| 161 | buffer.close(); |
| 162 | |
| 163 | return bArray; |
| 164 | } |
| 165 | |
| 166 | QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) { |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 167 | // create the image somehow, load from file, draw into it... |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 168 | auto sourceImgRef = CGImageSourceCreateWithData((__bridge CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 169 | auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 170 | auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size)); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 171 | CFRelease(sourceImgRef); |
| 172 | CFRelease(imgRef); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 173 | |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 174 | return finalpxm; |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 175 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 176 | |
| 177 | CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) { |
| 178 | // create context, keeping original image properties |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 179 | CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(), |
| 180 | CGImageGetBitsPerComponent(image), |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 181 | CGImageGetBytesPerRow(image), |
| 182 | CGImageGetColorSpace(image), |
| 183 | kCGImageAlphaPremultipliedLast); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 184 | |
| 185 | if(context == NULL) |
| 186 | return nil; |
| 187 | |
| 188 | // draw image to context (resizing it) |
| 189 | CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image); |
| 190 | // extract resulting image from context |
| 191 | CGImageRef imgRef = CGBitmapContextCreateImage(context); |
| 192 | CGContextRelease(context); |
| 193 | |
| 194 | return imgRef; |
| 195 | } |
| 196 | |
| 197 | QVariant |
| 198 | ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent) |
| 199 | { |
| 200 | Q_UNUSED(p) |
| 201 | Q_UNUSED(size) |
| 202 | Q_UNUSED(displayPresence) |
| 203 | Q_UNUSED(isPresent) |
| 204 | return QVariant(); |
| 205 | } |
| 206 | |
| 207 | QVariant |
| 208 | ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index) |
| 209 | { |
| 210 | Q_UNUSED(index) |
| 211 | return QVariant(); |
| 212 | } |
| 213 | |
| 214 | QVariant |
| 215 | ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const |
| 216 | { |
| 217 | Q_UNUSED(interface) |
| 218 | Q_UNUSED(hint) |
| 219 | return QVariant(); |
| 220 | } |
| 221 | QVariant |
| 222 | ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const |
| 223 | { |
| 224 | Q_UNUSED(level) |
| 225 | return QVariant(); |
| 226 | } |
| 227 | QVariant |
| 228 | ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const |
| 229 | { |
| 230 | Q_UNUSED(cat) |
| 231 | return QVariant(); |
| 232 | } |
| 233 | QVariant |
| 234 | ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const |
| 235 | { |
| 236 | Q_UNUSED(cat) |
| 237 | return QVariant(); |
| 238 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 239 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 240 | QVariant |
| 241 | ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const |
| 242 | { |
| 243 | Q_UNUSED(state) |
| 244 | return QVariant(); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 245 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 246 | |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 247 | QVariant ImageManipulationDelegate::decorationRole(const QModelIndex& index) |
| 248 | { |
| 249 | Q_UNUSED(index) |
| 250 | return QVariant(); |
| 251 | } |
| 252 | |
| 253 | QVariant ImageManipulationDelegate::decorationRole(const Call* c) |
| 254 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 255 | if (c && c->peerContactMethod() |
| 256 | && c->peerContactMethod()->contact()) { |
| 257 | return contactPhoto(c->peerContactMethod()->contact(), decorationSize); |
| 258 | } else |
| 259 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | QVariant ImageManipulationDelegate::decorationRole(const ContactMethod* cm) |
| 263 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 264 | QImage photo; |
| 265 | if (cm && cm->contact() && cm->contact()->photo().isValid()) |
| 266 | return contactPhoto(cm->contact(), decorationSize); |
| 267 | else |
| 268 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | QVariant ImageManipulationDelegate::decorationRole(const Person* p) |
| 272 | { |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 273 | return contactPhoto(const_cast<Person*>(p), decorationSize); |
| 274 | } |
| 275 | |
| 276 | QVariant ImageManipulationDelegate::decorationRole(const Account* acc) |
| 277 | { |
| 278 | Q_UNUSED(acc) |
| 279 | if (auto pro = ProfileModel::instance().selectedProfile()) |
| 280 | return contactPhoto(pro->person(), decorationSize); |
| 281 | return drawDefaultUserPixmap(decorationSize); |
Stepan Salenikovich | 3a6ea30 | 2016-01-08 13:54:34 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 284 | } // namespace Interfaces |