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