File indexing completed on 2024-05-19 04:48:39

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com>                       *
0003  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0008  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0009  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0010  * version 3 of the license.                                                            *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ****************************************************************************************/
0019 
0020 #include "DynamicBiasDelegate.h"
0021 #include "dynamic/DynamicModel.h"
0022 
0023 #include "App.h"
0024 // #include "Bias.h"
0025 // #include "core/support/Debug.h"
0026 
0027 #include <QApplication>
0028 #include <QPainter>
0029 
0030 PlaylistBrowserNS::DynamicBiasDelegate::DynamicBiasDelegate( QWidget* parent )
0031     : QStyledItemDelegate( parent )
0032 {
0033     m_smallFont.setPointSize( m_smallFont.pointSize() - 1 );
0034 
0035     m_normalFm = new QFontMetrics( m_normalFont );
0036     m_smallFm = new QFontMetrics( m_smallFont );
0037 }
0038 
0039 PlaylistBrowserNS::DynamicBiasDelegate::~DynamicBiasDelegate()
0040 {
0041     delete m_normalFm;
0042     delete m_smallFm;
0043 }
0044 
0045 void
0046 PlaylistBrowserNS::DynamicBiasDelegate::paint( QPainter* painter,
0047                                                const QStyleOptionViewItem& option,
0048                                                const QModelIndex& index ) const
0049 {
0050     Dynamic::AbstractBias* bias = nullptr;
0051     QVariant v;
0052     if( index.isValid() ) {
0053         v = index.model()->data( index, Dynamic::DynamicModel::BiasRole );
0054         if( v.isValid() )
0055             bias = qobject_cast<Dynamic::AbstractBias*>(v.value<QObject*>() );
0056     }
0057 
0058     // for a bias paint the operator (e.g. a small progress bar for the part bias) in front of it
0059     if( bias )
0060     {
0061         QModelIndex parentIndex = index.parent();
0062         Dynamic::AbstractBias* parentBias = nullptr;
0063 
0064         //const bool isRTL = QApplication::isRightToLeft();
0065         //Q_UNUSED( isRTL );
0066 
0067         v = parentIndex.model()->data( parentIndex, Dynamic::DynamicModel::BiasRole );
0068         if( v.isValid() )
0069             parentBias = qobject_cast<Dynamic::AbstractBias*>(v.value<QObject*>() );
0070 
0071         if( parentBias )
0072         {
0073             // sub-biases have a operator drawn in front of them.
0074             const int operatorWidth = m_smallFm->boundingRect( QStringLiteral("mmmm") ).width();
0075 
0076             // draw the selection
0077             QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
0078 
0079             // TODO: isRTL
0080 
0081             // -- paint the operator
0082             QRect operatorRect( option.rect.x(), option.rect.y(),
0083                                 operatorWidth, option.rect.height() );
0084             painter->setFont( m_smallFont );
0085             parentBias->paintOperator( painter, operatorRect, bias );
0086 
0087             // -- paint the normal text
0088             QRect textRect( option.rect.x() + operatorWidth, option.rect.y(),
0089                             option.rect.width() - operatorWidth, option.rect.height() );
0090             painter->setFont( m_normalFont );
0091             const QString text = index.data( Qt::DisplayRole ).toString();
0092             painter->drawText( textRect, Qt::TextWordWrap, text );
0093         }
0094         else
0095         {
0096             QStyledItemDelegate::paint( painter, option, index );
0097         }
0098 
0099     }
0100     else
0101     {
0102         QStyledItemDelegate::paint( painter, option, index );
0103     }
0104 
0105 }
0106 
0107 
0108