homescreenapp/widgetplugins/hsshortcutwidgetplugin/src/hsshortcutwidget.cpp
changeset 39 4e8ebe173323
parent 36 cdae8c6c3876
child 46 23b5d6a29cce
--- a/homescreenapp/widgetplugins/hsshortcutwidgetplugin/src/hsshortcutwidget.cpp	Fri Apr 16 14:54:01 2010 +0300
+++ b/homescreenapp/widgetplugins/hsshortcutwidgetplugin/src/hsshortcutwidget.cpp	Mon May 03 12:24:59 2010 +0300
@@ -11,16 +11,18 @@
 *
 * Contributors:
 *
-* Description:  Shortcut widget
+* Description: Home screen shortcut widget.
 *
 */
 
-#include <QGraphicsLinearLayout>
 #include <QGraphicsSceneMouseEvent>
 
-#include <HbStackedLayout>
+#include <HbStyleLoader>
+#include <HbFrameItem>
+#include <HbFrameDrawer>
 #include <HbIconItem>
 #include <HbTextItem>
+#include <HbTouchArea>
 
 #include "hsshortcutwidget.h"
 #include "hsshortcutservice.h"
@@ -30,52 +32,37 @@
 
 /*!
     \class HsShortcutWidget
-    \ingroup group_hsshortcutwidgetprovider
+    \ingroup group_hsshortcutwidgetplugin
     \brief Implementation for the homescreen shortcut widget.
 
     Shortcut can be defined to launch different applications or applications 
     with parameters, for example browser with certain url.
-
-     \section how_to_use_shortcut_plugin How to use HsShortcutWidget
-
-    At the moment code is compiled with homescreen core implementation.
-
 */
 
 /*!
-
-    Constructs shortcut widget object
+    Constructor.
 */
 HsShortcutWidget::HsShortcutWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
   : HbWidget(parent, flags),
-    mShortcutBackgroundItem(0),
-    mShortcutIconItem(0),
-    mShortcutTextItem(0),
-    mMcsId(-1)    
+    mBackground(0), mIcon(0), mText(0), mTouchArea(0),
+    mIsPressed(false),
+    mMcsId(-1), mCaEntry() 
 {
-    setPreferredSize(QSizeF(82,82));
-    resize(82,82);    
+    HbStyleLoader::registerFilePath(":/hsshortcutwidget.widgetml");
+    HbStyleLoader::registerFilePath(":/hsshortcutwidget.css");
 }
 
 /*!
-    \fn HsShortcutWidget::~HsShortcutWidget()
-
-    Destructor
+    Destructor.
 */
 HsShortcutWidget::~HsShortcutWidget()
 {
+    HbStyleLoader::unregisterFilePath(":/hsshortcutwidget.widgetml");
+    HbStyleLoader::unregisterFilePath(":/hsshortcutwidget.css");
 }
 
 /*!
-    Getter for menu content service id
-*/
-int HsShortcutWidget::mcsId() const
-{
-    return mMcsId;
-}
-
-/*!
-    Sets menu content service id to \a mcsId
+    Sets the menu content service id.
 */
 void HsShortcutWidget::setMcsId(int mcsId)
 {
@@ -83,194 +70,186 @@
 }
 
 /*!
-    \fn void HsShortcutWidget::onEntryChanged(const CaEntry &entry, ChangeType changeType)
-    
-    Invoked when \a entry has changed with a \a changeType event.
+    Returns the menu content service id.
 */
-void HsShortcutWidget::onEntryChanged(const CaEntry &entry, ChangeType changeType)
+int HsShortcutWidget::mcsId() const
 {
+    return mMcsId;
+}
 
-    switch(changeType) {
-        case RemoveChangeType: {
-            emit finished();
+/*!
+    Filters touch area events.
+*/
+bool HsShortcutWidget::eventFilter(QObject *watched, QEvent *event)
+{
+    Q_UNUSED(watched)
+
+    switch (event->type()) {
+        case QEvent::GraphicsSceneMousePress:
+            handleMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
             break;
-        }
-        case UpdateChangeType: {
-            hideOrShowWidget(entry.flags());
-            mShortcutIconItem->setIcon(fetchIcon(mMcsId));
+
+        case QEvent::GraphicsSceneMouseRelease:
+            handleMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
+            break;
+
+        default:            
+            break;
+    }
+
+    return true;
+}
 
-            QString text = fetchText(mMcsId);
-            mShortcutTextItem->setText(text);            
-            break;
-        }
-        default:
-            break;
+/*!
+    Initializes this widget.
+*/
+void HsShortcutWidget::onInitialize()
+{
+    mCaEntry = CaService::instance()->getEntry(mMcsId);
+    if (!mCaEntry.isNull()) {
+        createCaNotifier(); 
+        updatePrimitives();
+    } else {
+        emit finished();
     }
 }
 
 /*!
-    \fn void HsShortcutWidget::onInitialize()
-
-    Initializes shortcut
+    Wakes up this widget.
 */
-void HsShortcutWidget::onInitialize()
+void HsShortcutWidget::onShow()
 {
-    constructUI();
-
-    if (!HsShortcutService::instance() || mMcsId < 0) {
-        emit finished();
-    }
-
-    createCaNotifier(mMcsId);
-    
-    mShortcutIconItem->setIcon(fetchIcon(mMcsId));
-
-    QString text = fetchText(mMcsId);
-    mShortcutTextItem->setText(text);
-
-    setEnabled(true);
+    updateVisibility();
 }
 
 /*!
-    \fn void HsClockWidget::show()
-
-    Shows the widget
-*/
-void HsShortcutWidget::onShow()
-{
-    hideOrShowWidget(fetchEntryFlags(mMcsId));
-}
-
-/*!
-    \fn void HsClockWidget::show()
-
-    Hides the widget
+    Puts this widget in quiescent state.
 */
 void HsShortcutWidget::onHide()
 {
 }
 
 /*!
-    \fn void HsShortcutWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
-
-    Executes configured action
+    Invoked when \a entry has changed with a \a changeType.
 */
-void HsShortcutWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
-{   
-    if (!contains(event->pos())) {
-        return;
-    }
+void HsShortcutWidget::onEntryChanged(const CaEntry &entry, ChangeType changeType)
+{
+    Q_UNUSED(entry)
+
+    mCaEntry = CaService::instance()->getEntry(mMcsId);
 
-    CaEntry *entry = CaService::instance()->getEntry(mMcsId);
-    if (!entry) {
-        return;
-    }
-
-    if (entry->role() == ItemEntryRole) {
-        CaService::instance()->executeCommand(mMcsId);
-    } else {
-        executeCollectionAction(mMcsId, entry->entryTypeName());
+    switch(changeType) {
+        case RemoveChangeType:
+            emit finished();
+            break;
+        case UpdateChangeType:
+            updateVisibility();
+            updatePrimitives();
+            break;
+        default:
+            break;
     }
 }
 
 /*!
-    \fn void HsShortcutWidget::constructUI()
-
-    Constructs and initializes ui parts
+    Mouse press handler.
 */
-void HsShortcutWidget::constructUI()
-{
-    mShortcutBackgroundItem = new HbIconItem("hs_shortcut_bg");
-    mShortcutBackgroundItem->setAlignment(Qt::AlignCenter);
-    mShortcutIconItem = new HbIconItem;
-    mShortcutIconItem->setAlignment(Qt::AlignCenter);
-    mShortcutTextItem = new HbTextItem;
-    mShortcutTextItem->setAlignment(Qt::AlignCenter);
+void HsShortcutWidget::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
+{   
+    Q_UNUSED(event)
 
-    HbStackedLayout *mainLayout = new HbStackedLayout;
-    mainLayout->addItem(mShortcutBackgroundItem);
-    
-    QGraphicsLinearLayout *contentLayout = 
-        new QGraphicsLinearLayout(Qt::Vertical);
-    contentLayout->setContentsMargins(4, 4, 4, 4);
-    contentLayout->addItem(mShortcutIconItem);
-    contentLayout->addItem(mShortcutTextItem);
-    mainLayout->addItem(contentLayout);
-
-    setLayout(mainLayout);
-}
-
-/*!
-    \internal
-*/
-HbIcon HsShortcutWidget::fetchIcon(int aShortcutId)
-{
-	CaEntry *entry = CaService::instance()->getEntry(aShortcutId);
-    if (!entry) {
-        return HbIcon();
-    }
-    return entry->makeIcon();    
+    mIsPressed = true;
+    updatePrimitives();
 }
 
 /*!
-    \internal
+    Mouse release handler.
 */
-QString HsShortcutWidget::fetchText(int aShortcutId)
+void HsShortcutWidget::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
-	CaEntry *entry = CaService::instance()->getEntry(aShortcutId);
-    if (!entry) {
-        return QString();
-    }
-    return entry->text();
-}
-
-/*!
-    \internal
-*/
-void HsShortcutWidget::executeCollectionAction(
-        int shortcutId, const QString& collectionType)
-{
-    HsShortcutService::instance()->executeCollectionAction(
-            shortcutId, collectionType);
-}
+    mIsPressed = false;
+    updatePrimitives();
 
-/*!
-    \internal
-*/
-void HsShortcutWidget::createCaNotifier(int aShortcutId)
-{
-    CaNotifierFilter filter;
-    filter.setIds(QList<int>() << aShortcutId);
-    
-    CaNotifier *itemNotifier = CaService::instance()->createNotifier(filter);
-    itemNotifier->setParent(this);
+    if (!contains(event->pos())) {
+        return;
+    }
 
-    connect(itemNotifier,
-        SIGNAL(entryChanged(CaEntry,ChangeType)),
-        SLOT(onEntryChanged(CaEntry,ChangeType)),Qt::QueuedConnection);
-}
-
-/*!
-    \internal
-*/
-void HsShortcutWidget::hideOrShowWidget(EntryFlags aEntryFlags)
-{
-    if (aEntryFlags.testFlag(MissingEntryFlag)) {
-        hide();
+    if (mCaEntry->role() == ItemEntryRole) {
+        CaService::instance()->executeCommand(mMcsId);
     } else {
-        show();
+        HsShortcutService::instance()->executeCollectionAction(mMcsId, mCaEntry->entryTypeName());
     }
 }
 
 /*!
     \internal
 */
-EntryFlags HsShortcutWidget::fetchEntryFlags(int aShortcutId)
+void HsShortcutWidget::updatePrimitives()
+{   
+    // Background
+    if (!mBackground) {
+        HbFrameDrawer *drawer = new HbFrameDrawer(
+            QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
+        mBackground = new HbFrameItem(drawer, this);
+        HbStyle::setItemName(mBackground, QLatin1String("background"));
+    }
+    if (mIsPressed) {        
+        mBackground->frameDrawer().setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
+    } else {
+        mBackground->frameDrawer().setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
+    }
+
+    // Icon
+    if (!mIcon) {
+        mIcon = new HbIconItem(this);
+        HbStyle::setItemName(mIcon, QLatin1String("icon"));
+    }
+    mIcon->setIcon(mCaEntry->makeIcon());
+    
+    // Text
+    QString text = mCaEntry->text();
+    if (!text.isNull()) {
+        if (!mText) {
+            mText = new HbTextItem(this);
+            HbStyle::setItemName(mText, QLatin1String("text"));
+        }
+        mText->setText(text);
+    } else {
+        if (mText) {
+            delete mText;
+            mText = 0;
+        }
+    }
+
+    // Touch Area
+    if (!mTouchArea) {
+        mTouchArea = new HbTouchArea(this);
+        mTouchArea->installEventFilter(this);
+        HbStyle::setItemName(mTouchArea, QLatin1String("toucharea"));
+    }
+}
+
+/*!
+    \internal
+*/
+void HsShortcutWidget::createCaNotifier()
 {
-    CaEntry *entry = CaService::instance()->getEntry(aShortcutId);
-    EntryFlags entryFlags = 0;
-    if (entry) {
-        entryFlags = entry->flags();
-    }
-    return entryFlags;
+    CaNotifierFilter filter;
+    filter.setIds(QList<int>() << mMcsId);
+    
+    CaNotifier *notifier = CaService::instance()->createNotifier(filter);
+    notifier->setParent(this);
+
+    connect(notifier,
+        SIGNAL(entryChanged(CaEntry,ChangeType)),
+        SLOT(onEntryChanged(CaEntry,ChangeType)), 
+        Qt::QueuedConnection);
 }
+
+/*!
+    \internal
+*/
+void HsShortcutWidget::updateVisibility()
+{
+    setVisible(!mCaEntry->flags().testFlag(MissingEntryFlag));
+}