12
|
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: Homescreen bookmark widget
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include <QGraphicsSceneMouseEvent>
|
|
18 |
#include <QPainter>
|
|
19 |
|
|
20 |
#include <HbStyleLoader>
|
|
21 |
#include <HbFrameItem>
|
|
22 |
#include <HbFrameDrawer>
|
|
23 |
#include <HbIconItem>
|
|
24 |
#include <HbTextItem>
|
16
|
25 |
#include <HbTapGesture>
|
12
|
26 |
#include <HbInstantFeedback>
|
|
27 |
|
|
28 |
#include <QDir>
|
|
29 |
#include <qdesktopservices.h>
|
|
30 |
#include <qurl.h>
|
|
31 |
#include <qdebug.h>
|
|
32 |
#include "hsbookmarkwidget.h"
|
|
33 |
|
|
34 |
/*!
|
|
35 |
\ingroup group_hsbookmark_widget
|
|
36 |
\class HsBookmarkWidget
|
|
37 |
\brief Implementation for homescreen bookmark widget.
|
|
38 |
|
|
39 |
HsBookmarkWidget derived from the HbWidget and implements
|
|
40 |
needed functions for the homescreen bookmark widget.
|
|
41 |
*/
|
|
42 |
|
|
43 |
/*!
|
|
44 |
Constructs a widget which is a child of \a parent, with widget flags set to \a flags.
|
|
45 |
*/
|
|
46 |
HsBookmarkWidget::HsBookmarkWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
|
|
47 |
: HbWidget(parent, flags),
|
16
|
48 |
mBackground(0), mIcon(0), mText(0)
|
12
|
49 |
{
|
16
|
50 |
grabGesture(Qt::TapGesture);
|
|
51 |
|
12
|
52 |
HbStyleLoader::registerFilePath(":/hsbookmarkwidget.widgetml");
|
|
53 |
HbStyleLoader::registerFilePath(":/hsbookmarkwidget.css");
|
|
54 |
|
|
55 |
createPrimitives();
|
|
56 |
}
|
|
57 |
|
|
58 |
/*!
|
|
59 |
Destructor
|
|
60 |
*/
|
|
61 |
HsBookmarkWidget::~HsBookmarkWidget()
|
|
62 |
{
|
|
63 |
HbStyleLoader::unregisterFilePath(":/hsbookmarkwidget.widgetml");
|
|
64 |
HbStyleLoader::unregisterFilePath(":/hsbookmarkwidget.css");
|
|
65 |
}
|
|
66 |
|
|
67 |
/*!
|
|
68 |
Set's widget root path as \a rootPath
|
|
69 |
*/
|
|
70 |
void HsBookmarkWidget::setRootPath(const QString &rootPath)
|
|
71 |
{
|
|
72 |
mRootPath = rootPath;
|
|
73 |
}
|
|
74 |
|
|
75 |
/*!
|
|
76 |
Root path for widget's resources
|
|
77 |
*/
|
|
78 |
QString HsBookmarkWidget::rootPath()const
|
|
79 |
{
|
|
80 |
return mRootPath;
|
|
81 |
}
|
|
82 |
|
|
83 |
/*!
|
|
84 |
Set the bookmarkTitle property. This property is needed by css selector.
|
|
85 |
*/
|
|
86 |
void HsBookmarkWidget::setBookmarkTitle(const QString &bookmarkTitle)
|
|
87 |
{
|
|
88 |
mBookmarkTitle = bookmarkTitle;
|
|
89 |
}
|
|
90 |
|
|
91 |
/*!
|
|
92 |
Return the bookmarkTitle property. This property is needed by css selector.
|
|
93 |
*/
|
|
94 |
QString HsBookmarkWidget::bookmarkTitle() const
|
|
95 |
{
|
|
96 |
return mBookmarkTitle;
|
|
97 |
}
|
|
98 |
|
|
99 |
/*!
|
|
100 |
Set the bookmarkUrl property. This property is needed by QDestopService
|
|
101 |
to launch into browser.
|
|
102 |
*/
|
|
103 |
void HsBookmarkWidget::setBookmarkUrl(const QString &bookmarkUrl)
|
|
104 |
{
|
|
105 |
mBookmarkUrl = bookmarkUrl;
|
|
106 |
}
|
|
107 |
|
|
108 |
/*!
|
|
109 |
Return the bookmarkUrl property. This property is needed by QDestopService
|
|
110 |
to launch into browser.
|
|
111 |
*/
|
|
112 |
QString HsBookmarkWidget::bookmarkUrl() const
|
|
113 |
{
|
|
114 |
return mBookmarkUrl;
|
|
115 |
}
|
|
116 |
|
|
117 |
/*!
|
|
118 |
Set the iconFileName property. This property is to load the favison icon.
|
|
119 |
*/
|
|
120 |
void HsBookmarkWidget::setFaviconFileName(const QString &faviconFileName)
|
|
121 |
{
|
|
122 |
mFaviconFileName = faviconFileName;
|
|
123 |
}
|
|
124 |
|
|
125 |
/*!
|
|
126 |
Return the iconFileName property. This property is to load the favison icon.
|
|
127 |
*/
|
|
128 |
QString HsBookmarkWidget::faviconFileName() const
|
|
129 |
{
|
|
130 |
return mFaviconFileName;
|
|
131 |
}
|
|
132 |
|
|
133 |
/*!
|
|
134 |
Set the iconFileName property. This property is to load the favison icon.
|
|
135 |
*/
|
|
136 |
void HsBookmarkWidget::setFaviconPath(const QString &faviconPath)
|
|
137 |
{
|
|
138 |
mFaviconPath = faviconPath;
|
|
139 |
}
|
|
140 |
|
|
141 |
/*!
|
|
142 |
Return the iconFileName property. This property is to load the favison icon.
|
|
143 |
*/
|
|
144 |
QString HsBookmarkWidget::faviconPath() const
|
|
145 |
{
|
|
146 |
return mFaviconPath;
|
|
147 |
}
|
|
148 |
|
|
149 |
/*!
|
16
|
150 |
Process gesture events.
|
12
|
151 |
*/
|
16
|
152 |
void HsBookmarkWidget::gestureEvent(QGestureEvent *event)
|
12
|
153 |
{
|
16
|
154 |
HbTapGesture *gesture = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
|
|
155 |
if (gesture) {
|
|
156 |
switch (gesture->state()) {
|
|
157 |
case Qt::GestureStarted:
|
|
158 |
setBackgroundToPressed();
|
|
159 |
break;
|
|
160 |
case Qt::GestureCanceled:
|
|
161 |
setBackgroundToNormal();
|
|
162 |
break;
|
|
163 |
case Qt::GestureFinished:
|
|
164 |
setBackgroundToNormal();
|
|
165 |
if (gesture->tapStyleHint() == HbTapGesture::Tap) {
|
|
166 |
launch();
|
|
167 |
}
|
|
168 |
break;
|
|
169 |
default:
|
|
170 |
break;
|
|
171 |
}
|
12
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
void HsBookmarkWidget::onInitialize()
|
|
176 |
{
|
|
177 |
if (mFaviconFileName.isNull())
|
|
178 |
{
|
|
179 |
// get default icon, rootPath is set thus we can use it
|
|
180 |
QString iconPath = QDir::toNativeSeparators(mRootPath + "/nokiabrowser-bm-icon_70x70_no_ico.png");
|
|
181 |
HbIcon icon = HbIcon(iconPath);
|
|
182 |
updateContent(mBookmarkTitle, icon);
|
|
183 |
}
|
|
184 |
else
|
|
185 |
{
|
|
186 |
// get favicon first
|
|
187 |
QString faviconPath = QDir::toNativeSeparators(mFaviconPath + mFaviconFileName);
|
|
188 |
|
|
189 |
HbIcon favicon = HbIcon(faviconPath);
|
|
190 |
favicon.setSize(QSize(HsBookmarkWidget::faviconSize, HsBookmarkWidget::faviconSize));
|
|
191 |
|
|
192 |
QString iconPath = QDir::toNativeSeparators(mRootPath + "/nokiabrowser-bm-icon_70x70_ico.png");
|
|
193 |
HbIcon icon = HbIcon(iconPath);
|
|
194 |
|
|
195 |
updateContent(mBookmarkTitle,
|
|
196 |
createIconWithOverlay(icon,
|
|
197 |
QPoint(HsBookmarkWidget::faviconPoint, HsBookmarkWidget::faviconPoint),
|
|
198 |
favicon));
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
/*!
|
|
203 |
Called when widget is shown in the home screen
|
|
204 |
*/
|
|
205 |
void HsBookmarkWidget::onShow()
|
|
206 |
{
|
|
207 |
setVisible(true);
|
|
208 |
}
|
|
209 |
|
|
210 |
/*!
|
|
211 |
Called when widget is hidden from the home screen
|
|
212 |
*/
|
|
213 |
|
|
214 |
void HsBookmarkWidget::onHide()
|
|
215 |
{
|
|
216 |
}
|
|
217 |
|
16
|
218 |
|
12
|
219 |
|
|
220 |
/*!
|
|
221 |
\internal
|
|
222 |
*/
|
16
|
223 |
void HsBookmarkWidget::launch()
|
12
|
224 |
{
|
|
225 |
//HbInstantFeedback::play(HSCONFIGURATION_GET(bookmarkWidgetTapFeedbackEffect));
|
|
226 |
|
|
227 |
QUrl url = QUrl(mBookmarkUrl);
|
|
228 |
qDebug() << "hsBookmarkWidget - opening url - " << url << ", " << stat;
|
|
229 |
bool stat = QDesktopServices::openUrl(url);
|
|
230 |
qDebug() << "hsBookmarkWidget - opened url - " << url << ", " << stat;
|
|
231 |
}
|
|
232 |
|
|
233 |
/*!
|
|
234 |
\internal
|
|
235 |
*/
|
|
236 |
void HsBookmarkWidget::createPrimitives()
|
|
237 |
{
|
|
238 |
// Background
|
|
239 |
if (!mBackground) {
|
|
240 |
HbFrameDrawer *drawer = new HbFrameDrawer(
|
|
241 |
QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
|
|
242 |
mBackground = new HbFrameItem(drawer, this);
|
|
243 |
HbStyle::setItemName(mBackground, QLatin1String("background"));
|
|
244 |
}
|
|
245 |
|
|
246 |
// Icon
|
|
247 |
if (!mIcon) {
|
|
248 |
mIcon = new HbIconItem(this);
|
|
249 |
HbStyle::setItemName(mIcon, QLatin1String("icon"));
|
|
250 |
}
|
|
251 |
|
|
252 |
// Text
|
|
253 |
if (!mText) {
|
|
254 |
mText = new HbTextItem(this);
|
|
255 |
HbStyle::setItemName(mText, QLatin1String("text"));
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
/*!
|
|
260 |
\internal
|
|
261 |
*/
|
|
262 |
void HsBookmarkWidget::setBackgroundToNormal()
|
|
263 |
{
|
|
264 |
mBackground->frameDrawer().
|
|
265 |
setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
|
|
266 |
}
|
|
267 |
|
|
268 |
/*!
|
|
269 |
\internal
|
|
270 |
*/
|
|
271 |
void HsBookmarkWidget::setBackgroundToPressed()
|
|
272 |
{
|
|
273 |
mBackground->frameDrawer().
|
|
274 |
setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
|
|
275 |
}
|
|
276 |
|
|
277 |
/*!
|
|
278 |
\internal
|
|
279 |
*/
|
|
280 |
void HsBookmarkWidget::updateContent(const QString title, const HbIcon& icon)
|
|
281 |
{
|
|
282 |
mIcon->setIcon(icon);
|
|
283 |
|
|
284 |
if (title != "") {
|
|
285 |
mText->setText(title.left(HsBookmarkWidget::maxDisplayLen));
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
QImage HsBookmarkWidget::createImageWithOverlay(const QImage& baseImage, const QPoint& point, const QImage& overlayImage)
|
|
290 |
{
|
|
291 |
QImage imageWithOverlay = QImage(baseImage.size(), QImage::Format_ARGB32_Premultiplied);
|
|
292 |
QPainter painter(&imageWithOverlay);
|
|
293 |
|
|
294 |
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
295 |
painter.fillRect(imageWithOverlay.rect(), Qt::transparent);
|
|
296 |
|
|
297 |
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
298 |
painter.drawImage(0, 0, baseImage);
|
|
299 |
|
|
300 |
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
301 |
painter.drawImage(point, overlayImage);
|
|
302 |
|
|
303 |
painter.end();
|
|
304 |
|
|
305 |
return imageWithOverlay;
|
|
306 |
}
|
|
307 |
|
|
308 |
HbIcon HsBookmarkWidget::createIconWithOverlay(HbIcon& baseIcon, const QPoint& point, HbIcon& overlayIcon)
|
|
309 |
{
|
|
310 |
|
|
311 |
// Create overlay image with favicon on top
|
|
312 |
QPixmap basePixmap = baseIcon.pixmap();
|
|
313 |
QPixmap overlayImage = overlayIcon.pixmap();
|
|
314 |
|
|
315 |
QImage imageWithOverlay = createImageWithOverlay(basePixmap.toImage(), point, overlayImage.toImage());
|
|
316 |
QPixmap pixmapWithOverlay = QPixmap::fromImage(imageWithOverlay);
|
|
317 |
|
|
318 |
return HbIcon(QIcon(pixmapWithOverlay));
|
|
319 |
}
|
|
320 |
|
|
321 |
void HsBookmarkWidget::removeFavicon()
|
|
322 |
{
|
|
323 |
QDir faviconDir(mFaviconPath);
|
|
324 |
|
|
325 |
faviconDir.remove(mFaviconFileName);
|
|
326 |
}
|