phoneapp/phoneuiview2/src/phoneuiqtview.cpp
changeset 37 ba76fc04e6c2
child 45 6b911d05207e
equal deleted inserted replaced
36:2eacb6118286 37:ba76fc04e6c2
       
     1 /*!
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Phone UI's Qt view.
       
    15 *
       
    16 */
       
    17 #include <hbinstance.h>
       
    18 #include <QSignalMapper>
       
    19 #include <hbaction.h>
       
    20 #include <hbtoolbar.h>
       
    21 #include <hbvolumesliderpopup.h>
       
    22 #include <hbnamespace.h>
       
    23 #include <bubblemanager2.h>
       
    24 #include <hblineedit.h>
       
    25 #include <hbmenu.h>
       
    26 
       
    27 #include <xqserviceutil.h>
       
    28 #include <xqkeycapture.h>
       
    29 #include <dialpad.h>
       
    30 
       
    31 #include "phoneuiqtview.h"
       
    32 #include "phoneaction.h"
       
    33 #include "qtphonelog.h"
       
    34 
       
    35 PhoneUIQtView::PhoneUIQtView (HbMainWindow &window, QGraphicsItem *parent) :
       
    36     HbView (parent),
       
    37     m_window(window),
       
    38     m_volumeSlider (0),
       
    39     m_expandSignalMapper(0),
       
    40     m_participantListSignalMapper(0),
       
    41     m_volumeCommandId(0),
       
    42     m_keyCapture(0),
       
    43     m_networkInfo(0)
       
    44 {
       
    45     // Set network name
       
    46     m_networkInfo = new QSystemNetworkInfo(this);
       
    47     QString networkName = m_networkInfo->networkName(QSystemNetworkInfo::GsmMode);
       
    48     connect(m_networkInfo, SIGNAL (networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)), this, SLOT(networkNameChanged(QSystemNetworkInfo::NetworkMode, QString)));
       
    49     setTitle(networkName);
       
    50 
       
    51     // Capturing long press of end key
       
    52     m_keyCapture = new XqKeyCapture();
       
    53     m_keyCapture->captureLongKey(Qt::Key_No);
       
    54     m_keyCapture->captureKey(Qt::Key_No);
       
    55     
       
    56     // Dialpad
       
    57     m_dialpad = new Dialpad(m_window);
       
    58     m_dialpad->setCallButtonEnabled(false);
       
    59     m_dialpad->setTapOutsideDismiss(true);
       
    60     connect(&m_dialpad->editor(),SIGNAL(contentsChanged()),
       
    61             SLOT(onEditorContentChanged()));
       
    62     connect(m_dialpad,SIGNAL(aboutToClose()),this,
       
    63                 SLOT(dialpadClosed()));
       
    64                 
       
    65     // Call handling widget
       
    66     m_bubbleManager = new BubbleManager (this);
       
    67     setWidget(m_bubbleManager);
       
    68 
       
    69     // Set event filter
       
    70     m_window.installEventFilter(this);
       
    71 
       
    72     m_signalMapper = new QSignalMapper (this);
       
    73     connect(m_signalMapper, SIGNAL (mapped (int)), this, SIGNAL (command (int)));
       
    74     connect(&m_window,SIGNAL(orientationChanged(Qt::Orientation)),
       
    75             this,SLOT(handleOrientationChange(Qt::Orientation)));
       
    76 
       
    77     m_menuSignalMapper = new QSignalMapper(this);
       
    78     connect(m_menuSignalMapper, SIGNAL(mapped(int)), this, SIGNAL(command(int)));
       
    79 
       
    80     m_bubbleManager->handleOrientationChange(m_window.orientation());
       
    81 
       
    82     // change exit softkey to back button
       
    83     m_backAction = new HbAction(Hb::BackNaviAction, this);
       
    84     connect(m_backAction, SIGNAL(triggered()), this, SLOT(backButtonClicked()));
       
    85     setNavigationAction(m_backAction);
       
    86 
       
    87     createToolBarActions();
       
    88 }
       
    89 
       
    90 PhoneUIQtView::~PhoneUIQtView ()
       
    91 {
       
    92 
       
    93     foreach (HbAction *action, m_toolbarActions ) {
       
    94         delete action;
       
    95     }
       
    96     m_window.removeEventFilter(this);
       
    97     delete m_volumeSlider;
       
    98     delete m_dialpad;
       
    99 }
       
   100 
       
   101 BubbleManagerIF& PhoneUIQtView::bubbleManager()
       
   102 {
       
   103     return *m_bubbleManager;
       
   104 }
       
   105 
       
   106 void PhoneUIQtView::addBubbleCommand (
       
   107     int bubbleId,
       
   108     const PhoneAction& action )
       
   109 {
       
   110     HbAction* bubbleAction = new HbAction ();
       
   111     bubbleAction->setText (action.text());
       
   112     bubbleAction->setIcon (action.icon());
       
   113     setActionRole(action,*bubbleAction);
       
   114     m_bubbleManager->addAction (bubbleId, bubbleAction);
       
   115 
       
   116     QList<int> bubbles = m_bubbleMap.keys();
       
   117     bool found(false);
       
   118 
       
   119     for ( int i=0; i<bubbles.size(); ++i ) {
       
   120         if (bubbleId==bubbles[i]){
       
   121             connect(bubbleAction, SIGNAL (triggered ()), m_bubbleMap.value(bubbleId), SLOT (map ()));
       
   122             m_bubbleMap.value(bubbleId)->setMapping(bubbleAction, action.command());
       
   123             m_bubbleActionMap.value(bubbleId)->append(bubbleAction);
       
   124             found = true;
       
   125         }
       
   126     }
       
   127 
       
   128     if (!found) {
       
   129         QSignalMapper *mapper = new QSignalMapper();
       
   130         connect(mapper, SIGNAL (mapped (int)), this, SIGNAL (command (int)));
       
   131         connect(bubbleAction, SIGNAL (triggered ()), mapper, SLOT (map ()));
       
   132         mapper->setMapping (bubbleAction, action.command());
       
   133         QList<HbAction *> *actionList = new QList<HbAction *>();
       
   134         actionList->append( bubbleAction );
       
   135         m_bubbleActionMap.insert(bubbleId,actionList);
       
   136         m_bubbleMap.insert(bubbleId,mapper);
       
   137     }
       
   138 }
       
   139 
       
   140 void PhoneUIQtView::addParticipantListAction(
       
   141     int commandId,
       
   142     const QString& text,
       
   143     const HbIcon& icon)
       
   144 {
       
   145     HbAction* action = new HbAction ();
       
   146     action->setText (text);
       
   147     action->setIcon (icon);
       
   148     m_bubbleManager->addParticipantListAction(action);
       
   149 
       
   150     if (!m_participantListSignalMapper) {
       
   151         m_participantListSignalMapper = new QSignalMapper();
       
   152         connect(m_participantListSignalMapper, SIGNAL (mapped (int)), this, SIGNAL (command (int)));
       
   153     }
       
   154 
       
   155     connect(action, SIGNAL (triggered ()), m_participantListSignalMapper, SLOT (map ()));
       
   156     m_participantListSignalMapper->setMapping (action, commandId);
       
   157     m_participantListActions.append( action );
       
   158 }
       
   159 
       
   160 void PhoneUIQtView::clearParticipantListActions()
       
   161 {
       
   162 
       
   163     if (m_participantListSignalMapper) {
       
   164         m_bubbleManager->clearParticipantListActions();
       
   165 
       
   166         foreach (HbAction *action, m_participantListActions ) {
       
   167             m_participantListSignalMapper->removeMappings(action);
       
   168             delete action;
       
   169         }
       
   170 
       
   171         m_participantListActions.clear();
       
   172         delete m_participantListSignalMapper;
       
   173         m_participantListSignalMapper = 0;
       
   174     }
       
   175 
       
   176 }
       
   177 
       
   178 void PhoneUIQtView::clearBubbleCommands (int bubbleId)
       
   179 {
       
   180     m_bubbleManager->clearActions (bubbleId);
       
   181     QSignalMapper *mapper = m_bubbleMap.value(bubbleId);
       
   182 
       
   183     if (mapper) {
       
   184         QList<HbAction *> *actions = m_bubbleActionMap.value(bubbleId);
       
   185 
       
   186         foreach (HbAction *action, *actions ) {
       
   187             mapper->removeMappings(action);
       
   188             delete action;
       
   189         }
       
   190 
       
   191         actions->clear();
       
   192         m_bubbleMap.remove(bubbleId);
       
   193         m_bubbleActionMap.remove(bubbleId);
       
   194         delete mapper;
       
   195         delete actions;
       
   196     }
       
   197 
       
   198 }
       
   199 
       
   200 void PhoneUIQtView::setToolbarActions(const QList<PhoneAction*>& actions)
       
   201 {
       
   202     // clear current toolbar actions
       
   203     for (int i=0;i<toolBar()->actions().count();++i) {
       
   204         m_signalMapper->removeMappings(
       
   205                 static_cast<HbAction*>(toolBar()->actions().at(i)));
       
   206     }
       
   207 
       
   208     QList<QAction*> toolBarActions = toolBar()->actions();
       
   209 
       
   210     if (toolBarActions.size()<actions.size()) {
       
   211         for (int i=toolBarActions.size(); i<actions.size(); ++i) {
       
   212             toolBar()->addAction(m_toolbarActions.at(i));
       
   213         }
       
   214     } else if (toolBarActions.size()>actions.size()) {
       
   215         for (int i=toolBarActions.size(); 0<i; --i) {
       
   216             if (i>actions.size()) {
       
   217                 HbAction* action = static_cast<HbAction*>(toolBarActions.at(i-1));
       
   218                 toolBar()->removeAction(action);
       
   219             }
       
   220         }
       
   221     }
       
   222 
       
   223     for (int i=0; i<toolBar()->actions().size(); ++i) {
       
   224 
       
   225         if (i<actions.count()) {
       
   226             HbAction* action = static_cast<HbAction*>(toolBar()->actions().at(i));
       
   227             action->setText(actions.at(i)->text());
       
   228             action->setIcon(actions.at(i)->icon());
       
   229             action->setDisabled(actions.at(i)->isDisabled());
       
   230 
       
   231             m_signalMapper->setMapping(action, actions.at(i)->command());
       
   232         }
       
   233     }
       
   234 
       
   235     if ( m_window.orientation() == Qt::Horizontal ) {
       
   236         toolBar()->setOrientation(Qt::Horizontal);
       
   237     }
       
   238     // update toolbar
       
   239     toolBar()->update();
       
   240 }
       
   241 
       
   242 void PhoneUIQtView::hideToolbar ()
       
   243 {
       
   244     toolBar()->hide ();
       
   245 }
       
   246 
       
   247 void PhoneUIQtView::showToolbar ()
       
   248 {
       
   249     setFocus();
       
   250     toolBar()->show();
       
   251 }
       
   252 
       
   253 int PhoneUIQtView::volumeSliderValue ()
       
   254 {
       
   255     if (m_volumeSlider) {
       
   256         return m_volumeSlider->value ();
       
   257     } else {
       
   258         return -1;
       
   259     }
       
   260 }
       
   261 
       
   262 void PhoneUIQtView::removeVolumeSlider ()
       
   263 {
       
   264     if (m_volumeSlider) {
       
   265         if (m_volumeSlider->isVisible()) {
       
   266             m_volumeSlider->hide();
       
   267         }
       
   268         m_volumeSlider->deleteLater();
       
   269         m_volumeSlider = 0;
       
   270     }
       
   271 }
       
   272 
       
   273 void PhoneUIQtView::volumeSliderClosed ()
       
   274 {
       
   275     removeVolumeSlider();
       
   276 }
       
   277 
       
   278 void PhoneUIQtView::setVolumeSliderValue (
       
   279         int value, int commandId, int maxVolumeValue, int minVolumeValue)
       
   280 {
       
   281     m_volumeCommandId = commandId;
       
   282 
       
   283     if (!m_volumeSlider) {
       
   284         m_volumeSlider = new HbVolumeSliderPopup ();
       
   285         m_volumeSlider->setDismissPolicy(HbDialog::TapOutside);
       
   286         m_volumeSlider->setTimeout (10000); // TODO: 10 seconds for now, replace with correct value when spec is ready and says what it is
       
   287         connect(m_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeSliderChanged(int)));
       
   288         connect(m_volumeSlider, SIGNAL(aboutToClose()), this, SLOT(volumeSliderClosed()));
       
   289     }
       
   290 
       
   291 
       
   292     if (m_volumeSlider->minimum() != minVolumeValue ||
       
   293             m_volumeSlider->maximum() !=  maxVolumeValue  ) {
       
   294         m_volumeSlider->setRange (minVolumeValue, maxVolumeValue);
       
   295     }
       
   296 
       
   297     if (value != m_volumeSlider->value())
       
   298         m_volumeSlider->setValue (value);
       
   299 
       
   300     if (false == m_volumeSlider->isVisible()) {
       
   301         m_volumeSlider->show();
       
   302     }
       
   303 }
       
   304 
       
   305 void PhoneUIQtView::volumeSliderChanged(int value)
       
   306 {
       
   307     Q_UNUSED (value);
       
   308     emit command (m_volumeCommandId);
       
   309 }
       
   310 
       
   311 void PhoneUIQtView::setExpandAction(int bubbleId, int commandId)
       
   312 {
       
   313     removeExpandAction(bubbleId);
       
   314 
       
   315     HbAction* action = new HbAction();
       
   316     m_bubbleManager->setExpandAction(bubbleId, action);
       
   317 
       
   318     if (!m_expandSignalMapper) {
       
   319         m_expandSignalMapper = new QSignalMapper(this);
       
   320         connect(m_expandSignalMapper, SIGNAL (mapped (int)),
       
   321                 this, SIGNAL (command (int)));
       
   322     }
       
   323 
       
   324     connect(action, SIGNAL (triggered ()), m_expandSignalMapper, SLOT (map ()));
       
   325     m_expandSignalMapper->setMapping(action, commandId);
       
   326 
       
   327     m_expandActionMap.insert(bubbleId,action);
       
   328 }
       
   329 
       
   330 void PhoneUIQtView::removeExpandAction(int bubbleId)
       
   331 {
       
   332     if (m_expandActionMap.contains(bubbleId)) {
       
   333         m_bubbleManager->setExpandAction(bubbleId, 0);
       
   334         HbAction* action = m_expandActionMap.value(bubbleId);
       
   335         m_expandSignalMapper->removeMappings(action);
       
   336         m_expandActionMap.remove(bubbleId);
       
   337         delete action;
       
   338     }
       
   339 }
       
   340 
       
   341 void PhoneUIQtView::showDialpad()
       
   342 {
       
   343     if (false == m_dialpad->isVisible()) {
       
   344         setDialpadPosition();
       
   345         m_dialpad->openDialpad();
       
   346     }
       
   347 }
       
   348 
       
   349 void PhoneUIQtView::hideDialpad()
       
   350 {
       
   351     if (true == m_dialpad->isVisible())
       
   352         m_dialpad->closeDialpad();
       
   353 }
       
   354 
       
   355 bool PhoneUIQtView::isDialpadVisible()
       
   356 {
       
   357     return m_dialpad->isVisible();
       
   358 }
       
   359 
       
   360 QString PhoneUIQtView::dialpadText()
       
   361 {
       
   362     return m_dialpad->editor().text();
       
   363 }
       
   364 
       
   365 void PhoneUIQtView::clearAndHideDialpad()
       
   366 {
       
   367     m_dialpad->editor().setText(QString(""));
       
   368     hideDialpad();
       
   369 }
       
   370 
       
   371 void PhoneUIQtView::bringToForeground()
       
   372 {
       
   373     m_window.show();
       
   374     m_window.raise();
       
   375 }
       
   376 
       
   377 void PhoneUIQtView::setMenuActions(const QList<PhoneAction*>& actions)
       
   378 {
       
   379 
       
   380     for (int i=menu()->actions().count(); 0<i; --i) {
       
   381         HbAction* action = static_cast<HbAction*>(menu()->actions().at(i-1));
       
   382         m_menuSignalMapper->removeMappings(action);
       
   383         menu()->removeAction(action);
       
   384         delete action;
       
   385     }
       
   386 
       
   387     for (int i=0; i<actions.count(); ++i) {
       
   388         HbAction* action = new HbAction();
       
   389         action->setText(actions.at(i)->text());
       
   390         menu()->addAction(action);
       
   391         connect(action, SIGNAL(triggered()), m_menuSignalMapper, SLOT(map()));
       
   392         m_menuSignalMapper->setMapping(action, actions.at(i)->command());
       
   393     }
       
   394 
       
   395 }
       
   396 
       
   397 HbMenu &PhoneUIQtView::menuReference()
       
   398 {
       
   399     return *menu();
       
   400 }
       
   401 
       
   402 void PhoneUIQtView::handleOrientationChange(Qt::Orientation orientation)
       
   403 {
       
   404     if (orientation==Qt::Horizontal) {
       
   405         toolBar()->setOrientation(Qt::Horizontal);
       
   406     }
       
   407 
       
   408     m_bubbleManager->handleOrientationChange(orientation);
       
   409 
       
   410     setDialpadPosition();
       
   411 }
       
   412 
       
   413 void PhoneUIQtView::backButtonClicked()
       
   414 {
       
   415     XQServiceUtil::toBackground(true);
       
   416 }
       
   417 
       
   418 void PhoneUIQtView::onEditorContentChanged()
       
   419 {
       
   420     m_dialpad->setCallButtonEnabled(
       
   421         m_dialpad->editor().text().length());
       
   422 }
       
   423 
       
   424 void PhoneUIQtView::dialpadClosed()
       
   425 {
       
   426     emit dialpadIsAboutToClose();
       
   427 }
       
   428 
       
   429 bool PhoneUIQtView::eventFilter(QObject * /*watched*/, QEvent * event)
       
   430 {
       
   431     PHONE_DEBUG2("PhoneUIQtView::eventFilter event type:", event->type());
       
   432     if(event->type() == QEvent::KeyPress) {
       
   433         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
   434         PHONE_DEBUG2("PhoneUIQtView::eventFilter pressed key:", keyEvent->key());
       
   435         PHONE_DEBUG2("PhoneUIQtView::eventFilter isAutoRepeat:", keyEvent->isAutoRepeat());
       
   436         emit keyPressed(keyEvent);        
       
   437         keyEvent->accept();
       
   438         
       
   439         return false;
       
   440     } else if(event->type() == QEvent::KeyRelease) {
       
   441         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
   442         PHONE_DEBUG2("PhoneUIQtView::eventFilter released key:", keyEvent->key());
       
   443         emit keyReleased(keyEvent);
       
   444         keyEvent->accept();
       
   445         
       
   446         return false;
       
   447     } else if (event->type() == QEvent::WindowActivate){
       
   448         PHONE_DEBUG("PhoneUIQtView::eventFilter WindowActivate");
       
   449         emit windowActivated();
       
   450         return false;
       
   451     } else if (event->type() == QEvent::WindowDeactivate){
       
   452         PHONE_DEBUG("PhoneUIQtView::eventFilter WindowDeactivate");
       
   453         emit windowDeactivated();
       
   454         return false;
       
   455     }else{
       
   456         return false;
       
   457     }
       
   458 }
       
   459 
       
   460 void PhoneUIQtView::setDialpadPosition()
       
   461 {
       
   462     // workaround to tsw error JMKN-83NAPU (fix coming in MCL wk14)
       
   463     // QRectF screenRect(m_window.layoutRect());
       
   464     QRectF screenRect = (m_window.orientation() == Qt::Horizontal) ?
       
   465                         QRectF(0,0,640,360) : QRectF(0,0,360,640);
       
   466                         	
       
   467     if (m_window.orientation() == Qt::Horizontal) {
       
   468             // dialpad takes half of the screen
       
   469         m_dialpad->setPos(QPointF(screenRect.width()/2,
       
   470                                   this->scenePos().y()));
       
   471         m_dialpad->setPreferredSize(screenRect.width()/2,
       
   472                                            (screenRect.height()-scenePos().y()));
       
   473     } else {
       
   474         // dialpad takes 65% of the screen height
       
   475         qreal screenHeight = screenRect.height();
       
   476         m_dialpad->setPos(QPointF(0,
       
   477                                   screenHeight/2.25));
       
   478         m_dialpad->setPreferredSize(screenRect.width(),
       
   479                                     screenHeight-screenHeight/2.25);
       
   480     }
       
   481 }
       
   482 
       
   483 void PhoneUIQtView::setActionRole(const PhoneAction& pa, HbAction& action)
       
   484 {
       
   485     if (pa.actionRole()==PhoneAction::Accept) {
       
   486         action.setSoftKeyRole(QAction::PositiveSoftKey);
       
   487     } else if (pa.actionRole()==PhoneAction::Decline) {
       
   488         action.setSoftKeyRole(QAction::NegativeSoftKey);
       
   489     }
       
   490 }
       
   491 
       
   492 void PhoneUIQtView::createToolBarActions()
       
   493 {
       
   494     for (int i=0;i<4;++i) {
       
   495         HbAction* action = new HbAction();
       
   496         connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
       
   497         m_toolbarActions.append(action);
       
   498     }
       
   499 }
       
   500 
       
   501 void PhoneUIQtView::shutdownPhoneApp()
       
   502 {
       
   503     PHONE_DEBUG("PhoneUIQtView::shutdownPhoneApp");
       
   504     QCoreApplication::quit();
       
   505 }
       
   506 
       
   507 void PhoneUIQtView::setBackButtonVisible(bool visible)
       
   508 {
       
   509     if (visible) {
       
   510         setNavigationAction(m_backAction);
       
   511         }
       
   512     else {
       
   513         setNavigationAction(0);
       
   514     }
       
   515 }
       
   516 
       
   517 void PhoneUIQtView::networkNameChanged(QSystemNetworkInfo::NetworkMode mode, const QString &netName)
       
   518 {
       
   519     if(mode == QSystemNetworkInfo::GsmMode) {
       
   520         setTitle(netName);
       
   521     }	
       
   522 }