1 #include "playlistview.h"
3 #include "playlist/PrettyItemDelegate.h"
5 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent) {
6 connect(this, SIGNAL(entered(const QModelIndex &)), SLOT(itemEntered(const QModelIndex &)));
7 setMouseTracking(true);
10 void PlaylistView::itemEntered(const QModelIndex &index) {
11 ListModel *listModel = dynamic_cast<ListModel *>(model());
12 if (listModel) listModel->setHoveredRow(index.row());
15 void PlaylistView::leaveEvent(QEvent * /* event */) {
16 ListModel *listModel = dynamic_cast<ListModel *>(model());
17 if (listModel) listModel->clearHover();
20 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
21 // qDebug() << "PlaylistView::mouseMoveEvent" << event->pos();
23 QListView::mouseMoveEvent(event);
25 if (isHoveringAuthor(event)) {
27 // check for special "message" item
28 ListModel *listModel = dynamic_cast<ListModel *>(model());
29 if (listModel && listModel->rowCount() == indexAt(event->pos()).row())
32 QMetaObject::invokeMethod(model(), "enterAuthorHover");
33 setCursor(Qt::PointingHandCursor);
35 QMetaObject::invokeMethod(model(), "exitAuthorHover");
41 void PlaylistView::mousePressEvent(QMouseEvent *event) {
42 if (event->button() == Qt::LeftButton
43 && isHoveringAuthor(event)) {
44 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
47 QListView::mousePressEvent(event);
51 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
52 if (event->button() == Qt::LeftButton) {
53 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
54 if (isHoveringAuthor(event))
55 emit authorPushed(indexAt(event->pos()));
57 QListView::mousePressEvent(event);
61 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
62 const QModelIndex itemIndex = indexAt(event->pos());
63 const QRect itemRect = visualRect(itemIndex);
64 // qDebug() << " itemRect.x()" << itemRect.x();
66 PrettyItemDelegate *delegate = dynamic_cast<PrettyItemDelegate *>(itemDelegate());
67 if (!delegate) return false;
69 QRect rect = delegate->authorRect(itemIndex);
71 const int x = event->x() - itemRect.x() - rect.x();
72 const int y = event->y() - itemRect.y() - rect.y();
73 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();