blob: 491a2429ce36581c482987886ac4b3efb2b0b79a [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) {
Alexandre Lision7a166e42015-09-02 15:04:43 -0400128 // create the image somehow, load from file, draw into it...
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500129 auto sourceImgRef = CGImageSourceCreateWithData((CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400130 auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL);
Alexandre Lisionde0314b2015-09-02 15:45:21 -0400131 auto finalpxm = QtMac::fromCGImageRef(resizeCGImage(imgRef, size));
Alexandre Lision7a166e42015-09-02 15:04:43 -0400132 CFRelease(sourceImgRef);
133 CFRelease(imgRef);
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400134
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500135 return finalpxm;
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400136 }
Alexandre Lision7a166e42015-09-02 15:04:43 -0400137
138 CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) {
139 // create context, keeping original image properties
Alexandre Lision7a166e42015-09-02 15:04:43 -0400140 CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(),
141 CGImageGetBitsPerComponent(image),
Alexandre Lisiond5229f32015-11-16 11:17:41 -0500142 CGImageGetBytesPerRow(image),
143 CGImageGetColorSpace(image),
144 kCGImageAlphaPremultipliedLast);
Alexandre Lision7a166e42015-09-02 15:04:43 -0400145
146 if(context == NULL)
147 return nil;
148
149 // draw image to context (resizing it)
150 CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image);
151 // extract resulting image from context
152 CGImageRef imgRef = CGBitmapContextCreateImage(context);
153 CGContextRelease(context);
154
155 return imgRef;
156 }
157
158 QVariant
159 ImageManipulationDelegate::numberCategoryIcon(const QVariant& p, const QSize& size, bool displayPresence, bool isPresent)
160 {
161 Q_UNUSED(p)
162 Q_UNUSED(size)
163 Q_UNUSED(displayPresence)
164 Q_UNUSED(isPresent)
165 return QVariant();
166 }
167
168 QVariant
169 ImageManipulationDelegate::securityIssueIcon(const QModelIndex& index)
170 {
171 Q_UNUSED(index)
172 return QVariant();
173 }
174
175 QVariant
176 ImageManipulationDelegate::collectionIcon(const CollectionInterface* interface, PixmapManipulatorI::CollectionIconHint hint) const
177 {
178 Q_UNUSED(interface)
179 Q_UNUSED(hint)
180 return QVariant();
181 }
182 QVariant
183 ImageManipulationDelegate::securityLevelIcon(const SecurityEvaluationModel::SecurityLevel level) const
184 {
185 Q_UNUSED(level)
186 return QVariant();
187 }
188 QVariant
189 ImageManipulationDelegate::historySortingCategoryIcon(const CategorizedHistoryModel::SortedProxy::Categories cat) const
190 {
191 Q_UNUSED(cat)
192 return QVariant();
193 }
194 QVariant
195 ImageManipulationDelegate::contactSortingCategoryIcon(const CategorizedContactModel::SortedProxy::Categories cat) const
196 {
197 Q_UNUSED(cat)
198 return QVariant();
199 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400200
Alexandre Lision7a166e42015-09-02 15:04:43 -0400201 QVariant
202 ImageManipulationDelegate::userActionIcon(const UserActionElement& state) const
203 {
204 Q_UNUSED(state)
205 return QVariant();
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400206 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400207
Alexandre Lision7a166e42015-09-02 15:04:43 -0400208} // namespace Interfaces