blob: 4984d40e194eee09c8e02ddec2d2d78dcf5d8512 [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 QPixmap pxm;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040049 if (c && c->photo().isValid()) {
Alexandre Lision196545b2016-05-13 17:05:13 -040050 // Check cache
51 auto index = QStringLiteral("%1%2%3").arg(size.width())
52 .arg(size.height())
53 .arg(QString::fromUtf8(c->uid()));
54
55 if (m_hContactsPixmap.contains(index)) {
56 return m_hContactsPixmap.value(index).second;
57 }
58
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040059 QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding,
60 Qt::SmoothTransformation));
61
62 QPixmap finalImg;
63 if (contactPhoto.size() != size) {
64 finalImg = crop(contactPhoto, size);
65 } else
66 finalImg = contactPhoto;
67
Alexandre Lision7a166e42015-09-02 15:04:43 -040068 pxm = QPixmap(size);
69 pxm.fill(Qt::transparent);
70 QPainter painter(&pxm);
71
72 //Clear the pixmap
Alexandre Lisionde0314b2015-09-02 15:45:21 -040073 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040074 painter.setCompositionMode(QPainter::CompositionMode_Clear);
75 painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white));
76 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
77
78 //Add corner radius to the Pixmap
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040079 QRect pxRect = finalImg.rect();
Alexandre Lision7a166e42015-09-02 15:04:43 -040080 QBitmap mask(pxRect.size());
81 QPainter customPainter(&mask);
Alexandre Lisionde0314b2015-09-02 15:45:21 -040082 customPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
Alexandre Lision7a166e42015-09-02 15:04:43 -040083 customPainter.fillRect (pxRect , Qt::white );
84 customPainter.setBackground (Qt::black );
85 customPainter.setBrush (Qt::black );
Alexandre Lisionf02a32b2016-04-19 15:01:07 -040086 customPainter.drawRoundedRect(pxRect,radius,radius );
87 finalImg.setMask (mask );
88 painter.drawPixmap (0,0,finalImg );
Alexandre Lisionde0314b2015-09-02 15:45:21 -040089 painter.setBrush (Qt::NoBrush );
90 painter.setPen (Qt::black );
91 painter.setCompositionMode (QPainter::CompositionMode_SourceIn);
92 painter.drawRoundedRect(0,0,pxm.height(),pxm.height(),radius,radius);
Alexandre Lision7a166e42015-09-02 15:04:43 -040093
Alexandre Lision196545b2016-05-13 17:05:13 -040094 // Save in cache
95 QPair<QMetaObject::Connection, QPixmap> toInsert;
96 toInsert.first = QObject::connect(c,
97 &Person::changed,
98 [=]() {
99 if (c) {
100 auto index = QStringLiteral("%1%2%3").arg(size.width())
101 .arg(size.height())
102 .arg(QString::fromUtf8(c->uid()));
103 if (m_hContactsPixmap.contains(index)) {
104 QObject::disconnect(m_hContactsPixmap.value(index).first);
105 m_hContactsPixmap.remove(index);
106 }
107 }
108 });
109 toInsert.second = pxm;
110 m_hContactsPixmap.insert(index, toInsert);
111
112 } else {
113 return drawDefaultUserPixmap(size);
114 }
Alexandre Lisionafa56dc2016-05-08 17:30:20 -0400115
Alexandre Lision7a166e42015-09-02 15:04:43 -0400116 return pxm;
117 }
118
Alexandre Lisionf02a32b2016-04-19 15:01:07 -0400119 QPixmap
120 ImageManipulationDelegate::crop(QPixmap& photo, const QSize& destSize)
121 {
122 auto initSize = photo.size();
123 float leftDelta = 0;
124 float topDelta = 0;
125
126 if (destSize.height() == initSize.height()) {
127 leftDelta = (destSize.width() - initSize.width()) / 2;
128 } else {
129 topDelta = (destSize.height() - initSize.height()) / 2;
130 }
131
132 float xScale = (float)destSize.width() / initSize.width();
133 float yScale = (float)destSize.height() / initSize.height();
134
135 QRectF destRect(leftDelta, topDelta,
136 initSize.width() - leftDelta, initSize.height() - topDelta);
137
138 destRect.setLeft(leftDelta * xScale);
139 destRect.setTop(topDelta * yScale);
140
141 destRect.setWidth((initSize.width() - leftDelta) * xScale);
142 destRect.setHeight((initSize.height() - topDelta) * yScale);
143
144 return photo.copy(destRect.toRect());
145 }
146
Alexandre Lision7a166e42015-09-02 15:04:43 -0400147 QVariant
148 ImageManipulationDelegate::callPhoto(Call* c, const QSize& size, bool displayPresence)
149 {
150 return callPhoto(c->peerContactMethod(), size, displayPresence);
151 }
152
153 QVariant
154 ImageManipulationDelegate::callPhoto(const ContactMethod* n, const QSize& size, bool displayPresence)
155 {
156 if (n->contact()) {
157 return contactPhoto(n->contact(), size, displayPresence);
158 } else {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400159 return drawDefaultUserPixmap(size);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400160 }
161 }
162
163 QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type)
164 {
165 QImage image;
166 //For now, ENCODING is only base64 and image type PNG or JPG
167 const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1());
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400168 if (!ret) {
Alexandre Lision7a166e42015-09-02 15:04:43 -0400169 qDebug() << "vCard image loading failed";
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400170 return drawDefaultUserPixmap(decorationSize);
171 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400172
173 return QPixmap::fromImage(image);
174 }
175
176 QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm)
177 {
178 //Preparation of our QPixmap
179 QByteArray bArray;
180 QBuffer buffer(&bArray);
181 buffer.open(QIODevice::WriteOnly);
182
183 //PNG ?
184 (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG");
185 buffer.close();
186
187 return bArray;
188 }
189
190 QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) {
Alexandre Lision4f264622016-05-08 17:08:56 -0400191
192 auto index = QStringLiteral("%1%2").arg(size.width()).arg(size.height());
193 if (m_hDefaultUserPixmap.contains(index)) {
194 return m_hDefaultUserPixmap.value(index);
195 }
196
Alexandre Lision7a166e42015-09-02 15:04:43 -0400197 // create the image somehow, load from file, draw into it...
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500198 auto sourceImgRef = CGImageSourceCreateWithData((__bridge CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400199 auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL);
Alexandre Lisionde0314b2015-09-02 15:45:21 -0400200 auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size));
Alexandre Lision7a166e42015-09-02 15:04:43 -0400201 CFRelease(sourceImgRef);
202 CFRelease(imgRef);
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400203
Alexandre Lision4f264622016-05-08 17:08:56 -0400204 m_hDefaultUserPixmap.insert(index, finalpxm);
205
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500206 return finalpxm;
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400207 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400208
209 CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) {
210 // create context, keeping original image properties
Alexandre Lision7a166e42015-09-02 15:04:43 -0400211 CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(),
212 CGImageGetBitsPerComponent(image),
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500213 CGImageGetBytesPerRow(image),
214 CGImageGetColorSpace(image),
215 kCGImageAlphaPremultipliedLast);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400216
217 if(context == NULL)
218 return nil;
219
220 // draw image to context (resizing it)
221 CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image);
222 // extract resulting image from context
223 CGImageRef imgRef = CGBitmapContextCreateImage(context);
224 CGContextRelease(context);
225
226 return imgRef;
227 }
228
229 QVariant
230 ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent)
231 {
232 Q_UNUSED(p)
233 Q_UNUSED(size)
234 Q_UNUSED(displayPresence)
235 Q_UNUSED(isPresent)
236 return QVariant();
237 }
238
239 QVariant
240 ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index)
241 {
242 Q_UNUSED(index)
243 return QVariant();
244 }
245
246 QVariant
247 ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const
248 {
249 Q_UNUSED(interface)
250 Q_UNUSED(hint)
251 return QVariant();
252 }
253 QVariant
254 ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const
255 {
256 Q_UNUSED(level)
257 return QVariant();
258 }
259 QVariant
260 ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const
261 {
262 Q_UNUSED(cat)
263 return QVariant();
264 }
265 QVariant
266 ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const
267 {
268 Q_UNUSED(cat)
269 return QVariant();
270 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400271
Alexandre Lision7a166e42015-09-02 15:04:43 -0400272 QVariant
273 ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const
274 {
275 Q_UNUSED(state)
276 return QVariant();
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400277 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400278
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500279 QVariant ImageManipulationDelegate::decorationRole(const QModelIndex& index)
280 {
281 Q_UNUSED(index)
282 return QVariant();
283 }
284
285 QVariant ImageManipulationDelegate::decorationRole(const Call* c)
286 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400287 if (c && c->peerContactMethod()
288 && c->peerContactMethod()->contact()) {
289 return contactPhoto(c->peerContactMethod()->contact(), decorationSize);
290 } else
291 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500292 }
293
294 QVariant ImageManipulationDelegate::decorationRole(const ContactMethod* cm)
295 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400296 QImage photo;
297 if (cm && cm->contact() && cm->contact()->photo().isValid())
298 return contactPhoto(cm->contact(), decorationSize);
299 else
300 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500301 }
302
303 QVariant ImageManipulationDelegate::decorationRole(const Person* p)
304 {
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400305 return contactPhoto(const_cast<Person*>(p), decorationSize);
306 }
307
308 QVariant ImageManipulationDelegate::decorationRole(const Account* acc)
309 {
310 Q_UNUSED(acc)
311 if (auto pro = ProfileModel::instance().selectedProfile())
312 return contactPhoto(pro->person(), decorationSize);
313 return drawDefaultUserPixmap(decorationSize);
Stepan Salenikovich3a6ea302016-01-08 13:54:34 -0500314 }
315
Alexandre Lision7a166e42015-09-02 15:04:43 -0400316} // namespace Interfaces