src/hbwidgets/popups/hbnotificationdialog.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include <QGraphicsSceneMouseEvent>
       
    27 #include <QTimer>
       
    28 #include <QGraphicsScene>
       
    29 #include <QCoreApplication>
       
    30 #include <QtGlobal>
       
    31 #include <hbnotificationdialog.h>
       
    32 #include <hbnotificationdialog_p.h>
       
    33 #include <hblabel.h>
       
    34 #include <hbmainwindow.h>
       
    35 #include <hbwidgetsequentialshow_p.h>
       
    36 #include <hbdevicedialogserverstatus_p.h>
       
    37 
       
    38 #ifdef HB_EFFECTS
       
    39 #include "hbeffectinternal_p.h"
       
    40 #endif
       
    41 
       
    42 #include <hbstyleoptionnotificationdialog.h>
       
    43 #include "hbnotificationdialogcontent_p.h"
       
    44 
       
    45 #define H_MARGIN QString("hb-param-margin-gene-middle-horizontal")
       
    46 #define V_MARGIN QString("hb-param-margin-gene-middle-vertical")
       
    47 
       
    48 // Container to encapsulate device dialog server status and sequential show
       
    49 class SequentialShow : public HbWidgetSequentialShow
       
    50 {
       
    51 public:
       
    52     SequentialShow();
       
    53     static bool allowNotification(void *serverStatus);
       
    54 private:
       
    55     HbDeviceDialogServerStatus mServerStatus;
       
    56 };
       
    57 // Constructor
       
    58 SequentialShow::SequentialShow() :
       
    59     HbWidgetSequentialShow(SequentialShow::allowNotification, &mServerStatus), mServerStatus(false)
       
    60 {
       
    61     connect(&mServerStatus, SIGNAL(statusChanged()), SLOT(externalStatusChanged()));
       
    62 }
       
    63 // Return true if notification dialog can be shown
       
    64 bool SequentialShow::allowNotification(void *serverStatus)
       
    65 {
       
    66     HbDeviceDialogServerStatus* srvStatus =
       
    67         reinterpret_cast<HbDeviceDialogServerStatus*>(serverStatus);
       
    68     HbDeviceDialogServerStatus::StatusFlags flags = srvStatus->status();
       
    69     bool allow = (flags & HbDeviceDialogServerStatus::ShowingDialog) ==
       
    70         HbDeviceDialogServerStatus::NoFlags;
       
    71     // Monitor changes only when notifications are not allowed
       
    72     srvStatus->enableMonitoring(!allow);
       
    73     return allow;
       
    74 }
       
    75 
       
    76 // Singleton
       
    77 Q_GLOBAL_STATIC(SequentialShow, notificationDialogSequentialShowInstance)
       
    78 static SequentialShow *sequentialShowInstance()
       
    79 {
       
    80     return notificationDialogSequentialShowInstance();
       
    81 }
       
    82 
       
    83 /*!
       
    84     \class HbNotificationDialog
       
    85     \brief HbNotificationDialog is a non-modal dialog displayed on top of applications.
       
    86     Notification dialog is a dialog that can be used for notifying users of the system
       
    87     generated or user activated events in the UI. These notifications do not require
       
    88     user input.
       
    89 
       
    90     If wanted, some action can be activated with a tap to the notification dialog. The user
       
    91     of notification dialog can do this by first enabling the touch activation with
       
    92     enableTouchActivation() and then starting the action with the signal
       
    93     HbNotificationDialog::activated()
       
    94 
       
    95     HbNotificationDialog is a concrete class. For the content, you can use the default content
       
    96     widgets which provides two rows of text (title only, or title and text) and optionally an icon.
       
    97 	You can use the default content widget by invoking the HbNotificationDialog with its
       
    98     static launch-methods or by using the methods setText, setTitle, setIcon.
       
    99 
       
   100     Alternatively, you can create a separate widget, and set it to the dialog with inherited method
       
   101     HbNotificationDialog::setContentWidget().
       
   102 
       
   103     To display a notification dialog, show() or exec() has to be called. By default, notifications
       
   104     are synchronized with device dialogs. Showing of notification dialogs are delayed until there
       
   105     are no device dialogs on display. Notifications are also synchronized with each other.
       
   106     If several notifications are shown at the same time with show() function, they are shown
       
   107     sequentially instead of on top of each other. Sequential show and device dialog
       
   108     synchronization can be disabled by setSequentialShow() function.
       
   109 
       
   110     \beta
       
   111     \hbwidgets
       
   112 */
       
   113 
       
   114 /*!
       
   115     \fn void HbNotificationDialog::activated();
       
   116 
       
   117     This signal is emitted when the dialog is closed with a pointer tap
       
   118  */
       
   119 
       
   120 /*!
       
   121     \enum HbNotificationDialog::WrapMode
       
   122     \deprecated HbNotificationDialog::WrapMode
       
   123         is deprecated and will be removed in future.
       
   124 */
       
   125 
       
   126 HbNotificationDialog::HbNotificationDialog() : HbDialog(*new HbNotificationDialogPrivate, 0)
       
   127 {
       
   128     Q_D(HbNotificationDialog);
       
   129     d->q_ptr = this;
       
   130     setFocusPolicy(Qt::NoFocus);
       
   131     d->timeout = HbDialog::StandardTimeout;
       
   132 
       
   133     // Preferred position from style
       
   134     qreal hMargin = 0;
       
   135     qreal vMargin = 0;
       
   136     if ((style()->parameter(H_MARGIN, hMargin)) &&
       
   137         (style()->parameter(V_MARGIN, vMargin))) {
       
   138         setPreferredPos(QPointF(hMargin, vMargin));
       
   139     }
       
   140 
       
   141     // todo: priority
       
   142     d->setPriority(1);
       
   143 
       
   144     setModal(false);
       
   145 
       
   146     setBackgroundFaded(false);
       
   147     setDismissPolicy(HbPopup::NoDismiss);
       
   148     setTimeout(HbPopup::StandardTimeout);
       
   149     d->setBackgroundStyle();
       
   150 
       
   151 #ifdef HB_EFFECTS
       
   152     HbEffectInternal::add(this, "notificationdialog_appear", "appear");
       
   153     HbEffectInternal::add(this, "notificationdialog_disappear", "disappear");
       
   154 #endif
       
   155 }
       
   156 
       
   157 HbNotificationDialog::~HbNotificationDialog()
       
   158 {
       
   159     Q_D(HbNotificationDialog);
       
   160     if (d->sequentialShow) {
       
   161         sequentialShowInstance()->remove(this);
       
   162     }
       
   163 }
       
   164 
       
   165 /*!
       
   166     Enable user interaction on dialog.
       
   167     \param enable - When enabled, activated() signal is emitted on user action.
       
   168     \sa isTouchActivating()
       
   169 */
       
   170 void HbNotificationDialog::enableTouchActivation(bool enabled)
       
   171 {
       
   172     Q_D(HbNotificationDialog);
       
   173     d->isTouchActivating = enabled;
       
   174     if (d->content) {
       
   175         d->content->enableTouchActivation(enabled);
       
   176     }
       
   177 }
       
   178 
       
   179 /*!
       
   180     returns true if the use interaction is enabled.
       
   181     \sa enableTouchActivation()
       
   182 */
       
   183 bool HbNotificationDialog::isTouchActivating() const
       
   184 {
       
   185     Q_D(const HbNotificationDialog);
       
   186     return d->isTouchActivating;
       
   187 }
       
   188 
       
   189 /*!
       
   190  Convenience method for using HbNotificationDialog. Shows a notification dialog with
       
   191  the given parameters. The dialog is owned by HbNotificationDialog.
       
   192 */
       
   193 void HbNotificationDialog::launchDialog(const QString &title, const QString &text, QGraphicsScene* scene)
       
   194 {
       
   195     HbNotificationDialog *self = new HbNotificationDialog();
       
   196     if (scene) {
       
   197         scene->addItem(self);
       
   198     }
       
   199     self->setAttribute(Qt::WA_DeleteOnClose, true);
       
   200     self->setText(text);
       
   201     self->setTitle(title);
       
   202     self->show();
       
   203 }
       
   204 
       
   205 /*!
       
   206  Convenience method for using HbNotificationDialog. Shows a notification dialog with
       
   207  the given parameters. The dialog is owned by NotificationDialog.
       
   208  \deprecated HbNotificationDialog::launchDialog(const HbIcon &icon, QGraphicsScene* scene) is deprecated.
       
   209     Showing only icon is not supported by the layout. Use other launchDialog-methods instead.
       
   210 */
       
   211 void HbNotificationDialog::launchDialog(const HbIcon &icon, QGraphicsScene* scene)
       
   212 {
       
   213     HbNotificationDialog *self = new HbNotificationDialog();
       
   214     if (scene) {
       
   215         scene->addItem(self);
       
   216     }
       
   217     self->setAttribute(Qt::WA_DeleteOnClose, true);
       
   218     self->setIcon(icon);
       
   219     self->show();
       
   220 }
       
   221 
       
   222 /*!
       
   223  Convenience method for using HbNotificationDialog. Shows a notification dialog with
       
   224  the given parameters. The dialog is owned by NotificationDialog.
       
   225 */
       
   226 void HbNotificationDialog::launchDialog(const QString &title, QGraphicsScene* scene)
       
   227 {
       
   228     HbNotificationDialog *self = new HbNotificationDialog();
       
   229     if (scene) {
       
   230         scene->addItem(self);
       
   231     }
       
   232     self->setAttribute(Qt::WA_DeleteOnClose, true);
       
   233     self->setTitle(title);
       
   234     self->show();
       
   235 }
       
   236 
       
   237 /*!
       
   238  Convenience method for using HbNotificationDialog. Shows a notification dialog with
       
   239  the given parameters. The dialog is owned by HbNotificationDialog.
       
   240 */
       
   241 void HbNotificationDialog::launchDialog(const HbIcon &icon, const QString &title,
       
   242                                         const QString &text, QGraphicsScene* scene)
       
   243 {
       
   244     HbNotificationDialog *self = new HbNotificationDialog();
       
   245     if (scene) {
       
   246         scene->addItem(self);
       
   247     }
       
   248     self->setAttribute(Qt::WA_DeleteOnClose, true);
       
   249     self->setIcon(icon);
       
   250     self->setText(text);
       
   251     self->setTitle(title);
       
   252     self->show();
       
   253 }
       
   254 
       
   255 /*!
       
   256     \property HbNotificationDialog::title
       
   257     \brief title text
       
   258 
       
   259     If a default content widget doesn't exist, it is created.
       
   260 */
       
   261 /*!
       
   262  returns title text.
       
   263  \sa setTitle()
       
   264 */
       
   265 QString HbNotificationDialog::title() const
       
   266 {
       
   267     Q_D(const HbNotificationDialog);
       
   268     if(d->content) {
       
   269         return d->content->title();
       
   270     } else {
       
   271         return QString();
       
   272     }
       
   273 }
       
   274 
       
   275 /*!
       
   276  set title text
       
   277  \sa title()
       
   278 */
       
   279 void HbNotificationDialog::setTitle(const QString& title)
       
   280 {
       
   281     Q_D(HbNotificationDialog);
       
   282     d->checkAndCreateContentWidget();
       
   283     d->content->setTitle( title );
       
   284     d->setNotificationDialogContent();
       
   285 }
       
   286 
       
   287 /*!
       
   288     \property HbNotificationDialog::text
       
   289     \brief text for the dialog
       
   290 
       
   291     If a default content widget doesn't exist, it is created.
       
   292 */
       
   293 /*!
       
   294  returns text for the dialog.
       
   295  \sa setText()
       
   296 */
       
   297 QString HbNotificationDialog::text() const
       
   298 {
       
   299     Q_D(const HbNotificationDialog);
       
   300     if(d->content) {
       
   301         return d->content->text();
       
   302     } else {
       
   303         return QString();
       
   304     }
       
   305 }
       
   306 
       
   307 /*!
       
   308  set text for the dialog.
       
   309  \sa text()
       
   310 */
       
   311 void HbNotificationDialog::setText(const QString& text)
       
   312 {
       
   313     Q_D(HbNotificationDialog);
       
   314     d->checkAndCreateContentWidget();
       
   315     d->content->setText( text );
       
   316     d->setNotificationDialogContent();
       
   317 }
       
   318 
       
   319 /*!
       
   320     \property HbNotificationDialog::icon
       
   321     \brief icon
       
   322 
       
   323     If a default content widget doesn't exist, it is created.
       
   324 */
       
   325 /*!
       
   326  returns the icon.
       
   327  \sa setIcon()
       
   328 */
       
   329 HbIcon HbNotificationDialog::icon() const
       
   330 {
       
   331     Q_D(const HbNotificationDialog);
       
   332     if(d->content) {
       
   333         return d->content->icon();
       
   334     } else {
       
   335         return QString();
       
   336     }
       
   337 }
       
   338 
       
   339 /*!
       
   340  set the icon.
       
   341  \sa icon()
       
   342 */
       
   343 void HbNotificationDialog::setIcon(const HbIcon& icon)
       
   344 {
       
   345     Q_D(HbNotificationDialog);
       
   346     d->checkAndCreateContentWidget();
       
   347     d->content->setIcon( icon );
       
   348     d->setNotificationDialogContent();
       
   349 }
       
   350 
       
   351 /*!
       
   352 \deprecated HbNotificationDialog::setWrapMode(int mode)
       
   353       is deprecated. Please use setTitleTextWrapping(Hb::TextWrapping wrapping) instead.
       
   354 
       
   355 Deprecated.
       
   356 */
       
   357 void HbNotificationDialog::setWrapMode(int mode)
       
   358 {
       
   359     if (mode == NoWrap) {
       
   360         setTitleTextWrapping(Hb::TextNoWrap);
       
   361     } else {
       
   362         setTitleTextWrapping(Hb::TextWordWrap);
       
   363     }
       
   364 }
       
   365 
       
   366 /*!
       
   367     \property HbNotificationDialog::titleTextWrapping
       
   368     \brief sets the wrapping for title.
       
   369 
       
   370     The title can wrap to two lines only if the text is empty.
       
   371     \sa HbNotificationDialog::title, HbNotificationDialog::text
       
   372 */
       
   373 /*!
       
   374     Returns the wrapping mode. The title can wrap to two lines only if the text is empty.
       
   375     \sa setTitleTextWrapping()
       
   376 */
       
   377 Hb::TextWrapping HbNotificationDialog::titleTextWrapping() const
       
   378 {
       
   379     Q_D(const HbNotificationDialog);
       
   380     return d->titleWrapping;
       
   381 }
       
   382 
       
   383 /*!
       
   384     \brief sets the wrapping for title.
       
   385 
       
   386     The title can wrap to two lines only if the text is empty.
       
   387     \sa titleTextWrapping()
       
   388 */
       
   389 void HbNotificationDialog::setTitleTextWrapping(Hb::TextWrapping wrapping)
       
   390 {
       
   391     Q_D(HbNotificationDialog);
       
   392     if (d->titleWrapping != wrapping) {
       
   393         d->titleWrapping = wrapping;
       
   394         if (d->content) {
       
   395             d->content->setTitleTextWrapping(d->titleWrapping);
       
   396         }
       
   397         d->doLayout();
       
   398     }
       
   399 }
       
   400 
       
   401 /*!
       
   402     Enables or disables sequential showing of Notification Dialog. Notification dialogs are by
       
   403     default shown sequentially. Several dialogs displayed by show() at the same time are shown
       
   404     one after another instead of on top of each other. Showing of the dialogs are also synchronized
       
   405     with device dialogs by delaying until none of them are shown. With sequential show disabled,
       
   406     HbNotificationDialog behaves like other popups. While a dialog is waiting to be shown,
       
   407     setVisible(), hide() or show() has no effect. setSequentialShow(false) removes a dialog from the
       
   408     wait queue.
       
   409 
       
   410     \sa isSequentialShow()
       
   411 */
       
   412 void HbNotificationDialog::setSequentialShow(bool sequentially)
       
   413 {
       
   414     Q_D(HbNotificationDialog);
       
   415     if (d->sequentialShow != sequentially && !sequentially) {
       
   416         sequentialShowInstance()->remove(this);
       
   417     }
       
   418     d->sequentialShow = sequentially;
       
   419 }
       
   420 
       
   421 /*!
       
   422     Returns sequential show setting.
       
   423 
       
   424     \sa setSequentialShow()
       
   425 */
       
   426 bool HbNotificationDialog::isSequentialShow() const
       
   427 {
       
   428     Q_D(const HbNotificationDialog);
       
   429     return d->sequentialShow;
       
   430 }
       
   431 
       
   432 /*!
       
   433 \deprecated HbNotificationDialog::wrapMode() const
       
   434       is deprecated. Please use titleTextWrapping() const instead.
       
   435 
       
   436 Deprecated.
       
   437 */
       
   438 int HbNotificationDialog::wrapMode() const
       
   439 {
       
   440     return NoWrap;
       
   441 }
       
   442 
       
   443 /*!
       
   444  Constructor required by the shared d-pointer paradigm.
       
   445 */
       
   446 HbNotificationDialog::HbNotificationDialog(HbNotificationDialogPrivate &dd, QGraphicsItem *parent) :
       
   447     HbDialog(dd, parent)
       
   448 {
       
   449 }
       
   450 
       
   451 /*!
       
   452     \reimp
       
   453  */
       
   454 void HbNotificationDialog::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   455 {
       
   456     Q_D(HbNotificationDialog);
       
   457     d->pointerDownPoint = event->pos();
       
   458     // "Pop-up is visible as long as the user holds the finger on top of it."
       
   459     d->stopTimeout();
       
   460     event->accept();
       
   461 }
       
   462 
       
   463 /*!
       
   464     \reimp
       
   465  */
       
   466 void HbNotificationDialog::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   467 {
       
   468     Q_D(HbNotificationDialog);
       
   469     // Touch release => Inactive or application specific functionality
       
   470 
       
   471     if (d->isTouchActivating) {
       
   472         QPointF point = event->pos() - d->pointerDownPoint;
       
   473         qreal manhattanLength = qAbs(point.x()) + qAbs(point.y());
       
   474         if (manhattanLength < 20)
       
   475         {
       
   476             emit activated();
       
   477         }
       
   478     }
       
   479     close();
       
   480 }
       
   481 
       
   482 // Widget is about to hide. Closing effect has ended,
       
   483 void HbNotificationDialog::hideEvent(QHideEvent *event)
       
   484 {
       
   485     HbDialog::hideEvent(event);
       
   486     HbMainWindow* mainWnd = mainWindow();
       
   487     if (mainWnd) {
       
   488         disconnect(mainWnd, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
       
   489     }
       
   490 }
       
   491 
       
   492 // Widget is about to show
       
   493 void HbNotificationDialog::showEvent(QShowEvent *event)
       
   494 {
       
   495     HbDialog::showEvent(event);
       
   496     HbMainWindow* mainWnd = mainWindow();
       
   497     if (mainWnd) {
       
   498         connect(mainWnd, SIGNAL(orientationChanged(Qt::Orientation)),this, SLOT(orientationChanged(Qt::Orientation)));
       
   499     }
       
   500 }
       
   501 
       
   502 // Item change event
       
   503 QVariant HbNotificationDialog::itemChange(GraphicsItemChange change, const QVariant &value)
       
   504 {
       
   505     // If item is about to show
       
   506     if (change == QGraphicsItem::ItemVisibleChange && value.toBool()) {
       
   507         Q_D(HbNotificationDialog);
       
   508         if (d->sequentialShow) {
       
   509             HbWidgetSequentialShow *sequentialShow = sequentialShowInstance();
       
   510             sequentialShow->add(this);
       
   511             QVariant retVal(false);
       
   512             // Dialog to show now
       
   513             HbWidget *toShow = sequentialShow->toShowNow();
       
   514             if (toShow == this) { // this dialog should show now
       
   515                 sequentialShow->setShowing(this);
       
   516                 retVal = HbDialog::itemChange(change, value);
       
   517                 if (!retVal.toBool()) {
       
   518                     sequentialShow->remove(this);
       
   519                 }
       
   520             }
       
   521             // Return true if this dialog is showing. Otherwise the dialog is shown later.
       
   522             return retVal;
       
   523         }
       
   524     }
       
   525     return HbDialog::itemChange(change, value);
       
   526 }
       
   527 
       
   528 void HbNotificationDialog::orientationChanged(Qt::Orientation orientation)
       
   529 {
       
   530      Q_UNUSED(orientation);
       
   531 
       
   532     // Preferred position from style
       
   533     qreal hMargin = 0;
       
   534     qreal vMargin = 0;
       
   535     if ((style()->parameter(H_MARGIN, hMargin)) &&
       
   536         (style()->parameter(V_MARGIN, vMargin))) {
       
   537         setPreferredPos(QPointF(hMargin, vMargin));
       
   538     }
       
   539 }
       
   540 
       
   541 HbNotificationDialogPrivate::HbNotificationDialogPrivate() :
       
   542         HbDialogPrivate(), isTouchActivating(false),
       
   543         titleWrapping(Hb::TextWordWrap),
       
   544         content(0), sequentialShow(true)
       
   545 {
       
   546 }
       
   547 
       
   548 HbNotificationDialogPrivate::~HbNotificationDialogPrivate()
       
   549 {
       
   550 }
       
   551 
       
   552 void HbNotificationDialogPrivate::checkAndCreateContentWidget()
       
   553 {
       
   554     Q_Q(HbNotificationDialog);
       
   555     if(!content) {
       
   556         content = new HbNotificationDialogContent(q);
       
   557         HbStyle::setItemName(content, "content");
       
   558     }
       
   559 }
       
   560 void HbNotificationDialogPrivate::setBackgroundStyle()
       
   561 {
       
   562     Q_Q(HbNotificationDialog);
       
   563     q->setBackgroundItem(HbStyle::P_NotificationDialog_frame);
       
   564 }
       
   565 
       
   566 void HbNotificationDialogPrivate::setNotificationDialogContent()
       
   567 {
       
   568     Q_Q(HbNotificationDialog);
       
   569     content->enableTouchActivation(isTouchActivating);
       
   570     content->setTitleTextWrapping(titleWrapping);
       
   571     if (q->contentWidget() == content) {
       
   572         doLayout();
       
   573     } else {
       
   574         q->setContentWidget( content );
       
   575     }
       
   576 }
       
   577