blob: 082c9416978c5461f51db358c891c8f118e50492 [file] [log] [blame]
Alexandre Lision3b0bd332015-03-15 18:43:07 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision3b0bd332015-03-15 18:43:07 -04003 * 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>
Alexandre Lision4f264622016-05-08 17:08:56 -040029#import <QHash>
Alexandre Lision3b0bd332015-03-15 18:43:07 -040030#import <QtGui/QBitmap>
31#import <QtWidgets/QApplication>
32#import <QtGui/QImage>
33#import <QtMacExtras/qmacfunctions.h>
34#import <QtGui/QPalette>
35
36//Ring
37#import <person.h>
Alexandre Lision43e91bc2016-04-19 18:04:52 -040038#import <profilemodel.h>
39#import <profile.h>
Alexandre Lision3b0bd332015-03-15 18:43:07 -040040#import <contactmethod.h>
Alexandre Lision3b0bd332015-03-15 18:43:07 -040041
Alexandre Lision7a166e42015-09-02 15:04:43 -040042namespace Interfaces {
Alexandre Lision3b0bd332015-03-15 18:43:07 -040043
Alexandre Lision4f264622016-05-08 17:08:56 -040044 ImageManipulationDelegate::ImageManipulationDelegate() {}
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
Alexandre Lisionafa56dc2016-05-08 17:30:20 -040049 auto index = QStringLiteral("%1%2%3").arg(size.width())
50 .arg(size.height())
51 .arg(QString::fromUtf8(c->uid()));
52 if (m_hContactsPixmap.contains(index)) {
53 return m_hContactsPixmap.value(index);
54 }
55
Alexandre Lision7a166e42015-09-02 15:04:43 -040056 QPixmap pxm;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040057 if (c && c->photo().isValid()) {
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040058 QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding,
59 Qt::SmoothTransformation));
60
61 QPixmap finalImg;
62 if (contactPhoto.size() != size) {
63 finalImg = crop(contactPhoto, size);
64 } else
65 finalImg = contactPhoto;
66
Alexandre Lision7a166e42015-09-02 15:04:43 -040067 pxm = QPixmap(size);
68 pxm.fill(Qt::transparent);
69 QPainter painter(&pxm);
70
71 //Clear the pixmap
Alexandre Lisionde0314b2015-09-02 15:45:21 -040072 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040073 painter.setCompositionMode(QPainter::CompositionMode_Clear);
74 painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white));
75 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
76
77 //Add corner radius to the Pixmap
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040078 QRect pxRect = finalImg.rect();
Alexandre Lision7a166e42015-09-02 15:04:43 -040079 QBitmap mask(pxRect.size());
80 QPainter customPainter(&mask);
Alexandre Lisionde0314b2015-09-02 15:45:21 -040081 customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040082 customPainter.fillRect (pxRect , Qt::white );
83 customPainter.setBackground (Qt::black );
84 customPainter.setBrush (Qt::black );
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040085 customPainter.drawRoundedRect(pxRect,radius,radius );
86 finalImg.setMask (mask );
87 painter.drawPixmap (0,0,finalImg );
Alexandre Lisionde0314b2015-09-02 15:45:21 -040088 painter.setBrush (Qt::NoBrush );
89 painter.setPen (Qt::black );
90 painter.setCompositionMode (QPainter::CompositionMode_SourceIn);
91 painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius);
Alexandre Lision7a166e42015-09-02 15:04:43 -040092 }
93 else {
Alexandre Lision43e91bc2016-04-19 18:04:52 -040094 pxm = drawDefaultUserPixmap(size);
Alexandre Lision7a166e42015-09-02 15:04:43 -040095 }
96
Alexandre Lisionafa56dc2016-05-08 17:30:20 -040097 m_hContactsPixmap.insert(index, pxm);
98
Alexandre Lision7a166e42015-09-02 15:04:43 -040099 return pxm;
100 }
101
Alexandre Lisionf02a32b2016-04-19 15:01:07 -0400102 QPixmap
103 ImageManipulationDelegate::crop(QPixmap& photo, const QSize& destSize)
104 {
105 auto initSize = photo.size();
106 float leftDelta = 0;
107 float topDelta = 0;
108
109 if (destSize.height() == initSize.height()) {
110 leftDelta = (destSize.width() - initSize.width()) / 2;
111 } else {
112 topDelta = (destSize.height() - initSize.height()) / 2;
113 }
114
115 float xScale = (float)destSize.width() / initSize.width();
116 float yScale = (float)destSize.height() / initSize.height();
117
118 QRectF destRect(leftDelta, topDelta,
119 initSize.width() - leftDelta, initSize.height() - topDelta);
120
121 destRect.setLeft(leftDelta * xScale);
122 destRect.setTop(topDelta * yScale);
123
124 destRect.setWidth((initSize.width() - leftDelta) * xScale);
125 destRect.setHeight((initSize.height() - topDelta) * yScale);
126
127 return photo.copy(destRect.toRect());
128 }
129
Alexandre Lision7a166e42015-09-02 15:04:43 -0400130 QVariant
131 ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence)
132 {
133 return callPhoto(c->peerContactMethod(), size, displayPresence);
134 }
135
136 QVariant
137 ImageManipulationDelegate::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence)
138 {
139 if (n->contact()) {
140 return contactPhoto(n->contact(), size, displayPresence);
141 } else {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400142 return drawDefaultUserPixmap(size);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400143 }
144 }
145
146 QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type)
147 {
148 QImage image;
149 //For now, ENCODING is only base64 and image type PNG or JPG
150 const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1());
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400151 if (!ret) {
Alexandre Lision7a166e42015-09-02 15:04:43 -0400152 qDebug() << "vCard image loading failed";
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400153 return drawDefaultUserPixmap(decorationSize);
154 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400155
156 return QPixmap::fromImage(image);
157 }
158
159 QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm)
160 {
161 //Preparation of our QPixmap
162 QByteArray bArray;
163 QBuffer buffer(&bArray);
164 buffer.open(QIODevice::WriteOnly);
165
166 //PNG ?
167 (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG");
168 buffer.close();
169
170 return bArray;
171 }
172
173 QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) {
Alexandre Lision4f264622016-05-08 17:08:56 -0400174
175 auto index = QStringLiteral("%1%2").arg(size.width()).arg(size.height());
176 if (m_hDefaultUserPixmap.contains(index)) {
177 return m_hDefaultUserPixmap.value(index);
178 }
179
Alexandre Lision7a166e42015-09-02 15:04:43 -0400180 // create the image somehow, load from file, draw into it...
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500181 auto sourceImgRef = CGImageSourceCreateWithData((__bridge CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400182 auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL);
Alexandre Lisionde0314b2015-09-02 15:45:21 -0400183 auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size));
Alexandre Lision7a166e42015-09-02 15:04:43 -0400184 CFRelease(sourceImgRef);
185 CFRelease(imgRef);
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400186
Alexandre Lision4f264622016-05-08 17:08:56 -0400187 m_hDefaultUserPixmap.insert(index, finalpxm);
188
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500189 return finalpxm;
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400190 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400191
192 CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) {
193 // create context, keeping original image properties
Alexandre Lision7a166e42015-09-02 15:04:43 -0400194 CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(),
195 CGImageGetBitsPerComponent(image),
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500196 CGImageGetBytesPerRow(image),
197 CGImageGetColorSpace(image),
198 kCGImageAlphaPremultipliedLast);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400199
200 if(context == NULL)
201 return nil;
202
203 // draw image to context (resizing it)
204 CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image);
205 // extract resulting image from context
206 CGImageRef imgRef = CGBitmapContextCreateImage(context);
207 CGContextRelease(context);
208
209 return imgRef;
210 }
211
212 QVariant
213 ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent)
214 {
215 Q_UNUSED(p)
216 Q_UNUSED(size)
217 Q_UNUSED(displayPresence)
218 Q_UNUSED(isPresent)
219 return QVariant();
220 }
221
222 QVariant
223 ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index)
224 {
225 Q_UNUSED(index)
226 return QVariant();
227 }
228
229 QVariant
230 ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const
231 {
232 Q_UNUSED(interface)
233 Q_UNUSED(hint)
234 return QVariant();
235 }
236 QVariant
237 ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const
238 {
239 Q_UNUSED(level)
240 return QVariant();
241 }
242 QVariant
243 ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const
244 {
245 Q_UNUSED(cat)
246 return QVariant();
247 }
248 QVariant
249 ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const
250 {
251 Q_UNUSED(cat)
252 return QVariant();
253 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400254
Alexandre Lision7a166e42015-09-02 15:04:43 -0400255 QVariant
256 ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const
257 {
258 Q_UNUSED(state)
259 return QVariant();
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400260 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400261
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500262 QVariant ImageManipulationDelegate::decorationRole(const QModelIndex& index)
263 {
264 Q_UNUSED(index)
265 return QVariant();
266 }
267
268 QVariant ImageManipulationDelegate::decorationRole(const Call* c)
269 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400270 if (c && c->peerContactMethod()
271 && c->peerContactMethod()->contact()) {
272 return contactPhoto(c->peerContactMethod()->contact(), decorationSize);
273 } else
274 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500275 }
276
277 QVariant ImageManipulationDelegate::decorationRole(const ContactMethod* cm)
278 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400279 QImage photo;
280 if (cm && cm->contact() && cm->contact()->photo().isValid())
281 return contactPhoto(cm->contact(), decorationSize);
282 else
283 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500284 }
285
286 QVariant ImageManipulationDelegate::decorationRole(const Person* p)
287 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400288 return contactPhoto(const_cast<Person*>(p), decorationSize);
289 }
290
291 QVariant ImageManipulationDelegate::decorationRole(const Account* acc)
292 {
293 Q_UNUSED(acc)
294 if (auto pro = ProfileModel::instance().selectedProfile())
295 return contactPhoto(pro->person(), decorationSize);
296 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500297 }
298
Alexandre Lision7a166e42015-09-02 15:04:43 -0400299} // namespace Interfaces