src/hbcore/gui/hbdialog.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 7 923ff622b8b9
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    39 #include <QShowEvent>
    39 #include <QShowEvent>
    40 #include <QHideEvent>
    40 #include <QHideEvent>
    41 #include <QEventLoop>
    41 #include <QEventLoop>
    42 #include <QPointer>
    42 #include <QPointer>
    43 #include <QDebug>
    43 #include <QDebug>
    44 #include <QGraphicsLinearLayout>
       
    45 #include <QApplication> // krazy:exclude=qclasses
    44 #include <QApplication> // krazy:exclude=qclasses
    46 
    45 
    47 #include <hbfeedbackmanager.h>
    46 #include <hbfeedbackmanager.h>
    48 
    47 
    49 #ifdef HB_EFFECTS
    48 #ifdef HB_EFFECTS
    68     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,13}
    67     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,13}
    69 
    68 
    70     An example of how to handle dialog signals from previous example.
    69     An example of how to handle dialog signals from previous example.
    71     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,53}
    70     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,53}
    72 
    71 
       
    72     An example of how to handle if finished(int) is connected instead of finished(HbAction*) in above example.
       
    73     \snippet{ultimatecodesnipped/ultimatecodesnippet.cpp,55}
       
    74 
    73     An example of how to create a non-modal dialog and show it.
    75     An example of how to create a non-modal dialog and show it.
    74     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,26}
    76     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,26}
    75 
    77 
    76 */
    78 */
       
    79 
       
    80 /*!
       
    81     \fn void HbDialog::finished( int code )
       
    82 
       
    83     This signal is emitted when the dialog is closed.
       
    84     This will have the HbDialog::DialogCode as the parameter code.
       
    85 
       
    86     \sa done(), accept(), reject()
       
    87 */
       
    88 /*!
       
    89     \fn void HbDialog::finished( HbAction *action )
       
    90 
       
    91     This signal is emitted when an action has been triggered in a dialog.
       
    92     The parameter will be the triggered action.
       
    93   */
       
    94 /*!
       
    95     \fn void HbDialog::accepted( )
       
    96 
       
    97     This signal is emitted when the dialog is closed and the user
       
    98     has accepted the dialog. which implies that either action has triggered
       
    99     or through function call the accept method is called, causing this signal.
       
   100 
       
   101     \sa done(), accept(), reject()
       
   102 */
       
   103 /*!
       
   104     \fn void HbDialog::rejected( )
       
   105 
       
   106     This signal is emitted when the dialog is closed and the user
       
   107     has rejected the dialog. which implies that either action triggered
       
   108     or through function call the reject method is called, causing this signal.
       
   109 
       
   110     \sa done(), accept(), reject()
       
   111 */
       
   112 
    77 
   113 
    78 /*!
   114 /*!
    79     \reimp
   115     \reimp
    80     \fn int HbDialog::type() const
   116     \fn int HbDialog::type() const
    81  */
   117  */
    82 
   118 
    83 HbDialogPrivate::HbDialogPrivate( ) :
   119 HbDialogPrivate::HbDialogPrivate( ) :
    84     contentWidget(0),
   120     contentWidget(0),
    85     headingWidget(0),
   121     headingWidget(0),
    86     mainLayout(new QGraphicsLinearLayout(Qt::Vertical)),
       
    87     primaryAction(0),
   122     primaryAction(0),
    88     secondaryAction(0),
   123     secondaryAction(0),
    89     closingAction(0),
   124     closingAction(0),
    90     toolBar(0)
   125     toolBar(0)
    91 {
   126 {
    93 
   128 
    94 HbDialogPrivate::~HbDialogPrivate()
   129 HbDialogPrivate::~HbDialogPrivate()
    95 {
   130 {
    96 }
   131 }
    97 
   132 
    98 void HbDialogPrivate::init()
   133 /*!
    99 {
       
   100     Q_Q(HbDialog);
       
   101 
       
   102     // Content is responsible of adding spacings & margins
       
   103     mainLayout->setSpacing(0);
       
   104     mainLayout->setContentsMargins(0,0,0,0);
       
   105 
       
   106     q->setLayout(mainLayout);
       
   107     mainLayout->setMinimumSize(0, 0);    
       
   108 }
       
   109 
       
   110 void HbDialogPrivate::setWidget(int layoutIndex, QGraphicsWidget *&destWidget, QGraphicsWidget *widget)
       
   111 {
       
   112     Q_Q(HbDialog);
       
   113     if (destWidget != widget) {
       
   114         if (destWidget) {
       
   115             mainLayout->removeItem(destWidget);
       
   116             delete destWidget;
       
   117             destWidget = 0;
       
   118         }
       
   119         if (widget) {
       
   120             destWidget = widget;
       
   121             destWidget->setParentItem(q);
       
   122             mainLayout->insertItem(layoutIndex, widget);
       
   123             mainLayout->setAlignment(widget, Qt::AlignCenter);
       
   124         }
       
   125 
       
   126         doLayout();
       
   127     }
       
   128 }
       
   129 
       
   130 /*
       
   131   Relayouts the popup. If expandSize is true it the new calculated size of the popup
   134   Relayouts the popup. If expandSize is true it the new calculated size of the popup
   132   cannot be smaller than the current size.
   135   cannot be smaller than the current size.
   133 */
   136 */
   134 void HbDialogPrivate::doLayout()
   137 void HbDialogPrivate::doLayout()
   135 {
   138 {
   148     }
   151     }
   149 
   152 
   150     q->updateGeometry();
   153     q->updateGeometry();
   151 }
   154 }
   152 
   155 
       
   156 /*!
       
   157  Utility function removes the spaces from string if any
       
   158 */
       
   159 void HbDialogPrivate::removeSpaces(QString& string)
       
   160 {
       
   161     QString tempStr(string);
       
   162     string.clear();
       
   163     foreach(QChar ch, tempStr)
       
   164     {
       
   165         if(!ch.isSpace())
       
   166             string.append(ch);
       
   167     }
       
   168 }
   153 
   169 
   154 /*!
   170 /*!
   155  Constructs a dialog with given  \a parent graphics item.\n
   171  Constructs a dialog with given  \a parent graphics item.\n
   156  Note: dialogs with \a parent set as 0 are behaving as real popups. 
   172  Note: dialogs with \a parent set as 0 are behaving as real popups. 
   157  This is actually the intended use. \sa HbPopup::HbPopup
   173  This is actually the intended use. \sa HbPopup::HbPopup
   160     HbPopup(*new HbDialogPrivate, parent)
   176     HbPopup(*new HbDialogPrivate, parent)
   161 {
   177 {
   162     Q_D(HbDialog);
   178     Q_D(HbDialog);
   163     d->q_ptr = this;
   179     d->q_ptr = this;
   164     d->init();
   180     d->init();
       
   181     d->timeout = HbPopupPrivate::timeoutValue(HbPopup::NoTimeout);
   165 }
   182 }
   166 
   183 
   167 /*!
   184 /*!
   168     \internal
   185     \internal
   169  */
   186  */
   171     HbPopup(dd, parent)
   188     HbPopup(dd, parent)
   172 {
   189 {
   173     Q_D(HbDialog);
   190     Q_D(HbDialog);
   174     d->q_ptr = this;
   191     d->q_ptr = this;
   175     d->init();
   192     d->init();
       
   193     d->timeout = HbPopupPrivate::timeoutValue(HbPopup::NoTimeout);
   176 }
   194 }
   177 
   195 
   178 /*!
   196 /*!
   179  Destroys the popup.
   197  Destroys the popup.
   180 */
   198 */
   198  \sa headingWidget()
   216  \sa headingWidget()
   199 */
   217 */
   200 void HbDialog::setHeadingWidget(QGraphicsWidget *headingWidget)
   218 void HbDialog::setHeadingWidget(QGraphicsWidget *headingWidget)
   201 {
   219 {
   202     Q_D(HbDialog);
   220     Q_D(HbDialog);
   203     HbStyle::setItemName(headingWidget,"heading");
   221     if (d->headingWidget == headingWidget)
   204     d->setWidget(0, d->headingWidget, headingWidget);
   222         return;
       
   223     if (d->headingWidget)
       
   224         delete d->headingWidget;
       
   225     d->headingWidget = headingWidget;
       
   226     if (headingWidget) {
       
   227         setProperty("heading_layout", true);
       
   228         headingWidget->setParentItem(this);
       
   229         HbStyle::setItemName(headingWidget,"heading");
       
   230     } else {
       
   231         setProperty("heading_layout", false);
       
   232     }
       
   233     repolish();
   205 }
   234 }
   206 
   235 
   207 /*!
   236 /*!
   208  Returns the content widget property of the popup.
   237  Returns the content widget property of the popup.
   209  HbDialog only draws a bordered rect, the rest is drawn by the content widget.
   238  HbDialog only draws a bordered rect, the rest is drawn by the content widget.
   222  to popup. If \a contentWidget is 0 the content widget is removed.
   251  to popup. If \a contentWidget is 0 the content widget is removed.
   223  \sa contentWidget()
   252  \sa contentWidget()
   224 */
   253 */
   225 void HbDialog::setContentWidget(QGraphicsWidget *contentWidget)
   254 void HbDialog::setContentWidget(QGraphicsWidget *contentWidget)
   226 {
   255 {
   227    Q_D(HbDialog);
   256     Q_D(HbDialog);
   228    HbStyle::setItemName(contentWidget,"content");
   257 
   229    d->setWidget((d->headingWidget?1:0), d->contentWidget, contentWidget);
   258     if (d->contentWidget == contentWidget)
       
   259         return;
       
   260     if (d->contentWidget)
       
   261         delete d->contentWidget;
       
   262     d->contentWidget = contentWidget;
       
   263     if (contentWidget) {
       
   264         contentWidget->setParentItem(this);
       
   265         HbStyle::setItemName(contentWidget,"content");
       
   266     }
       
   267     repolish();
   230 }
   268 }
   231 
   269 
   232 /*!
   270 /*!
   233  \deprecated HbDialog::primaryAction() const
   271  \deprecated HbDialog::primaryAction() const
   234        is deprecated.
   272        is deprecated.
   298     d->secondaryAction = action;
   336     d->secondaryAction = action;
   299     addAction(action);
   337     addAction(action);
   300 }
   338 }
   301 
   339 
   302 /*!
   340 /*!
       
   341     This is a slot which shows the dialog and returns immediately.
       
   342 
       
   343     \sa open(QObject*,const char*)
       
   344 */
       
   345 void HbDialog::open()
       
   346 {
       
   347     open(0,0);
       
   348 }
       
   349 /*!
   303 
   350 
   304  Shows the dialog as modal dialog returning immediately.  
   351  Shows the dialog as modal dialog returning immediately.  
   305 
   352 
   306  Connects finished(HbAction*) signal to the slot specified by \a receiver and
   353  Connects finished(HbAction*) or finished(int) signal to the slot specified by \a receiver and
   307  \a member. The signal will be disconnected from the slot when the
   354  \a member. The signal will be disconnected from the slot when the
   308  popup is closed.
   355  popup is closed. disambiguation between which method to connect to is done at runtime.
   309 
   356 
   310  For non modal popups, use show().  
   357  For non modal popups, use show().  
   311 */
   358 */
   312 
   359 
   313 void HbDialog::open( QObject* receiver, const char* member )
   360 void HbDialog::open( QObject* receiver, const char* member )
   314 {
   361 {
   315     Q_D(HbDialog);
   362     Q_D(HbDialog);
   316     if ( receiver && member ) {
   363     if ( receiver && member ) {
   317         connect( this, SIGNAL(finished(HbAction*)), receiver, member );
   364 
       
   365         QString myStr(member);
       
   366         d->removeSpaces(myStr);
       
   367         if(myStr.contains("(int)")) {
       
   368             connect( this, SIGNAL(finished(int)), receiver, member );
       
   369         }
       
   370         else {
       
   371             connect( this, SIGNAL(finished(HbAction*)), receiver, member );
       
   372         }
   318         d->receiverToDisconnectOnClose = receiver;
   373         d->receiverToDisconnectOnClose = receiver;
   319         d->memberToDisconnectOnClose = member;
   374         d->memberToDisconnectOnClose = member;
   320     } else {
   375     } else {
   321         d->receiverToDisconnectOnClose = 0;
   376         d->receiverToDisconnectOnClose = 0;
   322         d->memberToDisconnectOnClose.clear();
   377         d->memberToDisconnectOnClose.clear();
   323     }
   378     }
   324     show();
   379     show();
       
   380 }
       
   381 /*!
       
   382   Closes the dialog and emits finished ,accepted and rejected signals appropriately.
       
   383 
       
   384   If the dialog is accepted the code is HbDialog::Accepted, if it is rejected code
       
   385   is HbDialog::Rejected.
       
   386   As with HbWidget::close(), done() deletes the dialog if the
       
   387   Qt::WA_DeleteOnClose flag is set. 
       
   388 
       
   389   \sa accept(), reject()
       
   390 */
       
   391 void HbDialog::done( int code )
       
   392 {  
       
   393     HbAction *action=qobject_cast<HbAction*>(sender());
       
   394     if(!action) {
       
   395         close();
       
   396         //if there is no sender or if there is some sender which is not hbaction
       
   397         //then we need to close the dialog when done is called.
       
   398     }
       
   399     else if(actions().contains(action)==false) {
       
   400         close();
       
   401         //if our actions done have this HbAction. then we need to call the
       
   402         //close method explicitly.
       
   403     } //otherwise close will be called automatically due to connection in base class
       
   404     
       
   405     emit finished(code);
       
   406     if(code == Accepted) {
       
   407         emit accepted();
       
   408     }
       
   409     else if(code == Rejected) {
       
   410         emit rejected();
       
   411     }
       
   412 }
       
   413 /*!
       
   414   Hides the modal dialog and emits finished(HbDialog::Accepted),accepted() and finished(HbAction*) signals.
       
   415 
       
   416   \sa reject(), done()
       
   417 */
       
   418 void HbDialog::accept()
       
   419 {
       
   420     done(Accepted);
       
   421 }
       
   422 /*!
       
   423   Hides the modal dialog and emits finished(HbDialog::Rejected),rejected() and finished(HbAction*) signals.
       
   424 
       
   425   \sa accept(), done()
       
   426 */
       
   427 void HbDialog::reject()
       
   428 {
       
   429     done(Rejected);
   325 }
   430 }
   326 
   431 
   327 /*!
   432 /*!
   328  \reimp
   433  \reimp
   329 */
   434 */
   390     if (event->type() == QEvent::ActionAdded) {
   495     if (event->type() == QEvent::ActionAdded) {
   391         if (!d->toolBar) {
   496         if (!d->toolBar) {
   392             // TODO: HbToolBar private interface should make it possible to choose
   497             // TODO: HbToolBar private interface should make it possible to choose
   393             // different graphics for tool buttons.            
   498             // different graphics for tool buttons.            
   394             d->toolBar = new HbToolBar();
   499             d->toolBar = new HbToolBar();
       
   500             d->toolBar->setParentItem(this);
   395             HbStyle::setItemName(d->toolBar ,"controls");
   501             HbStyle::setItemName(d->toolBar ,"controls");
   396             d->toolBar->setParentItem(this);
   502             setProperty("controls_layout", true);
   397             d->toolBar->setOrientation(Qt::Horizontal);
   503             d->toolBar->setOrientation(Qt::Horizontal);
   398             HbToolBarPrivate::d_ptr(d->toolBar)->mDialogToolBar = true;
   504             HbToolBarPrivate::d_ptr(d->toolBar)->mDialogToolBar = true;
   399             // prevent stretching buttons, should the content be small
   505             repolish();
   400             // but dialog size forcibly large
       
   401             d->mainLayout->addStretch();
       
   402             d->mainLayout->addItem(d->toolBar);
       
   403         }
   506         }
   404         QActionEvent *actionEvent = static_cast<QActionEvent *>(event);
   507         QActionEvent *actionEvent = static_cast<QActionEvent *>(event);
   405         d->toolBar->insertAction (actionEvent->before(), actionEvent->action());
   508         d->toolBar->insertAction (actionEvent->before(), actionEvent->action());
   406         if (!parentItem()) { // only for popup without parent
   509         if (!parentItem()) { // only for popup without parent
   407             connect(actionEvent->action(), SIGNAL(triggered()), this, SLOT(close()));
   510             connect(actionEvent->action(), SIGNAL(triggered()), this, SLOT(close()));
   421         disconnect(actionEvent->action(), 0, this, 0);
   524         disconnect(actionEvent->action(), 0, this, 0);
   422 
   525 
   423         if (d->toolBar) {
   526         if (d->toolBar) {
   424            d->toolBar->removeAction(actionEvent->action());
   527            d->toolBar->removeAction(actionEvent->action());
   425            if (!d->toolBar->actions().count()) {
   528            if (!d->toolBar->actions().count()) {
   426                d->mainLayout->removeItem(d->toolBar);
       
   427                d->toolBar->deleteLater();
   529                d->toolBar->deleteLater();
   428                d->toolBar = 0;
   530                d->toolBar = 0;
       
   531                setProperty("controls_layout", false);
   429            }
   532            }
   430         }
   533         }
   431         d->doLayout();
   534         d->doLayout();
   432         return true;
   535         return true;
   433  
   536