Fixed FacebookLoginBrowser::destroyed() signal handling
[situare] / src / ui / avatarimage.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jukka Saastamoinen - jukka.saastamoinen@ixonos.com
6        Jussi Laitinen - jussi.laitinen@ixonos.com
7        Ville Tiensuu - ville.tiensuu@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include "avatarimage.h"
25 #include <QPainter>
26 #include <QDebug>
27
28 // constants for normal images
29 const int IMAGE_WIDTH = 64;     ///< Created image width
30 const int IMAGE_HEIGHT = 64;    ///< Created image height
31 const int ORIGINAL_IMAGE_X = 7; ///< Original image x position
32 const int ORIGINAL_IMAGE_Y = 7; ///< Original image y position
33 const int ROUNDNESS = 9;        ///< Image roundness
34 const int CLIP_X_OFFSET = 1;    ///< Clip
35
36 // constants for large images
37 const int BORDER_HEIGHT = 118; ///< Height of large profile image border
38 const int BORDER_OFFSET = 9; ///< Transparent area in border graphics edges
39 const int IMAGE_MAX_WIDTH = 220; ///< Mamimum allowed width for profile image
40 const int LEFT_BORDER_WIDTH = 20; ///< Width of large profile image left border
41 const int RIGHT_BORDER_WIDHT = 20; ///< Width of large profile image right border
42
43 QPixmap AvatarImage::create(const QPixmap &image, ImageHeight height)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     if (height == Small) {
48         QPixmap avatarImage = QPixmap(IMAGE_WIDTH, IMAGE_HEIGHT);
49         avatarImage.fill(Qt::transparent);
50         QPainter painter(&avatarImage);
51
52         QRect imageRect = QRect(0, 0, image.width(), image.height());
53
54         QPainterPath roundedRect;
55         roundedRect.addRoundedRect(ORIGINAL_IMAGE_X-1, ORIGINAL_IMAGE_Y-1, imageRect.width()+1,
56                                    imageRect.height()+1,ROUNDNESS,ROUNDNESS);
57         painter.save();
58         painter.setClipPath(roundedRect);
59         painter.drawPixmap(QPointF(ORIGINAL_IMAGE_X, ORIGINAL_IMAGE_Y), image);
60         painter.restore();
61         painter.drawPixmap(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
62                            QPixmap(":/res/images/profile_pic_border.png"));
63
64         return avatarImage;
65     } else if (height == Large) {
66         qDebug() << "Avatarimage original size: " << image.width() << image.height();
67
68         QPixmap scaledProfileImage = image.scaledToHeight(height,
69                                                           Qt::SmoothTransformation);
70
71         qDebug() << "Avatarimage scaled size:  "
72                    << scaledProfileImage.width() << scaledProfileImage.height();
73
74         //  unsupported image size ratios are drawn without borders.
75         if (scaledProfileImage.width() + 2*BORDER_OFFSET > IMAGE_MAX_WIDTH)
76             return image;
77
78         else if (scaledProfileImage.width() < 2*LEFT_BORDER_WIDTH)
79             return image;
80
81         else {
82
83             QPixmap avatarImage = QPixmap(scaledProfileImage.width()+BORDER_OFFSET*2,
84                                           BORDER_HEIGHT);
85             avatarImage.fill(Qt::transparent);
86             QPainter painter(&avatarImage);
87
88             QPainterPath roundedRect;
89             roundedRect.addRoundedRect(BORDER_OFFSET, BORDER_OFFSET, scaledProfileImage.width(),
90                                        height, ROUNDNESS, ROUNDNESS);
91             painter.save();
92             painter.setClipPath(roundedRect);
93             painter.drawPixmap(QPointF(BORDER_OFFSET, BORDER_OFFSET), scaledProfileImage);
94             painter.restore();
95
96             QRect leftFrameRectangle(0,0, LEFT_BORDER_WIDTH, BORDER_HEIGHT);
97             QRect middleFrameRectangle(LEFT_BORDER_WIDTH, 0,
98                                        scaledProfileImage.width() - LEFT_BORDER_WIDTH
99                                        - RIGHT_BORDER_WIDHT + 2*BORDER_OFFSET, BORDER_HEIGHT);
100             QRect rightFrameRectangle(LEFT_BORDER_WIDTH + middleFrameRectangle.width(), 0,
101                                       RIGHT_BORDER_WIDHT, BORDER_HEIGHT);
102
103             painter.drawPixmap(leftFrameRectangle,
104                                QPixmap(":/res/images/large_profile_pic_border_left.png"));
105             painter.drawPixmap(middleFrameRectangle,
106                                QPixmap(":/res/images/large_profile_pic_border_middle.png"));
107             painter.drawPixmap(rightFrameRectangle,
108                                QPixmap(":/res/images/large_profile_pic_border_right.png"));
109
110             return avatarImage;
111         }
112     }
113
114     return image;
115 }