1 /* $BEGIN_LICENSE
2 
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5 
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18 
19 $END_LICENSE */
20 
21 #include "playlistview.h"
22 #include "painterutils.h"
23 #include "playlistitemdelegate.h"
24 #include "playlistmodel.h"
25 
PlaylistView(QWidget * parent)26 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent), clickableAuthors(true) {
27     setItemDelegate(new PlaylistItemDelegate(this));
28     setSelectionMode(QAbstractItemView::ExtendedSelection);
29 
30     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
31 
32     // dragndrop
33     setDragEnabled(true);
34     setAcceptDrops(true);
35     setDropIndicatorShown(true);
36     setDragDropMode(QAbstractItemView::DragDrop);
37 
38     // cosmetics
39     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
40     setFrameShape(QFrame::NoFrame);
41     setAttribute(Qt::WA_MacShowFocusRect, false);
42     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43     setUniformItemSizes(true);
44 
45     connect(this, SIGNAL(entered(const QModelIndex &)), SLOT(itemEntered(const QModelIndex &)));
46     setMouseTracking(true);
47 
48     QScrollBar *vScrollbar = verticalScrollBar();
49     connect(vScrollbar, &QAbstractSlider::valueChanged, this, [this, vScrollbar](int value) {
50         if (isVisible() && value == vScrollbar->maximum()) {
51             PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
52             listModel->searchMore();
53         }
54     });
55     setMinimumHeight(PlaylistItemDelegate::thumbHeight * 4);
56 
57     setMinimumWidth(PlaylistItemDelegate::thumbWidth);
58 #ifndef APP_MAC
59     setMinimumWidth(minimumWidth() + vScrollbar->width());
60 #endif
61 }
62 
itemEntered(const QModelIndex & index)63 void PlaylistView::itemEntered(const QModelIndex &index) {
64     PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
65     if (listModel) listModel->setHoveredRow(index.row());
66 }
67 
leaveEvent(QEvent * event)68 void PlaylistView::leaveEvent(QEvent *event) {
69     QListView::leaveEvent(event);
70     PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
71     if (listModel) listModel->clearHover();
72 }
73 
mouseMoveEvent(QMouseEvent * event)74 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
75     if (isHoveringThumbnail(event)) {
76         setCursor(Qt::PointingHandCursor);
77     } else if (isShowMoreItem(indexAt(event->pos()))) {
78         setCursor(Qt::PointingHandCursor);
79     } else if (isHoveringAuthor(event)) {
80         QMetaObject::invokeMethod(model(), "enterAuthorHover");
81         setCursor(Qt::PointingHandCursor);
82     } else {
83         QMetaObject::invokeMethod(model(), "exitAuthorHover");
84         unsetCursor();
85     }
86     QListView::mouseMoveEvent(event);
87 }
88 
mousePressEvent(QMouseEvent * event)89 void PlaylistView::mousePressEvent(QMouseEvent *event) {
90     if (event->button() == Qt::LeftButton) {
91         if (isHoveringAuthor(event)) {
92             QMetaObject::invokeMethod(model(), "enterAuthorPressed");
93         } else if (isHoveringThumbnail(event)) {
94             const QModelIndex index = indexAt(event->pos());
95             emit activated(index);
96             unsetCursor();
97             return;
98         }
99         QListView::mousePressEvent(event);
100     }
101 }
102 
mouseReleaseEvent(QMouseEvent * event)103 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
104     if (event->button() == Qt::LeftButton) {
105         QMetaObject::invokeMethod(model(), "exitAuthorPressed");
106         const QModelIndex index = indexAt(event->pos());
107         if (isHoveringAuthor(event)) {
108             emit authorPushed(index);
109         } else if (isShowMoreItem(index)) {
110             PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
111             listModel->searchMore();
112             unsetCursor();
113         }
114     }
115     QListView::mouseReleaseEvent(event);
116 }
117 
isHoveringAuthor(QMouseEvent * event)118 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
119     if (!clickableAuthors) return false;
120 
121     const QModelIndex itemIndex = indexAt(event->pos());
122     const QRect itemRect = visualRect(itemIndex);
123     // qDebug() << " itemRect.x()" <<  itemRect.x();
124 
125     PlaylistItemDelegate *delegate = qobject_cast<PlaylistItemDelegate *>(itemDelegate());
126     if (!delegate) return false;
127 
128     QRect rect = delegate->authorRect(itemIndex);
129 
130     const int x = event->x() - itemRect.x() - rect.x();
131     const int y = event->y() - itemRect.y() - rect.y();
132     bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
133 
134     return ret;
135 }
136 
isHoveringThumbnail(QMouseEvent * event)137 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
138     const QModelIndex index = indexAt(event->pos());
139     const QRect itemRect = visualRect(index);
140     static const QRect thumbRect(0, 0, PlaylistItemDelegate::thumbWidth,
141                                  PlaylistItemDelegate::thumbHeight);
142     const int x = event->x() - itemRect.x() - thumbRect.x();
143     const int y = event->y() - itemRect.y() - thumbRect.y();
144     return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
145 }
146 
isShowMoreItem(const QModelIndex & index)147 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
148     return model()->rowCount() > 1 && model()->rowCount() == index.row() + 1;
149 }
150