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_() 66431d01deSNicolas Bonnefon #ifdef GLOGG_SUPPORTS_VERSION_CHECKING 67431d01deSNicolas Bonnefon ,versionChecker_() 68431d01deSNicolas Bonnefon #endif 69bb02e0acSNicolas Bonnefon { 70bb02e0acSNicolas Bonnefon createActions(); 71bb02e0acSNicolas Bonnefon createMenus(); 72bb02e0acSNicolas Bonnefon createToolBars(); 73bb02e0acSNicolas Bonnefon // createStatusBar(); 74bb02e0acSNicolas Bonnefon 75bb02e0acSNicolas Bonnefon setAcceptDrops( true ); 76bb02e0acSNicolas Bonnefon 77bb02e0acSNicolas Bonnefon // Default geometry 78bb02e0acSNicolas Bonnefon const QRect geometry = QApplication::desktop()->availableGeometry( this ); 79bb02e0acSNicolas Bonnefon setGeometry( geometry.x() + 20, geometry.y() + 40, 80bb02e0acSNicolas Bonnefon geometry.width() - 140, geometry.height() - 140 ); 81bb02e0acSNicolas Bonnefon 82bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/16x16/glogg.png" ); 83bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/24x24/glogg.png" ); 84bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/32x32/glogg.png" ); 85bb02e0acSNicolas Bonnefon mainIcon_.addFile( ":/images/hicolor/48x48/glogg.png" ); 86bb02e0acSNicolas Bonnefon 87bb02e0acSNicolas Bonnefon setWindowIcon( mainIcon_ ); 88f0708ca8SNicolas Bonnefon 890a90ca6aSNicolas Bonnefon readSettings(); 900a90ca6aSNicolas Bonnefon 9127ddfd3aSNicolas Bonnefon // Connect the signals to the mux (they will be forwarded to the 9227ddfd3aSNicolas Bonnefon // "current" crawlerwidget 9327ddfd3aSNicolas Bonnefon 9427ddfd3aSNicolas Bonnefon // Send actions to the crawlerwidget 9527ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( followSet( bool ) ), 9627ddfd3aSNicolas Bonnefon SIGNAL( followSet( bool ) ) ); 9727ddfd3aSNicolas Bonnefon signalMux_.connect( this, SIGNAL( optionsChanged() ), 9827ddfd3aSNicolas Bonnefon SLOT( applyConfiguration() ) ); 998570d8d2SNicolas Bonnefon signalMux_.connect( this, SIGNAL( enteringQuickFind() ), 1008570d8d2SNicolas Bonnefon SLOT( enteringQuickFind() ) ); 1018570d8d2SNicolas Bonnefon signalMux_.connect( &quickFindWidget_, SIGNAL( close() ), 1028570d8d2SNicolas Bonnefon SLOT( exitingQuickFind() ) ); 10327ddfd3aSNicolas Bonnefon 10427ddfd3aSNicolas Bonnefon // Actions from the CrawlerWidget 10527ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( followDisabled() ), 10627ddfd3aSNicolas Bonnefon this, SLOT( disableFollow() ) ); 10727ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( updateLineNumber( int ) ), 10827ddfd3aSNicolas Bonnefon this, SLOT( lineNumberHandler( int ) ) ); 10927ddfd3aSNicolas Bonnefon 11027ddfd3aSNicolas Bonnefon // Register for progress status bar 11127ddfd3aSNicolas Bonnefon signalMux_.connect( SIGNAL( loadingProgressed( int ) ), 11227ddfd3aSNicolas Bonnefon this, SLOT( updateLoadingProgress( int ) ) ); 113812146a8SNicolas Bonnefon signalMux_.connect( SIGNAL( loadingFinished( LoadingStatus ) ), 114812146a8SNicolas Bonnefon this, SLOT( handleLoadingFinished( LoadingStatus ) ) ); 11527ddfd3aSNicolas Bonnefon 116cdd89779SNicolas Bonnefon // Configure the main tabbed widget 117093a1bf6SNicolas Bonnefon mainTabWidget_.setDocumentMode( true ); 118cdd89779SNicolas Bonnefon mainTabWidget_.setMovable( true ); 119093a1bf6SNicolas Bonnefon //mainTabWidget_.setTabShape( QTabWidget::Triangular ); 120cdd89779SNicolas Bonnefon mainTabWidget_.setTabsClosable( true ); 121cdd89779SNicolas Bonnefon 122cdd89779SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( tabCloseRequested( int ) ), 123cdd89779SNicolas Bonnefon this, SLOT( closeTab( int ) ) ); 124ee835f33SNicolas Bonnefon connect( &mainTabWidget_, SIGNAL( currentChanged( int ) ), 125ee835f33SNicolas Bonnefon this, SLOT( currentTabChanged( int ) ) ); 126b423cd88SNicolas Bonnefon 127b423cd88SNicolas Bonnefon // Establish the QuickFindWidget and mux ( to send requests from the 128b423cd88SNicolas Bonnefon // QFWidget to the right window ) 129b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternConfirmed( const QString&, bool ) ), 130b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( confirmPattern( const QString&, bool ) ) ); 131b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( patternUpdated( const QString&, bool ) ), 132b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( setNewPattern( const QString&, bool ) ) ); 133b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( cancelSearch() ), 134b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( cancelSearch() ) ); 135b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchForward() ), 136b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchForward() ) ); 137b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchBackward() ), 138b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchBackward() ) ); 139b423cd88SNicolas Bonnefon connect( &quickFindWidget_, SIGNAL( searchNext() ), 140b423cd88SNicolas Bonnefon &quickFindMux_, SLOT( searchNext() ) ); 141b423cd88SNicolas Bonnefon 142b423cd88SNicolas Bonnefon // QuickFind changes coming from the views 1438570d8d2SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( patternChanged( const QString& ) ), 144b423cd88SNicolas Bonnefon this, SLOT( changeQFPattern( const QString& ) ) ); 145b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( notify( const QFNotification& ) ), 146b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( notify( const QFNotification& ) ) ); 147b423cd88SNicolas Bonnefon connect( &quickFindMux_, SIGNAL( clearNotification() ), 148b423cd88SNicolas Bonnefon &quickFindWidget_, SLOT( clearNotification() ) ); 149b423cd88SNicolas Bonnefon 15009aff35dSNicolas Bonnefon // Actions from external instances 15109aff35dSNicolas Bonnefon connect( externalCommunicator_.get(), SIGNAL( loadFile( const QString& ) ), 15209aff35dSNicolas Bonnefon this, SLOT( loadFileNonInteractive( const QString& ) ) ); 15309aff35dSNicolas Bonnefon 154f109b95bSNicolas Bonnefon #ifdef GLOGG_SUPPORTS_VERSION_CHECKING 155460de700SNicolas Bonnefon // Version checker notification 156460de700SNicolas Bonnefon connect( &versionChecker_, SIGNAL( newVersionFound( const QString& ) ), 157460de700SNicolas Bonnefon this, SLOT( newVersionNotification( const QString& ) ) ); 158f109b95bSNicolas Bonnefon #endif 159460de700SNicolas Bonnefon 160b423cd88SNicolas Bonnefon // Construct the QuickFind bar 161b423cd88SNicolas Bonnefon quickFindWidget_.hide(); 162b423cd88SNicolas Bonnefon 163b423cd88SNicolas Bonnefon QWidget* central_widget = new QWidget(); 164b423cd88SNicolas Bonnefon QVBoxLayout* main_layout = new QVBoxLayout(); 165548acbf6SNicolas Bonnefon main_layout->setContentsMargins( 0, 0, 0, 0 ); 166b423cd88SNicolas Bonnefon main_layout->addWidget( &mainTabWidget_ ); 167b423cd88SNicolas Bonnefon main_layout->addWidget( &quickFindWidget_ ); 168b423cd88SNicolas Bonnefon central_widget->setLayout( main_layout ); 169b423cd88SNicolas Bonnefon 170b423cd88SNicolas Bonnefon setCentralWidget( central_widget ); 171bb02e0acSNicolas Bonnefon } 172bb02e0acSNicolas Bonnefon 1738964a9adSNicolas Bonnefon void MainWindow::reloadSession() 1748964a9adSNicolas Bonnefon { 175b76966a6SNicolas Bonnefon QByteArray geometry; 176b76966a6SNicolas Bonnefon 177b76966a6SNicolas Bonnefon session_->storedGeometry( &geometry ); 178b76966a6SNicolas Bonnefon restoreGeometry( geometry ); 179b76966a6SNicolas Bonnefon 1808964a9adSNicolas Bonnefon int current_file_index = -1; 1818964a9adSNicolas Bonnefon 1828964a9adSNicolas Bonnefon for ( auto open_file: session_->restore( 1838964a9adSNicolas Bonnefon []() { return new CrawlerWidget(); }, 1848964a9adSNicolas Bonnefon ¤t_file_index ) ) 1858964a9adSNicolas Bonnefon { 1868964a9adSNicolas Bonnefon QString file_name = { open_file.first.c_str() }; 1878964a9adSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 1888964a9adSNicolas Bonnefon open_file.second ); 1898964a9adSNicolas Bonnefon 1908964a9adSNicolas Bonnefon assert( crawler_widget ); 1918964a9adSNicolas Bonnefon 1928964a9adSNicolas Bonnefon mainTabWidget_.addTab( crawler_widget, strippedName( file_name ) ); 1938964a9adSNicolas Bonnefon } 1948964a9adSNicolas Bonnefon 1958964a9adSNicolas Bonnefon if ( current_file_index >= 0 ) 1968964a9adSNicolas Bonnefon mainTabWidget_.setCurrentIndex( current_file_index ); 1978964a9adSNicolas Bonnefon } 1988964a9adSNicolas Bonnefon 199bb02e0acSNicolas Bonnefon void MainWindow::loadInitialFile( QString fileName ) 200bb02e0acSNicolas Bonnefon { 201bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadInitialFile"; 202bb02e0acSNicolas Bonnefon 203bb02e0acSNicolas Bonnefon // Is there a file passed as argument? 204bb02e0acSNicolas Bonnefon if ( !fileName.isEmpty() ) 205bb02e0acSNicolas Bonnefon loadFile( fileName ); 206bb02e0acSNicolas Bonnefon } 207bb02e0acSNicolas Bonnefon 2088a9275b2SNicolas Bonnefon void MainWindow::startBackgroundTasks() 2098a9275b2SNicolas Bonnefon { 2108a9275b2SNicolas Bonnefon LOG(logDEBUG) << "startBackgroundTasks"; 211431d01deSNicolas Bonnefon 212431d01deSNicolas Bonnefon #ifdef GLOGG_SUPPORTS_VERSION_CHECKING 213431d01deSNicolas Bonnefon versionChecker_.startCheck(); 214431d01deSNicolas Bonnefon #endif 2158a9275b2SNicolas Bonnefon } 2168a9275b2SNicolas Bonnefon 217bb02e0acSNicolas Bonnefon // 218bb02e0acSNicolas Bonnefon // Private functions 219bb02e0acSNicolas Bonnefon // 220bb02e0acSNicolas Bonnefon 221bb02e0acSNicolas Bonnefon // Menu actions 222bb02e0acSNicolas Bonnefon void MainWindow::createActions() 223bb02e0acSNicolas Bonnefon { 22411582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 22511582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 226bb02e0acSNicolas Bonnefon 227bb02e0acSNicolas Bonnefon openAction = new QAction(tr("&Open..."), this); 228bb02e0acSNicolas Bonnefon openAction->setShortcut(QKeySequence::Open); 229bb02e0acSNicolas Bonnefon openAction->setIcon( QIcon(":/images/open16.png") ); 230bb02e0acSNicolas Bonnefon openAction->setStatusTip(tr("Open a file")); 231bb02e0acSNicolas Bonnefon connect(openAction, SIGNAL(triggered()), this, SLOT(open())); 232bb02e0acSNicolas Bonnefon 23363dc3514SGustav Andersson closeAction = new QAction(tr("&Close"), this); 23404325d43SGustav Andersson closeAction->setShortcut(tr("Ctrl+W")); 23563dc3514SGustav Andersson closeAction->setStatusTip(tr("Close document")); 23663dc3514SGustav Andersson connect(closeAction, SIGNAL(triggered()), this, SLOT(closeTab())); 23763dc3514SGustav Andersson 23863dc3514SGustav Andersson closeAllAction = new QAction(tr("Close &All"), this); 23963dc3514SGustav Andersson closeAllAction->setStatusTip(tr("Close all documents")); 24063dc3514SGustav Andersson connect(closeAllAction, SIGNAL(triggered()), this, SLOT(closeAll())); 24163dc3514SGustav Andersson 242bb02e0acSNicolas Bonnefon // Recent files 243bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 244bb02e0acSNicolas Bonnefon recentFileActions[i] = new QAction(this); 245bb02e0acSNicolas Bonnefon recentFileActions[i]->setVisible(false); 246bb02e0acSNicolas Bonnefon connect(recentFileActions[i], SIGNAL(triggered()), 247bb02e0acSNicolas Bonnefon this, SLOT(openRecentFile())); 248bb02e0acSNicolas Bonnefon } 249bb02e0acSNicolas Bonnefon 250bb02e0acSNicolas Bonnefon exitAction = new QAction(tr("E&xit"), this); 251bb02e0acSNicolas Bonnefon exitAction->setShortcut(tr("Ctrl+Q")); 252bb02e0acSNicolas Bonnefon exitAction->setStatusTip(tr("Exit the application")); 253bb02e0acSNicolas Bonnefon connect( exitAction, SIGNAL(triggered()), this, SLOT(close()) ); 254bb02e0acSNicolas Bonnefon 255bb02e0acSNicolas Bonnefon copyAction = new QAction(tr("&Copy"), this); 256bb02e0acSNicolas Bonnefon copyAction->setShortcut(QKeySequence::Copy); 257bb02e0acSNicolas Bonnefon copyAction->setStatusTip(tr("Copy the selection")); 258bb02e0acSNicolas Bonnefon connect( copyAction, SIGNAL(triggered()), this, SLOT(copy()) ); 259bb02e0acSNicolas Bonnefon 260bb02e0acSNicolas Bonnefon selectAllAction = new QAction(tr("Select &All"), this); 261bb02e0acSNicolas Bonnefon selectAllAction->setShortcut(tr("Ctrl+A")); 262bb02e0acSNicolas Bonnefon selectAllAction->setStatusTip(tr("Select all the text")); 263bb02e0acSNicolas Bonnefon connect( selectAllAction, SIGNAL(triggered()), 264bb02e0acSNicolas Bonnefon this, SLOT( selectAll() ) ); 265bb02e0acSNicolas Bonnefon 266bb02e0acSNicolas Bonnefon findAction = new QAction(tr("&Find..."), this); 267bb02e0acSNicolas Bonnefon findAction->setShortcut(QKeySequence::Find); 268bb02e0acSNicolas Bonnefon findAction->setStatusTip(tr("Find the text")); 269bb02e0acSNicolas Bonnefon connect( findAction, SIGNAL(triggered()), 270bb02e0acSNicolas Bonnefon this, SLOT( find() ) ); 271bb02e0acSNicolas Bonnefon 272bb02e0acSNicolas Bonnefon overviewVisibleAction = new QAction( tr("Matches &overview"), this ); 273bb02e0acSNicolas Bonnefon overviewVisibleAction->setCheckable( true ); 27411582726SNicolas Bonnefon overviewVisibleAction->setChecked( config->isOverviewVisible() ); 275bb02e0acSNicolas Bonnefon connect( overviewVisibleAction, SIGNAL( toggled( bool ) ), 276bb02e0acSNicolas Bonnefon this, SLOT( toggleOverviewVisibility( bool )) ); 277bb02e0acSNicolas Bonnefon 278bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction = 279bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in main view"), this ); 280bb02e0acSNicolas Bonnefon lineNumbersVisibleInMainAction->setCheckable( true ); 28111582726SNicolas Bonnefon lineNumbersVisibleInMainAction->setChecked( config->mainLineNumbersVisible() ); 282bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInMainAction, SIGNAL( toggled( bool ) ), 283bb02e0acSNicolas Bonnefon this, SLOT( toggleMainLineNumbersVisibility( bool )) ); 284bb02e0acSNicolas Bonnefon 285bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction = 286bb02e0acSNicolas Bonnefon new QAction( tr("Line &numbers in filtered view"), this ); 287bb02e0acSNicolas Bonnefon lineNumbersVisibleInFilteredAction->setCheckable( true ); 28811582726SNicolas Bonnefon lineNumbersVisibleInFilteredAction->setChecked( config->filteredLineNumbersVisible() ); 289bb02e0acSNicolas Bonnefon connect( lineNumbersVisibleInFilteredAction, SIGNAL( toggled( bool ) ), 290bb02e0acSNicolas Bonnefon this, SLOT( toggleFilteredLineNumbersVisibility( bool )) ); 291bb02e0acSNicolas Bonnefon 292bb02e0acSNicolas Bonnefon followAction = new QAction( tr("&Follow File"), this ); 293bb02e0acSNicolas Bonnefon followAction->setShortcut(Qt::Key_F); 294bb02e0acSNicolas Bonnefon followAction->setCheckable(true); 295bb02e0acSNicolas Bonnefon connect( followAction, SIGNAL(toggled( bool )), 296bb02e0acSNicolas Bonnefon this, SIGNAL(followSet( bool )) ); 297bb02e0acSNicolas Bonnefon 298bb02e0acSNicolas Bonnefon reloadAction = new QAction( tr("&Reload"), this ); 299bb02e0acSNicolas Bonnefon reloadAction->setShortcut(QKeySequence::Refresh); 300bb02e0acSNicolas Bonnefon reloadAction->setIcon( QIcon(":/images/reload16.png") ); 30132e44cfdSNicolas Bonnefon signalMux_.connect( reloadAction, SIGNAL(triggered()), SLOT(reload()) ); 302bb02e0acSNicolas Bonnefon 303bb02e0acSNicolas Bonnefon stopAction = new QAction( tr("&Stop"), this ); 304bb02e0acSNicolas Bonnefon stopAction->setIcon( QIcon(":/images/stop16.png") ); 3057875bde2SNicolas Bonnefon stopAction->setEnabled( true ); 3067847299cSNicolas Bonnefon signalMux_.connect( stopAction, SIGNAL(triggered()), SLOT(stopLoading()) ); 307bb02e0acSNicolas Bonnefon 308bb02e0acSNicolas Bonnefon filtersAction = new QAction(tr("&Filters..."), this); 309bb02e0acSNicolas Bonnefon filtersAction->setStatusTip(tr("Show the Filters box")); 310bb02e0acSNicolas Bonnefon connect( filtersAction, SIGNAL(triggered()), this, SLOT(filters()) ); 311bb02e0acSNicolas Bonnefon 312bb02e0acSNicolas Bonnefon optionsAction = new QAction(tr("&Options..."), this); 313bb02e0acSNicolas Bonnefon optionsAction->setStatusTip(tr("Show the Options box")); 314bb02e0acSNicolas Bonnefon connect( optionsAction, SIGNAL(triggered()), this, SLOT(options()) ); 315bb02e0acSNicolas Bonnefon 316bb02e0acSNicolas Bonnefon aboutAction = new QAction(tr("&About"), this); 317bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the About box")); 318bb02e0acSNicolas Bonnefon connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) ); 319bb02e0acSNicolas Bonnefon 320bb02e0acSNicolas Bonnefon aboutQtAction = new QAction(tr("About &Qt"), this); 321bb02e0acSNicolas Bonnefon aboutAction->setStatusTip(tr("Show the Qt library's About box")); 322bb02e0acSNicolas Bonnefon connect( aboutQtAction, SIGNAL(triggered()), this, SLOT(aboutQt()) ); 323bb02e0acSNicolas Bonnefon } 324bb02e0acSNicolas Bonnefon 325bb02e0acSNicolas Bonnefon void MainWindow::createMenus() 326bb02e0acSNicolas Bonnefon { 327bb02e0acSNicolas Bonnefon fileMenu = menuBar()->addMenu( tr("&File") ); 328bb02e0acSNicolas Bonnefon fileMenu->addAction( openAction ); 32963dc3514SGustav Andersson fileMenu->addAction( closeAction ); 33063dc3514SGustav Andersson fileMenu->addAction( closeAllAction ); 331bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 332bb02e0acSNicolas Bonnefon for (int i = 0; i < MaxRecentFiles; ++i) { 333bb02e0acSNicolas Bonnefon fileMenu->addAction( recentFileActions[i] ); 334bb02e0acSNicolas Bonnefon recentFileActionBehaviors[i] = 335bb02e0acSNicolas Bonnefon new MenuActionToolTipBehavior(recentFileActions[i], fileMenu, this); 336bb02e0acSNicolas Bonnefon } 337bb02e0acSNicolas Bonnefon fileMenu->addSeparator(); 338bb02e0acSNicolas Bonnefon fileMenu->addAction( exitAction ); 339bb02e0acSNicolas Bonnefon 340bb02e0acSNicolas Bonnefon editMenu = menuBar()->addMenu( tr("&Edit") ); 341bb02e0acSNicolas Bonnefon editMenu->addAction( copyAction ); 342bb02e0acSNicolas Bonnefon editMenu->addAction( selectAllAction ); 343bb02e0acSNicolas Bonnefon editMenu->addSeparator(); 344bb02e0acSNicolas Bonnefon editMenu->addAction( findAction ); 345bb02e0acSNicolas Bonnefon 346bb02e0acSNicolas Bonnefon viewMenu = menuBar()->addMenu( tr("&View") ); 347bb02e0acSNicolas Bonnefon viewMenu->addAction( overviewVisibleAction ); 348bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 349bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInMainAction ); 350bb02e0acSNicolas Bonnefon viewMenu->addAction( lineNumbersVisibleInFilteredAction ); 351bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 352bb02e0acSNicolas Bonnefon viewMenu->addAction( followAction ); 353bb02e0acSNicolas Bonnefon viewMenu->addSeparator(); 354bb02e0acSNicolas Bonnefon viewMenu->addAction( reloadAction ); 355bb02e0acSNicolas Bonnefon 356bb02e0acSNicolas Bonnefon toolsMenu = menuBar()->addMenu( tr("&Tools") ); 357bb02e0acSNicolas Bonnefon toolsMenu->addAction( filtersAction ); 358bb02e0acSNicolas Bonnefon toolsMenu->addSeparator(); 359bb02e0acSNicolas Bonnefon toolsMenu->addAction( optionsAction ); 360bb02e0acSNicolas Bonnefon 361bb02e0acSNicolas Bonnefon menuBar()->addSeparator(); 362bb02e0acSNicolas Bonnefon 363bb02e0acSNicolas Bonnefon helpMenu = menuBar()->addMenu( tr("&Help") ); 364bb02e0acSNicolas Bonnefon helpMenu->addAction( aboutAction ); 365bb02e0acSNicolas Bonnefon } 366bb02e0acSNicolas Bonnefon 367bb02e0acSNicolas Bonnefon void MainWindow::createToolBars() 368bb02e0acSNicolas Bonnefon { 369bb02e0acSNicolas Bonnefon infoLine = new InfoLine(); 370bb02e0acSNicolas Bonnefon infoLine->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); 371bb02e0acSNicolas Bonnefon infoLine->setLineWidth( 0 ); 372bb02e0acSNicolas Bonnefon 373bb02e0acSNicolas Bonnefon lineNbField = new QLabel( ); 374bb02e0acSNicolas Bonnefon lineNbField->setText( "Line 0" ); 375bb02e0acSNicolas Bonnefon lineNbField->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 376bb02e0acSNicolas Bonnefon lineNbField->setMinimumSize( 377bb02e0acSNicolas Bonnefon lineNbField->fontMetrics().size( 0, "Line 0000000") ); 378bb02e0acSNicolas Bonnefon 379bb02e0acSNicolas Bonnefon toolBar = addToolBar( tr("&Toolbar") ); 380bb02e0acSNicolas Bonnefon toolBar->setIconSize( QSize( 16, 16 ) ); 381bb02e0acSNicolas Bonnefon toolBar->setMovable( false ); 382bb02e0acSNicolas Bonnefon toolBar->addAction( openAction ); 383bb02e0acSNicolas Bonnefon toolBar->addAction( reloadAction ); 384bb02e0acSNicolas Bonnefon toolBar->addWidget( infoLine ); 385bb02e0acSNicolas Bonnefon toolBar->addAction( stopAction ); 386bb02e0acSNicolas Bonnefon toolBar->addWidget( lineNbField ); 387bb02e0acSNicolas Bonnefon } 388bb02e0acSNicolas Bonnefon 389bb02e0acSNicolas Bonnefon // 390bb02e0acSNicolas Bonnefon // Slots 391bb02e0acSNicolas Bonnefon // 392bb02e0acSNicolas Bonnefon 393bb02e0acSNicolas Bonnefon // Opens the file selection dialog to select a new log file 394bb02e0acSNicolas Bonnefon void MainWindow::open() 395bb02e0acSNicolas Bonnefon { 396bb02e0acSNicolas Bonnefon QString defaultDir = "."; 397bb02e0acSNicolas Bonnefon 398bb02e0acSNicolas Bonnefon // Default to the path of the current file if there is one 3990e97f16dSNicolas Bonnefon if ( auto current = currentCrawlerWidget() ) 4000e97f16dSNicolas Bonnefon { 4010e97f16dSNicolas Bonnefon std::string current_file = session_->getFilename( current ); 4020e97f16dSNicolas Bonnefon QFileInfo fileInfo = QFileInfo( QString( current_file.c_str() ) ); 403bb02e0acSNicolas Bonnefon defaultDir = fileInfo.path(); 404bb02e0acSNicolas Bonnefon } 405bb02e0acSNicolas Bonnefon 406bb02e0acSNicolas Bonnefon QString fileName = QFileDialog::getOpenFileName(this, 407bb02e0acSNicolas Bonnefon tr("Open file"), defaultDir, tr("All files (*)")); 408bb02e0acSNicolas Bonnefon if (!fileName.isEmpty()) 409bb02e0acSNicolas Bonnefon loadFile(fileName); 410bb02e0acSNicolas Bonnefon } 411bb02e0acSNicolas Bonnefon 412bb02e0acSNicolas Bonnefon // Opens a log file from the recent files list 413bb02e0acSNicolas Bonnefon void MainWindow::openRecentFile() 414bb02e0acSNicolas Bonnefon { 415bb02e0acSNicolas Bonnefon QAction* action = qobject_cast<QAction*>(sender()); 416bb02e0acSNicolas Bonnefon if (action) 417bb02e0acSNicolas Bonnefon loadFile(action->data().toString()); 418bb02e0acSNicolas Bonnefon } 419bb02e0acSNicolas Bonnefon 42063dc3514SGustav Andersson // Close current tab 42163dc3514SGustav Andersson void MainWindow::closeTab() 42263dc3514SGustav Andersson { 42363dc3514SGustav Andersson int currentIndex = mainTabWidget_.currentIndex(); 42463dc3514SGustav Andersson 42563dc3514SGustav Andersson if ( currentIndex >= 0 ) 42663dc3514SGustav Andersson { 42763dc3514SGustav Andersson closeTab(currentIndex); 42863dc3514SGustav Andersson } 42963dc3514SGustav Andersson } 43063dc3514SGustav Andersson 43163dc3514SGustav Andersson // Close all tabs 43263dc3514SGustav Andersson void MainWindow::closeAll() 43363dc3514SGustav Andersson { 43463dc3514SGustav Andersson while ( mainTabWidget_.count() ) 43563dc3514SGustav Andersson { 43663dc3514SGustav Andersson closeTab(0); 43763dc3514SGustav Andersson } 43863dc3514SGustav Andersson } 43963dc3514SGustav Andersson 440bb02e0acSNicolas Bonnefon // Select all the text in the currently selected view 441bb02e0acSNicolas Bonnefon void MainWindow::selectAll() 442bb02e0acSNicolas Bonnefon { 44327ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 44427ddfd3aSNicolas Bonnefon 44527ddfd3aSNicolas Bonnefon if ( current ) 44627ddfd3aSNicolas Bonnefon current->selectAll(); 447bb02e0acSNicolas Bonnefon } 448bb02e0acSNicolas Bonnefon 449bb02e0acSNicolas Bonnefon // Copy the currently selected line into the clipboard 450bb02e0acSNicolas Bonnefon void MainWindow::copy() 451bb02e0acSNicolas Bonnefon { 452bb02e0acSNicolas Bonnefon static QClipboard* clipboard = QApplication::clipboard(); 45327ddfd3aSNicolas Bonnefon CrawlerWidget* current = currentCrawlerWidget(); 454bb02e0acSNicolas Bonnefon 45527ddfd3aSNicolas Bonnefon if ( current ) { 45627ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText() ); 457bb02e0acSNicolas Bonnefon 458bb02e0acSNicolas Bonnefon // Put it in the global selection as well (X11 only) 45927ddfd3aSNicolas Bonnefon clipboard->setText( current->getSelectedText(), 460bb02e0acSNicolas Bonnefon QClipboard::Selection ); 461bb02e0acSNicolas Bonnefon } 46227ddfd3aSNicolas Bonnefon } 463bb02e0acSNicolas Bonnefon 464bb02e0acSNicolas Bonnefon // Display the QuickFind bar 465bb02e0acSNicolas Bonnefon void MainWindow::find() 466bb02e0acSNicolas Bonnefon { 467b423cd88SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 468bb02e0acSNicolas Bonnefon } 469bb02e0acSNicolas Bonnefon 470bb02e0acSNicolas Bonnefon // Opens the 'Filters' dialog box 471bb02e0acSNicolas Bonnefon void MainWindow::filters() 472bb02e0acSNicolas Bonnefon { 473bb02e0acSNicolas Bonnefon FiltersDialog dialog(this); 47427ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 475bb02e0acSNicolas Bonnefon dialog.exec(); 47627ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 477bb02e0acSNicolas Bonnefon } 478bb02e0acSNicolas Bonnefon 479bb02e0acSNicolas Bonnefon // Opens the 'Options' modal dialog box 480bb02e0acSNicolas Bonnefon void MainWindow::options() 481bb02e0acSNicolas Bonnefon { 482bb02e0acSNicolas Bonnefon OptionsDialog dialog(this); 48327ddfd3aSNicolas Bonnefon signalMux_.connect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 484bb02e0acSNicolas Bonnefon dialog.exec(); 48527ddfd3aSNicolas Bonnefon signalMux_.disconnect(&dialog, SIGNAL( optionsChanged() ), SLOT( applyConfiguration() )); 486bb02e0acSNicolas Bonnefon } 487bb02e0acSNicolas Bonnefon 488bb02e0acSNicolas Bonnefon // Opens the 'About' dialog box. 489bb02e0acSNicolas Bonnefon void MainWindow::about() 490bb02e0acSNicolas Bonnefon { 491bb02e0acSNicolas Bonnefon QMessageBox::about(this, tr("About glogg"), 492bb02e0acSNicolas Bonnefon tr("<h2>glogg " GLOGG_VERSION "</h2>" 493bb02e0acSNicolas Bonnefon "<p>A fast, advanced log explorer." 494bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 495bb02e0acSNicolas Bonnefon "<p>Built " GLOGG_DATE " from " GLOGG_COMMIT 496bb02e0acSNicolas Bonnefon #endif 49735c20658SNicolas Bonnefon "<p><a href=\"http://glogg.bonnefon.org/\">http://glogg.bonnefon.org/</a></p>" 498a1202e0cSNicolas Bonnefon "<p>Copyright © 2009, 2010, 2011, 2012, 2013, 2014 Nicolas Bonnefon and other contributors" 499bb02e0acSNicolas Bonnefon "<p>You may modify and redistribute the program under the terms of the GPL (version 3 or later)." ) ); 500bb02e0acSNicolas Bonnefon } 501bb02e0acSNicolas Bonnefon 502bb02e0acSNicolas Bonnefon // Opens the 'About Qt' dialog box. 503bb02e0acSNicolas Bonnefon void MainWindow::aboutQt() 504bb02e0acSNicolas Bonnefon { 505bb02e0acSNicolas Bonnefon } 506bb02e0acSNicolas Bonnefon 507bb02e0acSNicolas Bonnefon void MainWindow::toggleOverviewVisibility( bool isVisible ) 508bb02e0acSNicolas Bonnefon { 50911582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 51011582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 51111582726SNicolas Bonnefon config->setOverviewVisible( isVisible ); 512bb02e0acSNicolas Bonnefon emit optionsChanged(); 513bb02e0acSNicolas Bonnefon } 514bb02e0acSNicolas Bonnefon 515bb02e0acSNicolas Bonnefon void MainWindow::toggleMainLineNumbersVisibility( bool isVisible ) 516bb02e0acSNicolas Bonnefon { 51711582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 51811582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 51911582726SNicolas Bonnefon config->setMainLineNumbersVisible( isVisible ); 520bb02e0acSNicolas Bonnefon emit optionsChanged(); 521bb02e0acSNicolas Bonnefon } 522bb02e0acSNicolas Bonnefon 523bb02e0acSNicolas Bonnefon void MainWindow::toggleFilteredLineNumbersVisibility( bool isVisible ) 524bb02e0acSNicolas Bonnefon { 52511582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 52611582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 52711582726SNicolas Bonnefon config->setFilteredLineNumbersVisible( isVisible ); 528bb02e0acSNicolas Bonnefon emit optionsChanged(); 529bb02e0acSNicolas Bonnefon } 530bb02e0acSNicolas Bonnefon 531bb02e0acSNicolas Bonnefon void MainWindow::disableFollow() 532bb02e0acSNicolas Bonnefon { 533bb02e0acSNicolas Bonnefon followAction->setChecked( false ); 534bb02e0acSNicolas Bonnefon } 535bb02e0acSNicolas Bonnefon 536bb02e0acSNicolas Bonnefon void MainWindow::lineNumberHandler( int line ) 537bb02e0acSNicolas Bonnefon { 538bb02e0acSNicolas Bonnefon // The line number received is the internal (starts at 0) 539bb02e0acSNicolas Bonnefon lineNbField->setText( tr( "Line %1" ).arg( line + 1 ) ); 540bb02e0acSNicolas Bonnefon } 541bb02e0acSNicolas Bonnefon 542bb02e0acSNicolas Bonnefon void MainWindow::updateLoadingProgress( int progress ) 543bb02e0acSNicolas Bonnefon { 544bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Loading progress: " << progress; 545bb02e0acSNicolas Bonnefon 546f8bd90d8SNicolas Bonnefon QString current_file = 547f8bd90d8SNicolas Bonnefon session_->getFilename( currentCrawlerWidget() ).c_str(); 5480e97f16dSNicolas Bonnefon 549bb02e0acSNicolas Bonnefon // We ignore 0% and 100% to avoid a flash when the file (or update) 550bb02e0acSNicolas Bonnefon // is very short. 551bb02e0acSNicolas Bonnefon if ( progress > 0 && progress < 100 ) { 552f8bd90d8SNicolas Bonnefon infoLine->setText( current_file + 553f8bd90d8SNicolas Bonnefon tr( " - Indexing lines... (%1 %)" ).arg( progress ) ); 554bb02e0acSNicolas Bonnefon infoLine->displayGauge( progress ); 555bb02e0acSNicolas Bonnefon 556bb02e0acSNicolas Bonnefon stopAction->setEnabled( true ); 557f8bd90d8SNicolas Bonnefon reloadAction->setEnabled( false ); 558bb02e0acSNicolas Bonnefon } 559bb02e0acSNicolas Bonnefon } 560bb02e0acSNicolas Bonnefon 561812146a8SNicolas Bonnefon void MainWindow::handleLoadingFinished( LoadingStatus status ) 562bb02e0acSNicolas Bonnefon { 563bb02e0acSNicolas Bonnefon QLocale defaultLocale; 564bb02e0acSNicolas Bonnefon 565812146a8SNicolas Bonnefon LOG(logDEBUG) << "handleLoadingFinished success=" << 566812146a8SNicolas Bonnefon ( status == LoadingStatus::Successful ); 567bb02e0acSNicolas Bonnefon 5680e97f16dSNicolas Bonnefon // No file is loading 5690e97f16dSNicolas Bonnefon loadingFileName.clear(); 5700e97f16dSNicolas Bonnefon 571812146a8SNicolas Bonnefon if ( status == LoadingStatus::Successful ) 572f8bd90d8SNicolas Bonnefon { 5730e97f16dSNicolas Bonnefon // Following should always work as we will only receive enter 5740e97f16dSNicolas Bonnefon // this slot if there is a crawler connected. 5750e97f16dSNicolas Bonnefon QString current_file = 5760e97f16dSNicolas Bonnefon session_->getFilename( currentCrawlerWidget() ).c_str(); 5770e97f16dSNicolas Bonnefon 5780a90ca6aSNicolas Bonnefon uint64_t fileSize; 5790a90ca6aSNicolas Bonnefon uint32_t fileNbLine; 580bb02e0acSNicolas Bonnefon QDateTime lastModified; 581bb02e0acSNicolas Bonnefon 58227ddfd3aSNicolas Bonnefon session_->getFileInfo( currentCrawlerWidget(), 5830a90ca6aSNicolas Bonnefon &fileSize, &fileNbLine, &lastModified ); 584bb02e0acSNicolas Bonnefon if ( lastModified.isValid() ) { 585bb02e0acSNicolas Bonnefon const QString date = 586bb02e0acSNicolas Bonnefon defaultLocale.toString( lastModified, QLocale::NarrowFormat ); 587bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines - modified on %4)" ) 5880e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 589bb02e0acSNicolas Bonnefon .arg(fileNbLine).arg( date ) ); 590bb02e0acSNicolas Bonnefon } 591bb02e0acSNicolas Bonnefon else { 592bb02e0acSNicolas Bonnefon infoLine->setText( tr( "%1 (%2 - %3 lines)" ) 5930e97f16dSNicolas Bonnefon .arg(current_file).arg(readableSize(fileSize)) 594bb02e0acSNicolas Bonnefon .arg(fileNbLine) ); 595bb02e0acSNicolas Bonnefon } 596bb02e0acSNicolas Bonnefon 597bb02e0acSNicolas Bonnefon infoLine->hideGauge(); 598bb02e0acSNicolas Bonnefon stopAction->setEnabled( false ); 599f8bd90d8SNicolas Bonnefon reloadAction->setEnabled( true ); 6000a90ca6aSNicolas Bonnefon 6010a90ca6aSNicolas Bonnefon // Now everything is ready, we can finally show the file! 60227ddfd3aSNicolas Bonnefon currentCrawlerWidget()->show(); 603f8bd90d8SNicolas Bonnefon } 604f8bd90d8SNicolas Bonnefon else 605f8bd90d8SNicolas Bonnefon { 606812146a8SNicolas Bonnefon if ( status == LoadingStatus::NoMemory ) 607812146a8SNicolas Bonnefon { 608812146a8SNicolas Bonnefon QMessageBox alertBox; 609812146a8SNicolas Bonnefon alertBox.setText( "Not enough memory." ); 610812146a8SNicolas Bonnefon alertBox.setInformativeText( "The system does not have enough \ 611812146a8SNicolas Bonnefon memory to hold the index for this file. The file will now be closed." ); 612812146a8SNicolas Bonnefon alertBox.setIcon( QMessageBox::Critical ); 613812146a8SNicolas Bonnefon alertBox.exec(); 614812146a8SNicolas Bonnefon } 615812146a8SNicolas Bonnefon 616f8bd90d8SNicolas Bonnefon closeTab( mainTabWidget_.currentIndex() ); 617f8bd90d8SNicolas Bonnefon } 618f8bd90d8SNicolas Bonnefon 6197875bde2SNicolas Bonnefon // mainTabWidget_.setEnabled( true ); 620bb02e0acSNicolas Bonnefon } 621bb02e0acSNicolas Bonnefon 622cdd89779SNicolas Bonnefon void MainWindow::closeTab( int index ) 623cdd89779SNicolas Bonnefon { 624cdd89779SNicolas Bonnefon auto widget = dynamic_cast<CrawlerWidget*>( 625cdd89779SNicolas Bonnefon mainTabWidget_.widget( index ) ); 626cdd89779SNicolas Bonnefon 627cdd89779SNicolas Bonnefon assert( widget ); 628cdd89779SNicolas Bonnefon 629f8bd90d8SNicolas Bonnefon widget->stopLoading(); 630cdd89779SNicolas Bonnefon mainTabWidget_.removeTab( index ); 631cdd89779SNicolas Bonnefon session_->close( widget ); 632cdd89779SNicolas Bonnefon delete widget; 633cdd89779SNicolas Bonnefon } 634cdd89779SNicolas Bonnefon 635ee835f33SNicolas Bonnefon void MainWindow::currentTabChanged( int index ) 636ee835f33SNicolas Bonnefon { 637ee835f33SNicolas Bonnefon LOG(logDEBUG) << "currentTabChanged"; 638ee835f33SNicolas Bonnefon 639ee835f33SNicolas Bonnefon if ( index >= 0 ) 640ee835f33SNicolas Bonnefon { 641ee835f33SNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 642ee835f33SNicolas Bonnefon mainTabWidget_.widget( index ) ); 643ee835f33SNicolas Bonnefon signalMux_.setCurrentDocument( crawler_widget ); 644ee835f33SNicolas Bonnefon quickFindMux_.registerSelector( crawler_widget ); 645ee835f33SNicolas Bonnefon 646ee835f33SNicolas Bonnefon // New tab is set up with fonts etc... 647ee835f33SNicolas Bonnefon emit optionsChanged(); 6480e97f16dSNicolas Bonnefon 6490e97f16dSNicolas Bonnefon // Update the title bar 6500e97f16dSNicolas Bonnefon updateTitleBar( QString( 6510e97f16dSNicolas Bonnefon session_->getFilename( crawler_widget ).c_str() ) ); 652ee835f33SNicolas Bonnefon } 653ee835f33SNicolas Bonnefon else 654ee835f33SNicolas Bonnefon { 655a1202e0cSNicolas Bonnefon // No tab left 656a1202e0cSNicolas Bonnefon signalMux_.setCurrentDocument( nullptr ); 657a1202e0cSNicolas Bonnefon quickFindMux_.registerSelector( nullptr ); 658a1202e0cSNicolas Bonnefon 659a1202e0cSNicolas Bonnefon infoLine->hideGauge(); 660a1202e0cSNicolas Bonnefon infoLine->clear(); 661a1202e0cSNicolas Bonnefon 662a1202e0cSNicolas Bonnefon updateTitleBar( QString() ); 663ee835f33SNicolas Bonnefon } 664ee835f33SNicolas Bonnefon } 665ee835f33SNicolas Bonnefon 6668570d8d2SNicolas Bonnefon void MainWindow::changeQFPattern( const QString& newPattern ) 6678570d8d2SNicolas Bonnefon { 6688570d8d2SNicolas Bonnefon quickFindWidget_.changeDisplayedPattern( newPattern ); 6698570d8d2SNicolas Bonnefon } 6708570d8d2SNicolas Bonnefon 67109aff35dSNicolas Bonnefon void MainWindow::loadFileNonInteractive( const QString& file_name ) 67209aff35dSNicolas Bonnefon { 67309aff35dSNicolas Bonnefon LOG(logDEBUG) << "loadFileNonInteractive( " 67409aff35dSNicolas Bonnefon << file_name.toStdString() << " )"; 67509aff35dSNicolas Bonnefon 67609aff35dSNicolas Bonnefon loadFile( file_name ); 677*67cec333SNicolas Bonnefon 678*67cec333SNicolas Bonnefon // Try to get the window to the front 679*67cec333SNicolas Bonnefon // This is a bit of a hack but has been tested on: 680*67cec333SNicolas Bonnefon // Qt 5.3 / Gnome / Linux 681*67cec333SNicolas Bonnefon Qt::WindowFlags window_flags = windowFlags(); 682*67cec333SNicolas Bonnefon window_flags |= Qt::WindowStaysOnTopHint; 683*67cec333SNicolas Bonnefon setWindowFlags( window_flags ); 684*67cec333SNicolas Bonnefon 685*67cec333SNicolas Bonnefon activateWindow(); 686*67cec333SNicolas Bonnefon raise(); 687*67cec333SNicolas Bonnefon 688*67cec333SNicolas Bonnefon window_flags = windowFlags(); 689*67cec333SNicolas Bonnefon window_flags &= ~Qt::WindowStaysOnTopHint; 690*67cec333SNicolas Bonnefon setWindowFlags( window_flags ); 691*67cec333SNicolas Bonnefon 692*67cec333SNicolas Bonnefon show(); 69309aff35dSNicolas Bonnefon } 69409aff35dSNicolas Bonnefon 695460de700SNicolas Bonnefon void MainWindow::newVersionNotification( const QString& new_version ) 696460de700SNicolas Bonnefon { 697460de700SNicolas Bonnefon LOG(logDEBUG) << "newVersionNotification( " << 698460de700SNicolas Bonnefon new_version.toStdString() << " )"; 699460de700SNicolas Bonnefon 700460de700SNicolas Bonnefon QMessageBox msgBox; 701460de700SNicolas Bonnefon msgBox.setText( QString( "A new version of glogg (%1) is available for download <p>" 702460de700SNicolas Bonnefon "<a href=\"http://glogg.bonnefon.org/download.html\">http://glogg.bonnefon.org/download.html</a>" 703460de700SNicolas Bonnefon ).arg( new_version ) ); 704460de700SNicolas Bonnefon msgBox.exec(); 705460de700SNicolas Bonnefon } 706460de700SNicolas Bonnefon 707bb02e0acSNicolas Bonnefon // 708bb02e0acSNicolas Bonnefon // Events 709bb02e0acSNicolas Bonnefon // 710bb02e0acSNicolas Bonnefon 711bb02e0acSNicolas Bonnefon // Closes the application 712bb02e0acSNicolas Bonnefon void MainWindow::closeEvent( QCloseEvent *event ) 713bb02e0acSNicolas Bonnefon { 714bb02e0acSNicolas Bonnefon writeSettings(); 715bb02e0acSNicolas Bonnefon event->accept(); 716bb02e0acSNicolas Bonnefon } 717bb02e0acSNicolas Bonnefon 718bb02e0acSNicolas Bonnefon // Accepts the drag event if it looks like a filename 719bb02e0acSNicolas Bonnefon void MainWindow::dragEnterEvent( QDragEnterEvent* event ) 720bb02e0acSNicolas Bonnefon { 721bb02e0acSNicolas Bonnefon if ( event->mimeData()->hasFormat( "text/uri-list" ) ) 722bb02e0acSNicolas Bonnefon event->acceptProposedAction(); 723bb02e0acSNicolas Bonnefon } 724bb02e0acSNicolas Bonnefon 725bb02e0acSNicolas Bonnefon // Tries and loads the file if the URL dropped is local 726bb02e0acSNicolas Bonnefon void MainWindow::dropEvent( QDropEvent* event ) 727bb02e0acSNicolas Bonnefon { 728bb02e0acSNicolas Bonnefon QList<QUrl> urls = event->mimeData()->urls(); 729bb02e0acSNicolas Bonnefon if ( urls.isEmpty() ) 730bb02e0acSNicolas Bonnefon return; 731bb02e0acSNicolas Bonnefon 732bb02e0acSNicolas Bonnefon QString fileName = urls.first().toLocalFile(); 733bb02e0acSNicolas Bonnefon if ( fileName.isEmpty() ) 734bb02e0acSNicolas Bonnefon return; 735bb02e0acSNicolas Bonnefon 736bb02e0acSNicolas Bonnefon loadFile( fileName ); 737bb02e0acSNicolas Bonnefon } 738bb02e0acSNicolas Bonnefon 7398570d8d2SNicolas Bonnefon void MainWindow::keyPressEvent( QKeyEvent* keyEvent ) 7408570d8d2SNicolas Bonnefon { 7418570d8d2SNicolas Bonnefon LOG(logDEBUG4) << "keyPressEvent received"; 7428570d8d2SNicolas Bonnefon 7438570d8d2SNicolas Bonnefon switch ( (keyEvent->text())[0].toLatin1() ) { 7448570d8d2SNicolas Bonnefon case '/': 7458570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Forward ); 7468570d8d2SNicolas Bonnefon break; 7478570d8d2SNicolas Bonnefon case '?': 7488570d8d2SNicolas Bonnefon displayQuickFindBar( QuickFindMux::Backward ); 7498570d8d2SNicolas Bonnefon break; 7508570d8d2SNicolas Bonnefon default: 7518570d8d2SNicolas Bonnefon keyEvent->ignore(); 7528570d8d2SNicolas Bonnefon } 7538570d8d2SNicolas Bonnefon 7548570d8d2SNicolas Bonnefon if ( !keyEvent->isAccepted() ) 7558570d8d2SNicolas Bonnefon QMainWindow::keyPressEvent( keyEvent ); 7568570d8d2SNicolas Bonnefon } 7578570d8d2SNicolas Bonnefon 758bb02e0acSNicolas Bonnefon // 759bb02e0acSNicolas Bonnefon // Private functions 760bb02e0acSNicolas Bonnefon // 761bb02e0acSNicolas Bonnefon 7620a90ca6aSNicolas Bonnefon // Create a CrawlerWidget for the passed file, start its loading 7630a90ca6aSNicolas Bonnefon // and update the title bar. 764bb02e0acSNicolas Bonnefon // The loading is done asynchronously. 765bb02e0acSNicolas Bonnefon bool MainWindow::loadFile( const QString& fileName ) 766bb02e0acSNicolas Bonnefon { 767bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "loadFile ( " << fileName.toStdString() << " )"; 768bb02e0acSNicolas Bonnefon 7693b4aad7fSNicolas Bonnefon // First check if the file is already open... 7703b4aad7fSNicolas Bonnefon CrawlerWidget* existing_crawler = dynamic_cast<CrawlerWidget*>( 7713b4aad7fSNicolas Bonnefon session_->getViewIfOpen( fileName.toStdString() ) ); 7723b4aad7fSNicolas Bonnefon if ( existing_crawler ) { 7733b4aad7fSNicolas Bonnefon // ... and switch to it. 7743b4aad7fSNicolas Bonnefon mainTabWidget_.setCurrentWidget( existing_crawler ); 7753b4aad7fSNicolas Bonnefon 7763b4aad7fSNicolas Bonnefon return true; 7773b4aad7fSNicolas Bonnefon } 7783b4aad7fSNicolas Bonnefon 779bb02e0acSNicolas Bonnefon // Load the file 780bb02e0acSNicolas Bonnefon loadingFileName = fileName; 781039481acSNicolas Bonnefon 782a3b56311SNicolas Bonnefon try { 78327ddfd3aSNicolas Bonnefon CrawlerWidget* crawler_widget = dynamic_cast<CrawlerWidget*>( 78427ddfd3aSNicolas Bonnefon session_->open( fileName.toStdString(), 7851b5e406eSNicolas Bonnefon []() { return new CrawlerWidget(); } ) ); 7861b5e406eSNicolas Bonnefon assert( crawler_widget ); 787f0708ca8SNicolas Bonnefon 7880a90ca6aSNicolas Bonnefon // We won't show the widget until the file is fully loaded 78927ddfd3aSNicolas Bonnefon crawler_widget->hide(); 790f0708ca8SNicolas Bonnefon 79127ddfd3aSNicolas Bonnefon // We disable the tab widget to avoid having someone switch 79227ddfd3aSNicolas Bonnefon // tab during loading. (maybe FIXME) 7937875bde2SNicolas Bonnefon //mainTabWidget_.setEnabled( false ); 794313a820fSNicolas Bonnefon 795a3b56311SNicolas Bonnefon int index = mainTabWidget_.addTab( 796a3b56311SNicolas Bonnefon crawler_widget, strippedName( fileName ) ); 797f0708ca8SNicolas Bonnefon 79827ddfd3aSNicolas Bonnefon // Setting the new tab, the user will see a blank page for the duration 79927ddfd3aSNicolas Bonnefon // of the loading, with no way to switch to another tab 80027ddfd3aSNicolas Bonnefon mainTabWidget_.setCurrentIndex( index ); 80127ddfd3aSNicolas Bonnefon 80260864ff5SNicolas Bonnefon // Update the recent files list 80360864ff5SNicolas Bonnefon // (reload the list first in case another glogg changed it) 80460864ff5SNicolas Bonnefon GetPersistentInfo().retrieve( "recentFiles" ); 80511582726SNicolas Bonnefon recentFiles_->addRecent( fileName ); 80660864ff5SNicolas Bonnefon GetPersistentInfo().save( "recentFiles" ); 80760864ff5SNicolas Bonnefon updateRecentFileActions(); 808a3b56311SNicolas Bonnefon } 809a3b56311SNicolas Bonnefon catch ( FileUnreadableErr ) { 810a3b56311SNicolas Bonnefon LOG(logDEBUG) << "Can't open file " << fileName.toStdString(); 811a3b56311SNicolas Bonnefon return false; 812a3b56311SNicolas Bonnefon } 81360864ff5SNicolas Bonnefon 814bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Success loading file " << fileName.toStdString(); 815bb02e0acSNicolas Bonnefon return true; 816a3b56311SNicolas Bonnefon 817bb02e0acSNicolas Bonnefon } 818bb02e0acSNicolas Bonnefon 819bb02e0acSNicolas Bonnefon // Strips the passed filename from its directory part. 820bb02e0acSNicolas Bonnefon QString MainWindow::strippedName( const QString& fullFileName ) const 821bb02e0acSNicolas Bonnefon { 822bb02e0acSNicolas Bonnefon return QFileInfo( fullFileName ).fileName(); 823bb02e0acSNicolas Bonnefon } 824bb02e0acSNicolas Bonnefon 82527ddfd3aSNicolas Bonnefon // Return the currently active CrawlerWidget, or NULL if none 82627ddfd3aSNicolas Bonnefon CrawlerWidget* MainWindow::currentCrawlerWidget() const 82727ddfd3aSNicolas Bonnefon { 82827ddfd3aSNicolas Bonnefon auto current = dynamic_cast<CrawlerWidget*>( 82927ddfd3aSNicolas Bonnefon mainTabWidget_.currentWidget() ); 83027ddfd3aSNicolas Bonnefon 83127ddfd3aSNicolas Bonnefon return current; 83227ddfd3aSNicolas Bonnefon } 83327ddfd3aSNicolas Bonnefon 8340e97f16dSNicolas Bonnefon // Update the title bar. 8350e97f16dSNicolas Bonnefon void MainWindow::updateTitleBar( const QString& file_name ) 836bb02e0acSNicolas Bonnefon { 837bb02e0acSNicolas Bonnefon QString shownName = tr( "Untitled" ); 8380e97f16dSNicolas Bonnefon if ( !file_name.isEmpty() ) 8390e97f16dSNicolas Bonnefon shownName = strippedName( file_name ); 840bb02e0acSNicolas Bonnefon 841bb02e0acSNicolas Bonnefon setWindowTitle( 842bb02e0acSNicolas Bonnefon tr("%1 - %2").arg(shownName).arg(tr("glogg")) 843bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 844bb02e0acSNicolas Bonnefon + " (dev build " GLOGG_VERSION ")" 845bb02e0acSNicolas Bonnefon #endif 846bb02e0acSNicolas Bonnefon ); 847bb02e0acSNicolas Bonnefon } 848bb02e0acSNicolas Bonnefon 849bb02e0acSNicolas Bonnefon // Updates the actions for the recent files. 850bb02e0acSNicolas Bonnefon // Must be called after having added a new name to the list. 851bb02e0acSNicolas Bonnefon void MainWindow::updateRecentFileActions() 852bb02e0acSNicolas Bonnefon { 85311582726SNicolas Bonnefon QStringList recent_files = recentFiles_->recentFiles(); 854bb02e0acSNicolas Bonnefon 855bb02e0acSNicolas Bonnefon for ( int j = 0; j < MaxRecentFiles; ++j ) { 856bb02e0acSNicolas Bonnefon if ( j < recent_files.count() ) { 857bb02e0acSNicolas Bonnefon QString text = tr("&%1 %2").arg(j + 1).arg(strippedName(recent_files[j])); 858bb02e0acSNicolas Bonnefon recentFileActions[j]->setText( text ); 859bb02e0acSNicolas Bonnefon recentFileActions[j]->setToolTip( recent_files[j] ); 860bb02e0acSNicolas Bonnefon recentFileActions[j]->setData( recent_files[j] ); 861bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( true ); 862bb02e0acSNicolas Bonnefon } 863bb02e0acSNicolas Bonnefon else { 864bb02e0acSNicolas Bonnefon recentFileActions[j]->setVisible( false ); 865bb02e0acSNicolas Bonnefon } 866bb02e0acSNicolas Bonnefon } 867bb02e0acSNicolas Bonnefon 868bb02e0acSNicolas Bonnefon // separatorAction->setVisible(!recentFiles.isEmpty()); 869bb02e0acSNicolas Bonnefon } 870bb02e0acSNicolas Bonnefon 871bb02e0acSNicolas Bonnefon // Write settings to permanent storage 872bb02e0acSNicolas Bonnefon void MainWindow::writeSettings() 873bb02e0acSNicolas Bonnefon { 874bb02e0acSNicolas Bonnefon // Save the session 875b57881faSNicolas Bonnefon // Generate the ordered list of widgets and their topLine 876a44d09bcSNicolas Bonnefon std::vector< 877a44d09bcSNicolas Bonnefon std::tuple<const ViewInterface*, uint64_t, std::shared_ptr<const ViewContextInterface>> 878a44d09bcSNicolas Bonnefon > widget_list; 879b57881faSNicolas Bonnefon for ( int i = 0; i < mainTabWidget_.count(); ++i ) 880a44d09bcSNicolas Bonnefon { 881a44d09bcSNicolas Bonnefon auto view = dynamic_cast<const ViewInterface*>( mainTabWidget_.widget( i ) ); 882a44d09bcSNicolas Bonnefon widget_list.push_back( std::make_tuple( 883a44d09bcSNicolas Bonnefon view, 884a44d09bcSNicolas Bonnefon 0UL, 885a44d09bcSNicolas Bonnefon view->context() ) ); 886a44d09bcSNicolas Bonnefon } 887b76966a6SNicolas Bonnefon session_->save( widget_list, saveGeometry() ); 888bb02e0acSNicolas Bonnefon 889bb02e0acSNicolas Bonnefon // User settings 890bb02e0acSNicolas Bonnefon GetPersistentInfo().save( QString( "settings" ) ); 891bb02e0acSNicolas Bonnefon } 892bb02e0acSNicolas Bonnefon 893bb02e0acSNicolas Bonnefon // Read settings from permanent storage 894bb02e0acSNicolas Bonnefon void MainWindow::readSettings() 895bb02e0acSNicolas Bonnefon { 896bb02e0acSNicolas Bonnefon // Get and restore the session 897b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "session" ) ); 898b57881faSNicolas Bonnefon // SessionInfo session = Persistent<SessionInfo>( "session" ); 8990a90ca6aSNicolas Bonnefon /* 9000a90ca6aSNicolas Bonnefon * FIXME: should be in the session 9010a90ca6aSNicolas Bonnefon crawlerWidget->restoreState( session.crawlerState() ); 9020a90ca6aSNicolas Bonnefon */ 903bb02e0acSNicolas Bonnefon 904bb02e0acSNicolas Bonnefon // History of recent files 905bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "recentFiles" ) ); 906bb02e0acSNicolas Bonnefon updateRecentFileActions(); 907bb02e0acSNicolas Bonnefon 908b57881faSNicolas Bonnefon // GetPersistentInfo().retrieve( QString( "settings" ) ); 909bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "filterSet" ) ); 910bb02e0acSNicolas Bonnefon } 911bb02e0acSNicolas Bonnefon 912b423cd88SNicolas Bonnefon void MainWindow::displayQuickFindBar( QuickFindMux::QFDirection direction ) 913b423cd88SNicolas Bonnefon { 914b423cd88SNicolas Bonnefon LOG(logDEBUG) << "MainWindow::displayQuickFindBar"; 915b423cd88SNicolas Bonnefon 9168570d8d2SNicolas Bonnefon // Warn crawlers so they can save the position of the focus in order 9178570d8d2SNicolas Bonnefon // to do incremental search in the right view. 9188570d8d2SNicolas Bonnefon emit enteringQuickFind(); 919b423cd88SNicolas Bonnefon 920b423cd88SNicolas Bonnefon quickFindMux_.setDirection( direction ); 921b423cd88SNicolas Bonnefon quickFindWidget_.userActivate(); 922b423cd88SNicolas Bonnefon } 923b423cd88SNicolas Bonnefon 924bb02e0acSNicolas Bonnefon // Returns the size in human readable format 9250a90ca6aSNicolas Bonnefon static QString readableSize( qint64 size ) 926bb02e0acSNicolas Bonnefon { 927bb02e0acSNicolas Bonnefon static const QString sizeStrs[] = { 9280a90ca6aSNicolas Bonnefon QObject::tr("B"), QObject::tr("KiB"), QObject::tr("MiB"), 9290a90ca6aSNicolas Bonnefon QObject::tr("GiB"), QObject::tr("TiB") }; 930bb02e0acSNicolas Bonnefon 931bb02e0acSNicolas Bonnefon QLocale defaultLocale; 932bb02e0acSNicolas Bonnefon unsigned int i; 933bb02e0acSNicolas Bonnefon double humanSize = size; 934bb02e0acSNicolas Bonnefon 935bb02e0acSNicolas Bonnefon for ( i=0; i+1 < (sizeof(sizeStrs)/sizeof(QString)) && (humanSize/1024.0) >= 1024.0; i++ ) 936bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 937bb02e0acSNicolas Bonnefon 938bb02e0acSNicolas Bonnefon if ( humanSize >= 1024.0 ) { 939bb02e0acSNicolas Bonnefon humanSize /= 1024.0; 940bb02e0acSNicolas Bonnefon i++; 941bb02e0acSNicolas Bonnefon } 942bb02e0acSNicolas Bonnefon 943bb02e0acSNicolas Bonnefon QString output; 944bb02e0acSNicolas Bonnefon if ( i == 0 ) 945bb02e0acSNicolas Bonnefon // No decimal part if we display straight bytes. 946bb02e0acSNicolas Bonnefon output = defaultLocale.toString( (int) humanSize ); 947bb02e0acSNicolas Bonnefon else 948bb02e0acSNicolas Bonnefon output = defaultLocale.toString( humanSize, 'f', 1 ); 949bb02e0acSNicolas Bonnefon 950bb02e0acSNicolas Bonnefon output += QString(" ") + sizeStrs[i]; 951bb02e0acSNicolas Bonnefon 952bb02e0acSNicolas Bonnefon return output; 953bb02e0acSNicolas Bonnefon } 954