]> git.sur5r.net Git - minitube/blob - src/playlistview.cpp
52bdb63789aa99a433dc62fe3b08a9122174281b
[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     // qDebug() << "PlaylistView::mouseMoveEvent" << event->pos();
22
23     QListView::mouseMoveEvent(event);
24
25     if (isHoveringAuthor(event)) {
26
27         // check for special "message" item
28         ListModel *listModel = dynamic_cast<ListModel *>(model());
29         if (listModel && listModel->rowCount() == indexAt(event->pos()).row())
30             return;
31
32         QMetaObject::invokeMethod(model(), "enterAuthorHover");
33         setCursor(Qt::PointingHandCursor);
34     } else {
35         QMetaObject::invokeMethod(model(), "exitAuthorHover");
36         unsetCursor();
37     }
38
39 }
40
41 void PlaylistView::mousePressEvent(QMouseEvent *event) {
42     if (event->button() == Qt::LeftButton
43         && isHoveringAuthor(event)) {
44         QMetaObject::invokeMethod(model(), "enterAuthorPressed");
45         event->ignore();
46     } else {
47         QListView::mousePressEvent(event);
48     }
49 }
50
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()));
56     } else {
57         QListView::mousePressEvent(event);
58     }
59 }
60
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();
65
66     PrettyItemDelegate *delegate = dynamic_cast<PrettyItemDelegate *>(itemDelegate());
67     if (!delegate) return false;
68
69     QRect rect = delegate->authorRect(itemIndex);
70
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();
74
75     return ret;
76 }