src/hbinput/inputwidgets/hbinputexactwordpopup.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 7 923ff622b8b9
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    40 #include "hbframeitem.h"
    40 #include "hbframeitem.h"
    41 #include "hbframedrawer.h"
    41 #include "hbframedrawer.h"
    42 #include "hbcolorscheme.h"
    42 #include "hbcolorscheme.h"
    43 #include "hbdialog_p.h"
    43 #include "hbdialog_p.h"
    44 
    44 
    45 const QSizeF  HbExactWordPopupSize(10,10);
    45 const qreal HbExactWordPopupHeight = 50.0;
    46 const QPointF HbExactWordPopupStartupDisplay(12, 33);
       
    47 
    46 
    48 class HbExactWordPopupPrivate : public HbDialogPrivate
    47 class HbExactWordPopupPrivate : public HbDialogPrivate
    49 {
    48 {
    50     Q_DECLARE_PUBLIC(HbExactWordPopup)
    49     Q_DECLARE_PUBLIC(HbExactWordPopup)
    51 
    50 
    55 
    54 
    56     void initBackground();
    55     void initBackground();
    57 
    56 
    58 public:
    57 public:
    59     HbLabel *mText;
    58     HbLabel *mText;
    60     HbStyleOptionLabel *mOption;
       
    61     HbIconItem *iconPrim;
       
    62     HbFrameItem *mPopupBackground;
    59     HbFrameItem *mPopupBackground;
    63 };
    60 };
    64 
    61 
    65 HbExactWordPopupPrivate::HbExactWordPopupPrivate(){
    62 HbExactWordPopupPrivate::HbExactWordPopupPrivate()
    66     mText = 0;
    63  : mText(0), mPopupBackground(0)
    67     mOption = 0;
    64 {
    68     iconPrim = 0;
    65 }
    69 }
    66 
    70 
    67 HbExactWordPopupPrivate::~HbExactWordPopupPrivate()
    71 HbExactWordPopupPrivate::~HbExactWordPopupPrivate(){
    68 {
    72     delete mOption;
       
    73     mOption = 0;
       
    74 }
    69 }
    75 
    70 
    76 void HbExactWordPopupPrivate::initBackground()
    71 void HbExactWordPopupPrivate::initBackground()
    77 {
    72 {
    78     Q_Q(HbExactWordPopup);
    73     Q_Q(HbExactWordPopup);
    79 
    74 
    80     mPopupBackground = static_cast<HbFrameItem*>(q->primitive( HbStyle::P_Popup_background));
    75     mPopupBackground = static_cast<HbFrameItem*>(q->primitive(HbStyle::P_Popup_background));
    81 
    76 
    82     if( mPopupBackground == 0 ) {
    77     if (!mPopupBackground) {
    83         mPopupBackground = static_cast<HbFrameItem*>(q->style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Popup_background), q));
    78         mPopupBackground = static_cast<HbFrameItem*>(q->style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Popup_background), q));
    84     }
    79     }
    85 
    80 
    86     if ( mPopupBackground->frameDrawer().isNull()) {
    81     if (mPopupBackground->frameDrawer().isNull()) {
    87         HbFrameDrawer* fd = new HbFrameDrawer("qtg_fr_popup_secondary", HbFrameDrawer::NinePieces);
    82         HbFrameDrawer *fd = new HbFrameDrawer("qtg_fr_popup_secondary", HbFrameDrawer::NinePieces);
    88         mPopupBackground->setFrameDrawer(fd);
    83         mPopupBackground->setFrameDrawer(fd);
    89     }
    84     }
    90 
    85 }
    91     // the size of the layout in the base class has been set to 0, reset it by passing an invalid size to setMinimumSize
    86 
    92     if(mainLayout) {
    87 HbExactWordPopup* HbExactWordPopup::instance(HbExactWordPopupIndicator indicatorArrow)
    93         mainLayout->setMinimumSize(-1, -1);
    88 {
    94     }
       
    95 
       
    96 }
       
    97 
       
    98 HbExactWordPopup* HbExactWordPopup::instance( HbExactWordPopupIndicator indicatorArrow ) {
       
    99     static QPointer<HbExactWordPopup> exactWordPopup;
    89     static QPointer<HbExactWordPopup> exactWordPopup;
   100     if (!exactWordPopup) {
    90     if (!exactWordPopup) {
   101         // HbExactWordPopup is owned by the scene
    91         // HbExactWordPopup is owned by the scene
   102         exactWordPopup = new HbExactWordPopup( 0, indicatorArrow );
    92         exactWordPopup = new HbExactWordPopup( 0, indicatorArrow );
   103     }
    93     }
   106 
    96 
   107 /*!
    97 /*!
   108     Constructor.
    98     Constructor.
   109     \param parent An optional parameter.
    99     \param parent An optional parameter.
   110 */
   100 */
   111 HbExactWordPopup::HbExactWordPopup(QGraphicsWidget *parent, HbExactWordPopupIndicator indicatorArrow ) :
   101 HbExactWordPopup::HbExactWordPopup(QGraphicsWidget *parent, HbExactWordPopupIndicator indicatorArrow)
   112     HbDialog(*new HbExactWordPopupPrivate(), parent)
   102  : HbDialog(*new HbExactWordPopupPrivate(), parent)
   113 {
   103 {
   114     Q_D(HbExactWordPopup);
   104     Q_D(HbExactWordPopup);
   115     HbInputRegionCollector::instance()->attach(this);
   105     HbInputRegionCollector::instance()->attach(this);
   116     d->mText = new HbLabel(this);
   106 
       
   107     d->mText = new HbLabel();
   117     d->mText->setAlignment(Qt::AlignCenter);
   108     d->mText->setAlignment(Qt::AlignCenter);
   118 
   109     d->mText->setFontSpec(HbFontSpec(HbFontSpec::Primary));
   119     d->setPriority(HbPopupPrivate::VirtualKeyboard + 1);  // Should be shown on top of virtual keyboard.
   110     d->mText->setContentsMargins(0, 0, 0, 0);
       
   111 
       
   112     setContentWidget(d->mText);
   120 
   113 
   121     d->initBackground();
   114     d->initBackground();
   122 
   115 
   123     setTimeout(HbPopup::NoTimeout);
   116     setTimeout(HbPopup::NoTimeout);
   124     setBackgroundFaded(false);
   117     setBackgroundFaded(false);
   125     setVisible(false);
       
   126     setDismissPolicy(HbPopup::TapInside);
   118     setDismissPolicy(HbPopup::TapInside);
   127     setFocusPolicy(Qt::ClickFocus);
   119     setFocusPolicy(Qt::ClickFocus);
   128     setContentWidget(d->mText);
       
   129     setModal(false);
   120     setModal(false);
   130 
   121     setContentsMargins(0, 0, 0, 0);
   131 #if QT_VERSION >= 0x040600
   122 
   132     // Make sure the exact word popup never steals focus.
   123     // Make sure the exact word popup never steals focus.
   133     setFlag(QGraphicsItem::ItemIsPanel, true);
   124     setFlag(QGraphicsItem::ItemIsPanel, true);
   134     setActive(false);
   125     setActive(false);
   135 #endif
   126 
   136 
   127     setIndicatorArrow(indicatorArrow);
   137     d->mOption = new HbStyleOptionLabel();
       
   138     if(d->mOption != 0 ) {
       
   139         d->mOption->text = QString(" ");
       
   140         d->mOption->boundingRect = QRectF(HbExactWordPopupStartupDisplay,HbExactWordPopupSize);
       
   141         // for hardware keypad, we need to show the arrow to indicate the word
       
   142         // and in virtual keypad this is not needed, so set the image accordingly
       
   143         setIndicatorArrow( indicatorArrow );
       
   144         d->mOption->alignment = Qt::AlignCenter;
       
   145     }
       
   146 
       
   147     d->iconPrim = static_cast<HbIconItem*>(primitive(HbStyle::P_Label_icon));
       
   148     if(d->iconPrim == 0) {
       
   149         d->iconPrim = static_cast<HbIconItem*>(style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Label_icon), this));
       
   150     }
       
   151     style()->updatePrimitive(d->iconPrim, (HbStyle::Primitive)(HbStyle::P_Label_icon), d->mOption);
       
   152 }
   128 }
   153 
   129 
   154 /*!
   130 /*!
   155     Returns the text of the tooltip. The default value is "Exact Word popup text".
   131     Returns the text of the tooltip. The default value is "Exact Word popup text".
   156 
   132 
   170 */
   146 */
   171 void HbExactWordPopup::setText(const QString &newText)
   147 void HbExactWordPopup::setText(const QString &newText)
   172 {
   148 {
   173     Q_D(HbExactWordPopup);
   149     Q_D(HbExactWordPopup);
   174     d->mText->setPlainText(newText);
   150     d->mText->setPlainText(newText);
   175 
       
   176     QRectF ps=QRectF(QPointF(0,0), d->mText->preferredSize()).adjusted(-9,-9,9,9);
       
   177     resize(ps.size());
       
   178 }
   151 }
   179 
   152 
   180 /*!
   153 /*!
   181     Displays exact word popup at a given point
   154     Displays exact word popup at a given point
   182 
   155 
   183     \sa hideText()
   156     \sa hideText()
   184 */
   157 */
   185 void HbExactWordPopup::showText(QPointF pos)
   158 void HbExactWordPopup::showText(QPointF pos)
   186 {
   159 {
   187     // the popup should know at this stage in which main window/scene it's been launched at.
   160     Q_D(HbExactWordPopup);
   188     int screenWidth = 0;
   161 
   189     if ( mainWindow() ) {
   162     QFontMetrics fontMetrics(HbFontSpec(HbFontSpec::Primary).font());
   190         screenWidth = HbDeviceProfile::profile(this).logicalSize().width();
   163     qreal width = fontMetrics.width(text());
   191     } else {
   164 
   192         // this is the fall-back if the main window is not know - can be removed when popup
   165     d->mText->setMinimumWidth(width);
   193         // is not relying on the primary window anymore.
   166     d->mText->setMaximumWidth(width);
   194         screenWidth = HbDeviceProfile::profile(mainWindow()).logicalSize().width();
   167   
   195     }
   168     pos.setY(pos.y() - HbExactWordPopupHeight);
   196 
       
   197     const QRectF br = boundingRect();
       
   198     const qreal brCenter = br.width()/2;
       
   199     pos.setX(pos.x()-brCenter);
       
   200     // fix x position to keep tooltip visible
       
   201     const qreal requiredWidth = pos.x()+br.width();
       
   202     if (requiredWidth > screenWidth) {
       
   203         pos.setX(pos.x()-(requiredWidth-screenWidth));
       
   204     } else if (0 > pos.x()) {
       
   205         pos.setX(0);
       
   206     }
       
   207     pos.setY(pos.y()-br.height());
       
   208     setPos(pos);
   169     setPos(pos);
   209 
       
   210     QSizeF mySize = size();
       
   211     mySize.setHeight(HbExactWordPopupSize.height());
       
   212     resize(mySize);    
       
   213 
       
   214     Q_D(HbExactWordPopup);
       
   215     d->mOption->boundingRect = QRectF(rect().center().x() - (HbExactWordPopupSize.width()/2),rect().bottom(),HbExactWordPopupSize.width(),HbExactWordPopupSize.height());
       
   216     style()->updatePrimitive(d->iconPrim, (HbStyle::Primitive)(HbStyle::P_Label_icon), d->mOption);
       
   217 
   170 
   218     show();
   171     show();
   219 }
   172 }
   220 
   173 
   221 /*!
   174 /*!
   241 
   194 
   242 }
   195 }
   243 
   196 
   244 void HbExactWordPopup::updatePrimitives()
   197 void HbExactWordPopup::updatePrimitives()
   245 {
   198 {
   246     Q_D( HbExactWordPopup );
   199     Q_D(HbExactWordPopup);
   247 
   200 
   248     d->mPopupBackground->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
   201     d->mPopupBackground->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
   249     d->mPopupBackground->frameDrawer().setFrameGraphicsName("qtg_fr_popup_secondary");
   202     d->mPopupBackground->frameDrawer().setFrameGraphicsName("qtg_fr_popup_secondary");
   250     d->mPopupBackground->setGeometry(boundingRect());
   203     d->mPopupBackground->setGeometry(boundingRect());
   251 
   204 
   255     }
   208     }
   256 }
   209 }
   257 
   210 
   258 // this method is called whenever there is a switch of keypad usage from h/w to virtual
   211 // this method is called whenever there is a switch of keypad usage from h/w to virtual
   259 // h/w keypad needs an indicator, whereas virtual does not, hence set the image appropriately.
   212 // h/w keypad needs an indicator, whereas virtual does not, hence set the image appropriately.
   260 void HbExactWordPopup::setIndicatorArrow( HbExactWordPopupIndicator indicatorArrow )
   213 void HbExactWordPopup::setIndicatorArrow(HbExactWordPopupIndicator indicatorArrow)
   261 {
   214 {
   262     Q_D( HbExactWordPopup );
   215     Q_D(HbExactWordPopup);
   263 
   216 
   264     if (indicatorArrow == HbNoIndicatorArrow) {
   217     if (indicatorArrow == HbNoIndicatorArrow) {
   265         d->mOption->icon = (QString(""));
   218         d->mText->setIcon(HbIcon());
   266     } else {
   219     } else {
   267         d->mOption->icon = (QString("qtg_graf_inpu_swipe"));
   220         d->mText->setIcon(HbIcon("qtg_graf_inpu_swipe"));
   268    }
   221    }
   269 }
   222 }