homescreen/widgetplugins/hsbookmarkwidgetplugin/src/hsbookmarkwidget.cpp
changeset 15 73c48011b8c7
child 16 3c88a81ff781
equal deleted inserted replaced
13:491a1d15372f 15:73c48011b8c7
       
     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>
       
    25 #include <HbTouchArea>
       
    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),
       
    48     	mBackground(0), mIcon(0), mText(0), mTouchArea(0)
       
    49 {
       
    50     HbStyleLoader::registerFilePath(":/hsbookmarkwidget.widgetml");
       
    51     HbStyleLoader::registerFilePath(":/hsbookmarkwidget.css");
       
    52     	
       
    53     createPrimitives();
       
    54 }
       
    55 
       
    56 /*!
       
    57     Destructor
       
    58 */
       
    59 HsBookmarkWidget::~HsBookmarkWidget()
       
    60 {
       
    61     HbStyleLoader::unregisterFilePath(":/hsbookmarkwidget.widgetml");
       
    62     HbStyleLoader::unregisterFilePath(":/hsbookmarkwidget.css");
       
    63 }
       
    64 
       
    65 /*!
       
    66     Set's widget root path as \a rootPath
       
    67 */
       
    68 void HsBookmarkWidget::setRootPath(const QString &rootPath)
       
    69 {
       
    70     mRootPath = rootPath;
       
    71 }
       
    72 
       
    73 /*!
       
    74     Root path for widget's resources
       
    75 */
       
    76 QString HsBookmarkWidget::rootPath()const
       
    77 {
       
    78     return mRootPath;
       
    79 }
       
    80 
       
    81 /*!
       
    82     Set the bookmarkTitle property. This property is needed by css selector.
       
    83 */
       
    84 void HsBookmarkWidget::setBookmarkTitle(const QString &bookmarkTitle) 
       
    85 { 
       
    86     mBookmarkTitle = bookmarkTitle; 
       
    87 }
       
    88 
       
    89 /*!
       
    90     Return the bookmarkTitle property. This property is needed by css selector.
       
    91 */
       
    92 QString HsBookmarkWidget::bookmarkTitle() const 
       
    93 { 
       
    94     return mBookmarkTitle; 
       
    95 }
       
    96 
       
    97 /*!
       
    98     Set the bookmarkUrl property. This property is needed by QDestopService 
       
    99     to launch into browser.
       
   100 */
       
   101 void HsBookmarkWidget::setBookmarkUrl(const QString &bookmarkUrl) 
       
   102 { 
       
   103     mBookmarkUrl = bookmarkUrl; 
       
   104 }
       
   105     
       
   106 /*!
       
   107     Return the bookmarkUrl property. This property is needed by QDestopService 
       
   108     to launch into browser.
       
   109 */
       
   110 QString HsBookmarkWidget::bookmarkUrl() const 
       
   111 { 
       
   112     return mBookmarkUrl; 
       
   113 }
       
   114 
       
   115 /*!
       
   116     Set the iconFileName property. This property is to load the favison icon.
       
   117 */
       
   118 void HsBookmarkWidget::setFaviconFileName(const QString &faviconFileName) 
       
   119 { 
       
   120     mFaviconFileName = faviconFileName; 
       
   121 }
       
   122     
       
   123 /*!
       
   124     Return the iconFileName property. This property is to load the favison icon.
       
   125 */
       
   126 QString HsBookmarkWidget::faviconFileName() const 
       
   127 { 
       
   128     return mFaviconFileName; 
       
   129 }
       
   130 
       
   131 /*!
       
   132     Set the iconFileName property. This property is to load the favison icon.
       
   133 */
       
   134 void HsBookmarkWidget::setFaviconPath(const QString &faviconPath) 
       
   135 { 
       
   136     mFaviconPath = faviconPath; 
       
   137 }
       
   138     
       
   139 /*!
       
   140     Return the iconFileName property. This property is to load the favison icon.
       
   141 */
       
   142 QString HsBookmarkWidget::faviconPath() const 
       
   143 { 
       
   144     return mFaviconPath; 
       
   145 }
       
   146 
       
   147 /*!
       
   148     Filters touch area events.
       
   149 */
       
   150 bool HsBookmarkWidget::eventFilter(QObject *watched, QEvent *event)
       
   151 {
       
   152     Q_UNUSED(watched)
       
   153 
       
   154     switch (event->type()) {
       
   155         case QEvent::GraphicsSceneMousePress:
       
   156             handleMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
   157             return true;
       
   158         case QEvent::GraphicsSceneMouseMove:
       
   159             handleMouseMoveEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
   160             return true;
       
   161         case QEvent::GraphicsSceneMouseRelease:
       
   162             handleMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
   163             return true;
       
   164         case QEvent::UngrabMouse:
       
   165             setBackgroundToNormal();
       
   166             return true;
       
   167         default:
       
   168             break;
       
   169     }
       
   170 
       
   171     return false;
       
   172 }
       
   173 
       
   174 void HsBookmarkWidget::onInitialize()
       
   175 {
       
   176     if (mFaviconFileName.isNull())
       
   177     {	  
       
   178         // get default icon, rootPath is set thus we can use it
       
   179         QString iconPath = QDir::toNativeSeparators(mRootPath + "/nokiabrowser-bm-icon_70x70_no_ico.png");
       
   180         HbIcon icon = HbIcon(iconPath);
       
   181         updateContent(mBookmarkTitle, icon);
       
   182     } 
       
   183     else
       
   184     {
       
   185         // get favicon first
       
   186         QString faviconPath = QDir::toNativeSeparators(mFaviconPath + mFaviconFileName);
       
   187 	  	
       
   188         HbIcon favicon = HbIcon(faviconPath);
       
   189         favicon.setSize(QSize(HsBookmarkWidget::faviconSize, HsBookmarkWidget::faviconSize));
       
   190          	
       
   191         QString iconPath = QDir::toNativeSeparators(mRootPath + "/nokiabrowser-bm-icon_70x70_ico.png");
       
   192         HbIcon icon = HbIcon(iconPath);
       
   193         
       
   194         updateContent(mBookmarkTitle, 
       
   195                       createIconWithOverlay(icon, 
       
   196                                             QPoint(HsBookmarkWidget::faviconPoint, HsBookmarkWidget::faviconPoint), 
       
   197                                             favicon));
       
   198     }
       
   199 }
       
   200 
       
   201 /*!
       
   202     Called when widget is shown in the home screen
       
   203 */
       
   204 void HsBookmarkWidget::onShow()
       
   205 {
       
   206     setVisible(true);
       
   207 }
       
   208 
       
   209 /*!
       
   210     Called when widget is hidden from the home screen
       
   211 */
       
   212 
       
   213 void HsBookmarkWidget::onHide()
       
   214 {
       
   215 }
       
   216 
       
   217 /*!
       
   218     \internal
       
   219 */
       
   220 void HsBookmarkWidget::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
       
   221 {
       
   222     Q_UNUSED(event)
       
   223     setBackgroundToPressed();
       
   224 }
       
   225 
       
   226 /*!
       
   227     \internal
       
   228 */
       
   229 void HsBookmarkWidget::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   230 {
       
   231     if (contains(event->pos())) {
       
   232         setBackgroundToPressed();
       
   233     } else {
       
   234         setBackgroundToNormal();
       
   235     }
       
   236 }
       
   237 
       
   238 /*!
       
   239     \internal
       
   240 */
       
   241 void HsBookmarkWidget::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   242 {
       
   243     setBackgroundToNormal();
       
   244 
       
   245     if (!contains(event->pos())) {
       
   246         return;
       
   247     }
       
   248 
       
   249     //HbInstantFeedback::play(HSCONFIGURATION_GET(bookmarkWidgetTapFeedbackEffect));
       
   250     
       
   251     QUrl url = QUrl(mBookmarkUrl);
       
   252     qDebug() << "hsBookmarkWidget - opening url - " << url << ", " << stat;
       
   253     bool stat = QDesktopServices::openUrl(url);
       
   254     qDebug() << "hsBookmarkWidget - opened url - " << url << ", " << stat;
       
   255 }
       
   256 
       
   257 /*!
       
   258     \internal
       
   259 */
       
   260 void HsBookmarkWidget::createPrimitives()
       
   261 {
       
   262     // Background
       
   263     if (!mBackground) {
       
   264         HbFrameDrawer *drawer = new HbFrameDrawer(
       
   265             QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
       
   266         mBackground = new HbFrameItem(drawer, this);
       
   267         HbStyle::setItemName(mBackground, QLatin1String("background"));
       
   268     }
       
   269 
       
   270     // Icon
       
   271     if (!mIcon) {
       
   272         mIcon = new HbIconItem(this);
       
   273         HbStyle::setItemName(mIcon, QLatin1String("icon"));
       
   274     }
       
   275     
       
   276     // Text
       
   277     if (!mText) {
       
   278         mText = new HbTextItem(this);
       
   279         HbStyle::setItemName(mText, QLatin1String("text"));
       
   280     }
       
   281                
       
   282     // Touch AreaOK
       
   283     if (!mTouchArea) {
       
   284         mTouchArea = new HbTouchArea(this);
       
   285         mTouchArea->installEventFilter(this);
       
   286         HbStyle::setItemName(mTouchArea, QLatin1String("toucharea"));
       
   287     }
       
   288 }
       
   289 
       
   290 /*!
       
   291     \internal
       
   292 */
       
   293 void HsBookmarkWidget::setBackgroundToNormal()
       
   294 {
       
   295     mBackground->frameDrawer().
       
   296         setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
       
   297 }
       
   298 
       
   299 /*!
       
   300     \internal
       
   301 */
       
   302 void HsBookmarkWidget::setBackgroundToPressed()
       
   303 {
       
   304     mBackground->frameDrawer().
       
   305         setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
       
   306 }
       
   307 
       
   308 /*!
       
   309     \internal
       
   310 */
       
   311 void HsBookmarkWidget::updateContent(const QString title, const HbIcon& icon)
       
   312 {
       
   313     mIcon->setIcon(icon);
       
   314 
       
   315     if (title != "") {
       
   316         mText->setText(title.left(HsBookmarkWidget::maxDisplayLen));     
       
   317     }
       
   318 }
       
   319 
       
   320 QImage HsBookmarkWidget::createImageWithOverlay(const QImage& baseImage, const QPoint& point, const QImage& overlayImage)
       
   321 {
       
   322     QImage imageWithOverlay = QImage(baseImage.size(), QImage::Format_ARGB32_Premultiplied);
       
   323     QPainter painter(&imageWithOverlay);
       
   324 
       
   325     painter.setCompositionMode(QPainter::CompositionMode_Source);
       
   326     painter.fillRect(imageWithOverlay.rect(), Qt::transparent);
       
   327 
       
   328     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
       
   329     painter.drawImage(0, 0, baseImage);
       
   330 
       
   331     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
       
   332     painter.drawImage(point, overlayImage);
       
   333 
       
   334     painter.end();
       
   335 
       
   336     return imageWithOverlay;
       
   337 }
       
   338 
       
   339 HbIcon HsBookmarkWidget::createIconWithOverlay(HbIcon& baseIcon, const QPoint& point, HbIcon& overlayIcon)
       
   340 {
       
   341 
       
   342     // Create overlay image with favicon on top
       
   343     QPixmap basePixmap = baseIcon.pixmap();
       
   344     QPixmap overlayImage = overlayIcon.pixmap();
       
   345     
       
   346     QImage imageWithOverlay = createImageWithOverlay(basePixmap.toImage(), point, overlayImage.toImage());
       
   347     QPixmap pixmapWithOverlay = QPixmap::fromImage(imageWithOverlay);
       
   348    
       
   349     return HbIcon(QIcon(pixmapWithOverlay));
       
   350 }
       
   351 
       
   352 void HsBookmarkWidget::removeFavicon()
       
   353 {
       
   354     QDir faviconDir(mFaviconPath);
       
   355 	
       
   356     faviconDir.remove(mFaviconFileName);
       
   357 }