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