15 * Displays default avatar, when image is not set. |
15 * Displays default avatar, when image is not set. |
16 * |
16 * |
17 */ |
17 */ |
18 |
18 |
19 #include <QPainter> |
19 #include <QPainter> |
|
20 #include <QGraphicsSceneResizeEvent> |
|
21 #include <QDebug> |
20 #include <hbstyle.h> |
22 #include <hbstyle.h> |
21 #include <hbiconitem.h> |
23 #include <hbiconitem.h> |
22 #include <hbstyleloader.h> |
24 #include <hbstyleloader.h> |
|
25 #include <hbdeviceprofile.h> |
23 #include "bubbleimagemanager.h" |
26 #include "bubbleimagemanager.h" |
24 #include "bubbleimagewidget.h" |
27 #include "bubbleimagewidget.h" |
|
28 |
|
29 static const qreal BubbbleImageRounding = 1.5; // units |
25 |
30 |
26 BubbleImageWidget::BubbleImageWidget( |
31 BubbleImageWidget::BubbleImageWidget( |
27 BubbleImageManager& imageManager, |
32 BubbleImageManager& imageManager, |
28 QGraphicsItem* parent) |
33 QGraphicsItem* parent) |
29 : HbWidget(parent), |
34 : HbWidget(parent), |
30 mImageManager(imageManager), |
35 mImageManager(imageManager), |
31 mDefaultAvatar(0) |
36 mDefaultAvatar(0), |
|
37 mKeepSquareShape(0) |
32 { |
38 { |
|
39 setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
40 |
33 // create avatar |
41 // create avatar |
34 mDefaultAvatar = new HbIconItem(this); |
42 mDefaultAvatar = new HbIconItem(this); |
35 style()->setItemName( mDefaultAvatar, "default_avatar" ); |
43 style()->setItemName( mDefaultAvatar, "default_avatar" ); |
36 mDefaultAvatar->setAspectRatioMode(Qt::KeepAspectRatioByExpanding); |
44 mDefaultAvatar->setAspectRatioMode(Qt::KeepAspectRatioByExpanding); |
37 mDefaultAvatar->setAlignment(Qt::AlignCenter); |
45 mDefaultAvatar->setAlignment(Qt::AlignCenter); |
38 |
46 |
39 connect(&mImageManager, SIGNAL(pixmapReady(QString)), |
47 connect(&mImageManager, SIGNAL(pixmapReady(QString)), |
40 this, SLOT(imageLoaded(QString))); |
48 this, SLOT(imageLoaded(QString))); |
41 |
49 |
42 HbStyleLoader::registerFilePath(":/bubbleimagewidget.css"); |
50 HbStyleLoader::registerFilePath( |
43 HbStyleLoader::registerFilePath(":/bubbleimagewidget.widgetml"); |
51 QLatin1String(":/bubbleimagewidget.css")); |
|
52 HbStyleLoader::registerFilePath( |
|
53 QLatin1String(":/bubbleimagewidget.widgetml")); |
44 } |
54 } |
45 |
55 |
46 BubbleImageWidget::~BubbleImageWidget() |
56 BubbleImageWidget::~BubbleImageWidget() |
47 { |
57 { |
48 } |
58 } |
49 |
59 |
50 void BubbleImageWidget::setImage(const QString& name) |
60 void BubbleImageWidget::setImage(const QString& name) |
51 { |
61 { |
52 mImageName = name; |
62 mImageName = name; |
|
63 mUpdateCount = 0; |
53 |
64 |
54 if (!mImageName.isEmpty()) { |
65 if (!mImageName.isEmpty()) { |
55 mDefaultAvatar->setVisible(false); |
66 mDefaultAvatar->setVisible(false); |
56 mDefaultAvatar->setIcon(HbIcon()); |
67 mDefaultAvatar->setIcon(HbIcon()); |
57 mImageManager.loadImage(mImageName); |
68 mImageManager.loadImage(mImageName); |
58 } else { |
69 } else { |
59 mDefaultAvatar->setIconName("qtg_large_avatar"); |
70 mDefaultAvatar->setIconName(QLatin1String("qtg_large_avatar")); |
60 mDefaultAvatar->setVisible(true); |
71 mDefaultAvatar->setVisible(true); |
61 } |
72 } |
62 } |
73 } |
63 |
74 |
64 void BubbleImageWidget::imageLoaded(QString imageFileName) |
75 void BubbleImageWidget::imageLoaded(QString imageFileName) |
73 const QStyleOptionGraphicsItem* option, |
84 const QStyleOptionGraphicsItem* option, |
74 QWidget* widget) |
85 QWidget* widget) |
75 { |
86 { |
76 Q_UNUSED(option); |
87 Q_UNUSED(option); |
77 Q_UNUSED(widget); |
88 Q_UNUSED(widget); |
78 |
89 |
79 if (mImageName.isEmpty()) |
90 if (mImageName.isEmpty()) |
80 return; |
91 return; |
81 |
92 |
82 const QPixmap* pixmap = |
93 const QPixmap* pixmap = |
83 mImageManager.pixmap(mImageName); |
94 mImageManager.pixmap(mImageName); |
84 |
95 |
85 if (pixmap) { |
96 if (pixmap) { |
|
97 painter->save(); |
|
98 |
86 // up or downscales images to fill image area |
99 // up or downscales images to fill image area |
87 QSize imageSize(pixmap->size()); |
100 QSize imageSize(pixmap->size()); |
88 QSize sourceSize(rect().size().toSize()); |
101 QSize sourceSize(rect().size().toSize()); |
89 sourceSize.scale(imageSize,Qt::KeepAspectRatio); |
102 sourceSize.scale(imageSize,Qt::KeepAspectRatio); |
90 QRect sourceRect(QPoint(0,0),sourceSize); |
103 QRect sourceRect(QPoint(0,0),sourceSize); |
93 sourceRect.moveLeft(xOff); |
106 sourceRect.moveLeft(xOff); |
94 sourceRect.moveTop(yOff); |
107 sourceRect.moveTop(yOff); |
95 // qDebug() << "imageSize:" << imageSize; |
108 // qDebug() << "imageSize:" << imageSize; |
96 // qDebug() << "sourceRect:" << sourceRect; |
109 // qDebug() << "sourceRect:" << sourceRect; |
97 painter->setRenderHint(QPainter::SmoothPixmapTransform); |
110 painter->setRenderHint(QPainter::SmoothPixmapTransform); |
|
111 painter->setRenderHint(QPainter::Antialiasing); |
|
112 |
|
113 QPainterPath clip; |
|
114 qreal rounding = BubbbleImageRounding * |
|
115 HbDeviceProfile::profile(this).unitValue(); |
|
116 clip.addRoundedRect(rect(), rounding, rounding); |
|
117 painter->setClipPath(clip); |
98 |
118 |
99 #ifdef __WINS__ |
119 #ifdef __WINS__ |
100 // sourceRect crashes emulator, RC ou1cimx1#320113 |
120 // sourceRect crashes emulator, RC ou1cimx1#320113 |
101 painter->drawPixmap(rect().toRect(),*pixmap); |
121 painter->drawPixmap(rect().toRect(),*pixmap); |
102 #else |
122 #else |
103 painter->drawPixmap(rect().toRect(),*pixmap,sourceRect); |
123 painter->drawPixmap(rect().toRect(),*pixmap,sourceRect); |
104 #endif |
124 #endif |
105 |
125 painter->restore(); |
106 } |
126 } |
107 } |
127 } |
|
128 |
|
129 QSizeF BubbleImageWidget::sizeHint( |
|
130 Qt::SizeHint which, |
|
131 const QSizeF & constraint) const |
|
132 { |
|
133 Q_UNUSED(which); |
|
134 Q_UNUSED(constraint); |
|
135 |
|
136 if (mKeepSquareShape && which==Qt::PreferredSize ) { |
|
137 QSizeF hint(size().height(), size().height()); |
|
138 if (hint.isNull()) { |
|
139 hint = QSizeF(100,100); |
|
140 } |
|
141 |
|
142 // workaround, sometimes last resize event is missing |
|
143 if ((size().width()!=size().height()) && (mUpdateCount<10)) { |
|
144 BubbleImageWidget* mutableThis = const_cast<BubbleImageWidget*>(this); |
|
145 mutableThis->updateGeometry(); |
|
146 mutableThis->mUpdateCount++; // prevent looping |
|
147 } |
|
148 |
|
149 // qDebug() << "size hint: " << hint; |
|
150 return hint; |
|
151 } else { |
|
152 return HbWidget::sizeHint(which,constraint); |
|
153 } |
|
154 } |
|
155 |
|
156 void BubbleImageWidget::resizeEvent(QGraphicsSceneResizeEvent * event) |
|
157 { |
|
158 // qDebug() << "size:" << event->newSize(); |
|
159 if ( event->newSize().height()!= event->oldSize().height() && |
|
160 event->newSize().height() > 1 ) { |
|
161 updateGeometry(); |
|
162 } |
|
163 |
|
164 HbWidget::resizeEvent(event); |
|
165 } |
|
166 |
|
167 bool BubbleImageWidget::keepSquareShape() const |
|
168 { |
|
169 return mKeepSquareShape; |
|
170 } |
|
171 |
|
172 void BubbleImageWidget::setKeepSquareShape(bool keepSquare) |
|
173 { |
|
174 mKeepSquareShape = keepSquare; |
|
175 |
|
176 if (mKeepSquareShape) { |
|
177 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); |
|
178 } else { |
|
179 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
180 } |
|
181 |
|
182 updateGeometry(); |
|
183 } |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |