1bb02e0acSNicolas Bonnefon /* 2093a1bf6SNicolas Bonnefon * Copyright (C) 2009, 2010, 2011, 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 MainWindow. It is responsible for creating and 21bb02e0acSNicolas Bonnefon // managing the menus, the toolbar, and the CrawlerWidget. It also 22bb02e0acSNicolas Bonnefon // load/save the settings on opening/closing of the app 23bb02e0acSNicolas Bonnefon 24bb02e0acSNicolas Bonnefon #include <iostream> 251b5e406eSNicolas Bonnefon #include <cassert> 26eb6a8f99SNicolas Bonnefon 27eb6a8f99SNicolas Bonnefon #include <QAction> 28eb6a8f99SNicolas Bonnefon #include <QDesktopWidget> 29eb6a8f99SNicolas Bonnefon #include <QMenuBar> 30eb6a8f99SNicolas Bonnefon #include <QToolBar> 31eb6a8f99SNicolas Bonnefon #include <QFileInfo> 32eb6a8f99SNicolas Bonnefon #include <QFileDialog> 33eb6a8f99SNicolas Bonnefon #include <QClipboard> 34eb6a8f99SNicolas Bonnefon #include <QMessageBox> 35eb6a8f99SNicolas Bonnefon #include <QCloseEvent> 36eb6a8f99SNicolas Bonnefon #include <QDragEnterEvent> 37eb6a8f99SNicolas Bonnefon #include <QMimeData> 38eb6a8f99SNicolas Bonnefon #include <QUrl> 39bb02e0acSNicolas Bonnefon 40bb02e0acSNicolas Bonnefon #include "log.h" 41bb02e0acSNicolas Bonnefon 42bb02e0acSNicolas Bonnefon #include "mainwindow.h" 43bb02e0acSNicolas Bonnefon 44bb02e0acSNicolas Bonnefon #include "sessioninfo.h" 45bb02e0acSNicolas Bonnefon #include "recentfiles.h" 46bb02e0acSNicolas Bonnefon #include "crawlerwidget.h" 47bb02e0acSNicolas Bonnefon #include "filtersdialog.h" 48bb02e0acSNicolas Bonnefon #include "optionsdialog.h" 49bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 50bb02e0acSNicolas Bonnefon #include "menuactiontooltipbehavior.h" 51093a1bf6SNicolas Bonnefon #include "tabbedcrawlerwidget.h" 52bb02e0acSNicolas Bonnefon 530a90ca6aSNicolas Bonnefon // Returns the size in human readable format 540a90ca6aSNicolas Bonnefon static QString readableSize( qint64 size ); 550a90ca6aSNicolas Bonnefon 56d96f3f21SNicolas Bonnefon MainWindow::MainWindow( std::unique_ptr<Session> session ) : 57d96f3f21SNicolas Bonnefon session_( std::move( session ) ), 5811582726SNicolas Bonnefon recentFiles_( Persistent<RecentFiles>( "recentFiles" ) ), 59313a820fSNicolas Bonnefon mainIcon_(), 6027ddfd3aSNicolas Bonnefon signalMux_(), 61b423cd88SNicolas Bonnefon quickFindMux_( session_->getQuickFindPattern() ), 62093a1bf6SNicolas Bonnefon mainTabWidget_() 63bb02e0acSNicolas Bonnefon { 64bb02e0acSNicolas Bonnefon createActions(); 65bb02e0acSNicolas Bonnefon createMenus(); 66bb02e0acSNicolas Bonnefon createToolBars(); 67bb02e0acSNicolas Bonnefon // createStatusBar(); 68bb02e0acSNicolas Bonnefon 69bb02e0acSNicolas Bonnefon setAcceptDrops( true ); 70bb02e0acSNicolas Bonnefon 71bb02e0acSNicolas Bonnefon // Default geometry 72bb02e0acSNicolas Bonnefon const QRect geometry = QApplication::desktop()->availableGeometry( this ); 73bb02e0acSNicolas Bonnefon setGeometry( geometry.x() + 20, geometry.y() + 40, 74bb02e0acSNicolas Bonnefon geometry.width() - 140, geometry.height() - 140 ); 75bb02e0acSNicolas Bonnefon 76bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" ); 77bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" ); 78bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" ); 79bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" ); 80bb02e0acSNicolas Bonnefon 81bb02e0acSNicolas Bonnefon setWindowIcon( mainIcon_ ); 82f0708ca8SNicolas Bonnefon 830a90ca6aSNicolas Bonnefon readSettings(); 840a90ca6aSNicolas Bonnefon 8527ddfd3aSNicolas Bonnefon // Connect the signals to the mux (they will be forwarded to the 8627ddfd3aSNicolas Bonnefon // "current" crawlerwidget 8727ddfd3aSNicolas Bonnefon 8827ddfd3aSNicolas Bonnefon // Send actions to the crawlerwidget 8927ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( followSet( bool ) ), 9027ddfd3aSNicolas Bonnefon SIGNAL( followSet( bool ) ) ); 9127ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( optionsChanged() ), 9227ddfd3aSNicolas Bonnefon SLOT( applyConfiguration() ) ); 938570d8d2SNicolas Bonnefon signalMux_.connect( this, SIGNAL( enteringQuickFind() ), 948570d8d2SNicolas Bonnefon SLOT( enteringQuickFind() ) ); 958570d8d2SNicolas Bonnefon signalMux_.connect( &quickFindWidget_, SIGNAL( close() ), 968570d8d2SNicolas Bonnefon SLOT( exitingQuickFind() ) ); 9727ddfd3aSNicolas Bonnefon 9827ddfd3aSNicolas Bonnefon // Actions from the CrawlerWidget 9927ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( followDisabled() ), 10027ddfd3aSNicolas Bonnefon this, SLOT( disableFollow() ) ); 10127ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 10227ddfd3aSNicolas Bonnefon this, SLOT( lineNumberHandler( int ) ) ); 10327ddfd3aSNicolas Bonnefon 10427ddfd3aSNicolas Bonnefon // Register for progress status bar 10527ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 10627ddfd3aSNicolas Bonnefon this, SLOT( updateLoadingProgress( int ) ) ); 10727ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( loadingFinished( bool ) ), 10827ddfd3aSNicolas Bonnefon this, SLOT( displayNormalStatus( bool ) ) ); 10927ddfd3aSNicolas Bonnefon 110cdd89779SNicolas Bonnefon // Configure the main tabbed widget 111093a1bf6SNicolas Bonnefon mainTabWidget_.setDocumentMode( true ); 112cdd89779SNicolas Bonnefon mainTabWidget_.setMovable( true ); 113093a1bf6SNicolas Bonnefon //mainTabWidget_.setTabShape( QTabWidget::Triangular ); 114cdd89779SNicolas Bonnefon mainTabWidget_.setTabsClosable( true ); 115cdd89779SNicolas Bonnefon 116cdd89779SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( tabCloseRequested( int ) ), 117cdd89779SNicolas Bonnefon this, SLOT( closeTab( int ) ) ); 118ee835f33SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( currentChanged( int ) ), 119ee835f33SNicolas Bonnefon this, SLOT( currentTabChanged( int ) ) ); 120b423cd88SNicolas Bonnefon 121b423cd88SNicolas Bonnefon // Establish the QuickFindWidget and mux ( to send requests from the 122b423cd88SNicolas Bonnefon // QFWidget to the right window ) 123b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 124b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 125b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 126b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 127b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( cancelSearch() ), 128b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( cancelSearch() ) ); 129b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchForward() ), 130b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchForward() ) ); 131b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchBackward() ), 132b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchBackward() ) ); 133b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchNext() ), 134b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchNext() ) ); 135b423cd88SNicolas Bonnefon 136b423cd88SNicolas Bonnefon // QuickFind changes coming from the views 1378570d8d2SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 138b423cd88SNicolas Bonnefon this, SLOT( changeQFPattern( const QString& ) ) ); 139b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 140b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 141b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( clearNotification() ), 142b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( clearNotification() ) ); 143b423cd88SNicolas Bonnefon 144b423cd88SNicolas Bonnefon // Construct the QuickFind bar 145b423cd88SNicolas Bonnefon quickFindWidget_.hide(); 146b423cd88SNicolas Bonnefon 147b423cd88SNicolas Bonnefon QWidget* central_widget = new QWidget(); 148b423cd88SNicolas Bonnefon QVBoxLayout* main_layout = new QVBoxLayout(); 149548acbf6SNicolas Bonnefon main_layout->setContentsMargins( 0, 0, 0, 0 ); 150b423cd88SNicolas Bonnefon main_layout->addWidget( &mainTabWidget_ ); 151b423cd88SNicolas Bonnefon main_layout->addWidget( &quickFindWidget_ ); 152b423cd88SNicolas Bonnefon central_widget->setLayout( main_layout ); 153b423cd88SNicolas Bonnefon 154b423cd88SNicolas Bonnefon setCentralWidget( central_widget ); 155bb02e0acSNicolas Bonnefon } 156bb02e0acSNicolas Bonnefon 1578964a9adSNicolas Bonnefon void MainWindow::reloadSession() 1588964a9adSNicolas Bonnefon { 1598964a9adSNicolas Bonnefon int current_file_index = -1; 1608964a9adSNicolas Bonnefon 1618964a9adSNicolas Bonnefon for ( auto open_file: session_->restore( 1628964a9adSNicolas Bonnefon []() { return new CrawlerWidget(); }, 1638964a9adSNicolas Bonnefon ¤t_file_index ) ) 1648964a9adSNicolas Bonnefon { 1658964a9adSNicolas Bonnefon QString file_name = { open_file.first.c_str() }; 1668964a9adSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 1678964a9adSNicolas Bonnefon open_file.second ); 1688964a9adSNicolas Bonnefon 1698964a9adSNicolas Bonnefon assert( crawler_widget ); 1708964a9adSNicolas Bonnefon 1718964a9adSNicolas Bonnefon mainTabWidget_.addTab( crawler_widget, strippedName( file_name ) ); 1728964a9adSNicolas Bonnefon } 1738964a9adSNicolas Bonnefon 1748964a9adSNicolas Bonnefon if ( current_file_index >= 0 ) 1758964a9adSNicolas Bonnefon mainTabWidget_.setCurrentIndex( current_file_index ); 1768964a9adSNicolas Bonnefon } 1778964a9adSNicolas Bonnefon 178bb02e0acSNicolas Bonnefon void MainWindow::loadInitialFile( QString fileName ) 179bb02e0acSNicolas Bonnefon { 180bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadInitialFile"; 181bb02e0acSNicolas Bonnefon 182bb02e0acSNicolas Bonnefon // Is there a file passed as argument? 183bb02e0acSNicolas Bonnefon if ( !fileName.isEmpty() ) 184bb02e0acSNicolas Bonnefon loadFile( fileName ); 185bb02e0acSNicolas Bonnefon } 186bb02e0acSNicolas Bonnefon 187bb02e0acSNicolas Bonnefon // 188bb02e0acSNicolas Bonnefon // Private functions 189bb02e0acSNicolas Bonnefon // 190bb02e0acSNicolas Bonnefon 191bb02e0acSNicolas Bonnefon // Menu actions 192bb02e0acSNicolas Bonnefon void MainWindow::createActions() 193bb02e0acSNicolas Bonnefon { 19411582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 19511582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 196bb02e0acSNicolas Bonnefon 197bb02e0acSNicolas Bonnefon openAction = new QAction(tr("&Open..."), this); 198bb02e0acSNicolas Bonnefon openAction->setShortcut(QKeySequence::Open); 199bb02e0acSNicolas Bonnefon openAction->setIcon( QIcon(":/images/open16.png") ); 200bb02e0acSNicolas Bonnefon openAction->setStatusTip(tr("Open a file")); 201bb02e0acSNicolas Bonnefon connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 202bb02e0acSNicolas Bonnefon 203bb02e0acSNicolas Bonnefon // Recent files 204bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 205bb02e0acSNicolas Bonnefon recentFileActions[i] = new QAction(this); 206bb02e0acSNicolas Bonnefon recentFileActions[i]->setVisible(false); 207bb02e0acSNicolas Bonnefon connect(recentFileActions[i], SIGNAL(triggered()), 208bb02e0acSNicolas Bonnefon this, SLOT(openRecentFile())); 209bb02e0acSNicolas Bonnefon } 210bb02e0acSNicolas Bonnefon 211bb02e0acSNicolas Bonnefon exitAction = new QAction(tr("E&xit"), this); 212bb02e0acSNicolas Bonnefon exitAction->setShortcut(tr("Ctrl+Q")); 213bb02e0acSNicolas Bonnefon exitAction->setStatusTip(tr("Exit the application")); 214bb02e0acSNicolas Bonnefon connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) ); 215bb02e0acSNicolas Bonnefon 216bb02e0acSNicolas Bonnefon copyAction = new QAction(tr("&Copy"), this); 217bb02e0acSNicolas Bonnefon copyAction->setShortcut(QKeySequence::Copy); 218bb02e0acSNicolas Bonnefon copyAction->setStatusTip(tr("Copy the selection")); 219bb02e0acSNicolas Bonnefon connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) ); 220bb02e0acSNicolas Bonnefon 221bb02e0acSNicolas Bonnefon selectAllAction = new QAction(tr("Select &All"), this); 222bb02e0acSNicolas Bonnefon selectAllAction->setShortcut(tr("Ctrl+A")); 223bb02e0acSNicolas Bonnefon selectAllAction->setStatusTip(tr("Select all the text")); 224bb02e0acSNicolas Bonnefon connect( selectAllAction, SIGNAL(triggered()), 225bb02e0acSNicolas Bonnefon this, SLOT( selectAll() ) ); 226bb02e0acSNicolas Bonnefon 227bb02e0acSNicolas Bonnefon findAction = new QAction(tr("&Find..."), this); 228bb02e0acSNicolas Bonnefon findAction->setShortcut(QKeySequence::Find); 229bb02e0acSNicolas Bonnefon findAction->setStatusTip(tr("Find the text")); 230bb02e0acSNicolas Bonnefon connect( findAction, SIGNAL(triggered()), 231bb02e0acSNicolas Bonnefon this, SLOT( find() ) ); 232bb02e0acSNicolas Bonnefon 233bb02e0acSNicolas Bonnefon overviewVisibleAction = new QAction( tr("Matches &overview"), this ); 234bb02e0acSNicolas Bonnefon overviewVisibleAction->setCheckable( true ); 23511582726SNicolas Bonnefon overviewVisibleAction->setChecked( config->isOverviewVisible() ); 236bb02e0acSNicolas Bonnefon connect( overviewVisibleAction, SIGNAL( toggled( bool ) ), 237bb02e0acSNicolas Bonnefon this, SLOT( toggleOverviewVisibility( bool )) ); 238bb02e0acSNicolas Bonnefon 239bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction = 240bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in main view"), this ); 241bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction->setCheckable( true ); 24211582726SNicolas Bonnefon lineNumbersVisibleInMainAction->setChecked( config->mainLineNumbersVisible() ); 243bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ), 244bb02e0acSNicolas Bonnefon this, SLOT( toggleMainLineNumbersVisibility( bool )) ); 245bb02e0acSNicolas Bonnefon 246bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction = 247bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in filtered view"), this ); 248bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction->setCheckable( true ); 24911582726SNicolas Bonnefon lineNumbersVisibleInFilteredAction->setChecked( config->filteredLineNumbersVisible() ); 250bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ), 251bb02e0acSNicolas Bonnefon this, SLOT( toggleFilteredLineNumbersVisibility( bool )) ); 252bb02e0acSNicolas Bonnefon 253bb02e0acSNicolas Bonnefon followAction = new QAction( tr("&Follow File"), this ); 254bb02e0acSNicolas Bonnefon followAction->setShortcut(Qt::Key_F); 255bb02e0acSNicolas Bonnefon followAction->setCheckable(true); 256bb02e0acSNicolas Bonnefon connect( followAction, SIGNAL(toggled( bool )), 257bb02e0acSNicolas Bonnefon this, SIGNAL(followSet( bool )) ); 258bb02e0acSNicolas Bonnefon 259bb02e0acSNicolas Bonnefon reloadAction = new QAction( tr("&Reload"), this ); 260bb02e0acSNicolas Bonnefon reloadAction->setShortcut(QKeySequence::Refresh); 261bb02e0acSNicolas Bonnefon reloadAction->setIcon( QIcon(":/images/reload16.png") ); 26232e44cfdSNicolas Bonnefon signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) ); 263bb02e0acSNicolas Bonnefon 264bb02e0acSNicolas Bonnefon stopAction = new QAction( tr("&Stop"), this ); 265bb02e0acSNicolas Bonnefon stopAction->setIcon( QIcon(":/images/stop16.png") ); 266bb02e0acSNicolas Bonnefon stopAction->setEnabled( false ); 2677847299cSNicolas Bonnefon signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) ); 268bb02e0acSNicolas Bonnefon 269bb02e0acSNicolas Bonnefon filtersAction = new QAction(tr("&Filters..."), this); 270bb02e0acSNicolas Bonnefon filtersAction->setStatusTip(tr("Show the Filters box")); 271bb02e0acSNicolas Bonnefon connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) ); 272bb02e0acSNicolas Bonnefon 273bb02e0acSNicolas Bonnefon optionsAction = new QAction(tr("&Options..."), this); 274bb02e0acSNicolas Bonnefon optionsAction->setStatusTip(tr("Show the Options box")); 275bb02e0acSNicolas Bonnefon connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) ); 276bb02e0acSNicolas Bonnefon 277bb02e0acSNicolas Bonnefon aboutAction = new QAction(tr("&About"), this); 278bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the About box")); 279bb02e0acSNicolas Bonnefon connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) ); 280bb02e0acSNicolas Bonnefon 281bb02e0acSNicolas Bonnefon aboutQtAction = new QAction(tr("About &Qt"), this); 282bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the Qt library's About box")); 283bb02e0acSNicolas Bonnefon connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) ); 284bb02e0acSNicolas Bonnefon } 285bb02e0acSNicolas Bonnefon 286bb02e0acSNicolas Bonnefon void MainWindow::createMenus() 287bb02e0acSNicolas Bonnefon { 288bb02e0acSNicolas Bonnefon fileMenu = menuBar()->addMenu( tr("&File") ); 289bb02e0acSNicolas Bonnefon fileMenu->addAction( openAction ); 290bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 291bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 292bb02e0acSNicolas Bonnefon fileMenu->addAction( recentFileActions[i] ); 293bb02e0acSNicolas Bonnefon recentFileActionBehaviors[i] = 294bb02e0acSNicolas Bonnefon new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this); 295bb02e0acSNicolas Bonnefon } 296bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 297bb02e0acSNicolas Bonnefon fileMenu->addAction( exitAction ); 298bb02e0acSNicolas Bonnefon 299bb02e0acSNicolas Bonnefon editMenu = menuBar()->addMenu( tr("&Edit") ); 300bb02e0acSNicolas Bonnefon editMenu->addAction( copyAction ); 301bb02e0acSNicolas Bonnefon editMenu->addAction( selectAllAction ); 302bb02e0acSNicolas Bonnefon editMenu->addSeparator(); 303bb02e0acSNicolas Bonnefon editMenu->addAction( findAction ); 304bb02e0acSNicolas Bonnefon 305bb02e0acSNicolas Bonnefon viewMenu = menuBar()->addMenu( tr("&View") ); 306bb02e0acSNicolas Bonnefon viewMenu->addAction( overviewVisibleAction ); 307bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 308bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInMainAction ); 309bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInFilteredAction ); 310bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 311bb02e0acSNicolas Bonnefon viewMenu->addAction( followAction ); 312bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 313bb02e0acSNicolas Bonnefon viewMenu->addAction( reloadAction ); 314bb02e0acSNicolas Bonnefon 315bb02e0acSNicolas Bonnefon toolsMenu = menuBar()->addMenu( tr("&Tools") ); 316bb02e0acSNicolas Bonnefon toolsMenu->addAction( filtersAction ); 317bb02e0acSNicolas Bonnefon toolsMenu->addSeparator(); 318bb02e0acSNicolas Bonnefon toolsMenu->addAction( optionsAction ); 319bb02e0acSNicolas Bonnefon 320bb02e0acSNicolas Bonnefon menuBar()->addSeparator(); 321bb02e0acSNicolas Bonnefon 322bb02e0acSNicolas Bonnefon helpMenu = menuBar()->addMenu( tr("&Help") ); 323bb02e0acSNicolas Bonnefon helpMenu->addAction( aboutAction ); 324bb02e0acSNicolas Bonnefon } 325bb02e0acSNicolas Bonnefon 326bb02e0acSNicolas Bonnefon void MainWindow::createToolBars() 327bb02e0acSNicolas Bonnefon { 328bb02e0acSNicolas Bonnefon infoLine = new InfoLine(); 329bb02e0acSNicolas Bonnefon infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 330bb02e0acSNicolas Bonnefon infoLine->setLineWidth( 0 ); 331bb02e0acSNicolas Bonnefon 332bb02e0acSNicolas Bonnefon lineNbField = new QLabel( ); 333bb02e0acSNicolas Bonnefon lineNbField->setText( "Line 0" ); 334bb02e0acSNicolas Bonnefon lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 335bb02e0acSNicolas Bonnefon lineNbField->setMinimumSize( 336bb02e0acSNicolas Bonnefon lineNbField->fontMetrics().size( 0, "Line 0000000") ); 337bb02e0acSNicolas Bonnefon 338bb02e0acSNicolas Bonnefon toolBar = addToolBar( tr("&Toolbar") ); 339bb02e0acSNicolas Bonnefon toolBar->setIconSize( QSize( 16, 16 ) ); 340bb02e0acSNicolas Bonnefon toolBar->setMovable( false ); 341bb02e0acSNicolas Bonnefon toolBar->addAction( openAction ); 342bb02e0acSNicolas Bonnefon toolBar->addAction( reloadAction ); 343bb02e0acSNicolas Bonnefon toolBar->addWidget( infoLine ); 344bb02e0acSNicolas Bonnefon toolBar->addAction( stopAction ); 345bb02e0acSNicolas Bonnefon toolBar->addWidget( lineNbField ); 346bb02e0acSNicolas Bonnefon } 347bb02e0acSNicolas Bonnefon 348bb02e0acSNicolas Bonnefon // 349bb02e0acSNicolas Bonnefon // Slots 350bb02e0acSNicolas Bonnefon // 351bb02e0acSNicolas Bonnefon 352bb02e0acSNicolas Bonnefon // Opens the file selection dialog to select a new log file 353bb02e0acSNicolas Bonnefon void MainWindow::open() 354bb02e0acSNicolas Bonnefon { 355bb02e0acSNicolas Bonnefon QString defaultDir = "."; 356bb02e0acSNicolas Bonnefon 357bb02e0acSNicolas Bonnefon // Default to the path of the current file if there is one 3580e97f16dSNicolas Bonnefon if ( auto current = currentCrawlerWidget() ) 3590e97f16dSNicolas Bonnefon { 3600e97f16dSNicolas Bonnefon std::string current_file = session_->getFilename( current ); 3610e97f16dSNicolas Bonnefon QFileInfo fileInfo = QFileInfo( QString( current_file.c_str() ) ); 362bb02e0acSNicolas Bonnefon defaultDir = fileInfo.path(); 363bb02e0acSNicolas Bonnefon } 364bb02e0acSNicolas Bonnefon 365bb02e0acSNicolas Bonnefon QString fileName = QFileDialog::getOpenFileName(this, 366bb02e0acSNicolas Bonnefon tr("Open file"), defaultDir, tr("All files (*)")); 367bb02e0acSNicolas Bonnefon if (!fileName.isEmpty()) 368bb02e0acSNicolas Bonnefon loadFile(fileName); 369bb02e0acSNicolas Bonnefon } 370bb02e0acSNicolas Bonnefon 371bb02e0acSNicolas Bonnefon // Opens a log file from the recent files list 372bb02e0acSNicolas Bonnefon void MainWindow::openRecentFile() 373bb02e0acSNicolas Bonnefon { 374bb02e0acSNicolas Bonnefon QAction* action = qobject_cast<QAction*>(sender()); 375bb02e0acSNicolas Bonnefon if (action) 376bb02e0acSNicolas Bonnefon loadFile(action->data().toString()); 377bb02e0acSNicolas Bonnefon } 378bb02e0acSNicolas Bonnefon 379bb02e0acSNicolas Bonnefon // Select all the text in the currently selected view 380bb02e0acSNicolas Bonnefon void MainWindow::selectAll() 381bb02e0acSNicolas Bonnefon { 38227ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 38327ddfd3aSNicolas Bonnefon 38427ddfd3aSNicolas Bonnefon if ( current ) 38527ddfd3aSNicolas Bonnefon current->selectAll(); 386bb02e0acSNicolas Bonnefon } 387bb02e0acSNicolas Bonnefon 388bb02e0acSNicolas Bonnefon // Copy the currently selected line into the clipboard 389bb02e0acSNicolas Bonnefon void MainWindow::copy() 390bb02e0acSNicolas Bonnefon { 391bb02e0acSNicolas Bonnefon static QClipboard* clipboard = QApplication::clipboard(); 39227ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 393bb02e0acSNicolas Bonnefon 39427ddfd3aSNicolas Bonnefon if ( current ) { 39527ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText() ); 396bb02e0acSNicolas Bonnefon 397bb02e0acSNicolas Bonnefon // Put it in the global selection as well (X11 only) 39827ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText(), 399bb02e0acSNicolas Bonnefon QClipboard::Selection ); 400bb02e0acSNicolas Bonnefon } 40127ddfd3aSNicolas Bonnefon } 402bb02e0acSNicolas Bonnefon 403bb02e0acSNicolas Bonnefon // Display the QuickFind bar 404bb02e0acSNicolas Bonnefon void MainWindow::find() 405bb02e0acSNicolas Bonnefon { 406b423cd88SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 407bb02e0acSNicolas Bonnefon } 408bb02e0acSNicolas Bonnefon 409bb02e0acSNicolas Bonnefon // Opens the 'Filters' dialog box 410bb02e0acSNicolas Bonnefon void MainWindow::filters() 411bb02e0acSNicolas Bonnefon { 412bb02e0acSNicolas Bonnefon FiltersDialog dialog(this); 41327ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 414bb02e0acSNicolas Bonnefon dialog.exec(); 41527ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 416bb02e0acSNicolas Bonnefon } 417bb02e0acSNicolas Bonnefon 418bb02e0acSNicolas Bonnefon // Opens the 'Options' modal dialog box 419bb02e0acSNicolas Bonnefon void MainWindow::options() 420bb02e0acSNicolas Bonnefon { 421bb02e0acSNicolas Bonnefon OptionsDialog dialog(this); 42227ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 423bb02e0acSNicolas Bonnefon dialog.exec(); 42427ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 425bb02e0acSNicolas Bonnefon } 426bb02e0acSNicolas Bonnefon 427bb02e0acSNicolas Bonnefon // Opens the 'About' dialog box. 428bb02e0acSNicolas Bonnefon void MainWindow::about() 429bb02e0acSNicolas Bonnefon { 430bb02e0acSNicolas Bonnefon QMessageBox::about(this, tr("About glogg"), 431bb02e0acSNicolas Bonnefon tr("<h2>glogg " GLOGG_VERSION "</h2>" 432bb02e0acSNicolas Bonnefon "<p>A fast, advanced log explorer." 433bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 434bb02e0acSNicolas Bonnefon "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 435bb02e0acSNicolas Bonnefon #endif 436*a1202e0cSNicolas Bonnefon "<p>Copyright © 2009, 2010, 2011, 2012, 2013, 2014 Nicolas Bonnefon and other contributors" 437bb02e0acSNicolas Bonnefon "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 438bb02e0acSNicolas Bonnefon } 439bb02e0acSNicolas Bonnefon 440bb02e0acSNicolas Bonnefon // Opens the 'About Qt' dialog box. 441bb02e0acSNicolas Bonnefon void MainWindow::aboutQt() 442bb02e0acSNicolas Bonnefon { 443bb02e0acSNicolas Bonnefon } 444bb02e0acSNicolas Bonnefon 445bb02e0acSNicolas Bonnefon void MainWindow::toggleOverviewVisibility( bool isVisible ) 446bb02e0acSNicolas Bonnefon { 44711582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 44811582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 44911582726SNicolas Bonnefon config->setOverviewVisible( isVisible ); 450bb02e0acSNicolas Bonnefon emit optionsChanged(); 451bb02e0acSNicolas Bonnefon } 452bb02e0acSNicolas Bonnefon 453bb02e0acSNicolas Bonnefon void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 454bb02e0acSNicolas Bonnefon { 45511582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 45611582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 45711582726SNicolas Bonnefon config->setMainLineNumbersVisible( isVisible ); 458bb02e0acSNicolas Bonnefon emit optionsChanged(); 459bb02e0acSNicolas Bonnefon } 460bb02e0acSNicolas Bonnefon 461bb02e0acSNicolas Bonnefon void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 462bb02e0acSNicolas Bonnefon { 46311582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 46411582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 46511582726SNicolas Bonnefon config->setFilteredLineNumbersVisible( isVisible ); 466bb02e0acSNicolas Bonnefon emit optionsChanged(); 467bb02e0acSNicolas Bonnefon } 468bb02e0acSNicolas Bonnefon 469bb02e0acSNicolas Bonnefon void MainWindow::disableFollow() 470bb02e0acSNicolas Bonnefon { 471bb02e0acSNicolas Bonnefon followAction->setChecked( false ); 472bb02e0acSNicolas Bonnefon } 473bb02e0acSNicolas Bonnefon 474bb02e0acSNicolas Bonnefon void MainWindow::lineNumberHandler( int line ) 475bb02e0acSNicolas Bonnefon { 476bb02e0acSNicolas Bonnefon // The line number received is the internal (starts at 0) 477bb02e0acSNicolas Bonnefon lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 478bb02e0acSNicolas Bonnefon } 479bb02e0acSNicolas Bonnefon 480bb02e0acSNicolas Bonnefon void MainWindow::updateLoadingProgress( int progress ) 481bb02e0acSNicolas Bonnefon { 482bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Loading progress: " << progress; 483bb02e0acSNicolas Bonnefon 4840e97f16dSNicolas Bonnefon // FIXME - should check and set loadingFileName 4850e97f16dSNicolas Bonnefon 486bb02e0acSNicolas Bonnefon // We ignore 0% and 100% to avoid a flash when the file (or update) 487bb02e0acSNicolas Bonnefon // is very short. 488bb02e0acSNicolas Bonnefon if ( progress > 0 && progress < 100 ) { 489bb02e0acSNicolas Bonnefon infoLine->setText( loadingFileName + tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 490bb02e0acSNicolas Bonnefon infoLine->displayGauge( progress ); 491bb02e0acSNicolas Bonnefon 492bb02e0acSNicolas Bonnefon stopAction->setEnabled( true ); 493bb02e0acSNicolas Bonnefon } 494bb02e0acSNicolas Bonnefon } 495bb02e0acSNicolas Bonnefon 496bb02e0acSNicolas Bonnefon void MainWindow::displayNormalStatus( bool success ) 497bb02e0acSNicolas Bonnefon { 498bb02e0acSNicolas Bonnefon QLocale defaultLocale; 499bb02e0acSNicolas Bonnefon 500bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "displayNormalStatus"; 501bb02e0acSNicolas Bonnefon 5020e97f16dSNicolas Bonnefon // No file is loading 5030e97f16dSNicolas Bonnefon loadingFileName.clear(); 5040e97f16dSNicolas Bonnefon 5050e97f16dSNicolas Bonnefon // Following should always work as we will only receive enter 5060e97f16dSNicolas Bonnefon // this slot if there is a crawler connected. 5070e97f16dSNicolas Bonnefon QString current_file = 5080e97f16dSNicolas Bonnefon session_->getFilename( currentCrawlerWidget() ).c_str(); 5090e97f16dSNicolas Bonnefon 5100a90ca6aSNicolas Bonnefon uint64_t fileSize; 5110a90ca6aSNicolas Bonnefon uint32_t fileNbLine; 512bb02e0acSNicolas Bonnefon QDateTime lastModified; 513bb02e0acSNicolas Bonnefon 51427ddfd3aSNicolas Bonnefon session_->getFileInfo( currentCrawlerWidget(), 5150a90ca6aSNicolas Bonnefon &fileSize, &fileNbLine, &lastModified ); 516bb02e0acSNicolas Bonnefon if ( lastModified.isValid() ) { 517bb02e0acSNicolas Bonnefon const QString date = 518bb02e0acSNicolas Bonnefon defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 519bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 5200e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 521bb02e0acSNicolas Bonnefon .arg(fileNbLine).arg( date ) ); 522bb02e0acSNicolas Bonnefon } 523bb02e0acSNicolas Bonnefon else { 524bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 5250e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 526bb02e0acSNicolas Bonnefon .arg(fileNbLine) ); 527bb02e0acSNicolas Bonnefon } 528bb02e0acSNicolas Bonnefon 529bb02e0acSNicolas Bonnefon infoLine->hideGauge(); 530bb02e0acSNicolas Bonnefon stopAction->setEnabled( false ); 5310a90ca6aSNicolas Bonnefon 5320a90ca6aSNicolas Bonnefon // Now everything is ready, we can finally show the file! 53327ddfd3aSNicolas Bonnefon currentCrawlerWidget()->show(); 53427ddfd3aSNicolas Bonnefon mainTabWidget_.setEnabled( true ); 535bb02e0acSNicolas Bonnefon } 536bb02e0acSNicolas Bonnefon 537cdd89779SNicolas Bonnefon void MainWindow::closeTab( int index ) 538cdd89779SNicolas Bonnefon { 539cdd89779SNicolas Bonnefon auto widget = dynamic_cast<CrawlerWidget*>( 540cdd89779SNicolas Bonnefon mainTabWidget_.widget( index ) ); 541cdd89779SNicolas Bonnefon 542cdd89779SNicolas Bonnefon assert( widget ); 543cdd89779SNicolas Bonnefon 544cdd89779SNicolas Bonnefon mainTabWidget_.removeTab( index ); 545cdd89779SNicolas Bonnefon session_->close( widget ); 546cdd89779SNicolas Bonnefon delete widget; 547cdd89779SNicolas Bonnefon } 548cdd89779SNicolas Bonnefon 549ee835f33SNicolas Bonnefon void MainWindow::currentTabChanged( int index ) 550ee835f33SNicolas Bonnefon { 551ee835f33SNicolas Bonnefon LOG(logDEBUG) << "currentTabChanged"; 552ee835f33SNicolas Bonnefon 553ee835f33SNicolas Bonnefon if ( index >= 0 ) 554ee835f33SNicolas Bonnefon { 555ee835f33SNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 556ee835f33SNicolas Bonnefon mainTabWidget_.widget( index ) ); 557ee835f33SNicolas Bonnefon signalMux_.setCurrentDocument( crawler_widget ); 558ee835f33SNicolas Bonnefon quickFindMux_.registerSelector( crawler_widget ); 559ee835f33SNicolas Bonnefon 560ee835f33SNicolas Bonnefon // New tab is set up with fonts etc... 561ee835f33SNicolas Bonnefon emit optionsChanged(); 5620e97f16dSNicolas Bonnefon 5630e97f16dSNicolas Bonnefon // Update the title bar 5640e97f16dSNicolas Bonnefon updateTitleBar( QString( 5650e97f16dSNicolas Bonnefon session_->getFilename( crawler_widget ).c_str() ) ); 566ee835f33SNicolas Bonnefon } 567ee835f33SNicolas Bonnefon else 568ee835f33SNicolas Bonnefon { 569*a1202e0cSNicolas Bonnefon // No tab left 570*a1202e0cSNicolas Bonnefon signalMux_.setCurrentDocument( nullptr ); 571*a1202e0cSNicolas Bonnefon quickFindMux_.registerSelector( nullptr ); 572*a1202e0cSNicolas Bonnefon 573*a1202e0cSNicolas Bonnefon infoLine->hideGauge(); 574*a1202e0cSNicolas Bonnefon infoLine->clear(); 575*a1202e0cSNicolas Bonnefon 576*a1202e0cSNicolas Bonnefon updateTitleBar( QString() ); 577ee835f33SNicolas Bonnefon } 578ee835f33SNicolas Bonnefon } 579ee835f33SNicolas Bonnefon 5808570d8d2SNicolas Bonnefon void MainWindow::changeQFPattern( const QString& newPattern ) 5818570d8d2SNicolas Bonnefon { 5828570d8d2SNicolas Bonnefon quickFindWidget_.changeDisplayedPattern( newPattern ); 5838570d8d2SNicolas Bonnefon } 5848570d8d2SNicolas Bonnefon 585bb02e0acSNicolas Bonnefon // 586bb02e0acSNicolas Bonnefon // Events 587bb02e0acSNicolas Bonnefon // 588bb02e0acSNicolas Bonnefon 589bb02e0acSNicolas Bonnefon // Closes the application 590bb02e0acSNicolas Bonnefon void MainWindow::closeEvent( QCloseEvent *event ) 591bb02e0acSNicolas Bonnefon { 592bb02e0acSNicolas Bonnefon writeSettings(); 593bb02e0acSNicolas Bonnefon event->accept(); 594bb02e0acSNicolas Bonnefon } 595bb02e0acSNicolas Bonnefon 596bb02e0acSNicolas Bonnefon // Accepts the drag event if it looks like a filename 597bb02e0acSNicolas Bonnefon void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 598bb02e0acSNicolas Bonnefon { 599bb02e0acSNicolas Bonnefon if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 600bb02e0acSNicolas Bonnefon event->acceptProposedAction(); 601bb02e0acSNicolas Bonnefon } 602bb02e0acSNicolas Bonnefon 603bb02e0acSNicolas Bonnefon // Tries and loads the file if the URL dropped is local 604bb02e0acSNicolas Bonnefon void MainWindow::dropEvent( QDropEvent* event ) 605bb02e0acSNicolas Bonnefon { 606bb02e0acSNicolas Bonnefon QList<QUrl> urls = event->mimeData()->urls(); 607bb02e0acSNicolas Bonnefon if ( urls.isEmpty() ) 608bb02e0acSNicolas Bonnefon return; 609bb02e0acSNicolas Bonnefon 610bb02e0acSNicolas Bonnefon QString fileName = urls.first().toLocalFile(); 611bb02e0acSNicolas Bonnefon if ( fileName.isEmpty() ) 612bb02e0acSNicolas Bonnefon return; 613bb02e0acSNicolas Bonnefon 614bb02e0acSNicolas Bonnefon loadFile( fileName ); 615bb02e0acSNicolas Bonnefon } 616bb02e0acSNicolas Bonnefon 6178570d8d2SNicolas Bonnefon void MainWindow::keyPressEvent( QKeyEvent* keyEvent ) 6188570d8d2SNicolas Bonnefon { 6198570d8d2SNicolas Bonnefon LOG(logDEBUG4) << "keyPressEvent received"; 6208570d8d2SNicolas Bonnefon 6218570d8d2SNicolas Bonnefon switch ( (keyEvent->text())[0].toLatin1() ) { 6228570d8d2SNicolas Bonnefon case '/': 6238570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 6248570d8d2SNicolas Bonnefon break; 6258570d8d2SNicolas Bonnefon case '?': 6268570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Backward ); 6278570d8d2SNicolas Bonnefon break; 6288570d8d2SNicolas Bonnefon default: 6298570d8d2SNicolas Bonnefon keyEvent->ignore(); 6308570d8d2SNicolas Bonnefon } 6318570d8d2SNicolas Bonnefon 6328570d8d2SNicolas Bonnefon if ( !keyEvent->isAccepted() ) 6338570d8d2SNicolas Bonnefon QMainWindow::keyPressEvent( keyEvent ); 6348570d8d2SNicolas Bonnefon } 6358570d8d2SNicolas Bonnefon 636bb02e0acSNicolas Bonnefon // 637bb02e0acSNicolas Bonnefon // Private functions 638bb02e0acSNicolas Bonnefon // 639bb02e0acSNicolas Bonnefon 6400a90ca6aSNicolas Bonnefon // Create a CrawlerWidget for the passed file, start its loading 6410a90ca6aSNicolas Bonnefon // and update the title bar. 642bb02e0acSNicolas Bonnefon // The loading is done asynchronously. 643bb02e0acSNicolas Bonnefon bool MainWindow::loadFile( const QString& fileName ) 644bb02e0acSNicolas Bonnefon { 645bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 646bb02e0acSNicolas Bonnefon 647bb02e0acSNicolas Bonnefon // Load the file 648bb02e0acSNicolas Bonnefon loadingFileName = fileName; 649039481acSNicolas Bonnefon 650a3b56311SNicolas Bonnefon try { 65127ddfd3aSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 65227ddfd3aSNicolas Bonnefon session_->open( fileName.toStdString(), 6531b5e406eSNicolas Bonnefon []() { return new CrawlerWidget(); } ) ); 6541b5e406eSNicolas Bonnefon assert( crawler_widget ); 655f0708ca8SNicolas Bonnefon 6560a90ca6aSNicolas Bonnefon // We won't show the widget until the file is fully loaded 65727ddfd3aSNicolas Bonnefon crawler_widget->hide(); 658f0708ca8SNicolas Bonnefon 65927ddfd3aSNicolas Bonnefon // We disable the tab widget to avoid having someone switch 66027ddfd3aSNicolas Bonnefon // tab during loading. (maybe FIXME) 66127ddfd3aSNicolas Bonnefon mainTabWidget_.setEnabled( false ); 662313a820fSNicolas Bonnefon 663a3b56311SNicolas Bonnefon int index = mainTabWidget_.addTab( 664a3b56311SNicolas Bonnefon crawler_widget, strippedName( fileName ) ); 665f0708ca8SNicolas Bonnefon 66627ddfd3aSNicolas Bonnefon // Setting the new tab, the user will see a blank page for the duration 66727ddfd3aSNicolas Bonnefon // of the loading, with no way to switch to another tab 66827ddfd3aSNicolas Bonnefon mainTabWidget_.setCurrentIndex( index ); 66927ddfd3aSNicolas Bonnefon 67060864ff5SNicolas Bonnefon // Update the recent files list 67160864ff5SNicolas Bonnefon // (reload the list first in case another glogg changed it) 67260864ff5SNicolas Bonnefon GetPersistentInfo().retrieve( "recentFiles" ); 67311582726SNicolas Bonnefon recentFiles_->addRecent( fileName ); 67460864ff5SNicolas Bonnefon GetPersistentInfo().save( "recentFiles" ); 67560864ff5SNicolas Bonnefon updateRecentFileActions(); 676a3b56311SNicolas Bonnefon } 677a3b56311SNicolas Bonnefon catch ( FileUnreadableErr ) { 678a3b56311SNicolas Bonnefon LOG(logDEBUG) << "Can't open file " << fileName.toStdString(); 679a3b56311SNicolas Bonnefon return false; 680a3b56311SNicolas Bonnefon } 68160864ff5SNicolas Bonnefon 682bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 683bb02e0acSNicolas Bonnefon return true; 684a3b56311SNicolas Bonnefon 685bb02e0acSNicolas Bonnefon } 686bb02e0acSNicolas Bonnefon 687bb02e0acSNicolas Bonnefon // Strips the passed filename from its directory part. 688bb02e0acSNicolas Bonnefon QString MainWindow::strippedName( const QString& fullFileName ) const 689bb02e0acSNicolas Bonnefon { 690bb02e0acSNicolas Bonnefon return QFileInfo( fullFileName ).fileName(); 691bb02e0acSNicolas Bonnefon } 692bb02e0acSNicolas Bonnefon 69327ddfd3aSNicolas Bonnefon // Return the currently active CrawlerWidget, or NULL if none 69427ddfd3aSNicolas Bonnefon CrawlerWidget* MainWindow::currentCrawlerWidget() const 69527ddfd3aSNicolas Bonnefon { 69627ddfd3aSNicolas Bonnefon auto current = dynamic_cast<CrawlerWidget*>( 69727ddfd3aSNicolas Bonnefon mainTabWidget_.currentWidget() ); 69827ddfd3aSNicolas Bonnefon 69927ddfd3aSNicolas Bonnefon return current; 70027ddfd3aSNicolas Bonnefon } 70127ddfd3aSNicolas Bonnefon 7020e97f16dSNicolas Bonnefon // Update the title bar. 7030e97f16dSNicolas Bonnefon void MainWindow::updateTitleBar( const QString& file_name ) 704bb02e0acSNicolas Bonnefon { 705bb02e0acSNicolas Bonnefon QString shownName = tr( "Untitled" ); 7060e97f16dSNicolas Bonnefon if ( !file_name.isEmpty() ) 7070e97f16dSNicolas Bonnefon shownName = strippedName( file_name ); 708bb02e0acSNicolas Bonnefon 709bb02e0acSNicolas Bonnefon setWindowTitle( 710bb02e0acSNicolas Bonnefon tr("%1 - %2").arg(shownName).arg(tr("glogg")) 711bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 712bb02e0acSNicolas Bonnefon + " (dev build " GLOGG_VERSION ")" 713bb02e0acSNicolas Bonnefon #endif 714bb02e0acSNicolas Bonnefon ); 715bb02e0acSNicolas Bonnefon } 716bb02e0acSNicolas Bonnefon 717bb02e0acSNicolas Bonnefon // Updates the actions for the recent files. 718bb02e0acSNicolas Bonnefon // Must be called after having added a new name to the list. 719bb02e0acSNicolas Bonnefon void MainWindow::updateRecentFileActions() 720bb02e0acSNicolas Bonnefon { 72111582726SNicolas Bonnefon QStringList recent_files = recentFiles_->recentFiles(); 722bb02e0acSNicolas Bonnefon 723bb02e0acSNicolas Bonnefon for ( int j = 0; j < MaxRecentFiles; ++j ) { 724bb02e0acSNicolas Bonnefon if ( j < recent_files.count() ) { 725bb02e0acSNicolas Bonnefon QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 726bb02e0acSNicolas Bonnefon recentFileActions[j]->setText( text ); 727bb02e0acSNicolas Bonnefon recentFileActions[j]->setToolTip( recent_files[j] ); 728bb02e0acSNicolas Bonnefon recentFileActions[j]->setData( recent_files[j] ); 729bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( true ); 730bb02e0acSNicolas Bonnefon } 731bb02e0acSNicolas Bonnefon else { 732bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( false ); 733bb02e0acSNicolas Bonnefon } 734bb02e0acSNicolas Bonnefon } 735bb02e0acSNicolas Bonnefon 736bb02e0acSNicolas Bonnefon // separatorAction->setVisible(!recentFiles.isEmpty()); 737bb02e0acSNicolas Bonnefon } 738bb02e0acSNicolas Bonnefon 739bb02e0acSNicolas Bonnefon // Write settings to permanent storage 740bb02e0acSNicolas Bonnefon void MainWindow::writeSettings() 741bb02e0acSNicolas Bonnefon { 742bb02e0acSNicolas Bonnefon // Save the session 743b57881faSNicolas Bonnefon // Generate the ordered list of widgets and their topLine 744b57881faSNicolas Bonnefon std::vector<std::pair<const ViewInterface*, uint64_t>> widget_list; 745b57881faSNicolas Bonnefon for ( int i = 0; i < mainTabWidget_.count(); ++i ) 746b57881faSNicolas Bonnefon widget_list.push_back( { 747b57881faSNicolas Bonnefon dynamic_cast<const ViewInterface*>( mainTabWidget_.widget( i ) ), 748b57881faSNicolas Bonnefon 0 } ); 749b57881faSNicolas Bonnefon session_->save( widget_list ); 750b57881faSNicolas Bonnefon //SessionInfo& session = Persistent<SessionInfo>( "session" ); 751b57881faSNicolas Bonnefon //session.setGeometry( saveGeometry() ); 75227ddfd3aSNicolas Bonnefon //session.setCrawlerState( crawlerWidget->saveState() ); 753b57881faSNicolas Bonnefon //GetPersistentInfo().save( QString( "session" ) ); 754bb02e0acSNicolas Bonnefon 755bb02e0acSNicolas Bonnefon // User settings 756bb02e0acSNicolas Bonnefon GetPersistentInfo().save( QString( "settings" ) ); 757bb02e0acSNicolas Bonnefon } 758bb02e0acSNicolas Bonnefon 759bb02e0acSNicolas Bonnefon // Read settings from permanent storage 760bb02e0acSNicolas Bonnefon void MainWindow::readSettings() 761bb02e0acSNicolas Bonnefon { 762bb02e0acSNicolas Bonnefon // Get and restore the session 763b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "session" ) ); 764b57881faSNicolas Bonnefon // SessionInfo session = Persistent<SessionInfo>( "session" ); 765b57881faSNicolas Bonnefon //restoreGeometry( session.geometry() ); 7660a90ca6aSNicolas Bonnefon /* 7670a90ca6aSNicolas Bonnefon * FIXME: should be in the session 7680a90ca6aSNicolas Bonnefon crawlerWidget->restoreState( session.crawlerState() ); 7690a90ca6aSNicolas Bonnefon */ 770bb02e0acSNicolas Bonnefon 771bb02e0acSNicolas Bonnefon // History of recent files 772bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 773bb02e0acSNicolas Bonnefon updateRecentFileActions(); 774bb02e0acSNicolas Bonnefon 775b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "settings" ) ); 776bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "filterSet" ) ); 777bb02e0acSNicolas Bonnefon } 778bb02e0acSNicolas Bonnefon 779b423cd88SNicolas Bonnefon void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction ) 780b423cd88SNicolas Bonnefon { 781b423cd88SNicolas Bonnefon LOG(logDEBUG) << "MainWindow::displayQuickFindBar"; 782b423cd88SNicolas Bonnefon 7838570d8d2SNicolas Bonnefon // Warn crawlers so they can save the position of the focus in order 7848570d8d2SNicolas Bonnefon // to do incremental search in the right view. 7858570d8d2SNicolas Bonnefon emit enteringQuickFind(); 786b423cd88SNicolas Bonnefon 787b423cd88SNicolas Bonnefon quickFindMux_.setDirection( direction ); 788b423cd88SNicolas Bonnefon quickFindWidget_.userActivate(); 789b423cd88SNicolas Bonnefon } 790b423cd88SNicolas Bonnefon 791bb02e0acSNicolas Bonnefon // Returns the size in human readable format 7920a90ca6aSNicolas Bonnefon static QString readableSize( qint64 size ) 793bb02e0acSNicolas Bonnefon { 794bb02e0acSNicolas Bonnefon static const QString sizeStrs[] = { 7950a90ca6aSNicolas Bonnefon QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 7960a90ca6aSNicolas Bonnefon QObject::tr("GiB"), QObject::tr("TiB") }; 797bb02e0acSNicolas Bonnefon 798bb02e0acSNicolas Bonnefon QLocale defaultLocale; 799bb02e0acSNicolas Bonnefon unsigned int i; 800bb02e0acSNicolas Bonnefon double humanSize = size; 801bb02e0acSNicolas Bonnefon 802bb02e0acSNicolas Bonnefon for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 803bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 804bb02e0acSNicolas Bonnefon 805bb02e0acSNicolas Bonnefon if ( humanSize >= 1024.0 ) { 806bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 807bb02e0acSNicolas Bonnefon i++; 808bb02e0acSNicolas Bonnefon } 809bb02e0acSNicolas Bonnefon 810bb02e0acSNicolas Bonnefon QString output; 811bb02e0acSNicolas Bonnefon if ( i == 0 ) 812bb02e0acSNicolas Bonnefon // No decimal part if we display straight bytes. 813bb02e0acSNicolas Bonnefon output = defaultLocale.toString( (int) humanSize ); 814bb02e0acSNicolas Bonnefon else 815bb02e0acSNicolas Bonnefon output = defaultLocale.toString( humanSize, 'f', 1 ); 816bb02e0acSNicolas Bonnefon 817bb02e0acSNicolas Bonnefon output += QString(" ") + sizeStrs[i]; 818bb02e0acSNicolas Bonnefon 819bb02e0acSNicolas Bonnefon return output; 820bb02e0acSNicolas Bonnefon } 821