src/hbinput/inputwidgets/hbinputcandidatelist.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 "hbinputcandidatelist.h"
    25 
    26 
    26 #include <QLabel>
    27 #include <QLabel>
    27 #include <QGraphicsLayout>
    28 #include <QGraphicsLayout>
    28 #include <hblistwidget.h>
    29 #include <hblistwidget.h>
    29 #include <hblistwidgetitem.h>
    30 #include <hblistwidgetitem.h>
    36 #include <hbinputmethod.h>
    37 #include <hbinputmethod.h>
    37 #include <hbinputsettingproxy.h>
    38 #include <hbinputsettingproxy.h>
    38 #include <hbinputvkbhost.h>
    39 #include <hbinputvkbhost.h>
    39 #include <hbinputregioncollector_p.h>
    40 #include <hbinputregioncollector_p.h>
    40 
    41 
    41 #include "hbinputcandidatelist.h"
       
    42 
       
    43 #include "hbdialog_p.h"
    42 #include "hbdialog_p.h"
    44 
    43 
    45 const int HbCandListDefaultNumRows = 5;
    44 const int HbCandListDefaultNumRows = 5;
    46 const qreal HbCandListMaxWidthMultiplier = 0.8;
    45 const qreal HbCandListMaxWidthMultiplier = 0.8;
    47 const qreal HbCandListMinWidth = 30.0;
    46 const qreal HbCandListMinWidth = 30.0;
    51 
    50 
    52 /// @cond
    51 /// @cond
    53 
    52 
    54 class HbCandidateListPrivate : public HbDialogPrivate
    53 class HbCandidateListPrivate : public HbDialogPrivate
    55 {
    54 {
    56    Q_DECLARE_PUBLIC(HbCandidateList)
    55     Q_DECLARE_PUBLIC(HbCandidateList)
    57 
    56 
    58 public:
    57 public:
    59     HbCandidateListPrivate(HbInputMethod* input);
    58     HbCandidateListPrivate(HbInputMethod *input);
    60     ~HbCandidateListPrivate();
    59     ~HbCandidateListPrivate();
    61     void calculateAndSetSize(qreal maxWidth);
    60     void calculateAndSetSize(qreal maxWidth);
    62     void initFrameIcon();
    61     void initFrameIcon();
    63 
    62 
    64 public:
    63 public:
    65     HbListWidget* mList;
    64     HbListWidget *mList;
    66     HbInputMethod* mInput;
    65     HbInputMethod *mInput;
    67     int numRows;
    66     int numRows;
    68     int numCandidates;
    67     int numCandidates;
    69     int longestStringWidth;
    68     int longestStringWidth;
    70     HbFrameItem *mFrameBackground;
    69     HbFrameItem *mFrameBackground;
    71     HbListWidgetItem* mSpellQueryItem;
    70     HbListWidgetItem *mSpellQueryItem;
    72     bool mCandidateCommitted;
    71     bool mCandidateCommitted;
    73     bool mSpellQueryOpenIsPending;
    72     bool mSpellQueryOpenIsPending;
    74 };
    73 };
    75 
    74 
    76 HbCandidateListPrivate::HbCandidateListPrivate(HbInputMethod* input)
    75 HbCandidateListPrivate::HbCandidateListPrivate(HbInputMethod *input)
    77     : mInput(input),
    76     : mInput(input),
    78       numRows(HbCandListDefaultNumRows),
    77       numRows(HbCandListDefaultNumRows),
    79       numCandidates(0),
    78       numCandidates(0),
    80       longestStringWidth(0),
    79       longestStringWidth(0),
    81       mFrameBackground(0),
    80       mFrameBackground(0),
    95 
    94 
    96 void HbCandidateListPrivate::initFrameIcon()
    95 void HbCandidateListPrivate::initFrameIcon()
    97 {
    96 {
    98     Q_Q(HbCandidateList);
    97     Q_Q(HbCandidateList);
    99 
    98 
   100     mFrameBackground = static_cast<HbFrameItem*>( q->primitive( HbStyle::P_Popup_background ));
    99     mFrameBackground = static_cast<HbFrameItem *>(q->primitive(HbStyle::P_Popup_background));
   101 
   100 
   102     if( mFrameBackground == 0 ) {
   101     if (mFrameBackground == 0) {
   103         mFrameBackground = static_cast<HbFrameItem*>( q->style()->createPrimitive(( HbStyle::Primitive )( HbStyle::P_Popup_background ), q ));
   102         mFrameBackground = static_cast<HbFrameItem *>(q->style()->createPrimitive((HbStyle::Primitive)(HbStyle::P_Popup_background), q));
   104     }
   103     }
   105 }
   104 }
   106 
   105 
   107 
   106 
   108 void HbCandidateListPrivate::calculateAndSetSize(qreal maxWidth)
   107 void HbCandidateListPrivate::calculateAndSetSize(qreal maxWidth)
   126     }
   125     }
   127 
   126 
   128     finalWidth = finalWidth + l + r ;
   127     finalWidth = finalWidth + l + r ;
   129     finalHeight = (qreal)numLines * oneLineHeight + 5.0 + t + b;
   128     finalHeight = (qreal)numLines * oneLineHeight + 5.0 + t + b;
   130 
   129 
   131     if(mSpellQueryItem) {
   130     if (mSpellQueryItem) {
   132         finalHeight += oneLineHeight ; // for spell button
   131         finalHeight += oneLineHeight ; // for spell button
   133     }
   132     }
   134 
   133 
   135     if(finalHeight > HbDeviceProfile::current().logicalSize().height() - 30) {
   134     if (finalHeight > HbDeviceProfile::current().logicalSize().height() - 30) {
   136         finalHeight = HbDeviceProfile::current().logicalSize().height() - 30;
   135         finalHeight = HbDeviceProfile::current().logicalSize().height() - 30;
   137     }
   136     }
   138 
   137 
   139     geom.setHeight(finalHeight);
   138     geom.setHeight(finalHeight);
   140     geom.setWidth(finalWidth);
   139     geom.setWidth(finalWidth);
   160 /*!
   159 /*!
   161 Constructor.
   160 Constructor.
   162 @param input The input method that uses this widget.
   161 @param input The input method that uses this widget.
   163 @param parent parent of the widget.
   162 @param parent parent of the widget.
   164 */
   163 */
   165 HbCandidateList::HbCandidateList(HbInputMethod* input, QGraphicsItem* parent)
   164 HbCandidateList::HbCandidateList(HbInputMethod *input, QGraphicsItem *parent)
   166     : HbDialog(*new HbCandidateListPrivate(input), parent)
   165     : HbDialog(*new HbCandidateListPrivate(input), parent)
   167 {
   166 {
   168     Q_D(HbCandidateList);
   167     Q_D(HbCandidateList);
   169 
   168 
   170     HbInputRegionCollector::instance()->attach(this);
   169     HbInputRegionCollector::instance()->attach(this);
   176     setFlag(QGraphicsItem::ItemIsPanel, true);
   175     setFlag(QGraphicsItem::ItemIsPanel, true);
   177     setActive(false);
   176     setActive(false);
   178 
   177 
   179     setTimeout(NoTimeout);
   178     setTimeout(NoTimeout);
   180     setAttribute(Qt::WA_InputMethodEnabled, false);
   179     setAttribute(Qt::WA_InputMethodEnabled, false);
   181     connect(d->mList, SIGNAL(activated(HbListWidgetItem*)), this, SLOT(itemActivated(HbListWidgetItem*)));
   180     connect(d->mList, SIGNAL(activated(HbListWidgetItem *)), this, SLOT(itemActivated(HbListWidgetItem *)));
   182     connect(d->mList, SIGNAL(longPressed(HbListWidgetItem*, const QPointF&)), this, SLOT(itemActivated(HbListWidgetItem*)));
   181     connect(d->mList, SIGNAL(longPressed(HbListWidgetItem *, const QPointF &)), this, SLOT(itemActivated(HbListWidgetItem *)));
   183 
   182 
   184     setBackgroundFaded(false);
   183     setBackgroundFaded(false);
   185 }
   184 }
   186 
   185 
   187 /*!
   186 /*!
   191 {
   190 {
   192 }
   191 }
   193 
   192 
   194 /*!
   193 /*!
   195 Populates the candidate list with text strings given as parameter.
   194 Populates the candidate list with text strings given as parameter.
   196 
   195 */
   197 @param
   196 void HbCandidateList::populateList(const QStringList &candidates, bool addSpellQuery)
   198 */
       
   199 void HbCandidateList::populateList(const QStringList& candidates,bool addSpellQuery)
       
   200 {
   197 {
   201     Q_D(HbCandidateList);
   198     Q_D(HbCandidateList);
   202 
   199 
   203     setContentWidget(d->mList);
   200     setContentWidget(d->mList);
   204 
   201 
   206     d->mList->clear();
   203     d->mList->clear();
   207 
   204 
   208     int longestwidth = 0;
   205     int longestwidth = 0;
   209     int finalWidth = 250;
   206     int finalWidth = 250;
   210     for (int i = 0; i < candidates.count(); i++) {
   207     for (int i = 0; i < candidates.count(); i++) {
   211         HbListWidgetItem* item = new HbListWidgetItem();
   208         HbListWidgetItem *item = new HbListWidgetItem();
   212         item->setText(candidates[i]);
   209         item->setText(candidates[i]);
   213         d->mList->addItem(item);
   210         d->mList->addItem(item);
   214 
   211 
   215         // TODO: Font has not been set corretly yet...
   212         // TODO: Font has not been set corretly yet...
   216         QFontMetrics fontMetrics(d->mList->fontSpec().font());
   213         QFontMetrics fontMetrics(d->mList->fontSpec().font());
   217         finalWidth = fontMetrics.width(candidates[i]);
   214         finalWidth = fontMetrics.width(candidates[i]);
   218         if (finalWidth > longestwidth) {
   215         if (finalWidth > longestwidth) {
   219             longestwidth = finalWidth;
   216             longestwidth = finalWidth;
   220         }
   217         }
   221     }
   218     }
   222     
   219 
   223     d->mSpellQueryItem = 0;
   220     d->mSpellQueryItem = 0;
   224     if(addSpellQuery) {
   221     if (addSpellQuery) {
   225         d->mSpellQueryItem = new HbListWidgetItem();
   222         d->mSpellQueryItem = new HbListWidgetItem();
   226         d->mSpellQueryItem->setText(tr("Spell"));
   223         d->mSpellQueryItem->setText(tr("Spell"));
   227         d->mList->addItem(d->mSpellQueryItem);
   224         d->mList->addItem(d->mSpellQueryItem);
   228         QFontMetrics fontMetrics(d->mList->fontSpec().font());
   225         QFontMetrics fontMetrics(d->mList->fontSpec().font());
   229         finalWidth = fontMetrics.width(tr("Spell"));
   226         finalWidth = fontMetrics.width(tr("Spell"));
   244 }
   241 }
   245 
   242 
   246 /*!
   243 /*!
   247 Inherited from HbDialog.
   244 Inherited from HbDialog.
   248 */
   245 */
   249 void HbCandidateList::keyPressEvent(QKeyEvent* event)
   246 void HbCandidateList::keyPressEvent(QKeyEvent *event)
   250 {
   247 {
   251     Q_D(HbCandidateList);
   248     Q_D(HbCandidateList);
   252 
   249 
   253     if (event->key() == Qt::Key_Space
   250     if (event->key() == Qt::Key_Space
   254         || event->key() == Qt::Key_Enter
   251         || event->key() == Qt::Key_Enter
   277 */
   274 */
   278 void HbCandidateList::itemActivated(HbListWidgetItem *item)
   275 void HbCandidateList::itemActivated(HbListWidgetItem *item)
   279 {
   276 {
   280     Q_D(HbCandidateList);
   277     Q_D(HbCandidateList);
   281     if (!d->mCandidateCommitted)  {
   278     if (!d->mCandidateCommitted)  {
   282         if(d->mSpellQueryItem != item) {
   279         if (d->mSpellQueryItem != item) {
   283             emit candidateSelected(0, currentCandidate());
   280             emit candidateSelected(0, currentCandidate());
   284         } else if(d->mSpellQueryItem){
   281         } else if (d->mSpellQueryItem) {
   285             d->mSpellQueryOpenIsPending = true;
   282             d->mSpellQueryOpenIsPending = true;
   286         }
   283         }
   287         d->mCandidateCommitted = true;
   284         d->mCandidateCommitted = true;
   288     }
   285     }
   289     hide();
   286     hide();
   293 Returns the currently selected candidate on the list.
   290 Returns the currently selected candidate on the list.
   294 */
   291 */
   295 QString HbCandidateList::currentCandidate()
   292 QString HbCandidateList::currentCandidate()
   296 {
   293 {
   297     Q_D(HbCandidateList);
   294     Q_D(HbCandidateList);
   298     return d->mList->currentItem()->text();
   295     if (d->mList->currentItem()) {
       
   296         return d->mList->currentItem()->text();
       
   297     }
       
   298 
       
   299     return QString();
   299 }
   300 }
   300 
   301 
   301 /*!
   302 /*!
   302 Sets the maximum number of visible candidates.
   303 Sets the maximum number of visible candidates.
   303 */
   304 */
   310 }
   311 }
   311 
   312 
   312 /*!
   313 /*!
   313 this event handler is called, for Hide events, is delivered after the widget has been hidden.
   314 this event handler is called, for Hide events, is delivered after the widget has been hidden.
   314 */
   315 */
   315 void HbCandidateList::hideEvent(QHideEvent * event)
   316 void HbCandidateList::hideEvent(QHideEvent *event)
   316 {
   317 {
   317 	Q_D(HbCandidateList);
   318     Q_D(HbCandidateList);
   318 	d->mCandidateCommitted = false;
   319     d->mCandidateCommitted = false;
   319     HbDialog::hideEvent(event);
   320     HbDialog::hideEvent(event);
   320     // If we try to open spell query in itemActivated, first spell query tries to 
   321     // If we try to open spell query in itemActivated, first spell query tries to
   321     // open, and then candidatel list is closed. This creates problems if we 
   322     // open, and then candidatel list is closed. This creates problems if we
   322     // quickly double click on the Spell button. So open spell query only after candidate
   323     // quickly double click on the Spell button. So open spell query only after candidate
   323     // list is closed.
   324     // list is closed.
   324     if(d->mSpellQueryOpenIsPending) {
   325     if (d->mSpellQueryOpenIsPending) {
   325         d->mSpellQueryOpenIsPending = false;
   326         d->mSpellQueryOpenIsPending = false;
   326         emit launchSpellQueryDialog();
   327         emit launchSpellQueryDialog();
   327     }
   328     }
   328 }
   329 }
   329 
   330 
   367                     resize(topRect.width(), topRect.height());
   368                     resize(topRect.width(), topRect.height());
   368                     setPos(topRect.topLeft());
   369                     setPos(topRect.topLeft());
   369                     return true;
   370                     return true;
   370                 }
   371                 }
   371             } else {
   372             } else {
   372               if (bottomRect.height() > HbAutoComplPopupMinAllowedHeight) {
   373                 if (bottomRect.height() > HbAutoComplPopupMinAllowedHeight) {
   373                     qreal finalHeight = (size().height() < bottomRect.height() ? size().height() : bottomRect.height());
   374                     qreal finalHeight = (size().height() < bottomRect.height() ? size().height() : bottomRect.height());
   374                     bottomRect.setHeight(finalHeight);
   375                     bottomRect.setHeight(finalHeight);
   375                     resize(bottomRect.width(), bottomRect.height());
   376                     resize(bottomRect.width(), bottomRect.height());
   376                     setPos(bottomRect.topLeft());
   377                     setPos(bottomRect.topLeft());
   377                     return true;
   378                     return true;