PageRenderTime 123ms CodeModel.GetById 40ms app.highlight 33ms RepoModel.GetById 28ms app.codeStats 0ms

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