|
1 /* |
|
2 * Copyright (c) 2010 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: Indicator Widget. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "snsrindicatorwidget.h" |
|
19 #include "snsrindicatorinfo.h" |
|
20 |
|
21 #include <HbColorScheme> |
|
22 #include <QDebug> |
|
23 #include <QSizePolicy> |
|
24 #include <HbStyleLoader> |
|
25 #include <HbIconItem> |
|
26 #include <HbIndicatorInterface> |
|
27 |
|
28 |
|
29 const char *gIndicatorCssFilePath =":/style/snsrindicatorwidget.css"; |
|
30 const char *gIndicatorWidgetMLFilePath = ":/style/snsrindicatorwidget.widgetml"; |
|
31 const char *gIndicatorColorCssFilePath = ":/style/snsrindicatorwidget_color.css"; |
|
32 |
|
33 const int gNumberOfIcons(6); |
|
34 |
|
35 /*! |
|
36 \class SnsrIndicatorWidget |
|
37 \ingroup group_snsrbigclockscreensaverprovider |
|
38 \brief Screensaver indicator widget. |
|
39 */ |
|
40 |
|
41 /*! |
|
42 Constructs a new SnsrIndicatorWidget. |
|
43 \param parent Parent object. |
|
44 */ |
|
45 SnsrIndicatorWidget::SnsrIndicatorWidget(QGraphicsItem* parent): |
|
46 HbWidget(parent), mLayoutType(IndicatorsCentered) |
|
47 { |
|
48 HbStyleLoader::registerFilePath(gIndicatorCssFilePath); |
|
49 HbStyleLoader::registerFilePath(gIndicatorWidgetMLFilePath); |
|
50 |
|
51 // TODO: will some color variables be added into hbcolorgroup.css so |
|
52 // that one don't need to use hard-coded values? If not, then |
|
53 // changes needed in this widget. |
|
54 HbStyleLoader::registerFilePath(gIndicatorColorCssFilePath); |
|
55 |
|
56 setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); |
|
57 |
|
58 createPrimitives(); |
|
59 } |
|
60 |
|
61 /*! |
|
62 Destructs the class. |
|
63 */ |
|
64 SnsrIndicatorWidget::~SnsrIndicatorWidget() |
|
65 { |
|
66 // icons within the list are deleted by parent |
|
67 mIcons.clear(); |
|
68 |
|
69 HbStyleLoader::unregisterFilePath( gIndicatorCssFilePath ); |
|
70 HbStyleLoader::unregisterFilePath( gIndicatorWidgetMLFilePath ); |
|
71 HbStyleLoader::unregisterFilePath(gIndicatorColorCssFilePath); |
|
72 } |
|
73 |
|
74 |
|
75 SnsrIndicatorWidget::IndicatorLayoutType SnsrIndicatorWidget::layoutType() const |
|
76 { |
|
77 return mLayoutType; |
|
78 } |
|
79 |
|
80 /*! |
|
81 Sets the name of layout definition \a layoutName for selecting |
|
82 the layout of view items of this view from css/xml files. |
|
83 |
|
84 This layout is accessible from css file as layout property |
|
85 of the view item. |
|
86 |
|
87 */ |
|
88 void SnsrIndicatorWidget::setLayoutType(IndicatorLayoutType type) |
|
89 { |
|
90 if (mLayoutType != type ) { |
|
91 mLayoutType = type; |
|
92 QSizePolicy::Policy hSizePolicy(QSizePolicy::Expanding); |
|
93 if ( mLayoutType == IndicatorsAlignedLeft ) { |
|
94 hSizePolicy = QSizePolicy::Preferred; |
|
95 } |
|
96 setSizePolicy(hSizePolicy,QSizePolicy::Preferred); |
|
97 } |
|
98 } |
|
99 |
|
100 /*! |
|
101 This is called whenever indicators' status changes: parameter list |
|
102 tells which indicators should be shown at the given moment. All |
|
103 previously shown indicators should be hidden and show only these |
|
104 indicators passed as a parameter. |
|
105 */ |
|
106 void SnsrIndicatorWidget::showIndicators(const QList<SnsrIndicatorInfo> &indicators) |
|
107 { |
|
108 // Update icon paths so that shown iconItems have |
|
109 // valid icon path and rest have empty path (->invisible) |
|
110 |
|
111 int indicatorAmount = indicators.size(); |
|
112 |
|
113 removeAllIndicators(); |
|
114 |
|
115 // TODO: after wk18 color is lost after icon name is set again - bug or what? |
|
116 // workaround: ask color from theme and set it again here |
|
117 // If this must be left here, then change back-up color to white? |
|
118 QColor iconColor = HbColorScheme::color("snsrforeground"); // TODO: final variable name? |
|
119 if (iconColor.spec() == QColor::Invalid ) { |
|
120 iconColor = Qt::magenta; |
|
121 } |
|
122 // update icon paths for items that are gonna be shown |
|
123 for (int i=0; i < indicatorAmount && i < mIcons.size(); ++i) { |
|
124 mIcons.at(i)->setIconName(indicators.at(i).iconPath); |
|
125 mIcons.at(i)->setFlags(HbIcon::Colorized); |
|
126 mIcons.at(i)->setColor(iconColor); |
|
127 } |
|
128 |
|
129 // To recalculate layouts |
|
130 updateGeometry(); |
|
131 if ( parentLayoutItem() ) { |
|
132 parentLayoutItem()->updateGeometry(); // clear the cache of the parent |
|
133 } |
|
134 } |
|
135 |
|
136 /*! |
|
137 Remove all indicators from UI. |
|
138 */ |
|
139 void SnsrIndicatorWidget::removeAllIndicators() |
|
140 { |
|
141 for (int i=0; i < mIcons.size(); ++i) { |
|
142 mIcons.at(i)->setIconName(QString()); |
|
143 } |
|
144 } |
|
145 |
|
146 /*! |
|
147 If indicator pane is going to be centered, then calculate width so that it's |
|
148 exactly the same as visible icons are taking space (+spacers between them). |
|
149 If indicator pane is left aligned, then actual width doesn't matter. |
|
150 Assumption is that icons are fixed sized. |
|
151 \reimp |
|
152 */ |
|
153 QSizeF SnsrIndicatorWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
154 { |
|
155 QSizeF hint = HbWidget::sizeHint(which, constraint); |
|
156 |
|
157 // TODO: seems to work if icons are fixed-sized (units) |
|
158 // TODO: even though size policy is preferred, should we give also |
|
159 // minimum and maximum sizes? |
|
160 if (which == Qt::PreferredSize && layoutType() == IndicatorsCentered ) { |
|
161 qreal width(0); |
|
162 qreal itemWidth(0); |
|
163 qreal iconSpace(0); |
|
164 int visibleIconCount(0); |
|
165 |
|
166 // TODO: use geometry or size? |
|
167 for (int i=0; i < mIcons.size(); ++i) { |
|
168 if (!mIcons.at(i)->iconName().isEmpty() ) { |
|
169 visibleIconCount++; |
|
170 itemWidth = mIcons.at(i)->iconItemSize().width(); |
|
171 width = width + itemWidth; |
|
172 } |
|
173 } |
|
174 if (visibleIconCount > 1) { |
|
175 style()->parameter("hb-param-margin-gene-middle-horizontal", iconSpace); |
|
176 width = width + ((visibleIconCount-1)*iconSpace); |
|
177 } |
|
178 |
|
179 hint.setWidth(width); |
|
180 } |
|
181 |
|
182 return hint; |
|
183 } |
|
184 |
|
185 /*! |
|
186 Creates all widget primitives. |
|
187 Coloring is handled via CSS. |
|
188 */ |
|
189 void SnsrIndicatorWidget::createPrimitives() |
|
190 { |
|
191 if (!mIcons.isEmpty()) { |
|
192 return; |
|
193 } |
|
194 |
|
195 HbIconItem *icon(0); |
|
196 QLatin1String itemNameBase("icon"); |
|
197 int itemNameEnd(1); |
|
198 QString itemName(""); |
|
199 |
|
200 while (mIcons.count() < gNumberOfIcons) { |
|
201 // When created, icon names are left empty -> nothing shown in UI. |
|
202 icon = new HbIconItem(this); |
|
203 icon->setFlags(HbIcon::Colorized); |
|
204 icon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); |
|
205 |
|
206 // Create items that are named icon1..icon6. |
|
207 itemName.clear(); |
|
208 itemName.append(itemNameBase); |
|
209 itemName.append(QString::number(itemNameEnd)); |
|
210 HbStyle::setItemName(icon, itemName); |
|
211 ++itemNameEnd; |
|
212 |
|
213 mIcons.append(icon); |
|
214 } |
|
215 } |