/src/libtomahawk/playlist/ViewHeader.cpp

http://github.com/tomahawk-player/tomahawk · 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. #include "ViewHeader.h"
  19. #include <QContextMenuEvent>
  20. #include <QMenu>
  21. #include "TomahawkSettings.h"
  22. #include "utils/TomahawkUtilsGui.h"
  23. #include "utils/Logger.h"
  24. ViewHeader::ViewHeader( QAbstractItemView* parent )
  25. : QHeaderView( Qt::Horizontal, parent )
  26. , m_parent( parent )
  27. , m_menu( new QMenu( this ) )
  28. , m_sigmap( new QSignalMapper( this ) )
  29. , m_init( false )
  30. {
  31. m_menu->setFont( TomahawkUtils::systemFont() );
  32. setSectionResizeMode( QHeaderView::Interactive );
  33. setSectionsMovable( true );
  34. setMinimumSectionSize( 60 );
  35. setDefaultAlignment( Qt::AlignLeft );
  36. setStretchLastSection( true );
  37. // m_menu->addAction( tr( "Resize columns to fit window" ), this, SLOT( onToggleResizeColumns() ) );
  38. // m_menu->addSeparator();
  39. connect( m_sigmap, SIGNAL( mapped( int ) ), SLOT( toggleVisibility( int ) ) );
  40. }
  41. ViewHeader::~ViewHeader()
  42. {
  43. }
  44. int
  45. ViewHeader::visibleSectionCount() const
  46. {
  47. return count() - hiddenSectionCount();
  48. }
  49. void
  50. ViewHeader::onSectionsChanged()
  51. {
  52. tDebug( LOGVERBOSE ) << "Saving columns state for view guid:" << m_guid;
  53. if ( !m_guid.isEmpty() )
  54. TomahawkSettings::instance()->setPlaylistColumnSizes( m_guid, saveState() );
  55. }
  56. bool
  57. ViewHeader::checkState()
  58. {
  59. if ( !count() || m_init )
  60. return false;
  61. disconnect( this, SIGNAL( sectionMoved( int, int, int ) ), this, SLOT( onSectionsChanged() ) );
  62. disconnect( this, SIGNAL( sectionResized( int, int, int ) ), this, SLOT( onSectionsChanged() ) );
  63. QByteArray state;
  64. if ( !m_guid.isEmpty() )
  65. state = TomahawkSettings::instance()->playlistColumnSizes( m_guid );
  66. if ( !state.isEmpty() )
  67. {
  68. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Restoring columns state for view:" << m_guid;
  69. restoreState( state );
  70. }
  71. else
  72. {
  73. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Giving columns of view" << m_guid << "initial weighting:" << m_columnWeights << "for" << count() << "columns";
  74. for ( int i = 0; i < count() - 1; i++ )
  75. {
  76. if ( isSectionHidden( i ) )
  77. continue;
  78. if ( i >= m_columnWeights.count() )
  79. break;
  80. double nw = (double)m_parent->width() * m_columnWeights.at( i );
  81. resizeSection( i, qMax( minimumSectionSize(), int( nw - 0.5 ) ) );
  82. }
  83. }
  84. connect( this, SIGNAL( sectionMoved( int, int, int ) ), SLOT( onSectionsChanged() ) );
  85. connect( this, SIGNAL( sectionResized( int, int, int ) ), SLOT( onSectionsChanged() ) );
  86. m_init = true;
  87. return true;
  88. }
  89. void
  90. ViewHeader::addColumnToMenu( int index )
  91. {
  92. QString title = m_parent->model()->headerData( index, Qt::Horizontal, Qt::DisplayRole ).toString();
  93. QAction* action = m_menu->addAction( title, m_sigmap, SLOT( map() ) );
  94. action->setCheckable( true );
  95. action->setChecked( !isSectionHidden( index ) );
  96. m_visActions << action;
  97. m_sigmap->setMapping( action, index );
  98. }
  99. void
  100. ViewHeader::contextMenuEvent( QContextMenuEvent* e )
  101. {
  102. qDeleteAll( m_visActions );
  103. m_visActions.clear();
  104. for ( int i = 0; i < count(); i++ )
  105. addColumnToMenu( i );
  106. m_menu->popup( e->globalPos() );
  107. }
  108. void
  109. ViewHeader::onToggleResizeColumns()
  110. {
  111. }
  112. void
  113. ViewHeader::toggleVisibility( int index )
  114. {
  115. if ( isSectionHidden( index ) )
  116. showSection( index );
  117. else
  118. hideSection( index );
  119. }
  120. void
  121. ViewHeader::setGuid( const QString& guid )
  122. {
  123. m_guid = guid;
  124. // If we are _not_ initialized yet, this means we're still waiting for the view to resize initially.
  125. // We need to wait with restoring our state (column sizes) until that has happened.
  126. if ( m_init )
  127. {
  128. m_init = false;
  129. checkState();
  130. }
  131. }