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) { |
| 128 | |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame^] | 129 | const int radius = size.height() / 2; |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 130 | QPixmap pxm(size); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 131 | pxm.fill(Qt::transparent); |
| 132 | QPainter painter(&pxm); |
| 133 | |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame^] | 134 | painter.setCompositionMode(QPainter::CompositionMode_Clear); |
| 135 | painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white)); |
| 136 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 137 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 138 | // create the image somehow, load from file, draw into it... |
| 139 | auto sourceImgRef = CGImageSourceCreateWithData((CFDataRef)[[NSImage imageNamed:@"NSUser"] TIFFRepresentation], NULL); |
| 140 | auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL); |
Alexandre Lision | de0314b | 2015-09-02 15:45:21 -0400 | [diff] [blame^] | 141 | auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size)); |
| 142 | |
| 143 | QRect pxRect = finalpxm.rect(); |
| 144 | QBitmap mask(pxRect.size()); |
| 145 | QPainter customPainter(&mask); |
| 146 | customPainter.setRenderHint (QPainter::Antialiasing, true ); |
| 147 | customPainter.fillRect (pxRect , Qt::white ); |
| 148 | customPainter.setBackground (Qt::black ); |
| 149 | customPainter.setBrush (Qt::black ); |
| 150 | customPainter.drawRoundedRect(pxRect,radius,radius); |
| 151 | finalpxm.setMask(mask); |
| 152 | painter.setRenderHint (QPainter::Antialiasing, true ); |
| 153 | painter.drawPixmap(0,0,finalpxm); |
| 154 | painter.setBrush(Qt::NoBrush); |
| 155 | painter.setPen(Qt::black); |
| 156 | painter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 157 | painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 158 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 159 | CFRelease(sourceImgRef); |
| 160 | CFRelease(imgRef); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 161 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 162 | return pxm; |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 163 | } |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 164 | |
| 165 | CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) { |
| 166 | // create context, keeping original image properties |
| 167 | CGColorSpaceRef colorspace = CGImageGetColorSpace(image); |
| 168 | |
| 169 | CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(), |
| 170 | CGImageGetBitsPerComponent(image), |
| 171 | size.width() * CGImageGetBitsPerComponent(image), |
| 172 | colorspace, |
| 173 | CGImageGetAlphaInfo(image)); |
| 174 | |
| 175 | if(context == NULL) |
| 176 | return nil; |
| 177 | |
| 178 | // draw image to context (resizing it) |
| 179 | CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image); |
| 180 | // extract resulting image from context |
| 181 | CGImageRef imgRef = CGBitmapContextCreateImage(context); |
| 182 | CGContextRelease(context); |
| 183 | |
| 184 | return imgRef; |
| 185 | } |
| 186 | |
| 187 | QVariant |
| 188 | ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent) |
| 189 | { |
| 190 | Q_UNUSED(p) |
| 191 | Q_UNUSED(size) |
| 192 | Q_UNUSED(displayPresence) |
| 193 | Q_UNUSED(isPresent) |
| 194 | return QVariant(); |
| 195 | } |
| 196 | |
| 197 | QVariant |
| 198 | ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index) |
| 199 | { |
| 200 | Q_UNUSED(index) |
| 201 | return QVariant(); |
| 202 | } |
| 203 | |
| 204 | QVariant |
| 205 | ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const |
| 206 | { |
| 207 | Q_UNUSED(interface) |
| 208 | Q_UNUSED(hint) |
| 209 | return QVariant(); |
| 210 | } |
| 211 | QVariant |
| 212 | ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const |
| 213 | { |
| 214 | Q_UNUSED(level) |
| 215 | return QVariant(); |
| 216 | } |
| 217 | QVariant |
| 218 | ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const |
| 219 | { |
| 220 | Q_UNUSED(cat) |
| 221 | return QVariant(); |
| 222 | } |
| 223 | QVariant |
| 224 | ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const |
| 225 | { |
| 226 | Q_UNUSED(cat) |
| 227 | return QVariant(); |
| 228 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 229 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 230 | QVariant |
| 231 | ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const |
| 232 | { |
| 233 | Q_UNUSED(state) |
| 234 | return QVariant(); |
Alexandre Lision | 3b0bd33 | 2015-03-15 18:43:07 -0400 | [diff] [blame] | 235 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 236 | |
Alexandre Lision | 7a166e4 | 2015-09-02 15:04:43 -0400 | [diff] [blame] | 237 | } // namespace Interfaces |