/src/settingslistdelegate.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 74 lines · 43 code · 12 blank · 19 comment · 4 complexity · 916a4511369205b6f0f91367f46c3225 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2011, Michael Zanetti <mzanetti@kde.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 "settingslistdelegate.h"
  20. #include "utils/logger.h"
  21. #include <QPainter>
  22. #include <QIcon>
  23. #include <QApplication>
  24. SettingsListDelegate::SettingsListDelegate(QObject *parent) :
  25. QStyledItemDelegate(parent)
  26. {
  27. }
  28. void SettingsListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  29. {
  30. painter->save();
  31. QStyleOptionViewItemV4 opt = option;
  32. initStyleOption( &opt, QModelIndex() );
  33. qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
  34. #if defined(Q_WS_MAC) || defined(Q_WS_WIN)
  35. // On mac draw our own selection rect as we don't get one from osx (around the whole icon or around just text)
  36. if ( opt.state & QStyle::State_Selected )
  37. {
  38. painter->setRenderHint( QPainter::Antialiasing );
  39. QPainterPath p;
  40. p.addRoundedRect( opt.rect.adjusted( 2, 1, -1, -1 ), 5, 5 );
  41. QColor fill( 214, 214, 214 );
  42. QColor border( 107, 107, 107 );
  43. painter->setPen( border );
  44. painter->drawPath( p );
  45. painter->fillPath( p, fill );
  46. }
  47. #else
  48. if ( ( option.state & QStyle::State_Selected ) == QStyle::State_Selected )
  49. {
  50. painter->setPen( option.palette.color( QPalette::HighlightedText ) );
  51. }
  52. #endif
  53. int horizontalOffset = ( option.rect.width() - option.decorationSize.width() ) /2;
  54. QRect iconRect = option.rect.adjusted( horizontalOffset, 6, -horizontalOffset, -option.rect.height() + 6 + option.decorationSize.height() );
  55. QPixmap avatar = index.data( Qt::DecorationRole ).value<QIcon>().pixmap( iconRect.size() );
  56. painter->drawPixmap( iconRect, avatar );
  57. QRect textRect = option.rect.adjusted( 6, iconRect.height() + 8, -6, 0 );
  58. QString text = painter->fontMetrics().elidedText( index.data( Qt::DisplayRole ).toString(), Qt::ElideRight, textRect.width() );
  59. QTextOption to( Qt::AlignHCenter );
  60. painter->drawText( textRect, text, to );
  61. painter->restore();
  62. }