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