defuze.me  Client
librarydelegate.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "librarydelegate.hpp"
00012 #include <QPainter>
00013 #include <QDebug>
00014 
00015 using namespace Library;
00016 
00017 LibraryDelegate::LibraryDelegate(QObject *parent) : QStyledItemDelegate(parent)
00018 {
00019     note.load(":icons/note");
00020 }
00021 
00022 QSize LibraryDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
00023 {
00024     Q_UNUSED(index);
00025     //LibraryItem *item = (LibraryItem*)index.data().value<void*>();
00026     int height = 2;
00027 
00028     return QSize(200, QFontMetrics(option.font).height() * height);
00029 }
00030 
00031 void LibraryDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00032 {
00033     painter->save();
00034 
00035     LibraryItem *item = static_cast<LibraryItem*>(index.internalPointer());
00036 
00037     // Draw background when selected
00038     if (option.state & QStyle::State_Selected)
00039         painter->fillRect(option.rect, option.palette.highlight());
00040 
00041     // Draw shadows
00042     painter->setPen(QColor(255, 255, 255, 20));
00043     painter->drawLine(QPoint(0, option.rect.top()), option.rect.topRight());
00044     painter->setPen(QColor(0, 0, 0, 60));
00045     painter->drawLine(QPoint(0, option.rect.bottom()), option.rect.bottomRight());
00046     // Set text mode
00047     if (option.state & QStyle::State_Selected)
00048         painter->setPen(option.palette.highlightedText().color());
00049     else
00050         painter->setPen(option.palette.foreground().color());
00051 
00052 
00053     // Set the title font
00054     QFont   font(option.font.family());
00055 
00056     QRect   titleRect = option.rect.adjusted(31, 0, -31, 0);
00057     QRect   pixmapRect = QRect(option.rect.left(), option.rect.top() + (option.rect.height() - 22) / 2, 22, 22);
00058 
00059     font.setPointSize(11);
00060     painter->setFont(font);
00061     if (item->getKind() == LibraryItem::ALBUM)
00062     {
00063         // Draw album image
00064         if (AudioTrack::isLoaded(item->getId()))
00065             painter->drawPixmap(pixmapRect.topLeft(), item->getTrack()->getAlbumArtAtSize(pixmapRect.height()));
00066         else
00067             AudioTrack::loadAndCall(item->getId(), parent(), SLOT(update()));
00068     }
00069     else if (item->getKind() == LibraryItem::TRACK)
00070     {
00071         if (item->getIsInAlbum())
00072         {
00073             // Draw track number
00074             if (AudioTrack::isLoaded(item->getId()))
00075             {
00076                 if (item->getTrack())
00077                 {
00078                     if (item->getTrack()->getTrack() > 0)
00079                     {
00080                         painter->setOpacity(0.6);
00081                         painter->drawText(pixmapRect, Qt::AlignVCenter | Qt::AlignRight, QString("%1").arg(item->getTrack()->getTrack()));
00082                         painter->setOpacity(1);
00083                     }
00084                     else
00085                     {
00086                         // Draw generic track icon
00087                         painter->drawPixmap(pixmapRect.adjusted(4, 2, 0, 0).topLeft(), note);
00088                     }
00089                 }
00090                 else
00091                 {
00092                     qDebug() << "invalid track" << item->getId();
00093                     painter->drawText(pixmapRect, Qt::AlignVCenter | Qt::AlignRight, "/!\\");
00094                 }
00095             }
00096             else
00097                 AudioTrack::loadAndCall(item->getId(), parent(), SLOT(update()));
00098             // else ask preload
00099         }
00100         else
00101         {
00102             // Draw track icon
00103             painter->drawPixmap(pixmapRect.adjusted(4, 2, 0, 0).topLeft(), note);
00104         }
00105     }
00106     else if (item->getKind() == LibraryItem::ARTIST)
00107     {
00108         painter->drawPixmap(pixmapRect.topLeft(), item->getTrack()->getArtistArtAtSize(pixmapRect.height()));
00109     }
00110     else
00111         painter->fillRect(pixmapRect, QColor(255,255,255,51));
00112 
00113     font.setPointSize(13);
00114     painter->setFont(font);
00115     // Draw Title
00116     painter->drawText(titleRect, Qt::AlignLeft | Qt::AlignVCenter, item->data(0).toString());
00117 
00118     painter->restore();
00119 }
00120