cleaning
[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
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include "avatarimage.h"
24 #include <QPainter>
25 #include <QDebug>
26
27 // constants for normal images
28 const int IMAGE_WIDTH = 64;     ///< Created image width
29 const int IMAGE_HEIGHT = 64;    ///< Created image height
30 const int ORIGINAL_IMAGE_X = 7; ///< Original image x position
31 const int ORIGINAL_IMAGE_Y = 7; ///< Original image y position
32 const int ROUNDNESS = 9;        ///< Image roundness
33 const int CLIP_X_OFFSET = 1;    ///< Clip
34
35 // constants for large images
36 const int BORDER_HEIGHT = 118; ///< Height of large profile image border
37 const int BORDER_OFFSET = 9; ///< Transparent area in border graphics edges
38 const int FORCED_IMAGE_HEIGHT = 100; ///< Height where user images are scaled
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 const int SMALL_IMAGE_SIZE = 50;  ///< Size of small facebook profile image
43
44 QPixmap AvatarImage::create(const QPixmap &image)
45 {
46     qWarning() << __PRETTY_FUNCTION__;
47
48     if (image.height() == SMALL_IMAGE_SIZE && image.width() == SMALL_IMAGE_SIZE)
49     {
50         QPixmap avatarImage = QPixmap(IMAGE_WIDTH, IMAGE_HEIGHT);
51         avatarImage.fill(Qt::transparent);
52         QPainter painter(&avatarImage);
53
54         QRect imageRect = QRect(0, 0, image.width(), image.height());
55
56         QPainterPath roundedRect;
57         roundedRect.addRoundedRect(ORIGINAL_IMAGE_X-1, ORIGINAL_IMAGE_Y-1, imageRect.width()+1,
58                                    imageRect.height()+1,ROUNDNESS,ROUNDNESS);
59         painter.save();
60         painter.setClipPath(roundedRect);
61         painter.drawPixmap(QPointF(ORIGINAL_IMAGE_X, ORIGINAL_IMAGE_Y), image);
62         painter.restore();
63         painter.drawPixmap(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
64                            QPixmap(":/res/images/profile_pic_border.png"));
65
66         return avatarImage;
67     } else {
68
69         qWarning() << "Avatarimage original size: " << image.width() << image.height();
70
71         QPixmap scaledProfileImage = image.scaledToHeight(FORCED_IMAGE_HEIGHT,
72                                                           Qt::SmoothTransformation);
73
74         qWarning() << "Avatarimage scaled size:  "
75                    << scaledProfileImage.width() << scaledProfileImage.height();
76
77         //  unsupported image size ratios are drawn without borders.
78         if (scaledProfileImage.width() + 2*BORDER_OFFSET > IMAGE_MAX_WIDTH)
79             return image;
80
81         else if (scaledProfileImage.width() < 2*LEFT_BORDER_WIDTH)
82             return image;
83
84         else {
85
86             QPixmap avatarImage = QPixmap(scaledProfileImage.width()+BORDER_OFFSET*2,
87                                           BORDER_HEIGHT);
88             avatarImage.fill(Qt::transparent);
89             QPainter painter(&avatarImage);
90             painter.drawPixmap(QPointF(BORDER_OFFSET, BORDER_OFFSET),scaledProfileImage);
91
92             QRect leftFrameRectangle(0,0, LEFT_BORDER_WIDTH, BORDER_HEIGHT);
93             QRect middleFrameRectangle(LEFT_BORDER_WIDTH, 0,
94                                        scaledProfileImage.width() - LEFT_BORDER_WIDTH
95                                        - RIGHT_BORDER_WIDHT + 2*BORDER_OFFSET, BORDER_HEIGHT);
96             QRect rightFrameRectangle(LEFT_BORDER_WIDTH + middleFrameRectangle.width(), 0,
97                                       RIGHT_BORDER_WIDHT, BORDER_HEIGHT);
98
99             painter.drawPixmap(leftFrameRectangle,
100                                QPixmap(":/res/images/large_profile_pic_border_left.png"));
101             painter.drawPixmap(middleFrameRectangle,
102                                QPixmap(":/res/images/large_profile_pic_border_middle.png"));
103             painter.drawPixmap(rightFrameRectangle,
104                                QPixmap(":/res/images/large_profile_pic_border_right.png"));
105
106             return avatarImage;
107         }
108     }
109 }