PageRenderTime 224ms CodeModel.GetById 120ms app.highlight 16ms RepoModel.GetById 85ms app.codeStats 0ms

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