1bb02e0acSNicolas Bonnefon /* 2bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon // This file implements the CrawlerWidget class. 21bb02e0acSNicolas Bonnefon // It is responsible for creating and managing the two views and all 22bb02e0acSNicolas Bonnefon // the UI elements. It implements the connection between the UI elements. 23*32e44cfdSNicolas Bonnefon // It also interacts with the sets of data (full and filtered). 24bb02e0acSNicolas Bonnefon 25bb02e0acSNicolas Bonnefon #include "log.h" 26bb02e0acSNicolas Bonnefon 27039481acSNicolas Bonnefon #include <cassert> 28039481acSNicolas Bonnefon 29bb02e0acSNicolas Bonnefon #include <Qt> 30bb02e0acSNicolas Bonnefon #include <QApplication> 31bb02e0acSNicolas Bonnefon #include <QFile> 32bb02e0acSNicolas Bonnefon #include <QLineEdit> 33bb02e0acSNicolas Bonnefon #include <QFileInfo> 34bb02e0acSNicolas Bonnefon #include <QKeyEvent> 35bb02e0acSNicolas Bonnefon #include <QStandardItemModel> 36bb02e0acSNicolas Bonnefon #include <QHeaderView> 37bb02e0acSNicolas Bonnefon #include <QListView> 38bb02e0acSNicolas Bonnefon 39bb02e0acSNicolas Bonnefon #include "crawlerwidget.h" 40bb02e0acSNicolas Bonnefon 41bb02e0acSNicolas Bonnefon #include "quickfindpattern.h" 42bb02e0acSNicolas Bonnefon #include "overview.h" 43bb02e0acSNicolas Bonnefon #include "infoline.h" 44bb02e0acSNicolas Bonnefon #include "savedsearches.h" 45bb02e0acSNicolas Bonnefon #include "quickfindwidget.h" 46bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 47bb02e0acSNicolas Bonnefon #include "configuration.h" 48bb02e0acSNicolas Bonnefon 49bb02e0acSNicolas Bonnefon // Palette for error signaling (yellow background) 50bb02e0acSNicolas Bonnefon const QPalette CrawlerWidget::errorPalette( QColor( "yellow" ) ); 51bb02e0acSNicolas Bonnefon 52039481acSNicolas Bonnefon // Constructor only does trivial construction. The real work is done once 53039481acSNicolas Bonnefon // the data is attached. 54bb02e0acSNicolas Bonnefon CrawlerWidget::CrawlerWidget(SavedSearches* searches, QWidget *parent) 55bb02e0acSNicolas Bonnefon : QSplitter(parent) 56bb02e0acSNicolas Bonnefon { 57039481acSNicolas Bonnefon logData_ = nullptr; 58039481acSNicolas Bonnefon logFilteredData_ = nullptr; 59039481acSNicolas Bonnefon 60039481acSNicolas Bonnefon savedSearches = searches; 61039481acSNicolas Bonnefon } 62039481acSNicolas Bonnefon 63039481acSNicolas Bonnefon // The top line is first one on the main display 64039481acSNicolas Bonnefon int CrawlerWidget::getTopLine() const 65039481acSNicolas Bonnefon { 66039481acSNicolas Bonnefon return logMainView->getTopLine(); 67039481acSNicolas Bonnefon } 68039481acSNicolas Bonnefon 69039481acSNicolas Bonnefon QString CrawlerWidget::getSelectedText() const 70039481acSNicolas Bonnefon { 71039481acSNicolas Bonnefon if ( filteredView->hasFocus() ) 72039481acSNicolas Bonnefon return filteredView->getSelection(); 73039481acSNicolas Bonnefon else 74039481acSNicolas Bonnefon return logMainView->getSelection(); 75039481acSNicolas Bonnefon } 76039481acSNicolas Bonnefon 77039481acSNicolas Bonnefon void CrawlerWidget::selectAll() 78039481acSNicolas Bonnefon { 79039481acSNicolas Bonnefon activeView()->selectAll(); 80039481acSNicolas Bonnefon } 81039481acSNicolas Bonnefon 82039481acSNicolas Bonnefon // Return a pointer to the view in which we should do the QuickFind 83039481acSNicolas Bonnefon SearchableWidgetInterface* CrawlerWidget::getActiveSearchable() const 84039481acSNicolas Bonnefon { 85039481acSNicolas Bonnefon QWidget* searchableWidget; 86039481acSNicolas Bonnefon 87039481acSNicolas Bonnefon // Search in the window that has focus, or the window where 'Find' was 88039481acSNicolas Bonnefon // called from, or the main window. 89039481acSNicolas Bonnefon if ( filteredView->hasFocus() || logMainView->hasFocus() ) 90039481acSNicolas Bonnefon searchableWidget = QApplication::focusWidget(); 91039481acSNicolas Bonnefon else 92039481acSNicolas Bonnefon searchableWidget = qfSavedFocus_; 93039481acSNicolas Bonnefon 94039481acSNicolas Bonnefon if ( AbstractLogView* view = qobject_cast<AbstractLogView*>( searchableWidget ) ) 95039481acSNicolas Bonnefon return view; 96039481acSNicolas Bonnefon else 97039481acSNicolas Bonnefon return logMainView; 98039481acSNicolas Bonnefon } 99039481acSNicolas Bonnefon 100*32e44cfdSNicolas Bonnefon void CrawlerWidget::reload() 101*32e44cfdSNicolas Bonnefon { 102*32e44cfdSNicolas Bonnefon searchState_.resetState(); 103*32e44cfdSNicolas Bonnefon logFilteredData_->clearSearch(); 104*32e44cfdSNicolas Bonnefon filteredView->updateData(); 105*32e44cfdSNicolas Bonnefon printSearchInfoMessage(); 106*32e44cfdSNicolas Bonnefon 107*32e44cfdSNicolas Bonnefon logData_->reload(); 108*32e44cfdSNicolas Bonnefon } 109*32e44cfdSNicolas Bonnefon 110039481acSNicolas Bonnefon // 111039481acSNicolas Bonnefon // Protected functions 112039481acSNicolas Bonnefon // 113039481acSNicolas Bonnefon void CrawlerWidget::doSetData( 114039481acSNicolas Bonnefon std::shared_ptr<LogData> log_data, 115039481acSNicolas Bonnefon std::shared_ptr<LogFilteredData> filtered_data ) 116039481acSNicolas Bonnefon { 117039481acSNicolas Bonnefon logData_ = log_data.get(); 118039481acSNicolas Bonnefon logFilteredData_ = filtered_data.get(); 119039481acSNicolas Bonnefon 120039481acSNicolas Bonnefon setup(); 121039481acSNicolas Bonnefon } 122039481acSNicolas Bonnefon 123039481acSNicolas Bonnefon // 124039481acSNicolas Bonnefon // Events handlers 125039481acSNicolas Bonnefon // 126039481acSNicolas Bonnefon 127039481acSNicolas Bonnefon void CrawlerWidget::keyPressEvent( QKeyEvent* keyEvent ) 128039481acSNicolas Bonnefon { 129039481acSNicolas Bonnefon LOG(logDEBUG4) << "keyPressEvent received"; 130039481acSNicolas Bonnefon 131039481acSNicolas Bonnefon switch ( (keyEvent->text())[0].toLatin1() ) { 132039481acSNicolas Bonnefon case '/': 133039481acSNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 134039481acSNicolas Bonnefon break; 135039481acSNicolas Bonnefon case '?': 136039481acSNicolas Bonnefon displayQuickFindBar( QuickFindMux::Backward ); 137039481acSNicolas Bonnefon break; 138039481acSNicolas Bonnefon default: 139039481acSNicolas Bonnefon keyEvent->ignore(); 140039481acSNicolas Bonnefon } 141039481acSNicolas Bonnefon 142039481acSNicolas Bonnefon if ( !keyEvent->isAccepted() ) 143039481acSNicolas Bonnefon QSplitter::keyPressEvent( keyEvent ); 144039481acSNicolas Bonnefon } 145039481acSNicolas Bonnefon 146039481acSNicolas Bonnefon // 147039481acSNicolas Bonnefon // Slots 148039481acSNicolas Bonnefon // 149039481acSNicolas Bonnefon 150039481acSNicolas Bonnefon void CrawlerWidget::startNewSearch() 151039481acSNicolas Bonnefon { 152039481acSNicolas Bonnefon // Record the search line in the recent list 153039481acSNicolas Bonnefon // (reload the list first in case another glogg changed it) 154039481acSNicolas Bonnefon GetPersistentInfo().retrieve( "savedSearches" ); 155039481acSNicolas Bonnefon savedSearches->addRecent( searchLineEdit->currentText() ); 156039481acSNicolas Bonnefon GetPersistentInfo().save( "savedSearches" ); 157039481acSNicolas Bonnefon 158039481acSNicolas Bonnefon // Update the SearchLine (history) 159039481acSNicolas Bonnefon updateSearchCombo(); 160039481acSNicolas Bonnefon // Call the private function to do the search 161039481acSNicolas Bonnefon replaceCurrentSearch( searchLineEdit->currentText() ); 162039481acSNicolas Bonnefon } 163039481acSNicolas Bonnefon 164039481acSNicolas Bonnefon void CrawlerWidget::stopSearch() 165039481acSNicolas Bonnefon { 166039481acSNicolas Bonnefon logFilteredData_->interruptSearch(); 167039481acSNicolas Bonnefon searchState_.stopSearch(); 168039481acSNicolas Bonnefon printSearchInfoMessage(); 169039481acSNicolas Bonnefon } 170039481acSNicolas Bonnefon 171039481acSNicolas Bonnefon // When receiving the 'newDataAvailable' signal from LogFilteredData 172039481acSNicolas Bonnefon void CrawlerWidget::updateFilteredView( int nbMatches, int progress ) 173039481acSNicolas Bonnefon { 174039481acSNicolas Bonnefon LOG(logDEBUG) << "updateFilteredView received."; 175039481acSNicolas Bonnefon 176039481acSNicolas Bonnefon if ( progress == 100 ) { 177039481acSNicolas Bonnefon // Searching done 178039481acSNicolas Bonnefon printSearchInfoMessage( nbMatches ); 179039481acSNicolas Bonnefon searchInfoLine->hideGauge(); 180039481acSNicolas Bonnefon // De-activate the stop button 181039481acSNicolas Bonnefon stopButton->setEnabled( false ); 182039481acSNicolas Bonnefon } 183039481acSNicolas Bonnefon else { 184039481acSNicolas Bonnefon // Search in progress 185039481acSNicolas Bonnefon // We ignore 0% and 100% to avoid a flash when the search is very short 186039481acSNicolas Bonnefon if ( progress > 0 ) { 187039481acSNicolas Bonnefon searchInfoLine->setText( 188039481acSNicolas Bonnefon tr("Search in progress (%1 %)... %2 match%3 found so far.") 189039481acSNicolas Bonnefon .arg( progress ) 190039481acSNicolas Bonnefon .arg( nbMatches ) 191039481acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ) ); 192039481acSNicolas Bonnefon searchInfoLine->displayGauge( progress ); 193039481acSNicolas Bonnefon } 194039481acSNicolas Bonnefon } 195039481acSNicolas Bonnefon 196039481acSNicolas Bonnefon // Recompute the content of the filtered window. 197039481acSNicolas Bonnefon filteredView->updateData(); 198039481acSNicolas Bonnefon 199039481acSNicolas Bonnefon // Update the match overview 200039481acSNicolas Bonnefon overview_->updateData( logData_->getNbLine() ); 201039481acSNicolas Bonnefon 202039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 203039481acSNicolas Bonnefon update(); 204039481acSNicolas Bonnefon } 205039481acSNicolas Bonnefon 206039481acSNicolas Bonnefon void CrawlerWidget::jumpToMatchingLine(int filteredLineNb) 207039481acSNicolas Bonnefon { 208039481acSNicolas Bonnefon int mainViewLine = logFilteredData_->getMatchingLineNumber(filteredLineNb); 209039481acSNicolas Bonnefon logMainView->selectAndDisplayLine(mainViewLine); // FIXME: should be done with a signal. 210039481acSNicolas Bonnefon } 211039481acSNicolas Bonnefon 212039481acSNicolas Bonnefon void CrawlerWidget::markLineFromMain( qint64 line ) 213039481acSNicolas Bonnefon { 214039481acSNicolas Bonnefon if ( logFilteredData_->isLineMarked( line ) ) 215039481acSNicolas Bonnefon logFilteredData_->deleteMark( line ); 216039481acSNicolas Bonnefon else 217039481acSNicolas Bonnefon logFilteredData_->addMark( line ); 218039481acSNicolas Bonnefon 219039481acSNicolas Bonnefon // Recompute the content of the filtered window. 220039481acSNicolas Bonnefon filteredView->updateData(); 221039481acSNicolas Bonnefon 222039481acSNicolas Bonnefon // Update the match overview 223039481acSNicolas Bonnefon overview_->updateData( logData_->getNbLine() ); 224039481acSNicolas Bonnefon 225039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 226039481acSNicolas Bonnefon update(); 227039481acSNicolas Bonnefon } 228039481acSNicolas Bonnefon 229039481acSNicolas Bonnefon void CrawlerWidget::markLineFromFiltered( qint64 line ) 230039481acSNicolas Bonnefon { 231039481acSNicolas Bonnefon qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line ); 232039481acSNicolas Bonnefon if ( logFilteredData_->filteredLineTypeByIndex( line ) 233039481acSNicolas Bonnefon == LogFilteredData::Mark ) 234039481acSNicolas Bonnefon logFilteredData_->deleteMark( line_in_file ); 235039481acSNicolas Bonnefon else 236039481acSNicolas Bonnefon logFilteredData_->addMark( line_in_file ); 237039481acSNicolas Bonnefon 238039481acSNicolas Bonnefon // Recompute the content of the filtered window. 239039481acSNicolas Bonnefon filteredView->updateData(); 240039481acSNicolas Bonnefon 241039481acSNicolas Bonnefon // Update the match overview 242039481acSNicolas Bonnefon overview_->updateData( logData_->getNbLine() ); 243039481acSNicolas Bonnefon 244039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 245039481acSNicolas Bonnefon update(); 246039481acSNicolas Bonnefon } 247039481acSNicolas Bonnefon 248039481acSNicolas Bonnefon void CrawlerWidget::applyConfiguration() 249039481acSNicolas Bonnefon { 250039481acSNicolas Bonnefon Configuration& config = Persistent<Configuration>( "settings" ); 251039481acSNicolas Bonnefon QFont font = config.mainFont(); 252039481acSNicolas Bonnefon 253039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::applyConfiguration"; 254039481acSNicolas Bonnefon 255039481acSNicolas Bonnefon // Whatever font we use, we should NOT use kerning 256039481acSNicolas Bonnefon font.setKerning( false ); 257039481acSNicolas Bonnefon font.setFixedPitch( true ); 258039481acSNicolas Bonnefon #if QT_VERSION > 0x040700 259039481acSNicolas Bonnefon // Necessary on systems doing subpixel positionning (e.g. Ubuntu 12.04) 260039481acSNicolas Bonnefon font.setStyleStrategy( QFont::ForceIntegerMetrics ); 261039481acSNicolas Bonnefon #endif 262039481acSNicolas Bonnefon logMainView->setFont(font); 263039481acSNicolas Bonnefon filteredView->setFont(font); 264039481acSNicolas Bonnefon 265039481acSNicolas Bonnefon logMainView->setLineNumbersVisible( config.mainLineNumbersVisible() ); 266039481acSNicolas Bonnefon filteredView->setLineNumbersVisible( config.filteredLineNumbersVisible() ); 267039481acSNicolas Bonnefon 268039481acSNicolas Bonnefon overview_->setVisible( config.isOverviewVisible() ); 269039481acSNicolas Bonnefon logMainView->refreshOverview(); 270039481acSNicolas Bonnefon 271039481acSNicolas Bonnefon logMainView->updateDisplaySize(); 272039481acSNicolas Bonnefon logMainView->update(); 273039481acSNicolas Bonnefon filteredView->updateDisplaySize(); 274039481acSNicolas Bonnefon filteredView->update(); 275039481acSNicolas Bonnefon 276039481acSNicolas Bonnefon // Update the SearchLine (history) 277039481acSNicolas Bonnefon updateSearchCombo(); 278039481acSNicolas Bonnefon } 279039481acSNicolas Bonnefon 280039481acSNicolas Bonnefon void CrawlerWidget::loadingFinishedHandler( bool success ) 281039481acSNicolas Bonnefon { 282039481acSNicolas Bonnefon // We need to refresh the main window because the view lines on the 283039481acSNicolas Bonnefon // overview have probably changed. 284039481acSNicolas Bonnefon overview_->updateData( logData_->getNbLine() ); 285039481acSNicolas Bonnefon 286039481acSNicolas Bonnefon // FIXME, handle topLine 287039481acSNicolas Bonnefon // logMainView->updateData( logData_, topLine ); 288039481acSNicolas Bonnefon logMainView->updateData(); 289039481acSNicolas Bonnefon 290039481acSNicolas Bonnefon // Shall we Forbid starting a search when loading in progress? 291039481acSNicolas Bonnefon // searchButton->setEnabled( false ); 292039481acSNicolas Bonnefon 293039481acSNicolas Bonnefon // searchButton->setEnabled( true ); 294039481acSNicolas Bonnefon 295039481acSNicolas Bonnefon // See if we need to auto-refresh the search 296039481acSNicolas Bonnefon if ( searchState_.isAutorefreshAllowed() ) { 297039481acSNicolas Bonnefon LOG(logDEBUG) << "Refreshing the search"; 298039481acSNicolas Bonnefon logFilteredData_->updateSearch(); 299039481acSNicolas Bonnefon } 300039481acSNicolas Bonnefon 301039481acSNicolas Bonnefon emit loadingFinished( success ); 302039481acSNicolas Bonnefon } 303039481acSNicolas Bonnefon 304039481acSNicolas Bonnefon void CrawlerWidget::fileChangedHandler( LogData::MonitoredFileStatus status ) 305039481acSNicolas Bonnefon { 306039481acSNicolas Bonnefon // Handle the case where the file has been truncated 307039481acSNicolas Bonnefon if ( status == LogData::Truncated ) { 308039481acSNicolas Bonnefon // Clear all marks (TODO offer the option to keep them) 309039481acSNicolas Bonnefon logFilteredData_->clearMarks(); 310039481acSNicolas Bonnefon if ( ! searchInfoLine->text().isEmpty() ) { 311039481acSNicolas Bonnefon // Invalidate the search 312039481acSNicolas Bonnefon logFilteredData_->clearSearch(); 313039481acSNicolas Bonnefon filteredView->updateData(); 314039481acSNicolas Bonnefon searchState_.truncateFile(); 315039481acSNicolas Bonnefon printSearchInfoMessage(); 316039481acSNicolas Bonnefon } 317039481acSNicolas Bonnefon } 318039481acSNicolas Bonnefon } 319039481acSNicolas Bonnefon 320039481acSNicolas Bonnefon void CrawlerWidget::displayQuickFindBar( QuickFindMux::QFDirection direction ) 321039481acSNicolas Bonnefon { 322039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::displayQuickFindBar"; 323039481acSNicolas Bonnefon 324039481acSNicolas Bonnefon // Remember who had the focus 325039481acSNicolas Bonnefon qfSavedFocus_ = QApplication::focusWidget(); 326039481acSNicolas Bonnefon 327039481acSNicolas Bonnefon quickFindMux_->setDirection( direction ); 328039481acSNicolas Bonnefon quickFindWidget_->userActivate(); 329039481acSNicolas Bonnefon } 330039481acSNicolas Bonnefon 331039481acSNicolas Bonnefon void CrawlerWidget::hideQuickFindBar() 332039481acSNicolas Bonnefon { 333039481acSNicolas Bonnefon // Restore the focus once the QFBar has been hidden 334039481acSNicolas Bonnefon qfSavedFocus_->setFocus(); 335039481acSNicolas Bonnefon } 336039481acSNicolas Bonnefon 337039481acSNicolas Bonnefon void CrawlerWidget::changeQFPattern( const QString& newPattern ) 338039481acSNicolas Bonnefon { 339039481acSNicolas Bonnefon quickFindWidget_->changeDisplayedPattern( newPattern ); 340039481acSNicolas Bonnefon } 341039481acSNicolas Bonnefon 342039481acSNicolas Bonnefon // Returns a pointer to the window in which the search should be done 343039481acSNicolas Bonnefon AbstractLogView* CrawlerWidget::activeView() const 344039481acSNicolas Bonnefon { 345039481acSNicolas Bonnefon QWidget* activeView; 346039481acSNicolas Bonnefon 347039481acSNicolas Bonnefon // Search in the window that has focus, or the window where 'Find' was 348039481acSNicolas Bonnefon // called from, or the main window. 349039481acSNicolas Bonnefon if ( filteredView->hasFocus() || logMainView->hasFocus() ) 350039481acSNicolas Bonnefon activeView = QApplication::focusWidget(); 351039481acSNicolas Bonnefon else 352039481acSNicolas Bonnefon activeView = qfSavedFocus_; 353039481acSNicolas Bonnefon 354039481acSNicolas Bonnefon if ( AbstractLogView* view = qobject_cast<AbstractLogView*>( activeView ) ) 355039481acSNicolas Bonnefon return view; 356039481acSNicolas Bonnefon else 357039481acSNicolas Bonnefon return logMainView; 358039481acSNicolas Bonnefon } 359039481acSNicolas Bonnefon 360039481acSNicolas Bonnefon void CrawlerWidget::searchForward() 361039481acSNicolas Bonnefon { 362039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::searchForward"; 363039481acSNicolas Bonnefon 364039481acSNicolas Bonnefon activeView()->searchForward(); 365039481acSNicolas Bonnefon } 366039481acSNicolas Bonnefon 367039481acSNicolas Bonnefon void CrawlerWidget::searchBackward() 368039481acSNicolas Bonnefon { 369039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::searchBackward"; 370039481acSNicolas Bonnefon 371039481acSNicolas Bonnefon activeView()->searchBackward(); 372039481acSNicolas Bonnefon } 373039481acSNicolas Bonnefon 374039481acSNicolas Bonnefon void CrawlerWidget::searchRefreshChangedHandler( int state ) 375039481acSNicolas Bonnefon { 376039481acSNicolas Bonnefon searchState_.setAutorefresh( state == Qt::Checked ); 377039481acSNicolas Bonnefon printSearchInfoMessage( logFilteredData_->getNbMatches() ); 378039481acSNicolas Bonnefon } 379039481acSNicolas Bonnefon 380039481acSNicolas Bonnefon void CrawlerWidget::searchTextChangeHandler() 381039481acSNicolas Bonnefon { 382039481acSNicolas Bonnefon // We suspend auto-refresh 383039481acSNicolas Bonnefon searchState_.changeExpression(); 384039481acSNicolas Bonnefon printSearchInfoMessage( logFilteredData_->getNbMatches() ); 385039481acSNicolas Bonnefon } 386039481acSNicolas Bonnefon 387039481acSNicolas Bonnefon void CrawlerWidget::changeFilteredViewVisibility( int index ) 388039481acSNicolas Bonnefon { 389039481acSNicolas Bonnefon QStandardItem* item = visibilityModel_->item( index ); 390039481acSNicolas Bonnefon FilteredView::Visibility visibility = 391039481acSNicolas Bonnefon static_cast< FilteredView::Visibility>( item->data().toInt() ); 392039481acSNicolas Bonnefon 393039481acSNicolas Bonnefon filteredView->setVisibility( visibility ); 394039481acSNicolas Bonnefon } 395039481acSNicolas Bonnefon 396039481acSNicolas Bonnefon void CrawlerWidget::addToSearch( const QString& string ) 397039481acSNicolas Bonnefon { 398039481acSNicolas Bonnefon QString text = searchLineEdit->currentText(); 399039481acSNicolas Bonnefon 400039481acSNicolas Bonnefon if ( text.isEmpty() ) 401039481acSNicolas Bonnefon text = string; 402039481acSNicolas Bonnefon else { 403039481acSNicolas Bonnefon // Escape the regexp chars from the string before adding it. 404039481acSNicolas Bonnefon text += ( '|' + QRegExp::escape( string ) ); 405039481acSNicolas Bonnefon } 406039481acSNicolas Bonnefon 407039481acSNicolas Bonnefon searchLineEdit->setEditText( text ); 408039481acSNicolas Bonnefon 409039481acSNicolas Bonnefon // Set the focus to lineEdit so that the user can press 'Return' immediately 410039481acSNicolas Bonnefon searchLineEdit->lineEdit()->setFocus(); 411039481acSNicolas Bonnefon } 412039481acSNicolas Bonnefon 413039481acSNicolas Bonnefon void CrawlerWidget::mouseHoveredOverMatch( qint64 line ) 414039481acSNicolas Bonnefon { 415039481acSNicolas Bonnefon qint64 line_in_mainview = logFilteredData_->getMatchingLineNumber( line ); 416039481acSNicolas Bonnefon 417039481acSNicolas Bonnefon overviewWidget_->highlightLine( line_in_mainview ); 418039481acSNicolas Bonnefon } 419039481acSNicolas Bonnefon 420039481acSNicolas Bonnefon // 421039481acSNicolas Bonnefon // Private functions 422039481acSNicolas Bonnefon // 423039481acSNicolas Bonnefon 424039481acSNicolas Bonnefon // Build the widget and connect all the signals, this must be done once 425039481acSNicolas Bonnefon // the data are attached. 426039481acSNicolas Bonnefon void CrawlerWidget::setup() 427039481acSNicolas Bonnefon { 428bb02e0acSNicolas Bonnefon setOrientation(Qt::Vertical); 429bb02e0acSNicolas Bonnefon 430039481acSNicolas Bonnefon assert( logData_ ); 431039481acSNicolas Bonnefon assert( logFilteredData_ ); 432bb02e0acSNicolas Bonnefon 433bb02e0acSNicolas Bonnefon // The matches overview 434bb02e0acSNicolas Bonnefon overview_ = new Overview(); 435bb02e0acSNicolas Bonnefon 436bb02e0acSNicolas Bonnefon // Initialise the QF Mux to send requests from the QFWidget 437bb02e0acSNicolas Bonnefon // to the right window 438bb02e0acSNicolas Bonnefon quickFindMux_ = new QuickFindMux( this ); 439bb02e0acSNicolas Bonnefon 440bb02e0acSNicolas Bonnefon // The views 441bb02e0acSNicolas Bonnefon bottomWindow = new QWidget; 442bb02e0acSNicolas Bonnefon overviewWidget_ = new OverviewWidget(); 443bb02e0acSNicolas Bonnefon logMainView = new LogMainView( 444bb02e0acSNicolas Bonnefon logData_, quickFindMux_->getPattern(), overview_, overviewWidget_ ); 445bb02e0acSNicolas Bonnefon filteredView = new FilteredView( 446bb02e0acSNicolas Bonnefon logFilteredData_, quickFindMux_->getPattern() ); 447bb02e0acSNicolas Bonnefon 448bb02e0acSNicolas Bonnefon overviewWidget_->setOverview( overview_ ); 449bb02e0acSNicolas Bonnefon overviewWidget_->setParent( logMainView ); 450bb02e0acSNicolas Bonnefon 451bb02e0acSNicolas Bonnefon quickFindMux_->registerSearchable( logMainView ); 452bb02e0acSNicolas Bonnefon quickFindMux_->registerSearchable( filteredView ); 453bb02e0acSNicolas Bonnefon 454bb02e0acSNicolas Bonnefon // Construct the visibility button 455bb02e0acSNicolas Bonnefon visibilityModel_ = new QStandardItemModel( this ); 456bb02e0acSNicolas Bonnefon 457bb02e0acSNicolas Bonnefon QStandardItem *marksAndMatchesItem = new QStandardItem( tr( "Marks and matches" ) ); 458bb02e0acSNicolas Bonnefon QPixmap marksAndMatchesPixmap( 16, 10 ); 459bb02e0acSNicolas Bonnefon marksAndMatchesPixmap.fill( Qt::gray ); 460bb02e0acSNicolas Bonnefon marksAndMatchesItem->setIcon( QIcon( marksAndMatchesPixmap ) ); 461bb02e0acSNicolas Bonnefon marksAndMatchesItem->setData( FilteredView::MarksAndMatches ); 462bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( marksAndMatchesItem ); 463bb02e0acSNicolas Bonnefon 464bb02e0acSNicolas Bonnefon QStandardItem *marksItem = new QStandardItem( tr( "Marks" ) ); 465bb02e0acSNicolas Bonnefon QPixmap marksPixmap( 16, 10 ); 466bb02e0acSNicolas Bonnefon marksPixmap.fill( Qt::blue ); 467bb02e0acSNicolas Bonnefon marksItem->setIcon( QIcon( marksPixmap ) ); 468bb02e0acSNicolas Bonnefon marksItem->setData( FilteredView::MarksOnly ); 469bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( marksItem ); 470bb02e0acSNicolas Bonnefon 471bb02e0acSNicolas Bonnefon QStandardItem *matchesItem = new QStandardItem( tr( "Matches" ) ); 472bb02e0acSNicolas Bonnefon QPixmap matchesPixmap( 16, 10 ); 473bb02e0acSNicolas Bonnefon matchesPixmap.fill( Qt::red ); 474bb02e0acSNicolas Bonnefon matchesItem->setIcon( QIcon( matchesPixmap ) ); 475bb02e0acSNicolas Bonnefon matchesItem->setData( FilteredView::MatchesOnly ); 476bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( matchesItem ); 477bb02e0acSNicolas Bonnefon 478bb02e0acSNicolas Bonnefon QListView *visibilityView = new QListView( this ); 479bb02e0acSNicolas Bonnefon visibilityView->setMovement( QListView::Static ); 480bb02e0acSNicolas Bonnefon visibilityView->setMinimumWidth( 170 ); // Only needed with custom style-sheet 481bb02e0acSNicolas Bonnefon 482bb02e0acSNicolas Bonnefon visibilityBox = new QComboBox(); 483bb02e0acSNicolas Bonnefon visibilityBox->setModel( visibilityModel_ ); 484bb02e0acSNicolas Bonnefon visibilityBox->setView( visibilityView ); 485bb02e0acSNicolas Bonnefon 486bb02e0acSNicolas Bonnefon // Select "Marks and matches" by default (same default as the filtered view) 487bb02e0acSNicolas Bonnefon visibilityBox->setCurrentIndex( 0 ); 488bb02e0acSNicolas Bonnefon 489bb02e0acSNicolas Bonnefon // TODO: Maybe there is some way to set the popup width to be 490bb02e0acSNicolas Bonnefon // sized-to-content (as it is when the stylesheet is not overriden) in the 491bb02e0acSNicolas Bonnefon // stylesheet as opposed to setting a hard min-width on the view above. 492bb02e0acSNicolas Bonnefon visibilityBox->setStyleSheet( " \ 493bb02e0acSNicolas Bonnefon QComboBox:on {\ 494bb02e0acSNicolas Bonnefon padding: 1px 2px 1px 6px;\ 495bb02e0acSNicolas Bonnefon width: 19px;\ 496bb02e0acSNicolas Bonnefon } \ 497bb02e0acSNicolas Bonnefon QComboBox:!on {\ 498bb02e0acSNicolas Bonnefon padding: 1px 2px 1px 7px;\ 499bb02e0acSNicolas Bonnefon width: 19px;\ 500bb02e0acSNicolas Bonnefon height: 16px;\ 501bb02e0acSNicolas Bonnefon border: 1px solid gray;\ 502bb02e0acSNicolas Bonnefon } \ 503bb02e0acSNicolas Bonnefon QComboBox::drop-down::down-arrow {\ 504bb02e0acSNicolas Bonnefon width: 0px;\ 505bb02e0acSNicolas Bonnefon border-width: 0px;\ 506bb02e0acSNicolas Bonnefon } \ 507bb02e0acSNicolas Bonnefon " ); 508bb02e0acSNicolas Bonnefon 509bb02e0acSNicolas Bonnefon // Construct the Search Info line 510bb02e0acSNicolas Bonnefon searchInfoLine = new InfoLine(); 511bb02e0acSNicolas Bonnefon searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 512bb02e0acSNicolas Bonnefon searchInfoLine->setLineWidth( 1 ); 513bb02e0acSNicolas Bonnefon searchInfoLineDefaultPalette = searchInfoLine->palette(); 514bb02e0acSNicolas Bonnefon 515bb02e0acSNicolas Bonnefon ignoreCaseCheck = new QCheckBox( "Ignore &case" ); 516bb02e0acSNicolas Bonnefon searchRefreshCheck = new QCheckBox( "Auto-&refresh" ); 517bb02e0acSNicolas Bonnefon 518bb02e0acSNicolas Bonnefon // Construct the Search line 519bb02e0acSNicolas Bonnefon searchLabel = new QLabel(tr("&Text: ")); 520bb02e0acSNicolas Bonnefon searchLineEdit = new QComboBox; 521bb02e0acSNicolas Bonnefon searchLineEdit->setEditable( true ); 522bb02e0acSNicolas Bonnefon searchLineEdit->setCompleter( 0 ); 523bb02e0acSNicolas Bonnefon searchLineEdit->addItems( savedSearches->recentSearches() ); 524bb02e0acSNicolas Bonnefon searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); 525bb02e0acSNicolas Bonnefon searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); 526bb02e0acSNicolas Bonnefon 527bb02e0acSNicolas Bonnefon searchLabel->setBuddy( searchLineEdit ); 528bb02e0acSNicolas Bonnefon 529bb02e0acSNicolas Bonnefon searchButton = new QToolButton(); 530bb02e0acSNicolas Bonnefon searchButton->setText( tr("&Search") ); 531bb02e0acSNicolas Bonnefon searchButton->setAutoRaise( true ); 532bb02e0acSNicolas Bonnefon 533bb02e0acSNicolas Bonnefon stopButton = new QToolButton(); 534bb02e0acSNicolas Bonnefon stopButton->setIcon( QIcon(":/images/stop16.png") ); 535bb02e0acSNicolas Bonnefon stopButton->setAutoRaise( true ); 536bb02e0acSNicolas Bonnefon stopButton->setEnabled( false ); 537bb02e0acSNicolas Bonnefon 538bb02e0acSNicolas Bonnefon // Construct the QuickFind bar 539bb02e0acSNicolas Bonnefon quickFindWidget_ = new QuickFindWidget(); 540bb02e0acSNicolas Bonnefon 541bb02e0acSNicolas Bonnefon QHBoxLayout* searchLineLayout = new QHBoxLayout; 542bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchLabel); 543bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchLineEdit); 544bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchButton); 545bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(stopButton); 546bb02e0acSNicolas Bonnefon searchLineLayout->setContentsMargins(6, 0, 6, 0); 547bb02e0acSNicolas Bonnefon stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 548bb02e0acSNicolas Bonnefon searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 549bb02e0acSNicolas Bonnefon 550bb02e0acSNicolas Bonnefon QHBoxLayout* searchInfoLineLayout = new QHBoxLayout; 551bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( visibilityBox ); 552bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( searchInfoLine ); 553bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( ignoreCaseCheck ); 554bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( searchRefreshCheck ); 555bb02e0acSNicolas Bonnefon 556bb02e0acSNicolas Bonnefon // Construct the bottom window 557bb02e0acSNicolas Bonnefon quickFindWidget_->hide(); 558bb02e0acSNicolas Bonnefon QVBoxLayout* bottomMainLayout = new QVBoxLayout; 559bb02e0acSNicolas Bonnefon bottomMainLayout->addLayout(searchLineLayout); 560bb02e0acSNicolas Bonnefon bottomMainLayout->addLayout(searchInfoLineLayout); 561bb02e0acSNicolas Bonnefon bottomMainLayout->addWidget(filteredView); 562bb02e0acSNicolas Bonnefon bottomMainLayout->addWidget(quickFindWidget_); 563bb02e0acSNicolas Bonnefon bottomMainLayout->setContentsMargins(2, 1, 2, 1); 564bb02e0acSNicolas Bonnefon bottomWindow->setLayout(bottomMainLayout); 565bb02e0acSNicolas Bonnefon 566bb02e0acSNicolas Bonnefon addWidget( logMainView ); 567bb02e0acSNicolas Bonnefon addWidget( bottomWindow ); 568bb02e0acSNicolas Bonnefon 569bb02e0acSNicolas Bonnefon // Default splitter position (usually overridden by the config file) 570bb02e0acSNicolas Bonnefon QList<int> splitterSizes; 571bb02e0acSNicolas Bonnefon splitterSizes += 400; 572bb02e0acSNicolas Bonnefon splitterSizes += 100; 573bb02e0acSNicolas Bonnefon setSizes( splitterSizes ); 574bb02e0acSNicolas Bonnefon 575bb02e0acSNicolas Bonnefon // Connect the signals 576bb02e0acSNicolas Bonnefon connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ), 577bb02e0acSNicolas Bonnefon searchButton, SIGNAL( clicked() )); 578bb02e0acSNicolas Bonnefon connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ), 579bb02e0acSNicolas Bonnefon this, SLOT( searchTextChangeHandler() )); 580bb02e0acSNicolas Bonnefon connect(searchButton, SIGNAL( clicked() ), 581bb02e0acSNicolas Bonnefon this, SLOT( startNewSearch() ) ); 582bb02e0acSNicolas Bonnefon connect(stopButton, SIGNAL( clicked() ), 583bb02e0acSNicolas Bonnefon this, SLOT( stopSearch() ) ); 584bb02e0acSNicolas Bonnefon 585bb02e0acSNicolas Bonnefon connect(visibilityBox, SIGNAL( currentIndexChanged( int ) ), 586bb02e0acSNicolas Bonnefon this, SLOT( changeFilteredViewVisibility( int ) ) ); 587bb02e0acSNicolas Bonnefon 588bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( newSelection( int ) ), 589bb02e0acSNicolas Bonnefon logMainView, SLOT( update() ) ); 590bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( newSelection( int ) ), 591bb02e0acSNicolas Bonnefon this, SLOT( jumpToMatchingLine( int ) ) ); 592bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( newSelection( int ) ), 593bb02e0acSNicolas Bonnefon filteredView, SLOT( update() ) ); 594bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( updateLineNumber( int ) ), 595bb02e0acSNicolas Bonnefon this, SIGNAL( updateLineNumber( int ) ) ); 596bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( markLine( qint64 ) ), 597bb02e0acSNicolas Bonnefon this, SLOT( markLineFromMain( qint64 ) ) ); 598bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( markLine( qint64 ) ), 599bb02e0acSNicolas Bonnefon this, SLOT( markLineFromFiltered( qint64 ) ) ); 600bb02e0acSNicolas Bonnefon 601bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( addToSearch( const QString& ) ), 602bb02e0acSNicolas Bonnefon this, SLOT( addToSearch( const QString& ) ) ); 603bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( addToSearch( const QString& ) ), 604bb02e0acSNicolas Bonnefon this, SLOT( addToSearch( const QString& ) ) ); 605bb02e0acSNicolas Bonnefon 606bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( mouseHoveredOverLine( qint64 ) ), 607bb02e0acSNicolas Bonnefon this, SLOT( mouseHoveredOverMatch( qint64 ) ) ); 608bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( mouseLeftHoveringZone() ), 609bb02e0acSNicolas Bonnefon overviewWidget_, SLOT( removeHighlight() ) ); 610bb02e0acSNicolas Bonnefon 611bb02e0acSNicolas Bonnefon // Follow option (up and down) 612bb02e0acSNicolas Bonnefon connect(this, SIGNAL( followSet( bool ) ), 613bb02e0acSNicolas Bonnefon logMainView, SLOT( followSet( bool ) ) ); 614bb02e0acSNicolas Bonnefon connect(this, SIGNAL( followSet( bool ) ), 615bb02e0acSNicolas Bonnefon filteredView, SLOT( followSet( bool ) ) ); 616bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( followDisabled() ), 617bb02e0acSNicolas Bonnefon this, SIGNAL( followDisabled() ) ); 618bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( followDisabled() ), 619bb02e0acSNicolas Bonnefon this, SIGNAL( followDisabled() ) ); 620bb02e0acSNicolas Bonnefon 621bb02e0acSNicolas Bonnefon connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ), 622bb02e0acSNicolas Bonnefon this, SLOT( updateFilteredView( int, int ) ) ); 623bb02e0acSNicolas Bonnefon 624bb02e0acSNicolas Bonnefon // QuickFind 625bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( close() ), 626bb02e0acSNicolas Bonnefon this, SLOT( hideQuickFindBar() ) ); 627bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 628bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 629bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 630bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 631bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( cancelSearch() ), 632bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( cancelSearch() ) ); 633bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( searchForward() ), 634bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( searchForward() ) ); 635bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( searchBackward() ), 636bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( searchBackward() ) ); 637bb02e0acSNicolas Bonnefon connect( quickFindWidget_, SIGNAL( searchNext() ), 638bb02e0acSNicolas Bonnefon quickFindMux_, SLOT( searchNext() ) ); 639bb02e0acSNicolas Bonnefon 640bb02e0acSNicolas Bonnefon // QuickFind changes coming from the views 641bb02e0acSNicolas Bonnefon connect( quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 642bb02e0acSNicolas Bonnefon this, SLOT( changeQFPattern( const QString& ) ) ); 643bb02e0acSNicolas Bonnefon connect( quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 644bb02e0acSNicolas Bonnefon quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 645bb02e0acSNicolas Bonnefon connect( quickFindMux_, SIGNAL( clearNotification() ), 646bb02e0acSNicolas Bonnefon quickFindWidget_, SLOT( clearNotification() ) ); 647bb02e0acSNicolas Bonnefon 648bb02e0acSNicolas Bonnefon // Sent load file update to MainWindow (for status update) 649bb02e0acSNicolas Bonnefon connect( logData_, SIGNAL( loadingProgressed( int ) ), 650bb02e0acSNicolas Bonnefon this, SIGNAL( loadingProgressed( int ) ) ); 651bb02e0acSNicolas Bonnefon connect( logData_, SIGNAL( loadingFinished( bool ) ), 652bb02e0acSNicolas Bonnefon this, SLOT( loadingFinishedHandler( bool ) ) ); 653bb02e0acSNicolas Bonnefon connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ), 654bb02e0acSNicolas Bonnefon this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) ); 655bb02e0acSNicolas Bonnefon 656bb02e0acSNicolas Bonnefon // Search auto-refresh 657bb02e0acSNicolas Bonnefon connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 658bb02e0acSNicolas Bonnefon this, SLOT( searchRefreshChangedHandler( int ) ) ); 659bb02e0acSNicolas Bonnefon } 660bb02e0acSNicolas Bonnefon 661bb02e0acSNicolas Bonnefon // Create a new search using the text passed, replace the currently 662bb02e0acSNicolas Bonnefon // used one and destroy the old one. 663bb02e0acSNicolas Bonnefon void CrawlerWidget::replaceCurrentSearch( const QString& searchText ) 664bb02e0acSNicolas Bonnefon { 665bb02e0acSNicolas Bonnefon // Interrupt the search if it's ongoing 666bb02e0acSNicolas Bonnefon logFilteredData_->interruptSearch(); 667bb02e0acSNicolas Bonnefon 668bb02e0acSNicolas Bonnefon // We have to wait for the last search update (100%) 669bb02e0acSNicolas Bonnefon // before clearing/restarting to avoid having remaining results. 670bb02e0acSNicolas Bonnefon 671bb02e0acSNicolas Bonnefon // FIXME: this is a bit of a hack, we call processEvents 672bb02e0acSNicolas Bonnefon // for Qt to empty its event queue, including (hopefully) 673bb02e0acSNicolas Bonnefon // the search update event sent by logFilteredData_. It saves 674bb02e0acSNicolas Bonnefon // us the overhead of having proper sync. 675bb02e0acSNicolas Bonnefon QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); 676bb02e0acSNicolas Bonnefon 677bb02e0acSNicolas Bonnefon if ( !searchText.isEmpty() ) { 678bb02e0acSNicolas Bonnefon // Determine the type of regexp depending on the config 679bb02e0acSNicolas Bonnefon QRegExp::PatternSyntax syntax; 680bb02e0acSNicolas Bonnefon static Configuration& config = Persistent<Configuration>( "settings" ); 681bb02e0acSNicolas Bonnefon switch ( config.mainRegexpType() ) { 682bb02e0acSNicolas Bonnefon case Wildcard: 683bb02e0acSNicolas Bonnefon syntax = QRegExp::Wildcard; 684bb02e0acSNicolas Bonnefon break; 685bb02e0acSNicolas Bonnefon case FixedString: 686bb02e0acSNicolas Bonnefon syntax = QRegExp::FixedString; 687bb02e0acSNicolas Bonnefon break; 688bb02e0acSNicolas Bonnefon default: 689bb02e0acSNicolas Bonnefon syntax = QRegExp::RegExp2; 690bb02e0acSNicolas Bonnefon break; 691bb02e0acSNicolas Bonnefon } 692bb02e0acSNicolas Bonnefon 693bb02e0acSNicolas Bonnefon // Set the pattern case insensitive if needed 694bb02e0acSNicolas Bonnefon Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive; 695bb02e0acSNicolas Bonnefon if ( ignoreCaseCheck->checkState() == Qt::Checked ) 696bb02e0acSNicolas Bonnefon case_sensitivity = Qt::CaseInsensitive; 697bb02e0acSNicolas Bonnefon 698bb02e0acSNicolas Bonnefon // Constructs the regexp 699bb02e0acSNicolas Bonnefon QRegExp regexp( searchText, case_sensitivity, syntax ); 700bb02e0acSNicolas Bonnefon 701bb02e0acSNicolas Bonnefon if ( regexp.isValid() ) { 702bb02e0acSNicolas Bonnefon // Activate the stop button 703bb02e0acSNicolas Bonnefon stopButton->setEnabled( true ); 704bb02e0acSNicolas Bonnefon // Start a new asynchronous search 705bb02e0acSNicolas Bonnefon logFilteredData_->runSearch( regexp ); 706bb02e0acSNicolas Bonnefon // Accept auto-refresh of the search 707bb02e0acSNicolas Bonnefon searchState_.startSearch(); 708bb02e0acSNicolas Bonnefon } 709bb02e0acSNicolas Bonnefon else { 710bb02e0acSNicolas Bonnefon // The regexp is wrong 711bb02e0acSNicolas Bonnefon logFilteredData_->clearSearch(); 712bb02e0acSNicolas Bonnefon filteredView->updateData(); 713bb02e0acSNicolas Bonnefon searchState_.resetState(); 714bb02e0acSNicolas Bonnefon 715bb02e0acSNicolas Bonnefon // Inform the user 716bb02e0acSNicolas Bonnefon QString errorMessage = tr("Error in expression: "); 717bb02e0acSNicolas Bonnefon errorMessage += regexp.errorString(); 718bb02e0acSNicolas Bonnefon searchInfoLine->setPalette( errorPalette ); 719bb02e0acSNicolas Bonnefon searchInfoLine->setText( errorMessage ); 720bb02e0acSNicolas Bonnefon } 721bb02e0acSNicolas Bonnefon } 722bb02e0acSNicolas Bonnefon else { 723bb02e0acSNicolas Bonnefon logFilteredData_->clearSearch(); 724bb02e0acSNicolas Bonnefon filteredView->updateData(); 725bb02e0acSNicolas Bonnefon searchState_.resetState(); 726bb02e0acSNicolas Bonnefon printSearchInfoMessage(); 727bb02e0acSNicolas Bonnefon } 728bb02e0acSNicolas Bonnefon // Connect the search to the top view 729bb02e0acSNicolas Bonnefon logMainView->useNewFiltering( logFilteredData_ ); 730bb02e0acSNicolas Bonnefon } 731bb02e0acSNicolas Bonnefon 732bb02e0acSNicolas Bonnefon // Updates the content of the drop down list for the saved searches, 733bb02e0acSNicolas Bonnefon // called when the SavedSearch has been changed. 734bb02e0acSNicolas Bonnefon void CrawlerWidget::updateSearchCombo() 735bb02e0acSNicolas Bonnefon { 736bb02e0acSNicolas Bonnefon const QString text = searchLineEdit->lineEdit()->text(); 737bb02e0acSNicolas Bonnefon searchLineEdit->clear(); 738bb02e0acSNicolas Bonnefon searchLineEdit->addItems( savedSearches->recentSearches() ); 739bb02e0acSNicolas Bonnefon // In case we had something that wasn't added to the list (blank...): 740bb02e0acSNicolas Bonnefon searchLineEdit->lineEdit()->setText( text ); 741bb02e0acSNicolas Bonnefon } 742bb02e0acSNicolas Bonnefon 743bb02e0acSNicolas Bonnefon // Print the search info message. 744bb02e0acSNicolas Bonnefon void CrawlerWidget::printSearchInfoMessage( int nbMatches ) 745bb02e0acSNicolas Bonnefon { 746bb02e0acSNicolas Bonnefon QString text; 747bb02e0acSNicolas Bonnefon 748bb02e0acSNicolas Bonnefon switch ( searchState_.getState() ) { 749bb02e0acSNicolas Bonnefon case SearchState::NoSearch: 750bb02e0acSNicolas Bonnefon // Blank text is fine 751bb02e0acSNicolas Bonnefon break; 752bb02e0acSNicolas Bonnefon case SearchState::Static: 753bb02e0acSNicolas Bonnefon text = tr("%1 match%2 found.").arg( nbMatches ) 754bb02e0acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ); 755bb02e0acSNicolas Bonnefon break; 756bb02e0acSNicolas Bonnefon case SearchState::Autorefreshing: 757bb02e0acSNicolas Bonnefon text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches ) 758bb02e0acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ); 759bb02e0acSNicolas Bonnefon break; 760bb02e0acSNicolas Bonnefon case SearchState::FileTruncated: 761bb02e0acSNicolas Bonnefon text = tr("File truncated on disk, previous search results are not valid anymore."); 762bb02e0acSNicolas Bonnefon break; 763bb02e0acSNicolas Bonnefon } 764bb02e0acSNicolas Bonnefon 765bb02e0acSNicolas Bonnefon searchInfoLine->setPalette( searchInfoLineDefaultPalette ); 766bb02e0acSNicolas Bonnefon searchInfoLine->setText( text ); 767bb02e0acSNicolas Bonnefon } 768bb02e0acSNicolas Bonnefon 769bb02e0acSNicolas Bonnefon // 770bb02e0acSNicolas Bonnefon // SearchState implementation 771bb02e0acSNicolas Bonnefon // 772bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::resetState() 773bb02e0acSNicolas Bonnefon { 774bb02e0acSNicolas Bonnefon state_ = NoSearch; 775bb02e0acSNicolas Bonnefon } 776bb02e0acSNicolas Bonnefon 777bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::setAutorefresh( bool refresh ) 778bb02e0acSNicolas Bonnefon { 779bb02e0acSNicolas Bonnefon autoRefreshRequested_ = refresh; 780bb02e0acSNicolas Bonnefon 781bb02e0acSNicolas Bonnefon if ( refresh ) { 782bb02e0acSNicolas Bonnefon if ( state_ == Static ) 783bb02e0acSNicolas Bonnefon state_ = Autorefreshing; 784bb02e0acSNicolas Bonnefon } 785bb02e0acSNicolas Bonnefon else { 786bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 787bb02e0acSNicolas Bonnefon state_ = Static; 788bb02e0acSNicolas Bonnefon } 789bb02e0acSNicolas Bonnefon } 790bb02e0acSNicolas Bonnefon 791bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::truncateFile() 792bb02e0acSNicolas Bonnefon { 793bb02e0acSNicolas Bonnefon state_ = FileTruncated; 794bb02e0acSNicolas Bonnefon } 795bb02e0acSNicolas Bonnefon 796bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::changeExpression() 797bb02e0acSNicolas Bonnefon { 798bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 799bb02e0acSNicolas Bonnefon state_ = Static; 800bb02e0acSNicolas Bonnefon } 801bb02e0acSNicolas Bonnefon 802bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::stopSearch() 803bb02e0acSNicolas Bonnefon { 804bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 805bb02e0acSNicolas Bonnefon state_ = Static; 806bb02e0acSNicolas Bonnefon } 807bb02e0acSNicolas Bonnefon 808bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::startSearch() 809bb02e0acSNicolas Bonnefon { 810bb02e0acSNicolas Bonnefon if ( autoRefreshRequested_ ) 811bb02e0acSNicolas Bonnefon state_ = Autorefreshing; 812bb02e0acSNicolas Bonnefon else 813bb02e0acSNicolas Bonnefon state_ = Static; 814bb02e0acSNicolas Bonnefon } 815