src/hbinput/inputwidgets/hbinputexactwordpopup.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 HbInput 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 <QGraphicsWidget>
       
    27 #include <QPointer>
       
    28 #include <QIcon>
       
    29 #include <QGraphicsSceneMouseEvent>
       
    30 #include <QGraphicsLinearLayout>
       
    31 #if QT_VERSION >= 0x040600
       
    32 #include <QGraphicsDropShadowEffect>
       
    33 #endif
       
    34 
       
    35 #include "hbdeviceprofile.h"
       
    36 #include "hbdialog.h"
       
    37 #include "hblabel.h"
       
    38 #include "hbstyleoptionlabel.h"
       
    39 #include "hbinputexactwordpopup.h"
       
    40 #include "hbiconitem.h"
       
    41 #include "hbinputsettingproxy.h"
       
    42 #include "hbframeitem.h"
       
    43 #include "hbframedrawer.h"
       
    44 #include "hbcolorscheme.h"
       
    45 #include "hbdialog_p.h"
       
    46 
       
    47 const QSizeF  HbExactWordPopupSize(10,10);
       
    48 const QPointF HbExactWordPopupStartupDisplay(12, 33);
       
    49 
       
    50 class HbExactWordPopupPrivate : public HbDialogPrivate
       
    51 {
       
    52     Q_DECLARE_PUBLIC(HbExactWordPopup)
       
    53 
       
    54 public:
       
    55     HbExactWordPopupPrivate();
       
    56     ~HbExactWordPopupPrivate();
       
    57 
       
    58     void initBackground();
       
    59 
       
    60 public:
       
    61     HbLabel *mText;
       
    62     HbStyleOptionLabel *mOption;
       
    63     HbIconItem *iconPrim;
       
    64     HbFrameItem *mPopupBackground;
       
    65 };
       
    66 
       
    67 HbExactWordPopupPrivate::HbExactWordPopupPrivate(){
       
    68     mText = 0;
       
    69     mOption = 0;
       
    70     iconPrim = 0;
       
    71 }
       
    72 
       
    73 HbExactWordPopupPrivate::~HbExactWordPopupPrivate(){
       
    74     delete mOption;
       
    75     mOption = 0;
       
    76 }
       
    77 
       
    78 void HbExactWordPopupPrivate::initBackground()
       
    79 {
       
    80     Q_Q(HbExactWordPopup);
       
    81 
       
    82     mPopupBackground = static_cast<HbFrameItem*>(q->primitive( HbStyle::P_Popup_background));
       
    83 
       
    84     if( mPopupBackground == 0 ) {
       
    85         mPopupBackground = static_cast<HbFrameItem*>(q->style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Popup_background), q));
       
    86     }
       
    87 
       
    88     if ( mPopupBackground->frameDrawer().isNull()) {
       
    89         HbFrameDrawer* fd = new HbFrameDrawer("qtg_fr_popup_secondary", HbFrameDrawer::NinePieces);
       
    90         mPopupBackground->setFrameDrawer(fd);
       
    91     }
       
    92 
       
    93     // the size of the layout in the base class has been set to 0, reset it by passing an invalid size to setMinimumSize
       
    94     if(mainLayout) {
       
    95         mainLayout->setMinimumSize(-1, -1);
       
    96     }
       
    97 
       
    98 }
       
    99 
       
   100 HbExactWordPopup* HbExactWordPopup::instance( HbExactWordPopupIndicator indicatorArrow ) {
       
   101     static QPointer<HbExactWordPopup> exactWordPopup;
       
   102     if (!exactWordPopup) {
       
   103         // HbExactWordPopup is owned by the scene
       
   104         exactWordPopup = new HbExactWordPopup( 0, indicatorArrow );
       
   105     }
       
   106     return exactWordPopup;
       
   107 }
       
   108 
       
   109 /*!
       
   110     Constructor.
       
   111     \param parent An optional parameter.
       
   112 */
       
   113 HbExactWordPopup::HbExactWordPopup(QGraphicsWidget *parent, HbExactWordPopupIndicator indicatorArrow ) :
       
   114     HbDialog(*new HbExactWordPopupPrivate(), parent)
       
   115 {
       
   116     Q_D(HbExactWordPopup);
       
   117     d->mText = new HbLabel(this);
       
   118     d->mText->setAlignment(Qt::AlignCenter);
       
   119 
       
   120     d->setPriority(HbPopupPrivate::VirtualKeyboard + 1);  // Should be shown on top of virtual keyboard.
       
   121 
       
   122     d->initBackground();
       
   123 
       
   124     setTimeout(HbPopup::NoTimeout);
       
   125     setBackgroundFaded(false);
       
   126     setVisible(false);
       
   127     setDismissPolicy(HbPopup::TapInside);
       
   128     setFocusPolicy(Qt::ClickFocus);
       
   129     setContentWidget(d->mText);
       
   130     setModal(false);
       
   131 
       
   132 #if QT_VERSION >= 0x040600
       
   133     // Make sure the excat word popup never steals focus.
       
   134     setFlag(QGraphicsItem::ItemIsPanel, true);
       
   135     setActive(false);
       
   136 
       
   137     // enable drop shadow for the preview pane
       
   138     QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
       
   139     effect->setBlurRadius(8);
       
   140     setGraphicsEffect(effect);
       
   141 #endif
       
   142 
       
   143     d->mOption = new HbStyleOptionLabel();
       
   144     if(d->mOption != 0 ) {
       
   145         d->mOption->text = QString(" ");
       
   146         d->mOption->boundingRect = QRectF(HbExactWordPopupStartupDisplay,HbExactWordPopupSize);
       
   147         // for hardware keypad, we need to show the arrow to indicate the word
       
   148         // and in virtual keypad this is not needed, so set the image accordingly
       
   149         setIndicatorArrow( indicatorArrow );
       
   150         d->mOption->alignment = Qt::AlignCenter;
       
   151     }
       
   152 
       
   153     d->iconPrim = static_cast<HbIconItem*>(primitive(HbStyle::P_Label_icon));
       
   154     if(d->iconPrim == 0) {
       
   155         d->iconPrim = static_cast<HbIconItem*>(style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Label_icon), this));
       
   156     }
       
   157     style()->updatePrimitive(d->iconPrim, (HbStyle::Primitive)(HbStyle::P_Label_icon), d->mOption);
       
   158 }
       
   159 
       
   160 /*!
       
   161     Returns the text of the tooltip. The default value is "Exact Word popup text".
       
   162 
       
   163     \sa setText()
       
   164 */
       
   165 
       
   166 QString HbExactWordPopup::text()
       
   167 {
       
   168     Q_D(HbExactWordPopup);
       
   169     return d->mText->plainText();
       
   170 }
       
   171 
       
   172 /*!
       
   173     Sets the text of the label within the progress note.
       
   174 
       
   175     \sa text()
       
   176 */
       
   177 void HbExactWordPopup::setText(const QString &newText)
       
   178 {
       
   179     Q_D(HbExactWordPopup);
       
   180     d->mText->setPlainText(newText);
       
   181 
       
   182     QRectF ps=QRectF(QPointF(0,0), d->mText->preferredSize()).adjusted(-9,-9,9,9);
       
   183     resize(ps.size());
       
   184 }
       
   185 
       
   186 /*!
       
   187     Displays exact word popup at a given point
       
   188 
       
   189     \sa hideText()
       
   190 */
       
   191 void HbExactWordPopup::showText(QPointF pos)
       
   192 {
       
   193     // the popup should know at this stage in which main window/scene it's been launched at.
       
   194     int screenWidth = 0;
       
   195     if ( mainWindow() ) {
       
   196         screenWidth = HbDeviceProfile::profile(this).logicalSize().width();
       
   197     } else {
       
   198         // this is the fall-back if the main window is not know - can be removed when popup
       
   199         // is not relying on the primary window anymore.
       
   200         screenWidth = HbDeviceProfile::profile(mainWindow()).logicalSize().width();
       
   201     }
       
   202 
       
   203     const QRectF br = boundingRect();
       
   204     const qreal brCenter = br.width()/2;
       
   205     pos.setX(pos.x()-brCenter);
       
   206     // fix x position to keep tooltip visible
       
   207     const qreal requiredWidth = pos.x()+br.width();
       
   208     if (requiredWidth > screenWidth) {
       
   209         pos.setX(pos.x()-(requiredWidth-screenWidth));
       
   210     } else if (0 > pos.x()) {
       
   211         pos.setX(0);
       
   212     }
       
   213     pos.setY(pos.y()-br.height());
       
   214     setPos(pos);
       
   215 
       
   216     QSizeF mySize = size();
       
   217     mySize.setHeight(HbExactWordPopupSize.height());
       
   218     resize(mySize);    
       
   219 
       
   220     Q_D(HbExactWordPopup);
       
   221     d->mOption->boundingRect = QRectF(rect().center().x() - (HbExactWordPopupSize.width()/2),rect().bottom(),HbExactWordPopupSize.width(),HbExactWordPopupSize.height());
       
   222     style()->updatePrimitive(d->iconPrim, (HbStyle::Primitive)(HbStyle::P_Label_icon), d->mOption);
       
   223 
       
   224     show();
       
   225 }
       
   226 
       
   227 /*!
       
   228     Hides exact word popup
       
   229 
       
   230     \sa hideText()
       
   231 */
       
   232 void HbExactWordPopup::hideText()
       
   233 {
       
   234     close();
       
   235 }
       
   236 
       
   237 void HbExactWordPopup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   238 {
       
   239     if (contains(mapFromScene(event->scenePos()))) {
       
   240         // commit word only if mouse release event happens inside popup area
       
   241         emit exactWordSelected();
       
   242         // this call closes the popup, and this is called only when mouse relase event happens
       
   243         // inside popup area
       
   244         HbDialog::mouseReleaseEvent(event);
       
   245     }
       
   246     // if mouse release event happens outside popup area, don't commit or close the popup
       
   247 
       
   248 }
       
   249 
       
   250 void HbExactWordPopup::updatePrimitives()
       
   251 {
       
   252     Q_D( HbExactWordPopup );
       
   253 
       
   254     d->mPopupBackground->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
       
   255     d->mPopupBackground->frameDrawer().setFrameGraphicsName("qtg_fr_popup_secondary");
       
   256     d->mPopupBackground->setGeometry(boundingRect());
       
   257 
       
   258 	QColor col = HbColorScheme::color( "qtc_editor_normal" ); //popupforeground
       
   259     if (col.isValid()) {
       
   260         d->mText->setTextColor(col);
       
   261     }
       
   262 }
       
   263 
       
   264 // this method is called whenever there is a switch of keypad usage from h/w to virtual
       
   265 // h/w keypad needs an indicator, whereas virtual does not, hence set the image appropriately.
       
   266 void HbExactWordPopup::setIndicatorArrow( HbExactWordPopupIndicator indicatorArrow )
       
   267 {
       
   268     Q_D( HbExactWordPopup );
       
   269 
       
   270     if (indicatorArrow == HbNoIndicatorArrow) {
       
   271         d->mOption->icon = (QString(""));
       
   272     } else {
       
   273         d->mOption->icon = (QString("qtg_mono_input_down"));
       
   274    }
       
   275 }