tools/assistant/lib/qhelpsearchquerywidget.cpp
changeset 7 f7bc934e204c
parent 0 1918ee327afb
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the Qt Assistant of the Qt Toolkit.
     7 ** This file is part of the Qt Assistant of the Qt Toolkit.
     8 **
     8 **
   118     ~QHelpSearchQueryWidgetPrivate()
   118     ~QHelpSearchQueryWidgetPrivate()
   119     {
   119     {
   120         // nothing todo
   120         // nothing todo
   121     }
   121     }
   122 
   122 
       
   123     void retranslate()
       
   124     {
       
   125         simpleSearchLabel->setText(QHelpSearchQueryWidget::tr("Search for:"));
       
   126         prevQueryButton->setToolTip(QHelpSearchQueryWidget::tr("Previous search"));
       
   127         nextQueryButton->setToolTip(QHelpSearchQueryWidget::tr("Next search"));
       
   128         searchButton->setText(QHelpSearchQueryWidget::tr("Search"));
       
   129 #ifdef QT_CLUCENE_SUPPORT
       
   130         advancedSearchLabel->setText(QHelpSearchQueryWidget::tr("Advanced search"));
       
   131         similarLabel->setText(QHelpSearchQueryWidget::tr("words <B>similar</B> to:"));
       
   132         withoutLabel->setText(QHelpSearchQueryWidget::tr("<B>without</B> the words:"));
       
   133         exactLabel->setText(QHelpSearchQueryWidget::tr("with <B>exact phrase</B>:"));
       
   134         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:"));
       
   136 #endif
       
   137     }
       
   138 
   123     QString escapeString(const QString &text)
   139     QString escapeString(const QString &text)
   124     {
   140     {
   125         QString retValue = text;
   141         QString retValue = text;
   126         const QString escape(QLatin1String("\\"));
   142         const QString escape(QLatin1String("\\"));
   127         QStringList escapableCharsList;
   143         QStringList escapableCharsList;
   130             << QLatin1String(")") << QLatin1String(":") << QLatin1String("^")
   146             << QLatin1String(")") << QLatin1String(":") << QLatin1String("^")
   131             << QLatin1String("[") << QLatin1String("]") << QLatin1String("{")
   147             << QLatin1String("[") << QLatin1String("]") << QLatin1String("{")
   132             << QLatin1String("}") << QLatin1String("~");
   148             << QLatin1String("}") << QLatin1String("~");
   133 
   149 
   134         // make sure we won't end up with an empty string
   150         // make sure we won't end up with an empty string
   135         foreach (const QString escapeChar, escapableCharsList) {
   151         foreach (const QString &escapeChar, escapableCharsList) {
   136             if (retValue.contains(escapeChar))
   152             if (retValue.contains(escapeChar))
   137                 retValue.replace(escapeChar, QLatin1String(""));
   153                 retValue.replace(escapeChar, QLatin1String(""));
   138         }
   154         }
   139         if (retValue.trimmed().isEmpty())
   155         if (retValue.trimmed().isEmpty())
   140             return retValue;
   156             return retValue;
   141 
   157 
   142         retValue = text; // now realy escape the string...
   158         retValue = text; // now really escape the string...
   143         foreach (const QString escapeChar, escapableCharsList) {
   159         foreach (const QString &escapeChar, escapableCharsList) {
   144             if (retValue.contains(escapeChar))
   160             if (retValue.contains(escapeChar))
   145                 retValue.replace(escapeChar, escape + escapeChar);
   161                 retValue.replace(escapeChar, escape + escapeChar);
   146         }
   162         }
   147         return retValue;
   163         return retValue;
   148     }
   164     }
   299         } else {
   315         } else {
   300             const QRegExp exp(QLatin1String("\\s+"));
   316             const QRegExp exp(QLatin1String("\\s+"));
   301             QStringList lst = similarQuery->text().split(exp, QString::SkipEmptyParts);
   317             QStringList lst = similarQuery->text().split(exp, QString::SkipEmptyParts);
   302             if (!lst.isEmpty()) {
   318             if (!lst.isEmpty()) {
   303                 QStringList fuzzy;
   319                 QStringList fuzzy;
   304                 foreach (const QString term, lst)
   320                 foreach (const QString &term, lst)
   305                     fuzzy += buildTermList(escapeString(term));
   321                     fuzzy += buildTermList(escapeString(term));
   306                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY, fuzzy));
   322                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY, fuzzy));
   307             }
   323             }
   308 
   324 
   309             lst = withoutQuery->text().split(exp, QString::SkipEmptyParts);
   325             lst = withoutQuery->text().split(exp, QString::SkipEmptyParts);
   310             if (!lst.isEmpty()) {
   326             if (!lst.isEmpty()) {
   311                 QStringList without;
   327                 QStringList without;
   312                 foreach (const QString term, lst)
   328                 foreach (const QString &term, lst)
   313                     without.append(escapeString(term));
   329                     without.append(escapeString(term));
   314                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT, without));
   330                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT, without));
   315             }
   331             }
   316 
   332 
   317             if (!exactQuery->text().isEmpty()) {
   333             if (!exactQuery->text().isEmpty()) {
   321             }
   337             }
   322 
   338 
   323             lst = allQuery->text().split(exp, QString::SkipEmptyParts);
   339             lst = allQuery->text().split(exp, QString::SkipEmptyParts);
   324             if (!lst.isEmpty()) {
   340             if (!lst.isEmpty()) {
   325                 QStringList all;
   341                 QStringList all;
   326                 foreach (const QString term, lst)
   342                 foreach (const QString &term, lst)
   327                     all.append(escapeString(term));
   343                     all.append(escapeString(term));
   328                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all));
   344                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all));
   329             }
   345             }
   330 
   346 
   331             lst = atLeastQuery->text().split(exp, QString::SkipEmptyParts);
   347             lst = atLeastQuery->text().split(exp, QString::SkipEmptyParts);
   332             if (!lst.isEmpty()) {
   348             if (!lst.isEmpty()) {
   333                 QStringList atLeast;
   349                 QStringList atLeast;
   334                 foreach (const QString term, lst)
   350                 foreach (const QString &term, lst)
   335                     atLeast += buildTermList(escapeString(term));
   351                     atLeast += buildTermList(escapeString(term));
   336                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST, atLeast));
   352                 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST, atLeast));
   337             }
   353             }
   338         }
   354         }
   339 #endif
   355 #endif
   358 
   374 
   359 private:
   375 private:
   360     friend class QHelpSearchQueryWidget;
   376     friend class QHelpSearchQueryWidget;
   361 
   377 
   362     bool simpleSearch;
   378     bool simpleSearch;
       
   379     QLabel *simpleSearchLabel;
       
   380     QLabel *advancedSearchLabel;
       
   381     QLabel *similarLabel;
       
   382     QLabel *withoutLabel;
       
   383     QLabel *exactLabel;
       
   384     QLabel *allLabel;
       
   385     QLabel *atLeastLabel;
   363     QPushButton *searchButton;
   386     QPushButton *searchButton;
   364     QWidget* advancedSearchWidget;
   387     QWidget* advancedSearchWidget;
   365     QToolButton *showHideAdvancedSearchButton;
   388     QToolButton *showHideAdvancedSearchButton;
   366     QLineEdit *defaultQuery;
   389     QLineEdit *defaultQuery;
   367     QLineEdit *exactQuery;
   390     QLineEdit *exactQuery;
   406 
   429 
   407     QVBoxLayout *vLayout = new QVBoxLayout(this);
   430     QVBoxLayout *vLayout = new QVBoxLayout(this);
   408     vLayout->setMargin(0);
   431     vLayout->setMargin(0);
   409 
   432 
   410     QHBoxLayout* hBoxLayout = new QHBoxLayout();
   433     QHBoxLayout* hBoxLayout = new QHBoxLayout();
   411     QLabel *label = new QLabel(tr("Search for:"), this);
   434     d->simpleSearchLabel = new QLabel(this);
   412     d->defaultQuery = new QLineEdit(this);
   435     d->defaultQuery = new QLineEdit(this);
   413     d->defaultQuery->setCompleter(&d->searchCompleter);
   436     d->defaultQuery->setCompleter(&d->searchCompleter);
   414     d->prevQueryButton = new QToolButton(this);
   437     d->prevQueryButton = new QToolButton(this);
   415     d->prevQueryButton->setArrowType(Qt::LeftArrow);
   438     d->prevQueryButton->setArrowType(Qt::LeftArrow);
   416     d->prevQueryButton->setToolTip(tr("Previous search"));
       
   417     d->prevQueryButton->setEnabled(false);
   439     d->prevQueryButton->setEnabled(false);
   418     d->nextQueryButton = new QToolButton(this);
   440     d->nextQueryButton = new QToolButton(this);
   419     d->nextQueryButton->setArrowType(Qt::RightArrow);
   441     d->nextQueryButton->setArrowType(Qt::RightArrow);
   420     d->nextQueryButton->setToolTip(tr("Next search"));
       
   421     d->nextQueryButton->setEnabled(false);
   442     d->nextQueryButton->setEnabled(false);
   422     d->searchButton = new QPushButton(tr("Search"), this);
   443     d->searchButton = new QPushButton(this);
   423     hBoxLayout->addWidget(label);
   444     hBoxLayout->addWidget(d->simpleSearchLabel);
   424     hBoxLayout->addWidget(d->defaultQuery);
   445     hBoxLayout->addWidget(d->defaultQuery);
   425     hBoxLayout->addWidget(d->prevQueryButton);
   446     hBoxLayout->addWidget(d->prevQueryButton);
   426     hBoxLayout->addWidget(d->nextQueryButton);
   447     hBoxLayout->addWidget(d->nextQueryButton);
   427     hBoxLayout->addWidget(d->searchButton);
   448     hBoxLayout->addWidget(d->searchButton);
   428 
   449 
   437     hBoxLayout = new QHBoxLayout();
   458     hBoxLayout = new QHBoxLayout();
   438     d->showHideAdvancedSearchButton = new QToolButton(this);
   459     d->showHideAdvancedSearchButton = new QToolButton(this);
   439     d->showHideAdvancedSearchButton->setText(QLatin1String("+"));
   460     d->showHideAdvancedSearchButton->setText(QLatin1String("+"));
   440     d->showHideAdvancedSearchButton->setMinimumSize(25, 20);
   461     d->showHideAdvancedSearchButton->setMinimumSize(25, 20);
   441 
   462 
   442     label = new QLabel(tr("Advanced search"), this);
   463     d->advancedSearchLabel = new QLabel(this);
   443     QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
   464     QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
   444     sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
   465     sizePolicy.setHeightForWidth(d->advancedSearchLabel->sizePolicy().hasHeightForWidth());
   445     label->setSizePolicy(sizePolicy);
   466     d->advancedSearchLabel->setSizePolicy(sizePolicy);
   446 
   467 
   447     QFrame* hLine = new QFrame(this);
   468     QFrame* hLine = new QFrame(this);
   448     hLine->setFrameStyle(QFrame::HLine);
   469     hLine->setFrameStyle(QFrame::HLine);
   449     hBoxLayout->addWidget(d->showHideAdvancedSearchButton);
   470     hBoxLayout->addWidget(d->showHideAdvancedSearchButton);
   450     hBoxLayout->addWidget(label);
   471     hBoxLayout->addWidget(d->advancedSearchLabel);
   451     hBoxLayout->addWidget(hLine);
   472     hBoxLayout->addWidget(hLine);
   452 
   473 
   453     vLayout->addLayout(hBoxLayout);
   474     vLayout->addLayout(hBoxLayout);
   454 
   475 
   455     // setup advanced search layout
   476     // setup advanced search layout
   456     d->advancedSearchWidget = new QWidget(this);
   477     d->advancedSearchWidget = new QWidget(this);
   457     QGridLayout *gLayout = new QGridLayout(d->advancedSearchWidget);
   478     QGridLayout *gLayout = new QGridLayout(d->advancedSearchWidget);
   458     gLayout->setMargin(0);
   479     gLayout->setMargin(0);
   459 
   480 
   460     label = new QLabel(tr("words <B>similar</B> to:"), this);
   481     d->similarLabel = new QLabel(this);
   461     gLayout->addWidget(label, 0, 0);
   482     gLayout->addWidget(d->similarLabel, 0, 0);
   462     d->similarQuery = new QLineEdit(this);
   483     d->similarQuery = new QLineEdit(this);
   463     d->similarQuery->setCompleter(&d->searchCompleter);
   484     d->similarQuery->setCompleter(&d->searchCompleter);
   464     gLayout->addWidget(d->similarQuery, 0, 1);
   485     gLayout->addWidget(d->similarQuery, 0, 1);
   465 
   486 
   466     label = new QLabel(tr("<B>without</B> the words:"), this);
   487     d->withoutLabel = new QLabel(this);
   467     gLayout->addWidget(label, 1, 0);
   488     gLayout->addWidget(d->withoutLabel, 1, 0);
   468     d->withoutQuery = new QLineEdit(this);
   489     d->withoutQuery = new QLineEdit(this);
   469     d->withoutQuery->setCompleter(&d->searchCompleter);
   490     d->withoutQuery->setCompleter(&d->searchCompleter);
   470     gLayout->addWidget(d->withoutQuery, 1, 1);
   491     gLayout->addWidget(d->withoutQuery, 1, 1);
   471 
   492 
   472     label = new QLabel(tr("with <B>exact phrase</B>:"), this);
   493     d->exactLabel = new QLabel(this);
   473     gLayout->addWidget(label, 2, 0);
   494     gLayout->addWidget(d->exactLabel, 2, 0);
   474     d->exactQuery = new QLineEdit(this);
   495     d->exactQuery = new QLineEdit(this);
   475     d->exactQuery->setCompleter(&d->searchCompleter);
   496     d->exactQuery->setCompleter(&d->searchCompleter);
   476     gLayout->addWidget(d->exactQuery, 2, 1);
   497     gLayout->addWidget(d->exactQuery, 2, 1);
   477 
   498 
   478     label = new QLabel(tr("with <B>all</B> of the words:"), this);
   499     d->allLabel = new QLabel(this);
   479     gLayout->addWidget(label, 3, 0);
   500     gLayout->addWidget(d->allLabel, 3, 0);
   480     d->allQuery = new QLineEdit(this);
   501     d->allQuery = new QLineEdit(this);
   481     d->allQuery->setCompleter(&d->searchCompleter);
   502     d->allQuery->setCompleter(&d->searchCompleter);
   482     gLayout->addWidget(d->allQuery, 3, 1);
   503     gLayout->addWidget(d->allQuery, 3, 1);
   483 
   504 
   484     label = new QLabel(tr("with <B>at least one</B> of the words:"), this);
   505     d->atLeastLabel = new QLabel(this);
   485     gLayout->addWidget(label, 4, 0);
   506     gLayout->addWidget(d->atLeastLabel, 4, 0);
   486     d->atLeastQuery = new QLineEdit(this);
   507     d->atLeastQuery = new QLineEdit(this);
   487     d->atLeastQuery->setCompleter(&d->searchCompleter);
   508     d->atLeastQuery->setCompleter(&d->searchCompleter);
   488     gLayout->addWidget(d->atLeastQuery, 4, 1);
   509     gLayout->addWidget(d->atLeastQuery, 4, 1);
   489 
   510 
   490     vLayout->addWidget(d->advancedSearchWidget);
   511     vLayout->addWidget(d->advancedSearchWidget);
   491     d->advancedSearchWidget->hide();
   512     d->advancedSearchWidget->hide();
       
   513 
       
   514     d->retranslate();
   492 
   515 
   493     connect(d->exactQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   516     connect(d->exactQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   494     connect(d->similarQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   517     connect(d->similarQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   495     connect(d->withoutQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   518     connect(d->withoutQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   496     connect(d->allQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   519     connect(d->allQuery, SIGNAL(returnPressed()), this, SIGNAL(search()));
   508 {
   531 {
   509     delete d;
   532     delete d;
   510 }
   533 }
   511 
   534 
   512 /*!
   535 /*!
   513     Returns a list of querys to use in combination with the search engines
   536     Returns a list of queries to use in combination with the search engines
   514     search(QList<QHelpSearchQuery> &query) function.
   537     search(QList<QHelpSearchQuery> &query) function.
   515 */
   538 */
   516 QList<QHelpSearchQuery> QHelpSearchQueryWidget::query() const
   539 QList<QHelpSearchQuery> QHelpSearchQueryWidget::query() const
   517 {
   540 {
   518     const QHelpSearchQueryWidgetPrivate::QueryHistory &queryHist =
   541     const QHelpSearchQueryWidgetPrivate::QueryHistory &queryHist =
   529         d->defaultQuery->selectAll();
   552         d->defaultQuery->selectAll();
   530         d->defaultQuery->setFocus();
   553         d->defaultQuery->setFocus();
   531     }
   554     }
   532 }
   555 }
   533 
   556 
       
   557 /*! \reimp
       
   558 */
       
   559 void QHelpSearchQueryWidget::changeEvent(QEvent *event)
       
   560 {
       
   561     if (event->type() == QEvent::LanguageChange)
       
   562         d->retranslate();
       
   563     else
       
   564         QWidget::changeEvent(event);
       
   565 }
       
   566 
   534 QT_END_NAMESPACE
   567 QT_END_NAMESPACE