Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
| 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> |
| 37 | #import <contactmethod.h> |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 38 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 39 | namespace Interfaces { |
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 | ImageManipulationDelegate::ImageManipulationDelegate() |
| 42 | { |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 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 | 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 | |
| 49 | QPixmap pxm; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 50 | if (c && c->photo().isValid()) { |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 51 | QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaledToWidth(size.height(), |
| 52 | Qt::TransformationMode::SmoothTransformation)); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 53 | pxm = QPixmap(size); |
| 54 | pxm.fill(Qt::transparent); |
| 55 | QPainter painter(&pxm); |
| 56 | |
| 57 | //Clear the pixmap |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 58 | painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 59 | painter.setCompositionMode(QPainter::CompositionMode_Clear); |
| 60 | painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white)); |
| 61 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 62 | |
| 63 | //Add corner radius to the Pixmap |
| 64 | QRect pxRect = contactPhoto.rect(); |
| 65 | QBitmap mask(pxRect.size()); |
| 66 | QPainter customPainter(&mask); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 67 | customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 68 | customPainter.fillRect (pxRect , Qt::white ); |
| 69 | customPainter.setBackground (Qt::black ); |
| 70 | customPainter.setBrush (Qt::black ); |
| 71 | customPainter.drawRoundedRect(pxRect,radius,radius); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 72 | contactPhoto.setMask (mask ); |
| 73 | painter.drawPixmap (0,0,contactPhoto ); |
| 74 | painter.setBrush (Qt::NoBrush ); |
| 75 | painter.setPen (Qt::black ); |
| 76 | painter.setCompositionMode (QPainter::CompositionMode_SourceIn); |
| 77 | painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 78 | } |
| 79 | else { |
| 80 | pxm = drawDefaultUserPixmap(size, false, false); |
| 81 | } |
| 82 | |
| 83 | return pxm; |
| 84 | } |
| 85 | |
| 86 | QVariant |
| 87 | ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence) |
| 88 | { |
| 89 | return callPhoto(c->peerContactMethod(), size, displayPresence); |
| 90 | } |
| 91 | |
| 92 | QVariant |
| 93 | ImageManipulationDelegate::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence) |
| 94 | { |
| 95 | if (n->contact()) { |
| 96 | return contactPhoto(n->contact(), size, displayPresence); |
| 97 | } else { |
| 98 | return drawDefaultUserPixmap(size, false, false); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type) |
| 103 | { |
| 104 | QImage image; |
| 105 | //For now, ENCODING is only base64 and image type PNG or JPG |
| 106 | const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1()); |
| 107 | if (!ret) |
| 108 | qDebug() << "vCard image loading failed"; |
| 109 | |
| 110 | return QPixmap::fromImage(image); |
| 111 | } |
| 112 | |
| 113 | QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm) |
| 114 | { |
| 115 | //Preparation of our QPixmap |
| 116 | QByteArray bArray; |
| 117 | QBuffer buffer(&bArray); |
| 118 | buffer.open(QIODevice::WriteOnly); |
| 119 | |
| 120 | //PNG ? |
| 121 | (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG"); |
| 122 | buffer.close(); |
| 123 | |
| 124 | return bArray; |
| 125 | } |
| 126 | |
| 127 | QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) { |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 128 | // create the image somehow, load from file, draw into it... |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 129 | auto sourceImgRef = CGImageSourceCreateWithData((CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 130 | auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame] | 131 | auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size)); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 132 | CFRelease(sourceImgRef); |
| 133 | CFRelease(imgRef); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 134 | |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 135 | return finalpxm; |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 136 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 137 | |
| 138 | CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) { |
| 139 | // create context, keeping original image properties |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 140 | CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(), |
| 141 | CGImageGetBitsPerComponent(image), |
Alexandre Lision | d5229f3 | 2015-11-16 11:17:41 -0500 | [diff] [blame] | 142 | CGImageGetBytesPerRow(image), |
| 143 | CGImageGetColorSpace(image), |
| 144 | kCGImageAlphaPremultipliedLast); |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 145 | |
| 146 | if(context == NULL) |
| 147 | return nil; |
| 148 | |
| 149 | // draw image to context (resizing it) |
| 150 | CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image); |
| 151 | // extract resulting image from context |
| 152 | CGImageRef imgRef = CGBitmapContextCreateImage(context); |
| 153 | CGContextRelease(context); |
| 154 | |
| 155 | return imgRef; |
| 156 | } |
| 157 | |
| 158 | QVariant |
| 159 | ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent) |
| 160 | { |
| 161 | Q_UNUSED(p) |
| 162 | Q_UNUSED(size) |
| 163 | Q_UNUSED(displayPresence) |
| 164 | Q_UNUSED(isPresent) |
| 165 | return QVariant(); |
| 166 | } |
| 167 | |
| 168 | QVariant |
| 169 | ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index) |
| 170 | { |
| 171 | Q_UNUSED(index) |
| 172 | return QVariant(); |
| 173 | } |
| 174 | |
| 175 | QVariant |
| 176 | ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const |
| 177 | { |
| 178 | Q_UNUSED(interface) |
| 179 | Q_UNUSED(hint) |
| 180 | return QVariant(); |
| 181 | } |
| 182 | QVariant |
| 183 | ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const |
| 184 | { |
| 185 | Q_UNUSED(level) |
| 186 | return QVariant(); |
| 187 | } |
| 188 | QVariant |
| 189 | ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const |
| 190 | { |
| 191 | Q_UNUSED(cat) |
| 192 | return QVariant(); |
| 193 | } |
| 194 | QVariant |
| 195 | ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const |
| 196 | { |
| 197 | Q_UNUSED(cat) |
| 198 | return QVariant(); |
| 199 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 200 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 201 | QVariant |
| 202 | ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const |
| 203 | { |
| 204 | Q_UNUSED(state) |
| 205 | return QVariant(); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 206 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 207 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 208 | } // namespace Interfaces |