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" 5209aff35dSNicolas Bonnefon #include "externalcom.h" 53bb02e0acSNicolas Bonnefon 540a90ca6aSNicolas Bonnefon // Returns the size in human readable format 550a90ca6aSNicolas Bonnefon static QString readableSize( qint64 size ); 560a90ca6aSNicolas Bonnefon 5709aff35dSNicolas Bonnefon MainWindow::MainWindow( std::unique_ptr<Session> session, 5809aff35dSNicolas Bonnefon std::shared_ptr<ExternalCommunicator> external_communicator ) : 59d96f3f21SNicolas Bonnefon session_( std::move( session ) ), 6009aff35dSNicolas Bonnefon externalCommunicator_( external_communicator ), 6111582726SNicolas Bonnefon recentFiles_( Persistent<RecentFiles>( "recentFiles" ) ), 62313a820fSNicolas Bonnefon mainIcon_(), 6327ddfd3aSNicolas Bonnefon signalMux_(), 64b423cd88SNicolas Bonnefon quickFindMux_( session_->getQuickFindPattern() ), 65093a1bf6SNicolas Bonnefon mainTabWidget_() 66bb02e0acSNicolas Bonnefon { 67bb02e0acSNicolas Bonnefon createActions(); 68bb02e0acSNicolas Bonnefon createMenus(); 69bb02e0acSNicolas Bonnefon createToolBars(); 70bb02e0acSNicolas Bonnefon // createStatusBar(); 71bb02e0acSNicolas Bonnefon 72bb02e0acSNicolas Bonnefon setAcceptDrops( true ); 73bb02e0acSNicolas Bonnefon 74bb02e0acSNicolas Bonnefon // Default geometry 75bb02e0acSNicolas Bonnefon const QRect geometry = QApplication::desktop()->availableGeometry( this ); 76bb02e0acSNicolas Bonnefon setGeometry( geometry.x() + 20, geometry.y() + 40, 77bb02e0acSNicolas Bonnefon geometry.width() - 140, geometry.height() - 140 ); 78bb02e0acSNicolas Bonnefon 79bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" ); 80bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" ); 81bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" ); 82bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" ); 83bb02e0acSNicolas Bonnefon 84bb02e0acSNicolas Bonnefon setWindowIcon( mainIcon_ ); 85f0708ca8SNicolas Bonnefon 860a90ca6aSNicolas Bonnefon readSettings(); 870a90ca6aSNicolas Bonnefon 8827ddfd3aSNicolas Bonnefon // Connect the signals to the mux (they will be forwarded to the 8927ddfd3aSNicolas Bonnefon // "current" crawlerwidget 9027ddfd3aSNicolas Bonnefon 9127ddfd3aSNicolas Bonnefon // Send actions to the crawlerwidget 9227ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( followSet( bool ) ), 9327ddfd3aSNicolas Bonnefon SIGNAL( followSet( bool ) ) ); 9427ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( optionsChanged() ), 9527ddfd3aSNicolas Bonnefon SLOT( applyConfiguration() ) ); 968570d8d2SNicolas Bonnefon signalMux_.connect( this, SIGNAL( enteringQuickFind() ), 978570d8d2SNicolas Bonnefon SLOT( enteringQuickFind() ) ); 988570d8d2SNicolas Bonnefon signalMux_.connect( &quickFindWidget_, SIGNAL( close() ), 998570d8d2SNicolas Bonnefon SLOT( exitingQuickFind() ) ); 10027ddfd3aSNicolas Bonnefon 10127ddfd3aSNicolas Bonnefon // Actions from the CrawlerWidget 10227ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( followDisabled() ), 10327ddfd3aSNicolas Bonnefon this, SLOT( disableFollow() ) ); 10427ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 10527ddfd3aSNicolas Bonnefon this, SLOT( lineNumberHandler( int ) ) ); 10627ddfd3aSNicolas Bonnefon 10727ddfd3aSNicolas Bonnefon // Register for progress status bar 10827ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 10927ddfd3aSNicolas Bonnefon this, SLOT( updateLoadingProgress( int ) ) ); 110812146a8SNicolas Bonnefon signalMux_.connect( SIGNAL( loadingFinished( LoadingStatus ) ), 111812146a8SNicolas Bonnefon this, SLOT( handleLoadingFinished( LoadingStatus ) ) ); 11227ddfd3aSNicolas Bonnefon 113cdd89779SNicolas Bonnefon // Configure the main tabbed widget 114093a1bf6SNicolas Bonnefon mainTabWidget_.setDocumentMode( true ); 115cdd89779SNicolas Bonnefon mainTabWidget_.setMovable( true ); 116093a1bf6SNicolas Bonnefon //mainTabWidget_.setTabShape( QTabWidget::Triangular ); 117cdd89779SNicolas Bonnefon mainTabWidget_.setTabsClosable( true ); 118cdd89779SNicolas Bonnefon 119cdd89779SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( tabCloseRequested( int ) ), 120cdd89779SNicolas Bonnefon this, SLOT( closeTab( int ) ) ); 121ee835f33SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( currentChanged( int ) ), 122ee835f33SNicolas Bonnefon this, SLOT( currentTabChanged( int ) ) ); 123b423cd88SNicolas Bonnefon 124b423cd88SNicolas Bonnefon // Establish the QuickFindWidget and mux ( to send requests from the 125b423cd88SNicolas Bonnefon // QFWidget to the right window ) 126b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 127b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 128b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 129b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 130b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( cancelSearch() ), 131b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( cancelSearch() ) ); 132b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchForward() ), 133b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchForward() ) ); 134b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchBackward() ), 135b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchBackward() ) ); 136b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchNext() ), 137b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchNext() ) ); 138b423cd88SNicolas Bonnefon 139b423cd88SNicolas Bonnefon // QuickFind changes coming from the views 1408570d8d2SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 141b423cd88SNicolas Bonnefon this, SLOT( changeQFPattern( const QString& ) ) ); 142b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 143b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 144b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( clearNotification() ), 145b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( clearNotification() ) ); 146b423cd88SNicolas Bonnefon 14709aff35dSNicolas Bonnefon // Actions from external instances 14809aff35dSNicolas Bonnefon connect( externalCommunicator_.get(), SIGNAL( loadFile( const QString& ) ), 14909aff35dSNicolas Bonnefon this, SLOT( loadFileNonInteractive( const QString& ) ) ); 15009aff35dSNicolas Bonnefon 151b423cd88SNicolas Bonnefon // Construct the QuickFind bar 152b423cd88SNicolas Bonnefon quickFindWidget_.hide(); 153b423cd88SNicolas Bonnefon 154b423cd88SNicolas Bonnefon QWidget* central_widget = new QWidget(); 155b423cd88SNicolas Bonnefon QVBoxLayout* main_layout = new QVBoxLayout(); 156548acbf6SNicolas Bonnefon main_layout->setContentsMargins( 0, 0, 0, 0 ); 157b423cd88SNicolas Bonnefon main_layout->addWidget( &mainTabWidget_ ); 158b423cd88SNicolas Bonnefon main_layout->addWidget( &quickFindWidget_ ); 159b423cd88SNicolas Bonnefon central_widget->setLayout( main_layout ); 160b423cd88SNicolas Bonnefon 161b423cd88SNicolas Bonnefon setCentralWidget( central_widget ); 162bb02e0acSNicolas Bonnefon } 163bb02e0acSNicolas Bonnefon 1648964a9adSNicolas Bonnefon void MainWindow::reloadSession() 1658964a9adSNicolas Bonnefon { 1668964a9adSNicolas Bonnefon int current_file_index = -1; 1678964a9adSNicolas Bonnefon 1688964a9adSNicolas Bonnefon for ( auto open_file: session_->restore( 1698964a9adSNicolas Bonnefon []() { return new CrawlerWidget(); }, 1708964a9adSNicolas Bonnefon ¤t_file_index ) ) 1718964a9adSNicolas Bonnefon { 1728964a9adSNicolas Bonnefon QString file_name = { open_file.first.c_str() }; 1738964a9adSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 1748964a9adSNicolas Bonnefon open_file.second ); 1758964a9adSNicolas Bonnefon 1768964a9adSNicolas Bonnefon assert( crawler_widget ); 1778964a9adSNicolas Bonnefon 1788964a9adSNicolas Bonnefon mainTabWidget_.addTab( crawler_widget, strippedName( file_name ) ); 1798964a9adSNicolas Bonnefon } 1808964a9adSNicolas Bonnefon 1818964a9adSNicolas Bonnefon if ( current_file_index >= 0 ) 1828964a9adSNicolas Bonnefon mainTabWidget_.setCurrentIndex( current_file_index ); 1838964a9adSNicolas Bonnefon } 1848964a9adSNicolas Bonnefon 185bb02e0acSNicolas Bonnefon void MainWindow::loadInitialFile( QString fileName ) 186bb02e0acSNicolas Bonnefon { 187bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadInitialFile"; 188bb02e0acSNicolas Bonnefon 189bb02e0acSNicolas Bonnefon // Is there a file passed as argument? 190bb02e0acSNicolas Bonnefon if ( !fileName.isEmpty() ) 191bb02e0acSNicolas Bonnefon loadFile( fileName ); 192bb02e0acSNicolas Bonnefon } 193bb02e0acSNicolas Bonnefon 194bb02e0acSNicolas Bonnefon // 195bb02e0acSNicolas Bonnefon // Private functions 196bb02e0acSNicolas Bonnefon // 197bb02e0acSNicolas Bonnefon 198bb02e0acSNicolas Bonnefon // Menu actions 199bb02e0acSNicolas Bonnefon void MainWindow::createActions() 200bb02e0acSNicolas Bonnefon { 20111582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 20211582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 203bb02e0acSNicolas Bonnefon 204bb02e0acSNicolas Bonnefon openAction = new QAction(tr("&Open..."), this); 205bb02e0acSNicolas Bonnefon openAction->setShortcut(QKeySequence::Open); 206bb02e0acSNicolas Bonnefon openAction->setIcon( QIcon(":/images/open16.png") ); 207bb02e0acSNicolas Bonnefon openAction->setStatusTip(tr("Open a file")); 208bb02e0acSNicolas Bonnefon connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 209bb02e0acSNicolas Bonnefon 210bb02e0acSNicolas Bonnefon // Recent files 211bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 212bb02e0acSNicolas Bonnefon recentFileActions[i] = new QAction(this); 213bb02e0acSNicolas Bonnefon recentFileActions[i]->setVisible(false); 214bb02e0acSNicolas Bonnefon connect(recentFileActions[i], SIGNAL(triggered()), 215bb02e0acSNicolas Bonnefon this, SLOT(openRecentFile())); 216bb02e0acSNicolas Bonnefon } 217bb02e0acSNicolas Bonnefon 218bb02e0acSNicolas Bonnefon exitAction = new QAction(tr("E&xit"), this); 219bb02e0acSNicolas Bonnefon exitAction->setShortcut(tr("Ctrl+Q")); 220bb02e0acSNicolas Bonnefon exitAction->setStatusTip(tr("Exit the application")); 221bb02e0acSNicolas Bonnefon connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) ); 222bb02e0acSNicolas Bonnefon 223bb02e0acSNicolas Bonnefon copyAction = new QAction(tr("&Copy"), this); 224bb02e0acSNicolas Bonnefon copyAction->setShortcut(QKeySequence::Copy); 225bb02e0acSNicolas Bonnefon copyAction->setStatusTip(tr("Copy the selection")); 226bb02e0acSNicolas Bonnefon connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) ); 227bb02e0acSNicolas Bonnefon 228bb02e0acSNicolas Bonnefon selectAllAction = new QAction(tr("Select &All"), this); 229bb02e0acSNicolas Bonnefon selectAllAction->setShortcut(tr("Ctrl+A")); 230bb02e0acSNicolas Bonnefon selectAllAction->setStatusTip(tr("Select all the text")); 231bb02e0acSNicolas Bonnefon connect( selectAllAction, SIGNAL(triggered()), 232bb02e0acSNicolas Bonnefon this, SLOT( selectAll() ) ); 233bb02e0acSNicolas Bonnefon 234bb02e0acSNicolas Bonnefon findAction = new QAction(tr("&Find..."), this); 235bb02e0acSNicolas Bonnefon findAction->setShortcut(QKeySequence::Find); 236bb02e0acSNicolas Bonnefon findAction->setStatusTip(tr("Find the text")); 237bb02e0acSNicolas Bonnefon connect( findAction, SIGNAL(triggered()), 238bb02e0acSNicolas Bonnefon this, SLOT( find() ) ); 239bb02e0acSNicolas Bonnefon 240bb02e0acSNicolas Bonnefon overviewVisibleAction = new QAction( tr("Matches &overview"), this ); 241bb02e0acSNicolas Bonnefon overviewVisibleAction->setCheckable( true ); 24211582726SNicolas Bonnefon overviewVisibleAction->setChecked( config->isOverviewVisible() ); 243bb02e0acSNicolas Bonnefon connect( overviewVisibleAction, SIGNAL( toggled( bool ) ), 244bb02e0acSNicolas Bonnefon this, SLOT( toggleOverviewVisibility( bool )) ); 245bb02e0acSNicolas Bonnefon 246bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction = 247bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in main view"), this ); 248bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction->setCheckable( true ); 24911582726SNicolas Bonnefon lineNumbersVisibleInMainAction->setChecked( config->mainLineNumbersVisible() ); 250bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ), 251bb02e0acSNicolas Bonnefon this, SLOT( toggleMainLineNumbersVisibility( bool )) ); 252bb02e0acSNicolas Bonnefon 253bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction = 254bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in filtered view"), this ); 255bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction->setCheckable( true ); 25611582726SNicolas Bonnefon lineNumbersVisibleInFilteredAction->setChecked( config->filteredLineNumbersVisible() ); 257bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ), 258bb02e0acSNicolas Bonnefon this, SLOT( toggleFilteredLineNumbersVisibility( bool )) ); 259bb02e0acSNicolas Bonnefon 260bb02e0acSNicolas Bonnefon followAction = new QAction( tr("&Follow File"), this ); 261bb02e0acSNicolas Bonnefon followAction->setShortcut(Qt::Key_F); 262bb02e0acSNicolas Bonnefon followAction->setCheckable(true); 263bb02e0acSNicolas Bonnefon connect( followAction, SIGNAL(toggled( bool )), 264bb02e0acSNicolas Bonnefon this, SIGNAL(followSet( bool )) ); 265bb02e0acSNicolas Bonnefon 266bb02e0acSNicolas Bonnefon reloadAction = new QAction( tr("&Reload"), this ); 267bb02e0acSNicolas Bonnefon reloadAction->setShortcut(QKeySequence::Refresh); 268bb02e0acSNicolas Bonnefon reloadAction->setIcon( QIcon(":/images/reload16.png") ); 26932e44cfdSNicolas Bonnefon signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) ); 270bb02e0acSNicolas Bonnefon 271bb02e0acSNicolas Bonnefon stopAction = new QAction( tr("&Stop"), this ); 272bb02e0acSNicolas Bonnefon stopAction->setIcon( QIcon(":/images/stop16.png") ); 2737875bde2SNicolas Bonnefon stopAction->setEnabled( true ); 2747847299cSNicolas Bonnefon signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) ); 275bb02e0acSNicolas Bonnefon 276bb02e0acSNicolas Bonnefon filtersAction = new QAction(tr("&Filters..."), this); 277bb02e0acSNicolas Bonnefon filtersAction->setStatusTip(tr("Show the Filters box")); 278bb02e0acSNicolas Bonnefon connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) ); 279bb02e0acSNicolas Bonnefon 280bb02e0acSNicolas Bonnefon optionsAction = new QAction(tr("&Options..."), this); 281bb02e0acSNicolas Bonnefon optionsAction->setStatusTip(tr("Show the Options box")); 282bb02e0acSNicolas Bonnefon connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) ); 283bb02e0acSNicolas Bonnefon 284bb02e0acSNicolas Bonnefon aboutAction = new QAction(tr("&About"), this); 285bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the About box")); 286bb02e0acSNicolas Bonnefon connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) ); 287bb02e0acSNicolas Bonnefon 288bb02e0acSNicolas Bonnefon aboutQtAction = new QAction(tr("About &Qt"), this); 289bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the Qt library's About box")); 290bb02e0acSNicolas Bonnefon connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) ); 291bb02e0acSNicolas Bonnefon } 292bb02e0acSNicolas Bonnefon 293bb02e0acSNicolas Bonnefon void MainWindow::createMenus() 294bb02e0acSNicolas Bonnefon { 295bb02e0acSNicolas Bonnefon fileMenu = menuBar()->addMenu( tr("&File") ); 296bb02e0acSNicolas Bonnefon fileMenu->addAction( openAction ); 297bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 298bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 299bb02e0acSNicolas Bonnefon fileMenu->addAction( recentFileActions[i] ); 300bb02e0acSNicolas Bonnefon recentFileActionBehaviors[i] = 301bb02e0acSNicolas Bonnefon new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this); 302bb02e0acSNicolas Bonnefon } 303bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 304bb02e0acSNicolas Bonnefon fileMenu->addAction( exitAction ); 305bb02e0acSNicolas Bonnefon 306bb02e0acSNicolas Bonnefon editMenu = menuBar()->addMenu( tr("&Edit") ); 307bb02e0acSNicolas Bonnefon editMenu->addAction( copyAction ); 308bb02e0acSNicolas Bonnefon editMenu->addAction( selectAllAction ); 309bb02e0acSNicolas Bonnefon editMenu->addSeparator(); 310bb02e0acSNicolas Bonnefon editMenu->addAction( findAction ); 311bb02e0acSNicolas Bonnefon 312bb02e0acSNicolas Bonnefon viewMenu = menuBar()->addMenu( tr("&View") ); 313bb02e0acSNicolas Bonnefon viewMenu->addAction( overviewVisibleAction ); 314bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 315bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInMainAction ); 316bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInFilteredAction ); 317bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 318bb02e0acSNicolas Bonnefon viewMenu->addAction( followAction ); 319bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 320bb02e0acSNicolas Bonnefon viewMenu->addAction( reloadAction ); 321bb02e0acSNicolas Bonnefon 322bb02e0acSNicolas Bonnefon toolsMenu = menuBar()->addMenu( tr("&Tools") ); 323bb02e0acSNicolas Bonnefon toolsMenu->addAction( filtersAction ); 324bb02e0acSNicolas Bonnefon toolsMenu->addSeparator(); 325bb02e0acSNicolas Bonnefon toolsMenu->addAction( optionsAction ); 326bb02e0acSNicolas Bonnefon 327bb02e0acSNicolas Bonnefon menuBar()->addSeparator(); 328bb02e0acSNicolas Bonnefon 329bb02e0acSNicolas Bonnefon helpMenu = menuBar()->addMenu( tr("&Help") ); 330bb02e0acSNicolas Bonnefon helpMenu->addAction( aboutAction ); 331bb02e0acSNicolas Bonnefon } 332bb02e0acSNicolas Bonnefon 333bb02e0acSNicolas Bonnefon void MainWindow::createToolBars() 334bb02e0acSNicolas Bonnefon { 335bb02e0acSNicolas Bonnefon infoLine = new InfoLine(); 336bb02e0acSNicolas Bonnefon infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 337bb02e0acSNicolas Bonnefon infoLine->setLineWidth( 0 ); 338bb02e0acSNicolas Bonnefon 339bb02e0acSNicolas Bonnefon lineNbField = new QLabel( ); 340bb02e0acSNicolas Bonnefon lineNbField->setText( "Line 0" ); 341bb02e0acSNicolas Bonnefon lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 342bb02e0acSNicolas Bonnefon lineNbField->setMinimumSize( 343bb02e0acSNicolas Bonnefon lineNbField->fontMetrics().size( 0, "Line 0000000") ); 344bb02e0acSNicolas Bonnefon 345bb02e0acSNicolas Bonnefon toolBar = addToolBar( tr("&Toolbar") ); 346bb02e0acSNicolas Bonnefon toolBar->setIconSize( QSize( 16, 16 ) ); 347bb02e0acSNicolas Bonnefon toolBar->setMovable( false ); 348bb02e0acSNicolas Bonnefon toolBar->addAction( openAction ); 349bb02e0acSNicolas Bonnefon toolBar->addAction( reloadAction ); 350bb02e0acSNicolas Bonnefon toolBar->addWidget( infoLine ); 351bb02e0acSNicolas Bonnefon toolBar->addAction( stopAction ); 352bb02e0acSNicolas Bonnefon toolBar->addWidget( lineNbField ); 353bb02e0acSNicolas Bonnefon } 354bb02e0acSNicolas Bonnefon 355bb02e0acSNicolas Bonnefon // 356bb02e0acSNicolas Bonnefon // Slots 357bb02e0acSNicolas Bonnefon // 358bb02e0acSNicolas Bonnefon 359bb02e0acSNicolas Bonnefon // Opens the file selection dialog to select a new log file 360bb02e0acSNicolas Bonnefon void MainWindow::open() 361bb02e0acSNicolas Bonnefon { 362bb02e0acSNicolas Bonnefon QString defaultDir = "."; 363bb02e0acSNicolas Bonnefon 364bb02e0acSNicolas Bonnefon // Default to the path of the current file if there is one 3650e97f16dSNicolas Bonnefon if ( auto current = currentCrawlerWidget() ) 3660e97f16dSNicolas Bonnefon { 3670e97f16dSNicolas Bonnefon std::string current_file = session_->getFilename( current ); 3680e97f16dSNicolas Bonnefon QFileInfo fileInfo = QFileInfo( QString( current_file.c_str() ) ); 369bb02e0acSNicolas Bonnefon defaultDir = fileInfo.path(); 370bb02e0acSNicolas Bonnefon } 371bb02e0acSNicolas Bonnefon 372bb02e0acSNicolas Bonnefon QString fileName = QFileDialog::getOpenFileName(this, 373bb02e0acSNicolas Bonnefon tr("Open file"), defaultDir, tr("All files (*)")); 374bb02e0acSNicolas Bonnefon if (!fileName.isEmpty()) 375bb02e0acSNicolas Bonnefon loadFile(fileName); 376bb02e0acSNicolas Bonnefon } 377bb02e0acSNicolas Bonnefon 378bb02e0acSNicolas Bonnefon // Opens a log file from the recent files list 379bb02e0acSNicolas Bonnefon void MainWindow::openRecentFile() 380bb02e0acSNicolas Bonnefon { 381bb02e0acSNicolas Bonnefon QAction* action = qobject_cast<QAction*>(sender()); 382bb02e0acSNicolas Bonnefon if (action) 383bb02e0acSNicolas Bonnefon loadFile(action->data().toString()); 384bb02e0acSNicolas Bonnefon } 385bb02e0acSNicolas Bonnefon 386bb02e0acSNicolas Bonnefon // Select all the text in the currently selected view 387bb02e0acSNicolas Bonnefon void MainWindow::selectAll() 388bb02e0acSNicolas Bonnefon { 38927ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 39027ddfd3aSNicolas Bonnefon 39127ddfd3aSNicolas Bonnefon if ( current ) 39227ddfd3aSNicolas Bonnefon current->selectAll(); 393bb02e0acSNicolas Bonnefon } 394bb02e0acSNicolas Bonnefon 395bb02e0acSNicolas Bonnefon // Copy the currently selected line into the clipboard 396bb02e0acSNicolas Bonnefon void MainWindow::copy() 397bb02e0acSNicolas Bonnefon { 398bb02e0acSNicolas Bonnefon static QClipboard* clipboard = QApplication::clipboard(); 39927ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 400bb02e0acSNicolas Bonnefon 40127ddfd3aSNicolas Bonnefon if ( current ) { 40227ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText() ); 403bb02e0acSNicolas Bonnefon 404bb02e0acSNicolas Bonnefon // Put it in the global selection as well (X11 only) 40527ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText(), 406bb02e0acSNicolas Bonnefon QClipboard::Selection ); 407bb02e0acSNicolas Bonnefon } 40827ddfd3aSNicolas Bonnefon } 409bb02e0acSNicolas Bonnefon 410bb02e0acSNicolas Bonnefon // Display the QuickFind bar 411bb02e0acSNicolas Bonnefon void MainWindow::find() 412bb02e0acSNicolas Bonnefon { 413b423cd88SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 414bb02e0acSNicolas Bonnefon } 415bb02e0acSNicolas Bonnefon 416bb02e0acSNicolas Bonnefon // Opens the 'Filters' dialog box 417bb02e0acSNicolas Bonnefon void MainWindow::filters() 418bb02e0acSNicolas Bonnefon { 419bb02e0acSNicolas Bonnefon FiltersDialog dialog(this); 42027ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 421bb02e0acSNicolas Bonnefon dialog.exec(); 42227ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 423bb02e0acSNicolas Bonnefon } 424bb02e0acSNicolas Bonnefon 425bb02e0acSNicolas Bonnefon // Opens the 'Options' modal dialog box 426bb02e0acSNicolas Bonnefon void MainWindow::options() 427bb02e0acSNicolas Bonnefon { 428bb02e0acSNicolas Bonnefon OptionsDialog dialog(this); 42927ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 430bb02e0acSNicolas Bonnefon dialog.exec(); 43127ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 432bb02e0acSNicolas Bonnefon } 433bb02e0acSNicolas Bonnefon 434bb02e0acSNicolas Bonnefon // Opens the 'About' dialog box. 435bb02e0acSNicolas Bonnefon void MainWindow::about() 436bb02e0acSNicolas Bonnefon { 437bb02e0acSNicolas Bonnefon QMessageBox::about(this, tr("About glogg"), 438bb02e0acSNicolas Bonnefon tr("<h2>glogg " GLOGG_VERSION "</h2>" 439bb02e0acSNicolas Bonnefon "<p>A fast, advanced log explorer." 440bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 441bb02e0acSNicolas Bonnefon "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 442bb02e0acSNicolas Bonnefon #endif 443*35c20658SNicolas Bonnefon "<p><a href=\"http://glogg.bonnefon.org/\">http://glogg.bonnefon.org/</a></p>" 444a1202e0cSNicolas Bonnefon "<p>Copyright © 2009, 2010, 2011, 2012, 2013, 2014 Nicolas Bonnefon and other contributors" 445bb02e0acSNicolas Bonnefon "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 446bb02e0acSNicolas Bonnefon } 447bb02e0acSNicolas Bonnefon 448bb02e0acSNicolas Bonnefon // Opens the 'About Qt' dialog box. 449bb02e0acSNicolas Bonnefon void MainWindow::aboutQt() 450bb02e0acSNicolas Bonnefon { 451bb02e0acSNicolas Bonnefon } 452bb02e0acSNicolas Bonnefon 453bb02e0acSNicolas Bonnefon void MainWindow::toggleOverviewVisibility( bool isVisible ) 454bb02e0acSNicolas Bonnefon { 45511582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 45611582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 45711582726SNicolas Bonnefon config->setOverviewVisible( isVisible ); 458bb02e0acSNicolas Bonnefon emit optionsChanged(); 459bb02e0acSNicolas Bonnefon } 460bb02e0acSNicolas Bonnefon 461bb02e0acSNicolas Bonnefon void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 462bb02e0acSNicolas Bonnefon { 46311582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 46411582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 46511582726SNicolas Bonnefon config->setMainLineNumbersVisible( isVisible ); 466bb02e0acSNicolas Bonnefon emit optionsChanged(); 467bb02e0acSNicolas Bonnefon } 468bb02e0acSNicolas Bonnefon 469bb02e0acSNicolas Bonnefon void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 470bb02e0acSNicolas Bonnefon { 47111582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 47211582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 47311582726SNicolas Bonnefon config->setFilteredLineNumbersVisible( isVisible ); 474bb02e0acSNicolas Bonnefon emit optionsChanged(); 475bb02e0acSNicolas Bonnefon } 476bb02e0acSNicolas Bonnefon 477bb02e0acSNicolas Bonnefon void MainWindow::disableFollow() 478bb02e0acSNicolas Bonnefon { 479bb02e0acSNicolas Bonnefon followAction->setChecked( false ); 480bb02e0acSNicolas Bonnefon } 481bb02e0acSNicolas Bonnefon 482bb02e0acSNicolas Bonnefon void MainWindow::lineNumberHandler( int line ) 483bb02e0acSNicolas Bonnefon { 484bb02e0acSNicolas Bonnefon // The line number received is the internal (starts at 0) 485bb02e0acSNicolas Bonnefon lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 486bb02e0acSNicolas Bonnefon } 487bb02e0acSNicolas Bonnefon 488bb02e0acSNicolas Bonnefon void MainWindow::updateLoadingProgress( int progress ) 489bb02e0acSNicolas Bonnefon { 490bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Loading progress: " << progress; 491bb02e0acSNicolas Bonnefon 492f8bd90d8SNicolas Bonnefon QString current_file = 493f8bd90d8SNicolas Bonnefon session_->getFilename( currentCrawlerWidget() ).c_str(); 4940e97f16dSNicolas Bonnefon 495bb02e0acSNicolas Bonnefon // We ignore 0% and 100% to avoid a flash when the file (or update) 496bb02e0acSNicolas Bonnefon // is very short. 497bb02e0acSNicolas Bonnefon if ( progress > 0 && progress < 100 ) { 498f8bd90d8SNicolas Bonnefon infoLine->setText( current_file + 499f8bd90d8SNicolas Bonnefon tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 500bb02e0acSNicolas Bonnefon infoLine->displayGauge( progress ); 501bb02e0acSNicolas Bonnefon 502bb02e0acSNicolas Bonnefon stopAction->setEnabled( true ); 503f8bd90d8SNicolas Bonnefon reloadAction->setEnabled( false ); 504bb02e0acSNicolas Bonnefon } 505bb02e0acSNicolas Bonnefon } 506bb02e0acSNicolas Bonnefon 507812146a8SNicolas Bonnefon void MainWindow::handleLoadingFinished( LoadingStatus status ) 508bb02e0acSNicolas Bonnefon { 509bb02e0acSNicolas Bonnefon QLocale defaultLocale; 510bb02e0acSNicolas Bonnefon 511812146a8SNicolas Bonnefon LOG(logDEBUG) << "handleLoadingFinished success=" << 512812146a8SNicolas Bonnefon ( status == LoadingStatus::Successful ); 513bb02e0acSNicolas Bonnefon 5140e97f16dSNicolas Bonnefon // No file is loading 5150e97f16dSNicolas Bonnefon loadingFileName.clear(); 5160e97f16dSNicolas Bonnefon 517812146a8SNicolas Bonnefon if ( status == LoadingStatus::Successful ) 518f8bd90d8SNicolas Bonnefon { 5190e97f16dSNicolas Bonnefon // Following should always work as we will only receive enter 5200e97f16dSNicolas Bonnefon // this slot if there is a crawler connected. 5210e97f16dSNicolas Bonnefon QString current_file = 5220e97f16dSNicolas Bonnefon session_->getFilename( currentCrawlerWidget() ).c_str(); 5230e97f16dSNicolas Bonnefon 5240a90ca6aSNicolas Bonnefon uint64_t fileSize; 5250a90ca6aSNicolas Bonnefon uint32_t fileNbLine; 526bb02e0acSNicolas Bonnefon QDateTime lastModified; 527bb02e0acSNicolas Bonnefon 52827ddfd3aSNicolas Bonnefon session_->getFileInfo( currentCrawlerWidget(), 5290a90ca6aSNicolas Bonnefon &fileSize, &fileNbLine, &lastModified ); 530bb02e0acSNicolas Bonnefon if ( lastModified.isValid() ) { 531bb02e0acSNicolas Bonnefon const QString date = 532bb02e0acSNicolas Bonnefon defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 533bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 5340e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 535bb02e0acSNicolas Bonnefon .arg(fileNbLine).arg( date ) ); 536bb02e0acSNicolas Bonnefon } 537bb02e0acSNicolas Bonnefon else { 538bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 5390e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 540bb02e0acSNicolas Bonnefon .arg(fileNbLine) ); 541bb02e0acSNicolas Bonnefon } 542bb02e0acSNicolas Bonnefon 543bb02e0acSNicolas Bonnefon infoLine->hideGauge(); 544bb02e0acSNicolas Bonnefon stopAction->setEnabled( false ); 545f8bd90d8SNicolas Bonnefon reloadAction->setEnabled( true ); 5460a90ca6aSNicolas Bonnefon 5470a90ca6aSNicolas Bonnefon // Now everything is ready, we can finally show the file! 54827ddfd3aSNicolas Bonnefon currentCrawlerWidget()->show(); 549f8bd90d8SNicolas Bonnefon } 550f8bd90d8SNicolas Bonnefon else 551f8bd90d8SNicolas Bonnefon { 552812146a8SNicolas Bonnefon if ( status == LoadingStatus::NoMemory ) 553812146a8SNicolas Bonnefon { 554812146a8SNicolas Bonnefon QMessageBox alertBox; 555812146a8SNicolas Bonnefon alertBox.setText( "Not enough memory." ); 556812146a8SNicolas Bonnefon alertBox.setInformativeText( "The system does not have enough \ 557812146a8SNicolas Bonnefon memory to hold the index for this file. The file will now be closed." ); 558812146a8SNicolas Bonnefon alertBox.setIcon( QMessageBox::Critical ); 559812146a8SNicolas Bonnefon alertBox.exec(); 560812146a8SNicolas Bonnefon } 561812146a8SNicolas Bonnefon 562f8bd90d8SNicolas Bonnefon closeTab( mainTabWidget_.currentIndex() ); 563f8bd90d8SNicolas Bonnefon } 564f8bd90d8SNicolas Bonnefon 5657875bde2SNicolas Bonnefon // mainTabWidget_.setEnabled( true ); 566bb02e0acSNicolas Bonnefon } 567bb02e0acSNicolas Bonnefon 568cdd89779SNicolas Bonnefon void MainWindow::closeTab( int index ) 569cdd89779SNicolas Bonnefon { 570cdd89779SNicolas Bonnefon auto widget = dynamic_cast<CrawlerWidget*>( 571cdd89779SNicolas Bonnefon mainTabWidget_.widget( index ) ); 572cdd89779SNicolas Bonnefon 573cdd89779SNicolas Bonnefon assert( widget ); 574cdd89779SNicolas Bonnefon 575f8bd90d8SNicolas Bonnefon widget->stopLoading(); 576cdd89779SNicolas Bonnefon mainTabWidget_.removeTab( index ); 577cdd89779SNicolas Bonnefon session_->close( widget ); 578cdd89779SNicolas Bonnefon delete widget; 579cdd89779SNicolas Bonnefon } 580cdd89779SNicolas Bonnefon 581ee835f33SNicolas Bonnefon void MainWindow::currentTabChanged( int index ) 582ee835f33SNicolas Bonnefon { 583ee835f33SNicolas Bonnefon LOG(logDEBUG) << "currentTabChanged"; 584ee835f33SNicolas Bonnefon 585ee835f33SNicolas Bonnefon if ( index >= 0 ) 586ee835f33SNicolas Bonnefon { 587ee835f33SNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 588ee835f33SNicolas Bonnefon mainTabWidget_.widget( index ) ); 589ee835f33SNicolas Bonnefon signalMux_.setCurrentDocument( crawler_widget ); 590ee835f33SNicolas Bonnefon quickFindMux_.registerSelector( crawler_widget ); 591ee835f33SNicolas Bonnefon 592ee835f33SNicolas Bonnefon // New tab is set up with fonts etc... 593ee835f33SNicolas Bonnefon emit optionsChanged(); 5940e97f16dSNicolas Bonnefon 5950e97f16dSNicolas Bonnefon // Update the title bar 5960e97f16dSNicolas Bonnefon updateTitleBar( QString( 5970e97f16dSNicolas Bonnefon session_->getFilename( crawler_widget ).c_str() ) ); 598ee835f33SNicolas Bonnefon } 599ee835f33SNicolas Bonnefon else 600ee835f33SNicolas Bonnefon { 601a1202e0cSNicolas Bonnefon // No tab left 602a1202e0cSNicolas Bonnefon signalMux_.setCurrentDocument( nullptr ); 603a1202e0cSNicolas Bonnefon quickFindMux_.registerSelector( nullptr ); 604a1202e0cSNicolas Bonnefon 605a1202e0cSNicolas Bonnefon infoLine->hideGauge(); 606a1202e0cSNicolas Bonnefon infoLine->clear(); 607a1202e0cSNicolas Bonnefon 608a1202e0cSNicolas Bonnefon updateTitleBar( QString() ); 609ee835f33SNicolas Bonnefon } 610ee835f33SNicolas Bonnefon } 611ee835f33SNicolas Bonnefon 6128570d8d2SNicolas Bonnefon void MainWindow::changeQFPattern( const QString& newPattern ) 6138570d8d2SNicolas Bonnefon { 6148570d8d2SNicolas Bonnefon quickFindWidget_.changeDisplayedPattern( newPattern ); 6158570d8d2SNicolas Bonnefon } 6168570d8d2SNicolas Bonnefon 61709aff35dSNicolas Bonnefon void MainWindow::loadFileNonInteractive( const QString& file_name ) 61809aff35dSNicolas Bonnefon { 61909aff35dSNicolas Bonnefon LOG(logDEBUG) << "loadFileNonInteractive( " 62009aff35dSNicolas Bonnefon << file_name.toStdString() << " )"; 62109aff35dSNicolas Bonnefon 62209aff35dSNicolas Bonnefon loadFile( file_name ); 62309aff35dSNicolas Bonnefon } 62409aff35dSNicolas Bonnefon 625bb02e0acSNicolas Bonnefon // 626bb02e0acSNicolas Bonnefon // Events 627bb02e0acSNicolas Bonnefon // 628bb02e0acSNicolas Bonnefon 629bb02e0acSNicolas Bonnefon // Closes the application 630bb02e0acSNicolas Bonnefon void MainWindow::closeEvent( QCloseEvent *event ) 631bb02e0acSNicolas Bonnefon { 632bb02e0acSNicolas Bonnefon writeSettings(); 633bb02e0acSNicolas Bonnefon event->accept(); 634bb02e0acSNicolas Bonnefon } 635bb02e0acSNicolas Bonnefon 636bb02e0acSNicolas Bonnefon // Accepts the drag event if it looks like a filename 637bb02e0acSNicolas Bonnefon void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 638bb02e0acSNicolas Bonnefon { 639bb02e0acSNicolas Bonnefon if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 640bb02e0acSNicolas Bonnefon event->acceptProposedAction(); 641bb02e0acSNicolas Bonnefon } 642bb02e0acSNicolas Bonnefon 643bb02e0acSNicolas Bonnefon // Tries and loads the file if the URL dropped is local 644bb02e0acSNicolas Bonnefon void MainWindow::dropEvent( QDropEvent* event ) 645bb02e0acSNicolas Bonnefon { 646bb02e0acSNicolas Bonnefon QList<QUrl> urls = event->mimeData()->urls(); 647bb02e0acSNicolas Bonnefon if ( urls.isEmpty() ) 648bb02e0acSNicolas Bonnefon return; 649bb02e0acSNicolas Bonnefon 650bb02e0acSNicolas Bonnefon QString fileName = urls.first().toLocalFile(); 651bb02e0acSNicolas Bonnefon if ( fileName.isEmpty() ) 652bb02e0acSNicolas Bonnefon return; 653bb02e0acSNicolas Bonnefon 654bb02e0acSNicolas Bonnefon loadFile( fileName ); 655bb02e0acSNicolas Bonnefon } 656bb02e0acSNicolas Bonnefon 6578570d8d2SNicolas Bonnefon void MainWindow::keyPressEvent( QKeyEvent* keyEvent ) 6588570d8d2SNicolas Bonnefon { 6598570d8d2SNicolas Bonnefon LOG(logDEBUG4) << "keyPressEvent received"; 6608570d8d2SNicolas Bonnefon 6618570d8d2SNicolas Bonnefon switch ( (keyEvent->text())[0].toLatin1() ) { 6628570d8d2SNicolas Bonnefon case '/': 6638570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 6648570d8d2SNicolas Bonnefon break; 6658570d8d2SNicolas Bonnefon case '?': 6668570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Backward ); 6678570d8d2SNicolas Bonnefon break; 6688570d8d2SNicolas Bonnefon default: 6698570d8d2SNicolas Bonnefon keyEvent->ignore(); 6708570d8d2SNicolas Bonnefon } 6718570d8d2SNicolas Bonnefon 6728570d8d2SNicolas Bonnefon if ( !keyEvent->isAccepted() ) 6738570d8d2SNicolas Bonnefon QMainWindow::keyPressEvent( keyEvent ); 6748570d8d2SNicolas Bonnefon } 6758570d8d2SNicolas Bonnefon 676bb02e0acSNicolas Bonnefon // 677bb02e0acSNicolas Bonnefon // Private functions 678bb02e0acSNicolas Bonnefon // 679bb02e0acSNicolas Bonnefon 6800a90ca6aSNicolas Bonnefon // Create a CrawlerWidget for the passed file, start its loading 6810a90ca6aSNicolas Bonnefon // and update the title bar. 682bb02e0acSNicolas Bonnefon // The loading is done asynchronously. 683bb02e0acSNicolas Bonnefon bool MainWindow::loadFile( const QString& fileName ) 684bb02e0acSNicolas Bonnefon { 685bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 686bb02e0acSNicolas Bonnefon 6873b4aad7fSNicolas Bonnefon // First check if the file is already open... 6883b4aad7fSNicolas Bonnefon CrawlerWidget* existing_crawler = dynamic_cast<CrawlerWidget*>( 6893b4aad7fSNicolas Bonnefon session_->getViewIfOpen( fileName.toStdString() ) ); 6903b4aad7fSNicolas Bonnefon if ( existing_crawler ) { 6913b4aad7fSNicolas Bonnefon // ... and switch to it. 6923b4aad7fSNicolas Bonnefon mainTabWidget_.setCurrentWidget( existing_crawler ); 6933b4aad7fSNicolas Bonnefon 6943b4aad7fSNicolas Bonnefon return true; 6953b4aad7fSNicolas Bonnefon } 6963b4aad7fSNicolas Bonnefon 697bb02e0acSNicolas Bonnefon // Load the file 698bb02e0acSNicolas Bonnefon loadingFileName = fileName; 699039481acSNicolas Bonnefon 700a3b56311SNicolas Bonnefon try { 70127ddfd3aSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 70227ddfd3aSNicolas Bonnefon session_->open( fileName.toStdString(), 7031b5e406eSNicolas Bonnefon []() { return new CrawlerWidget(); } ) ); 7041b5e406eSNicolas Bonnefon assert( crawler_widget ); 705f0708ca8SNicolas Bonnefon 7060a90ca6aSNicolas Bonnefon // We won't show the widget until the file is fully loaded 70727ddfd3aSNicolas Bonnefon crawler_widget->hide(); 708f0708ca8SNicolas Bonnefon 70927ddfd3aSNicolas Bonnefon // We disable the tab widget to avoid having someone switch 71027ddfd3aSNicolas Bonnefon // tab during loading. (maybe FIXME) 7117875bde2SNicolas Bonnefon //mainTabWidget_.setEnabled( false ); 712313a820fSNicolas Bonnefon 713a3b56311SNicolas Bonnefon int index = mainTabWidget_.addTab( 714a3b56311SNicolas Bonnefon crawler_widget, strippedName( fileName ) ); 715f0708ca8SNicolas Bonnefon 71627ddfd3aSNicolas Bonnefon // Setting the new tab, the user will see a blank page for the duration 71727ddfd3aSNicolas Bonnefon // of the loading, with no way to switch to another tab 71827ddfd3aSNicolas Bonnefon mainTabWidget_.setCurrentIndex( index ); 71927ddfd3aSNicolas Bonnefon 72060864ff5SNicolas Bonnefon // Update the recent files list 72160864ff5SNicolas Bonnefon // (reload the list first in case another glogg changed it) 72260864ff5SNicolas Bonnefon GetPersistentInfo().retrieve( "recentFiles" ); 72311582726SNicolas Bonnefon recentFiles_->addRecent( fileName ); 72460864ff5SNicolas Bonnefon GetPersistentInfo().save( "recentFiles" ); 72560864ff5SNicolas Bonnefon updateRecentFileActions(); 726a3b56311SNicolas Bonnefon } 727a3b56311SNicolas Bonnefon catch ( FileUnreadableErr ) { 728a3b56311SNicolas Bonnefon LOG(logDEBUG) << "Can't open file " << fileName.toStdString(); 729a3b56311SNicolas Bonnefon return false; 730a3b56311SNicolas Bonnefon } 73160864ff5SNicolas Bonnefon 732bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 733bb02e0acSNicolas Bonnefon return true; 734a3b56311SNicolas Bonnefon 735bb02e0acSNicolas Bonnefon } 736bb02e0acSNicolas Bonnefon 737bb02e0acSNicolas Bonnefon // Strips the passed filename from its directory part. 738bb02e0acSNicolas Bonnefon QString MainWindow::strippedName( const QString& fullFileName ) const 739bb02e0acSNicolas Bonnefon { 740bb02e0acSNicolas Bonnefon return QFileInfo( fullFileName ).fileName(); 741bb02e0acSNicolas Bonnefon } 742bb02e0acSNicolas Bonnefon 74327ddfd3aSNicolas Bonnefon // Return the currently active CrawlerWidget, or NULL if none 74427ddfd3aSNicolas Bonnefon CrawlerWidget* MainWindow::currentCrawlerWidget() const 74527ddfd3aSNicolas Bonnefon { 74627ddfd3aSNicolas Bonnefon auto current = dynamic_cast<CrawlerWidget*>( 74727ddfd3aSNicolas Bonnefon mainTabWidget_.currentWidget() ); 74827ddfd3aSNicolas Bonnefon 74927ddfd3aSNicolas Bonnefon return current; 75027ddfd3aSNicolas Bonnefon } 75127ddfd3aSNicolas Bonnefon 7520e97f16dSNicolas Bonnefon // Update the title bar. 7530e97f16dSNicolas Bonnefon void MainWindow::updateTitleBar( const QString& file_name ) 754bb02e0acSNicolas Bonnefon { 755bb02e0acSNicolas Bonnefon QString shownName = tr( "Untitled" ); 7560e97f16dSNicolas Bonnefon if ( !file_name.isEmpty() ) 7570e97f16dSNicolas Bonnefon shownName = strippedName( file_name ); 758bb02e0acSNicolas Bonnefon 759bb02e0acSNicolas Bonnefon setWindowTitle( 760bb02e0acSNicolas Bonnefon tr("%1 - %2").arg(shownName).arg(tr("glogg")) 761bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 762bb02e0acSNicolas Bonnefon + " (dev build " GLOGG_VERSION ")" 763bb02e0acSNicolas Bonnefon #endif 764bb02e0acSNicolas Bonnefon ); 765bb02e0acSNicolas Bonnefon } 766bb02e0acSNicolas Bonnefon 767bb02e0acSNicolas Bonnefon // Updates the actions for the recent files. 768bb02e0acSNicolas Bonnefon // Must be called after having added a new name to the list. 769bb02e0acSNicolas Bonnefon void MainWindow::updateRecentFileActions() 770bb02e0acSNicolas Bonnefon { 77111582726SNicolas Bonnefon QStringList recent_files = recentFiles_->recentFiles(); 772bb02e0acSNicolas Bonnefon 773bb02e0acSNicolas Bonnefon for ( int j = 0; j < MaxRecentFiles; ++j ) { 774bb02e0acSNicolas Bonnefon if ( j < recent_files.count() ) { 775bb02e0acSNicolas Bonnefon QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 776bb02e0acSNicolas Bonnefon recentFileActions[j]->setText( text ); 777bb02e0acSNicolas Bonnefon recentFileActions[j]->setToolTip( recent_files[j] ); 778bb02e0acSNicolas Bonnefon recentFileActions[j]->setData( recent_files[j] ); 779bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( true ); 780bb02e0acSNicolas Bonnefon } 781bb02e0acSNicolas Bonnefon else { 782bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( false ); 783bb02e0acSNicolas Bonnefon } 784bb02e0acSNicolas Bonnefon } 785bb02e0acSNicolas Bonnefon 786bb02e0acSNicolas Bonnefon // separatorAction->setVisible(!recentFiles.isEmpty()); 787bb02e0acSNicolas Bonnefon } 788bb02e0acSNicolas Bonnefon 789bb02e0acSNicolas Bonnefon // Write settings to permanent storage 790bb02e0acSNicolas Bonnefon void MainWindow::writeSettings() 791bb02e0acSNicolas Bonnefon { 792bb02e0acSNicolas Bonnefon // Save the session 793b57881faSNicolas Bonnefon // Generate the ordered list of widgets and their topLine 794a44d09bcSNicolas Bonnefon std::vector< 795a44d09bcSNicolas Bonnefon std::tuple<const ViewInterface*, uint64_t, std::shared_ptr<const ViewContextInterface>> 796a44d09bcSNicolas Bonnefon > widget_list; 797b57881faSNicolas Bonnefon for ( int i = 0; i < mainTabWidget_.count(); ++i ) 798a44d09bcSNicolas Bonnefon { 799a44d09bcSNicolas Bonnefon auto view = dynamic_cast<const ViewInterface*>( mainTabWidget_.widget( i ) ); 800a44d09bcSNicolas Bonnefon widget_list.push_back( std::make_tuple( 801a44d09bcSNicolas Bonnefon view, 802a44d09bcSNicolas Bonnefon 0UL, 803a44d09bcSNicolas Bonnefon view->context() ) ); 804a44d09bcSNicolas Bonnefon } 805b57881faSNicolas Bonnefon session_->save( widget_list ); 806b57881faSNicolas Bonnefon //SessionInfo& session = Persistent<SessionInfo>( "session" ); 807b57881faSNicolas Bonnefon //session.setGeometry( saveGeometry() ); 80827ddfd3aSNicolas Bonnefon //session.setCrawlerState( crawlerWidget->saveState() ); 809b57881faSNicolas Bonnefon //GetPersistentInfo().save( QString( "session" ) ); 810bb02e0acSNicolas Bonnefon 811bb02e0acSNicolas Bonnefon // User settings 812bb02e0acSNicolas Bonnefon GetPersistentInfo().save( QString( "settings" ) ); 813bb02e0acSNicolas Bonnefon } 814bb02e0acSNicolas Bonnefon 815bb02e0acSNicolas Bonnefon // Read settings from permanent storage 816bb02e0acSNicolas Bonnefon void MainWindow::readSettings() 817bb02e0acSNicolas Bonnefon { 818bb02e0acSNicolas Bonnefon // Get and restore the session 819b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "session" ) ); 820b57881faSNicolas Bonnefon // SessionInfo session = Persistent<SessionInfo>( "session" ); 821b57881faSNicolas Bonnefon //restoreGeometry( session.geometry() ); 8220a90ca6aSNicolas Bonnefon /* 8230a90ca6aSNicolas Bonnefon * FIXME: should be in the session 8240a90ca6aSNicolas Bonnefon crawlerWidget->restoreState( session.crawlerState() ); 8250a90ca6aSNicolas Bonnefon */ 826bb02e0acSNicolas Bonnefon 827bb02e0acSNicolas Bonnefon // History of recent files 828bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 829bb02e0acSNicolas Bonnefon updateRecentFileActions(); 830bb02e0acSNicolas Bonnefon 831b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "settings" ) ); 832bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "filterSet" ) ); 833bb02e0acSNicolas Bonnefon } 834bb02e0acSNicolas Bonnefon 835b423cd88SNicolas Bonnefon void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction ) 836b423cd88SNicolas Bonnefon { 837b423cd88SNicolas Bonnefon LOG(logDEBUG) << "MainWindow::displayQuickFindBar"; 838b423cd88SNicolas Bonnefon 8398570d8d2SNicolas Bonnefon // Warn crawlers so they can save the position of the focus in order 8408570d8d2SNicolas Bonnefon // to do incremental search in the right view. 8418570d8d2SNicolas Bonnefon emit enteringQuickFind(); 842b423cd88SNicolas Bonnefon 843b423cd88SNicolas Bonnefon quickFindMux_.setDirection( direction ); 844b423cd88SNicolas Bonnefon quickFindWidget_.userActivate(); 845b423cd88SNicolas Bonnefon } 846b423cd88SNicolas Bonnefon 847bb02e0acSNicolas Bonnefon // Returns the size in human readable format 8480a90ca6aSNicolas Bonnefon static QString readableSize( qint64 size ) 849bb02e0acSNicolas Bonnefon { 850bb02e0acSNicolas Bonnefon static const QString sizeStrs[] = { 8510a90ca6aSNicolas Bonnefon QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 8520a90ca6aSNicolas Bonnefon QObject::tr("GiB"), QObject::tr("TiB") }; 853bb02e0acSNicolas Bonnefon 854bb02e0acSNicolas Bonnefon QLocale defaultLocale; 855bb02e0acSNicolas Bonnefon unsigned int i; 856bb02e0acSNicolas Bonnefon double humanSize = size; 857bb02e0acSNicolas Bonnefon 858bb02e0acSNicolas Bonnefon for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 859bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 860bb02e0acSNicolas Bonnefon 861bb02e0acSNicolas Bonnefon if ( humanSize >= 1024.0 ) { 862bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 863bb02e0acSNicolas Bonnefon i++; 864bb02e0acSNicolas Bonnefon } 865bb02e0acSNicolas Bonnefon 866bb02e0acSNicolas Bonnefon QString output; 867bb02e0acSNicolas Bonnefon if ( i == 0 ) 868bb02e0acSNicolas Bonnefon // No decimal part if we display straight bytes. 869bb02e0acSNicolas Bonnefon output = defaultLocale.toString( (int) humanSize ); 870bb02e0acSNicolas Bonnefon else 871bb02e0acSNicolas Bonnefon output = defaultLocale.toString( humanSize, 'f', 1 ); 872bb02e0acSNicolas Bonnefon 873bb02e0acSNicolas Bonnefon output += QString(" ") + sizeStrs[i]; 874bb02e0acSNicolas Bonnefon 875bb02e0acSNicolas Bonnefon return output; 876bb02e0acSNicolas Bonnefon } 877