1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/*
* Copyright (C) 2009, 2010, 2011, 2012 Nicolas Bonnefon and other contributors
*
* This file is part of glogg.
*
* glogg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* glogg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with glogg. If not, see <http://www.gnu.org/licenses/>.
*/
// This file implements the CrawlerWidget class.
// It is responsible for creating and managing the two views and all
// the UI elements. It implements the connection between the UI elements.
// It also owns the sets of data (full and filtered).
#include "log.h"
#include <Qt>
#include <QApplication>
#include <QFile>
#include <QLineEdit>
#include <QFileInfo>
#include <QKeyEvent>
#include "crawlerwidget.h"
#include "quickfindpattern.h"
#include "overview.h"
#include "infoline.h"
#include "savedsearches.h"
#include "quickfindwidget.h"
#include "persistentinfo.h"
#include "configuration.h"
// Palette for error signaling (yellow background)
const QPalette CrawlerWidget::errorPalette( QColor( "yellow" ) );
// Constructor makes all the child widgets and set up connections.
CrawlerWidget::CrawlerWidget(SavedSearches* searches, QWidget *parent)
: QSplitter(parent)
{
setOrientation(Qt::Vertical);
// Initialise internal data (with empty file and search)
logData_ = new LogData();
logFilteredData_ = logData_->getNewFilteredData();
// Initialise the QFP object (one for both views)
// This is the confirmed pattern used by n/N and coloured in yellow.
quickFindPattern_ = new QuickFindPattern();
// The matches overview
overview_ = new Overview();
// The views
logMainView = new LogMainView( logData_, quickFindPattern_, overview_ );
bottomWindow = new QWidget;
filteredView = new FilteredView( logFilteredData_, quickFindPattern_ );
savedSearches = searches;
// Construct the Search Info line
searchInfoLine = new InfoLine();
searchInfoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
searchInfoLine->setLineWidth( 1 );
searchInfoLineDefaultPalette = searchInfoLine->palette();
ignoreCaseCheck = new QCheckBox( "Ignore &case" );
searchRefreshCheck = new QCheckBox( "Auto-&refresh" );
// Construct the Search line
searchLabel = new QLabel(tr("&Text: "));
searchLineEdit = new QComboBox;
searchLineEdit->setEditable( true );
searchLineEdit->setCompleter( 0 );
searchLineEdit->addItems( savedSearches->recentSearches() );
searchLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
searchLineEdit->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
searchLabel->setBuddy( searchLineEdit );
searchButton = new QToolButton();
searchButton->setText( tr("&Search") );
searchButton->setAutoRaise( true );
stopButton = new QToolButton();
stopButton->setIcon( QIcon(":/images/stop16.png") );
stopButton->setAutoRaise( true );
stopButton->setEnabled( false );
// Construct the QuickFind bar
quickFindWidget_ = new QuickFindWidget();
QHBoxLayout* searchLineLayout = new QHBoxLayout;
searchLineLayout->addWidget(searchLabel);
searchLineLayout->addWidget(searchLineEdit);
searchLineLayout->addWidget(searchButton);
searchLineLayout->addWidget(stopButton);
searchLineLayout->setContentsMargins(6, 0, 6, 0);
stopButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) );
searchButton->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) );
QHBoxLayout* searchInfoLineLayout = new QHBoxLayout;
searchInfoLineLayout->addWidget( searchInfoLine );
searchInfoLineLayout->addWidget( ignoreCaseCheck );
searchInfoLineLayout->addWidget( searchRefreshCheck );
// Construct the bottom window
quickFindWidget_->hide();
QVBoxLayout* bottomMainLayout = new QVBoxLayout;
bottomMainLayout->addLayout(searchLineLayout);
bottomMainLayout->addLayout(searchInfoLineLayout);
bottomMainLayout->addWidget(filteredView);
bottomMainLayout->addWidget(quickFindWidget_);
bottomMainLayout->setContentsMargins(2, 1, 2, 1);
bottomWindow->setLayout(bottomMainLayout);
addWidget(logMainView);
addWidget(bottomWindow);
// Default splitter position (usually overridden by the config file)
QList<int> splitterSizes;
splitterSizes += 400;
splitterSizes += 100;
setSizes( splitterSizes );
// Connect the signals
connect(searchLineEdit->lineEdit(), SIGNAL( returnPressed() ),
searchButton, SIGNAL( clicked() ));
connect(searchLineEdit->lineEdit(), SIGNAL( textEdited( const QString& ) ),
this, SLOT( searchTextChangeHandler() ));
connect(searchButton, SIGNAL( clicked() ),
this, SLOT( startNewSearch() ) );
connect(stopButton, SIGNAL( clicked() ),
this, SLOT( stopSearch() ) );
connect(logMainView, SIGNAL( newSelection( int ) ),
logMainView, SLOT( update() ) );
connect(filteredView, SIGNAL( newSelection( int ) ),
this, SLOT( jumpToMatchingLine( int ) ) );
connect(filteredView, SIGNAL( newSelection( int ) ),
filteredView, SLOT( update() ) );
connect(logMainView, SIGNAL( updateLineNumber( int ) ),
this, SIGNAL( updateLineNumber( int ) ) );
connect(logMainView, SIGNAL( markLine( qint64 ) ),
this, SLOT( markLineFromMain( qint64 ) ) );
connect(filteredView, SIGNAL( markLine( qint64 ) ),
this, SLOT( markLineFromFiltered( qint64 ) ) );
// Follow option (up and down)
connect(this, SIGNAL( followSet( bool ) ),
logMainView, SLOT( followSet( bool ) ) );
connect(logMainView, SIGNAL( followDisabled() ),
this, SIGNAL( followDisabled() ) );
connect( logFilteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( updateFilteredView( int, int ) ) );
// QuickFind
connect( quickFindWidget_, SIGNAL( patternConfirmed( const QString& ) ),
this, SLOT( applyNewQFPattern( const QString& ) ) );
connect( quickFindWidget_, SIGNAL( close() ),
this, SLOT( hideQuickFindBar() ) );
connect( quickFindWidget_, SIGNAL( searchForward() ),
this, SLOT( searchForward() ) );
connect( quickFindWidget_, SIGNAL( searchBackward() ),
this, SLOT( searchBackward() ) );
// QuickFind changes coming from the views
connect(logMainView, SIGNAL( changeQuickFind( const QString& ) ),
this, SLOT( changeQFPattern( const QString& ) ) );
connect(filteredView, SIGNAL( changeQuickFind( const QString& ) ),
this, SLOT( changeQFPattern( const QString& ) ) );
connect(logMainView, SIGNAL( notifyQuickFind( const QString& ) ),
quickFindWidget_, SLOT( notify( const QString& ) ) );
connect(filteredView, SIGNAL( notifyQuickFind( const QString& ) ),
quickFindWidget_, SLOT( notify( const QString& ) ) );
connect(logMainView, SIGNAL( clearQuickFindNotification() ),
quickFindWidget_, SLOT( clearNotification() ) );
connect(filteredView, SIGNAL( clearQuickFindNotification() ),
quickFindWidget_, SLOT( clearNotification() ) );
// Sent load file update to MainWindow (for status update)
connect( logData_, SIGNAL( loadingProgressed( int ) ),
this, SIGNAL( loadingProgressed( int ) ) );
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinishedHandler( bool ) ) );
connect( logData_, SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ),
this, SLOT( fileChangedHandler( LogData::MonitoredFileStatus ) ) );
// Search auto-refresh
connect( searchRefreshCheck, SIGNAL( stateChanged( int ) ),
this, SLOT( searchRefreshChangedHandler( int ) ) );
}
// Start the asynchronous loading of a file.
bool CrawlerWidget::readFile( const QString& fileName, int )
{
QFileInfo fileInfo( fileName );
if ( fileInfo.isReadable() )
{
LOG(logDEBUG) << "Entering readFile " << fileName.toStdString();
// First we cancel any in progress search and loading
stopLoading();
// The file exist, so we invalidate the search, remove all marks
// and redraw the screen.
replaceCurrentSearch( "" );
logFilteredData_->clearMarks();
logData_->attachFile( fileName );
logMainView->updateData();
// Forbid starting a search when loading in progress
// searchButton->setEnabled( false );
return true;
}
else {
return false;
}
}
void CrawlerWidget::stopLoading()
{
logFilteredData_->interruptSearch();
logData_->interruptLoading();
}
void CrawlerWidget::getFileInfo( qint64* fileSize, int* fileNbLine,
QDateTime* lastModified ) const
{
*fileSize = logData_->getFileSize();
*fileNbLine = logData_->getNbLine();
*lastModified = logData_->getLastModifiedDate();
}
// The top line is first one on the main display
int CrawlerWidget::getTopLine() const
{
return logMainView->getTopLine();
}
QString CrawlerWidget::getSelectedText() const
{
if ( filteredView->hasFocus() )
return filteredView->getSelection();
else
return logMainView->getSelection();
}
//
// Events handlers
//
void CrawlerWidget::keyPressEvent( QKeyEvent* keyEvent )
{
LOG(logDEBUG4) << "keyPressEvent received";
switch ( (keyEvent->text())[0].toAscii() ) {
case '/':
case '?':
displayQuickFindBar();
break;
default:
keyEvent->ignore();
}
if ( !keyEvent->isAccepted() )
QSplitter::keyPressEvent( keyEvent );
}
//
// Slots
//
void CrawlerWidget::startNewSearch()
{
// Record the search line in the recent list
// (reload the list first in case another glogg changed it)
GetPersistentInfo().retrieve( "savedSearches" );
savedSearches->addRecent( searchLineEdit->currentText() );
GetPersistentInfo().save( "savedSearches" );
// Update the SearchLine (history)
updateSearchCombo();
// Call the private function to do the search
replaceCurrentSearch( searchLineEdit->currentText() );
}
void CrawlerWidget::stopSearch()
{
logFilteredData_->interruptSearch();
searchState_.stopSearch();
printSearchInfoMessage();
}
// When receiving the 'newDataAvailable' signal from LogFilteredData
void CrawlerWidget::updateFilteredView( int nbMatches, int progress )
{
LOG(logDEBUG) << "updateFilteredView received.";
if ( progress == 100 ) {
// Searching done
printSearchInfoMessage( nbMatches );
searchInfoLine->hideGauge();
// De-activate the stop button
stopButton->setEnabled( false );
}
else {
// Search in progress
// We ignore 0% and 100% to avoid a flash when the search is very short
if ( progress > 0 ) {
searchInfoLine->setText(
tr("Search in progress (%1 %)... %2 match%3 found so far.")
.arg( progress )
.arg( nbMatches )
.arg( nbMatches > 1 ? "es" : "" ) );
searchInfoLine->displayGauge( progress );
}
}
// Recompute the content of the filtered window.
filteredView->updateData();
// Update the match overview
overview_->updateData( logData_->getNbLine() );
// Also update the top window for the coloured bullets.
update();
}
void CrawlerWidget::jumpToMatchingLine(int filteredLineNb)
{
int mainViewLine = logFilteredData_->getMatchingLineNumber(filteredLineNb);
logMainView->selectAndDisplayLine(mainViewLine); // FIXME: should be done with a signal.
}
void CrawlerWidget::markLineFromMain( qint64 line )
{
if ( logFilteredData_->isLineMarked( line ) )
logFilteredData_->deleteMark( line );
else
logFilteredData_->addMark( line );
// Recompute the content of the filtered window.
filteredView->updateData();
// Update the match overview
overview_->updateData( logData_->getNbLine() );
// Also update the top window for the coloured bullets.
update();
}
void CrawlerWidget::markLineFromFiltered( qint64 line )
{
qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line );
if ( logFilteredData_->filteredLineTypeByIndex( line )
== LogFilteredData::Mark )
logFilteredData_->deleteMark( line_in_file );
else
logFilteredData_->addMark( line_in_file );
// Recompute the content of the filtered window.
filteredView->updateData();
// Update the match overview
overview_->updateData( logData_->getNbLine() );
// Also update the top window for the coloured bullets.
update();
}
void CrawlerWidget::applyConfiguration()
{
Configuration& config = Persistent<Configuration>( "settings" );
QFont font = config.mainFont();
LOG(logDEBUG) << "CrawlerWidget::applyConfiguration";
logMainView->setFont(font);
filteredView->setFont(font);
overview_->setVisible( config.isOverviewVisible() );
logMainView->refreshOverview();
logMainView->updateDisplaySize();
logMainView->update();
filteredView->updateDisplaySize();
filteredView->update();
// Update the SearchLine (history)
updateSearchCombo();
}
void CrawlerWidget::loadingFinishedHandler( bool success )
{
// We need to refresh the main window because the view lines on the
// overview have probably changed.
overview_->updateData( logData_->getNbLine() );
// FIXME, handle topLine
// logMainView->updateData( logData_, topLine );
logMainView->updateData();
// searchButton->setEnabled( true );
// See if we need to auto-refresh the search
if ( searchState_.isAutorefreshAllowed() ) {
LOG(logDEBUG) << "Refreshing the search";
logFilteredData_->updateSearch();
}
emit loadingFinished( success );
}
void CrawlerWidget::fileChangedHandler( LogData::MonitoredFileStatus status )
{
// Handle the case where the file has been truncated
if ( status == LogData::Truncated ) {
// Clear all marks (TODO offer the option to keep them)
logFilteredData_->clearMarks();
if ( ! searchInfoLine->text().isEmpty() ) {
// Invalidate the search
logFilteredData_->clearSearch();
filteredView->updateData();
searchState_.truncateFile();
printSearchInfoMessage();
}
}
}
void CrawlerWidget::displayQuickFindBar()
{
LOG(logDEBUG) << "CrawlerWidget::displayQuickFindBar";
// Remember who had the focus
qfSavedFocus_ = QApplication::focusWidget();
quickFindWidget_->userActivate();
}
void CrawlerWidget::hideQuickFindBar()
{
// Restore the focus once the QFBar has been hidden
qfSavedFocus_->setFocus();
}
void CrawlerWidget::applyNewQFPattern( const QString& newPattern )
{
quickFindPattern_->changeSearchPattern( newPattern );
}
void CrawlerWidget::changeQFPattern( const QString& newPattern )
{
quickFindWidget_->changeDisplayedPattern( newPattern );
quickFindPattern_->changeSearchPattern( newPattern );
}
// Returns a pointer to the window in which the search should be done
AbstractLogView* CrawlerWidget::searchableWidget() const
{
QWidget* searchableWidget;
// Search in the window that has focus, or the window where 'Find' was
// called from, or the main window.
if ( filteredView->hasFocus() || logMainView->hasFocus() )
searchableWidget = QApplication::focusWidget();
else
searchableWidget = qfSavedFocus_;
if ( AbstractLogView* view = qobject_cast<AbstractLogView*>( searchableWidget ) )
return view;
else
return logMainView;
}
void CrawlerWidget::searchForward()
{
LOG(logDEBUG) << "CrawlerWidget::searchForward";
searchableWidget()->searchForward();
}
void CrawlerWidget::searchBackward()
{
LOG(logDEBUG) << "CrawlerWidget::searchBackward";
searchableWidget()->searchBackward();
}
void CrawlerWidget::searchRefreshChangedHandler( int state )
{
searchState_.setAutorefresh( state == Qt::Checked );
printSearchInfoMessage( logFilteredData_->getNbMatches() );
}
void CrawlerWidget::searchTextChangeHandler()
{
// We suspend auto-refresh
searchState_.changeExpression();
printSearchInfoMessage( logFilteredData_->getNbMatches() );
}
//
// Private functions
//
// Create a new search using the text passed, replace the currently
// used one and destroy the old one.
void CrawlerWidget::replaceCurrentSearch( const QString& searchText )
{
// Interrupt the search if it's ongoing
logFilteredData_->interruptSearch();
// We have to wait for the last search update (100%)
// before clearing/restarting to avoid having remaining results.
// FIXME: this is a bit of a hack, we call processEvents
// for Qt to empty its event queue, including (hopefully)
// the search update event sent by logFilteredData_. It saves
// us the overhead of having proper sync.
QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
if ( !searchText.isEmpty() ) {
// Determine the type of regexp depending on the config
QRegExp::PatternSyntax syntax;
Configuration& config = Persistent<Configuration>( "settings" );
switch ( config.mainRegexpType() ) {
case Wildcard:
syntax = QRegExp::Wildcard;
break;
case FixedString:
syntax = QRegExp::FixedString;
break;
default:
syntax = QRegExp::RegExp2;
break;
}
// Set the pattern case insensitive if needed
Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitive;
if ( ignoreCaseCheck->checkState() == Qt::Checked )
case_sensitivity = Qt::CaseInsensitive;
// Constructs the regexp
QRegExp regexp( searchText, case_sensitivity, syntax );
if ( regexp.isValid() ) {
// Activate the stop button
stopButton->setEnabled( true );
// Start a new asynchronous search
logFilteredData_->runSearch( regexp );
// Accept auto-refresh of the search
searchState_.startSearch();
}
else {
// The regexp is wrong
logFilteredData_->clearSearch();
filteredView->updateData();
searchState_.resetState();
// Inform the user
QString errorMessage = tr("Error in expression: ");
errorMessage += regexp.errorString();
searchInfoLine->setPalette( errorPalette );
searchInfoLine->setText( errorMessage );
}
}
else {
logFilteredData_->clearSearch();
filteredView->updateData();
searchState_.resetState();
printSearchInfoMessage();
}
// Connect the search to the top view
logMainView->useNewFiltering( logFilteredData_ );
}
// Updates the content of the drop down list for the saved searches,
// called when the SavedSearch has been changed.
void CrawlerWidget::updateSearchCombo()
{
const QString text = searchLineEdit->lineEdit()->text();
searchLineEdit->clear();
searchLineEdit->addItems( savedSearches->recentSearches() );
// In case we had something that wasn't added to the list (blank...):
searchLineEdit->lineEdit()->setText( text );
}
// Print the search info message.
void CrawlerWidget::printSearchInfoMessage( int nbMatches )
{
QString text;
switch ( searchState_.getState() ) {
case SearchState::NoSearch:
// Blank text is fine
break;
case SearchState::Static:
text = tr("%1 match%2 found.").arg( nbMatches )
.arg( nbMatches > 1 ? "es" : "" );
break;
case SearchState::Autorefreshing:
text = tr("%1 match%2 found. Search is auto-refreshing...").arg( nbMatches )
.arg( nbMatches > 1 ? "es" : "" );
break;
case SearchState::FileTruncated:
text = tr("File truncated on disk, previous search results are not valid anymore.");
break;
}
searchInfoLine->setPalette( searchInfoLineDefaultPalette );
searchInfoLine->setText( text );
}
//
// SearchState implementation
//
void CrawlerWidget::SearchState::resetState()
{
state_ = NoSearch;
}
void CrawlerWidget::SearchState::setAutorefresh( bool refresh )
{
autoRefreshRequested_ = refresh;
if ( refresh ) {
if ( state_ == Static )
state_ = Autorefreshing;
}
else {
if ( state_ == Autorefreshing )
state_ = Static;
}
}
void CrawlerWidget::SearchState::truncateFile()
{
state_ = FileTruncated;
}
void CrawlerWidget::SearchState::changeExpression()
{
if ( state_ == Autorefreshing )
state_ = Static;
}
void CrawlerWidget::SearchState::stopSearch()
{
if ( state_ == Autorefreshing )
state_ = Static;
}
void CrawlerWidget::SearchState::startSearch()
{
if ( autoRefreshRequested_ )
state_ = Autorefreshing;
else
state_ = Static;
}
|