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 // Construct the visibility button 529 visibilityModel_ = new QStandardItemModel( this ); 530 531 QStandardItem *marksAndMatchesItem = new QStandardItem( tr( "Marks and matches" ) ); 532 QPixmap marksAndMatchesPixmap( 16, 10 ); 533 marksAndMatchesPixmap.fill( Qt::gray ); 534 marksAndMatchesItem->setIcon( QIcon( marksAndMatchesPixmap ) ); 535 marksAndMatchesItem->setData( FilteredView::MarksAndMatches ); 536 visibilityModel_->appendRow( marksAndMatchesItem ); 537 538 QStandardItem *marksItem = new QStandardItem( tr( "Marks" ) ); 539 QPixmap marksPixmap( 16, 10 ); 540 marksPixmap.fill( Qt::blue ); 541 marksItem->setIcon( QIcon( marksPixmap ) ); 542 marksItem->setData( FilteredView::MarksOnly ); 543 visibilityModel_->appendRow( marksItem ); 544 545 QStandardItem *matchesItem = new QStandardItem( tr( "Matches" ) ); 546 QPixmap matchesPixmap( 16, 10 ); 547 matchesPixmap.fill( Qt::red ); 548 matchesItem->setIcon( QIcon( matchesPixmap ) ); 549 matchesItem->setData( FilteredView::MatchesOnly ); 550 visibilityModel_->appendRow( matchesItem ); 551 552 QListView *visibilityView = new QListView( this ); 553 visibilityView->setMovement( QListView::Static ); 554 visibilityView->setMinimumWidth( 170 ); // Only needed with custom style-sheet 555 556 visibilityBox = new QComboBox(); 557 visibilityBox->setModel( visibilityModel_ ); 558 visibilityBox->setView( visibilityView ); 559 560 // Select "Marks and matches" by default (same default as the filtered view) 561 visibilityBox->setCurrentIndex( 0 ); 562 563 // TODO: Maybe there is some way to set the popup width to be 564 // sized-to-content (as it is when the stylesheet is not overriden) in the 565 // stylesheet as opposed to setting a hard min-width on the view above. 566 visibilityBox->setStyleSheet( " \ 567 QComboBox:on {\ 568 padding: 1px 2px 1px 6px;\ 569 width: 19px;\ 570 } \ 571 QComboBox:!on {\ 572 padding: 1px 2px 1px 7px;\ 573 width: 19px;\ 574 height: 16px;\ 575 border: 1px solid gray;\ 576 } \ 577 QComboBox::drop-down::down-arrow {\ 578 width: 0px;\ 579 border-width: 0px;\ 580 } \ 581 " ); 582 583 // Construct the Search Info line 584 searchInfoLine = new InfoLine(); 585 searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 586 searchInfoLine->setLineWidth( 1 ); 587 searchInfoLineDefaultPalette = searchInfoLine->palette(); 588 589 ignoreCaseCheck = new QCheckBox( "Ignore &case" ); 590 searchRefreshCheck = new QCheckBox( "Auto-&refresh" ); 591 592 // Construct the Search line 593 searchLabel = new QLabel(tr("&Text: ")); 594 searchLineEdit = new QComboBox; 595 searchLineEdit->setEditable( true ); 596 searchLineEdit->setCompleter( 0 ); 597 searchLineEdit->addItems( savedSearches_->recentSearches() ); 598 searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); 599 searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); 600 601 searchLabel->setBuddy( searchLineEdit ); 602 603 searchButton = new QToolButton(); 604 searchButton->setText( tr("&Search") ); 605 searchButton->setAutoRaise( true ); 606 607 stopButton = new QToolButton(); 608 stopButton->setIcon( QIcon(":/images/stop16.png") ); 609 stopButton->setAutoRaise( true ); 610 stopButton->setEnabled( false ); 611 612 QHBoxLayout* searchLineLayout = new QHBoxLayout; 613 searchLineLayout->addWidget(searchLabel); 614 searchLineLayout->addWidget(searchLineEdit); 615 searchLineLayout->addWidget(searchButton); 616 searchLineLayout->addWidget(stopButton); 617 searchLineLayout->setContentsMargins(6, 0, 6, 0); 618 stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 619 searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 620 621 QHBoxLayout* searchInfoLineLayout = new QHBoxLayout; 622 searchInfoLineLayout->addWidget( visibilityBox ); 623 searchInfoLineLayout->addWidget( searchInfoLine ); 624 searchInfoLineLayout->addWidget( ignoreCaseCheck ); 625 searchInfoLineLayout->addWidget( searchRefreshCheck ); 626 627 // Construct the bottom window 628 QVBoxLayout* bottomMainLayout = new QVBoxLayout; 629 bottomMainLayout->addLayout(searchLineLayout); 630 bottomMainLayout->addLayout(searchInfoLineLayout); 631 bottomMainLayout->addWidget(filteredView); 632 bottomMainLayout->setContentsMargins(2, 1, 2, 1); 633 bottomWindow->setLayout(bottomMainLayout); 634 635 addWidget( logMainView ); 636 addWidget( bottomWindow ); 637 638 // Default splitter position (usually overridden by the config file) 639 QList<int> splitterSizes; 640 splitterSizes += 400; 641 splitterSizes += 100; 642 setSizes( splitterSizes ); 643 644 // Default search checkboxes 645 auto config = Persistent<Configuration>( "settings" ); 646 searchRefreshCheck->setCheckState( config->isSearchAutoRefreshDefault() ? 647 Qt::Checked : Qt::Unchecked ); 648 // Manually call the handler as it is not called when changing the state programmatically 649 searchRefreshChangedHandler( searchRefreshCheck->checkState() ); 650 ignoreCaseCheck->setCheckState( config->isSearchIgnoreCaseDefault() ? 651 Qt::Checked : Qt::Unchecked ); 652 653 // Connect the signals 654 connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ), 655 searchButton, SIGNAL( clicked() )); 656 connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ), 657 this, SLOT( searchTextChangeHandler() )); 658 connect(searchButton, SIGNAL( clicked() ), 659 this, SLOT( startNewSearch() ) ); 660 connect(stopButton, SIGNAL( clicked() ), 661 this, SLOT( stopSearch() ) ); 662 663 connect(visibilityBox, SIGNAL( currentIndexChanged( int ) ), 664 this, SLOT( changeFilteredViewVisibility( int ) ) ); 665 666 connect(logMainView, SIGNAL( newSelection( int ) ), 667 logMainView, SLOT( update() ) ); 668 connect(filteredView, SIGNAL( newSelection( int ) ), 669 this, SLOT( jumpToMatchingLine( int ) ) ); 670 connect(filteredView, SIGNAL( newSelection( int ) ), 671 filteredView, SLOT( update() ) ); 672 connect(logMainView, SIGNAL( updateLineNumber( int ) ), 673 this, SLOT( updateLineNumberHandler( int ) ) ); 674 connect(logMainView, SIGNAL( markLine( qint64 ) ), 675 this, SLOT( markLineFromMain( qint64 ) ) ); 676 connect(filteredView, SIGNAL( markLine( qint64 ) ), 677 this, SLOT( markLineFromFiltered( qint64 ) ) ); 678 679 connect(logMainView, SIGNAL( addToSearch( const QString& ) ), 680 this, SLOT( addToSearch( const QString& ) ) ); 681 connect(filteredView, SIGNAL( addToSearch( const QString& ) ), 682 this, SLOT( addToSearch( const QString& ) ) ); 683 684 connect(filteredView, SIGNAL( mouseHoveredOverLine( qint64 ) ), 685 this, SLOT( mouseHoveredOverMatch( qint64 ) ) ); 686 connect(filteredView, SIGNAL( mouseLeftHoveringZone() ), 687 overviewWidget_, SLOT( removeHighlight() ) ); 688 689 // Follow option (up and down) 690 connect(this, SIGNAL( followSet( bool ) ), 691 logMainView, SLOT( followSet( bool ) ) ); 692 connect(this, SIGNAL( followSet( bool ) ), 693 filteredView, SLOT( followSet( bool ) ) ); 694 connect(logMainView, SIGNAL( followDisabled() ), 695 this, SIGNAL( followDisabled() ) ); 696 connect(filteredView, SIGNAL( followDisabled() ), 697 this, SIGNAL( followDisabled() ) ); 698 699 connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ), 700 this, SLOT( updateFilteredView( int, int ) ) ); 701 702 // Sent load file update to MainWindow (for status update) 703 connect( logData_, SIGNAL( loadingProgressed( int ) ), 704 this, SIGNAL( loadingProgressed( int ) ) ); 705 connect( logData_, SIGNAL( loadingFinished( LoadingStatus ) ), 706 this, SLOT( loadingFinishedHandler( LoadingStatus ) ) ); 707 connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ), 708 this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) ); 709 710 // Search auto-refresh 711 connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 712 this, SLOT( searchRefreshChangedHandler( int ) ) ); 713 714 // Advise the parent the checkboxes have been changed 715 // (for maintaining default config) 716 connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 717 this, SIGNAL( searchRefreshChanged( int ) ) ); 718 connect( ignoreCaseCheck, SIGNAL( stateChanged( int ) ), 719 this, SIGNAL( ignoreCaseChanged( int ) ) ); 720 } 721 722 // Create a new search using the text passed, replace the currently 723 // used one and destroy the old one. 724 void CrawlerWidget::replaceCurrentSearch( const QString& searchText ) 725 { 726 // Interrupt the search if it's ongoing 727 logFilteredData_->interruptSearch(); 728 729 // We have to wait for the last search update (100%) 730 // before clearing/restarting to avoid having remaining results. 731 732 // FIXME: this is a bit of a hack, we call processEvents 733 // for Qt to empty its event queue, including (hopefully) 734 // the search update event sent by logFilteredData_. It saves 735 // us the overhead of having proper sync. 736 QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); 737 738 if ( !searchText.isEmpty() ) { 739 // Determine the type of regexp depending on the config 740 QRegExp::PatternSyntax syntax; 741 static std::shared_ptr<Configuration> config = 742 Persistent<Configuration>( "settings" ); 743 switch ( config->mainRegexpType() ) { 744 case Wildcard: 745 syntax = QRegExp::Wildcard; 746 break; 747 case FixedString: 748 syntax = QRegExp::FixedString; 749 break; 750 default: 751 syntax = QRegExp::RegExp2; 752 break; 753 } 754 755 // Set the pattern case insensitive if needed 756 Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive; 757 if ( ignoreCaseCheck->checkState() == Qt::Checked ) 758 case_sensitivity = Qt::CaseInsensitive; 759 760 // Constructs the regexp 761 QRegExp regexp( searchText, case_sensitivity, syntax ); 762 763 if ( regexp.isValid() ) { 764 // Activate the stop button 765 stopButton->setEnabled( true ); 766 // Start a new asynchronous search 767 logFilteredData_->runSearch( regexp ); 768 // Accept auto-refresh of the search 769 searchState_.startSearch(); 770 } 771 else { 772 // The regexp is wrong 773 logFilteredData_->clearSearch(); 774 filteredView->updateData(); 775 searchState_.resetState(); 776 777 // Inform the user 778 QString errorMessage = tr("Error in expression: "); 779 errorMessage += regexp.errorString(); 780 searchInfoLine->setPalette( errorPalette ); 781 searchInfoLine->setText( errorMessage ); 782 } 783 } 784 else { 785 logFilteredData_->clearSearch(); 786 filteredView->updateData(); 787 searchState_.resetState(); 788 printSearchInfoMessage(); 789 } 790 // Connect the search to the top view 791 logMainView->useNewFiltering( logFilteredData_ ); 792 } 793 794 // Updates the content of the drop down list for the saved searches, 795 // called when the SavedSearch has been changed. 796 void CrawlerWidget::updateSearchCombo() 797 { 798 const QString text = searchLineEdit->lineEdit()->text(); 799 searchLineEdit->clear(); 800 searchLineEdit->addItems( savedSearches_->recentSearches() ); 801 // In case we had something that wasn't added to the list (blank...): 802 searchLineEdit->lineEdit()->setText( text ); 803 } 804 805 // Print the search info message. 806 void CrawlerWidget::printSearchInfoMessage( int nbMatches ) 807 { 808 QString text; 809 810 switch ( searchState_.getState() ) { 811 case SearchState::NoSearch: 812 // Blank text is fine 813 break; 814 case SearchState::Static: 815 text = tr("%1 match%2 found.").arg( nbMatches ) 816 .arg( nbMatches > 1 ? "es" : "" ); 817 break; 818 case SearchState::Autorefreshing: 819 text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches ) 820 .arg( nbMatches > 1 ? "es" : "" ); 821 break; 822 case SearchState::FileTruncated: 823 case SearchState::TruncatedAutorefreshing: 824 text = tr("File truncated on disk, previous search results are not valid anymore."); 825 break; 826 } 827 828 searchInfoLine->setPalette( searchInfoLineDefaultPalette ); 829 searchInfoLine->setText( text ); 830 } 831 832 // 833 // SearchState implementation 834 // 835 void CrawlerWidget::SearchState::resetState() 836 { 837 state_ = NoSearch; 838 } 839 840 void CrawlerWidget::SearchState::setAutorefresh( bool refresh ) 841 { 842 autoRefreshRequested_ = refresh; 843 844 if ( refresh ) { 845 if ( state_ == Static ) 846 state_ = Autorefreshing; 847 /* 848 else if ( state_ == FileTruncated ) 849 state_ = TruncatedAutorefreshing; 850 */ 851 } 852 else { 853 if ( state_ == Autorefreshing ) 854 state_ = Static; 855 else if ( state_ == TruncatedAutorefreshing ) 856 state_ = FileTruncated; 857 } 858 } 859 860 void CrawlerWidget::SearchState::truncateFile() 861 { 862 if ( state_ == Autorefreshing || state_ == TruncatedAutorefreshing ) { 863 state_ = TruncatedAutorefreshing; 864 } 865 else { 866 state_ = FileTruncated; 867 } 868 } 869 870 void CrawlerWidget::SearchState::changeExpression() 871 { 872 if ( state_ == Autorefreshing ) 873 state_ = Static; 874 } 875 876 void CrawlerWidget::SearchState::stopSearch() 877 { 878 if ( state_ == Autorefreshing ) 879 state_ = Static; 880 } 881 882 void CrawlerWidget::SearchState::startSearch() 883 { 884 if ( autoRefreshRequested_ ) 885 state_ = Autorefreshing; 886 else 887 state_ = Static; 888 } 889 890 /* 891 * CrawlerWidgetContext 892 */ 893 CrawlerWidgetContext::CrawlerWidgetContext( const char* string ) 894 { 895 QRegExp regex = QRegExp( "S(\\d+):(\\d+)" ); 896 897 if ( regex.indexIn( string ) > -1 ) { 898 sizes_ = { regex.cap(1).toInt(), regex.cap(2).toInt() }; 899 LOG(logDEBUG) << "sizes_: " << sizes_[0] << " " << sizes_[1]; 900 } 901 else { 902 LOG(logWARNING) << "Unrecognised view size: " << string; 903 904 // Default values; 905 sizes_ = { 100, 400 }; 906 } 907 908 QRegExp case_refresh_regex = QRegExp( "IC(\\d+):AR(\\d+)" ); 909 910 if ( case_refresh_regex.indexIn( string ) > -1 ) { 911 ignore_case_ = ( case_refresh_regex.cap(1).toInt() == 1 ); 912 auto_refresh_ = ( case_refresh_regex.cap(2).toInt() == 1 ); 913 914 LOG(logDEBUG) << "ignore_case_: " << ignore_case_ << " auto_refresh_: " 915 << auto_refresh_; 916 } 917 else { 918 LOG(logWARNING) << "Unrecognised case/refresh: " << string; 919 ignore_case_ = false; 920 auto_refresh_ = false; 921 } 922 } 923 924 std::string CrawlerWidgetContext::toString() const 925 { 926 char string[160]; 927 928 snprintf( string, sizeof string, "S%d:%d:IC%d:AR%d", 929 sizes_[0], sizes_[1], 930 ignore_case_, auto_refresh_ ); 931 932 return { string }; 933 } 934