/src/resolverconfigdelegate.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 152 lines · 102 code · 30 blank · 20 comment · 10 complexity · 6fa911a8b988d8973244413f83922a18 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 "ResolverConfigDelegate.h"
  19. #include "ExternalResolverGui.h"
  20. #include "utils/Logger.h"
  21. #include <QApplication>
  22. #include <QPainter>
  23. #include <QMouseEvent>
  24. #define PADDING 4
  25. #define ICONSIZE 24
  26. ResolverConfigDelegate::ResolverConfigDelegate( QObject* parent )
  27. : ConfigDelegateBase( parent )
  28. {
  29. connect( this, SIGNAL( configPressed( QModelIndex ) ), this, SLOT( onConfigPressed( QModelIndex ) ) );
  30. }
  31. void
  32. ResolverConfigDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
  33. {
  34. QStyleOptionViewItemV4 opt = option;
  35. initStyleOption( &opt, index );
  36. QRect itemRect = opt.rect;
  37. int top = itemRect.top();
  38. QFont name = opt.font;
  39. name.setPointSize( name.pointSize() + 2 );
  40. name.setBold( true );
  41. QFont path = opt.font;
  42. path.setItalic( true );
  43. path.setPointSize( path.pointSize() - 1 );
  44. const bool error = (Tomahawk::ExternalResolver::ErrorState)index.data( ResolversModel::ErrorState ).toInt() != Tomahawk::ExternalResolver::NoError;
  45. const bool fileNotFound = (Tomahawk::ExternalResolver::ErrorState)index.data( ResolversModel::ErrorState ).toInt() == Tomahawk::ExternalResolver::FileNotFound;
  46. const bool failedToLoad = (Tomahawk::ExternalResolver::ErrorState)index.data( ResolversModel::ErrorState ).toInt() == Tomahawk::ExternalResolver::FailedToLoad;
  47. QFontMetrics bfm( name );
  48. QFontMetrics sfm( path );
  49. // draw the background
  50. const QWidget* w = opt.widget;
  51. QStyle* style = w ? w->style() : QApplication::style();
  52. style->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, w );
  53. int rightSplit = itemRect.width();
  54. QRect confRect = QRect( rightSplit - ICONSIZE - 2 * PADDING, 2 * PADDING + top, ICONSIZE, ICONSIZE );
  55. // if the Resolver.has a config widget, paint it first (right-aligned)
  56. if( index.data( ResolversModel::HasConfig ).toBool() ) {
  57. QStyleOptionToolButton topt;
  58. topt.rect = confRect;
  59. topt.pos = confRect.topLeft();
  60. drawConfigWrench( painter, opt, topt );
  61. }
  62. // draw check
  63. QRect checkRect = confRect;
  64. checkRect.moveTo( 2 * PADDING, 2 * PADDING + top );
  65. opt.rect = checkRect;
  66. drawCheckBox( opt, painter, w );
  67. itemRect.setX( opt.rect.topRight().x() + PADDING );
  68. painter->save();
  69. painter->setFont( name );
  70. QRect textRect = itemRect.adjusted( PADDING, PADDING, -PADDING, -PADDING );
  71. if( index.data( ResolversModel::HasConfig ).toBool() )
  72. textRect.setRight( confRect.topLeft().x() - PADDING );
  73. textRect.setBottom( itemRect.height() / 2 + top );
  74. QString nameStr = bfm.elidedText( index.data( ResolversModel::ResolverName ).toString(),Qt::ElideRight, textRect.width() );
  75. painter->drawText( textRect, nameStr );
  76. painter->restore();
  77. painter->save();
  78. painter->setFont( path );
  79. QString pathStr = index.data( ResolversModel::ResolverPath ).toString();
  80. if( error )
  81. {
  82. painter->setPen( QColor( Qt::red ).lighter( 150 ) );
  83. } else {
  84. painter->setPen( Qt::gray );
  85. }
  86. if( fileNotFound ) {
  87. pathStr = tr( "Not found: %1" ).arg( pathStr );
  88. } else if ( failedToLoad )
  89. {
  90. pathStr = tr( "Failed to load: %1" ).arg( pathStr );
  91. }
  92. textRect.moveTop( itemRect.height() / 2 + top );
  93. pathStr = sfm.elidedText( pathStr, Qt::ElideMiddle, textRect.width() );
  94. painter->drawText( textRect, pathStr );
  95. painter->restore();
  96. }
  97. QRect
  98. ResolverConfigDelegate::configRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const
  99. {
  100. QStyleOptionViewItemV4 opt = option;
  101. initStyleOption( &opt, idx );
  102. QRect itemRect = opt.rect;
  103. int top = itemRect.top();
  104. QRect confRect = QRect( itemRect.width() - ICONSIZE - 2 * PADDING, 2 * PADDING + top, ICONSIZE, ICONSIZE );
  105. return confRect;
  106. }
  107. QRect
  108. ResolverConfigDelegate::checkRectForIndex( const QStyleOptionViewItem &option, const QModelIndex &idx, int role ) const
  109. {
  110. Q_UNUSED( role );
  111. QStyleOptionViewItemV4 opt = option;
  112. initStyleOption( &opt, idx );
  113. QRect itemRect = opt.rect;
  114. int top = itemRect.top();
  115. QRect checkRect = QRect(2 * PADDING, 2 * PADDING + top, ICONSIZE, ICONSIZE );
  116. return checkRect;
  117. }
  118. void
  119. ResolverConfigDelegate::onConfigPressed( const QModelIndex& idx )
  120. {
  121. emit openConfig( idx.data( ResolversModel::ResolverPath ).toString() );
  122. }