Changeset 561 for trunk/tools/assistant/lib/qhelpsearchquerywidget.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/tools/assistant/lib/qhelpsearchquerywidget.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information (qt-info@nokia.com) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 5 6 ** 6 7 ** This file is part of the Qt Assistant of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at qt-info@nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 44 44 #include <QtCore/QDebug> 45 45 46 #include <QtCore/QAbstractListModel> 46 47 #include <QtCore/QObject> 47 48 #include <QtCore/QStringList> 48 49 #include <QtCore/QtGlobal> 50 51 #include <QtGui/QCompleter> 49 52 #include <QtGui/QLabel> 50 53 #include <QtGui/QLayout> … … 61 64 62 65 private: 66 struct QueryHistory { 67 explicit QueryHistory() : curQuery(-1) {} 68 QList<QList<QHelpSearchQuery> > queries; 69 int curQuery; 70 }; 71 72 class CompleterModel : public QAbstractListModel 73 { 74 public: 75 explicit CompleterModel(QObject *parent) 76 : QAbstractListModel(parent) {} 77 78 int rowCount(const QModelIndex &parent = QModelIndex()) const 79 { 80 return parent.isValid() ? 0 : termList.size(); 81 } 82 83 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const 84 { 85 if (!index.isValid() || index.row() >= termList.count()|| 86 (role != Qt::EditRole && role != Qt::DisplayRole)) 87 return QVariant(); 88 return termList.at(index.row()); 89 } 90 91 void addTerm(const QString &term) 92 { 93 if (!termList.contains(term)) { 94 termList.append(term); 95 reset(); 96 } 97 } 98 99 private: 100 QStringList termList; 101 }; 102 63 103 QHelpSearchQueryWidgetPrivate() 64 : QObject() 104 : QObject(), simpleSearch(true), 105 searchCompleter(new CompleterModel(this), this) 65 106 { 66 107 searchButton = 0; … … 137 178 } 138 179 180 void saveQuery(const QList<QHelpSearchQuery> &query, QueryHistory &queryHist) 181 { 182 // We only add the query to the list if it is different from the last one. 183 bool insert = false; 184 if (queryHist.queries.empty()) 185 insert = true; 186 else { 187 const QList<QHelpSearchQuery> &lastQuery = queryHist.queries.last(); 188 if (lastQuery.size() != query.size()) { 189 insert = true; 190 } else { 191 for (int i = 0; i < query.size(); ++i) { 192 if (query.at(i).fieldName != lastQuery.at(i).fieldName 193 || query.at(i).wordList != lastQuery.at(i).wordList) { 194 insert = true; 195 break; 196 } 197 } 198 } 199 } 200 if (insert) { 201 queryHist.queries.append(query); 202 foreach (const QHelpSearchQuery &queryPart, query) { 203 static_cast<CompleterModel *>(searchCompleter.model())-> 204 addTerm(queryPart.wordList.join(" ")); 205 } 206 } 207 } 208 209 void nextOrPrevQuery(int maxOrMinIndex, int addend, 210 QToolButton *thisButton, QToolButton *otherButton) 211 { 212 QueryHistory *queryHist; 213 QList<QLineEdit *> lineEdits; 214 if (simpleSearch) { 215 queryHist = &simpleQueries; 216 lineEdits << defaultQuery; 217 } else { 218 queryHist = &complexQueries; 219 lineEdits << allQuery << atLeastQuery << similarQuery 220 << withoutQuery << exactQuery; 221 } 222 foreach (QLineEdit *lineEdit, lineEdits) 223 lineEdit->clear(); 224 225 // Otherwise, the respective button would be disabled. 226 Q_ASSERT(queryHist->curQuery != maxOrMinIndex); 227 228 queryHist->curQuery += addend; 229 const QList<QHelpSearchQuery> &query = 230 queryHist->queries.at(queryHist->curQuery); 231 foreach (const QHelpSearchQuery &queryPart, query) { 232 QLineEdit *lineEdit = 0; 233 switch (queryPart.fieldName) { 234 case QHelpSearchQuery::DEFAULT: 235 lineEdit = defaultQuery; 236 break; 237 case QHelpSearchQuery::ALL: 238 lineEdit = allQuery; 239 break; 240 case QHelpSearchQuery::ATLEAST: 241 lineEdit = atLeastQuery; 242 break; 243 case QHelpSearchQuery::FUZZY: 244 lineEdit = similarQuery; 245 break; 246 case QHelpSearchQuery::WITHOUT: 247 lineEdit = withoutQuery; 248 break; 249 case QHelpSearchQuery::PHRASE: 250 lineEdit = exactQuery; 251 break; 252 default: 253 Q_ASSERT(0); 254 } 255 lineEdit->setText(queryPart.wordList.join(" ")); 256 } 257 258 if (queryHist->curQuery == maxOrMinIndex) 259 thisButton->setEnabled(false); 260 otherButton->setEnabled(true); 261 } 262 263 void enableOrDisableToolButtons() 264 { 265 const QueryHistory &queryHist = 266 simpleSearch ? simpleQueries : complexQueries; 267 prevQueryButton->setEnabled(queryHist.curQuery > 0); 268 nextQueryButton->setEnabled(queryHist.curQuery < 269 queryHist.queries.size() - 1); 270 } 271 139 272 private slots: 140 273 void showHideAdvancedSearch() 141 274 { 142 bool hidden = advancedSearchWidget->isHidden(); 143 if (hidden) { 275 if (simpleSearch) { 144 276 advancedSearchWidget->show(); 145 277 showHideAdvancedSearchButton->setText((QLatin1String("-"))); … … 149 281 } 150 282 151 defaultQuery->setEnabled(!hidden); 283 simpleSearch = !simpleSearch; 284 defaultQuery->setEnabled(simpleSearch); 285 enableOrDisableToolButtons(); 286 } 287 288 void searchRequested() 289 { 290 QList<QHelpSearchQuery> queryList; 291 #if !defined(QT_CLUCENE_SUPPORT) 292 queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT, 293 QStringList(defaultQuery->text()))); 294 295 #else 296 if (defaultQuery->isEnabled()) { 297 queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT, 298 buildTermList(escapeString(defaultQuery->text())))); 299 } else { 300 const QRegExp exp(QLatin1String("\\s+")); 301 QStringList lst = similarQuery->text().split(exp, QString::SkipEmptyParts); 302 if (!lst.isEmpty()) { 303 QStringList fuzzy; 304 foreach (const QString term, lst) 305 fuzzy += buildTermList(escapeString(term)); 306 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY, fuzzy)); 307 } 308 309 lst = withoutQuery->text().split(exp, QString::SkipEmptyParts); 310 if (!lst.isEmpty()) { 311 QStringList without; 312 foreach (const QString term, lst) 313 without.append(escapeString(term)); 314 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT, without)); 315 } 316 317 if (!exactQuery->text().isEmpty()) { 318 QString phrase = exactQuery->text().remove(QLatin1Char('\"')); 319 phrase = escapeString(phrase.simplified()); 320 queryList.append(QHelpSearchQuery(QHelpSearchQuery::PHRASE, QStringList(phrase))); 321 } 322 323 lst = allQuery->text().split(exp, QString::SkipEmptyParts); 324 if (!lst.isEmpty()) { 325 QStringList all; 326 foreach (const QString term, lst) 327 all.append(escapeString(term)); 328 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all)); 329 } 330 331 lst = atLeastQuery->text().split(exp, QString::SkipEmptyParts); 332 if (!lst.isEmpty()) { 333 QStringList atLeast; 334 foreach (const QString term, lst) 335 atLeast += buildTermList(escapeString(term)); 336 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST, atLeast)); 337 } 338 } 339 #endif 340 QueryHistory &queryHist = simpleSearch ? simpleQueries : complexQueries; 341 saveQuery(queryList, queryHist); 342 queryHist.curQuery = queryHist.queries.size() - 1; 343 if (queryHist.curQuery > 0) 344 prevQueryButton->setEnabled(true); 345 nextQueryButton->setEnabled(false); 346 } 347 348 void nextQuery() 349 { 350 nextOrPrevQuery((simpleSearch ? simpleQueries : complexQueries).queries.size() - 1, 351 1, nextQueryButton, prevQueryButton); 352 } 353 354 void prevQuery() 355 { 356 nextOrPrevQuery(0, -1, prevQueryButton, nextQueryButton); 152 357 } 153 358 … … 155 360 friend class QHelpSearchQueryWidget; 156 361 362 bool simpleSearch; 157 363 QPushButton *searchButton; 158 364 QWidget* advancedSearchWidget; … … 164 370 QLineEdit *allQuery; 165 371 QLineEdit *atLeastQuery; 372 QToolButton *nextQueryButton; 373 QToolButton *prevQueryButton; 374 QueryHistory simpleQueries; 375 QueryHistory complexQueries; 376 QCompleter searchCompleter; 166 377 }; 167 378 … … 200 411 QLabel *label = new QLabel(tr("Search for:"), this); 201 412 d->defaultQuery = new QLineEdit(this); 413 d->defaultQuery->setCompleter(&d->searchCompleter); 414 d->prevQueryButton = new QToolButton(this); 415 d->prevQueryButton->setArrowType(Qt::LeftArrow); 416 d->prevQueryButton->setToolTip(tr("Previous search")); 417 d->prevQueryButton->setEnabled(false); 418 d->nextQueryButton = new QToolButton(this); 419 d->nextQueryButton->setArrowType(Qt::RightArrow); 420 d->nextQueryButton->setToolTip(tr("Next search")); 421 d->nextQueryButton->setEnabled(false); 202 422 d->searchButton = new QPushButton(tr("Search"), this); 203 423 hBoxLayout->addWidget(label); 204 424 hBoxLayout->addWidget(d->defaultQuery); 425 hBoxLayout->addWidget(d->prevQueryButton); 426 hBoxLayout->addWidget(d->nextQueryButton); 205 427 hBoxLayout->addWidget(d->searchButton); 206 428 207 429 vLayout->addLayout(hBoxLayout); 208 430 431 connect(d->prevQueryButton, SIGNAL(clicked()), d, SLOT(prevQuery())); 432 connect(d->nextQueryButton, SIGNAL(clicked()), d, SLOT(nextQuery())); 209 433 connect(d->searchButton, SIGNAL(clicked()), this, SIGNAL(search())); 210 434 connect(d->defaultQuery, SIGNAL(returnPressed()), this, SIGNAL(search())); … … 237 461 gLayout->addWidget(label, 0, 0); 238 462 d->similarQuery = new QLineEdit(this); 463 d->similarQuery->setCompleter(&d->searchCompleter); 239 464 gLayout->addWidget(d->similarQuery, 0, 1); 240 465 … … 242 467 gLayout->addWidget(label, 1, 0); 243 468 d->withoutQuery = new QLineEdit(this); 469 d->withoutQuery->setCompleter(&d->searchCompleter); 244 470 gLayout->addWidget(d->withoutQuery, 1, 1); 245 471 … … 247 473 gLayout->addWidget(label, 2, 0); 248 474 d->exactQuery = new QLineEdit(this); 475 d->exactQuery->setCompleter(&d->searchCompleter); 249 476 gLayout->addWidget(d->exactQuery, 2, 1); 250 477 … … 252 479 gLayout->addWidget(label, 3, 0); 253 480 d->allQuery = new QLineEdit(this); 481 d->allQuery->setCompleter(&d->searchCompleter); 254 482 gLayout->addWidget(d->allQuery, 3, 1); 255 483 … … 257 485 gLayout->addWidget(label, 4, 0); 258 486 d->atLeastQuery = new QLineEdit(this); 487 d->atLeastQuery->setCompleter(&d->searchCompleter); 259 488 gLayout->addWidget(d->atLeastQuery, 4, 1); 260 489 … … 270 499 d, SLOT(showHideAdvancedSearch())); 271 500 #endif 501 connect(this, SIGNAL(search()), d, SLOT(searchRequested())); 272 502 } 273 503 … … 286 516 QList<QHelpSearchQuery> QHelpSearchQueryWidget::query() const 287 517 { 288 #if !defined(QT_CLUCENE_SUPPORT) 289 QList<QHelpSearchQuery> queryList; 290 queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT, 291 QStringList(d->defaultQuery->text()))); 292 293 return queryList; 294 #else 295 QList<QHelpSearchQuery> queryList; 296 if (d->defaultQuery->isEnabled()) { 297 queryList.append(QHelpSearchQuery(QHelpSearchQuery::DEFAULT, 298 d->buildTermList(d->escapeString(d->defaultQuery->text())))); 299 } else { 300 const QRegExp exp(QLatin1String("\\s+")); 301 QStringList lst = d->similarQuery->text().split(exp, QString::SkipEmptyParts); 302 if (!lst.isEmpty()) { 303 QStringList fuzzy; 304 foreach (const QString term, lst) 305 fuzzy += d->buildTermList(d->escapeString(term)); 306 queryList.append(QHelpSearchQuery(QHelpSearchQuery::FUZZY, fuzzy)); 307 } 308 309 lst = d->withoutQuery->text().split(exp, QString::SkipEmptyParts); 310 if (!lst.isEmpty()) { 311 QStringList without; 312 foreach (const QString term, lst) 313 without.append(d->escapeString(term)); 314 queryList.append(QHelpSearchQuery(QHelpSearchQuery::WITHOUT, without)); 315 } 316 317 if (!d->exactQuery->text().isEmpty()) { 318 QString phrase = d->exactQuery->text().remove(QLatin1Char('\"')); 319 phrase = d->escapeString(phrase.simplified()); 320 queryList.append(QHelpSearchQuery(QHelpSearchQuery::PHRASE, QStringList(phrase))); 321 } 322 323 lst = d->allQuery->text().split(exp, QString::SkipEmptyParts); 324 if (!lst.isEmpty()) { 325 QStringList all; 326 foreach (const QString term, lst) 327 all.append(d->escapeString(term)); 328 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ALL, all)); 329 } 330 331 lst = d->atLeastQuery->text().split(exp, QString::SkipEmptyParts); 332 if (!lst.isEmpty()) { 333 QStringList atLeast; 334 foreach (const QString term, lst) 335 atLeast += d->buildTermList(d->escapeString(term)); 336 queryList.append(QHelpSearchQuery(QHelpSearchQuery::ATLEAST, atLeast)); 337 } 338 } 339 return queryList; 340 #endif 518 const QHelpSearchQueryWidgetPrivate::QueryHistory &queryHist = 519 d->simpleSearch ? d->simpleQueries : d->complexQueries; 520 return queryHist.queries.isEmpty() ? 521 QList<QHelpSearchQuery>() : queryHist.queries.last(); 341 522 } 342 523
Note:
See TracChangeset
for help on using the changeset viewer.