src/hbcore/inputfw/hbinputmainwindow.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 7 923ff622b8b9
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    20 **
    20 **
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
       
    25 #include "hbinputmainwindow_p.h"
       
    26 
    25 #include <QGraphicsWidget>
    27 #include <QGraphicsWidget>
    26 #include <QGraphicsSceneMouseEvent>
    28 #include <QGraphicsSceneMouseEvent>
    27 #include <QStyleOptionGraphicsItem>
    29 #include <QStyleOptionGraphicsItem>
    28 #include <QInputContext>
    30 #include <QInputContext>
    29 
    31 
    30 #include "hbinputmainwindow_p.h"
       
    31 #include "hbinputregioncollector_p.h"
    32 #include "hbinputregioncollector_p.h"
    32 #include "hbinstance.h"
    33 #include "hbinstance.h"
    33 #include "hbwidget.h"
    34 #include "hbwidget.h"
    34 #include "hbview.h"
    35 #include "hbview.h"
    35 #include "hbnamespace_p.h"
    36 #include "hbnamespace_p.h"
    36 #include "hbview.h"
       
    37 #include "hbstackedlayout.h"
    37 #include "hbstackedlayout.h"
    38 
    38 
    39 #if defined (Q_OS_SYMBIAN)
    39 #if defined (Q_OS_SYMBIAN)
    40 #include <coemain.h>
    40 #include <coemain.h>
    41 #include <coecntrl.h>
    41 #include <coecntrl.h>
    43 
    43 
    44 TRect qt_QRect2TRect(const QRectF &rect)
    44 TRect qt_QRect2TRect(const QRectF &rect)
    45 {
    45 {
    46     TRect trect;
    46     TRect trect;
    47     trect.SetRect(rect.topLeft().x(), rect.topLeft().y(),
    47     trect.SetRect(rect.topLeft().x(), rect.topLeft().y(),
    48         rect.bottomRight().x() + 1, rect.bottomRight().y() + 1);
    48                   rect.bottomRight().x() + 1, rect.bottomRight().y() + 1);
    49     return trect;
    49     return trect;
    50 }
    50 }
    51 
    51 
    52 Q_DECLARE_TYPEINFO(TRect, Q_MOVABLE_TYPE);
    52 Q_DECLARE_TYPEINFO(TRect, Q_MOVABLE_TYPE);
    53 #endif
    53 #endif
    56 {
    56 {
    57 public:
    57 public:
    58 
    58 
    59     HbInputTransparentWindow(QGraphicsItem *parent = 0);
    59     HbInputTransparentWindow(QGraphicsItem *parent = 0);
    60     ~HbInputTransparentWindow();
    60     ~HbInputTransparentWindow();
    61     
    61 
    62     enum { Type = Hb::ItemType_TransparentWindow };
    62     enum { Type = Hb::ItemType_TransparentWindow };
    63     int type() const { return Type; }
    63     int type() const {
       
    64         return Type;
       
    65     }
    64 
    66 
    65     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    67     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    66 };
    68 };
    67 
    69 
    68 HbInputTransparentWindow::HbInputTransparentWindow(QGraphicsItem *parent) :
    70 HbInputTransparentWindow::HbInputTransparentWindow(QGraphicsItem *parent) :
    69     HbWidget(parent)
    71     HbWidget(parent)
    70 {
    72 {
       
    73     setFlag(QGraphicsItem::ItemHasNoContents, false);
    71 }
    74 }
    72 
    75 
    73 
    76 
    74 /*!
    77 /*!
    75     Destructs the transparent window.
    78     Destructs the transparent window.
    81 void HbInputTransparentWindow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    84 void HbInputTransparentWindow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    82 {
    85 {
    83     Q_UNUSED(widget)
    86     Q_UNUSED(widget)
    84     QPainter::CompositionMode compositionMode = painter->compositionMode();
    87     QPainter::CompositionMode compositionMode = painter->compositionMode();
    85     painter->setCompositionMode(QPainter::CompositionMode_Source);
    88     painter->setCompositionMode(QPainter::CompositionMode_Source);
    86     painter->fillRect(option->exposedRect, QColor(0,0,0,0));
    89     painter->fillRect(option->exposedRect, QColor(0, 0, 0, 0));
    87     painter->setCompositionMode(compositionMode);
    90     painter->setCompositionMode(compositionMode);
    88 }
    91 }
    89 
    92 
    90 /*
    93 /*
    91 creates an instance of HbInputMainWindow.
    94 creates an instance of HbInputMainWindow.
    96     return mainWindow;
    99     return mainWindow;
    97 }
   100 }
    98 
   101 
    99 HbInputMainWindow::~HbInputMainWindow()
   102 HbInputMainWindow::~HbInputMainWindow()
   100 {
   103 {
       
   104     delete mProxyWindow;
   101 }
   105 }
   102 
   106 
   103 // constructor.
   107 // constructor.
   104 HbInputMainWindow::HbInputMainWindow()
   108 HbInputMainWindow::HbInputMainWindow()
   105 // HbMainWindow creates a background QGraphicsItem, which has the background image. we need to hide it that.
   109 // HbMainWindow creates a background QGraphicsItem, which has the background image. we need to hide it that.
   106 :HbMainWindow(0, Hb::WindowFlagTransparent), mLastFocusedWidget(0), mSpellQueryLaunched(false)
   110     : HbMainWindow(0, Hb::WindowFlagTransparent), mLastFocusedWidget(0), mSpellQueryLaunched(false), mProxyWindow(0)
   107 {
   111 {
   108     // We need a window which is of type Qt::Window flag at the same time does not show
   112     // We need a window which is of type Qt::Window flag at the same time does not show
   109     // any decorators Qt::Tool seems to be the option, and we want this window to be always on top so Qt::WindowStaysOnTopHint.
   113     // any decorators Qt::Tool seems to be the option, and we want this window to be always on top so Qt::WindowStaysOnTopHint.
   110     // And since transparency requires to have a frameless window we are setting that too.
   114     // And since transparency requires to have a frameless window we are setting that too.
   111     setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool | Qt::FramelessWindowHint);
   115     setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool | Qt::FramelessWindowHint);
   112 
   116 
   113     // By default QGraphicsView has a background which is white in color (Other controls eg. QPushButton 
   117     // By default QGraphicsView has a background which is white in color (Other controls eg. QPushButton
   114     // have a grey background), we need to make that transparent too.
   118     // have a grey background), we need to make that transparent too.
   115     setStyleSheet("background: transparent");
   119     setStyleSheet("background: transparent");
   116 
   120 
   117     // No fous is necessary as we dont want the hbmainwindw to steal focus.
   121     // No focus is necessary as we don't want the hbmainwindw to steal focus.
   118     setFocusPolicy(Qt::NoFocus);
   122     setFocusPolicy(Qt::NoFocus);
   119   
   123 
   120     // add transparency begin.
   124     // add transparency begin.
   121     HbView* view = new HbView;
   125     HbView *view = new HbView;
   122     view->hideItems(Hb::AllItems);
   126     view->hideItems(Hb::AllItems);
   123     view->setContentFullScreen();
   127     view->setContentFullScreen();
   124 
   128 
   125 #if defined (Q_OS_SYMBIAN)
   129 #if defined (Q_OS_SYMBIAN)
   126     CCoeControl *c = effectiveWinId();
   130     CCoeControl *c = effectiveWinId();
   127     c->SetFocusing(false);
   131     c->SetFocusing(false);
   128     RWindow *rw = static_cast<RWindow*>(c->DrawableWindow());
   132     RWindow *rw = static_cast<RWindow *>(c->DrawableWindow());
   129     rw->SetRequiredDisplayMode( EColor16MA );
   133     rw->SetRequiredDisplayMode(EColor16MA);
   130     TInt err = rw->SetTransparencyAlphaChannel();
   134     TInt err = rw->SetTransparencyAlphaChannel();
   131     if ( err == KErrNone ) {
   135     if (err == KErrNone) {
   132         rw->SetBackgroundColor(~0);
   136         rw->SetBackgroundColor(~0);
   133     }
   137     }
   134 #endif // Q_OS_SYMBIAN
   138 #endif // Q_OS_SYMBIAN
   135 
   139 
   136     HbInputTransparentWindow *transparentWindow = new HbInputTransparentWindow;
   140     HbInputTransparentWindow *transparentWindow = new HbInputTransparentWindow;
   141     // add transparency ends.
   145     // add transparency ends.
   142 
   146 
   143     connect(HbInputRegionCollector::instance(), SIGNAL(updateRegion(QRegion)), this, SLOT(updateRegion(QRegion)));
   147     connect(HbInputRegionCollector::instance(), SIGNAL(updateRegion(QRegion)), this, SLOT(updateRegion(QRegion)));
   144 
   148 
   145     // QApplication signal for getting notification of any focus change. If therer
   149     // QApplication signal for getting notification of any focus change. If therer
   146     // is a switch between application window and HbInputMainWindow then we need to 
   150     // is a switch between application window and HbInputMainWindow then we need to
   147     // set the focus back to the application window, if we don't do that it will
   151     // set the focus back to the application window, if we don't do that it will
   148     // result in focusLost call inside framework.
   152     // result in focusLost call inside framework.
   149     connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
   153     connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
   150         this, SLOT(saveFocusWidget(QWidget *, QWidget *)));
   154             this, SLOT(saveFocusWidget(QWidget *, QWidget *)));
   151 }
   155 }
   152 
   156 
   153 
   157 
   154 void HbInputMainWindow::updateRegion(QRegion region)
   158 void HbInputMainWindow::updateRegion(QRegion region)
   155 {
   159 {
   156     mMask = region;
   160     mMask = region;
   157 #if defined (Q_OS_SYMBIAN)
   161 #if defined (Q_OS_SYMBIAN)
   158     RWindowBase *rwindow = effectiveWinId()->DrawableWindow();
   162     RWindowBase *rwindow = effectiveWinId()->DrawableWindow();
   159     if (region.isEmpty()) { 
   163     if (region.isEmpty()) {
   160         TRegionFix<1> tregion(TRect(TPoint(0, 0), TSize(0,0))); 
   164         TRegionFix<1> tregion(TRect(TPoint(0, 0), TSize(0, 0)));
   161         rwindow->SetShape(tregion); 
   165         rwindow->SetShape(tregion);
   162     } else {
   166     } else {
   163         // Using QVector assumes the memory layout is the same as RRegion 
   167         // Using QVector assumes the memory layout is the same as RRegion
   164         QVector<QRect> rects = region.rects(); 
   168         QVector<QRect> rects = region.rects();
   165         QVector<TRect> trects(rects.count()); 
   169         QVector<TRect> trects(rects.count());
   166         for (int i = 0; i < trects.count(); ++i) 
   170         for (int i = 0; i < trects.count(); ++i) {
   167             trects[i] = qt_QRect2TRect(rects.at(i)); 
   171             trects[i] = qt_QRect2TRect(rects.at(i));
   168         RRegion rregion(trects.count(), trects.data()); 
   172         }
   169         if (!rregion.CheckError())
   173         RRegion rregion(trects.count(), trects.data());
   170             rwindow->SetShape(rregion); 
   174         if (!rregion.CheckError()) {
   171    }
   175             rwindow->SetShape(rregion);
       
   176         }
       
   177     }
   172 #else
   178 #else
   173     setMask(region);
   179     setMask(region);
   174 #endif
   180 #endif
   175 }
   181 }
   176 
   182 
   217             // return true as we are interested party!
   223             // return true as we are interested party!
   218             return true;
   224             return true;
   219         }
   225         }
   220     }
   226     }
   221 
   227 
   222     // we need to only check for spontaneous events. 
   228     // we need to only check for spontaneous events.
   223     if (event->spontaneous() && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)) {
   229     if (event->spontaneous() && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)) {
   224         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
   230         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
   225         if (mouseEvent) {
   231         if (mouseEvent) {
   226             // get the top level widget at the point, and see if that widget is a HbMainWindow,
   232             // get the top level widget at the point, and see if that widget is a HbMainWindow,
   227             // If it is a HbMainWindow then do not do any thing, as events will propagate 
   233             // If it is a HbMainWindow then do not do any thing, as events will propagate
   228             // correctly. But when it is clicked inside application window then send the event to 
   234             // correctly. But when it is clicked inside application window then send the event to
   229             // viewport as we might want to close a popup.
   235             // viewport as we might want to close a popup.
   230             if (!mMask.contains(mouseEvent->globalPos())) {
   236             if (!mMask.contains(mouseEvent->globalPos())) {
   231                 qApp->sendEvent(viewport(), event);
   237                 qApp->sendEvent(viewport(), event);
   232             }
   238             }
   233         }
   239         }
   235 
   241 
   236     return HbMainWindow::eventFilter(obj, event);
   242     return HbMainWindow::eventFilter(obj, event);
   237 }
   243 }
   238 
   244 
   239 /*
   245 /*
   240 Since hbmainwindow is overlapped on top of the application window, we need to 
   246 Since hbmainwindow is overlapped on top of the application window, we need to
   241 set the focus back to the application window. Not doing so will result in a focus
   247 set the focus back to the application window. Not doing so will result in a focus
   242 lost.
   248 lost.
   243 */
   249 */
   244 void HbInputMainWindow::saveFocusWidget(QWidget * /*Old*/, QWidget *newFocus)
   250 void HbInputMainWindow::saveFocusWidget(QWidget * /*Old*/, QWidget *newFocus)
   245 {
   251 {
   248     }
   254     }
   249 }
   255 }
   250 
   256 
   251 void HbInputMainWindow::showInputWindow()
   257 void HbInputMainWindow::showInputWindow()
   252 {
   258 {
   253     // installing event filter to the application.. this is needed to get 
   259     // installing event filter to the application.. this is needed to get
   254     // the events happening in other vanilla windows.
   260     // the events happening in other vanilla windows.
   255     qApp->installEventFilter(this);
   261     qApp->installEventFilter(this);
   256 
   262     QInputContext *ic = qApp->inputContext();
       
   263     QWidget *fw = ic ? ic->focusWidget() : 0 ;
       
   264     QWidget *win = 0;
       
   265     if (fw) {
       
   266        win = fw->window();
       
   267     }
   257 #ifdef Q_WS_WIN
   268 #ifdef Q_WS_WIN
   258     // As in windows OS HbMainWindow can come anywhere on the screen.
   269     // As in windows OS HbMainWindow can come anywhere on the screen.
   259     // so we need to launch main window exactly at the top windows position.
   270     // so we need to launch main window exactly at the top windows position.
   260     QInputContext *ic = qApp->inputContext();
   271     if (win) {
   261     QWidget *fw = ic ? ic->focusWidget() : 0 ;
   272         move(win->frameGeometry().x(), win->pos().y());
   262     if (fw) {
       
   263         QWidget *win = fw->window();
       
   264         if (win) {
       
   265             move(win->frameGeometry().x(), win->pos().y());
       
   266         }
       
   267     }
   273     }
   268 #endif
   274 #endif
   269 
   275 
   270     HbInputRegionCollector::instance()->setEnabled(true);
   276     HbInputRegionCollector::instance()->setEnabled(true);
   271     show();
   277     if (win && win->windowModality() != Qt::NonModal) {
       
   278         if (!mProxyWindow) {
       
   279             mProxyWindow = new HbProxyWindow();
       
   280         }
       
   281         mProxyWindow->setWindow(this);
       
   282         // since the focused widget is inside a modal dialog which blocks events to other_window.
       
   283         // and since hbinputmainwindow also comes under the other_window. It does will not get the 
       
   284         // mouse click events.
       
   285         mProxyWindow->setParent(win);
       
   286         // setParent resets the window flags, so we have to set the flags once again before show() is called.
       
   287         setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool | Qt::FramelessWindowHint);
       
   288         show();
       
   289     } else {
       
   290         if (mProxyWindow && mProxyWindow->isAncestorOf(this)) {
       
   291             mProxyWindow->setWindow(0);
       
   292             setParent(0);
       
   293             // setParent resets the window flags, so we have to set the flags once again before show is called.
       
   294             setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool | Qt::FramelessWindowHint);
       
   295         }
       
   296         show();
       
   297     }
   272 
   298 
   273 #if defined(Q_OS_SYMBIAN)
   299 #if defined(Q_OS_SYMBIAN)
   274     // this is done to come on top of all the controls in symbian OS, done to overlap soft keys as well.
   300     // this is done to come on top of all the controls in symbian OS, done to overlap soft keys as well.
   275     RWindow * rWindow = static_cast<RWindow *>(effectiveWinId()->DrawableWindow());
   301     RWindow *rWindow = static_cast<RWindow *>(effectiveWinId()->DrawableWindow());
   276     const int positionForeground(0);
   302     const int positionForeground(0);
   277     rWindow->SetOrdinalPosition(positionForeground,
   303     rWindow->SetOrdinalPosition(positionForeground,
   278         ECoeWinPriorityAlwaysAtFront);
   304                                 ECoeWinPriorityAlwaysAtFront);
   279 #endif
   305 #endif
   280 }
   306 }
   281 
   307 
   282 void HbInputMainWindow::hideInputWindow()
   308 void HbInputMainWindow::hideInputWindow()
   283 {
   309 {
   291         RWindow *rWindow = static_cast<RWindow *>(effectiveWinId()->DrawableWindow());
   317         RWindow *rWindow = static_cast<RWindow *>(effectiveWinId()->DrawableWindow());
   292         const int positionBackground(-1);
   318         const int positionBackground(-1);
   293         rWindow->SetOrdinalPosition(positionBackground, ECoeWinPriorityNormal);
   319         rWindow->SetOrdinalPosition(positionBackground, ECoeWinPriorityNormal);
   294 #endif
   320 #endif
   295     }
   321     }
   296     
   322 
   297     HbInputRegionCollector::instance()->setEnabled(false);
   323     HbInputRegionCollector::instance()->setEnabled(false);
   298 
   324 
   299     // installing event filter to the application.. this is needed to get 
   325     // installing event filter to the application.. this is needed to get
   300     // the events happening in other vanilla windows.
   326     // the events happening in other vanilla windows.
   301     qApp->removeEventFilter(this);
   327     qApp->removeEventFilter(this);
   302 }
   328 }
   303 
   329 
   304 //EOF
   330 //EOF