1 /* 2 * File name: MainWindowLayout.cpp 3 * Summary: QDirStat main window layout-related functions 4 * License: GPL V2 - See file LICENSE for details. 5 * 6 * Author: Stefan Hundhammer <Stefan.Hundhammer@gmx.de> 7 */ 8 9 10 #include "MainWindow.h" 11 #include "QDirStatApp.h" 12 #include "HeaderTweaker.h" 13 #include "Settings.h" 14 #include "Exception.h" 15 #include "Logger.h" 16 17 using namespace QDirStat; 18 19 createLayouts()20void MainWindow::createLayouts() 21 { 22 // Notice that the column layouts are handled in the HeaderTweaker and its 23 // ColumnLayout helper class; see also HeaderTweaker.h and .cpp. 24 // 25 // The layout names "L1", "L2", "L3" here are important: They need to match 26 // the names in the HeaderTweaker. 27 28 TreeLayout * layout; 29 30 layout = new TreeLayout( "L1" ); 31 CHECK_NEW( layout ); 32 _layouts[ "L1" ] = layout; 33 34 layout = new TreeLayout( "L2" ); 35 CHECK_NEW( layout ); 36 _layouts[ "L2" ] = layout; 37 38 layout = new TreeLayout( "L3" ); 39 CHECK_NEW( layout ); 40 _layouts[ "L3" ] = layout; 41 42 // L3 is the only one where the defaults for the flags need changing. 43 layout->showDetailsPanel = false; 44 } 45 46 initLayoutActions()47void MainWindow::initLayoutActions() 48 { 49 // Qt Designer does not support QActionGroups; it was there for Qt 3, but 50 // they dropped that feature for Qt 4/5. 51 52 _layoutActionGroup = new QActionGroup( this ); 53 CHECK_NEW( _layoutActionGroup ); 54 55 _layoutActionGroup->addAction( _ui->actionLayout1 ); 56 _layoutActionGroup->addAction( _ui->actionLayout2 ); 57 _layoutActionGroup->addAction( _ui->actionLayout3 ); 58 59 _ui->actionLayout1->setData( "L1" ); 60 _ui->actionLayout2->setData( "L2" ); 61 _ui->actionLayout3->setData( "L3" ); 62 } 63 64 changeLayout(const QString & name)65void MainWindow::changeLayout( const QString & name ) 66 { 67 _layoutName = name; 68 69 if ( _layoutName.isEmpty() ) 70 { 71 // Get the layout to use from data() from the QAction that sent the signal. 72 73 QAction * action = qobject_cast<QAction *>( sender() ); 74 _layoutName = action && action->data().isValid() ? 75 action->data().toString() : "L2"; 76 } 77 78 logDebug() << "Changing to layout " << _layoutName << endl; 79 80 _ui->dirTreeView->headerTweaker()->changeLayout( _layoutName ); 81 82 if ( _currentLayout ) 83 saveLayout( _currentLayout ); 84 85 if ( _layouts.contains( _layoutName ) ) 86 { 87 _currentLayout = _layouts[ _layoutName ]; 88 applyLayout( _currentLayout ); 89 } 90 else 91 { 92 logError() << "No layout " << _layoutName << endl; 93 } 94 } 95 96 saveLayout(TreeLayout * layout)97void MainWindow::saveLayout( TreeLayout * layout ) 98 { 99 CHECK_PTR( layout ); 100 101 layout->showCurrentPath = _ui->actionShowCurrentPath->isChecked(); 102 layout->showDetailsPanel = _ui->actionShowDetailsPanel->isChecked(); 103 } 104 105 applyLayout(TreeLayout * layout)106void MainWindow::applyLayout( TreeLayout * layout ) 107 { 108 CHECK_PTR( layout ); 109 110 _ui->actionShowCurrentPath->setChecked ( layout->showCurrentPath ); 111 _ui->actionShowDetailsPanel->setChecked( layout->showDetailsPanel ); 112 } 113 114 readLayoutSettings(TreeLayout * layout)115void MainWindow::readLayoutSettings( TreeLayout * layout ) 116 { 117 CHECK_PTR( layout ); 118 119 Settings settings; 120 settings.beginGroup( QString( "TreeViewLayout_%1" ).arg( layout->name ) ); 121 122 layout->showCurrentPath = settings.value( "ShowCurrentPath" , layout->showCurrentPath ).toBool(); 123 layout->showDetailsPanel = settings.value( "ShowDetailsPanel", layout->showDetailsPanel ).toBool(); 124 125 settings.endGroup(); 126 } 127 128 writeLayoutSettings(TreeLayout * layout)129void MainWindow::writeLayoutSettings( TreeLayout * layout ) 130 { 131 CHECK_PTR( layout ); 132 133 Settings settings; 134 settings.beginGroup( QString( "TreeViewLayout_%1" ).arg( layout->name ) ); 135 136 settings.setValue( "ShowCurrentPath" , layout->showCurrentPath ); 137 settings.setValue( "ShowDetailsPanel", layout->showDetailsPanel ); 138 139 settings.endGroup(); 140 } 141 142