]> git.sur5r.net Git - minitube/blob - src/playlistview.cpp
Imported Upstream version 1.9
[minitube] / src / playlistview.cpp
1 #include "playlistview.h"
2 #include "ListModel.h"
3 #include "playlist/PrettyItemDelegate.h"
4
5 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent) {
6     connect(this, SIGNAL(entered(const QModelIndex &)), SLOT(itemEntered(const QModelIndex &)));
7     setMouseTracking(true);
8 }
9
10 void PlaylistView::itemEntered(const QModelIndex &index) {
11     ListModel *listModel = dynamic_cast<ListModel *>(model());
12     if (listModel) listModel->setHoveredRow(index.row());
13 }
14
15 void PlaylistView::leaveEvent(QEvent * /* event */) {
16     ListModel *listModel = dynamic_cast<ListModel *>(model());
17     if (listModel) listModel->clearHover();
18 }
19
20 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
21     QListView::mouseMoveEvent(event);
22     QWidget::mouseMoveEvent(event);
23
24     if (isHoveringAuthor(event)) {
25
26         // check for special "message" item
27         ListModel *listModel = dynamic_cast<ListModel *>(model());
28         if (listModel && listModel->rowCount() == indexAt(event->pos()).row())
29             return;
30
31         QMetaObject::invokeMethod(model(), "enterAuthorHover");
32         setCursor(Qt::PointingHandCursor);
33     } else {
34         QMetaObject::invokeMethod(model(), "exitAuthorHover");
35         unsetCursor();
36     }
37
38 }
39
40 void PlaylistView::mousePressEvent(QMouseEvent *event) {
41     if (event->button() == Qt::LeftButton
42         && isHoveringAuthor(event)) {
43         QMetaObject::invokeMethod(model(), "enterAuthorPressed");
44         event->ignore();
45     } else {
46         QListView::mousePressEvent(event);
47     }
48 }
49
50 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
51     if (event->button() == Qt::LeftButton) {
52         QMetaObject::invokeMethod(model(), "exitAuthorPressed");
53         if (isHoveringAuthor(event))
54             emit authorPushed(indexAt(event->pos()));
55     } else {
56         QListView::mousePressEvent(event);
57     }
58 }
59
60 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
61     const QModelIndex itemIndex = indexAt(event->pos());
62     const QRect itemRect = visualRect(itemIndex);
63     // qDebug() << " itemRect.x()" <<  itemRect.x();
64
65     PrettyItemDelegate *delegate = dynamic_cast<PrettyItemDelegate *>(itemDelegate());
66     if (!delegate) return false;
67
68     QRect rect = delegate->authorRect(itemIndex);
69
70     const int x = event->x() - itemRect.x() - rect.x();
71     const int y = event->y() - itemRect.y() - rect.y();
72     bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
73
74     return ret;
75 }