1 /* 2 * Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 // Implementation of the view context for the CrawlerWidget 53 class CrawlerWidgetContext : public ViewContextInterface { 54 public: 55 // Construct from the stored string representation 56 CrawlerWidgetContext( const char* string ); 57 // Construct from the value passsed 58 CrawlerWidgetContext( QList<int> sizes, 59 bool ignore_case, 60 bool auto_refresh ) 61 : sizes_( sizes ), 62 ignore_case_( ignore_case ), 63 auto_refresh_( auto_refresh ) {} 64 65 // Implementation of the ViewContextInterface function 66 std::string toString() const; 67 68 // Access the Qt sizes array for the QSplitter 69 QList<int> sizes() const { return sizes_; } 70 71 bool ignoreCase() const { return ignore_case_; } 72 bool autoRefresh() const { return auto_refresh_; } 73 74 private: 75 QList<int> sizes_; 76 77 bool ignore_case_; 78 bool auto_refresh_; 79 }; 80 81 // Constructor only does trivial construction. The real work is done once 82 // the data is attached. 83 CrawlerWidget::CrawlerWidget( QWidget *parent ) 84 : QSplitter( parent ), overview_() 85 { 86 logData_ = nullptr; 87 logFilteredData_ = nullptr; 88 89 quickFindPattern_ = nullptr; 90 savedSearches_ = nullptr; 91 qfSavedFocus_ = nullptr; 92 93 // Until we have received confirmation loading is finished, we 94 // should consider we are loading something. 95 loadingInProgress_ = true; 96 97 currentLineNumber_ = 0; 98 } 99 100 // The top line is first one on the main display 101 int CrawlerWidget::getTopLine() const 102 { 103 return logMainView->getTopLine(); 104 } 105 106 QString CrawlerWidget::getSelectedText() const 107 { 108 if ( filteredView->hasFocus() ) 109 return filteredView->getSelection(); 110 else 111 return logMainView->getSelection(); 112 } 113 114 void CrawlerWidget::selectAll() 115 { 116 activeView()->selectAll(); 117 } 118 119 // Return a pointer to the view in which we should do the QuickFind 120 SearchableWidgetInterface* CrawlerWidget::doGetActiveSearchable() const 121 { 122 return activeView(); 123 } 124 125 // Return all the searchable widgets (views) 126 std::vector<QObject*> CrawlerWidget::doGetAllSearchables() const 127 { 128 std::vector<QObject*> searchables = 129 { logMainView, filteredView }; 130 131 return searchables; 132 } 133 134 // Update the state of the parent 135 void CrawlerWidget::doSendAllStateSignals() 136 { 137 emit updateLineNumber( currentLineNumber_ ); 138 if ( !loadingInProgress_ ) 139 emit loadingFinished( LoadingStatus::Successful ); 140 } 141 142 // 143 // Public slots 144 // 145 146 void CrawlerWidget::stopLoading() 147 { 148 logFilteredData_->interruptSearch(); 149 logData_->interruptLoading(); 150 } 151 152 void CrawlerWidget::reload() 153 { 154 searchState_.resetState(); 155 logFilteredData_->clearSearch(); 156 filteredView->updateData(); 157 printSearchInfoMessage(); 158 159 logData_->reload(); 160 } 161 162 // 163 // Protected functions 164 // 165 void CrawlerWidget::doSetData( 166 std::shared_ptr<LogData> log_data, 167 std::shared_ptr<LogFilteredData> filtered_data ) 168 { 169 logData_ = log_data.get(); 170 logFilteredData_ = filtered_data.get(); 171 } 172 173 void CrawlerWidget::doSetQuickFindPattern( 174 std::shared_ptr<QuickFindPattern> qfp ) 175 { 176 quickFindPattern_ = qfp; 177 } 178 179 void CrawlerWidget::doSetSavedSearches( 180 std::shared_ptr<SavedSearches> saved_searches ) 181 { 182 savedSearches_ = saved_searches; 183 184 // We do setup now, assuming doSetData has been called before 185 // us, that's not great really... 186 setup(); 187 } 188 189 void CrawlerWidget::doSetViewContext( 190 const char* view_context ) 191 { 192 LOG(logDEBUG) << "CrawlerWidget::doSetViewContext: " << view_context; 193 194 CrawlerWidgetContext context = { view_context }; 195 196 setSizes( context.sizes() ); 197 ignoreCaseCheck->setCheckState( context.ignoreCase() ? Qt::Checked : Qt::Unchecked ); 198 199 auto auto_refresh_check_state = context.autoRefresh() ? Qt::Checked : Qt::Unchecked; 200 searchRefreshCheck->setCheckState( auto_refresh_check_state ); 201 // Manually call the handler as it is not called when changing the state programmatically 202 searchRefreshChangedHandler( auto_refresh_check_state ); 203 } 204 205 std::shared_ptr<const ViewContextInterface> 206 CrawlerWidget::doGetViewContext() const 207 { 208 auto context = std::make_shared<const CrawlerWidgetContext>( 209 sizes(), 210 ( ignoreCaseCheck->checkState() == Qt::Checked ), 211 ( searchRefreshCheck->checkState() == Qt::Checked ) ); 212 213 return static_cast<std::shared_ptr<const ViewContextInterface>>( context ); 214 } 215 216 // 217 // Slots 218 // 219 220 void CrawlerWidget::startNewSearch() 221 { 222 // Record the search line in the recent list 223 // (reload the list first in case another glogg changed it) 224 GetPersistentInfo().retrieve( "savedSearches" ); 225 savedSearches_->addRecent( searchLineEdit->currentText() ); 226 GetPersistentInfo().save( "savedSearches" ); 227 228 // Update the SearchLine (history) 229 updateSearchCombo(); 230 // Call the private function to do the search 231 replaceCurrentSearch( searchLineEdit->currentText() ); 232 } 233 234 void CrawlerWidget::stopSearch() 235 { 236 logFilteredData_->interruptSearch(); 237 searchState_.stopSearch(); 238 printSearchInfoMessage(); 239 } 240 241 // When receiving the 'newDataAvailable' signal from LogFilteredData 242 void CrawlerWidget::updateFilteredView( int nbMatches, int progress ) 243 { 244 LOG(logDEBUG) << "updateFilteredView received."; 245 246 if ( progress == 100 ) { 247 // Searching done 248 printSearchInfoMessage( nbMatches ); 249 searchInfoLine->hideGauge(); 250 // De-activate the stop button 251 stopButton->setEnabled( false ); 252 } 253 else { 254 // Search in progress 255 // We ignore 0% and 100% to avoid a flash when the search is very short 256 if ( progress > 0 ) { 257 searchInfoLine->setText( 258 tr("Search in progress (%1 %)... %2 match%3 found so far.") 259 .arg( progress ) 260 .arg( nbMatches ) 261 .arg( nbMatches > 1 ? "es" : "" ) ); 262 searchInfoLine->displayGauge( progress ); 263 } 264 } 265 266 // Recompute the content of the filtered window. 267 filteredView->updateData(); 268 269 // Update the match overview 270 overview_.updateData( logData_->getNbLine() ); 271 272 // Also update the top window for the coloured bullets. 273 update(); 274 } 275 276 void CrawlerWidget::jumpToMatchingLine(int filteredLineNb) 277 { 278 int mainViewLine = logFilteredData_->getMatchingLineNumber(filteredLineNb); 279 logMainView->selectAndDisplayLine(mainViewLine); // FIXME: should be done with a signal. 280 } 281 282 void CrawlerWidget::updateLineNumberHandler( int line ) 283 { 284 currentLineNumber_ = line; 285 emit updateLineNumber( line ); 286 } 287 288 void CrawlerWidget::markLineFromMain( qint64 line ) 289 { 290 if ( logFilteredData_->isLineMarked( line ) ) 291 logFilteredData_->deleteMark( line ); 292 else 293 logFilteredData_->addMark( line ); 294 295 // Recompute the content of the filtered window. 296 filteredView->updateData(); 297 298 // Update the match overview 299 overview_.updateData( logData_->getNbLine() ); 300 301 // Also update the top window for the coloured bullets. 302 update(); 303 } 304 305 void CrawlerWidget::markLineFromFiltered( qint64 line ) 306 { 307 qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line ); 308 if ( logFilteredData_->filteredLineTypeByIndex( line ) 309 == LogFilteredData::Mark ) 310 logFilteredData_->deleteMark( line_in_file ); 311 else 312 logFilteredData_->addMark( line_in_file ); 313 314 // Recompute the content of the filtered window. 315 filteredView->updateData(); 316 317 // Update the match overview 318 overview_.updateData( logData_->getNbLine() ); 319 320 // Also update the top window for the coloured bullets. 321 update(); 322 } 323 324 void CrawlerWidget::applyConfiguration() 325 { 326 std::shared_ptr<Configuration> config = 327 Persistent<Configuration>( "settings" ); 328 QFont font = config->mainFont(); 329 330 LOG(logDEBUG) << "CrawlerWidget::applyConfiguration"; 331 332 // Whatever font we use, we should NOT use kerning 333 font.setKerning( false ); 334 font.setFixedPitch( true ); 335 #if QT_VERSION > 0x040700 336 // Necessary on systems doing subpixel positionning (e.g. Ubuntu 12.04) 337 font.setStyleStrategy( QFont::ForceIntegerMetrics ); 338 #endif 339 logMainView->setFont(font); 340 filteredView->setFont(font); 341 342 logMainView->setLineNumbersVisible( config->mainLineNumbersVisible() ); 343 filteredView->setLineNumbersVisible( config->filteredLineNumbersVisible() ); 344 345 overview_.setVisible( config->isOverviewVisible() ); 346 logMainView->refreshOverview(); 347 348 logMainView->updateDisplaySize(); 349 logMainView->update(); 350 filteredView->updateDisplaySize(); 351 filteredView->update(); 352 353 // Update the SearchLine (history) 354 updateSearchCombo(); 355 } 356 357 void CrawlerWidget::enteringQuickFind() 358 { 359 LOG(logDEBUG) << "CrawlerWidget::enteringQuickFind"; 360 361 // Remember who had the focus (only if it is one of our views) 362 QWidget* focus_widget = QApplication::focusWidget(); 363 364 if ( ( focus_widget == logMainView ) || ( focus_widget == filteredView ) ) 365 qfSavedFocus_ = focus_widget; 366 else 367 qfSavedFocus_ = nullptr; 368 } 369 370 void CrawlerWidget::exitingQuickFind() 371 { 372 // Restore the focus once the QFBar has been hidden 373 if ( qfSavedFocus_ ) 374 qfSavedFocus_->setFocus(); 375 } 376 377 void CrawlerWidget::loadingFinishedHandler( LoadingStatus status ) 378 { 379 loadingInProgress_ = false; 380 381 // We need to refresh the main window because the view lines on the 382 // overview have probably changed. 383 overview_.updateData( logData_->getNbLine() ); 384 385 // FIXME, handle topLine 386 // logMainView->updateData( logData_, topLine ); 387 logMainView->updateData(); 388 389 // Shall we Forbid starting a search when loading in progress? 390 // searchButton->setEnabled( false ); 391 392 // searchButton->setEnabled( true ); 393 394 // See if we need to auto-refresh the search 395 if ( searchState_.isAutorefreshAllowed() ) { 396 if ( searchState_.isFileTruncated() ) 397 // We need to restart the search 398 replaceCurrentSearch( searchLineEdit->currentText() ); 399 else 400 logFilteredData_->updateSearch(); 401 } 402 403 emit loadingFinished( status ); 404 } 405 406 void CrawlerWidget::fileChangedHandler( LogData::MonitoredFileStatus status ) 407 { 408 // Handle the case where the file has been truncated 409 if ( status == LogData::Truncated ) { 410 // Clear all marks (TODO offer the option to keep them) 411 logFilteredData_->clearMarks(); 412 if ( ! searchInfoLine->text().isEmpty() ) { 413 // Invalidate the search 414 logFilteredData_->clearSearch(); 415 filteredView->updateData(); 416 searchState_.truncateFile(); 417 printSearchInfoMessage(); 418 } 419 } 420 } 421 422 // Returns a pointer to the window in which the search should be done 423 AbstractLogView* CrawlerWidget::activeView() const 424 { 425 QWidget* activeView; 426 427 // Search in the window that has focus, or the window where 'Find' was 428 // called from, or the main window. 429 if ( filteredView->hasFocus() || logMainView->hasFocus() ) 430 activeView = QApplication::focusWidget(); 431 else 432 activeView = qfSavedFocus_; 433 434 if ( activeView ) { 435 AbstractLogView* view = qobject_cast<AbstractLogView*>( activeView ); 436 return view; 437 } 438 else { 439 LOG(logWARNING) << "No active view, defaulting to logMainView"; 440 return logMainView; 441 } 442 } 443 444 void CrawlerWidget::searchForward() 445 { 446 LOG(logDEBUG) << "CrawlerWidget::searchForward"; 447 448 activeView()->searchForward(); 449 } 450 451 void CrawlerWidget::searchBackward() 452 { 453 LOG(logDEBUG) << "CrawlerWidget::searchBackward"; 454 455 activeView()->searchBackward(); 456 } 457 458 void CrawlerWidget::searchRefreshChangedHandler( int state ) 459 { 460 searchState_.setAutorefresh( state == Qt::Checked ); 461 printSearchInfoMessage( logFilteredData_->getNbMatches() ); 462 } 463 464 void CrawlerWidget::searchTextChangeHandler() 465 { 466 // We suspend auto-refresh 467 searchState_.changeExpression(); 468 printSearchInfoMessage( logFilteredData_->getNbMatches() ); 469 } 470 471 void CrawlerWidget::changeFilteredViewVisibility( int index ) 472 { 473 QStandardItem* item = visibilityModel_->item( index ); 474 FilteredView::Visibility visibility = 475 static_cast< FilteredView::Visibility>( item->data().toInt() ); 476 477 filteredView->setVisibility( visibility ); 478 } 479 480 void CrawlerWidget::addToSearch( const QString& string ) 481 { 482 QString text = searchLineEdit->currentText(); 483 484 if ( text.isEmpty() ) 485 text = string; 486 else { 487 // Escape the regexp chars from the string before adding it. 488 text += ( '|' + QRegExp::escape( string ) ); 489 } 490 491 searchLineEdit->setEditText( text ); 492 493 // Set the focus to lineEdit so that the user can press 'Return' immediately 494 searchLineEdit->lineEdit()->setFocus(); 495 } 496 497 void CrawlerWidget::mouseHoveredOverMatch( qint64 line ) 498 { 499 qint64 line_in_mainview = logFilteredData_->getMatchingLineNumber( line ); 500 501 overviewWidget_->highlightLine( line_in_mainview ); 502 } 503 504 // 505 // Private functions 506 // 507 508 // Build the widget and connect all the signals, this must be done once 509 // the data are attached. 510 void CrawlerWidget::setup() 511 { 512 setOrientation(Qt::Vertical); 513 514 assert( logData_ ); 515 assert( logFilteredData_ ); 516 517 // The views 518 bottomWindow = new QWidget; 519 overviewWidget_ = new OverviewWidget(); 520 logMainView = new LogMainView( 521 logData_, quickFindPattern_.get(), &overview_, overviewWidget_ ); 522 filteredView = new FilteredView( 523 logFilteredData_, quickFindPattern_.get() ); 524 525 overviewWidget_->setOverview( &overview_ ); 526 overviewWidget_->setParent( logMainView ); 527 528 // Connect the search to the top view 529 logMainView->useNewFiltering( logFilteredData_ ); 530 531 // Construct the visibility button 532 visibilityModel_ = new QStandardItemModel( this ); 533 534 QStandardItem *marksAndMatchesItem = new QStandardItem( tr( "Marks and matches" ) ); 535 QPixmap marksAndMatchesPixmap( 16, 10 ); 536 marksAndMatchesPixmap.fill( Qt::gray ); 537 marksAndMatchesItem->setIcon( QIcon( marksAndMatchesPixmap ) ); 538 marksAndMatchesItem->setData( FilteredView::MarksAndMatches ); 539 visibilityModel_->appendRow( marksAndMatchesItem ); 540 541 QStandardItem *marksItem = new QStandardItem( tr( "Marks" ) ); 542 QPixmap marksPixmap( 16, 10 ); 543 marksPixmap.fill( Qt::blue ); 544 marksItem->setIcon( QIcon( marksPixmap ) ); 545 marksItem->setData( FilteredView::MarksOnly ); 546 visibilityModel_->appendRow( marksItem ); 547 548 QStandardItem *matchesItem = new QStandardItem( tr( "Matches" ) ); 549 QPixmap matchesPixmap( 16, 10 ); 550 matchesPixmap.fill( Qt::red ); 551 matchesItem->setIcon( QIcon( matchesPixmap ) ); 552 matchesItem->setData( FilteredView::MatchesOnly ); 553 visibilityModel_->appendRow( matchesItem ); 554 555 QListView *visibilityView = new QListView( this ); 556 visibilityView->setMovement( QListView::Static ); 557 visibilityView->setMinimumWidth( 170 ); // Only needed with custom style-sheet 558 559 visibilityBox = new QComboBox(); 560 visibilityBox->setModel( visibilityModel_ ); 561 visibilityBox->setView( visibilityView ); 562 563 // Select "Marks and matches" by default (same default as the filtered view) 564 visibilityBox->setCurrentIndex( 0 ); 565 566 // TODO: Maybe there is some way to set the popup width to be 567 // sized-to-content (as it is when the stylesheet is not overriden) in the 568 // stylesheet as opposed to setting a hard min-width on the view above. 569 visibilityBox->setStyleSheet( " \ 570 QComboBox:on {\ 571 padding: 1px 2px 1px 6px;\ 572 width: 19px;\ 573 } \ 574 QComboBox:!on {\ 575 padding: 1px 2px 1px 7px;\ 576 width: 19px;\ 577 height: 16px;\ 578 border: 1px solid gray;\ 579 } \ 580 QComboBox::drop-down::down-arrow {\ 581 width: 0px;\ 582 border-width: 0px;\ 583 } \ 584 " ); 585 586 // Construct the Search Info line 587 searchInfoLine = new InfoLine(); 588 searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 589 searchInfoLine->setLineWidth( 1 ); 590 searchInfoLineDefaultPalette = searchInfoLine->palette(); 591 592 ignoreCaseCheck = new QCheckBox( "Ignore &case" ); 593 searchRefreshCheck = new QCheckBox( "Auto-&refresh" ); 594 595 // Construct the Search line 596 searchLabel = new QLabel(tr("&Text: ")); 597 searchLineEdit = new QComboBox; 598 searchLineEdit->setEditable( true ); 599 searchLineEdit->setCompleter( 0 ); 600 searchLineEdit->addItems( savedSearches_->recentSearches() ); 601 searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); 602 searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); 603 604 searchLabel->setBuddy( searchLineEdit ); 605 606 searchButton = new QToolButton(); 607 searchButton->setText( tr("&Search") ); 608 searchButton->setAutoRaise( true ); 609 610 stopButton = new QToolButton(); 611 stopButton->setIcon( QIcon(":/images/stop16.png") ); 612 stopButton->setAutoRaise( true ); 613 stopButton->setEnabled( false ); 614 615 QHBoxLayout* searchLineLayout = new QHBoxLayout; 616 searchLineLayout->addWidget(searchLabel); 617 searchLineLayout->addWidget(searchLineEdit); 618 searchLineLayout->addWidget(searchButton); 619 searchLineLayout->addWidget(stopButton); 620 searchLineLayout->setContentsMargins(6, 0, 6, 0); 621 stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 622 searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 623 624 QHBoxLayout* searchInfoLineLayout = new QHBoxLayout; 625 searchInfoLineLayout->addWidget( visibilityBox ); 626 searchInfoLineLayout->addWidget( searchInfoLine ); 627 searchInfoLineLayout->addWidget( ignoreCaseCheck ); 628 searchInfoLineLayout->addWidget( searchRefreshCheck ); 629 630 // Construct the bottom window 631 QVBoxLayout* bottomMainLayout = new QVBoxLayout; 632 bottomMainLayout->addLayout(searchLineLayout); 633 bottomMainLayout->addLayout(searchInfoLineLayout); 634 bottomMainLayout->addWidget(filteredView); 635 bottomMainLayout->setContentsMargins(2, 1, 2, 1); 636 bottomWindow->setLayout(bottomMainLayout); 637 638 addWidget( logMainView ); 639 addWidget( bottomWindow ); 640 641 // Default splitter position (usually overridden by the config file) 642 QList<int> splitterSizes; 643 splitterSizes += 400; 644 splitterSizes += 100; 645 setSizes( splitterSizes ); 646 647 // Default search checkboxes 648 auto config = Persistent<Configuration>( "settings" ); 649 searchRefreshCheck->setCheckState( config->isSearchAutoRefreshDefault() ? 650 Qt::Checked : Qt::Unchecked ); 651 // Manually call the handler as it is not called when changing the state programmatically 652 searchRefreshChangedHandler( searchRefreshCheck->checkState() ); 653 ignoreCaseCheck->setCheckState( config->isSearchIgnoreCaseDefault() ? 654 Qt::Checked : Qt::Unchecked ); 655 656 // Connect the signals 657 connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ), 658 searchButton, SIGNAL( clicked() )); 659 connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ), 660 this, SLOT( searchTextChangeHandler() )); 661 connect(searchButton, SIGNAL( clicked() ), 662 this, SLOT( startNewSearch() ) ); 663 connect(stopButton, SIGNAL( clicked() ), 664 this, SLOT( stopSearch() ) ); 665 666 connect(visibilityBox, SIGNAL( currentIndexChanged( int ) ), 667 this, SLOT( changeFilteredViewVisibility( int ) ) ); 668 669 connect(logMainView, SIGNAL( newSelection( int ) ), 670 logMainView, SLOT( update() ) ); 671 connect(filteredView, SIGNAL( newSelection( int ) ), 672 this, SLOT( jumpToMatchingLine( int ) ) ); 673 connect(filteredView, SIGNAL( newSelection( int ) ), 674 filteredView, SLOT( update() ) ); 675 connect(logMainView, SIGNAL( updateLineNumber( int ) ), 676 this, SLOT( updateLineNumberHandler( int ) ) ); 677 connect(logMainView, SIGNAL( markLine( qint64 ) ), 678 this, SLOT( markLineFromMain( qint64 ) ) ); 679 connect(filteredView, SIGNAL( markLine( qint64 ) ), 680 this, SLOT( markLineFromFiltered( qint64 ) ) ); 681 682 connect(logMainView, SIGNAL( addToSearch( const QString& ) ), 683 this, SLOT( addToSearch( const QString& ) ) ); 684 connect(filteredView, SIGNAL( addToSearch( const QString& ) ), 685 this, SLOT( addToSearch( const QString& ) ) ); 686 687 connect(filteredView, SIGNAL( mouseHoveredOverLine( qint64 ) ), 688 this, SLOT( mouseHoveredOverMatch( qint64 ) ) ); 689 connect(filteredView, SIGNAL( mouseLeftHoveringZone() ), 690 overviewWidget_, SLOT( removeHighlight() ) ); 691 692 // Follow option (up and down) 693 connect(this, SIGNAL( followSet( bool ) ), 694 logMainView, SLOT( followSet( bool ) ) ); 695 connect(this, SIGNAL( followSet( bool ) ), 696 filteredView, SLOT( followSet( bool ) ) ); 697 connect(logMainView, SIGNAL( followDisabled() ), 698 this, SIGNAL( followDisabled() ) ); 699 connect(filteredView, SIGNAL( followDisabled() ), 700 this, SIGNAL( followDisabled() ) ); 701 702 connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ), 703 this, SLOT( updateFilteredView( int, int ) ) ); 704 705 // Sent load file update to MainWindow (for status update) 706 connect( logData_, SIGNAL( loadingProgressed( int ) ), 707 this, SIGNAL( loadingProgressed( int ) ) ); 708 connect( logData_, SIGNAL( loadingFinished( LoadingStatus ) ), 709 this, SLOT( loadingFinishedHandler( LoadingStatus ) ) ); 710 connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ), 711 this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) ); 712 713 // Search auto-refresh 714 connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 715 this, SLOT( searchRefreshChangedHandler( int ) ) ); 716 717 // Advise the parent the checkboxes have been changed 718 // (for maintaining default config) 719 connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 720 this, SIGNAL( searchRefreshChanged( int ) ) ); 721 connect( ignoreCaseCheck, SIGNAL( stateChanged( int ) ), 722 this, SIGNAL( ignoreCaseChanged( int ) ) ); 723 } 724 725 // Create a new search using the text passed, replace the currently 726 // used one and destroy the old one. 727 void CrawlerWidget::replaceCurrentSearch( const QString& searchText ) 728 { 729 // Interrupt the search if it's ongoing 730 logFilteredData_->interruptSearch(); 731 732 // We have to wait for the last search update (100%) 733 // before clearing/restarting to avoid having remaining results. 734 735 // FIXME: this is a bit of a hack, we call processEvents 736 // for Qt to empty its event queue, including (hopefully) 737 // the search update event sent by logFilteredData_. It saves 738 // us the overhead of having proper sync. 739 QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); 740 741 if ( !searchText.isEmpty() ) { 742 // Determine the type of regexp depending on the config 743 QRegExp::PatternSyntax syntax; 744 static std::shared_ptr<Configuration> config = 745 Persistent<Configuration>( "settings" ); 746 switch ( config->mainRegexpType() ) { 747 case Wildcard: 748 syntax = QRegExp::Wildcard; 749 break; 750 case FixedString: 751 syntax = QRegExp::FixedString; 752 break; 753 default: 754 syntax = QRegExp::RegExp2; 755 break; 756 } 757 758 // Set the pattern case insensitive if needed 759 Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive; 760 if ( ignoreCaseCheck->checkState() == Qt::Checked ) 761 case_sensitivity = Qt::CaseInsensitive; 762 763 // Constructs the regexp 764 QRegExp regexp( searchText, case_sensitivity, syntax ); 765 766 if ( regexp.isValid() ) { 767 // Activate the stop button 768 stopButton->setEnabled( true ); 769 // Start a new asynchronous search 770 logFilteredData_->runSearch( regexp ); 771 // Accept auto-refresh of the search 772 searchState_.startSearch(); 773 } 774 else { 775 // The regexp is wrong 776 logFilteredData_->clearSearch(); 777 filteredView->updateData(); 778 searchState_.resetState(); 779 780 // Inform the user 781 QString errorMessage = tr("Error in expression: "); 782 errorMessage += regexp.errorString(); 783 searchInfoLine->setPalette( errorPalette ); 784 searchInfoLine->setText( errorMessage ); 785 } 786 } 787 else { 788 logFilteredData_->clearSearch(); 789 filteredView->updateData(); 790 searchState_.resetState(); 791 printSearchInfoMessage(); 792 } 793 } 794 795 // Updates the content of the drop down list for the saved searches, 796 // called when the SavedSearch has been changed. 797 void CrawlerWidget::updateSearchCombo() 798 { 799 const QString text = searchLineEdit->lineEdit()->text(); 800 searchLineEdit->clear(); 801 searchLineEdit->addItems( savedSearches_->recentSearches() ); 802 // In case we had something that wasn't added to the list (blank...): 803 searchLineEdit->lineEdit()->setText( text ); 804 } 805 806 // Print the search info message. 807 void CrawlerWidget::printSearchInfoMessage( int nbMatches ) 808 { 809 QString text; 810 811 switch ( searchState_.getState() ) { 812 case SearchState::NoSearch: 813 // Blank text is fine 814 break; 815 case SearchState::Static: 816 text = tr("%1 match%2 found.").arg( nbMatches ) 817 .arg( nbMatches > 1 ? "es" : "" ); 818 break; 819 case SearchState::Autorefreshing: 820 text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches ) 821 .arg( nbMatches > 1 ? "es" : "" ); 822 break; 823 case SearchState::FileTruncated: 824 case SearchState::TruncatedAutorefreshing: 825 text = tr("File truncated on disk, previous search results are not valid anymore."); 826 break; 827 } 828 829 searchInfoLine->setPalette( searchInfoLineDefaultPalette ); 830 searchInfoLine->setText( text ); 831 } 832 833 // 834 // SearchState implementation 835 // 836 void CrawlerWidget::SearchState::resetState() 837 { 838 state_ = NoSearch; 839 } 840 841 void CrawlerWidget::SearchState::setAutorefresh( bool refresh ) 842 { 843 autoRefreshRequested_ = refresh; 844 845 if ( refresh ) { 846 if ( state_ == Static ) 847 state_ = Autorefreshing; 848 /* 849 else if ( state_ == FileTruncated ) 850 state_ = TruncatedAutorefreshing; 851 */ 852 } 853 else { 854 if ( state_ == Autorefreshing ) 855 state_ = Static; 856 else if ( state_ == TruncatedAutorefreshing ) 857 state_ = FileTruncated; 858 } 859 } 860 861 void CrawlerWidget::SearchState::truncateFile() 862 { 863 if ( state_ == Autorefreshing || state_ == TruncatedAutorefreshing ) { 864 state_ = TruncatedAutorefreshing; 865 } 866 else { 867 state_ = FileTruncated; 868 } 869 } 870 871 void CrawlerWidget::SearchState::changeExpression() 872 { 873 if ( state_ == Autorefreshing ) 874 state_ = Static; 875 } 876 877 void CrawlerWidget::SearchState::stopSearch() 878 { 879 if ( state_ == Autorefreshing ) 880 state_ = Static; 881 } 882 883 void CrawlerWidget::SearchState::startSearch() 884 { 885 if ( autoRefreshRequested_ ) 886 state_ = Autorefreshing; 887 else 888 state_ = Static; 889 } 890 891 /* 892 * CrawlerWidgetContext 893 */ 894 CrawlerWidgetContext::CrawlerWidgetContext( const char* string ) 895 { 896 QRegExp regex = QRegExp( "S(\\d+):(\\d+)" ); 897 898 if ( regex.indexIn( string ) > -1 ) { 899 sizes_ = { regex.cap(1).toInt(), regex.cap(2).toInt() }; 900 LOG(logDEBUG) << "sizes_: " << sizes_[0] << " " << sizes_[1]; 901 } 902 else { 903 LOG(logWARNING) << "Unrecognised view size: " << string; 904 905 // Default values; 906 sizes_ = { 100, 400 }; 907 } 908 909 QRegExp case_refresh_regex = QRegExp( "IC(\\d+):AR(\\d+)" ); 910 911 if ( case_refresh_regex.indexIn( string ) > -1 ) { 912 ignore_case_ = ( case_refresh_regex.cap(1).toInt() == 1 ); 913 auto_refresh_ = ( case_refresh_regex.cap(2).toInt() == 1 ); 914 915 LOG(logDEBUG) << "ignore_case_: " << ignore_case_ << " auto_refresh_: " 916 << auto_refresh_; 917 } 918 else { 919 LOG(logWARNING) << "Unrecognised case/refresh: " << string; 920 ignore_case_ = false; 921 auto_refresh_ = false; 922 } 923 } 924 925 std::string CrawlerWidgetContext::toString() const 926 { 927 char string[160]; 928 929 snprintf( string, sizeof string, "S%d:%d:IC%d:AR%d", 930 sizes_[0], sizes_[1], 931 ignore_case_, auto_refresh_ ); 932 933 return { string }; 934 } 935