1bb02e0acSNicolas Bonnefon /* 2b423cd88SNicolas Bonnefon * Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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. 2332e44cfdSNicolas 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 52a44d09bcSNicolas Bonnefon // Implementation of the view context for the CrawlerWidget 53a44d09bcSNicolas Bonnefon class CrawlerWidgetContext : public ViewContextInterface { 54a44d09bcSNicolas Bonnefon public: 55a44d09bcSNicolas Bonnefon // Construct from the stored string representation 56a44d09bcSNicolas Bonnefon CrawlerWidgetContext( const char* string ); 57a44d09bcSNicolas Bonnefon // Construct from the value passsed 58f688be2eSNicolas Bonnefon CrawlerWidgetContext( QList<int> sizes, 59f688be2eSNicolas Bonnefon bool ignore_case, 60f688be2eSNicolas Bonnefon bool auto_refresh ) 61f688be2eSNicolas Bonnefon : sizes_( sizes ), 62f688be2eSNicolas Bonnefon ignore_case_( ignore_case ), 63f688be2eSNicolas Bonnefon auto_refresh_( auto_refresh ) {} 64a44d09bcSNicolas Bonnefon 65a44d09bcSNicolas Bonnefon // Implementation of the ViewContextInterface function 66a44d09bcSNicolas Bonnefon std::string toString() const; 67a44d09bcSNicolas Bonnefon 68a44d09bcSNicolas Bonnefon // Access the Qt sizes array for the QSplitter 69a44d09bcSNicolas Bonnefon QList<int> sizes() const { return sizes_; } 70a44d09bcSNicolas Bonnefon 71f688be2eSNicolas Bonnefon bool ignoreCase() const { return ignore_case_; } 72f688be2eSNicolas Bonnefon bool autoRefresh() const { return auto_refresh_; } 73f688be2eSNicolas Bonnefon 74a44d09bcSNicolas Bonnefon private: 75a44d09bcSNicolas Bonnefon QList<int> sizes_; 76f688be2eSNicolas Bonnefon 77f688be2eSNicolas Bonnefon bool ignore_case_; 78f688be2eSNicolas Bonnefon bool auto_refresh_; 79a44d09bcSNicolas Bonnefon }; 80a44d09bcSNicolas Bonnefon 81039481acSNicolas Bonnefon // Constructor only does trivial construction. The real work is done once 82039481acSNicolas Bonnefon // the data is attached. 831b5e406eSNicolas Bonnefon CrawlerWidget::CrawlerWidget( QWidget *parent ) 840f9fd9edSNicolas Bonnefon : QSplitter( parent ), overview_() 85bb02e0acSNicolas Bonnefon { 86039481acSNicolas Bonnefon logData_ = nullptr; 87039481acSNicolas Bonnefon logFilteredData_ = nullptr; 88039481acSNicolas Bonnefon 89b423cd88SNicolas Bonnefon quickFindPattern_ = nullptr; 901b5e406eSNicolas Bonnefon savedSearches_ = nullptr; 918570d8d2SNicolas Bonnefon qfSavedFocus_ = nullptr; 929cacd6a9SNicolas Bonnefon 9360864ff5SNicolas Bonnefon // Until we have received confirmation loading is finished, we 9460864ff5SNicolas Bonnefon // should consider we are loading something. 9560864ff5SNicolas Bonnefon loadingInProgress_ = true; 9660864ff5SNicolas Bonnefon 979cacd6a9SNicolas Bonnefon currentLineNumber_ = 0; 98039481acSNicolas Bonnefon } 99039481acSNicolas Bonnefon 100039481acSNicolas Bonnefon // The top line is first one on the main display 101039481acSNicolas Bonnefon int CrawlerWidget::getTopLine() const 102039481acSNicolas Bonnefon { 103039481acSNicolas Bonnefon return logMainView->getTopLine(); 104039481acSNicolas Bonnefon } 105039481acSNicolas Bonnefon 106039481acSNicolas Bonnefon QString CrawlerWidget::getSelectedText() const 107039481acSNicolas Bonnefon { 108039481acSNicolas Bonnefon if ( filteredView->hasFocus() ) 109039481acSNicolas Bonnefon return filteredView->getSelection(); 110039481acSNicolas Bonnefon else 111039481acSNicolas Bonnefon return logMainView->getSelection(); 112039481acSNicolas Bonnefon } 113039481acSNicolas Bonnefon 114039481acSNicolas Bonnefon void CrawlerWidget::selectAll() 115039481acSNicolas Bonnefon { 116039481acSNicolas Bonnefon activeView()->selectAll(); 117039481acSNicolas Bonnefon } 118039481acSNicolas Bonnefon 119039481acSNicolas Bonnefon // Return a pointer to the view in which we should do the QuickFind 120b423cd88SNicolas Bonnefon SearchableWidgetInterface* CrawlerWidget::doGetActiveSearchable() const 121039481acSNicolas Bonnefon { 122b423cd88SNicolas Bonnefon return activeView(); 123b423cd88SNicolas Bonnefon } 124039481acSNicolas Bonnefon 125b423cd88SNicolas Bonnefon // Return all the searchable widgets (views) 126b423cd88SNicolas Bonnefon std::vector<QObject*> CrawlerWidget::doGetAllSearchables() const 127b423cd88SNicolas Bonnefon { 128b423cd88SNicolas Bonnefon std::vector<QObject*> searchables = 129b423cd88SNicolas Bonnefon { logMainView, filteredView }; 130039481acSNicolas Bonnefon 131b423cd88SNicolas Bonnefon return searchables; 132039481acSNicolas Bonnefon } 133039481acSNicolas Bonnefon 1349cacd6a9SNicolas Bonnefon // Update the state of the parent 1359cacd6a9SNicolas Bonnefon void CrawlerWidget::doSendAllStateSignals() 1369cacd6a9SNicolas Bonnefon { 1379cacd6a9SNicolas Bonnefon emit updateLineNumber( currentLineNumber_ ); 13860864ff5SNicolas Bonnefon if ( !loadingInProgress_ ) 139812146a8SNicolas Bonnefon emit loadingFinished( LoadingStatus::Successful ); 1409cacd6a9SNicolas Bonnefon } 1419cacd6a9SNicolas Bonnefon 1427847299cSNicolas Bonnefon // 1437847299cSNicolas Bonnefon // Public slots 1447847299cSNicolas Bonnefon // 1457847299cSNicolas Bonnefon 1467847299cSNicolas Bonnefon void CrawlerWidget::stopLoading() 1477847299cSNicolas Bonnefon { 1487847299cSNicolas Bonnefon logFilteredData_->interruptSearch(); 1497847299cSNicolas Bonnefon logData_->interruptLoading(); 1507847299cSNicolas Bonnefon } 1517847299cSNicolas Bonnefon 15232e44cfdSNicolas Bonnefon void CrawlerWidget::reload() 15332e44cfdSNicolas Bonnefon { 15432e44cfdSNicolas Bonnefon searchState_.resetState(); 15532e44cfdSNicolas Bonnefon logFilteredData_->clearSearch(); 15632e44cfdSNicolas Bonnefon filteredView->updateData(); 15732e44cfdSNicolas Bonnefon printSearchInfoMessage(); 15832e44cfdSNicolas Bonnefon 15932e44cfdSNicolas Bonnefon logData_->reload(); 16032e44cfdSNicolas Bonnefon } 16132e44cfdSNicolas Bonnefon 162039481acSNicolas Bonnefon // 163039481acSNicolas Bonnefon // Protected functions 164039481acSNicolas Bonnefon // 165039481acSNicolas Bonnefon void CrawlerWidget::doSetData( 166039481acSNicolas Bonnefon std::shared_ptr<LogData> log_data, 167039481acSNicolas Bonnefon std::shared_ptr<LogFilteredData> filtered_data ) 168039481acSNicolas Bonnefon { 169039481acSNicolas Bonnefon logData_ = log_data.get(); 170039481acSNicolas Bonnefon logFilteredData_ = filtered_data.get(); 1711b5e406eSNicolas Bonnefon } 172039481acSNicolas Bonnefon 173b423cd88SNicolas Bonnefon void CrawlerWidget::doSetQuickFindPattern( 174b423cd88SNicolas Bonnefon std::shared_ptr<QuickFindPattern> qfp ) 175b423cd88SNicolas Bonnefon { 176b423cd88SNicolas Bonnefon quickFindPattern_ = qfp; 177b423cd88SNicolas Bonnefon } 178b423cd88SNicolas Bonnefon 1791b5e406eSNicolas Bonnefon void CrawlerWidget::doSetSavedSearches( 1801b5e406eSNicolas Bonnefon std::shared_ptr<SavedSearches> saved_searches ) 1811b5e406eSNicolas Bonnefon { 1821b5e406eSNicolas Bonnefon savedSearches_ = saved_searches; 1831b5e406eSNicolas Bonnefon 1841b5e406eSNicolas Bonnefon // We do setup now, assuming doSetData has been called before 1851b5e406eSNicolas Bonnefon // us, that's not great really... 186039481acSNicolas Bonnefon setup(); 187039481acSNicolas Bonnefon } 188039481acSNicolas Bonnefon 189a44d09bcSNicolas Bonnefon void CrawlerWidget::doSetViewContext( 190a44d09bcSNicolas Bonnefon const char* view_context ) 191a44d09bcSNicolas Bonnefon { 192a44d09bcSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::doSetViewContext: " << view_context; 193a44d09bcSNicolas Bonnefon 194a44d09bcSNicolas Bonnefon CrawlerWidgetContext context = { view_context }; 195a44d09bcSNicolas Bonnefon 196a44d09bcSNicolas Bonnefon setSizes( context.sizes() ); 197f688be2eSNicolas Bonnefon ignoreCaseCheck->setCheckState( context.ignoreCase() ? Qt::Checked : Qt::Unchecked ); 198240a4b5eSNicolas Bonnefon 199240a4b5eSNicolas Bonnefon auto auto_refresh_check_state = context.autoRefresh() ? Qt::Checked : Qt::Unchecked; 200240a4b5eSNicolas Bonnefon searchRefreshCheck->setCheckState( auto_refresh_check_state ); 201240a4b5eSNicolas Bonnefon // Manually call the handler as it is not called when changing the state programmatically 202240a4b5eSNicolas Bonnefon searchRefreshChangedHandler( auto_refresh_check_state ); 203a44d09bcSNicolas Bonnefon } 204a44d09bcSNicolas Bonnefon 205a44d09bcSNicolas Bonnefon std::shared_ptr<const ViewContextInterface> 206a44d09bcSNicolas Bonnefon CrawlerWidget::doGetViewContext() const 207a44d09bcSNicolas Bonnefon { 208f688be2eSNicolas Bonnefon auto context = std::make_shared<const CrawlerWidgetContext>( 209f688be2eSNicolas Bonnefon sizes(), 210f688be2eSNicolas Bonnefon ( ignoreCaseCheck->checkState() == Qt::Checked ), 211f688be2eSNicolas Bonnefon ( searchRefreshCheck->checkState() == Qt::Checked ) ); 212a44d09bcSNicolas Bonnefon 213a44d09bcSNicolas Bonnefon return static_cast<std::shared_ptr<const ViewContextInterface>>( context ); 214a44d09bcSNicolas Bonnefon } 215a44d09bcSNicolas Bonnefon 216039481acSNicolas Bonnefon // 217039481acSNicolas Bonnefon // Slots 218039481acSNicolas Bonnefon // 219039481acSNicolas Bonnefon 220039481acSNicolas Bonnefon void CrawlerWidget::startNewSearch() 221039481acSNicolas Bonnefon { 222039481acSNicolas Bonnefon // Record the search line in the recent list 223039481acSNicolas Bonnefon // (reload the list first in case another glogg changed it) 224039481acSNicolas Bonnefon GetPersistentInfo().retrieve( "savedSearches" ); 2251b5e406eSNicolas Bonnefon savedSearches_->addRecent( searchLineEdit->currentText() ); 226039481acSNicolas Bonnefon GetPersistentInfo().save( "savedSearches" ); 227039481acSNicolas Bonnefon 228039481acSNicolas Bonnefon // Update the SearchLine (history) 229039481acSNicolas Bonnefon updateSearchCombo(); 230039481acSNicolas Bonnefon // Call the private function to do the search 231039481acSNicolas Bonnefon replaceCurrentSearch( searchLineEdit->currentText() ); 232039481acSNicolas Bonnefon } 233039481acSNicolas Bonnefon 234039481acSNicolas Bonnefon void CrawlerWidget::stopSearch() 235039481acSNicolas Bonnefon { 236039481acSNicolas Bonnefon logFilteredData_->interruptSearch(); 237039481acSNicolas Bonnefon searchState_.stopSearch(); 238039481acSNicolas Bonnefon printSearchInfoMessage(); 239039481acSNicolas Bonnefon } 240039481acSNicolas Bonnefon 241039481acSNicolas Bonnefon // When receiving the 'newDataAvailable' signal from LogFilteredData 242039481acSNicolas Bonnefon void CrawlerWidget::updateFilteredView( int nbMatches, int progress ) 243039481acSNicolas Bonnefon { 244039481acSNicolas Bonnefon LOG(logDEBUG) << "updateFilteredView received."; 245039481acSNicolas Bonnefon 246039481acSNicolas Bonnefon if ( progress == 100 ) { 247039481acSNicolas Bonnefon // Searching done 248039481acSNicolas Bonnefon printSearchInfoMessage( nbMatches ); 249039481acSNicolas Bonnefon searchInfoLine->hideGauge(); 250039481acSNicolas Bonnefon // De-activate the stop button 251039481acSNicolas Bonnefon stopButton->setEnabled( false ); 252039481acSNicolas Bonnefon } 253039481acSNicolas Bonnefon else { 254039481acSNicolas Bonnefon // Search in progress 255039481acSNicolas Bonnefon // We ignore 0% and 100% to avoid a flash when the search is very short 256039481acSNicolas Bonnefon if ( progress > 0 ) { 257039481acSNicolas Bonnefon searchInfoLine->setText( 258039481acSNicolas Bonnefon tr("Search in progress (%1 %)... %2 match%3 found so far.") 259039481acSNicolas Bonnefon .arg( progress ) 260039481acSNicolas Bonnefon .arg( nbMatches ) 261039481acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ) ); 262039481acSNicolas Bonnefon searchInfoLine->displayGauge( progress ); 263039481acSNicolas Bonnefon } 264039481acSNicolas Bonnefon } 265039481acSNicolas Bonnefon 266039481acSNicolas Bonnefon // Recompute the content of the filtered window. 267039481acSNicolas Bonnefon filteredView->updateData(); 268039481acSNicolas Bonnefon 269039481acSNicolas Bonnefon // Update the match overview 2700f9fd9edSNicolas Bonnefon overview_.updateData( logData_->getNbLine() ); 271039481acSNicolas Bonnefon 272039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 273039481acSNicolas Bonnefon update(); 274039481acSNicolas Bonnefon } 275039481acSNicolas Bonnefon 276039481acSNicolas Bonnefon void CrawlerWidget::jumpToMatchingLine(int filteredLineNb) 277039481acSNicolas Bonnefon { 278039481acSNicolas Bonnefon int mainViewLine = logFilteredData_->getMatchingLineNumber(filteredLineNb); 279039481acSNicolas Bonnefon logMainView->selectAndDisplayLine(mainViewLine); // FIXME: should be done with a signal. 280039481acSNicolas Bonnefon } 281039481acSNicolas Bonnefon 2829cacd6a9SNicolas Bonnefon void CrawlerWidget::updateLineNumberHandler( int line ) 2839cacd6a9SNicolas Bonnefon { 2849cacd6a9SNicolas Bonnefon currentLineNumber_ = line; 2859cacd6a9SNicolas Bonnefon emit updateLineNumber( line ); 2869cacd6a9SNicolas Bonnefon } 2879cacd6a9SNicolas Bonnefon 288039481acSNicolas Bonnefon void CrawlerWidget::markLineFromMain( qint64 line ) 289039481acSNicolas Bonnefon { 290039481acSNicolas Bonnefon if ( logFilteredData_->isLineMarked( line ) ) 291039481acSNicolas Bonnefon logFilteredData_->deleteMark( line ); 292039481acSNicolas Bonnefon else 293039481acSNicolas Bonnefon logFilteredData_->addMark( line ); 294039481acSNicolas Bonnefon 295039481acSNicolas Bonnefon // Recompute the content of the filtered window. 296039481acSNicolas Bonnefon filteredView->updateData(); 297039481acSNicolas Bonnefon 298039481acSNicolas Bonnefon // Update the match overview 2990f9fd9edSNicolas Bonnefon overview_.updateData( logData_->getNbLine() ); 300039481acSNicolas Bonnefon 301039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 302039481acSNicolas Bonnefon update(); 303039481acSNicolas Bonnefon } 304039481acSNicolas Bonnefon 305039481acSNicolas Bonnefon void CrawlerWidget::markLineFromFiltered( qint64 line ) 306039481acSNicolas Bonnefon { 307039481acSNicolas Bonnefon qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line ); 308039481acSNicolas Bonnefon if ( logFilteredData_->filteredLineTypeByIndex( line ) 309039481acSNicolas Bonnefon == LogFilteredData::Mark ) 310039481acSNicolas Bonnefon logFilteredData_->deleteMark( line_in_file ); 311039481acSNicolas Bonnefon else 312039481acSNicolas Bonnefon logFilteredData_->addMark( line_in_file ); 313039481acSNicolas Bonnefon 314039481acSNicolas Bonnefon // Recompute the content of the filtered window. 315039481acSNicolas Bonnefon filteredView->updateData(); 316039481acSNicolas Bonnefon 317039481acSNicolas Bonnefon // Update the match overview 3180f9fd9edSNicolas Bonnefon overview_.updateData( logData_->getNbLine() ); 319039481acSNicolas Bonnefon 320039481acSNicolas Bonnefon // Also update the top window for the coloured bullets. 321039481acSNicolas Bonnefon update(); 322039481acSNicolas Bonnefon } 323039481acSNicolas Bonnefon 324039481acSNicolas Bonnefon void CrawlerWidget::applyConfiguration() 325039481acSNicolas Bonnefon { 32611582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 32711582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 32811582726SNicolas Bonnefon QFont font = config->mainFont(); 329039481acSNicolas Bonnefon 330039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::applyConfiguration"; 331039481acSNicolas Bonnefon 332039481acSNicolas Bonnefon // Whatever font we use, we should NOT use kerning 333039481acSNicolas Bonnefon font.setKerning( false ); 334039481acSNicolas Bonnefon font.setFixedPitch( true ); 335039481acSNicolas Bonnefon #if QT_VERSION > 0x040700 336039481acSNicolas Bonnefon // Necessary on systems doing subpixel positionning (e.g. Ubuntu 12.04) 337039481acSNicolas Bonnefon font.setStyleStrategy( QFont::ForceIntegerMetrics ); 338039481acSNicolas Bonnefon #endif 339039481acSNicolas Bonnefon logMainView->setFont(font); 340039481acSNicolas Bonnefon filteredView->setFont(font); 341039481acSNicolas Bonnefon 34211582726SNicolas Bonnefon logMainView->setLineNumbersVisible( config->mainLineNumbersVisible() ); 34311582726SNicolas Bonnefon filteredView->setLineNumbersVisible( config->filteredLineNumbersVisible() ); 344039481acSNicolas Bonnefon 34511582726SNicolas Bonnefon overview_.setVisible( config->isOverviewVisible() ); 346039481acSNicolas Bonnefon logMainView->refreshOverview(); 347039481acSNicolas Bonnefon 348039481acSNicolas Bonnefon logMainView->updateDisplaySize(); 349039481acSNicolas Bonnefon logMainView->update(); 350039481acSNicolas Bonnefon filteredView->updateDisplaySize(); 351039481acSNicolas Bonnefon filteredView->update(); 352039481acSNicolas Bonnefon 353*80bca0a3SNicolas Bonnefon // Polling interval 354*80bca0a3SNicolas Bonnefon logData_->setPollingInterval( 355*80bca0a3SNicolas Bonnefon config->pollingEnabled() ? config->pollIntervalMs() : 0 ); 356*80bca0a3SNicolas Bonnefon 357039481acSNicolas Bonnefon // Update the SearchLine (history) 358039481acSNicolas Bonnefon updateSearchCombo(); 359039481acSNicolas Bonnefon } 360039481acSNicolas Bonnefon 3618570d8d2SNicolas Bonnefon void CrawlerWidget::enteringQuickFind() 3628570d8d2SNicolas Bonnefon { 3638570d8d2SNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::enteringQuickFind"; 3648570d8d2SNicolas Bonnefon 3658570d8d2SNicolas Bonnefon // Remember who had the focus (only if it is one of our views) 3668570d8d2SNicolas Bonnefon QWidget* focus_widget = QApplication::focusWidget(); 3678570d8d2SNicolas Bonnefon 3688570d8d2SNicolas Bonnefon if ( ( focus_widget == logMainView ) || ( focus_widget == filteredView ) ) 3698570d8d2SNicolas Bonnefon qfSavedFocus_ = focus_widget; 3708570d8d2SNicolas Bonnefon else 3718570d8d2SNicolas Bonnefon qfSavedFocus_ = nullptr; 3728570d8d2SNicolas Bonnefon } 3738570d8d2SNicolas Bonnefon 3748570d8d2SNicolas Bonnefon void CrawlerWidget::exitingQuickFind() 3758570d8d2SNicolas Bonnefon { 3768570d8d2SNicolas Bonnefon // Restore the focus once the QFBar has been hidden 3778570d8d2SNicolas Bonnefon if ( qfSavedFocus_ ) 3788570d8d2SNicolas Bonnefon qfSavedFocus_->setFocus(); 3798570d8d2SNicolas Bonnefon } 3808570d8d2SNicolas Bonnefon 381812146a8SNicolas Bonnefon void CrawlerWidget::loadingFinishedHandler( LoadingStatus status ) 382039481acSNicolas Bonnefon { 38360864ff5SNicolas Bonnefon loadingInProgress_ = false; 38460864ff5SNicolas Bonnefon 385039481acSNicolas Bonnefon // We need to refresh the main window because the view lines on the 386039481acSNicolas Bonnefon // overview have probably changed. 3870f9fd9edSNicolas Bonnefon overview_.updateData( logData_->getNbLine() ); 388039481acSNicolas Bonnefon 389039481acSNicolas Bonnefon // FIXME, handle topLine 390039481acSNicolas Bonnefon // logMainView->updateData( logData_, topLine ); 391039481acSNicolas Bonnefon logMainView->updateData(); 392039481acSNicolas Bonnefon 393039481acSNicolas Bonnefon // Shall we Forbid starting a search when loading in progress? 394039481acSNicolas Bonnefon // searchButton->setEnabled( false ); 395039481acSNicolas Bonnefon 396039481acSNicolas Bonnefon // searchButton->setEnabled( true ); 397039481acSNicolas Bonnefon 398039481acSNicolas Bonnefon // See if we need to auto-refresh the search 399039481acSNicolas Bonnefon if ( searchState_.isAutorefreshAllowed() ) { 40059d4e393SNicolas Bonnefon if ( searchState_.isFileTruncated() ) 40159d4e393SNicolas Bonnefon // We need to restart the search 40259d4e393SNicolas Bonnefon replaceCurrentSearch( searchLineEdit->currentText() ); 40359d4e393SNicolas Bonnefon else 404039481acSNicolas Bonnefon logFilteredData_->updateSearch(); 405039481acSNicolas Bonnefon } 406039481acSNicolas Bonnefon 407812146a8SNicolas Bonnefon emit loadingFinished( status ); 408039481acSNicolas Bonnefon } 409039481acSNicolas Bonnefon 410039481acSNicolas Bonnefon void CrawlerWidget::fileChangedHandler( LogData::MonitoredFileStatus status ) 411039481acSNicolas Bonnefon { 412039481acSNicolas Bonnefon // Handle the case where the file has been truncated 413039481acSNicolas Bonnefon if ( status == LogData::Truncated ) { 414039481acSNicolas Bonnefon // Clear all marks (TODO offer the option to keep them) 415039481acSNicolas Bonnefon logFilteredData_->clearMarks(); 416039481acSNicolas Bonnefon if ( ! searchInfoLine->text().isEmpty() ) { 417039481acSNicolas Bonnefon // Invalidate the search 418039481acSNicolas Bonnefon logFilteredData_->clearSearch(); 419039481acSNicolas Bonnefon filteredView->updateData(); 420039481acSNicolas Bonnefon searchState_.truncateFile(); 421039481acSNicolas Bonnefon printSearchInfoMessage(); 422039481acSNicolas Bonnefon } 423039481acSNicolas Bonnefon } 424039481acSNicolas Bonnefon } 425039481acSNicolas Bonnefon 426039481acSNicolas Bonnefon // Returns a pointer to the window in which the search should be done 427039481acSNicolas Bonnefon AbstractLogView* CrawlerWidget::activeView() const 428039481acSNicolas Bonnefon { 429039481acSNicolas Bonnefon QWidget* activeView; 430039481acSNicolas Bonnefon 431039481acSNicolas Bonnefon // Search in the window that has focus, or the window where 'Find' was 432039481acSNicolas Bonnefon // called from, or the main window. 433039481acSNicolas Bonnefon if ( filteredView->hasFocus() || logMainView->hasFocus() ) 434039481acSNicolas Bonnefon activeView = QApplication::focusWidget(); 435039481acSNicolas Bonnefon else 4368570d8d2SNicolas Bonnefon activeView = qfSavedFocus_; 437039481acSNicolas Bonnefon 438b423cd88SNicolas Bonnefon if ( activeView ) { 439b423cd88SNicolas Bonnefon AbstractLogView* view = qobject_cast<AbstractLogView*>( activeView ); 440039481acSNicolas Bonnefon return view; 441b423cd88SNicolas Bonnefon } 442b423cd88SNicolas Bonnefon else { 443b423cd88SNicolas Bonnefon LOG(logWARNING) << "No active view, defaulting to logMainView"; 444039481acSNicolas Bonnefon return logMainView; 445039481acSNicolas Bonnefon } 446b423cd88SNicolas Bonnefon } 447039481acSNicolas Bonnefon 448039481acSNicolas Bonnefon void CrawlerWidget::searchForward() 449039481acSNicolas Bonnefon { 450039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::searchForward"; 451039481acSNicolas Bonnefon 452039481acSNicolas Bonnefon activeView()->searchForward(); 453039481acSNicolas Bonnefon } 454039481acSNicolas Bonnefon 455039481acSNicolas Bonnefon void CrawlerWidget::searchBackward() 456039481acSNicolas Bonnefon { 457039481acSNicolas Bonnefon LOG(logDEBUG) << "CrawlerWidget::searchBackward"; 458039481acSNicolas Bonnefon 459039481acSNicolas Bonnefon activeView()->searchBackward(); 460039481acSNicolas Bonnefon } 461039481acSNicolas Bonnefon 462039481acSNicolas Bonnefon void CrawlerWidget::searchRefreshChangedHandler( int state ) 463039481acSNicolas Bonnefon { 464039481acSNicolas Bonnefon searchState_.setAutorefresh( state == Qt::Checked ); 465039481acSNicolas Bonnefon printSearchInfoMessage( logFilteredData_->getNbMatches() ); 466039481acSNicolas Bonnefon } 467039481acSNicolas Bonnefon 468039481acSNicolas Bonnefon void CrawlerWidget::searchTextChangeHandler() 469039481acSNicolas Bonnefon { 470039481acSNicolas Bonnefon // We suspend auto-refresh 471039481acSNicolas Bonnefon searchState_.changeExpression(); 472039481acSNicolas Bonnefon printSearchInfoMessage( logFilteredData_->getNbMatches() ); 473039481acSNicolas Bonnefon } 474039481acSNicolas Bonnefon 475039481acSNicolas Bonnefon void CrawlerWidget::changeFilteredViewVisibility( int index ) 476039481acSNicolas Bonnefon { 477039481acSNicolas Bonnefon QStandardItem* item = visibilityModel_->item( index ); 478039481acSNicolas Bonnefon FilteredView::Visibility visibility = 479039481acSNicolas Bonnefon static_cast< FilteredView::Visibility>( item->data().toInt() ); 480039481acSNicolas Bonnefon 481039481acSNicolas Bonnefon filteredView->setVisibility( visibility ); 482039481acSNicolas Bonnefon } 483039481acSNicolas Bonnefon 484039481acSNicolas Bonnefon void CrawlerWidget::addToSearch( const QString& string ) 485039481acSNicolas Bonnefon { 486039481acSNicolas Bonnefon QString text = searchLineEdit->currentText(); 487039481acSNicolas Bonnefon 488039481acSNicolas Bonnefon if ( text.isEmpty() ) 489039481acSNicolas Bonnefon text = string; 490039481acSNicolas Bonnefon else { 491039481acSNicolas Bonnefon // Escape the regexp chars from the string before adding it. 492039481acSNicolas Bonnefon text += ( '|' + QRegExp::escape( string ) ); 493039481acSNicolas Bonnefon } 494039481acSNicolas Bonnefon 495039481acSNicolas Bonnefon searchLineEdit->setEditText( text ); 496039481acSNicolas Bonnefon 497039481acSNicolas Bonnefon // Set the focus to lineEdit so that the user can press 'Return' immediately 498039481acSNicolas Bonnefon searchLineEdit->lineEdit()->setFocus(); 499039481acSNicolas Bonnefon } 500039481acSNicolas Bonnefon 501039481acSNicolas Bonnefon void CrawlerWidget::mouseHoveredOverMatch( qint64 line ) 502039481acSNicolas Bonnefon { 503039481acSNicolas Bonnefon qint64 line_in_mainview = logFilteredData_->getMatchingLineNumber( line ); 504039481acSNicolas Bonnefon 505039481acSNicolas Bonnefon overviewWidget_->highlightLine( line_in_mainview ); 506039481acSNicolas Bonnefon } 507039481acSNicolas Bonnefon 508039481acSNicolas Bonnefon // 509039481acSNicolas Bonnefon // Private functions 510039481acSNicolas Bonnefon // 511039481acSNicolas Bonnefon 512039481acSNicolas Bonnefon // Build the widget and connect all the signals, this must be done once 513039481acSNicolas Bonnefon // the data are attached. 514039481acSNicolas Bonnefon void CrawlerWidget::setup() 515039481acSNicolas Bonnefon { 516bb02e0acSNicolas Bonnefon setOrientation(Qt::Vertical); 517bb02e0acSNicolas Bonnefon 518039481acSNicolas Bonnefon assert( logData_ ); 519039481acSNicolas Bonnefon assert( logFilteredData_ ); 520bb02e0acSNicolas Bonnefon 521bb02e0acSNicolas Bonnefon // The views 522bb02e0acSNicolas Bonnefon bottomWindow = new QWidget; 523bb02e0acSNicolas Bonnefon overviewWidget_ = new OverviewWidget(); 524bb02e0acSNicolas Bonnefon logMainView = new LogMainView( 5250f9fd9edSNicolas Bonnefon logData_, quickFindPattern_.get(), &overview_, overviewWidget_ ); 526bb02e0acSNicolas Bonnefon filteredView = new FilteredView( 527b423cd88SNicolas Bonnefon logFilteredData_, quickFindPattern_.get() ); 528bb02e0acSNicolas Bonnefon 5290f9fd9edSNicolas Bonnefon overviewWidget_->setOverview( &overview_ ); 530bb02e0acSNicolas Bonnefon overviewWidget_->setParent( logMainView ); 531bb02e0acSNicolas Bonnefon 53258ab9c53SNicolas Bonnefon // Connect the search to the top view 53358ab9c53SNicolas Bonnefon logMainView->useNewFiltering( logFilteredData_ ); 53458ab9c53SNicolas Bonnefon 535bb02e0acSNicolas Bonnefon // Construct the visibility button 536bb02e0acSNicolas Bonnefon visibilityModel_ = new QStandardItemModel( this ); 537bb02e0acSNicolas Bonnefon 538bb02e0acSNicolas Bonnefon QStandardItem *marksAndMatchesItem = new QStandardItem( tr( "Marks and matches" ) ); 539bb02e0acSNicolas Bonnefon QPixmap marksAndMatchesPixmap( 16, 10 ); 540bb02e0acSNicolas Bonnefon marksAndMatchesPixmap.fill( Qt::gray ); 541bb02e0acSNicolas Bonnefon marksAndMatchesItem->setIcon( QIcon( marksAndMatchesPixmap ) ); 542bb02e0acSNicolas Bonnefon marksAndMatchesItem->setData( FilteredView::MarksAndMatches ); 543bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( marksAndMatchesItem ); 544bb02e0acSNicolas Bonnefon 545bb02e0acSNicolas Bonnefon QStandardItem *marksItem = new QStandardItem( tr( "Marks" ) ); 546bb02e0acSNicolas Bonnefon QPixmap marksPixmap( 16, 10 ); 547bb02e0acSNicolas Bonnefon marksPixmap.fill( Qt::blue ); 548bb02e0acSNicolas Bonnefon marksItem->setIcon( QIcon( marksPixmap ) ); 549bb02e0acSNicolas Bonnefon marksItem->setData( FilteredView::MarksOnly ); 550bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( marksItem ); 551bb02e0acSNicolas Bonnefon 552bb02e0acSNicolas Bonnefon QStandardItem *matchesItem = new QStandardItem( tr( "Matches" ) ); 553bb02e0acSNicolas Bonnefon QPixmap matchesPixmap( 16, 10 ); 554bb02e0acSNicolas Bonnefon matchesPixmap.fill( Qt::red ); 555bb02e0acSNicolas Bonnefon matchesItem->setIcon( QIcon( matchesPixmap ) ); 556bb02e0acSNicolas Bonnefon matchesItem->setData( FilteredView::MatchesOnly ); 557bb02e0acSNicolas Bonnefon visibilityModel_->appendRow( matchesItem ); 558bb02e0acSNicolas Bonnefon 559bb02e0acSNicolas Bonnefon QListView *visibilityView = new QListView( this ); 560bb02e0acSNicolas Bonnefon visibilityView->setMovement( QListView::Static ); 561bb02e0acSNicolas Bonnefon visibilityView->setMinimumWidth( 170 ); // Only needed with custom style-sheet 562bb02e0acSNicolas Bonnefon 563bb02e0acSNicolas Bonnefon visibilityBox = new QComboBox(); 564bb02e0acSNicolas Bonnefon visibilityBox->setModel( visibilityModel_ ); 565bb02e0acSNicolas Bonnefon visibilityBox->setView( visibilityView ); 566bb02e0acSNicolas Bonnefon 567bb02e0acSNicolas Bonnefon // Select "Marks and matches" by default (same default as the filtered view) 568bb02e0acSNicolas Bonnefon visibilityBox->setCurrentIndex( 0 ); 569bb02e0acSNicolas Bonnefon 570bb02e0acSNicolas Bonnefon // TODO: Maybe there is some way to set the popup width to be 571bb02e0acSNicolas Bonnefon // sized-to-content (as it is when the stylesheet is not overriden) in the 572bb02e0acSNicolas Bonnefon // stylesheet as opposed to setting a hard min-width on the view above. 573bb02e0acSNicolas Bonnefon visibilityBox->setStyleSheet( " \ 574bb02e0acSNicolas Bonnefon QComboBox:on {\ 575bb02e0acSNicolas Bonnefon padding: 1px 2px 1px 6px;\ 576bb02e0acSNicolas Bonnefon width: 19px;\ 577bb02e0acSNicolas Bonnefon } \ 578bb02e0acSNicolas Bonnefon QComboBox:!on {\ 579bb02e0acSNicolas Bonnefon padding: 1px 2px 1px 7px;\ 580bb02e0acSNicolas Bonnefon width: 19px;\ 581bb02e0acSNicolas Bonnefon height: 16px;\ 582bb02e0acSNicolas Bonnefon border: 1px solid gray;\ 583bb02e0acSNicolas Bonnefon } \ 584bb02e0acSNicolas Bonnefon QComboBox::drop-down::down-arrow {\ 585bb02e0acSNicolas Bonnefon width: 0px;\ 586bb02e0acSNicolas Bonnefon border-width: 0px;\ 587bb02e0acSNicolas Bonnefon } \ 588bb02e0acSNicolas Bonnefon " ); 589bb02e0acSNicolas Bonnefon 590bb02e0acSNicolas Bonnefon // Construct the Search Info line 591bb02e0acSNicolas Bonnefon searchInfoLine = new InfoLine(); 592bb02e0acSNicolas Bonnefon searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 593bb02e0acSNicolas Bonnefon searchInfoLine->setLineWidth( 1 ); 594bb02e0acSNicolas Bonnefon searchInfoLineDefaultPalette = searchInfoLine->palette(); 595bb02e0acSNicolas Bonnefon 596bb02e0acSNicolas Bonnefon ignoreCaseCheck = new QCheckBox( "Ignore &case" ); 597bb02e0acSNicolas Bonnefon searchRefreshCheck = new QCheckBox( "Auto-&refresh" ); 598bb02e0acSNicolas Bonnefon 599bb02e0acSNicolas Bonnefon // Construct the Search line 600bb02e0acSNicolas Bonnefon searchLabel = new QLabel(tr("&Text: ")); 601bb02e0acSNicolas Bonnefon searchLineEdit = new QComboBox; 602bb02e0acSNicolas Bonnefon searchLineEdit->setEditable( true ); 603bb02e0acSNicolas Bonnefon searchLineEdit->setCompleter( 0 ); 6041b5e406eSNicolas Bonnefon searchLineEdit->addItems( savedSearches_->recentSearches() ); 605bb02e0acSNicolas Bonnefon searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); 606bb02e0acSNicolas Bonnefon searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); 607bb02e0acSNicolas Bonnefon 608bb02e0acSNicolas Bonnefon searchLabel->setBuddy( searchLineEdit ); 609bb02e0acSNicolas Bonnefon 610bb02e0acSNicolas Bonnefon searchButton = new QToolButton(); 611bb02e0acSNicolas Bonnefon searchButton->setText( tr("&Search") ); 612bb02e0acSNicolas Bonnefon searchButton->setAutoRaise( true ); 613bb02e0acSNicolas Bonnefon 614bb02e0acSNicolas Bonnefon stopButton = new QToolButton(); 615bb02e0acSNicolas Bonnefon stopButton->setIcon( QIcon(":/images/stop16.png") ); 616bb02e0acSNicolas Bonnefon stopButton->setAutoRaise( true ); 617bb02e0acSNicolas Bonnefon stopButton->setEnabled( false ); 618bb02e0acSNicolas Bonnefon 619bb02e0acSNicolas Bonnefon QHBoxLayout* searchLineLayout = new QHBoxLayout; 620bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchLabel); 621bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchLineEdit); 622bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(searchButton); 623bb02e0acSNicolas Bonnefon searchLineLayout->addWidget(stopButton); 624bb02e0acSNicolas Bonnefon searchLineLayout->setContentsMargins(6, 0, 6, 0); 625bb02e0acSNicolas Bonnefon stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 626bb02e0acSNicolas Bonnefon searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 627bb02e0acSNicolas Bonnefon 628bb02e0acSNicolas Bonnefon QHBoxLayout* searchInfoLineLayout = new QHBoxLayout; 629bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( visibilityBox ); 630bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( searchInfoLine ); 631bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( ignoreCaseCheck ); 632bb02e0acSNicolas Bonnefon searchInfoLineLayout->addWidget( searchRefreshCheck ); 633bb02e0acSNicolas Bonnefon 634bb02e0acSNicolas Bonnefon // Construct the bottom window 635bb02e0acSNicolas Bonnefon QVBoxLayout* bottomMainLayout = new QVBoxLayout; 636bb02e0acSNicolas Bonnefon bottomMainLayout->addLayout(searchLineLayout); 637bb02e0acSNicolas Bonnefon bottomMainLayout->addLayout(searchInfoLineLayout); 638bb02e0acSNicolas Bonnefon bottomMainLayout->addWidget(filteredView); 639bb02e0acSNicolas Bonnefon bottomMainLayout->setContentsMargins(2, 1, 2, 1); 640bb02e0acSNicolas Bonnefon bottomWindow->setLayout(bottomMainLayout); 641bb02e0acSNicolas Bonnefon 642bb02e0acSNicolas Bonnefon addWidget( logMainView ); 643bb02e0acSNicolas Bonnefon addWidget( bottomWindow ); 644bb02e0acSNicolas Bonnefon 645bb02e0acSNicolas Bonnefon // Default splitter position (usually overridden by the config file) 646bb02e0acSNicolas Bonnefon QList<int> splitterSizes; 647bb02e0acSNicolas Bonnefon splitterSizes += 400; 648bb02e0acSNicolas Bonnefon splitterSizes += 100; 649bb02e0acSNicolas Bonnefon setSizes( splitterSizes ); 650bb02e0acSNicolas Bonnefon 651f688be2eSNicolas Bonnefon // Default search checkboxes 652f688be2eSNicolas Bonnefon auto config = Persistent<Configuration>( "settings" ); 653f688be2eSNicolas Bonnefon searchRefreshCheck->setCheckState( config->isSearchAutoRefreshDefault() ? 654f688be2eSNicolas Bonnefon Qt::Checked : Qt::Unchecked ); 655c580560cSNicolas Bonnefon // Manually call the handler as it is not called when changing the state programmatically 656c580560cSNicolas Bonnefon searchRefreshChangedHandler( searchRefreshCheck->checkState() ); 657f688be2eSNicolas Bonnefon ignoreCaseCheck->setCheckState( config->isSearchIgnoreCaseDefault() ? 658f688be2eSNicolas Bonnefon Qt::Checked : Qt::Unchecked ); 659f688be2eSNicolas Bonnefon 660bb02e0acSNicolas Bonnefon // Connect the signals 661bb02e0acSNicolas Bonnefon connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ), 662bb02e0acSNicolas Bonnefon searchButton, SIGNAL( clicked() )); 663bb02e0acSNicolas Bonnefon connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ), 664bb02e0acSNicolas Bonnefon this, SLOT( searchTextChangeHandler() )); 665bb02e0acSNicolas Bonnefon connect(searchButton, SIGNAL( clicked() ), 666bb02e0acSNicolas Bonnefon this, SLOT( startNewSearch() ) ); 667bb02e0acSNicolas Bonnefon connect(stopButton, SIGNAL( clicked() ), 668bb02e0acSNicolas Bonnefon this, SLOT( stopSearch() ) ); 669bb02e0acSNicolas Bonnefon 670bb02e0acSNicolas Bonnefon connect(visibilityBox, SIGNAL( currentIndexChanged( int ) ), 671bb02e0acSNicolas Bonnefon this, SLOT( changeFilteredViewVisibility( int ) ) ); 672bb02e0acSNicolas Bonnefon 673bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( newSelection( int ) ), 674bb02e0acSNicolas Bonnefon logMainView, SLOT( update() ) ); 675bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( newSelection( int ) ), 676bb02e0acSNicolas Bonnefon this, SLOT( jumpToMatchingLine( int ) ) ); 677bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( newSelection( int ) ), 678bb02e0acSNicolas Bonnefon filteredView, SLOT( update() ) ); 679bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( updateLineNumber( int ) ), 6809cacd6a9SNicolas Bonnefon this, SLOT( updateLineNumberHandler( int ) ) ); 681bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( markLine( qint64 ) ), 682bb02e0acSNicolas Bonnefon this, SLOT( markLineFromMain( qint64 ) ) ); 683bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( markLine( qint64 ) ), 684bb02e0acSNicolas Bonnefon this, SLOT( markLineFromFiltered( qint64 ) ) ); 685bb02e0acSNicolas Bonnefon 686bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( addToSearch( const QString& ) ), 687bb02e0acSNicolas Bonnefon this, SLOT( addToSearch( const QString& ) ) ); 688bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( addToSearch( const QString& ) ), 689bb02e0acSNicolas Bonnefon this, SLOT( addToSearch( const QString& ) ) ); 690bb02e0acSNicolas Bonnefon 691bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( mouseHoveredOverLine( qint64 ) ), 692bb02e0acSNicolas Bonnefon this, SLOT( mouseHoveredOverMatch( qint64 ) ) ); 693bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( mouseLeftHoveringZone() ), 694bb02e0acSNicolas Bonnefon overviewWidget_, SLOT( removeHighlight() ) ); 695bb02e0acSNicolas Bonnefon 696bb02e0acSNicolas Bonnefon // Follow option (up and down) 697bb02e0acSNicolas Bonnefon connect(this, SIGNAL( followSet( bool ) ), 698bb02e0acSNicolas Bonnefon logMainView, SLOT( followSet( bool ) ) ); 699bb02e0acSNicolas Bonnefon connect(this, SIGNAL( followSet( bool ) ), 700bb02e0acSNicolas Bonnefon filteredView, SLOT( followSet( bool ) ) ); 701bb02e0acSNicolas Bonnefon connect(logMainView, SIGNAL( followDisabled() ), 702bb02e0acSNicolas Bonnefon this, SIGNAL( followDisabled() ) ); 703bb02e0acSNicolas Bonnefon connect(filteredView, SIGNAL( followDisabled() ), 704bb02e0acSNicolas Bonnefon this, SIGNAL( followDisabled() ) ); 705bb02e0acSNicolas Bonnefon 706bb02e0acSNicolas Bonnefon connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ), 707bb02e0acSNicolas Bonnefon this, SLOT( updateFilteredView( int, int ) ) ); 708bb02e0acSNicolas Bonnefon 709bb02e0acSNicolas Bonnefon // Sent load file update to MainWindow (for status update) 710bb02e0acSNicolas Bonnefon connect( logData_, SIGNAL( loadingProgressed( int ) ), 711bb02e0acSNicolas Bonnefon this, SIGNAL( loadingProgressed( int ) ) ); 712812146a8SNicolas Bonnefon connect( logData_, SIGNAL( loadingFinished( LoadingStatus ) ), 713812146a8SNicolas Bonnefon this, SLOT( loadingFinishedHandler( LoadingStatus ) ) ); 714bb02e0acSNicolas Bonnefon connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ), 715bb02e0acSNicolas Bonnefon this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) ); 716bb02e0acSNicolas Bonnefon 717bb02e0acSNicolas Bonnefon // Search auto-refresh 718bb02e0acSNicolas Bonnefon connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 719bb02e0acSNicolas Bonnefon this, SLOT( searchRefreshChangedHandler( int ) ) ); 720f688be2eSNicolas Bonnefon 721f688be2eSNicolas Bonnefon // Advise the parent the checkboxes have been changed 722f688be2eSNicolas Bonnefon // (for maintaining default config) 723f688be2eSNicolas Bonnefon connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ), 724f688be2eSNicolas Bonnefon this, SIGNAL( searchRefreshChanged( int ) ) ); 725f688be2eSNicolas Bonnefon connect( ignoreCaseCheck, SIGNAL( stateChanged( int ) ), 726f688be2eSNicolas Bonnefon this, SIGNAL( ignoreCaseChanged( int ) ) ); 727bb02e0acSNicolas Bonnefon } 728bb02e0acSNicolas Bonnefon 729bb02e0acSNicolas Bonnefon // Create a new search using the text passed, replace the currently 730bb02e0acSNicolas Bonnefon // used one and destroy the old one. 731bb02e0acSNicolas Bonnefon void CrawlerWidget::replaceCurrentSearch( const QString& searchText ) 732bb02e0acSNicolas Bonnefon { 733bb02e0acSNicolas Bonnefon // Interrupt the search if it's ongoing 734bb02e0acSNicolas Bonnefon logFilteredData_->interruptSearch(); 735bb02e0acSNicolas Bonnefon 736bb02e0acSNicolas Bonnefon // We have to wait for the last search update (100%) 737bb02e0acSNicolas Bonnefon // before clearing/restarting to avoid having remaining results. 738bb02e0acSNicolas Bonnefon 739bb02e0acSNicolas Bonnefon // FIXME: this is a bit of a hack, we call processEvents 740bb02e0acSNicolas Bonnefon // for Qt to empty its event queue, including (hopefully) 741bb02e0acSNicolas Bonnefon // the search update event sent by logFilteredData_. It saves 742bb02e0acSNicolas Bonnefon // us the overhead of having proper sync. 743bb02e0acSNicolas Bonnefon QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); 744bb02e0acSNicolas Bonnefon 745bb02e0acSNicolas Bonnefon if ( !searchText.isEmpty() ) { 746bb02e0acSNicolas Bonnefon // Determine the type of regexp depending on the config 747bb02e0acSNicolas Bonnefon QRegExp::PatternSyntax syntax; 74811582726SNicolas Bonnefon static std::shared_ptr<Configuration> config = 74911582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 75011582726SNicolas Bonnefon switch ( config->mainRegexpType() ) { 751bb02e0acSNicolas Bonnefon case Wildcard: 752bb02e0acSNicolas Bonnefon syntax = QRegExp::Wildcard; 753bb02e0acSNicolas Bonnefon break; 754bb02e0acSNicolas Bonnefon case FixedString: 755bb02e0acSNicolas Bonnefon syntax = QRegExp::FixedString; 756bb02e0acSNicolas Bonnefon break; 757bb02e0acSNicolas Bonnefon default: 758bb02e0acSNicolas Bonnefon syntax = QRegExp::RegExp2; 759bb02e0acSNicolas Bonnefon break; 760bb02e0acSNicolas Bonnefon } 761bb02e0acSNicolas Bonnefon 762bb02e0acSNicolas Bonnefon // Set the pattern case insensitive if needed 763bb02e0acSNicolas Bonnefon Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive; 764bb02e0acSNicolas Bonnefon if ( ignoreCaseCheck->checkState() == Qt::Checked ) 765bb02e0acSNicolas Bonnefon case_sensitivity = Qt::CaseInsensitive; 766bb02e0acSNicolas Bonnefon 767bb02e0acSNicolas Bonnefon // Constructs the regexp 768bb02e0acSNicolas Bonnefon QRegExp regexp( searchText, case_sensitivity, syntax ); 769bb02e0acSNicolas Bonnefon 770bb02e0acSNicolas Bonnefon if ( regexp.isValid() ) { 771bb02e0acSNicolas Bonnefon // Activate the stop button 772bb02e0acSNicolas Bonnefon stopButton->setEnabled( true ); 773bb02e0acSNicolas Bonnefon // Start a new asynchronous search 774bb02e0acSNicolas Bonnefon logFilteredData_->runSearch( regexp ); 775bb02e0acSNicolas Bonnefon // Accept auto-refresh of the search 776bb02e0acSNicolas Bonnefon searchState_.startSearch(); 777bb02e0acSNicolas Bonnefon } 778bb02e0acSNicolas Bonnefon else { 779bb02e0acSNicolas Bonnefon // The regexp is wrong 780bb02e0acSNicolas Bonnefon logFilteredData_->clearSearch(); 781bb02e0acSNicolas Bonnefon filteredView->updateData(); 782bb02e0acSNicolas Bonnefon searchState_.resetState(); 783bb02e0acSNicolas Bonnefon 784bb02e0acSNicolas Bonnefon // Inform the user 785bb02e0acSNicolas Bonnefon QString errorMessage = tr("Error in expression: "); 786bb02e0acSNicolas Bonnefon errorMessage += regexp.errorString(); 787bb02e0acSNicolas Bonnefon searchInfoLine->setPalette( errorPalette ); 788bb02e0acSNicolas Bonnefon searchInfoLine->setText( errorMessage ); 789bb02e0acSNicolas Bonnefon } 790bb02e0acSNicolas Bonnefon } 791bb02e0acSNicolas Bonnefon else { 792bb02e0acSNicolas Bonnefon logFilteredData_->clearSearch(); 793bb02e0acSNicolas Bonnefon filteredView->updateData(); 794bb02e0acSNicolas Bonnefon searchState_.resetState(); 795bb02e0acSNicolas Bonnefon printSearchInfoMessage(); 796bb02e0acSNicolas Bonnefon } 797bb02e0acSNicolas Bonnefon } 798bb02e0acSNicolas Bonnefon 799bb02e0acSNicolas Bonnefon // Updates the content of the drop down list for the saved searches, 800bb02e0acSNicolas Bonnefon // called when the SavedSearch has been changed. 801bb02e0acSNicolas Bonnefon void CrawlerWidget::updateSearchCombo() 802bb02e0acSNicolas Bonnefon { 803bb02e0acSNicolas Bonnefon const QString text = searchLineEdit->lineEdit()->text(); 804bb02e0acSNicolas Bonnefon searchLineEdit->clear(); 8051b5e406eSNicolas Bonnefon searchLineEdit->addItems( savedSearches_->recentSearches() ); 806bb02e0acSNicolas Bonnefon // In case we had something that wasn't added to the list (blank...): 807bb02e0acSNicolas Bonnefon searchLineEdit->lineEdit()->setText( text ); 808bb02e0acSNicolas Bonnefon } 809bb02e0acSNicolas Bonnefon 810bb02e0acSNicolas Bonnefon // Print the search info message. 811bb02e0acSNicolas Bonnefon void CrawlerWidget::printSearchInfoMessage( int nbMatches ) 812bb02e0acSNicolas Bonnefon { 813bb02e0acSNicolas Bonnefon QString text; 814bb02e0acSNicolas Bonnefon 815bb02e0acSNicolas Bonnefon switch ( searchState_.getState() ) { 816bb02e0acSNicolas Bonnefon case SearchState::NoSearch: 817bb02e0acSNicolas Bonnefon // Blank text is fine 818bb02e0acSNicolas Bonnefon break; 819bb02e0acSNicolas Bonnefon case SearchState::Static: 820bb02e0acSNicolas Bonnefon text = tr("%1 match%2 found.").arg( nbMatches ) 821bb02e0acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ); 822bb02e0acSNicolas Bonnefon break; 823bb02e0acSNicolas Bonnefon case SearchState::Autorefreshing: 824bb02e0acSNicolas Bonnefon text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches ) 825bb02e0acSNicolas Bonnefon .arg( nbMatches > 1 ? "es" : "" ); 826bb02e0acSNicolas Bonnefon break; 827bb02e0acSNicolas Bonnefon case SearchState::FileTruncated: 82859d4e393SNicolas Bonnefon case SearchState::TruncatedAutorefreshing: 829bb02e0acSNicolas Bonnefon text = tr("File truncated on disk, previous search results are not valid anymore."); 830bb02e0acSNicolas Bonnefon break; 831bb02e0acSNicolas Bonnefon } 832bb02e0acSNicolas Bonnefon 833bb02e0acSNicolas Bonnefon searchInfoLine->setPalette( searchInfoLineDefaultPalette ); 834bb02e0acSNicolas Bonnefon searchInfoLine->setText( text ); 835bb02e0acSNicolas Bonnefon } 836bb02e0acSNicolas Bonnefon 837bb02e0acSNicolas Bonnefon // 838bb02e0acSNicolas Bonnefon // SearchState implementation 839bb02e0acSNicolas Bonnefon // 840bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::resetState() 841bb02e0acSNicolas Bonnefon { 842bb02e0acSNicolas Bonnefon state_ = NoSearch; 843bb02e0acSNicolas Bonnefon } 844bb02e0acSNicolas Bonnefon 845bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::setAutorefresh( bool refresh ) 846bb02e0acSNicolas Bonnefon { 847bb02e0acSNicolas Bonnefon autoRefreshRequested_ = refresh; 848bb02e0acSNicolas Bonnefon 849bb02e0acSNicolas Bonnefon if ( refresh ) { 850bb02e0acSNicolas Bonnefon if ( state_ == Static ) 851bb02e0acSNicolas Bonnefon state_ = Autorefreshing; 85259d4e393SNicolas Bonnefon /* 85359d4e393SNicolas Bonnefon else if ( state_ == FileTruncated ) 85459d4e393SNicolas Bonnefon state_ = TruncatedAutorefreshing; 85559d4e393SNicolas Bonnefon */ 856bb02e0acSNicolas Bonnefon } 857bb02e0acSNicolas Bonnefon else { 858bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 859bb02e0acSNicolas Bonnefon state_ = Static; 86059d4e393SNicolas Bonnefon else if ( state_ == TruncatedAutorefreshing ) 86159d4e393SNicolas Bonnefon state_ = FileTruncated; 862bb02e0acSNicolas Bonnefon } 863bb02e0acSNicolas Bonnefon } 864bb02e0acSNicolas Bonnefon 865bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::truncateFile() 866bb02e0acSNicolas Bonnefon { 86759d4e393SNicolas Bonnefon if ( state_ == Autorefreshing || state_ == TruncatedAutorefreshing ) { 86859d4e393SNicolas Bonnefon state_ = TruncatedAutorefreshing; 86959d4e393SNicolas Bonnefon } 87059d4e393SNicolas Bonnefon else { 871bb02e0acSNicolas Bonnefon state_ = FileTruncated; 872bb02e0acSNicolas Bonnefon } 87359d4e393SNicolas Bonnefon } 874bb02e0acSNicolas Bonnefon 875bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::changeExpression() 876bb02e0acSNicolas Bonnefon { 877bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 878bb02e0acSNicolas Bonnefon state_ = Static; 879bb02e0acSNicolas Bonnefon } 880bb02e0acSNicolas Bonnefon 881bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::stopSearch() 882bb02e0acSNicolas Bonnefon { 883bb02e0acSNicolas Bonnefon if ( state_ == Autorefreshing ) 884bb02e0acSNicolas Bonnefon state_ = Static; 885bb02e0acSNicolas Bonnefon } 886bb02e0acSNicolas Bonnefon 887bb02e0acSNicolas Bonnefon void CrawlerWidget::SearchState::startSearch() 888bb02e0acSNicolas Bonnefon { 889bb02e0acSNicolas Bonnefon if ( autoRefreshRequested_ ) 890bb02e0acSNicolas Bonnefon state_ = Autorefreshing; 891bb02e0acSNicolas Bonnefon else 892bb02e0acSNicolas Bonnefon state_ = Static; 893bb02e0acSNicolas Bonnefon } 894a44d09bcSNicolas Bonnefon 895a44d09bcSNicolas Bonnefon /* 896a44d09bcSNicolas Bonnefon * CrawlerWidgetContext 897a44d09bcSNicolas Bonnefon */ 898a44d09bcSNicolas Bonnefon CrawlerWidgetContext::CrawlerWidgetContext( const char* string ) 899a44d09bcSNicolas Bonnefon { 900a44d09bcSNicolas Bonnefon QRegExp regex = QRegExp( "S(\\d+):(\\d+)" ); 901a44d09bcSNicolas Bonnefon 902f688be2eSNicolas Bonnefon if ( regex.indexIn( string ) > -1 ) { 903a44d09bcSNicolas Bonnefon sizes_ = { regex.cap(1).toInt(), regex.cap(2).toInt() }; 904a44d09bcSNicolas Bonnefon LOG(logDEBUG) << "sizes_: " << sizes_[0] << " " << sizes_[1]; 905a44d09bcSNicolas Bonnefon } 906f688be2eSNicolas Bonnefon else { 907f688be2eSNicolas Bonnefon LOG(logWARNING) << "Unrecognised view size: " << string; 908a44d09bcSNicolas Bonnefon 909a44d09bcSNicolas Bonnefon // Default values; 910a44d09bcSNicolas Bonnefon sizes_ = { 100, 400 }; 911a44d09bcSNicolas Bonnefon } 912f688be2eSNicolas Bonnefon 913f688be2eSNicolas Bonnefon QRegExp case_refresh_regex = QRegExp( "IC(\\d+):AR(\\d+)" ); 914f688be2eSNicolas Bonnefon 915f688be2eSNicolas Bonnefon if ( case_refresh_regex.indexIn( string ) > -1 ) { 916f688be2eSNicolas Bonnefon ignore_case_ = ( case_refresh_regex.cap(1).toInt() == 1 ); 917f688be2eSNicolas Bonnefon auto_refresh_ = ( case_refresh_regex.cap(2).toInt() == 1 ); 918f688be2eSNicolas Bonnefon 919f688be2eSNicolas Bonnefon LOG(logDEBUG) << "ignore_case_: " << ignore_case_ << " auto_refresh_: " 920f688be2eSNicolas Bonnefon << auto_refresh_; 921f688be2eSNicolas Bonnefon } 922f688be2eSNicolas Bonnefon else { 923f688be2eSNicolas Bonnefon LOG(logWARNING) << "Unrecognised case/refresh: " << string; 924f688be2eSNicolas Bonnefon ignore_case_ = false; 925f688be2eSNicolas Bonnefon auto_refresh_ = false; 926f688be2eSNicolas Bonnefon } 927a44d09bcSNicolas Bonnefon } 928a44d09bcSNicolas Bonnefon 929a44d09bcSNicolas Bonnefon std::string CrawlerWidgetContext::toString() const 930a44d09bcSNicolas Bonnefon { 931a44d09bcSNicolas Bonnefon char string[160]; 932a44d09bcSNicolas Bonnefon 933f688be2eSNicolas Bonnefon snprintf( string, sizeof string, "S%d:%d:IC%d:AR%d", 934f688be2eSNicolas Bonnefon sizes_[0], sizes_[1], 935f688be2eSNicolas Bonnefon ignore_case_, auto_refresh_ ); 936a44d09bcSNicolas Bonnefon 937f688be2eSNicolas Bonnefon return { string }; 938a44d09bcSNicolas Bonnefon } 939