]> git.sur5r.net Git - minitube/blob - src/playlistview.cpp
Imported Upstream version 2.0
[minitube] / src / playlistview.cpp
1 #include "playlistview.h"
2 #include "playlistmodel.h"
3 #include "playlistitemdelegate.h"
4
5 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent) {
6     clickableAuthors = true;
7
8     setItemDelegate(new PlaylistItemDelegate(this));
9     setSelectionMode(QAbstractItemView::ExtendedSelection);
10
11     // dragndrop
12     setDragEnabled(true);
13     setAcceptDrops(true);
14     setDropIndicatorShown(true);
15     setDragDropMode(QAbstractItemView::DragDrop);
16
17     // cosmetics
18     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
19     setFrameShape(QFrame::NoFrame);
20     setAttribute(Qt::WA_MacShowFocusRect, false);
21     // setMinimumSize(120, 240);
22     setUniformItemSizes(true);
23
24     connect(this, SIGNAL(entered(const QModelIndex &)),
25             SLOT(itemEntered(const QModelIndex &)));
26     setMouseTracking(true);
27 }
28
29 void PlaylistView::itemEntered(const QModelIndex &index) {
30     PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
31     if (listModel) listModel->setHoveredRow(index.row());
32 }
33
34 void PlaylistView::leaveEvent(QEvent * /* event */) {
35     PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
36     if (listModel) listModel->clearHover();
37 }
38
39 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
40     QListView::mouseMoveEvent(event);
41     // QWidget::mouseMoveEvent(event);
42
43     if (isHoveringThumbnail(event)) {
44         setCursor(Qt::PointingHandCursor);
45     } else if (isShowMoreItem(indexAt(event->pos()))) {
46         setCursor(Qt::PointingHandCursor);
47     } else if (isHoveringAuthor(event)) {
48         QMetaObject::invokeMethod(model(), "enterAuthorHover");
49         setCursor(Qt::PointingHandCursor);
50     } else {
51         QMetaObject::invokeMethod(model(), "exitAuthorHover");
52         unsetCursor();
53     }
54 }
55
56 void PlaylistView::mousePressEvent(QMouseEvent *event) {
57     if (event->button() == Qt::LeftButton) {
58         if (isHoveringThumbnail(event)) {
59             event->accept();
60         } else if (isHoveringAuthor(event)) {
61             QMetaObject::invokeMethod(model(), "enterAuthorPressed");
62             event->ignore();
63         } else QListView::mousePressEvent(event);
64     } else QListView::mousePressEvent(event);
65 }
66
67 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
68     if (event->button() == Qt::LeftButton) {
69         QMetaObject::invokeMethod(model(), "exitAuthorPressed");
70         const QModelIndex index =  indexAt(event->pos());
71         if (isHoveringThumbnail(event)) {
72             emit activated(index);
73             unsetCursor();
74         } else if (isHoveringAuthor(event)) {
75             emit authorPushed(index);
76         } else if (isShowMoreItem(index)) {
77             PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
78             listModel->searchMore();
79             unsetCursor();
80         }
81
82     } else {
83         QListView::mousePressEvent(event);
84     }
85 }
86
87 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
88     if (!clickableAuthors) return false;
89
90     const QModelIndex itemIndex = indexAt(event->pos());
91     const QRect itemRect = visualRect(itemIndex);
92     // qDebug() << " itemRect.x()" <<  itemRect.x();
93
94     PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
95     if (!delegate) return false;
96
97     QRect rect = delegate->authorRect(itemIndex);
98
99     const int x = event->x() - itemRect.x() - rect.x();
100     const int y = event->y() - itemRect.y() - rect.y();
101     bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
102
103     return ret;
104 }
105
106 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
107     const QModelIndex index = indexAt(event->pos());
108     const QRect itemRect = visualRect(index);
109     static const QRect thumbRect(0, 0, 160, 90);
110     const int x = event->x() - itemRect.x() - thumbRect.x();
111     const int y = event->y() - itemRect.y() - thumbRect.y();
112     return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
113 }
114
115 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
116     return model()->rowCount() > 1 &&
117             model()->rowCount() == index.row() + 1;
118 }