homescreenapp/widgetplugins/hsshortcutwidgetplugin/src/hsshortcutwidget.cpp
changeset 46 23b5d6a29cce
parent 39 4e8ebe173323
child 51 4785f57bf3d4
equal deleted inserted replaced
39:4e8ebe173323 46:23b5d6a29cce
    21 #include <HbFrameItem>
    21 #include <HbFrameItem>
    22 #include <HbFrameDrawer>
    22 #include <HbFrameDrawer>
    23 #include <HbIconItem>
    23 #include <HbIconItem>
    24 #include <HbTextItem>
    24 #include <HbTextItem>
    25 #include <HbTouchArea>
    25 #include <HbTouchArea>
       
    26 #include <HbInstantFeedback>
    26 
    27 
    27 #include "hsshortcutwidget.h"
    28 #include "hsshortcutwidget.h"
    28 #include "hsshortcutservice.h"
    29 #include "hsshortcutservice.h"
    29 
    30 #include "hsapp_defs.h"
    30 #include "caservice.h"
    31 #include "caservice.h"
    31 #include "canotifier.h"
    32 #include "canotifier.h"
       
    33 #include "caquery.h"
    32 
    34 
    33 /*!
    35 /*!
    34     \class HsShortcutWidget
    36     \class HsShortcutWidget
    35     \ingroup group_hsshortcutwidgetplugin
    37     \ingroup group_hsshortcutwidgetplugin
    36     \brief Implementation for the homescreen shortcut widget.
    38     \brief Implementation for the homescreen shortcut widget.
    37 
    39 
    38     Shortcut can be defined to launch different applications or applications 
    40     Shortcut can be defined to launch different applications or applications
    39     with parameters, for example browser with certain url.
    41     with parameters, for example browser with certain url.
    40 */
    42 */
    41 
    43 
    42 /*!
    44 /*!
    43     Constructor.
    45     Constructor.
    44 */
    46 */
    45 HsShortcutWidget::HsShortcutWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
    47 HsShortcutWidget::HsShortcutWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
    46   : HbWidget(parent, flags),
    48   : HbWidget(parent, flags),
    47     mBackground(0), mIcon(0), mText(0), mTouchArea(0),
    49     mBackground(0), mIcon(0), mText(0), mTouchArea(0),
    48     mIsPressed(false),
    50     mCaEntryId(-1), mCaEntryRole(ItemEntryRole)
    49     mMcsId(-1), mCaEntry() 
       
    50 {
    51 {
    51     HbStyleLoader::registerFilePath(":/hsshortcutwidget.widgetml");
    52     HbStyleLoader::registerFilePath(":/hsshortcutwidget.widgetml");
    52     HbStyleLoader::registerFilePath(":/hsshortcutwidget.css");
    53     HbStyleLoader::registerFilePath(":/hsshortcutwidget.css");
       
    54 
       
    55     createPrimitives();
    53 }
    56 }
    54 
    57 
    55 /*!
    58 /*!
    56     Destructor.
    59     Destructor.
    57 */
    60 */
    62 }
    65 }
    63 
    66 
    64 /*!
    67 /*!
    65     Sets the menu content service id.
    68     Sets the menu content service id.
    66 */
    69 */
    67 void HsShortcutWidget::setMcsId(int mcsId)
    70 void HsShortcutWidget::setCaEntryId(int caEntryId)
    68 {
    71 {
    69     mMcsId = mcsId;
    72     mCaEntryId = caEntryId;
    70 }
    73 }
    71 
    74 
    72 /*!
    75 /*!
    73     Returns the menu content service id.
    76     Returns the menu content service id.
    74 */
    77 */
    75 int HsShortcutWidget::mcsId() const
    78 int HsShortcutWidget::caEntryId() const
    76 {
    79 {
    77     return mMcsId;
    80     return mCaEntryId;
       
    81 }
       
    82 
       
    83 /*!
       
    84     Sets the application's UID this shortcut points to.
       
    85 */
       
    86 void HsShortcutWidget::setUid(const QString &uid)
       
    87 {
       
    88     mUid = uid;
       
    89 }
       
    90 
       
    91 /*!
       
    92     Returns the application's UID this shortcut points to.
       
    93 */
       
    94 QString HsShortcutWidget::uid() const
       
    95 {
       
    96     return mUid;
    78 }
    97 }
    79 
    98 
    80 /*!
    99 /*!
    81     Filters touch area events.
   100     Filters touch area events.
    82 */
   101 */
    85     Q_UNUSED(watched)
   104     Q_UNUSED(watched)
    86 
   105 
    87     switch (event->type()) {
   106     switch (event->type()) {
    88         case QEvent::GraphicsSceneMousePress:
   107         case QEvent::GraphicsSceneMousePress:
    89             handleMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
   108             handleMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
    90             break;
   109             return true;
    91 
   110         case QEvent::GraphicsSceneMouseMove:
       
   111             handleMouseMoveEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
   112             return true;
    92         case QEvent::GraphicsSceneMouseRelease:
   113         case QEvent::GraphicsSceneMouseRelease:
    93             handleMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
   114             handleMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
       
   115             return true;
       
   116         default:
    94             break;
   117             break;
    95 
   118     }
    96         default:            
   119 
    97             break;
   120     return false;
    98     }
       
    99 
       
   100     return true;
       
   101 }
   121 }
   102 
   122 
   103 /*!
   123 /*!
   104     Initializes this widget.
   124     Initializes this widget.
   105 */
   125 */
   106 void HsShortcutWidget::onInitialize()
   126 void HsShortcutWidget::onInitialize()
   107 {
   127 {
   108     mCaEntry = CaService::instance()->getEntry(mMcsId);
   128     QSharedPointer<CaEntry> caEntry;
   109     if (!mCaEntry.isNull()) {
   129 
   110         createCaNotifier(); 
   130     if(-1 == mCaEntryId) {
   111         updatePrimitives();
   131         //Shortcut can be pre installed to Homescreen's DB by setting
       
   132         //application UID for its preference. Entry Id is retrieved
       
   133         //from CA by UID.
       
   134         CaQuery query;
       
   135         query.setEntryTypeNames(QStringList(applicationTypeName()));
       
   136         //Convert UID from hex to ten base, because CA t UIDs in ten base.
       
   137         bool ok;
       
   138         int hexBaseInteger = mUid.toInt(&ok, 0); // Qt Assistant: If base is 0, the C language convention is used:
       
   139                                              // If the string begins with "0x", base 16 is used.
       
   140         QString tenBaseString = QString::number(hexBaseInteger);
       
   141         query.setAttribute(applicationUidEntryKey(), tenBaseString);
       
   142         QList< QSharedPointer<CaEntry> > appEntries = CaService::instance()->getEntries(query);
       
   143 
       
   144         //Verify that entry's UID is what we want since we get all application entries
       
   145         //if UID did not match in getEntries() above.
       
   146         if (!appEntries.isEmpty()
       
   147             && appEntries.first()->attribute(applicationUidEntryKey()) == tenBaseString) {
       
   148             caEntry = appEntries.first();
       
   149             mCaEntryId = caEntry->id();
       
   150 			//Save caEntryId to Homescreen database
       
   151             emit setPreferences(QStringList() << QLatin1String("caEntryId"));
       
   152         }
       
   153     } else {
       
   154         caEntry = CaService::instance()->getEntry(mCaEntryId);
       
   155     }
       
   156 
       
   157     if (!caEntry.isNull()) {
       
   158         createCaNotifier();
       
   159         updateContent(*caEntry.data());
   112     } else {
   160     } else {
   113         emit finished();
   161         emit finished();
   114     }
   162     }
   115 }
   163 }
   116 
   164 
   128 void HsShortcutWidget::onHide()
   176 void HsShortcutWidget::onHide()
   129 {
   177 {
   130 }
   178 }
   131 
   179 
   132 /*!
   180 /*!
   133     Invoked when \a entry has changed with a \a changeType.
   181     \internal
   134 */
       
   135 void HsShortcutWidget::onEntryChanged(const CaEntry &entry, ChangeType changeType)
       
   136 {
       
   137     Q_UNUSED(entry)
       
   138 
       
   139     mCaEntry = CaService::instance()->getEntry(mMcsId);
       
   140 
       
   141     switch(changeType) {
       
   142         case RemoveChangeType:
       
   143             emit finished();
       
   144             break;
       
   145         case UpdateChangeType:
       
   146             updateVisibility();
       
   147             updatePrimitives();
       
   148             break;
       
   149         default:
       
   150             break;
       
   151     }
       
   152 }
       
   153 
       
   154 /*!
       
   155     Mouse press handler.
       
   156 */
   182 */
   157 void HsShortcutWidget::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
   183 void HsShortcutWidget::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
   158 {   
   184 {
   159     Q_UNUSED(event)
   185     Q_UNUSED(event)
   160 
   186     setBackgroundToPressed();
   161     mIsPressed = true;
   187 }
   162     updatePrimitives();
   188 
   163 }
   189 /*!
   164 
   190     \internal
   165 /*!
   191 */
   166     Mouse release handler.
   192 void HsShortcutWidget::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   193 {
       
   194     if (contains(event->pos())) {
       
   195         setBackgroundToPressed();
       
   196     } else {
       
   197         setBackgroundToNormal();
       
   198     }
       
   199 }
       
   200 
       
   201 /*!
       
   202     \internal
   167 */
   203 */
   168 void HsShortcutWidget::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   204 void HsShortcutWidget::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   169 {
   205 {
   170     mIsPressed = false;
   206     setBackgroundToNormal();
   171     updatePrimitives();
       
   172 
   207 
   173     if (!contains(event->pos())) {
   208     if (!contains(event->pos())) {
   174         return;
   209         return;
   175     }
   210     }
   176 
   211 
   177     if (mCaEntry->role() == ItemEntryRole) {
   212     HbInstantFeedback::play(HbFeedback::BasicItem);
   178         CaService::instance()->executeCommand(mMcsId);
   213     
       
   214     if (mCaEntryRole == ItemEntryRole) {
       
   215         CaService::instance()->executeCommand(mCaEntryId);
   179     } else {
   216     } else {
   180         HsShortcutService::instance()->executeCollectionAction(mMcsId, mCaEntry->entryTypeName());
   217         HsShortcutService::instance()->executeCollectionAction(mCaEntryId, mCaEntryTypeName);
   181     }
   218     }
   182 }
   219 }
   183 
   220 
   184 /*!
   221 /*!
   185     \internal
   222     \internal
   186 */
   223 */
   187 void HsShortcutWidget::updatePrimitives()
   224 void HsShortcutWidget::createPrimitives()
   188 {   
   225 {
   189     // Background
   226     // Background
   190     if (!mBackground) {
   227     if (!mBackground) {
   191         HbFrameDrawer *drawer = new HbFrameDrawer(
   228         HbFrameDrawer *drawer = new HbFrameDrawer(
   192             QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
   229             QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
   193         mBackground = new HbFrameItem(drawer, this);
   230         mBackground = new HbFrameItem(drawer, this);
   194         HbStyle::setItemName(mBackground, QLatin1String("background"));
   231         HbStyle::setItemName(mBackground, QLatin1String("background"));
   195     }
   232     }
   196     if (mIsPressed) {        
       
   197         mBackground->frameDrawer().setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
       
   198     } else {
       
   199         mBackground->frameDrawer().setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
       
   200     }
       
   201 
   233 
   202     // Icon
   234     // Icon
   203     if (!mIcon) {
   235     if (!mIcon) {
   204         mIcon = new HbIconItem(this);
   236         mIcon = new HbIconItem(this);
   205         HbStyle::setItemName(mIcon, QLatin1String("icon"));
   237         HbStyle::setItemName(mIcon, QLatin1String("icon"));
   206     }
   238     }
   207     mIcon->setIcon(mCaEntry->makeIcon());
   239 
   208     
       
   209     // Text
   240     // Text
   210     QString text = mCaEntry->text();
   241     if (!mText) {
   211     if (!text.isNull()) {
   242         mText = new HbTextItem(this);
   212         if (!mText) {
   243         HbStyle::setItemName(mText, QLatin1String("text"));
   213             mText = new HbTextItem(this);
       
   214             HbStyle::setItemName(mText, QLatin1String("text"));
       
   215         }
       
   216         mText->setText(text);
       
   217     } else {
       
   218         if (mText) {
       
   219             delete mText;
       
   220             mText = 0;
       
   221         }
       
   222     }
   244     }
   223 
   245 
   224     // Touch Area
   246     // Touch Area
   225     if (!mTouchArea) {
   247     if (!mTouchArea) {
   226         mTouchArea = new HbTouchArea(this);
   248         mTouchArea = new HbTouchArea(this);
   230 }
   252 }
   231 
   253 
   232 /*!
   254 /*!
   233     \internal
   255     \internal
   234 */
   256 */
       
   257 void HsShortcutWidget::updateContent(const CaEntry &caEntry)
       
   258 {
       
   259     mCaEntryRole = caEntry.role();
       
   260     mCaEntryFlags = caEntry.flags();
       
   261     mCaEntryTypeName = caEntry.entryTypeName();
       
   262     mIcon->setIcon(caEntry.makeIcon());
       
   263     mText->setText(caEntry.text());
       
   264 }
       
   265 
       
   266 /*!
       
   267     \internal
       
   268 */
       
   269 void HsShortcutWidget::updateVisibility()
       
   270 {
       
   271     setVisible(!mCaEntryFlags.testFlag(MissingEntryFlag));
       
   272 }
       
   273 
       
   274 /*!
       
   275     \internal
       
   276 */
       
   277 void HsShortcutWidget::setBackgroundToNormal()
       
   278 {
       
   279     mBackground->frameDrawer().
       
   280         setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
       
   281 }
       
   282 
       
   283 /*!
       
   284     \internal
       
   285 */
       
   286 void HsShortcutWidget::setBackgroundToPressed()
       
   287 {
       
   288     mBackground->frameDrawer().
       
   289         setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
       
   290 }
       
   291 
       
   292 /*!
       
   293     \internal
       
   294 */
   235 void HsShortcutWidget::createCaNotifier()
   295 void HsShortcutWidget::createCaNotifier()
   236 {
   296 {
   237     CaNotifierFilter filter;
   297     CaNotifierFilter filter;
   238     filter.setIds(QList<int>() << mMcsId);
   298     filter.setIds(QList<int>() << mCaEntryId);
   239     
   299 
   240     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
   300     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
   241     notifier->setParent(this);
   301     notifier->setParent(this);
   242 
   302 
   243     connect(notifier,
   303     connect(notifier,
   244         SIGNAL(entryChanged(CaEntry,ChangeType)),
   304         SIGNAL(entryChanged(CaEntry,ChangeType)),
   245         SLOT(onEntryChanged(CaEntry,ChangeType)), 
   305         SLOT(onEntryChanged(CaEntry,ChangeType)),
   246         Qt::QueuedConnection);
   306         Qt::QueuedConnection);
   247 }
   307 }
   248 
   308 
   249 /*!
   309 /*!
   250     \internal
   310     \internal
   251 */
   311 */
   252 void HsShortcutWidget::updateVisibility()
   312 void HsShortcutWidget::onEntryChanged(const CaEntry &caEntry, ChangeType changeType)
   253 {
   313 {
   254     setVisible(!mCaEntry->flags().testFlag(MissingEntryFlag));
   314     updateContent(caEntry);
   255 }
   315     updateVisibility();
       
   316 
       
   317     if ( changeType == RemoveChangeType && !(caEntry.flags() & MissingEntryFlag)) {
       
   318         //genuinely removed
       
   319         emit finished();
       
   320     }
       
   321 }