PageRenderTime 133ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 72ms app.codeStats 0ms

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