defuze.me  Client
playqueuedelegate.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 <QPainter>
00012 #include <QDebug>
00013 #include "playqueuedelegate.hpp"
00014 #include "queueable.hpp"
00015 #include "queuetrack.hpp"
00016 #include "queuebreak.hpp"
00017 #include "scheduler.hpp"
00018 #include "eventmodel.hpp"
00019 #include "playerlabel.hpp"
00020 
00021 using namespace Queue;
00022 
00023 PlayQueueDelegate::PlayQueueDelegate(PlayQueue *queue) :
00024     QStyledItemDelegate(),
00025     positionPolygon(4),
00026     timePolygon(4),
00027     queue(queue)
00028 {
00029     positionPolygon.setPoint(0, 0, 0);
00030     positionPolygon.setPoint(1, 30, 0);
00031     positionPolygon.setPoint(2, 20, 20);
00032     positionPolygon.setPoint(3, 0, 20);
00033     timePolygon.setPoint(0, 30, 0);
00034     timePolygon.setPoint(1, 100, 0);
00035     timePolygon.setPoint(2, 90, 20);
00036     timePolygon.setPoint(3, 20, 20);
00037 }
00038 
00039 void        PlayQueueDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00040 {
00041     painter->save();
00042 
00043     Queue::Queueable* queueable = (Queue::Queueable*) index.data().value<void*>();
00044 
00045     // Draw background when selected
00046     if (option.state & QStyle::State_Selected)
00047         painter->fillRect(option.rect, option.palette.highlight());
00048 
00049     if (queueable->getEvent() > 0)
00050     {
00051         QColor  eventColor(100, 100, 100);
00052         int id = queueable->getEvent();
00053         Scheduler::EventModel *event = queue->getSchedulerPlugin()->getEvent(id);
00054         if (event)
00055             eventColor = QColor("#" + event->getColor());
00056         QRect   block = QRect(option.rect.adjusted(option.rect.width() - 10, 0, 0, 0));
00057         if (index.row() % 3 == 0)
00058         {
00059             painter->save();
00060             QFont   font = option.font;
00061             font.setPointSize(8);
00062             painter->setPen(eventColor);
00063             painter->setFont(font);
00064             painter->translate(block.topLeft());
00065             painter->translate(1, 3);
00066             painter->rotate(90);
00067             painter->drawText(0, 0, event->getTitle());
00068             painter->restore();
00069         }
00070         eventColor.setAlpha(20);
00071         painter->fillRect(block, eventColor);
00072     }
00073 
00074     // Draw shadows
00075     painter->setPen(QColor(255, 255, 255, 20));
00076     painter->drawLine(option.rect.topLeft(), option.rect.topRight());
00077     painter->setPen(QColor(0, 0, 0, 60));
00078     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
00079 
00080     // Draw position background
00081     painter->setRenderHint(QPainter::Antialiasing);
00082     painter->setPen(Qt::NoPen);
00083     // Player color
00084     if (index.row() == 0)
00085         painter->setBrush(QBrush(Player::PlayerLabel::currentColor()));
00086     else if (index.row() == 1)
00087         painter->setBrush(QBrush(Player::PlayerLabel::nextColor()));
00088     else
00089         painter->setBrush(QBrush(QColor(255, 255, 255, 30)));
00090     QPolygon    positionZone = positionPolygon.translated(option.rect.topLeft());
00091     painter->drawConvexPolygon(positionZone);
00092 
00093     // Draw time background
00094     painter->setBrush(QBrush(QColor(0, 0, 0, 60)));
00095     QPolygon    timeZone = timePolygon.translated(option.rect.topLeft());
00096     painter->drawConvexPolygon(timeZone);
00097 
00098     // Set text mode
00099     if (option.state & QStyle::State_Selected)
00100         painter->setPen(option.palette.highlightedText().color());
00101     else
00102         painter->setPen(option.palette.foreground().color());
00103 
00104     // Draw position id
00105     painter->drawText(positionZone.boundingRect().adjusted(0, 0, -10, 0), Qt::AlignCenter, QString::number(index.row() + 1));
00106 
00107     // Draw play time
00108     //  QRect   timeRect = QRect(0, positionZone.boundingRect().bottom(), 40, option.rect.height() - positionZone.boundingRect().height());
00109     QRect   timeRect = timeZone.boundingRect();
00110     QString time = tr("--:--");
00111     if (queueable->getPlayTime().isValid())
00112         time = queueable->getPlayTime().toString(tr("h:mm ap"));
00113     painter->drawText(timeRect, Qt::AlignCenter, time);
00114 
00115     // Draw details
00116     int margin = 2; // top/bottom pix margin
00117     if (queueable->isTrack())
00118     {
00119         // Get the queuetrack element
00120         Queue::QueueTrack   *qtrack = queueable->toQueueTrack();
00121 
00122         // Set the title font
00123         QFont   font = option.font;
00124         font.setPointSize(13);
00125         painter->setFont(font);
00126 
00127         // Calculate position
00128         QRect   titleRect = QRect(110, option.rect.top() + margin, option.rect.width() - 215, painter->fontMetrics().height());
00129         int width = painter->fontMetrics().boundingRect(qtrack->getTrack()->getTitle()).width();
00130         if (width > titleRect.width())
00131             width = titleRect.width();
00132         // Draw title
00133         painter->drawText(titleRect, Qt::AlignLeft | Qt::AlignVCenter, qtrack->getTrack()->getTitle());
00134 
00135         // Set default font
00136         painter->setFont(option.font);
00137         painter->setOpacity(0.6);
00138         // Calculate duration position
00139         QRect   lengthRect = titleRect.adjusted(width + 5, 0, 60, 0);
00140         // Draw duration
00141         painter->drawText(lengthRect, Qt::AlignLeft | Qt::AlignVCenter, QString("(%1)").arg(qtrack->textDuration()));
00142         painter->setOpacity(1);
00143 
00144         // Calculate position
00145         QRect   artistRect = QRect(10, titleRect.bottom(), option.rect.width() - 60, 20);
00146         // Draw artist
00147         painter->drawText(artistRect, Qt::AlignLeft | Qt::AlignVCenter, qtrack->getTrack()->getArtist() + " - " + qtrack->getTrack()->getAlbum());
00148 
00149         // Draw album image
00150         QPixmap albumArt = qtrack->getTrack()->getAlbumArtAtSize(32);
00151         QRect   pixmapRect = QRect(option.rect.width() - (albumArt.width() + 17),
00152                                    option.rect.top() + (option.rect.height() - albumArt.height())/2,
00153                                    albumArt.width(),
00154                                    albumArt.height());
00155         painter->drawPixmap(pixmapRect, albumArt);
00156         // Draw jewelcase
00157         //QRect jewelcaseRect = pixmapRect.adjusted(-3, -2, 2, 2);
00158         //painter->drawPixmap(jewelcaseRect, QPixmap(":/jewelcase/32"));
00159     }
00160     else if (queueable->isBreak())
00161     {
00162         // Get the queuebreak element
00163         Queue::QueueBreak   *qbreak = queueable->toQueueBreak();
00164 
00165         // Calculate position
00166         QRect   textRect = QRect(10, option.rect.top() + 24, option.rect.width() - 70, 20);
00167         // Draw message
00168         painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, qbreak->message());
00169 
00170 
00171         // Draw break image
00172         QPixmap image(":/icons/clock");
00173         QRect   pixmapRect = QRect(option.rect.width() - (image.width() + 18),
00174                                    option.rect.top() + (option.rect.height() - image.height())/2,
00175                                    image.width(),
00176                                    image.height());
00177         painter->drawPixmap(pixmapRect, image);
00178     }
00179 
00180 
00181     painter->restore();
00182 }
00183 
00184 QSize       PlayQueueDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex&) const
00185 {
00186     return QSize(200, QFontMetrics(option.font).height()*3);
00187 }