|
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: |
|
15 * |
|
16 */ |
|
17 #include "hgwidgetitem.h" |
|
18 #include "hgquadrenderer.h" |
|
19 #include <hgwidgets/hgwidgets.h> |
|
20 #include <QStringList> |
|
21 #include <HbIcon> |
|
22 |
|
23 HgWidgetItem::HgWidgetItem(HgQuadRenderer* renderer): |
|
24 mTitle(""), |
|
25 mDescription(""), |
|
26 mValidData(false), |
|
27 mHgImage(NULL), |
|
28 mRenderer(renderer) |
|
29 { |
|
30 } |
|
31 |
|
32 HgWidgetItem::HgWidgetItem(HgQuadRenderer* renderer, QImage image, QString title, QString description ) : |
|
33 mTitle(""), |
|
34 mDescription(""), |
|
35 mValidData(false), |
|
36 mHgImage(NULL), |
|
37 mRenderer(renderer), |
|
38 mVisibility(true) |
|
39 { |
|
40 Q_UNUSED(image) |
|
41 setTitle(title); |
|
42 setDescription(description); |
|
43 } |
|
44 |
|
45 HgWidgetItem::~HgWidgetItem() |
|
46 { |
|
47 releaseItemData(); |
|
48 delete mHgImage; |
|
49 } |
|
50 |
|
51 void HgWidgetItem::setImage( QImage image ) |
|
52 { |
|
53 if (!mHgImage) |
|
54 { |
|
55 mHgImage = mRenderer->createNativeImage(); |
|
56 } |
|
57 mHgImage->setImage(image); |
|
58 if (!mVisibility) |
|
59 mHgImage->setAlpha(0); |
|
60 |
|
61 } |
|
62 |
|
63 void HgWidgetItem::setTitle( QString title ) |
|
64 { |
|
65 mTitle = title; |
|
66 } |
|
67 |
|
68 QString HgWidgetItem::title() const |
|
69 { |
|
70 return mTitle; |
|
71 } |
|
72 |
|
73 void HgWidgetItem::setDescription( QString description ) |
|
74 { |
|
75 mDescription = description; |
|
76 } |
|
77 |
|
78 QString HgWidgetItem::description() const |
|
79 { |
|
80 return mDescription; |
|
81 } |
|
82 |
|
83 void HgWidgetItem::setModelIndex(const QModelIndex& index) |
|
84 { |
|
85 mModelIndex = index; |
|
86 } |
|
87 |
|
88 QModelIndex HgWidgetItem::modelIndex() const |
|
89 { |
|
90 return mModelIndex; |
|
91 } |
|
92 |
|
93 bool HgWidgetItem::updateItemData() |
|
94 { |
|
95 mValidData = false; |
|
96 if( mModelIndex.isValid() ){ |
|
97 QVariant image = mModelIndex.data(Qt::DecorationRole); |
|
98 QVariant texts = mModelIndex.data(Qt::DisplayRole); |
|
99 |
|
100 QVariant vis = mModelIndex.data(HgWidget::HgVisibilityRole); |
|
101 if (vis.canConvert<bool>()) |
|
102 { |
|
103 setVisibility(vis.toBool()); |
|
104 } |
|
105 |
|
106 // Convert data to correct format if possible. |
|
107 if(image.canConvert<QImage>()){ |
|
108 setImage(image.value<QImage>()); |
|
109 mValidData = true; |
|
110 } |
|
111 else if(image.canConvert<HbIcon>()){ |
|
112 // This is heavy operation but we need to support |
|
113 // HbIcon too. |
|
114 HbIcon hbIcon = image.value<HbIcon>(); |
|
115 if (!hbIcon.isNull()){ |
|
116 QIcon &qicon = hbIcon.qicon(); |
|
117 QList<QSize> sizes = qicon.availableSizes(); |
|
118 QSize size; |
|
119 foreach(size, sizes){ |
|
120 if (size.width() != 0 && size.height() != 0 ){ |
|
121 QPixmap pixmap = qicon.pixmap(size); |
|
122 if (!pixmap.isNull()){ |
|
123 setImage(pixmap.toImage()); |
|
124 mValidData = true; |
|
125 } |
|
126 break; |
|
127 } |
|
128 } |
|
129 } |
|
130 } |
|
131 else if (image.canConvert<QIcon>()){ |
|
132 // This is heavy operation but we need to support |
|
133 // QIcon too. |
|
134 QIcon tempIcon = image.value<QIcon>(); |
|
135 QList<QSize> sizes = tempIcon.availableSizes(); |
|
136 QSize size; |
|
137 foreach(size, sizes){ |
|
138 if (size.width() != 0 && size.height() != 0 ){ |
|
139 QPixmap pixmap = tempIcon.pixmap(size); |
|
140 if (!pixmap.isNull()){ |
|
141 setImage(pixmap.toImage()); |
|
142 mValidData = true; |
|
143 } |
|
144 break; |
|
145 } |
|
146 } |
|
147 } |
|
148 if( texts.canConvert<QStringList>() ){ |
|
149 QStringList list(texts.toStringList() ); |
|
150 if( list.count() >= 2 ){ |
|
151 setTitle(list.at(0)); |
|
152 setDescription(list.at(1)); |
|
153 mValidData = true; |
|
154 } |
|
155 } |
|
156 |
|
157 } |
|
158 return mValidData; |
|
159 } |
|
160 |
|
161 void HgWidgetItem::releaseItemData() |
|
162 { |
|
163 mTitle = ""; |
|
164 mDescription = ""; |
|
165 mValidData = false; |
|
166 if (mHgImage) |
|
167 mHgImage->releaseImage(); |
|
168 } |
|
169 |
|
170 bool HgWidgetItem::validData() const |
|
171 { |
|
172 return mValidData; |
|
173 } |
|
174 |
|
175 const HgImage* HgWidgetItem::image() const |
|
176 { |
|
177 return mHgImage; |
|
178 } |
|
179 |
|
180 void HgWidgetItem::setVisibility(bool visibility) |
|
181 { |
|
182 mVisibility = visibility; |
|
183 if (mHgImage) |
|
184 { |
|
185 if (!mVisibility) |
|
186 mHgImage->setAlpha(0); |
|
187 else |
|
188 mHgImage->setAlpha(1); |
|
189 } |
|
190 } |