1 /* 2 * Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 // This file implements the CrawlerWidget class. 21 // It is responsible for creating and managing the two views and all 22 // the UI elements. It implements the connection between the UI elements. 23 // It also interacts with the sets of data (full and filtered). 24 25 #include "log.h" 26 27 #include <cassert> 28 29 #include <Qt> 30 #include <QApplication> 31 #include <QFile> 32 #include <QLineEdit> 33 #include <QFileInfo> 34 #include <QKeyEvent> 35 #include <QStandardItemModel> 36 #include <QHeaderView> 37 #include <QListView> 38 39 #include "crawlerwidget.h" 40 41 #include "quickfindpattern.h" 42 #include "overview.h" 43 #include "infoline.h" 44 #include "savedsearches.h" 45 #include "quickfindwidget.h" 46 #include "persistentinfo.h" 47 #include "configuration.h" 48 49 // Palette for error signaling (yellow background) 50 const QPalette CrawlerWidget::errorPalette( QColor( "yellow" ) ); 51 52 // Constructor only does trivial construction. The real work is done once 53 // the data is attached. 54 CrawlerWidget::CrawlerWidget(SavedSearches* searches, QWidget *parent) 55 : QSplitter(parent) 56 { 57 logData_ = nullptr; 58 logFilteredData_ = nullptr; 59 60 savedSearches = searches; 61 } 62 63 // The top line is first one on the main display 64 int CrawlerWidget::getTopLine() const 65 { 66 return logMainView->getTopLine(); 67 } 68 69 QString CrawlerWidget::getSelectedText() const 70 { 71 if ( filteredView->hasFocus() ) 72 return filteredView->getSelection(); 73 else 74 return logMainView->getSelection(); 75 } 76 77 void CrawlerWidget::selectAll() 78 { 79 activeView()->selectAll(); 80 } 81 82 // Return a pointer to the view in which we should do the QuickFind 83 SearchableWidgetInterface* CrawlerWidget::getActiveSearchable() const 84 { 85 QWidget* searchableWidget; 86 87 // Search in the window that has focus, or the window where 'Find' was 88 // called from, or the main window. 89 if ( filteredView->hasFocus() || logMainView->hasFocus() ) 90 searchableWidget = QApplication::focusWidget(); 91 else 92 searchableWidget = qfSavedFocus_; 93 94 if ( AbstractLogView* view = qobject_cast<AbstractLogView*>( searchableWidget ) ) 95 return view; 96 else 97 return logMainView; 98 } 99 100 void CrawlerWidget::reload() 101 { 102 searchState_.resetState(); 103 logFilteredData_->clearSearch(); 104 filteredView->updateData(); 105 printSearchInfoMessage(); 106 107 logData_->reload(); 108 } 109 110 // 111 // Protected functions 112 // 113 void CrawlerWidget::doSetData( 114 std::shared_ptr<LogData> log_data, 115 std::shared_ptr<LogFilteredData> filtered_data ) 116 { 117 logData_ = log_data.get(); 118 logFilteredData_ = filtered_data.get(); 119 120 setup(); 121 } 122 123 // 124 // Events handlers 125 // 126 127 void CrawlerWidget::keyPressEvent( QKeyEvent* keyEvent ) 128 { 129 LOG(logDEBUG4) << "keyPressEvent received"; 130 131 switch ( (keyEvent->text())[0].toLatin1() ) { 132 case '/': 133 displayQuickFindBar( QuickFindMux::Forward ); 134 break; 135 case '?': 136 displayQuickFindBar( QuickFindMux::Backward ); 137 break; 138 default: 139 keyEvent->ignore(); 140 } 141 142 if ( !keyEvent->isAccepted() ) 143 QSplitter::keyPressEvent( keyEvent ); 144 } 145 146 // 147 // Slots 148 // 149 150 void CrawlerWidget::startNewSearch() 151 { 152 // Record the search line in the recent list 153 // (reload the list first in case another glogg changed it) 154 GetPersistentInfo().retrieve( "savedSearches" ); 155 savedSearches->addRecent( searchLineEdit->currentText() ); 156 GetPersistentInfo().save( "savedSearches" ); 157 158 // Update the SearchLine (history) 159 updateSearchCombo(); 160 // Call the private function to do the search 161 replaceCurrentSearch( searchLineEdit->currentText() ); 162 } 163 164 void CrawlerWidget::stopSearch() 165 { 166 logFilteredData_->interruptSearch(); 167 searchState_.stopSearch(); 168 printSearchInfoMessage(); 169 } 170 171 // When receiving the 'newDataAvailable' signal from LogFilteredData 172 void CrawlerWidget::updateFilteredView( int nbMatches, int progress ) 173 { 174 LOG(logDEBUG) << "updateFilteredView received."; 175 176 if ( progress == 100 ) { 177 // Searching done 178 printSearchInfoMessage( nbMatches ); 179 searchInfoLine->hideGauge(); 180 // De-activate the stop button 181 stopButton->setEnabled( false ); 182 } 183 else { 184 // Search in progress 185 // We ignore 0% and 100% to avoid a flash when the search is very short 186 if ( progress > 0 ) { 187 searchInfoLine->setText( 188 tr("Search in progress (%1 %)... %2 match%3 found so far.") 189 .arg( progress ) 190 .arg( nbMatches ) 191 .arg( nbMatches > 1 ? "es" : "" ) ); 192 searchInfoLine->displayGauge( progress ); 193 } 194 } 195 196 // Recompute the content of the filtered window. 197 filteredView->updateData(); 198 199 // Update the match overview 200 overview_->updateData( logData_->getNbLine() ); 201 202 // Also update the top window for the coloured bullets. 203 update(); 204 } 205 206 void CrawlerWidget::jumpToMatchingLine(int filteredLineNb) 207 { 208 int mainViewLine = logFilteredData_->getMatchingLineNumber(filteredLineNb); 209 logMainView->selectAndDisplayLine(mainViewLine); // FIXME: should be done with a signal. 210 } 211 212 void CrawlerWidget::markLineFromMain( qint64 line ) 213 { 214 if ( logFilteredData_->isLineMarked( line ) ) 215 logFilteredData_->deleteMark( line ); 216 else 217 logFilteredData_->addMark( line ); 218 219 // Recompute the content of the filtered window. 220 filteredView->updateData(); 221 222 // Update the match overview 223 overview_->updateData( logData_->getNbLine() ); 224 225 // Also update the top window for the coloured bullets. 226 update(); 227 } 228 229 void CrawlerWidget::markLineFromFiltered( qint64 line ) 230 { 231 qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line ); 232 if ( logFilteredData_->filteredLineTypeByIndex( line ) 233 == LogFilteredData::Mark ) 234 logFilteredData_->deleteMark( line_in_file ); 235 else 236 logFilteredData_->addMark( line_in_file ); 237 238 // Recompute the content of the filtered window. 239 filteredView->updateData(); 240 241 // Update the match overview 242 overview_->updateData( logData_->getNbLine() ); 243 244 // Also update the top window for the coloured bullets. 245 update(); 246 } 247 248 void CrawlerWidget::applyConfiguration() 249 { 250 Configuration& config = Persistent<Configuration>( "settings" ); 251 QFont font = config.mainFont(); 252 253 LOG(logDEBUG) << "CrawlerWidget::applyConfiguration"; 254 255 // Whatever font we use, we should NOT use kerning 256 font.setKerning( false ); 257 font.setFixedPitch( true ); 258 #if QT_VERSION > 0x040700 259 // Necessary on systems doing subpixel positionning (e.g. Ubuntu 12.04) 260 font.setStyleStrategy( QFont::ForceIntegerMetrics ); 261 #endif 262 logMainView->setFont(font); 263 filteredView->setFont(font); 264 265 logMainView->setLineNumbersVisible( config.mainLineNumbersVisible() ); 266 filteredView->setLineNumbersVisible( config.filteredLineNumbersVisible() ); 267 268 overview_->setVisible( config.isOverviewVisible() ); 269 logMainView->refreshOverview(); 270 271 logMainView->updateDisplaySize(); 272 logMainView->update(); 273 filteredView->updateDisplaySize(); 274 filteredView->update(); 275 276 // Update the SearchLine (history) 277 updateSearchCombo(); 278 } 279 280 void CrawlerWidget::loadingFinishedHandler( bool success ) 281 { 282 // We need to refresh the main window because the view lines on the 283 // overview have probably changed. 284 overview_->updateData( logData_->getNbLine() ); 285 286 // FIXME, handle topLine 287 // logMainView->updateData( logData_, topLine ); 288 logMainView->updateData(); 289 290 // Shall we Forbid starting a search when loading in progress? 291 // searchButton->setEnabled( false ); 292 293 // searchButton->setEnabled( true ); 294 295 // See if we need to auto-refresh the search 296 if ( searchState_.isAutorefreshAllowed() ) { 297 LOG(logDEBUG) << "Refreshing the search"; 298 logFilteredData_->updateSearch(); 299 } 300 301 emit loadingFinished( success ); 302 } 303 304 void CrawlerWidget::fileChangedHandler( LogData::MonitoredFileStatus status ) 305 { 306 // Handle the case where the file has been truncated 307 if ( status == LogData::Truncated ) { 308 // Clear all marks (TODO offer the option to keep them) 309 logFilteredData_->clearMarks(); 310 if ( ! searchInfoLine->text().isEmpty() ) { 311 // Invalidate the search 312 logFilteredData_->clearSearch(); 313 filteredView->updateData(); 314 searchState_.truncateFile(); 315 printSearchInfoMessage(); 316 } 317 } 318 } 319 320 void CrawlerWidget::displayQuickFindBar( QuickFindMux::QFDirection direction ) 321 { 322 LOG(logDEBUG) << "CrawlerWidget::displayQuickFindBar"; 323 324 // Remember who had the focus 325 qfSavedFocus_ = QApplication::focusWidget(); 326 327 quickFindMux_->setDirection( direction ); 328 quickFindWidget_->userActivate(); 329 } 330 331 void CrawlerWidget::hideQuickFindBar() 332 { 333 // Restore the focus once the QFBar has been hidden 334 qfSavedFocus_->setFocus(); 335 } 336 337 void CrawlerWidget::changeQFPattern( const QString& newPattern ) 338 { 339 quickFindWidget_->changeDisplayedPattern( newPattern ); 340 } 341 342 // Returns a pointer to the window in which the search should be done 343 AbstractLogView* CrawlerWidget::activeView() const 344 { 345 QWidget* activeView; 346 347 // Search in the window that has focus, or the window where 'Find' was 348 // called from, or the main window. 349 if ( filteredView->hasFocus() || logMainView->hasFocus() ) 350 activeView = QApplication::focusWidget(); 351 else 352 activeView = qfSavedFocus_; 353 354 if ( AbstractLogView* view = qobject_cast<AbstractLogView*>( activeView ) ) 355 return view; 356 else 357 return logMainView; 358 } 359 360 void CrawlerWidget::searchForward() 361 { 362 LOG(logDEBUG) << "CrawlerWidget::searchForward"; 363 364 activeView()->searchForward(); 365 } 366 367 void CrawlerWidget::searchBackward() 368 { 369 LOG(logDEBUG) << "CrawlerWidget::searchBackward"; 370 371 activeView()->searchBackward(); 372 } 373 374 void CrawlerWidget::searchRefreshChangedHandler( int state ) 375 { 376 searchState_.setAutorefresh( state == Qt::Checked ); 377 printSearchInfoMessage( logFilteredData_->getNbMatches() ); 378 } 379 380 void CrawlerWidget::searchTextChangeHandler() 381 { 382 // We suspend auto-refresh 383 searchState_.changeExpression(); 384 printSearchInfoMessage( logFilteredData_->getNbMatches() ); 385 } 386 387 void CrawlerWidget::changeFilteredViewVisibility( int index ) 388 { 389 QStandardItem* item = visibilityModel_->item( index ); 390 FilteredView::Visibility visibility = 391 static_cast< FilteredView::Visibility>( item->data().toInt() ); 392 393 filteredView->setVisibility( visibility ); 394 } 395 396 void CrawlerWidget::addToSearch( const QString& string ) 397 { 398 QString text = searchLineEdit->currentText(); 399 400 if ( text.isEmpty() ) 401 text = string; 402 else { 403 // Escape the regexp chars from the string before adding it. 404 text += ( '|' + QRegExp::escape( string ) ); 405 } 406 407 searchLineEdit->setEditText( text ); 408 409 // Set the focus to lineEdit so that the user can press 'Return' immediately 410 searchLineEdit->lineEdit()->setFocus(); 411 } 412 413 void CrawlerWidget::mouseHoveredOverMatch( qint64 line ) 414 { 415 qint64 line_in_mainview = logFilteredData_->getMatchingLineNumber( line ); 416 417 overviewWidget_->highlightLine( line_in_mainview ); 418 } 419 420 // 421 // Private functions 422 // 423 424 // Build the widget and connect all the signals, this must be done once 425 // the data are attached. 426 void CrawlerWidget::setup() 427 { 428 setOrientation(Qt::Vertical); 429 430 assert( logData_ ); 431 assert( logFilteredData_ ); 432 433 // The matches overview 434 overview_ = new Overview(); 435 436 // Initialise the QF Mux to send requests from the QFWidget 437 // to the right window 438 quickFindMux_ = new QuickFindMux( this ); 439 440 // The views 441 bottomWindow = new QWidget; 442 overviewWidget_ = new OverviewWidget(); 443 logMainView = new LogMainView( 444 logData_, quickFindMux_->getPattern(), overview_, overviewWidget_ ); 445 filteredView = new FilteredView( 446 logFilteredData_, quickFindMux_->getPattern() ); 447 448 overviewWidget_->setOverview( overview_ ); 449 overviewWidget_->setParent( logMainView ); 450 451 quickFindMux_->registerSearchable( logMainView ); 452 quickFindMux_->registerSearchable( filteredView ); 453 454 // Construct the visibility button 455 visibilityModel_ = new QStandardItemModel( this ); 456 457 QStandardItem *marksAndMatchesItem = new QStandardItem( tr( "Marks and matches" ) ); 458 QPixmap marksAndMatchesPixmap( 16, 10 ); 459 marksAndMatchesPixmap.fill( Qt::gray ); 460 marksAndMatchesItem->setIcon( QIcon( marksAndMatchesPixmap ) ); 461 marksAndMatchesItem->setData( FilteredView::MarksAndMatches ); 462 visibilityModel_->appendRow( marksAndMatchesItem ); 463 464 QStandardItem *marksItem = new QStandardItem( tr( "Marks" ) ); 465 QPixmap marksPixmap( 16, 10 ); 466 marksPixmap.fill( Qt::blue ); 467 marksItem->setIcon( QIcon( marksPixmap ) ); 468 marksItem->setData( FilteredView::MarksOnly ); 469 visibilityModel_->appendRow( marksItem ); 470 471 QStandardItem *matchesItem = new QStandardItem( tr( "Matches" ) ); 472 QPixmap matchesPixmap( 16, 10 ); 473 matchesPixmap.fill( Qt::red ); 474 matchesItem->setIcon( QIcon( matchesPixmap ) ); 475 matchesItem->setData( FilteredView::MatchesOnly ); 476 visibilityModel_->appendRow( matchesItem ); 477 478 QListView *visibilityView = new QListView( this ); 479 visibilityView->setMovement( QListView::Static ); 480 visibilityView->setMinimumWidth( 170 ); // Only needed with custom style-sheet 481 482 visibilityBox = new QComboBox(); 483 visibilityBox->setModel( visibilityModel_ ); 484 visibilityBox->setView( visibilityView ); 485 486 // Select "Marks and matches" by default (same default as the filtered view) 487 visibilityBox->setCurrentIndex( 0 ); 488 489 // TODO: Maybe there is some way to set the popup width to be 490 // sized-to-content (as it is when the stylesheet is not overriden) in the 491 // stylesheet as opposed to setting a hard min-width on the view above. 492 visibilityBox->setStyleSheet( " \ 493 QComboBox:on {\ 494 padding: 1px 2px 1px 6px;\ 495 width: 19px;\ 496 } \ 497 QComboBox:!on {\ 498 padding: 1px 2px 1px 7px;\ 499 width: 19px;\ 500 height: 16px;\ 501 border: 1px solid gray;\ 502 } \ 503 QComboBox::drop-down::down-arrow {\ 504 width: 0px;\ 505 border-width: 0px;\ 506 } \ 507 " ); 508 509 // Construct the Search Info line 510 searchInfoLine = new InfoLine(); 511 searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 512 searchInfoLine->setLineWidth( 1 ); 513 searchInfoLineDefaultPalette = searchInfoLine->palette(); 514 515 ignoreCaseCheck = new QCheckBox( "Ignore &case" ); 516 searchRefreshCheck = new QCheckBox( "Auto-&refresh" ); 517 518 // Construct the Search line 519 searchLabel = new QLabel(tr("&Text: ")); 520 searchLineEdit = new QComboBox; 521 searchLineEdit->setEditable( true ); 522 searchLineEdit->setCompleter( 0 ); 523 searchLineEdit->addItems( savedSearches->recentSearches() ); 524 searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); 525 searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); 526 527 searchLabel->setBuddy( searchLineEdit ); 528 529 searchButton = new QToolButton(); 530 searchButton->setText( tr("&Search") ); 531 searchButton->setAutoRaise( true ); 532 533 stopButton = new QToolButton(); 534 stopButton->setIcon( QIcon(":/images/stop16.png") ); 535 stopButton->setAutoRaise( true ); 536 stopButton->setEnabled( false ); 537 538 // Construct the QuickFind bar 539 quickFindWidget_ = new QuickFindWidget(); 540 541 QHBoxLayout* searchLineLayout = new QHBoxLayout; 542 searchLineLayout->addWidget(searchLabel); 543 searchLineLayout->addWidget(searchLineEdit); 544 searchLineLayout->addWidget(searchButton); 545 searchLineLayout->addWidget(stopButton); 546 searchLineLayout->setContentsMargins(6, 0, 6, 0); 547 stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 548 searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 549 550 QHBoxLayout* searchInfoLineLayout = new QHBoxLayout; 551 searchInfoLineLayout->addWidget( visibilityBox ); 552 searchInfoLineLayout->addWidget( searchInfoLine ); 553 searchInfoLineLayout->addWidget( ignoreCaseCheck ); 554 searchInfoLineLayout->addWidget( searchRefreshCheck ); 555 556 // Construct the bottom window 557 quickFindWidget_->hide(); 558 QVBoxLayout* bottomMainLayout = new QVBoxLayout; 559 bottomMainLayout->addLayout(searchLineLayout); 560 bottomMainLayout->addLayout(searchInfoLineLayout); 561 bottomMainLayout->addWidget(filteredView); 562 bottomMainLayout->addWidget(quickFindWidget_); 563 bottomMainLayout->setContentsMargins(2, 1, 2, 1); 564 bottomWindow->setLayout(bottomMainLayout); 565 566 addWidget( logMainView ); 567 addWidget( bottomWindow ); 568 569 // Default splitter position (usually overridden by the config file) 570 QList<int> splitterSizes; 571 splitterSizes += 400; 572 splitterSizes += 100; 573 setSizes( splitterSizes ); 574 575 // Connect the signals 576 connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ), 577 searchButton, SIGNAL( clicked() )); 578 connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ), 579 this, SLOT( searchTextChangeHandler() )); 580 connect(searchButton, SIGNAL( clicked() ), 581 this, SLOT( startNewSearch() ) ); 582 connect(stopButton, SIGNAL( clicked() ), 583 this, SLOT( stopSearch() ) ); 584 585 connect(visibilityBox, SIGNAL( currentIndexChanged( int ) ), 586 this, SLOT( changeFilteredViewVisibility( int ) ) ); 587 588 connect(logMainView, SIGNAL( newSelection( int ) ), 589 logMainView, SLOT( update() ) ); 590 connect(filteredView, SIGNAL( newSelection( int ) ), 591 this, SLOT( jumpToMatchingLine( int ) ) ); 592 connect(filteredView, SIGNAL( newSelection( int ) ), 593 filteredView, SLOT( update() ) ); 594 connect(logMainView, SIGNAL( updateLineNumber( int ) ), 595 this, SIGNAL( updateLineNumber( int ) ) ); 596 connect(logMainView, SIGNAL( markLine( qint64 ) ), 597 this, SLOT( markLineFromMain( qint64 ) ) ); 598 connect(filteredView, SIGNAL( markLine( qint64 ) ), 599 this, SLOT( markLineFromFiltered( qint64 ) ) ); 600 601 connect(logMainView, SIGNAL( addToSearch( const QString& ) ), 602 this, SLOT( addToSearch( const QString& ) ) ); 603 connect(filteredView, SIGNAL( addToSearch( const QString& ) ), 604 this, SLOT( addToSearch( const QString& ) ) ); 605 606 connect(filteredView, SIGNAL( mouseHoveredOverLine( qint64 ) ), 607 this, SLOT( mouseHoveredOverMatch( qint64 ) ) ); 608 connect(filteredView, SIGNAL( mouseLeftHoveringZone() ), 609 overviewWidget_, SLOT( removeHighlight() ) ); 610 611 // Follow option (up and down) 612 connect(this, SIGNAL( followSet( bool ) ), 613 logMainView, SLOT( followSet( bool ) ) ); 614 connect(this, SIGNAL( followSet( bool ) ), 615 filteredView, SLOT( followSet( bool ) ) ); 616 connect(logMainView, SIGNAL( followDisabled() ), 617 this, SIGNAL( followDisabled() ) ); 618 connect(filteredView, SIGNAL( followDisabled() ), 619 this, SIGNAL( followDisabled() ) ); 620 621 connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ), 622 this, SLOT( updateFilteredView( int, int ) ) ); 623 624 // QuickFind 625 connect( quickFindWidget_, SIGNAL( close() ), 626 this, SLOT( hideQuickFindBar() ) ); 627 connect( quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 628 quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 629 connect( quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 630 quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 631 connect( quickFindWidget_, SIGNAL( cancelSearch() ), 632 quickFindMux_, SLOT( cancelSearch() ) ); 633 connect( quickFindWidget_, SIGNAL( searchForward() ), 634 quickFindMux_, SLOT( searchForward() ) ); 635 connect( quickFindWidget_, SIGNAL( searchBackward() ), 636 quickFindMux_, SLOT( searchBackward() ) ); 637 connect( quickFindWidget_, SIGNAL( searchNext() ), 638 quickFindMux_, SLOT( searchNext() ) ); 639 640 // QuickFind changes coming from the views 641 connect( quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 642 this, SLOT( changeQFPattern( const QString& ) ) ); 643 connect( quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 644 quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 645 connect( quickFindMux_, SIGNAL( clearNotification() ), 646 quickFindWidget_, SLOT( clearNotification() ) ); 647 648 // Sent load file update to MainWindow (for status update) 649 connect( logData_, SIGNAL( loadingProgressed( int ) ), 650 this, SIGNAL( loadingProgressed( int ) ) ); 651 connect( logData_, SIGNAL( loadingFinished( bool ) ), 652 this, SLOT( loadingFinishedHandler( bool ) ) ); 653 connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ), 654 this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) ); 655 656 // Search auto-refresh 657 connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 658 this, SLOT( searchRefreshChangedHandler( int ) ) ); 659 } 660 661 // Create a new search using the text passed, replace the currently 662 // used one and destroy the old one. 663 void CrawlerWidget::replaceCurrentSearch( const QString& searchText ) 664 { 665 // Interrupt the search if it's ongoing 666 logFilteredData_->interruptSearch(); 667 668 // We have to wait for the last search update (100%) 669 // before clearing/restarting to avoid having remaining results. 670 671 // FIXME: this is a bit of a hack, we call processEvents 672 // for Qt to empty its event queue, including (hopefully) 673 // the search update event sent by logFilteredData_. It saves 674 // us the overhead of having proper sync. 675 QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); 676 677 if ( !searchText.isEmpty() ) { 678 // Determine the type of regexp depending on the config 679 QRegExp::PatternSyntax syntax; 680 static Configuration& config = Persistent<Configuration>( "settings" ); 681 switch ( config.mainRegexpType() ) { 682 case Wildcard: 683 syntax = QRegExp::Wildcard; 684 break; 685 case FixedString: 686 syntax = QRegExp::FixedString; 687 break; 688 default: 689 syntax = QRegExp::RegExp2; 690 break; 691 } 692 693 // Set the pattern case insensitive if needed 694 Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive; 695 if ( ignoreCaseCheck->checkState() == Qt::Checked ) 696 case_sensitivity = Qt::CaseInsensitive; 697 698 // Constructs the regexp 699 QRegExp regexp( searchText, case_sensitivity, syntax ); 700 701 if ( regexp.isValid() ) { 702 // Activate the stop button 703 stopButton->setEnabled( true ); 704 // Start a new asynchronous search 705 logFilteredData_->runSearch( regexp ); 706 // Accept auto-refresh of the search 707 searchState_.startSearch(); 708 } 709 else { 710 // The regexp is wrong 711 logFilteredData_->clearSearch(); 712 filteredView->updateData(); 713 searchState_.resetState(); 714 715 // Inform the user 716 QString errorMessage = tr("Error in expression: "); 717 errorMessage += regexp.errorString(); 718 searchInfoLine->setPalette( errorPalette ); 719 searchInfoLine->setText( errorMessage ); 720 } 721 } 722 else { 723 logFilteredData_->clearSearch(); 724 filteredView->updateData(); 725 searchState_.resetState(); 726 printSearchInfoMessage(); 727 } 728 // Connect the search to the top view 729 logMainView->useNewFiltering( logFilteredData_ ); 730 } 731 732 // Updates the content of the drop down list for the saved searches, 733 // called when the SavedSearch has been changed. 734 void CrawlerWidget::updateSearchCombo() 735 { 736 const QString text = searchLineEdit->lineEdit()->text(); 737 searchLineEdit->clear(); 738 searchLineEdit->addItems( savedSearches->recentSearches() ); 739 // In case we had something that wasn't added to the list (blank...): 740 searchLineEdit->lineEdit()->setText( text ); 741 } 742 743 // Print the search info message. 744 void CrawlerWidget::printSearchInfoMessage( int nbMatches ) 745 { 746 QString text; 747 748 switch ( searchState_.getState() ) { 749 case SearchState::NoSearch: 750 // Blank text is fine 751 break; 752 case SearchState::Static: 753 text = tr("%1 match%2 found.").arg( nbMatches ) 754 .arg( nbMatches > 1 ? "es" : "" ); 755 break; 756 case SearchState::Autorefreshing: 757 text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches ) 758 .arg( nbMatches > 1 ? "es" : "" ); 759 break; 760 case SearchState::FileTruncated: 761 text = tr("File truncated on disk, previous search results are not valid anymore."); 762 break; 763 } 764 765 searchInfoLine->setPalette( searchInfoLineDefaultPalette ); 766 searchInfoLine->setText( text ); 767 } 768 769 // 770 // SearchState implementation 771 // 772 void CrawlerWidget::SearchState::resetState() 773 { 774 state_ = NoSearch; 775 } 776 777 void CrawlerWidget::SearchState::setAutorefresh( bool refresh ) 778 { 779 autoRefreshRequested_ = refresh; 780 781 if ( refresh ) { 782 if ( state_ == Static ) 783 state_ = Autorefreshing; 784 } 785 else { 786 if ( state_ == Autorefreshing ) 787 state_ = Static; 788 } 789 } 790 791 void CrawlerWidget::SearchState::truncateFile() 792 { 793 state_ = FileTruncated; 794 } 795 796 void CrawlerWidget::SearchState::changeExpression() 797 { 798 if ( state_ == Autorefreshing ) 799 state_ = Static; 800 } 801 802 void CrawlerWidget::SearchState::stopSearch() 803 { 804 if ( state_ == Autorefreshing ) 805 state_ = Static; 806 } 807 808 void CrawlerWidget::SearchState::startSearch() 809 { 810 if ( autoRefreshRequested_ ) 811 state_ = Autorefreshing; 812 else 813 state_ = Static; 814 } 815