homescreenapp/stateplugins/hsmenuworkerstateplugin/src/hspreviewhswidgetstate.cpp
changeset 62 341166945d65
parent 55 03646e8da489
child 63 52b0f64eeb51
child 77 4b195f3bea29
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
    15  *
    15  *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 #include <hblabel.h>
    19 #include <hblabel.h>
    20 //#include <hsiwidgetprovider.h>
       
    21 #include <hbmessagebox.h>
    20 #include <hbmessagebox.h>
    22 #include <qstatemachine.h>
    21 #include <qstatemachine.h>
    23 #include <hbaction.h>
    22 #include <hbaction.h>
    24 #include <HbDocumentLoader>
    23 #include <HbDocumentLoader>
    25 #include <hbdialog.h>
    24 #include <hbdialog.h>
       
    25 #include <HbIcon>
       
    26 #include <HbLabel>
    26 #include <hbwidget.h>
    27 #include <hbwidget.h>
    27 #include <hbscrollarea.h>
    28 #include <hbscrollarea.h>
    28 #include <hbscrollbar.h>
    29 #include <hbscrollbar.h>
    29 #include <QtAlgorithms>
    30 #include <QtAlgorithms>
    30 #include <hbnotificationdialog.h>
    31 #include <hbnotificationdialog.h>
    42 #include "canotifierfilter.h"
    43 #include "canotifierfilter.h"
    43 #include "caservice.h"
    44 #include "caservice.h"
    44 #include "hsdomainmodel_global.h"
    45 #include "hsdomainmodel_global.h"
    45 #include <hscontentservice.h>
    46 #include <hscontentservice.h>
    46 
    47 
    47 const char HS_PREVIEW_HS_WIDGET_STATE[] = "HsPreviewHSWidgetState";
       
    48 
    48 
    49 /*!
    49 /*!
    50  \class HsPreviewHSWidgetState
    50  \class HsPreviewHSWidgetState
    51  \ingroup group_hsworkerstateplugin
    51  \ingroup group_hsworkerstateplugin
    52  \brief Application Library State.
    52  \brief Application Library State.
    55  \lib ?library
    55  \lib ?library
    56  */
    56  */
    57 
    57 
    58 /*!
    58 /*!
    59  Constructor
    59  Constructor
    60  \param parent: parent state
    60  \param parent Parent state.
    61  \retval void
    61  \retval void
    62  */
    62  */
    63 HsPreviewHSWidgetState::HsPreviewHSWidgetState(QState *parent) :
    63 HsPreviewHSWidgetState::HsPreviewHSWidgetState(QState *parent) :
    64     HsMenuBaseState(HS_PREVIEW_HS_WIDGET_STATE, parent),
    64     QState(parent),
    65     mPopupDialog(0),
    65     mPreviewDialog(0),
    66     mNotifier(0),
    66     mNotifier(0),
    67     mScrollArea(0),
       
    68     mWidget(0),
    67     mWidget(0),
    69     mEntryId(0),
    68     mEntryId(0),
    70     mCorruptedMessage(0)
    69     mCorruptedMessage(0)
    71 {
    70 {
    72     requestServices(QList<QVariant> () << CONTENT_SERVICE_KEY);
    71     setObjectName("/HsPreviewHSWidgetState");
       
    72     if (this->parent()) {
       
    73         setObjectName(this->parent()->objectName() + objectName());
       
    74     }
    73     connect(this, SIGNAL(exited()), SLOT(cleanUp()));
    75     connect(this, SIGNAL(exited()), SLOT(cleanUp()));
    74 }
    76 }
    75 
    77 
    76 /*!
    78 /*!
    77  Destructor.
    79  Destructor.
    91  or shortcut.
    93  or shortcut.
    92  \param contentService: service for adding widgets
    94  \param contentService: service for adding widgets
    93  \param shortcutService: service for adding shortcuts
    95  \param shortcutService: service for adding shortcuts
    94  \retval void
    96  \retval void
    95  */
    97  */
    96 #ifdef COVERAGE_MEASUREMENT
       
    97 #pragma CTC SKIP
       
    98 #endif //COVERAGE_MEASUREMENT
       
    99 void HsPreviewHSWidgetState::onEntry(QEvent *event)
    98 void HsPreviewHSWidgetState::onEntry(QEvent *event)
   100 {
    99 {
   101     HSMENUTEST_FUNC_ENTRY("HsPreviewHSWidgetState::onEntry");
   100     HSMENUTEST_FUNC_ENTRY("HsPreviewHSWidgetState::onEntry");
   102     HsMenuBaseState::onEntry(event);
   101     QState::onEntry(event);
   103     HsMenuEvent *menuEvent = static_cast<HsMenuEvent *>(event);
   102     HsMenuEvent *menuEvent = static_cast<HsMenuEvent *>(event);
   104     QVariantMap data = menuEvent->data();
   103     QVariantMap data = menuEvent->data();
   105 
   104 
   106     mEntryId = data.value(itemIdKey()).toInt();
   105     mEntryId = data.value(itemIdKey()).toInt();
   107 
   106 
   108     QVariantHash widgetData;
   107     QVariantHash widgetData;
   109     widgetData.insert(LIBRARY, data.value(widgetLibraryAttributeName()).toString());
   108     widgetData.insert(LIBRARY, data.value(widgetLibraryAttributeName()).toString());
   110     widgetData.insert(URI, data.value(widgetUriAttributeName()).toString());
   109     widgetData.insert(URI, data.value(widgetUriAttributeName()).toString());
   111 
   110 
   112     QScopedPointer<HsWidgetHost> widget(contentService()->createWidgetForPreview(widgetData));
   111     mWidget.reset(
   113 
   112         HsContentService::instance()->createWidgetForPreview(widgetData));
   114     if (widget) {
   113 
   115         widget->setMinimumSize(widget->preferredWidth(),widget->preferredHeight());
   114     if (!mWidget.isNull()) {
   116 
   115         
   117         HbDocumentLoader loader;
   116         QSharedPointer<CaEntry> entry = 
   118 
   117             CaService::instance()->getEntry(mEntryId);
   119         bool loadStatusOk = false;
   118         
   120         mObjectList = 
   119         mPreviewDialog = buildPreviewDialog(*entry);
   121             loader.load(HS_WIDGET_PREVIEW_DIALOG_LAYOUT, &loadStatusOk);
   120         
   122         Q_ASSERT_X(loadStatusOk,
   121         if (mPreviewDialog != NULL) {
   123             HS_WIDGET_PREVIEW_DIALOG_LAYOUT,
       
   124                "Error while loading docml file.");
       
   125 
       
   126         mPopupDialog = 
       
   127             qobject_cast<HbDialog*>(
       
   128                 loader.findWidget(HS_WIDGET_PREVIEW_DIALOG_NAME));
       
   129 
       
   130         mScrollArea =
       
   131             qobject_cast<HbScrollArea*>(
       
   132                 loader.findWidget(HS_WIDGET_PREVIEW_SCROLL_AREA_NAME));
       
   133 
       
   134         // set parent to actions to delete them together with dialog
       
   135         mPopupDialog->actions()[0]->setParent(mPopupDialog);
       
   136         mPopupDialog->actions()[1]->setParent(mPopupDialog);
       
   137 
       
   138         if (mPopupDialog != NULL && mScrollArea != NULL) {
       
   139             mPopupDialog->setContentWidget(mScrollArea); // ownership transferred
       
   140             mPopupDialog->setTimeout(HbPopup::NoTimeout);
       
   141             mPopupDialog->setAttribute(Qt::WA_DeleteOnClose, true);
       
   142             mScrollArea->verticalScrollBar()->setInteractive(true);
       
   143             mScrollArea->horizontalScrollBar()->setInteractive(true);
       
   144             mWidget = widget.take(); // ownership transfered
       
   145             mScrollArea->setContentWidget(mWidget); // ownership transferred
       
   146             
       
   147             subscribeForMemoryCardRemove();
   122             subscribeForMemoryCardRemove();
   148             mWidget->startWidget();
       
   149             // Launch popup asyncronously
   123             // Launch popup asyncronously
   150             mPopupDialog->open(this, SLOT(previewDialogFinished(HbAction*)));    
   124             mPreviewDialog->open(this, SLOT(previewDialogFinished(HbAction*)));    
   151         }
   125         }
   152 
       
   153     } else {
   126     } else {
   154         subscribeForMemoryCardRemove();
   127         subscribeForMemoryCardRemove();
   155         showMessageWidgetCorrupted();
   128         showMessageWidgetCorrupted();
   156     }
   129     }
   157 
   130 
   158     HSMENUTEST_FUNC_EXIT("HsPreviewHSWidgetState::onEntry");
   131     HSMENUTEST_FUNC_EXIT("HsPreviewHSWidgetState::onEntry");
   159 }
   132 }
   160 #ifdef COVERAGE_MEASUREMENT
       
   161 #pragma CTC ENDSKIP
       
   162 #endif //COVERAGE_MEASUREMENT
       
   163 
   133 
   164 /*!
   134 /*!
   165  Slot launched after state has exited and in destructor.
   135  Slot launched after state has exited and in destructor.
   166  \retval void
   136  \retval void
   167  */
   137  */
   168 void HsPreviewHSWidgetState::cleanUp()
   138 void HsPreviewHSWidgetState::cleanUp()
   169 {
   139 {
   170     // Close popups if App key was pressed or memory card removed
   140     // Close popups if App key was pressed or memory card removed
   171     if (mPopupDialog) {
   141     if (mPreviewDialog) {
   172         disconnect(mPopupDialog, SIGNAL(finished(HbAction*)), this, SLOT(previewDialogFinished(HbAction*)));
   142         disconnect(mPreviewDialog, SIGNAL(finished(HbAction*)), 
   173         mPopupDialog->close();
   143             this, SLOT(previewDialogFinished(HbAction*)));
   174         mPopupDialog = NULL;
   144         mPreviewDialog->close();
       
   145         mPreviewDialog = NULL;
   175     }
   146     }
   176 
   147 
   177     if (mCorruptedMessage) {
   148     if (mCorruptedMessage) {
   178         disconnect(mCorruptedMessage, SIGNAL(finished(HbAction*)), this, SLOT(messageWidgetCorruptedFinished(HbAction*)));
   149         disconnect(mCorruptedMessage, SIGNAL(finished(HbAction*)), 
       
   150             this, SLOT(messageWidgetCorruptedFinished(HbAction*)));
   179         mCorruptedMessage->close();
   151         mCorruptedMessage->close();
   180         mCorruptedMessage = NULL;
   152         mCorruptedMessage = NULL;
   181     }
   153     }
   182 
   154 
   183     mScrollArea = NULL;
   155     mWidget.reset();
   184     mWidget = NULL;
       
   185 
   156 
   186     disconnect(mNotifier,
   157     disconnect(mNotifier,
   187                SIGNAL(entryChanged(CaEntry,ChangeType)),
   158                SIGNAL(entryChanged(CaEntry,ChangeType)),
   188                this, SIGNAL(exit()));
   159                this, SIGNAL(exit()));
   189 
   160 
   195  Slot launched on dismissing the preview dialog
   166  Slot launched on dismissing the preview dialog
   196  \retval void
   167  \retval void
   197  */
   168  */
   198 void HsPreviewHSWidgetState::previewDialogFinished(HbAction* finishedAction)
   169 void HsPreviewHSWidgetState::previewDialogFinished(HbAction* finishedAction)
   199 {
   170 {
   200     if (finishedAction == mPopupDialog->actions().value(0)) {
   171     if (finishedAction == mPreviewDialog->actions().value(0)) {
   201         mWidget->hideWidget();
   172 
   202         mScrollArea->takeContentWidget();
       
   203         HsScene::instance()->activePage()->addNewWidget(
   173         HsScene::instance()->activePage()->addNewWidget(
   204             mWidget); // ownership transferred
   174             mWidget.take()); // ownership transferred
   205         HbNotificationDialog *notificationDialog = new HbNotificationDialog();
   175         HbNotificationDialog *notificationDialog = new HbNotificationDialog();
   206         notificationDialog->setAttribute(Qt::WA_DeleteOnClose);
   176         notificationDialog->setAttribute(Qt::WA_DeleteOnClose);
   207         notificationDialog->setTitle(hbTrId(
   177         notificationDialog->setTitle(hbTrId(
   208                                      "txt_applib_dpophead_added_to_homescreen") );
   178                                      "txt_applib_dpophead_added_to_homescreen") );
   209         notificationDialog->show();
   179         notificationDialog->show();
   210     } else {
   180     } else {
   211             mScrollArea->takeContentWidget();
       
   212             mWidget->remove();
   181             mWidget->remove();
   213             mWidget = NULL;
   182             mWidget.take();   
       
   183             mWidget.reset();
   214     }
   184     }
   215     emit exit();
   185     emit exit();
   216 }
   186 }
   217 
   187 
   218 /*!
   188 /*!
   234 
   204 
   235 /*!
   205 /*!
   236  Shows message about corrupted widget library. Deletes widget eventually
   206  Shows message about corrupted widget library. Deletes widget eventually
   237  \retval void
   207  \retval void
   238  */
   208  */
   239 #ifdef COVERAGE_MEASUREMENT
       
   240 #pragma CTC SKIP
       
   241 #endif //COVERAGE_MEASUREMENT
       
   242 void HsPreviewHSWidgetState::showMessageWidgetCorrupted()
   209 void HsPreviewHSWidgetState::showMessageWidgetCorrupted()
   243 {
   210 {
   244     HSMENUTEST_FUNC_ENTRY("HsCollectionState::showMessageWidgetCorrupted");
   211     HSMENUTEST_FUNC_ENTRY("HsCollectionState::showMessageWidgetCorrupted");
   245 
   212 
   246     mCorruptedMessage = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
   213     mCorruptedMessage = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
   257     mCorruptedMessage->addAction(secondaryAction);
   224     mCorruptedMessage->addAction(secondaryAction);
   258 
   225 
   259     mCorruptedMessage->open(this, SLOT(messageWidgetCorruptedFinished(HbAction*)));
   226     mCorruptedMessage->open(this, SLOT(messageWidgetCorruptedFinished(HbAction*)));
   260     HSMENUTEST_FUNC_EXIT("HsCollectionState::showMessageWidgetCorrupted");
   227     HSMENUTEST_FUNC_EXIT("HsCollectionState::showMessageWidgetCorrupted");
   261 }
   228 }
   262 #ifdef COVERAGE_MEASUREMENT
       
   263 #pragma CTC ENDSKIP
       
   264 #endif //COVERAGE_MEASUREMENT
       
   265 
   229 
   266 /*!
   230 /*!
   267  Slot launched on dismissing the corrupted widget error note
   231  Slot launched on dismissing the corrupted widget error note
   268  \retval void
   232  \retval void
   269  */
   233  */
   272     if (finishedAction == mCorruptedMessage->actions().value(0)) {
   236     if (finishedAction == mCorruptedMessage->actions().value(0)) {
   273         HsMenuService::executeAction(mEntryId, removeActionIdentifier());
   237         HsMenuService::executeAction(mEntryId, removeActionIdentifier());
   274     }
   238     }
   275     emit exit();
   239     emit exit();
   276 }
   240 }
       
   241 
       
   242 /*!
       
   243  \param entry The entry which is to be presented by the preview dialog.
       
   244  \return Preview popup for a given entry on success, NULL otherwise. 
       
   245   Ownership is passed to the caller.
       
   246  */
       
   247 HbDialog* HsPreviewHSWidgetState::buildPreviewDialog(const CaEntry& entry) const
       
   248 {
       
   249     HbDocumentLoader loader;
       
   250 
       
   251     bool loadStatusOk = false;
       
   252     loader.load(HS_WIDGET_PREVIEW_DIALOG_LAYOUT, &loadStatusOk);
       
   253     
       
   254     HbDialog *const previewDialog = 
       
   255         qobject_cast<HbDialog*>(
       
   256             loader.findWidget(HS_WIDGET_PREVIEW_DIALOG_NAME));
       
   257     HbLabel *const headingLabel =
       
   258         qobject_cast<HbLabel*>(
       
   259             loader.findWidget(HS_WIDGET_PREVIEW_LABEL_NAME));
       
   260     HbLabel *const iconBox =
       
   261         qobject_cast<HbLabel*>(
       
   262             loader.findWidget(HS_WIDGET_PREVIEW_ICON_BOX_NAME));
       
   263     
       
   264     loadStatusOk = loadStatusOk && (previewDialog != NULL)
       
   265         && (headingLabel != NULL) && (iconBox != NULL);
       
   266     
       
   267     Q_ASSERT_X(loadStatusOk,
       
   268         HS_WIDGET_PREVIEW_DIALOG_LAYOUT,
       
   269            "Cannot initialize widgets based on docml file.");
       
   270     
       
   271     if (loadStatusOk) {
       
   272         previewDialog->actions()[0]->setParent(previewDialog);
       
   273         previewDialog->actions()[1]->setParent(previewDialog);
       
   274         
       
   275         previewDialog->setTimeout(HbPopup::NoTimeout);
       
   276         previewDialog->setAttribute(Qt::WA_DeleteOnClose, true);
       
   277         
       
   278         headingLabel->setPlainText(entry.text());
       
   279         
       
   280         const QString previewImageName(entry.attribute(
       
   281             QLatin1String(HS_PREVIEW_ATTRIBUTE_NAME))); 
       
   282         
       
   283         if (!previewImageName.isEmpty()) {
       
   284             const HbIcon previewImage(previewImageName);
       
   285             if (previewImage.size().isValid()) {
       
   286                 iconBox->setIcon(previewImage);
       
   287             }
       
   288         }
       
   289     }
       
   290     
       
   291     return previewDialog;
       
   292 }
       
   293