blob: 485c36ece8e0c3bae62f79395840f8e74339ea8d [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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall import the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30#import "ImageManipulationDelegate.h"
31
32#import <Cocoa/Cocoa.h>
33#import <Foundation/Foundation.h>
34
35//Qt
36#import <QSize>
37#import <QBuffer>
38#import <QtGui/QColor>
39#import <QtGui/QPainter>
40#import <QtGui/QBitmap>
41#import <QtWidgets/QApplication>
42#import <QtGui/QImage>
43#import <QtMacExtras/qmacfunctions.h>
44#import <QtGui/QPalette>
45
46//Ring
47#import <person.h>
48#import <contactmethod.h>
49#import <presencestatusmodel.h>
Alexandre Lision3b0bd332015-03-15 18:43:07 -040050#import <collectioninterface.h>
51#import <useractionmodel.h>
52#import <QStandardPaths>
53
54ImageManipulationDelegate::ImageManipulationDelegate() : PixmapManipulationDelegate()
55{
56
57}
58
59QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) {
60 const int radius = (size.height() > 35) ? 7 : 5;
61
62 QPixmap pxm;
63 if (c->photo().isValid()) {
64 QPixmap contactPhoto((qvariant_cast<QPixmap>(c->photo())).scaledToWidth(size.height()-6));
65 pxm = QPixmap(size);
66 pxm.fill(Qt::transparent);
67 QPainter painter(&pxm);
68
69 //Clear the pixmap
70 painter.setCompositionMode(QPainter::CompositionMode_Clear);
71 painter.fillRect(0,0,size.width(),size.height(),QBrush(Qt::white));
72 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
73
74 //Add corner radius to the Pixmap
75 QRect pxRect = contactPhoto.rect();
76 QBitmap mask(pxRect.size());
77 QPainter customPainter(&mask);
78 customPainter.setRenderHint (QPainter::Antialiasing, true );
79 customPainter.fillRect (pxRect , Qt::white );
80 customPainter.setBackground (Qt::black );
81 customPainter.setBrush (Qt::black );
82 customPainter.drawRoundedRect(pxRect,radius,radius);
83 contactPhoto.setMask(mask);
84 painter.drawPixmap(3,3,contactPhoto);
85 painter.setBrush(Qt::NoBrush);
86 painter.setPen(Qt::white);
87 painter.setRenderHint (QPainter::Antialiasing, true );
88 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
89 painter.drawRoundedRect(3,3,pxm.height()-6,pxm.height()-6,radius,radius);
90 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
91
92 }
93 else {
94 pxm = drawDefaultUserPixmap(size, false, false);
95 }
96
97 return pxm;
98}
99
100QVariant ImageManipulationDelegate::personPhoto(const QByteArray& data, const QString& type)
101{
102 QImage image;
103 //For now, ENCODING is only base64 and image type PNG or JPG
104 const bool ret = image.loadFromData(QByteArray::fromBase64(data),type.toLatin1());
105 if (!ret)
106 qDebug() << "vCard image loading failed";
107
108 return QPixmap::fromImage(image);
109}
110
111QByteArray ImageManipulationDelegate::toByteArray(const QVariant& pxm)
112{
113 //Preparation of our QPixmap
114 QByteArray bArray;
115 QBuffer buffer(&bArray);
116 buffer.open(QIODevice::WriteOnly);
117
118 //PNG ?
119 (qvariant_cast<QPixmap>(pxm)).save(&buffer, "PNG");
120 buffer.close();
121
122 return bArray;
123}
124
125QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) {
126
127 QPixmap pxm(size);
128 pxm.fill(Qt::transparent);
129 QPainter painter(&pxm);
130
131 // create the image somehow, load from file, draw into it...
132 CGImageSourceRef source;
133
134 source = CGImageSourceCreateWithData((CFDataRef)[[NSImage imageNamed:@"NSUser"] TIFFRepresentation], NULL);
135 CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
136 painter.drawPixmap(3,3,QtMac::fromCGImageRef(resizeCGImage(maskRef, size)));
137
138 return pxm;
139}
140
141CGImageRef ImageManipulationDelegate::resizeCGImage(CGImageRef image, const QSize& size) {
142 // create context, keeping original image properties
143 CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
144 CGContextRef context = CGBitmapContextCreate(NULL, size.width(), size.height(),
145 CGImageGetBitsPerComponent(image),
146 CGImageGetBytesPerRow(image),
147 colorspace,
148 CGImageGetAlphaInfo(image));
149 CGColorSpaceRelease(colorspace);
150
151
152 if(context == NULL)
153 return nil;
154
155 // draw image to context (resizing it)
156 CGContextDrawImage(context, CGRectMake(0, 0, size.width(), size.height()), image);
157 // extract resulting image from context
158 CGImageRef imgRef = CGBitmapContextCreateImage(context);
159 CGContextRelease(context);
160
161 return imgRef;
162}