/src/libtomahawk/jobview/JobStatusDelegate.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 120 lines · 75 code · 25 blank · 20 comment · 7 complexity · 59e63524eb561cb3805ecb63ea96bbcc MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "JobStatusDelegate.h"
  19. #include "JobStatusModel.h"
  20. #include "utils/TomahawkUtilsGui.h"
  21. #include "utils/Logger.h"
  22. #include <QPainter>
  23. #include <QApplication>
  24. #include <QListView>
  25. #define ROW_HEIGHT ( TomahawkUtils::defaultFontHeight() + 6 )
  26. #define ICON_PADDING 2
  27. #define PADDING 2
  28. JobStatusDelegate::JobStatusDelegate( QObject* parent )
  29. : QStyledItemDelegate ( parent )
  30. , m_parentView( qobject_cast< QListView* >( parent ) )
  31. {
  32. Q_ASSERT( m_parentView );
  33. }
  34. JobStatusDelegate::~JobStatusDelegate()
  35. {
  36. }
  37. void
  38. JobStatusDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
  39. {
  40. QStyleOptionViewItem opt = option;
  41. initStyleOption( &opt, index );
  42. QFontMetrics fm( painter->font() );
  43. const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
  44. opt.state &= ~QStyle::State_MouseOver;
  45. QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget );
  46. // painter->drawLine( opt.rect.topLeft(), opt.rect.topRight() );
  47. painter->setRenderHint( QPainter::Antialiasing );
  48. QRect iconRect( ICON_PADDING, ICON_PADDING + opt.rect.y(), ROW_HEIGHT - 2 * ICON_PADDING, ROW_HEIGHT - 2 * ICON_PADDING );
  49. if ( allowMultiLine )
  50. iconRect.moveTop( opt.rect.top() + opt.rect.height() / 2 - iconRect.height() / 2);
  51. QPixmap p = index.data( Qt::DecorationRole ).value< QPixmap >();
  52. if ( !p.isNull() )
  53. {
  54. p = p.scaledToHeight( iconRect.height(), Qt::SmoothTransformation );
  55. painter->drawPixmap( iconRect, p );
  56. }
  57. // draw right column if there is one
  58. const QString rCol = index.data( JobStatusModel::RightColumnRole ).toString();
  59. int rightEdge = opt.rect.right();
  60. if ( !rCol.isEmpty() )
  61. {
  62. const int w = fm.width( rCol );
  63. const QRect rRect( opt.rect.right() - PADDING - w, PADDING + opt.rect.y(), w, opt.rect.height() - 2 * PADDING );
  64. painter->drawText( rRect, Qt::AlignCenter, rCol );
  65. rightEdge = rRect.left();
  66. }
  67. const int mainW = rightEdge - 6 * PADDING - iconRect.right();
  68. QString mainText = index.data( Qt::DisplayRole ).toString();
  69. QTextOption to( Qt::AlignLeft | Qt::AlignVCenter );
  70. if ( !allowMultiLine )
  71. {
  72. to.setWrapMode( QTextOption::NoWrap );
  73. mainText = fm.elidedText( mainText, Qt::ElideRight, mainW );
  74. }
  75. else
  76. to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
  77. painter->drawText( QRect( iconRect.right() + 4 * PADDING, PADDING + opt.rect.y(), mainW, opt.rect.height() - 2 * PADDING ), mainText, to );
  78. }
  79. QSize
  80. JobStatusDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
  81. {
  82. const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
  83. if ( !allowMultiLine )
  84. return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), ROW_HEIGHT );
  85. else if ( m_cachedMultiLineHeights.contains( index ) )
  86. return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), m_cachedMultiLineHeights[ index ] );
  87. // Don't elide, but stretch across as many rows as required
  88. QStyleOptionViewItem opt = option;
  89. initStyleOption( &opt, index );
  90. const QString text = index.data( Qt::DisplayRole ).toString();
  91. const int leftEdge = ICON_PADDING + ROW_HEIGHT + 2 * PADDING;
  92. const QRect rect = opt.fontMetrics.boundingRect( leftEdge, opt.rect.top(), m_parentView->width() - leftEdge, 200, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
  93. m_cachedMultiLineHeights.insert( index, rect.height() + 4 * PADDING );
  94. return QSize( QStyledItemDelegate::sizeHint ( option, index ).width(), rect.height() + 4 * PADDING );
  95. }