/src/libtomahawk/playlist/ViewHeader.cpp
C++ | 168 lines | 110 code | 37 blank | 21 comment | 11 complexity | 979d24c0ea12440c0d1a2f08d2ed40fe MD5 | raw file
1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === 2 * 3 * Copyright 2010-2015, Christian Muehlhaeuser <muesli@tomahawk-player.org> 4 * 5 * Tomahawk is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * Tomahawk is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19#include "ViewHeader.h" 20 21#include <QContextMenuEvent> 22#include <QMenu> 23 24#include "TomahawkSettings.h" 25#include "utils/TomahawkUtilsGui.h" 26#include "utils/Logger.h" 27 28 29ViewHeader::ViewHeader( QAbstractItemView* parent ) 30 : QHeaderView( Qt::Horizontal, parent ) 31 , m_parent( parent ) 32 , m_menu( new QMenu( this ) ) 33 , m_sigmap( new QSignalMapper( this ) ) 34 , m_init( false ) 35{ 36 m_menu->setFont( TomahawkUtils::systemFont() ); 37 38 setSectionResizeMode( QHeaderView::Interactive ); 39 setSectionsMovable( true ); 40 setMinimumSectionSize( 60 ); 41 setDefaultAlignment( Qt::AlignLeft ); 42 setStretchLastSection( true ); 43 44// m_menu->addAction( tr( "Resize columns to fit window" ), this, SLOT( onToggleResizeColumns() ) ); 45// m_menu->addSeparator(); 46 47 connect( m_sigmap, SIGNAL( mapped( int ) ), SLOT( toggleVisibility( int ) ) ); 48} 49 50 51ViewHeader::~ViewHeader() 52{ 53} 54 55 56int 57ViewHeader::visibleSectionCount() const 58{ 59 return count() - hiddenSectionCount(); 60} 61 62 63void 64ViewHeader::onSectionsChanged() 65{ 66 tDebug( LOGVERBOSE ) << "Saving columns state for view guid:" << m_guid; 67 if ( !m_guid.isEmpty() ) 68 TomahawkSettings::instance()->setPlaylistColumnSizes( m_guid, saveState() ); 69} 70 71 72bool 73ViewHeader::checkState() 74{ 75 if ( !count() || m_init ) 76 return false; 77 78 disconnect( this, SIGNAL( sectionMoved( int, int, int ) ), this, SLOT( onSectionsChanged() ) ); 79 disconnect( this, SIGNAL( sectionResized( int, int, int ) ), this, SLOT( onSectionsChanged() ) ); 80 81 QByteArray state; 82 if ( !m_guid.isEmpty() ) 83 state = TomahawkSettings::instance()->playlistColumnSizes( m_guid ); 84 85 if ( !state.isEmpty() ) 86 { 87 tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Restoring columns state for view:" << m_guid; 88 restoreState( state ); 89 } 90 else 91 { 92 tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Giving columns of view" << m_guid << "initial weighting:" << m_columnWeights << "for" << count() << "columns"; 93 for ( int i = 0; i < count() - 1; i++ ) 94 { 95 if ( isSectionHidden( i ) ) 96 continue; 97 if ( i >= m_columnWeights.count() ) 98 break; 99 100 double nw = (double)m_parent->width() * m_columnWeights.at( i ); 101 resizeSection( i, qMax( minimumSectionSize(), int( nw - 0.5 ) ) ); 102 } 103 } 104 105 connect( this, SIGNAL( sectionMoved( int, int, int ) ), SLOT( onSectionsChanged() ) ); 106 connect( this, SIGNAL( sectionResized( int, int, int ) ), SLOT( onSectionsChanged() ) ); 107 108 m_init = true; 109 return true; 110} 111 112 113void 114ViewHeader::addColumnToMenu( int index ) 115{ 116 QString title = m_parent->model()->headerData( index, Qt::Horizontal, Qt::DisplayRole ).toString(); 117 118 QAction* action = m_menu->addAction( title, m_sigmap, SLOT( map() ) ); 119 action->setCheckable( true ); 120 action->setChecked( !isSectionHidden( index ) ); 121 m_visActions << action; 122 123 m_sigmap->setMapping( action, index ); 124} 125 126 127void 128ViewHeader::contextMenuEvent( QContextMenuEvent* e ) 129{ 130 qDeleteAll( m_visActions ); 131 m_visActions.clear(); 132 133 for ( int i = 0; i < count(); i++ ) 134 addColumnToMenu( i ); 135 136 m_menu->popup( e->globalPos() ); 137} 138 139 140void 141ViewHeader::onToggleResizeColumns() 142{ 143} 144 145 146void 147ViewHeader::toggleVisibility( int index ) 148{ 149 if ( isSectionHidden( index ) ) 150 showSection( index ); 151 else 152 hideSection( index ); 153} 154 155 156void 157ViewHeader::setGuid( const QString& guid ) 158{ 159 m_guid = guid; 160 161 // If we are _not_ initialized yet, this means we're still waiting for the view to resize initially. 162 // We need to wait with restoring our state (column sizes) until that has happened. 163 if ( m_init ) 164 { 165 m_init = false; 166 checkState(); 167 } 168}