1 /* 2 * Copyright (C) 2009, 2010, 2011, 2013, 2014 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 // This file implements MainWindow. It is responsible for creating and 21 // managing the menus, the toolbar, and the CrawlerWidget. It also 22 // load/save the settings on opening/closing of the app 23 24 #include <iostream> 25 #include <cassert> 26 27 #include <QAction> 28 #include <QDesktopWidget> 29 #include <QMenuBar> 30 #include <QToolBar> 31 #include <QFileInfo> 32 #include <QFileDialog> 33 #include <QClipboard> 34 #include <QMessageBox> 35 #include <QCloseEvent> 36 #include <QDragEnterEvent> 37 #include <QMimeData> 38 #include <QUrl> 39 40 #include "log.h" 41 42 #include "mainwindow.h" 43 44 #include "sessioninfo.h" 45 #include "recentfiles.h" 46 #include "crawlerwidget.h" 47 #include "filtersdialog.h" 48 #include "optionsdialog.h" 49 #include "persistentinfo.h" 50 #include "menuactiontooltipbehavior.h" 51 #include "tabbedcrawlerwidget.h" 52 53 // Returns the size in human readable format 54 static QString readableSize( qint64 size ); 55 56 MainWindow::MainWindow( std::unique_ptr<Session> session ) : 57 session_( std::move( session ) ), 58 recentFiles( Persistent<RecentFiles>( "recentFiles" ) ), 59 mainIcon_(), 60 signalMux_(), 61 quickFindMux_( session_->getQuickFindPattern() ), 62 mainTabWidget_() 63 { 64 createActions(); 65 createMenus(); 66 // createContextMenu(); 67 createToolBars(); 68 // createStatusBar(); 69 70 setAcceptDrops( true ); 71 72 // Default geometry 73 const QRect geometry = QApplication::desktop()->availableGeometry( this ); 74 setGeometry( geometry.x() + 20, geometry.y() + 40, 75 geometry.width() - 140, geometry.height() - 140 ); 76 77 mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" ); 78 mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" ); 79 mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" ); 80 mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" ); 81 82 setWindowIcon( mainIcon_ ); 83 84 readSettings(); 85 86 // Connect the signals to the mux (they will be forwarded to the 87 // "current" crawlerwidget 88 89 // Send actions to the crawlerwidget 90 signalMux_.connect( this, SIGNAL( followSet( bool ) ), 91 SIGNAL( followSet( bool ) ) ); 92 signalMux_.connect( this, SIGNAL( optionsChanged() ), 93 SLOT( applyConfiguration() ) ); 94 signalMux_.connect( this, SIGNAL( enteringQuickFind() ), 95 SLOT( enteringQuickFind() ) ); 96 signalMux_.connect( &quickFindWidget_, SIGNAL( close() ), 97 SLOT( exitingQuickFind() ) ); 98 99 // Actions from the CrawlerWidget 100 signalMux_.connect( SIGNAL( followDisabled() ), 101 this, SLOT( disableFollow() ) ); 102 signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 103 this, SLOT( lineNumberHandler( int ) ) ); 104 105 // Register for progress status bar 106 signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 107 this, SLOT( updateLoadingProgress( int ) ) ); 108 signalMux_.connect( SIGNAL( loadingFinished( bool ) ), 109 this, SLOT( displayNormalStatus( bool ) ) ); 110 111 // Configure the main tabbed widget 112 mainTabWidget_.setDocumentMode( true ); 113 mainTabWidget_.setMovable( true ); 114 //mainTabWidget_.setTabShape( QTabWidget::Triangular ); 115 mainTabWidget_.setTabsClosable( true ); 116 117 connect( &mainTabWidget_, SIGNAL( tabCloseRequested ( int ) ), 118 this, SLOT( closeTab( int ) ) ); 119 120 // Establish the QuickFindWidget and mux ( to send requests from the 121 // QFWidget to the right window ) 122 connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 123 &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 124 connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 125 &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 126 connect( &quickFindWidget_, SIGNAL( cancelSearch() ), 127 &quickFindMux_, SLOT( cancelSearch() ) ); 128 connect( &quickFindWidget_, SIGNAL( searchForward() ), 129 &quickFindMux_, SLOT( searchForward() ) ); 130 connect( &quickFindWidget_, SIGNAL( searchBackward() ), 131 &quickFindMux_, SLOT( searchBackward() ) ); 132 connect( &quickFindWidget_, SIGNAL( searchNext() ), 133 &quickFindMux_, SLOT( searchNext() ) ); 134 135 // QuickFind changes coming from the views 136 connect( &quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 137 this, SLOT( changeQFPattern( const QString& ) ) ); 138 connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 139 &quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 140 connect( &quickFindMux_, SIGNAL( clearNotification() ), 141 &quickFindWidget_, SLOT( clearNotification() ) ); 142 143 // Construct the QuickFind bar 144 quickFindWidget_.hide(); 145 146 QWidget* central_widget = new QWidget(); 147 QVBoxLayout* main_layout = new QVBoxLayout(); 148 main_layout->setContentsMargins( 0, 0, 0, 0 ); 149 main_layout->addWidget( &mainTabWidget_ ); 150 main_layout->addWidget( &quickFindWidget_ ); 151 central_widget->setLayout( main_layout ); 152 153 setCentralWidget( central_widget ); 154 } 155 156 void MainWindow::reloadSession() 157 { 158 int current_file_index = -1; 159 160 for ( auto open_file: session_->restore( 161 []() { return new CrawlerWidget(); }, 162 ¤t_file_index ) ) 163 { 164 QString file_name = { open_file.first.c_str() }; 165 CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 166 open_file.second ); 167 168 assert( crawler_widget ); 169 170 mainTabWidget_.addTab( crawler_widget, strippedName( file_name ) ); 171 } 172 173 if ( current_file_index >= 0 ) 174 mainTabWidget_.setCurrentIndex( current_file_index ); 175 } 176 177 void MainWindow::loadInitialFile( QString fileName ) 178 { 179 LOG(logDEBUG) << "loadInitialFile"; 180 181 // Is there a file passed as argument? 182 if ( !fileName.isEmpty() ) 183 loadFile( fileName ); 184 else if ( !previousFile.isEmpty() ) 185 loadFile( previousFile ); 186 } 187 188 // 189 // Private functions 190 // 191 192 // Menu actions 193 void MainWindow::createActions() 194 { 195 Configuration& config = Persistent<Configuration>( "settings" ); 196 197 openAction = new QAction(tr("&Open..."), this); 198 openAction->setShortcut(QKeySequence::Open); 199 openAction->setIcon( QIcon(":/images/open16.png") ); 200 openAction->setStatusTip(tr("Open a file")); 201 connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 202 203 // Recent files 204 for (int i = 0; i < MaxRecentFiles; ++i) { 205 recentFileActions[i] = new QAction(this); 206 recentFileActions[i]->setVisible(false); 207 connect(recentFileActions[i], SIGNAL(triggered()), 208 this, SLOT(openRecentFile())); 209 } 210 211 exitAction = new QAction(tr("E&xit"), this); 212 exitAction->setShortcut(tr("Ctrl+Q")); 213 exitAction->setStatusTip(tr("Exit the application")); 214 connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) ); 215 216 copyAction = new QAction(tr("&Copy"), this); 217 copyAction->setShortcut(QKeySequence::Copy); 218 copyAction->setStatusTip(tr("Copy the selection")); 219 connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) ); 220 221 selectAllAction = new QAction(tr("Select &All"), this); 222 selectAllAction->setShortcut(tr("Ctrl+A")); 223 selectAllAction->setStatusTip(tr("Select all the text")); 224 connect( selectAllAction, SIGNAL(triggered()), 225 this, SLOT( selectAll() ) ); 226 227 findAction = new QAction(tr("&Find..."), this); 228 findAction->setShortcut(QKeySequence::Find); 229 findAction->setStatusTip(tr("Find the text")); 230 connect( findAction, SIGNAL(triggered()), 231 this, SLOT( find() ) ); 232 233 overviewVisibleAction = new QAction( tr("Matches &overview"), this ); 234 overviewVisibleAction->setCheckable( true ); 235 overviewVisibleAction->setChecked( config.isOverviewVisible() ); 236 connect( overviewVisibleAction, SIGNAL( toggled( bool ) ), 237 this, SLOT( toggleOverviewVisibility( bool )) ); 238 239 lineNumbersVisibleInMainAction = 240 new QAction( tr("Line &numbers in main view"), this ); 241 lineNumbersVisibleInMainAction->setCheckable( true ); 242 lineNumbersVisibleInMainAction->setChecked( config.mainLineNumbersVisible() ); 243 connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ), 244 this, SLOT( toggleMainLineNumbersVisibility( bool )) ); 245 246 lineNumbersVisibleInFilteredAction = 247 new QAction( tr("Line &numbers in filtered view"), this ); 248 lineNumbersVisibleInFilteredAction->setCheckable( true ); 249 lineNumbersVisibleInFilteredAction->setChecked( config.filteredLineNumbersVisible() ); 250 connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ), 251 this, SLOT( toggleFilteredLineNumbersVisibility( bool )) ); 252 253 followAction = new QAction( tr("&Follow File"), this ); 254 followAction->setShortcut(Qt::Key_F); 255 followAction->setCheckable(true); 256 connect( followAction, SIGNAL(toggled( bool )), 257 this, SIGNAL(followSet( bool )) ); 258 259 reloadAction = new QAction( tr("&Reload"), this ); 260 reloadAction->setShortcut(QKeySequence::Refresh); 261 reloadAction->setIcon( QIcon(":/images/reload16.png") ); 262 signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) ); 263 264 stopAction = new QAction( tr("&Stop"), this ); 265 stopAction->setIcon( QIcon(":/images/stop16.png") ); 266 stopAction->setEnabled( false ); 267 signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) ); 268 269 filtersAction = new QAction(tr("&Filters..."), this); 270 filtersAction->setStatusTip(tr("Show the Filters box")); 271 connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) ); 272 273 optionsAction = new QAction(tr("&Options..."), this); 274 optionsAction->setStatusTip(tr("Show the Options box")); 275 connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) ); 276 277 aboutAction = new QAction(tr("&About"), this); 278 aboutAction->setStatusTip(tr("Show the About box")); 279 connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) ); 280 281 aboutQtAction = new QAction(tr("About &Qt"), this); 282 aboutAction->setStatusTip(tr("Show the Qt library's About box")); 283 connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) ); 284 } 285 286 void MainWindow::createMenus() 287 { 288 fileMenu = menuBar()->addMenu( tr("&File") ); 289 fileMenu->addAction( openAction ); 290 fileMenu->addSeparator(); 291 for (int i = 0; i < MaxRecentFiles; ++i) { 292 fileMenu->addAction( recentFileActions[i] ); 293 recentFileActionBehaviors[i] = 294 new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this); 295 } 296 fileMenu->addSeparator(); 297 fileMenu->addAction( exitAction ); 298 299 editMenu = menuBar()->addMenu( tr("&Edit") ); 300 editMenu->addAction( copyAction ); 301 editMenu->addAction( selectAllAction ); 302 editMenu->addSeparator(); 303 editMenu->addAction( findAction ); 304 305 viewMenu = menuBar()->addMenu( tr("&View") ); 306 viewMenu->addAction( overviewVisibleAction ); 307 viewMenu->addSeparator(); 308 viewMenu->addAction( lineNumbersVisibleInMainAction ); 309 viewMenu->addAction( lineNumbersVisibleInFilteredAction ); 310 viewMenu->addSeparator(); 311 viewMenu->addAction( followAction ); 312 viewMenu->addSeparator(); 313 viewMenu->addAction( reloadAction ); 314 315 toolsMenu = menuBar()->addMenu( tr("&Tools") ); 316 toolsMenu->addAction( filtersAction ); 317 toolsMenu->addSeparator(); 318 toolsMenu->addAction( optionsAction ); 319 320 menuBar()->addSeparator(); 321 322 helpMenu = menuBar()->addMenu( tr("&Help") ); 323 helpMenu->addAction( aboutAction ); 324 } 325 326 void MainWindow::createToolBars() 327 { 328 infoLine = new InfoLine(); 329 infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 330 infoLine->setLineWidth( 0 ); 331 332 lineNbField = new QLabel( ); 333 lineNbField->setText( "Line 0" ); 334 lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 335 lineNbField->setMinimumSize( 336 lineNbField->fontMetrics().size( 0, "Line 0000000") ); 337 338 toolBar = addToolBar( tr("&Toolbar") ); 339 toolBar->setIconSize( QSize( 16, 16 ) ); 340 toolBar->setMovable( false ); 341 toolBar->addAction( openAction ); 342 toolBar->addAction( reloadAction ); 343 toolBar->addWidget( infoLine ); 344 toolBar->addAction( stopAction ); 345 toolBar->addWidget( lineNbField ); 346 } 347 348 // 349 // Slots 350 // 351 352 // Opens the file selection dialog to select a new log file 353 void MainWindow::open() 354 { 355 QString defaultDir = "."; 356 357 // Default to the path of the current file if there is one 358 if ( !currentFile.isEmpty() ) { 359 QFileInfo fileInfo = QFileInfo( currentFile ); 360 defaultDir = fileInfo.path(); 361 } 362 363 QString fileName = QFileDialog::getOpenFileName(this, 364 tr("Open file"), defaultDir, tr("All files (*)")); 365 if (!fileName.isEmpty()) 366 loadFile(fileName); 367 } 368 369 // Opens a log file from the recent files list 370 void MainWindow::openRecentFile() 371 { 372 QAction* action = qobject_cast<QAction*>(sender()); 373 if (action) 374 loadFile(action->data().toString()); 375 } 376 377 // Select all the text in the currently selected view 378 void MainWindow::selectAll() 379 { 380 CrawlerWidget* current = currentCrawlerWidget(); 381 382 if ( current ) 383 current->selectAll(); 384 } 385 386 // Copy the currently selected line into the clipboard 387 void MainWindow::copy() 388 { 389 static QClipboard* clipboard = QApplication::clipboard(); 390 CrawlerWidget* current = currentCrawlerWidget(); 391 392 if ( current ) { 393 clipboard->setText( current->getSelectedText() ); 394 395 // Put it in the global selection as well (X11 only) 396 clipboard->setText( current->getSelectedText(), 397 QClipboard::Selection ); 398 } 399 } 400 401 // Display the QuickFind bar 402 void MainWindow::find() 403 { 404 displayQuickFindBar( QuickFindMux::Forward ); 405 } 406 407 // Opens the 'Filters' dialog box 408 void MainWindow::filters() 409 { 410 FiltersDialog dialog(this); 411 signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 412 dialog.exec(); 413 signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 414 } 415 416 // Opens the 'Options' modal dialog box 417 void MainWindow::options() 418 { 419 OptionsDialog dialog(this); 420 signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 421 dialog.exec(); 422 signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 423 } 424 425 // Opens the 'About' dialog box. 426 void MainWindow::about() 427 { 428 QMessageBox::about(this, tr("About glogg"), 429 tr("<h2>glogg " GLOGG_VERSION "</h2>" 430 "<p>A fast, advanced log explorer." 431 #ifdef GLOGG_COMMIT 432 "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 433 #endif 434 "<p>Copyright © 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors" 435 "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 436 } 437 438 // Opens the 'About Qt' dialog box. 439 void MainWindow::aboutQt() 440 { 441 } 442 443 void MainWindow::toggleOverviewVisibility( bool isVisible ) 444 { 445 Configuration& config = Persistent<Configuration>( "settings" ); 446 config.setOverviewVisible( isVisible ); 447 emit optionsChanged(); 448 } 449 450 void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 451 { 452 Configuration& config = Persistent<Configuration>( "settings" ); 453 config.setMainLineNumbersVisible( isVisible ); 454 emit optionsChanged(); 455 } 456 457 void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 458 { 459 Configuration& config = Persistent<Configuration>( "settings" ); 460 config.setFilteredLineNumbersVisible( isVisible ); 461 emit optionsChanged(); 462 } 463 464 void MainWindow::disableFollow() 465 { 466 followAction->setChecked( false ); 467 } 468 469 void MainWindow::lineNumberHandler( int line ) 470 { 471 // The line number received is the internal (starts at 0) 472 lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 473 } 474 475 void MainWindow::updateLoadingProgress( int progress ) 476 { 477 LOG(logDEBUG) << "Loading progress: " << progress; 478 479 // We ignore 0% and 100% to avoid a flash when the file (or update) 480 // is very short. 481 if ( progress > 0 && progress < 100 ) { 482 infoLine->setText( loadingFileName + tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 483 infoLine->displayGauge( progress ); 484 485 stopAction->setEnabled( true ); 486 } 487 } 488 489 void MainWindow::displayNormalStatus( bool success ) 490 { 491 QLocale defaultLocale; 492 493 LOG(logDEBUG) << "displayNormalStatus"; 494 495 if ( success ) 496 setCurrentFile( loadingFileName ); 497 498 uint64_t fileSize; 499 uint32_t fileNbLine; 500 QDateTime lastModified; 501 502 session_->getFileInfo( currentCrawlerWidget(), 503 &fileSize, &fileNbLine, &lastModified ); 504 if ( lastModified.isValid() ) { 505 const QString date = 506 defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 507 infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 508 .arg(currentFile).arg(readableSize(fileSize)) 509 .arg(fileNbLine).arg( date ) ); 510 } 511 else { 512 infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 513 .arg(currentFile).arg(readableSize(fileSize)) 514 .arg(fileNbLine) ); 515 } 516 517 infoLine->hideGauge(); 518 stopAction->setEnabled( false ); 519 520 // Now everything is ready, we can finally show the file! 521 currentCrawlerWidget()->show(); 522 mainTabWidget_.setEnabled( true ); 523 } 524 525 void MainWindow::closeTab( int index ) 526 { 527 auto widget = dynamic_cast<CrawlerWidget*>( 528 mainTabWidget_.widget( index ) ); 529 530 assert( widget ); 531 532 mainTabWidget_.removeTab( index ); 533 session_->close( widget ); 534 delete widget; 535 } 536 537 void MainWindow::changeQFPattern( const QString& newPattern ) 538 { 539 quickFindWidget_.changeDisplayedPattern( newPattern ); 540 } 541 542 // 543 // Events 544 // 545 546 // Closes the application 547 void MainWindow::closeEvent( QCloseEvent *event ) 548 { 549 writeSettings(); 550 event->accept(); 551 } 552 553 // Accepts the drag event if it looks like a filename 554 void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 555 { 556 if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 557 event->acceptProposedAction(); 558 } 559 560 // Tries and loads the file if the URL dropped is local 561 void MainWindow::dropEvent( QDropEvent* event ) 562 { 563 QList<QUrl> urls = event->mimeData()->urls(); 564 if ( urls.isEmpty() ) 565 return; 566 567 QString fileName = urls.first().toLocalFile(); 568 if ( fileName.isEmpty() ) 569 return; 570 571 loadFile( fileName ); 572 } 573 574 void MainWindow::keyPressEvent( QKeyEvent* keyEvent ) 575 { 576 LOG(logDEBUG4) << "keyPressEvent received"; 577 578 switch ( (keyEvent->text())[0].toLatin1() ) { 579 case '/': 580 displayQuickFindBar( QuickFindMux::Forward ); 581 break; 582 case '?': 583 displayQuickFindBar( QuickFindMux::Backward ); 584 break; 585 default: 586 keyEvent->ignore(); 587 } 588 589 if ( !keyEvent->isAccepted() ) 590 QMainWindow::keyPressEvent( keyEvent ); 591 } 592 593 // 594 // Private functions 595 // 596 597 // Create a CrawlerWidget for the passed file, start its loading 598 // and update the title bar. 599 // The loading is done asynchronously. 600 bool MainWindow::loadFile( const QString& fileName ) 601 { 602 LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 603 604 // Load the file 605 loadingFileName = fileName; 606 607 CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 608 session_->open( fileName.toStdString(), 609 []() { return new CrawlerWidget(); } ) ); 610 assert( crawler_widget ); 611 612 // We won't show the widget until the file is fully loaded 613 crawler_widget->hide(); 614 615 // We disable the tab widget to avoid having someone switch 616 // tab during loading. (maybe FIXME) 617 mainTabWidget_.setEnabled( false ); 618 619 int index = mainTabWidget_.addTab( crawler_widget, strippedName( fileName ) ); 620 621 // Setting the new tab, the user will see a blank page for the duration 622 // of the loading, with no way to switch to another tab 623 mainTabWidget_.setCurrentIndex( index ); 624 625 signalMux_.setCurrentDocument( crawler_widget ); 626 quickFindMux_.registerSelector( crawler_widget ); 627 628 // FIXME: is it necessary? 629 emit optionsChanged(); 630 631 // We start with the empty file 632 setCurrentFile( "" ); 633 634 LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 635 return true; 636 } 637 638 // Strips the passed filename from its directory part. 639 QString MainWindow::strippedName( const QString& fullFileName ) const 640 { 641 return QFileInfo( fullFileName ).fileName(); 642 } 643 644 // Return the currently active CrawlerWidget, or NULL if none 645 CrawlerWidget* MainWindow::currentCrawlerWidget() const 646 { 647 auto current = dynamic_cast<CrawlerWidget*>( 648 mainTabWidget_.currentWidget() ); 649 650 return current; 651 } 652 653 // Add the filename to the recent files list and update the title bar. 654 void MainWindow::setCurrentFile( const QString& fileName ) 655 { 656 if ( fileName != currentFile ) 657 { 658 // Change the current file 659 currentFile = fileName; 660 QString shownName = tr( "Untitled" ); 661 if ( !currentFile.isEmpty() ) { 662 // (reload the list first in case another glogg changed it) 663 GetPersistentInfo().retrieve( "recentFiles" ); 664 recentFiles.addRecent( currentFile ); 665 GetPersistentInfo().save( "recentFiles" ); 666 updateRecentFileActions(); 667 shownName = strippedName( currentFile ); 668 } 669 670 setWindowTitle( 671 tr("%1 - %2").arg(shownName).arg(tr("glogg")) 672 #ifdef GLOGG_COMMIT 673 + " (dev build " GLOGG_VERSION ")" 674 #endif 675 ); 676 } 677 else 678 { 679 // Nothing, happens when e.g., the file is reloaded 680 } 681 } 682 683 // Updates the actions for the recent files. 684 // Must be called after having added a new name to the list. 685 void MainWindow::updateRecentFileActions() 686 { 687 QStringList recent_files = recentFiles.recentFiles(); 688 689 for ( int j = 0; j < MaxRecentFiles; ++j ) { 690 if ( j < recent_files.count() ) { 691 QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 692 recentFileActions[j]->setText( text ); 693 recentFileActions[j]->setToolTip( recent_files[j] ); 694 recentFileActions[j]->setData( recent_files[j] ); 695 recentFileActions[j]->setVisible( true ); 696 } 697 else { 698 recentFileActions[j]->setVisible( false ); 699 } 700 } 701 702 // separatorAction->setVisible(!recentFiles.isEmpty()); 703 } 704 705 // Write settings to permanent storage 706 void MainWindow::writeSettings() 707 { 708 // Save the session 709 // Generate the ordered list of widgets and their topLine 710 std::vector<std::pair<const ViewInterface*, uint64_t>> widget_list; 711 for ( int i = 0; i < mainTabWidget_.count(); ++i ) 712 widget_list.push_back( { 713 dynamic_cast<const ViewInterface*>( mainTabWidget_.widget( i ) ), 714 0 } ); 715 session_->save( widget_list ); 716 //SessionInfo& session = Persistent<SessionInfo>( "session" ); 717 //session.setGeometry( saveGeometry() ); 718 //session.setCrawlerState( crawlerWidget->saveState() ); 719 //GetPersistentInfo().save( QString( "session" ) ); 720 721 // User settings 722 GetPersistentInfo().save( QString( "settings" ) ); 723 } 724 725 // Read settings from permanent storage 726 void MainWindow::readSettings() 727 { 728 // Get and restore the session 729 // GetPersistentInfo().retrieve( QString( "session" ) ); 730 // SessionInfo session = Persistent<SessionInfo>( "session" ); 731 //restoreGeometry( session.geometry() ); 732 /* 733 * FIXME: should be in the session 734 crawlerWidget->restoreState( session.crawlerState() ); 735 */ 736 737 // History of recent files 738 GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 739 updateRecentFileActions(); 740 741 // GetPersistentInfo().retrieve( QString( "settings" ) ); 742 GetPersistentInfo().retrieve( QString( "filterSet" ) ); 743 } 744 745 void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction ) 746 { 747 LOG(logDEBUG) << "MainWindow::displayQuickFindBar"; 748 749 // Warn crawlers so they can save the position of the focus in order 750 // to do incremental search in the right view. 751 emit enteringQuickFind(); 752 753 quickFindMux_.setDirection( direction ); 754 quickFindWidget_.userActivate(); 755 } 756 757 // Returns the size in human readable format 758 static QString readableSize( qint64 size ) 759 { 760 static const QString sizeStrs[] = { 761 QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 762 QObject::tr("GiB"), QObject::tr("TiB") }; 763 764 QLocale defaultLocale; 765 unsigned int i; 766 double humanSize = size; 767 768 for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 769 humanSize /= 1024.0; 770 771 if ( humanSize >= 1024.0 ) { 772 humanSize /= 1024.0; 773 i++; 774 } 775 776 QString output; 777 if ( i == 0 ) 778 // No decimal part if we display straight bytes. 779 output = defaultLocale.toString( (int) humanSize ); 780 else 781 output = defaultLocale.toString( humanSize, 'f', 1 ); 782 783 output += QString(" ") + sizeStrs[i]; 784 785 return output; 786 } 787