/src/libtomahawk/utils/stylehelper.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 177 lines · 121 code · 32 blank · 24 comment · 6 complexity · d80f8842202fa94fa2f6560df0cad085 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2011, Casey Link <unnamedrambler@gmail.com>
  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 "StyleHelper.h"
  19. #include <QPainter>
  20. #include <QPixmapCache>
  21. #include <QApplication>
  22. QColor
  23. StyleHelper::headerUpperColor()
  24. {
  25. return QColor( "#464e57" );
  26. }
  27. QColor
  28. StyleHelper::headerLowerColor()
  29. {
  30. return QColor( "#3f4650" );
  31. }
  32. QColor
  33. StyleHelper::headerHighlightColor()
  34. {
  35. return QColor( "#333" );
  36. }
  37. void
  38. StyleHelper::horizontalHeader( QPainter* painter, const QRect& r )
  39. {
  40. painter->save();
  41. QRect upperHalf( 0, 0, r.width(), r.height() / 2 );
  42. QRect lowerHalf( 0, upperHalf.height(), r.width(), r.height() );
  43. painter->fillRect( upperHalf, StyleHelper::headerUpperColor() );
  44. painter->fillRect( lowerHalf, StyleHelper::headerLowerColor() );
  45. {
  46. QColor lineColor( 100, 100, 100 );
  47. QLine line( 0, 0, r.width(), 0 );
  48. painter->setPen( lineColor );
  49. painter->drawLine( line );
  50. }
  51. {
  52. QColor lineColor( 30, 30, 30 );
  53. QLine line( 0, r.height() - 1, r.width(), r.height() - 1 );
  54. painter->setPen( lineColor );
  55. painter->drawLine( line );
  56. }
  57. painter->restore();
  58. }
  59. QColor
  60. StyleHelper::headerTextColor()
  61. {
  62. return Qt::white;
  63. }
  64. /*
  65. * This implementation is from QWindowsStyle (Qt 7.2)
  66. *
  67. * It is licensed under the GPL 3:
  68. * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  69. * Contact: Nokia Corporation (qt-info@nokia.com)
  70. */
  71. void StyleHelper::drawArrow( QStyle::PrimitiveElement element, QPainter* p, const QStyleOption* opt )
  72. {
  73. if ( opt->rect.width() <= 1 || opt->rect.height() <= 1 )
  74. return;
  75. QRect r = opt->rect;
  76. int size = qMin( r.height(), r.width() );
  77. QPixmap pixmap;
  78. QString pixmapName;
  79. pixmapName.sprintf( "arrow-%s-%d-%d-%d-%lld", "$qt_ia", uint(opt->state), element, size, opt->palette.cacheKey() );
  80. if ( !QPixmapCache::find( pixmapName, pixmap) )
  81. {
  82. int border = size / 5;
  83. int sqsize = 2 * ( size / 2 );
  84. QImage image( sqsize, sqsize, QImage::Format_ARGB32 );
  85. image.fill( 0 );
  86. QPainter imagePainter( &image );
  87. imagePainter.setRenderHint( QPainter::Antialiasing, true );
  88. QPolygon a;
  89. switch ( element )
  90. {
  91. case QStyle::PE_IndicatorArrowUp:
  92. a.setPoints( 3, border, sqsize / 2, sqsize / 2, border, sqsize - border, sqsize / 2 );
  93. break;
  94. case QStyle::PE_IndicatorArrowDown:
  95. a.setPoints( 3, border, sqsize / 2, sqsize / 2, sqsize - border, sqsize - border, sqsize / 2 );
  96. break;
  97. case QStyle::PE_IndicatorArrowRight:
  98. a.setPoints( 3, sqsize - border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border );
  99. break;
  100. case QStyle::PE_IndicatorArrowLeft:
  101. a.setPoints( 3, border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border );
  102. break;
  103. default:
  104. break;
  105. }
  106. int bsx = 0;
  107. int bsy = 0;
  108. if ( opt->state & QStyle::State_Sunken )
  109. {
  110. bsx = qApp->style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal );
  111. bsy = qApp->style()->pixelMetric( QStyle::PM_ButtonShiftVertical );
  112. }
  113. QRect bounds = a.boundingRect();
  114. int sx = sqsize / 2 - bounds.center().x() - 1;
  115. int sy = sqsize / 2 - bounds.center().y() - 1;
  116. imagePainter.translate( sx + bsx, sy + bsy );
  117. imagePainter.setPen( opt->palette.buttonText().color() );
  118. imagePainter.setBrush( opt->palette.buttonText() );
  119. if ( !( opt->state & QStyle::State_Enabled ) )
  120. {
  121. QColor foreGround( 150, 150, 150, 150 );
  122. imagePainter.setBrush( opt->palette.mid().color() );
  123. imagePainter.setPen( opt->palette.mid().color() );
  124. }
  125. else
  126. {
  127. QColor shadow( 0, 0, 0, 100 );
  128. imagePainter.translate( 0, 1 );
  129. imagePainter.setPen( shadow );
  130. imagePainter.setBrush( shadow );
  131. QColor foreGround( 255, 255, 255, 210 );
  132. imagePainter.drawPolygon( a );
  133. imagePainter.translate( 0, -1 );
  134. imagePainter.setPen( foreGround );
  135. imagePainter.setBrush( foreGround );
  136. }
  137. imagePainter.drawPolygon( a );
  138. imagePainter.end();
  139. pixmap = QPixmap::fromImage( image );
  140. QPixmapCache::insert( pixmapName, pixmap );
  141. }
  142. int xOffset = r.x() + ( r.width() - size ) / 2;
  143. int yOffset = r.y() + ( r.height() - size ) / 2;
  144. p->drawPixmap( xOffset, yOffset, pixmap );
  145. }