/src/libtomahawk/utils/ProxyStyle.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 133 lines · 95 code · 18 blank · 20 comment · 24 complexity · c5742e3876a2601af871275c11c79d04 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, Teo Mrnjavac <teo@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 "ProxyStyle.h"
  20. #include <QPainter>
  21. #include <QSplitter>
  22. #include <QStyleOption>
  23. #include <QWidget>
  24. #include "utils/Logger.h"
  25. #include "utils/TomahawkStyle.h"
  26. #define ARROW_WIDTH 7
  27. #define ARROW_HEIGHT 7
  28. ProxyStyle::ProxyStyle( bool interceptPolish )
  29. : m_interceptPolish( interceptPolish )
  30. {
  31. }
  32. void
  33. ProxyStyle::polish( QPalette& pal )
  34. {
  35. if( !m_interceptPolish )
  36. QProxyStyle::polish( pal );
  37. }
  38. void
  39. ProxyStyle::drawPrimitive( PrimitiveElement pe, const QStyleOption* opt, QPainter* p, const QWidget* w ) const
  40. {
  41. if ( pe == PE_IndicatorBranch )
  42. {
  43. if ( opt->state & QStyle::State_Children && !w->property( "flattenBranches" ).toBool() )
  44. {
  45. int hd = ( opt->rect.height() - ARROW_HEIGHT ) / 2;
  46. int wd = ( opt->rect.width() - ARROW_WIDTH ) / 2;
  47. QRect r = opt->rect.adjusted( wd, hd, 0, 0 );
  48. QPointF pointsOpened[3] = { QPointF( r.x(), r.y() ), QPointF( r.x() + ARROW_WIDTH, r.y() ), QPointF( r.x() + ARROW_WIDTH / 2, r.y() + ARROW_HEIGHT ) };
  49. QPointF pointsClosed[3] = { QPointF( r.x(), r.y() ), QPointF( r.x() + ARROW_WIDTH, r.y() + ARROW_HEIGHT / 2 ), QPointF( r.x(), r.y() + ARROW_HEIGHT ) };
  50. p->save();
  51. p->setRenderHint( QPainter::Antialiasing );
  52. p->setPen( opt->palette.dark().color() );
  53. p->setBrush( opt->palette.dark().color() );
  54. if ( !( opt->state & QStyle::State_Open ) )
  55. {
  56. p->drawPolygon( pointsClosed, 3 );
  57. }
  58. else
  59. {
  60. p->drawPolygon( pointsOpened, 3 );
  61. }
  62. p->restore();
  63. }
  64. return;
  65. }
  66. if ( pe != PE_FrameStatusBar && pe != PE_FrameFocusRect )
  67. QProxyStyle::drawPrimitive( pe, opt, p, w );
  68. }
  69. void
  70. ProxyStyle::drawControl( ControlElement ce, const QStyleOption* opt, QPainter* p, const QWidget* w ) const
  71. {
  72. if ( ce == CE_Splitter )
  73. {
  74. const QSplitter* splitter = qobject_cast< const QSplitter* >( w );
  75. if ( !splitter->sizes().contains( 0 ) )
  76. {
  77. p->setPen( QColor( "#dddddd" ) );
  78. // We must special-case this because of the AnimatedSplitterHandle which has a
  79. // SizeHint of 0,0.
  80. if ( splitter->orientation() == Qt::Vertical )
  81. {
  82. p->drawLine( opt->rect.topLeft(), opt->rect.topRight() );
  83. }
  84. else
  85. {
  86. if ( splitter->handleWidth() == 1 )
  87. {
  88. p->drawLine( opt->rect.topLeft(), opt->rect.bottomLeft() );
  89. }
  90. else if ( splitter->handleWidth() == 3 )
  91. {
  92. p->drawLine( opt->rect.topLeft(), opt->rect.bottomLeft() );
  93. p->drawLine( opt->rect.topLeft() + QPoint( 1, 0 ), opt->rect.bottomLeft() + QPoint( 1, 0 ) );
  94. p->drawLine( opt->rect.topLeft() + QPoint( 2, 0 ), opt->rect.bottomLeft() + QPoint( 2, 0 ) );
  95. }
  96. }
  97. }
  98. }
  99. else
  100. QProxyStyle::drawControl( ce, opt, p, w );
  101. }
  102. QSize
  103. ProxyStyle::sizeFromContents( QStyle::ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget ) const
  104. {
  105. if ( type == CT_Splitter )
  106. {
  107. const QSplitter* splitter = qobject_cast< const QSplitter* >( widget );
  108. if ( splitter->orientation() == Qt::Horizontal )
  109. return QSize( 2, size.height() );
  110. else
  111. return QSize( size.width(), 2 );
  112. }
  113. else
  114. return QProxyStyle::sizeFromContents( type, option, size, widget );
  115. }