tools/assistant/lib/qhelpsearchquerywidget.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "qhelpsearchquerywidget.h"
    42 #include "qhelpsearchquerywidget.h"
    43 
    43 
    44 #include <QtCore/QDebug>
       
    45 
       
    46 #include <QtCore/QAbstractListModel>
    44 #include <QtCore/QAbstractListModel>
    47 #include <QtCore/QObject>
    45 #include <QtCore/QObject>
    48 #include <QtCore/QStringList>
    46 #include <QtCore/QStringList>
    49 #include <QtCore/QtGlobal>
    47 #include <QtCore/QtGlobal>
    50 
    48 
    99     private:
    97     private:
   100         QStringList termList;
    98         QStringList termList;
   101     };
    99     };
   102 
   100 
   103     QHelpSearchQueryWidgetPrivate()
   101     QHelpSearchQueryWidgetPrivate()
   104         : QObject(), simpleSearch(true),
   102         : QObject()
   105           searchCompleter(new CompleterModel(this), this)
   103         , simpleSearch(true)
       
   104         , searchCompleter(new CompleterModel(this), this)
   106     {
   105     {
   107         searchButton = 0;
   106         searchButton = 0;
   108         advancedSearchWidget = 0;
   107         advancedSearchWidget = 0;
   109         showHideAdvancedSearchButton = 0;
   108         showHideAdvancedSearchButton = 0;
   110         defaultQuery = 0;
   109         defaultQuery = 0;
   132         withoutLabel->setText(QHelpSearchQueryWidget::tr("<B>without</B> the words:"));
   131         withoutLabel->setText(QHelpSearchQueryWidget::tr("<B>without</B> the words:"));
   133         exactLabel->setText(QHelpSearchQueryWidget::tr("with <B>exact phrase</B>:"));
   132         exactLabel->setText(QHelpSearchQueryWidget::tr("with <B>exact phrase</B>:"));
   134         allLabel->setText(QHelpSearchQueryWidget::tr("with <B>all</B> of the words:"));
   133         allLabel->setText(QHelpSearchQueryWidget::tr("with <B>all</B> of the words:"));
   135         atLeastLabel->setText(QHelpSearchQueryWidget::tr("with <B>at least one</B> of the words:"));
   134         atLeastLabel->setText(QHelpSearchQueryWidget::tr("with <B>at least one</B> of the words:"));
   136 #endif
   135 #endif
   137     }
       
   138 
       
   139     QString escapeString(const QString &text)
       
   140     {
       
   141         QString retValue = text;
       
   142         const QString escape(QLatin1String("\\"));
       
   143         QStringList escapableCharsList;
       
   144         escapableCharsList << QLatin1String("\\") << QLatin1String("+")
       
   145             << QLatin1String("-") << QLatin1String("!") << QLatin1String("(")
       
   146             << QLatin1String(")") << QLatin1String(":") << QLatin1String("^")
       
   147             << QLatin1String("[") << QLatin1String("]") << QLatin1String("{")
       
   148             << QLatin1String("}") << QLatin1String("~");
       
   149 
       
   150         // make sure we won't end up with an empty string
       
   151         foreach (const QString &escapeChar, escapableCharsList) {
       
   152             if (retValue.contains(escapeChar))
       
   153                 retValue.replace(escapeChar, QLatin1String(""));
       
   154         }
       
   155         if (retValue.trimmed().isEmpty())
       
   156             return retValue;
       
   157 
       
   158         retValue = text; // now really escape the string...
       
   159         foreach (const QString &escapeChar, escapableCharsList) {
       
   160             if (retValue.contains(escapeChar))
       
   161                 retValue.replace(escapeChar, escape + escapeChar);
       
   162         }
       
   163         return retValue;
       
   164     }
   136     }
   165 
   137 
   166     QStringList buildTermList(const QString query)
   138     QStringList buildTermList(const QString query)
   167     {
   139     {
   168         bool s = false;
   140         bool s = false;
   220                         addTerm(queryPart.wordList.join(" "));
   192                         addTerm(queryPart.wordList.join(" "));
   221             }
   193             }
   222         }
   194         }
   223     }
   195     }
   224 
   196 
   225     void nextOrPrevQuery(int maxOrMinIndex, int addend,
   197     void nextOrPrevQuery(int maxOrMinIndex, int addend, QToolButton *thisButton,
   226                          QToolButton *thisButton, QToolButton *otherButton)
   198         QToolButton *otherButton)
   227     {
   199     {
   228         QueryHistory *queryHist;
   200         QueryHistory *queryHist;
   229         QList<QLineEdit *> lineEdits;
   201         QList<QLineEdit *> lineEdits;
   230         if (simpleSearch) {
   202         if (simpleSearch) {
   231             queryHist = &simpleQueries;
   203             queryHist = &simpleQueries;
   232             lineEdits << defaultQuery;
   204             lineEdits << defaultQuery;
   233         } else {
   205         } else {
   234             queryHist = &complexQueries;
   206             queryHist = &complexQueries;
   235             lineEdits << allQuery << atLeastQuery << similarQuery
   207             lineEdits << allQuery << atLeastQuery << similarQuery
   236                     << withoutQuery << exactQuery;
   208                 << withoutQuery << exactQuery;
   237         }
   209         }
   238         foreach (QLineEdit *lineEdit, lineEdits)
   210         foreach (QLineEdit *lineEdit, lineEdits)
   239             lineEdit->clear();
   211             lineEdit->clear();
   240 
   212 
   241         // Otherwise, the respective button would be disabled.
   213         // Otherwise, the respective button would be disabled.
   276         otherButton->setEnabled(true);
   248         otherButton->setEnabled(true);
   277     }
   249     }
   278 
   250 
   279     void enableOrDisableToolButtons()
   251     void enableOrDisableToolButtons()
   280     {
   252     {
   281         const QueryHistory &queryHist =
   253         const QueryHistory &queryHist = simpleSearch ? simpleQueries
   282                 simpleSearch ? simpleQueries : complexQueries;
   254             : complexQueries;
   283         prevQueryButton->setEnabled(queryHist.curQuery > 0);
   255         prevQueryButton->setEnabled(queryHist.curQuery > 0);
   284         nextQueryButton->setEnabled(queryHist.curQuery <
   256         nextQueryButton->setEnabled(queryHist.curQuery
   285                                     queryHist.queries.size() - 1);
   257             < queryHist.queries.size() - 1);
   286     }
   258     }
   287 
   259 
   288 private slots:
   260 private slots:
   289     void showHideAdvancedSearch()
   261     void showHideAdvancedSearch()
   290     {
   262     {
   304     void searchRequested()
   276     void searchRequested()
   305     {
   277     {
   306         QList<QHelpSearchQuery> queryList;
   278         QList<QHelpSearchQuery> queryList;
   307 #if !defined(QT_CLUCENE_SUPPORT)
   279 #if !defined(QT_CLUCENE_SUPPORT)
   308         queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT,
   280         queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT,
   309                                           QStringList(defaultQuery->text())));
   281             QStringList(defaultQuery->text())));
   310 
   282 
   311 #else
   283 #else
   312         if (defaultQuery->isEnabled()) {
   284         if (defaultQuery->isEnabled()) {
   313             queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT,
   285             queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT,
   314                                               buildTermList(escapeString(defaultQuery->text()))));
   286                 buildTermList(defaultQuery->text())));
   315         } else {
   287         } else {
   316             const QRegExp exp(QLatin1String("\\s+"));
   288             const QRegExp exp(QLatin1String("\\s+"));
   317             QStringList lst = similarQuery->text().split(exp, QString::SkipEmptyParts);
   289             QStringList lst = similarQuery->text().split(exp,
       
   290                 QString::SkipEmptyParts);
   318             if (!lst.isEmpty()) {
   291             if (!lst.isEmpty()) {
   319                 QStringList fuzzy;
   292                 QStringList fuzzy;
   320                 foreach (const QString &term, lst)
   293                 foreach (const QString &term, lst)
   321                     fuzzy += buildTermList(escapeString(term));
   294                     fuzzy += buildTermList(term);
   322                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY, fuzzy));
   295                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY,
       
   296                     fuzzy));
   323             }
   297             }
   324 
   298 
   325             lst = withoutQuery->text().split(exp, QString::SkipEmptyParts);
   299             lst = withoutQuery->text().split(exp, QString::SkipEmptyParts);
   326             if (!lst.isEmpty()) {
   300             if (!lst.isEmpty()) {
   327                 QStringList without;
   301                 QStringList without;
   328                 foreach (const QString &term, lst)
   302                 foreach (const QString &term, lst)
   329                     without.append(escapeString(term));
   303                     without.append(term);
   330                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT, without));
   304                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT,
       
   305                     without));
   331             }
   306             }
   332 
   307 
   333             if (!exactQuery->text().isEmpty()) {
   308             if (!exactQuery->text().isEmpty()) {
   334                 QString phrase = exactQuery->text().remove(QLatin1Char('\"'));
   309                 QString phrase = exactQuery->text().remove(QLatin1Char('\"'));
   335                 phrase = escapeString(phrase.simplified());
   310                 phrase = phrase.simplified();
   336                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::PHRASE, QStringList(phrase)));
   311                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::PHRASE,
       
   312                     QStringList(phrase)));
   337             }
   313             }
   338 
   314 
   339             lst = allQuery->text().split(exp, QString::SkipEmptyParts);
   315             lst = allQuery->text().split(exp, QString::SkipEmptyParts);
   340             if (!lst.isEmpty()) {
   316             if (!lst.isEmpty()) {
   341                 QStringList all;
   317                 QStringList all;
   342                 foreach (const QString &term, lst)
   318                 foreach (const QString &term, lst)
   343                     all.append(escapeString(term));
   319                     all.append(term);
   344                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all));
   320                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all));
   345             }
   321             }
   346 
   322 
   347             lst = atLeastQuery->text().split(exp, QString::SkipEmptyParts);
   323             lst = atLeastQuery->text().split(exp, QString::SkipEmptyParts);
   348             if (!lst.isEmpty()) {
   324             if (!lst.isEmpty()) {
   349                 QStringList atLeast;
   325                 QStringList atLeast;
   350                 foreach (const QString &term, lst)
   326                 foreach (const QString &term, lst)
   351                     atLeast += buildTermList(escapeString(term));
   327                     atLeast += buildTermList(term);
   352                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST, atLeast));
   328                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST,
       
   329                     atLeast));
   353             }
   330             }
   354         }
   331         }
   355 #endif
   332 #endif
   356         QueryHistory &queryHist = simpleSearch ? simpleQueries : complexQueries;
   333         QueryHistory &queryHist = simpleSearch ? simpleQueries : complexQueries;
   357         saveQuery(queryList, queryHist);
   334         saveQuery(queryList, queryHist);
   361         nextQueryButton->setEnabled(false);
   338         nextQueryButton->setEnabled(false);
   362     }
   339     }
   363 
   340 
   364     void nextQuery()
   341     void nextQuery()
   365     {
   342     {
   366         nextOrPrevQuery((simpleSearch ? simpleQueries : complexQueries).queries.size() - 1,
   343         nextOrPrevQuery((simpleSearch ? simpleQueries
   367                         1, nextQueryButton, prevQueryButton);
   344             : complexQueries).queries.size() - 1, 1, nextQueryButton,
       
   345                 prevQueryButton);
   368     }
   346     }
   369 
   347 
   370     void prevQuery()
   348     void prevQuery()
   371     {
   349     {
   372         nextOrPrevQuery(0, -1, prevQueryButton, nextQueryButton);
   350         nextOrPrevQuery(0, -1, prevQueryButton, nextQueryButton);
   413 
   391 
   414 /*!
   392 /*!
   415     \fn void QHelpSearchQueryWidget::search()
   393     \fn void QHelpSearchQueryWidget::search()
   416 
   394 
   417     This signal is emitted when a the user has the search button invoked.
   395     This signal is emitted when a the user has the search button invoked.
   418     After reciving the signal you can ask the QHelpSearchQueryWidget for the build list
   396     After reciving the signal you can ask the QHelpSearchQueryWidget for the
   419     of QHelpSearchQuery's that you may pass to the QHelpSearchEngine's search() function.
   397     build list of QHelpSearchQuery's that you may pass to the QHelpSearchEngine's
       
   398     search() function.
   420 */
   399 */
   421 
   400 
   422 /*!
   401 /*!
   423     Constructs a new search query widget with the given \a parent.
   402     Constructs a new search query widget with the given \a parent.
   424 */
   403 */
   542         d->simpleSearch ? d->simpleQueries : d->complexQueries;
   521         d->simpleSearch ? d->simpleQueries : d->complexQueries;
   543     return queryHist.queries.isEmpty() ?
   522     return queryHist.queries.isEmpty() ?
   544         QList<QHelpSearchQuery>() : queryHist.queries.last();
   523         QList<QHelpSearchQuery>() : queryHist.queries.last();
   545 }
   524 }
   546 
   525 
   547 /*! \reimp
   526 /*!
       
   527     \reimp
   548 */
   528 */
   549 void QHelpSearchQueryWidget::focusInEvent(QFocusEvent *focusEvent)
   529 void QHelpSearchQueryWidget::focusInEvent(QFocusEvent *focusEvent)
   550 {
   530 {
   551     if (focusEvent->reason() != Qt::MouseFocusReason) {
   531     if (focusEvent->reason() != Qt::MouseFocusReason) {
   552         d->defaultQuery->selectAll();
   532         d->defaultQuery->selectAll();