examples/network/googlesuggest/googlesuggest.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include <QtCore>
    42 
    43 #include <QtGui>
    43 //! [1]
    44 #include <QtNetwork>
       
    45 
       
    46 #include "googlesuggest.h"
    44 #include "googlesuggest.h"
    47 
    45 
    48 #define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
    46 #define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
    49 
    47 //! [1]
       
    48 
       
    49 //! [2]
    50 GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
    50 GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
    51 {
    51 {
    52     popup = new QTreeWidget;
    52     popup = new QTreeWidget;
       
    53     popup->setWindowFlags(Qt::Popup);
       
    54     popup->setFocusPolicy(Qt::NoFocus);
       
    55     popup->setFocusProxy(parent);
       
    56     popup->setMouseTracking(true);
       
    57 
    53     popup->setColumnCount(2);
    58     popup->setColumnCount(2);
    54     popup->setUniformRowHeights(true);
    59     popup->setUniformRowHeights(true);
    55     popup->setRootIsDecorated(false);
    60     popup->setRootIsDecorated(false);
    56     popup->setEditTriggers(QTreeWidget::NoEditTriggers);
    61     popup->setEditTriggers(QTreeWidget::NoEditTriggers);
    57     popup->setSelectionBehavior(QTreeWidget::SelectRows);
    62     popup->setSelectionBehavior(QTreeWidget::SelectRows);
    58     popup->setFrameStyle(QFrame::Box | QFrame::Plain);
    63     popup->setFrameStyle(QFrame::Box | QFrame::Plain);
    59     popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    64     popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    60 
       
    61     popup->header()->hide();
    65     popup->header()->hide();
       
    66 
    62     popup->installEventFilter(this);
    67     popup->installEventFilter(this);
    63     popup->setMouseTracking(true);
    68 
    64 
    69     connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
    65     connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
       
    66             SLOT(doneCompletion()));
    70             SLOT(doneCompletion()));
    67 
       
    68     popup->setWindowFlags(Qt::Popup);
       
    69     popup->setFocusPolicy(Qt::NoFocus);
       
    70     popup->setFocusProxy(parent);
       
    71 
    71 
    72     timer = new QTimer(this);
    72     timer = new QTimer(this);
    73     timer->setSingleShot(true);
    73     timer->setSingleShot(true);
    74     timer->setInterval(500);
    74     timer->setInterval(500);
    75     connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
    75     connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
    77 
    77 
    78     connect(&networkManager, SIGNAL(finished(QNetworkReply*)),
    78     connect(&networkManager, SIGNAL(finished(QNetworkReply*)),
    79             this, SLOT(handleNetworkData(QNetworkReply*)));
    79             this, SLOT(handleNetworkData(QNetworkReply*)));
    80 
    80 
    81 }
    81 }
    82 
    82 //! [2]
       
    83 
       
    84 //! [3]
    83 GSuggestCompletion::~GSuggestCompletion()
    85 GSuggestCompletion::~GSuggestCompletion()
    84 {
    86 {
    85     delete popup;
    87     delete popup;
    86 }
    88 }
    87 
    89 //! [3]
       
    90 
       
    91 //! [4]
    88 bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
    92 bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
    89 {
    93 {
    90     if (obj != popup)
    94     if (obj != popup)
    91         return false;
    95         return false;
    92 
    96 
   129         return consumed;
   133         return consumed;
   130     }
   134     }
   131 
   135 
   132     return false;
   136     return false;
   133 }
   137 }
   134 
   138 //! [4]
       
   139 
       
   140 //! [5]
   135 void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
   141 void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
   136 {
   142 {
       
   143 
   137     if (choices.isEmpty() || choices.count() != hits.count())
   144     if (choices.isEmpty() || choices.count() != hits.count())
   138         return;
   145         return;
   139 
   146 
   140     const QPalette &pal = editor->palette();
   147     const QPalette &pal = editor->palette();
   141     QColor color = pal.color(QPalette::Disabled, QPalette::WindowText);
   148     QColor color = pal.color(QPalette::Disabled, QPalette::WindowText);
   161 
   168 
   162     popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
   169     popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
   163     popup->setFocus();
   170     popup->setFocus();
   164     popup->show();
   171     popup->show();
   165 }
   172 }
   166 
   173 //! [5]
       
   174 
       
   175 //! [6]
   167 void GSuggestCompletion::doneCompletion()
   176 void GSuggestCompletion::doneCompletion()
   168 {
   177 {
   169     timer->stop();
   178     timer->stop();
   170     popup->hide();
   179     popup->hide();
   171     editor->setFocus();
   180     editor->setFocus();
   172     QTreeWidgetItem *item = popup->currentItem();
   181     QTreeWidgetItem *item = popup->currentItem();
   173     if (item) {
   182     if (item) {
   174         editor->setText(item->text(0));
   183         editor->setText(item->text(0));
   175         QKeyEvent *e;
   184         QMetaObject::invokeMethod(editor, "returnPressed");
   176         e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
   185     }
   177         QApplication::postEvent(editor, e);
   186 }
   178         e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
   187 //! [6]
   179         QApplication::postEvent(editor, e);
   188 
   180     }
   189 //! [7]
   181 }
       
   182 
       
   183 void GSuggestCompletion::preventSuggest()
       
   184 {
       
   185     timer->stop();
       
   186 }
       
   187 
       
   188 void GSuggestCompletion::autoSuggest()
   190 void GSuggestCompletion::autoSuggest()
   189 {
   191 {
   190     QString str = editor->text();
   192     QString str = editor->text();
   191     QString url = QString(GSUGGEST_URL).arg(str);
   193     QString url = QString(GSUGGEST_URL).arg(str);
   192     networkManager.get(QNetworkRequest(QString(url)));
   194     networkManager.get(QNetworkRequest(QString(url)));
   193 }
   195 }
   194 
   196 //! [7]
       
   197 
       
   198 //! [8]
       
   199 void GSuggestCompletion::preventSuggest()
       
   200 {
       
   201     timer->stop();
       
   202 }
       
   203 //! [8]
       
   204 
       
   205 //! [9]
   195 void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
   206 void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
   196 {
   207 {
   197     QUrl url = networkReply->url();
   208     QUrl url = networkReply->url();
   198     if (!networkReply->error()) {
   209     if (!networkReply->error()) {
   199         QStringList choices;
   210         QStringList choices;
   200         QStringList hits;
   211         QStringList hits;
   201 
   212 
   202         QString response(networkReply->readAll());
   213         QByteArray response(networkReply->readAll());
   203         QXmlStreamReader xml(response);
   214         QXmlStreamReader xml(response);
   204         while (!xml.atEnd()) {
   215         while (!xml.atEnd()) {
   205             xml.readNext();
   216             xml.readNext();
   206             if (xml.isStartElement()) {
   217             if (xml.tokenType() == QXmlStreamReader::StartElement)
   207                 if (xml.name() == "suggestion") {
   218                 if (xml.name() == "suggestion") {
   208                     QStringRef str = xml.attributes().value("data");
   219                     QStringRef str = xml.attributes().value("data");
   209                     choices << str.toString();
   220                     choices << str.toString();
   210                 }
   221                 }
   211                 else if (xml.name() == "num_queries") {
   222             if (xml.tokenType() == QXmlStreamReader::StartElement)
       
   223                 if (xml.name() == "num_queries") {
   212                     QStringRef str = xml.attributes().value("int");
   224                     QStringRef str = xml.attributes().value("int");
   213                     hits << str.toString();
   225                     hits << str.toString();
   214                 }
   226                 }
   215             }
       
   216         }
   227         }
   217 
   228 
   218         showCompletion(choices, hits);
   229         showCompletion(choices, hits);
   219     }
   230     }
   220 
   231 
   221     networkReply->deleteLater();
   232     networkReply->deleteLater();
   222 }
   233 }
       
   234 //! [9]