37
|
1 |
/*!
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Widget to display contact image.
|
|
15 |
* Displays default avatar, when image is not set.
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QPainter>
|
|
20 |
#include <hbstyle.h>
|
|
21 |
#include <hbiconitem.h>
|
|
22 |
#include <hbstyleloader.h>
|
|
23 |
#include "bubbleimagemanager.h"
|
|
24 |
#include "bubbleimagewidget.h"
|
|
25 |
|
|
26 |
BubbleImageWidget::BubbleImageWidget(
|
|
27 |
BubbleImageManager& imageManager,
|
|
28 |
QGraphicsItem* parent)
|
|
29 |
: HbWidget(parent),
|
|
30 |
mImageManager(imageManager),
|
|
31 |
mDefaultAvatar(0)
|
|
32 |
{
|
|
33 |
// create avatar
|
|
34 |
mDefaultAvatar = new HbIconItem(this);
|
|
35 |
style()->setItemName( mDefaultAvatar, "default_avatar" );
|
|
36 |
mDefaultAvatar->setAspectRatioMode(Qt::KeepAspectRatioByExpanding);
|
|
37 |
mDefaultAvatar->setAlignment(Qt::AlignCenter);
|
|
38 |
|
|
39 |
connect(&mImageManager, SIGNAL(pixmapReady(QString)),
|
|
40 |
this, SLOT(imageLoaded(QString)));
|
|
41 |
|
|
42 |
HbStyleLoader::registerFilePath(":/bubbleimagewidget.css");
|
|
43 |
HbStyleLoader::registerFilePath(":/bubbleimagewidget.widgetml");
|
|
44 |
}
|
|
45 |
|
|
46 |
BubbleImageWidget::~BubbleImageWidget()
|
|
47 |
{
|
|
48 |
}
|
|
49 |
|
|
50 |
void BubbleImageWidget::setImage(const QString& name)
|
|
51 |
{
|
|
52 |
mImageName = name;
|
|
53 |
|
|
54 |
if (!mImageName.isEmpty()) {
|
|
55 |
mDefaultAvatar->setVisible(false);
|
|
56 |
mDefaultAvatar->setIcon(HbIcon());
|
|
57 |
mImageManager.loadImage(mImageName);
|
|
58 |
} else {
|
|
59 |
mDefaultAvatar->setIconName("qtg_large_avatar");
|
|
60 |
mDefaultAvatar->setVisible(true);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
void BubbleImageWidget::imageLoaded(QString imageFileName)
|
|
65 |
{
|
|
66 |
if (imageFileName==mImageName) {
|
|
67 |
update(); // does nothing if invisible
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
void BubbleImageWidget::paint(
|
|
72 |
QPainter* painter,
|
|
73 |
const QStyleOptionGraphicsItem* option,
|
|
74 |
QWidget* widget)
|
|
75 |
{
|
|
76 |
Q_UNUSED(option);
|
|
77 |
Q_UNUSED(widget);
|
|
78 |
|
|
79 |
if (mImageName.isEmpty())
|
|
80 |
return;
|
|
81 |
|
|
82 |
const QPixmap* pixmap =
|
|
83 |
mImageManager.pixmap(mImageName);
|
|
84 |
|
|
85 |
if (pixmap) {
|
|
86 |
// up or downscales images to fill image area
|
|
87 |
QSize imageSize(pixmap->size());
|
|
88 |
QSize sourceSize(rect().size().toSize());
|
|
89 |
sourceSize.scale(imageSize,Qt::KeepAspectRatio);
|
|
90 |
QRect sourceRect(QPoint(0,0),sourceSize);
|
|
91 |
int xOff = (imageSize.width() - sourceSize.width())/2;
|
|
92 |
int yOff = (imageSize.height() - sourceSize.height())/2;
|
|
93 |
sourceRect.moveLeft(xOff);
|
|
94 |
sourceRect.moveTop(yOff);
|
|
95 |
// qDebug() << "imageSize:" << imageSize;
|
|
96 |
// qDebug() << "sourceRect:" << sourceRect;
|
|
97 |
painter->setRenderHint(QPainter::SmoothPixmapTransform);
|
|
98 |
|
|
99 |
#ifdef __WINS__
|
|
100 |
// sourceRect crashes emulator, RC ou1cimx1#320113
|
|
101 |
painter->drawPixmap(rect().toRect(),*pixmap);
|
|
102 |
#else
|
|
103 |
painter->drawPixmap(rect().toRect(),*pixmap,sourceRect);
|
|
104 |
#endif
|
|
105 |
|
|
106 |
}
|
|
107 |
}
|