blob: 4a73edb7638967d102c5a14a7a6db840d59536fc [file] [log] [blame]
Alexandre Lision3b0bd332015-03-15 18:43:07 -04001/*
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 Lision3b0bd332015-03-15 18:43:07 -040018 */
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 Lision3b0bd332015-03-15 18:43:07 -040038
Alexandre Lision7a166e42015-09-02 15:04:43 -040039namespace Interfaces {
Alexandre Lision3b0bd332015-03-15 18:43:07 -040040
Alexandre Lision7a166e42015-09-02 15:04:43 -040041 ImageManipulationDelegate::ImageManipulationDelegate()
42 {
Alexandre Lision3b0bd332015-03-15 18:43:07 -040043
Alexandre Lision7a166e42015-09-02 15:04:43 -040044 }
Alexandre Lision3b0bd332015-03-15 18:43:07 -040045
Alexandre Lision7a166e42015-09-02 15:04:43 -040046 QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) {
Alexandre Lisionde0314b2015-09-02 15:45:21 -040047 const int radius = size.height() / 2;
Alexandre Lision7a166e42015-09-02 15:04:43 -040048
49 QPixmap pxm;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040050 if (c && c->photo().isValid()) {
Alexandre Lisionde0314b2015-09-02 15:45:21 -040051 QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaledToWidth(size.height(),
52 Qt::TransformationMode::SmoothTransformation));
Alexandre Lision7a166e42015-09-02 15:04:43 -040053 pxm = QPixmap(size);
54 pxm.fill(Qt::transparent);
55 QPainter painter(&pxm);
56
57 //Clear the pixmap
Alexandre Lisionde0314b2015-09-02 15:45:21 -040058 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040059 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 Lisionde0314b2015-09-02 15:45:21 -040067 customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040068 customPainter.fillRect (pxRect , Qt::white );
69 customPainter.setBackground (Qt::black );
70 customPainter.setBrush (Qt::black );
71 customPainter.drawRoundedRect(pxRect,radius,radius);
Alexandre Lisionde0314b2015-09-02 15:45:21 -040072 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 Lision7a166e42015-09-02 15:04:43 -040078 }
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 Lisionde0314b2015-09-02 15:45:21 -0400129 const int radius = size.height() / 2;
Alexandre Lision7a166e42015-09-02 15:04:43 -0400130 QPixmap pxm(size);
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400131 pxm.fill(Qt::transparent);
132 QPainter painter(&pxm);
133
Alexandre Lisionde0314b2015-09-02 15:45:21 -0400134 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 Lision7a166e42015-09-02 15:04:43 -0400138 // 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 Lisionde0314b2015-09-02 15:45:21 -0400141 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 Lision3b0bd332015-03-15 18:43:07 -0400158
Alexandre Lision7a166e42015-09-02 15:04:43 -0400159 CFRelease(sourceImgRef);
160 CFRelease(imgRef);
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400161
Alexandre Lision7a166e42015-09-02 15:04:43 -0400162 return pxm;
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400163 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400164
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 Lision4dfcafc2015-08-20 12:43:23 -0400229
Alexandre Lision7a166e42015-09-02 15:04:43 -0400230 QVariant
231 ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const
232 {
233 Q_UNUSED(state)
234 return QVariant();
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400235 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400236
Alexandre Lision7a166e42015-09-02 15:04:43 -0400237} // namespace Interfaces