File indexing completed on 2024-05-19 04:50:30

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TrackDelegate.h"
0018 
0019 #include "MetaValues.h"
0020 #include "core/support/Debug.h"
0021 #include "statsyncing/models/CommonModel.h"
0022 
0023 
0024 #include <QIcon>
0025 #include <KLocalizedString>
0026 #include <kratingpainter.h>
0027 
0028 #include <QApplication>
0029 #include <QPainter>
0030 #include <QDateTime>
0031 
0032 using namespace StatSyncing;
0033 
0034 TrackDelegate::TrackDelegate( QObject *parent )
0035     : QStyledItemDelegate( parent )
0036 {
0037 }
0038 
0039 void
0040 TrackDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option,
0041                       const QModelIndex &index ) const
0042 {
0043     qint64 field = index.data( CommonModel::FieldRole ).value<qint64>();
0044     QVariant data = index.data();
0045     // display the icon even for label conflicts:
0046     if( ( field == Meta::valRating || field == Meta::valLabel ) &&
0047         data.type() == QVariant::Int )
0048     {
0049         // following is largely inspired by QStyledItemDelegate::paint()
0050         QStyleOptionViewItem opt = option;
0051         initStyleOption( &opt, index );
0052 
0053         QPixmap starsPixmap( CommonModel::s_ratingSize );
0054         starsPixmap.fill( Qt::transparent );
0055         {
0056             KRatingPainter ratingPainter;
0057             int rating = data.toInt();
0058             int hoverRating = -1;
0059             if( rating < 0 ) // unresolved conflict
0060             {
0061                 rating = 0;
0062                 ratingPainter.setIcon( QIcon::fromTheme( QStringLiteral("status_unknown") ) );
0063                 ratingPainter.setEnabled( false );
0064                 ratingPainter.setMaxRating( 2 );
0065             }
0066             QPainter starsPainter( &starsPixmap );
0067             ratingPainter.paint( &starsPainter, QRect( QPoint( 0, 0 ),
0068                     CommonModel::s_ratingSize ), rating, hoverRating );
0069         }
0070 
0071         opt.text.clear();
0072         opt.features |= QStyleOptionViewItem::HasDecoration;
0073         opt.decorationSize = CommonModel::s_ratingSize;
0074         opt.decorationAlignment = Qt::AlignRight | Qt::AlignVCenter;
0075         opt.decorationPosition = QStyleOptionViewItem::Right;
0076         opt.icon = QIcon( starsPixmap );
0077 
0078         const QWidget *widget = opt.widget;
0079         QStyle *style = widget ? widget->style() : QApplication::style();
0080         style->drawControl( QStyle::CE_ItemViewItem, &opt, painter, widget );
0081     }
0082     else
0083         QStyledItemDelegate::paint( painter, option, index );
0084 }
0085 
0086 QString
0087 TrackDelegate::displayText( const QVariant &value, const QLocale &locale ) const
0088 {
0089     if( value.type() == QVariant::DateTime )
0090     {
0091         QDateTime date = value.toDateTime();
0092         return date.isValid() ? QLocale().toString( date, QLocale::ShortFormat ) : QString();
0093     }
0094     return QStyledItemDelegate::displayText( value, locale );
0095 }