9778e0d376c03ea26765d8864d8dbecf5b908a96
[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 <QPen>
26 #include <QDebug>
27
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
36 const int IMAGE_BORDER = 0;
37 const int SMALL_IMAGE_SIZE = 50;
38 const int IMAGE_MAX_HEIGHT = 200;
39 const int LEFT_BORDER_WIDTH = 20;
40 const int RIGHT_BORDER_WIDHT = 20;
41 const int BORDER_HEIGHT = 118;
42 const int MAX_IMAGE_WIDTH = 200;
43 const int OFFSET = 9;
44
45 QPixmap AvatarImage::create(const QPixmap &image)
46 {
47     qWarning() << __PRETTY_FUNCTION__;
48
49     if (image.height() == SMALL_IMAGE_SIZE && image.width() == SMALL_IMAGE_SIZE)
50     {
51         QPixmap avatarImage = QPixmap(IMAGE_WIDTH, IMAGE_HEIGHT);
52         avatarImage.fill(Qt::transparent);
53         QPainter painter(&avatarImage);
54
55         QRect imageRect = QRect(0, 0, image.width(), image.height());
56
57         QPainterPath roundedRect;
58         roundedRect.addRoundedRect(ORIGINAL_IMAGE_X-1, ORIGINAL_IMAGE_Y-1, imageRect.width()+1,
59                                    imageRect.height()+1,ROUNDNESS,ROUNDNESS);
60         painter.save();
61         painter.setClipPath(roundedRect);
62         painter.drawPixmap(QPointF(ORIGINAL_IMAGE_X, ORIGINAL_IMAGE_Y), image);
63         painter.restore();
64         painter.drawPixmap(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
65                            QPixmap(":/res/images/profile_pic_border.png"));
66
67         return avatarImage;
68     } else {
69
70     qWarning() << "AVATARIMAGE: alkup koko " << image.width() << image.height();
71
72     int imageHeight = image.height();
73     if (imageHeight > IMAGE_MAX_HEIGHT)
74         imageHeight = IMAGE_MAX_HEIGHT;
75
76     int imageWidth = image.width();
77     if (imageWidth < LEFT_BORDER_WIDTH + RIGHT_BORDER_WIDHT)
78         imageWidth = LEFT_BORDER_WIDTH + RIGHT_BORDER_WIDHT;
79     if (imageWidth > MAX_IMAGE_WIDTH)
80         imageWidth = MAX_IMAGE_WIDTH;
81
82     qWarning() << "AVATARIMAGE: uusi koko " << image.width() << image.height();
83     QPixmap avatarImage = QPixmap(imageWidth, imageHeight);
84     avatarImage.fill(Qt::transparent);
85     QPainter painter(&avatarImage);
86     painter.drawPixmap(QPointF(OFFSET, OFFSET),image);
87
88     QRect left(0,0, LEFT_BORDER_WIDTH, imageHeight);
89     QRect middle(LEFT_BORDER_WIDTH, 0, image.width() - LEFT_BORDER_WIDTH - RIGHT_BORDER_WIDHT,
90         imageHeight);
91     QRect right(LEFT_BORDER_WIDTH + middle.width(), 0, RIGHT_BORDER_WIDHT, imageHeight);
92
93     QPixmap leftBorder(":/res/images/large_profile_pic_border_left.png");
94     QPixmap middleBorder(":/res/images/large_profile_pic_border_middle.png");
95     QPixmap rightBorder(":/res/images/large_profile_pic_border_right.png");
96
97     QPixmap topLeft(":/res/images/large_profile_pic_border_top_left.png");
98     QPixmap topRight(":/res/images/large_profile_pic_border_top_right.png");
99     QPixmap bottomLeft(":/res/images/large_profile_pic_border_bottom_left.png");
100     QPixmap bottomRight(":/res/images/large_profile_pic_border_bottom_right.png");
101
102     painter.drawPixmap(QPointF(0,0), topLeft);
103     painter.drawPixmap(QPointF(10,10), topRight);
104     painter.drawPixmap(QPointF(10,20), bottomLeft);
105     painter.drawPixmap(QPointF(10,30), bottomRight);
106
107
108 //    painter.drawPixmap(left, leftBorder);
109 //    painter.drawPixmap(middle, middleBorder);
110 //    painter.drawPixmap(right, rightBorder);
111
112
113     return avatarImage;
114     }
115 }
116
117
118
119 ////       double scaleFactor = IMAGE_MAX_HEIGHT / image.height();
120 ////       painter.scale(scaleFactor, scaleFactor);
121 //       //painter.translate(image.width()/2, image.height()/2);
122
123
124
125
126 //       QPainterPath roundedRect;
127 //       roundedRect.addRoundedRect(QRect(0, 0, image.width(),
128 //                                  imageHeight), ROUNDNESS, ROUNDNESS, Qt::AbsoluteSize);
129 //       painter.setClipPath(roundedRect); //pyöristys laittaa reunat valkoisiksi
130 //       painter.drawPixmap(QPointF(IMAGE_BORDER, IMAGE_BORDER),image);
131
132        //painter.drawTiledPixmap();
133
134        //PYÖRISTÄ KUVAA
135 //       QRect imageRect = QRect(0, 0, image.width(), image.height());
136 //       QPainterPath roundedRect;
137 //       roundedRect.addRoundedRect(ORIGINAL_IMAGE_X-1, ORIGINAL_IMAGE_Y-1, imageRect.width()+1,
138 //                                  imageRect.height()+1,ROUNDNESS,ROUNDNESS);
139 //       painter.setClipPath(roundedRect);
140
141        // PIIRRÄ KEHYS
142 //       painter.drawPixmap(0, 0, image.width(), image.height(),
143 //                          QPixmap(":/res/images/profile_pic_border.png"));