/src/libtomahawk/jobview/JobStatusView.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 234 lines · 176 code · 40 blank · 18 comment · 23 complexity · 0c2d3acfacdd3da8d2e3abf684422429 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2011, Leo Franchi <lfranchi@kde.org>
  5. *
  6. * Tomahawk 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. * Tomahawk 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 Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "JobStatusView.h"
  20. #include "Pipeline.h"
  21. #include "AclJobItem.h"
  22. #include "JobStatusModel.h"
  23. #include "JobStatusItem.h"
  24. #include "JobStatusDelegate.h"
  25. #include "utils/Logger.h"
  26. #include "Source.h"
  27. #include "IndexingJobItem.h"
  28. #include "PipelineStatusItem.h"
  29. #include "ScannerStatusItem.h"
  30. #include "TransferStatusItem.h"
  31. #include "LatchedStatusItem.h"
  32. #include <QHeaderView>
  33. #include <QVBoxLayout>
  34. #include <QListView>
  35. #include <QAbstractItemModel>
  36. using namespace Tomahawk;
  37. JobStatusView* JobStatusView::s_instance = 0;
  38. QList< QPointer< JobStatusItem > > s_jobItems;
  39. void
  40. JobStatusView::addJob( JobStatusItem* item )
  41. {
  42. if ( s_instance == 0 || s_instance->model() == 0 )
  43. {
  44. s_jobItems.append( QPointer<JobStatusItem>( item ) );
  45. }
  46. else
  47. {
  48. s_instance->model()->addJob( item );
  49. }
  50. }
  51. void
  52. JobStatusView::addJob( const QPointer<JobStatusItem>& item )
  53. {
  54. if ( s_instance == 0 || s_instance->model() == 0 )
  55. {
  56. s_jobItems.append( item );
  57. }
  58. else
  59. {
  60. s_instance->model()->addJob( item.data() );
  61. }
  62. }
  63. JobStatusView::JobStatusView( AnimatedSplitter* parent )
  64. : AnimatedWidget( parent )
  65. , m_model( 0 )
  66. , m_parent( parent )
  67. , m_cachedHeight( -1 )
  68. {
  69. s_instance = this;
  70. setHiddenSize( QSize( 0, 0 ) );
  71. setLayout( new QVBoxLayout() );
  72. m_view = new QListView( this );
  73. layout()->setMargin( 0 );
  74. layout()->addWidget( m_view );
  75. m_view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  76. m_view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  77. m_view->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Ignored );
  78. m_view->setFrameShape( QFrame::NoFrame );
  79. m_view->setAttribute( Qt::WA_MacShowFocusRect, 0 );
  80. m_view->setUniformItemSizes( false );
  81. new IndexStatusManager( this );
  82. new PipelineStatusManager( this );
  83. new ScannerStatusManager( this );
  84. new TransferStatusManager( this );
  85. new LatchedStatusManager( this );
  86. setMouseTracking( true );
  87. m_view->setMouseTracking( true );
  88. }
  89. void
  90. JobStatusView::setModel( JobStatusSortModel* m )
  91. {
  92. m_model = m;
  93. m_view->setModel( m );
  94. m_view->setItemDelegate( new JobStatusDelegate( m_view ) );
  95. connect( m_view->model(), SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( checkCount() ) );
  96. connect( m_view->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( checkCount() ) );
  97. connect( m_view->model(), SIGNAL( modelReset() ), this, SLOT( checkCount() ) );
  98. connect( m_view->model(), SIGNAL( customDelegateJobInserted( int, JobStatusItem* ) ), this, SLOT( customDelegateJobInserted( int, JobStatusItem* ) ) );
  99. connect( m_view->model(), SIGNAL( customDelegateJobRemoved( int ) ), this, SLOT( customDelegateJobRemoved( int ) ) );
  100. connect( m_view->model(), SIGNAL( refreshDelegates() ), this, SLOT( refreshDelegates() ) );
  101. connect( m_view, SIGNAL( activated( QModelIndex ) ), this, SLOT( onItemActivated( QModelIndex ) ) );
  102. foreach ( const QPointer<JobStatusItem> item, s_jobItems )
  103. {
  104. if ( !item.isNull() )
  105. {
  106. m_model->addJob( item.data() );
  107. }
  108. }
  109. s_jobItems.clear();
  110. }
  111. void
  112. JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item )
  113. {
  114. if ( !item )
  115. return;
  116. item->createDelegate( m_view );
  117. m_view->setItemDelegateForRow( row, item->customDelegate() );
  118. ACLJobDelegate* delegate = qobject_cast< ACLJobDelegate* >( item->customDelegate() );
  119. if ( delegate )
  120. {
  121. connect( delegate, SIGNAL( update( const QModelIndex& ) ), m_view, SLOT( update( const QModelIndex & ) ) );
  122. connect( delegate, SIGNAL( aclResult( Tomahawk::ACLStatus::Type ) ), item, SLOT( aclResult( Tomahawk::ACLStatus::Type ) ) );
  123. delegate->emitSizeHintChanged( m_model->index( row, 0 ) );
  124. }
  125. else
  126. tLog() << Q_FUNC_INFO << "delegate was not properly found!";
  127. checkCount();
  128. }
  129. void
  130. JobStatusView::customDelegateJobRemoved( int row )
  131. {
  132. Q_UNUSED( row );
  133. checkCount();
  134. }
  135. void
  136. JobStatusView::refreshDelegates()
  137. {
  138. int count = m_model->rowCount();
  139. for ( int i = 0; i < count; i++ )
  140. {
  141. QModelIndex index = m_model->index( i, 0 );
  142. QVariant itemVar = index.data( JobStatusModel::JobDataRole );
  143. if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
  144. {
  145. tLog() << Q_FUNC_INFO << "unable to fetch JobStatusItem* at row" << i;
  146. continue;
  147. }
  148. JobStatusItem* item = itemVar.value< JobStatusItem* >();
  149. if ( item->hasCustomDelegate() )
  150. m_view->setItemDelegateForRow( i, item->customDelegate() );
  151. else
  152. m_view->setItemDelegateForRow( i, m_view->itemDelegate() );
  153. }
  154. checkCount();
  155. }
  156. void
  157. JobStatusView::onItemActivated( const QModelIndex& index )
  158. {
  159. QVariant itemVar = index.data( JobStatusModel::JobDataRole );
  160. if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
  161. {
  162. tLog() << Q_FUNC_INFO << "unable to fetch JobStatusItem*";
  163. return;
  164. }
  165. JobStatusItem* item = itemVar.value< JobStatusItem* >();
  166. item->activated();
  167. }
  168. void
  169. JobStatusView::checkCount()
  170. {
  171. m_cachedHeight = -1;
  172. if ( m_view->model()->rowCount() == 0 && !isHidden() )
  173. emit hideWidget();
  174. else
  175. emit sizeHintChanged( sizeHint() );
  176. }
  177. QSize
  178. JobStatusView::sizeHint() const
  179. {
  180. if ( m_cachedHeight >= 0 )
  181. return QSize( 0, m_cachedHeight );
  182. unsigned int y = 0;
  183. y += m_view->contentsMargins().top() + m_view->contentsMargins().bottom();
  184. if ( m_view->model()->rowCount() )
  185. {
  186. for ( int i = 0; i < m_view->model()->rowCount(); i++ )
  187. {
  188. y += m_view->sizeHintForRow( i );
  189. }
  190. y += 2; // some padding
  191. }
  192. m_cachedHeight = y;
  193. return QSize( 0, y );
  194. }