Changeset 70231 in vbox for trunk/src/VBox/Frontends
- Timestamp:
- Dec 20, 2017 8:17:28 AM (7 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/logviewer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerSearchPanel.cpp
r70098 r70231 49 49 , m_pNextPrevButtons(0) 50 50 , m_pCaseSensitiveCheckBox(0) 51 , m_pMatchWholeWordCheckBox(0) 52 , m_pHighlightAllCheckBox(0) 51 53 , m_pWarningSpacer(0), m_pWarningIcon(0), m_pWarningLabel(0) 54 , m_iSearchPosition(0) 52 55 { 53 56 /* Prepare: */ … … 71 74 m_pNextPrevButtons->setEnabled(0, strSearchString.length()); 72 75 m_pNextPrevButtons->setEnabled(1, strSearchString.length()); 73 /* Show/hide the warning as per search-string validity: */ 74 toggleWarning(!strSearchString.length()); 76 75 77 /* If search-string is valid: */ 76 78 if (strSearchString.length()) 77 search(true, true); 79 { 80 /* Reset the position to force the search restart from the document's beginnig: */ 81 m_iSearchPosition = 0; 82 search(ForwardSearch); 83 } 78 84 /* If search-string is not valid, reset cursor position: */ 79 85 else … … 92 98 } 93 99 100 void UIVMLogViewerSearchPanel::sltHighlightAllCheckBox() 101 { 102 QPlainTextEdit *pTextEdit = m_pViewer->currentLogPage(); 103 if (!pTextEdit) return; 104 QTextDocument *pDocument = pTextEdit->document(); 105 if (!pDocument) 106 return; 107 108 if (m_pHighlightAllCheckBox->isChecked()) 109 { 110 const QString &searchString = m_pSearchEditor->text(); 111 if (searchString.isEmpty()) 112 return; 113 highlightAll(pDocument, constructFindFlags(ForwardSearch), searchString); 114 } 115 else 116 { 117 pDocument->undo(); 118 } 119 } 120 94 121 void UIVMLogViewerSearchPanel::prepare() 95 122 { … … 112 139 /* Configure main-layout: */ 113 140 m_pMainLayout->setContentsMargins(0, 0, 0, 0); 141 m_pMainLayout->setSpacing(4); 114 142 115 143 /* Create close-button: */ … … 167 195 AssertPtrReturnVoid(m_pCaseSensitiveCheckBox); 168 196 { 169 /* Configure focus for case-sensitive checkbox: */170 setFocusProxy(m_pCaseSensitiveCheckBox);171 197 /* Configure font: */ 172 198 #ifdef VBOX_DARWIN_USE_NATIVE_CONTROLS … … 178 204 m_pMainLayout->addWidget(m_pCaseSensitiveCheckBox); 179 205 } 206 207 m_pMatchWholeWordCheckBox = new QCheckBox; 208 AssertPtrReturnVoid(m_pMatchWholeWordCheckBox); 209 { 210 /* Configure focus for case-sensitive checkbox: */ 211 setFocusProxy(m_pMatchWholeWordCheckBox); 212 /* Configure font: */ 213 #ifdef VBOX_DARWIN_USE_NATIVE_CONTROLS 214 QFont font = m_pMatchWholeWordCheckBox->font(); 215 font.setPointSize(::darwinSmallFontSize()); 216 m_pMatchWholeWordCheckBox->setFont(font); 217 #endif /* VBOX_DARWIN_USE_NATIVE_CONTROLS */ 218 m_pMainLayout->addWidget(m_pMatchWholeWordCheckBox); 219 } 220 221 m_pHighlightAllCheckBox = new QCheckBox; 222 AssertPtrReturnVoid(m_pHighlightAllCheckBox); 223 { 224 /* Configure font: */ 225 #ifdef VBOX_DARWIN_USE_NATIVE_CONTROLS 226 QFont font = m_pHighlightAllCheckBox->font(); 227 font.setPointSize(::darwinSmallFontSize()); 228 m_pHighlightAllCheckBox->setFont(font); 229 #endif /* VBOX_DARWIN_USE_NATIVE_CONTROLS */ 230 /* Add case-sensitive checkbox to main-layout: */ 231 m_pMainLayout->addWidget(m_pHighlightAllCheckBox); 232 } 233 180 234 181 235 /* Create warning-spacer: */ … … 233 287 this, SLOT(findCurrent(const QString &))); 234 288 connect(m_pNextPrevButtons, SIGNAL(clicked(int)), this, SLOT(find(int))); 289 connect(m_pHighlightAllCheckBox, &QCheckBox::stateChanged, 290 this, &UIVMLogViewerSearchPanel::sltHighlightAllCheckBox); 235 291 } 236 292 … … 242 298 m_pSearchEditor->setToolTip(UIVMLogViewerWidget::tr("Enter a search string here")); 243 299 244 m_pNextPrevButtons->setTitle(0, UIVMLogViewerWidget::tr("&Previous"));245 300 m_pNextPrevButtons->setToolTip(0, UIVMLogViewerWidget::tr("Search for the previous occurrence of the string")); 246 m_pNextPrevButtons->setTitle(1, UIVMLogViewerWidget::tr("&Next"));247 301 m_pNextPrevButtons->setToolTip(1, UIVMLogViewerWidget::tr("Search for the next occurrence of the string")); 248 302 249 m_pCaseSensitiveCheckBox->setText(UIVMLogViewerWidget::tr("C&ase 303 m_pCaseSensitiveCheckBox->setText(UIVMLogViewerWidget::tr("C&ase\nSensitive")); 250 304 m_pCaseSensitiveCheckBox->setToolTip(UIVMLogViewerWidget::tr("Perform case sensitive search (when checked)")); 305 306 m_pMatchWholeWordCheckBox->setText(UIVMLogViewerWidget::tr("Ma&tch\nWhole Word")); 307 m_pMatchWholeWordCheckBox->setToolTip(UIVMLogViewerWidget::tr("Search matches only complete words when checked")); 308 309 m_pHighlightAllCheckBox->setText(UIVMLogViewerWidget::tr("Highlight\nAll")); 310 m_pHighlightAllCheckBox->setToolTip(UIVMLogViewerWidget::tr("All occurence of the search text are highlighted")); 251 311 252 312 m_pWarningLabel->setText(UIVMLogViewerWidget::tr("String not found")); … … 259 319 /* Process Enter press as 'search-next', 260 320 * performed for any search panel widget: */ 261 case Qt::Key_Enter: 262 case Qt::Key_Return: 263 { 264 if (pEvent->modifiers() == 0 || 265 pEvent->modifiers() & Qt::KeypadModifier) 321 case Qt::Key_Enter: 322 case Qt::Key_Return: 266 323 { 267 /* Animate click on 'Next' button: */ 324 if (pEvent->modifiers() == 0 || 325 pEvent->modifiers() & Qt::KeypadModifier) 326 { 327 /* Animate click on 'Next' button: */ 268 328 m_pNextPrevButtons->animateClick(1); 269 329 return; 330 } 331 break; 270 332 } 333 default: 271 334 break; 272 }273 default:274 break;275 335 } 276 336 /* Call to base-class: */ … … 283 343 switch (pEvent->type()) 284 344 { 345 285 346 /* Process key press only: */ 286 case QEvent::KeyPress:347 case QEvent::KeyPress: 287 348 { 288 349 /* Cast to corresponding key press event: */ … … 368 429 } 369 430 370 void UIVMLogViewerSearchPanel::search(bool fForward, bool fStartCurrent /* = false */) 371 { 372 /* Get current log-page: */ 373 QPlainTextEdit *pBrowser = m_pViewer->currentLogPage(); 374 if (!pBrowser) return; 375 376 /* Get text-cursor and its attributes: */ 377 QTextCursor cursor = pBrowser->textCursor(); 378 int iPos = cursor.position(); 379 int iAnc = cursor.anchor(); 380 381 /* Get the text to be searched: */ 382 QString strText = pBrowser->toPlainText(); 383 int iDiff = fStartCurrent ? 0 : 1; 384 385 int iResult = -1; 386 /* If search-direction is forward and cursor position is valid: */ 387 if (fForward && (fStartCurrent || iPos < strText.size() - 1)) 388 { 389 /* Search and get the index of first match: */ 390 iResult = strText.indexOf(m_pSearchEditor->text(), iAnc + iDiff, 391 m_pCaseSensitiveCheckBox->isChecked() ? 392 Qt::CaseSensitive : Qt::CaseInsensitive); 393 394 /* When searchstring is changed, search from beginning of log again: */ 395 if (iResult == -1 && fStartCurrent) 396 iResult = strText.indexOf(m_pSearchEditor->text(), 0, 397 m_pCaseSensitiveCheckBox->isChecked() ? 398 Qt::CaseSensitive : Qt::CaseInsensitive); 399 } 400 /* If search-direction is backward: */ 401 else if (!fForward && iAnc > 0) 402 iResult = strText.lastIndexOf(m_pSearchEditor->text(), iAnc - 1, 403 m_pCaseSensitiveCheckBox->isChecked() ? 404 Qt::CaseSensitive : Qt::CaseInsensitive); 405 406 /* If the result is valid, move cursor-position: */ 407 if (iResult != -1) 408 { 409 cursor.movePosition(QTextCursor::Start, 410 QTextCursor::MoveAnchor); 411 cursor.movePosition(QTextCursor::NextCharacter, 412 QTextCursor::MoveAnchor, iResult); 413 cursor.movePosition(QTextCursor::NextCharacter, 414 QTextCursor::KeepAnchor, 415 m_pSearchEditor->text().size()); 416 pBrowser->setTextCursor(cursor); 417 } 418 419 /* Show/hide the warning as per search-result: */ 420 toggleWarning(iResult != -1); 431 void UIVMLogViewerSearchPanel::search(SearchDirection direction) 432 { 433 QPlainTextEdit *pTextEdit = m_pViewer->currentLogPage(); 434 if (!pTextEdit) return; 435 QTextDocument *pDocument = pTextEdit->document(); 436 if (!pDocument) 437 return; 438 439 const QString &searchString = m_pSearchEditor->text(); 440 if (searchString.isEmpty()) 441 return; 442 443 QTextCursor endCursor(pDocument); 444 endCursor.movePosition(QTextCursor::End); 445 QTextCursor startCursor(pDocument); 446 447 /* Construct the find flags for QTextDocument::find function: */ 448 QTextDocument::FindFlags findFlags = constructFindFlags(direction); 449 450 if (m_pHighlightAllCheckBox->isChecked()) 451 highlightAll(pDocument, findFlags, searchString); 452 453 QTextCursor resultCursor(pDocument); 454 int startPosition = m_iSearchPosition; 455 if (direction == BackwardSearch) 456 startPosition -= searchString.length(); 457 resultCursor = pDocument->find(searchString, startPosition, findFlags); 458 459 /* Decide whether to wrap around or to end the search */ 460 if (resultCursor.isNull()) 461 { 462 /* End the search if we search the whole document with no find: */ 463 if ((direction == ForwardSearch && startPosition == startCursor.position()) || 464 (direction == BackwardSearch && startPosition == endCursor.position())) 465 { 466 toggleWarning(false); 467 return; 468 } 469 /* Wrap the search */ 470 if (direction == ForwardSearch) 471 { 472 m_iSearchPosition = startCursor.position(); 473 search(ForwardSearch); 474 return; 475 } 476 else 477 { 478 /* Set the search position away from the end position to be 479 able to find the string at the end of the document: */ 480 m_iSearchPosition = endCursor.position() + searchString.length(); 481 search(BackwardSearch); 482 return; 483 } 484 } 485 pTextEdit->setTextCursor(resultCursor); 486 m_iSearchPosition = resultCursor.position(); 487 toggleWarning(true); 421 488 } 422 489 423 490 void UIVMLogViewerSearchPanel::findNext() 424 491 { 425 search( true);492 search(ForwardSearch); 426 493 } 427 494 428 495 void UIVMLogViewerSearchPanel::findBack() 429 496 { 430 search(false); 497 search(BackwardSearch); 498 } 499 500 void UIVMLogViewerSearchPanel::highlightAll(QTextDocument *pDocument, 501 const QTextDocument::FindFlags &findFlags, 502 const QString &searchString) 503 { 504 if (!pDocument) 505 return; 506 if (searchString.isEmpty()) 507 return; 508 509 pDocument->undo(); 510 QTextCursor highlightCursor(pDocument); 511 QTextCharFormat colorFormat(highlightCursor.charFormat()); 512 QTextCursor cursor(pDocument); 513 cursor.beginEditBlock(); 514 515 colorFormat.setBackground(Qt::yellow); 516 517 while (!highlightCursor.isNull() && !highlightCursor.atEnd()) 518 { 519 highlightCursor = pDocument->find(searchString, highlightCursor, findFlags); 520 521 if (!highlightCursor.isNull()) 522 highlightCursor.mergeCharFormat(colorFormat); 523 } 524 cursor.endEditBlock(); 431 525 } 432 526 … … 446 540 m_pWarningLabel->setHidden(fHide); 447 541 } 542 543 QTextDocument::FindFlags UIVMLogViewerSearchPanel::constructFindFlags(SearchDirection eDirection) 544 { 545 QTextDocument::FindFlags findFlags; 546 if (eDirection == BackwardSearch) 547 findFlags = findFlags | QTextDocument::FindBackward; 548 if (m_pCaseSensitiveCheckBox->isChecked()) 549 findFlags = findFlags | QTextDocument::FindCaseSensitively; 550 if (m_pMatchWholeWordCheckBox->isChecked()) 551 findFlags = findFlags | QTextDocument::FindWholeWords; 552 return findFlags; 553 } -
trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerSearchPanel.h
r70027 r70231 20 20 21 21 /* Qt includes: */ 22 #include <QTextDocument> 22 23 #include <QWidget> 23 24 … … 55 56 * @param strSearchString Specifies search-string. */ 56 57 void findCurrent(const QString &strSearchString); 58 59 void sltHighlightAllCheckBox(); 57 60 private: 58 61 enum SearchDirection { ForwardSearch, BackwardSearch }; 59 62 /** Prepares search-panel. */ 60 63 void prepare(); … … 76 79 77 80 /** Search routine. 78 * @param fForward Specifies the direction of search. 79 * @param fStartCurrent Specifies whether search should start from beginning of the log. */ 80 void search(bool fForward, bool fStartCurrent = false); 81 * @param eDirection Specifies the seach direction */ 82 void search(SearchDirection eDirection); 81 83 /** Forward search routine wrapper. */ 82 84 void findNext(); 83 85 /** Backward search routine wrapper. */ 84 86 void findBack(); 87 void highlightAll(QTextDocument *pDocument, const QTextDocument::FindFlags &findFlags, const QString &searchString); 85 88 /** Shows/hides the search border warning using @a fHide as hint. */ 86 89 void toggleWarning(bool fHide); 90 /** Constructs the find flags for QTextDocument::find function. */ 91 QTextDocument::FindFlags constructFindFlags(SearchDirection eDirection); 87 92 88 93 /** Holds the reference to the VM Log-Viewer this search-panel belongs to. */ … … 99 104 UIRoundRectSegmentedButton *m_pNextPrevButtons; 100 105 /** Holds the instance of case-sensitive checkbox we create. */ 101 QCheckBox *m_pCaseSensitiveCheckBox; 106 QCheckBox *m_pCaseSensitiveCheckBox; 107 QCheckBox *m_pMatchWholeWordCheckBox; 108 QCheckBox *m_pHighlightAllCheckBox; 102 109 /** Holds the instance of warning spacer-item we create. */ 103 110 QSpacerItem *m_pWarningSpacer; 104 111 /** Holds the instance of warning icon we create. */ 105 QLabel *m_pWarningIcon;112 QLabel *m_pWarningIcon; 106 113 /** Holds the instance of warning label we create. */ 107 QLabel *m_pWarningLabel;114 QLabel *m_pWarningLabel; 108 115 /** Holds the instance of spacer item we create. */ 109 116 QSpacerItem *m_pSpacerItem; 117 /** Holds the position where we start the next search. */ 118 int m_iSearchPosition; 110 119 }; 111 120
Note:
See TracChangeset
for help on using the changeset viewer.