/src/libtomahawk/playlist/treeitemdelegate.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 193 lines · 136 code · 37 blank · 20 comment · 30 complexity · 0a5f148ec9e00039a16bdfc1e763c481 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2012 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 "TreeItemDelegate.h"
  20. #include <QApplication>
  21. #include <QPainter>
  22. #include <QAbstractItemView>
  23. #include <QHeaderView>
  24. #include "Query.h"
  25. #include "Result.h"
  26. #include "utils/TomahawkUtilsGui.h"
  27. #include "utils/Logger.h"
  28. #include "utils/Closure.h"
  29. #include "utils/PixmapDelegateFader.h"
  30. #include "PlayableItem.h"
  31. #include "TreeProxyModel.h"
  32. #include "TreeView.h"
  33. TreeItemDelegate::TreeItemDelegate( TreeView* parent, TreeProxyModel* proxy )
  34. : QStyledItemDelegate( (QObject*)parent )
  35. , m_view( parent )
  36. , m_model( proxy )
  37. {
  38. }
  39. void
  40. TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
  41. {
  42. PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
  43. if ( !item )
  44. return;
  45. QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() );
  46. textOption.setWrapMode( QTextOption::NoWrap );
  47. QString text;
  48. if ( !item->artist().isNull() )
  49. {
  50. text = item->artist()->name();
  51. }
  52. else if ( !item->album().isNull() )
  53. {
  54. text = item->album()->name();
  55. }
  56. else if ( !item->result().isNull() || !item->query().isNull() )
  57. {
  58. float opacity = item->result().isNull() ? 0.0 : item->result()->score();
  59. opacity = qMax( (float)0.3, opacity );
  60. QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity );
  61. {
  62. QStyleOptionViewItemV4 o = option;
  63. initStyleOption( &o, QModelIndex() );
  64. painter->save();
  65. o.palette.setColor( QPalette::Text, textColor );
  66. if ( o.state & QStyle::State_Selected && o.state & QStyle::State_Active )
  67. {
  68. o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) );
  69. }
  70. if ( item->isPlaying() )
  71. {
  72. o.palette.setColor( QPalette::Highlight, o.palette.color( QPalette::Mid ) );
  73. if ( o.state & QStyle::State_Selected )
  74. o.palette.setColor( QPalette::Text, textColor );
  75. o.state |= QStyle::State_Selected;
  76. }
  77. int oldX = 0;
  78. if ( m_view->header()->visualIndex( index.column() ) == 0 )
  79. {
  80. oldX = o.rect.x();
  81. o.rect.setX( 0 );
  82. }
  83. qApp->style()->drawControl( QStyle::CE_ItemViewItem, &o, painter );
  84. if ( oldX > 0 )
  85. o.rect.setX( oldX );
  86. if ( m_view->hoveredIndex().row() == index.row() && m_view->hoveredIndex().column() == index.column() &&
  87. !index.data().toString().isEmpty() && index.column() == 0 )
  88. {
  89. o.rect.setWidth( o.rect.width() - 16 );
  90. QRect arrowRect( o.rect.x() + o.rect.width(), o.rect.y() + 1, o.rect.height() - 2, o.rect.height() - 2 );
  91. QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() );
  92. painter->drawPixmap( arrowRect, infoIcon );
  93. }
  94. {
  95. QRect r = o.rect.adjusted( 3, 0, 0, 0 );
  96. // Paint Now Playing Speaker Icon
  97. if ( item->isPlaying() && m_view->header()->visualIndex( index.column() ) == 0 )
  98. {
  99. r.adjust( 0, 0, 0, -3 );
  100. QRect npr = r.adjusted( 3, 1, 18 - r.width(), 1 );
  101. painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) );
  102. r.adjust( 25, 0, 0, 3 );
  103. }
  104. painter->setPen( o.palette.text().color() );
  105. QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, r.width() - 3 );
  106. painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, textOption );
  107. }
  108. painter->restore();
  109. }
  110. return;
  111. }
  112. else
  113. return;
  114. if ( text.trimmed().isEmpty() )
  115. text = tr( "Unknown" );
  116. QStyleOptionViewItemV4 opt = option;
  117. initStyleOption( &opt, QModelIndex() );
  118. qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
  119. if ( option.state & QStyle::State_Selected )
  120. {
  121. opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) );
  122. }
  123. if ( index.column() > 0 )
  124. return;
  125. painter->save();
  126. painter->setRenderHint( QPainter::Antialiasing );
  127. painter->setPen( opt.palette.color( QPalette::Text ) );
  128. QRect r = option.rect.adjusted( 4, 4, -option.rect.width() + option.rect.height() - 4, -4 );
  129. // painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) );
  130. if ( !m_pixmaps.contains( index ) )
  131. {
  132. if ( !item->album().isNull() )
  133. {
  134. m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->album(), r.size(), TomahawkUtils::ScaledCover, false ) ) );
  135. _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<TreeItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) );
  136. closure->setAutoDelete( false );
  137. }
  138. else if ( !item->artist().isNull() )
  139. {
  140. m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->artist(), r.size(), TomahawkUtils::ScaledCover, false ) ) );
  141. _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<TreeItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) );
  142. closure->setAutoDelete( false );
  143. }
  144. }
  145. const QPixmap cover = m_pixmaps[ index ]->currentPixmap();
  146. painter->drawPixmap( r, cover );
  147. r = option.rect.adjusted( option.rect.height(), 6, -4, -option.rect.height() + 22 );
  148. text = painter->fontMetrics().elidedText( text, Qt::ElideRight, r.width() );
  149. painter->drawText( r, text, textOption );
  150. painter->restore();
  151. }
  152. void
  153. TreeItemDelegate::doUpdateIndex( const QPersistentModelIndex& index )
  154. {
  155. emit updateIndex( index );
  156. }