3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "playlistview.h"
22 #include "painterutils.h"
23 #include "playlistitemdelegate.h"
24 #include "playlistmodel.h"
26 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent), clickableAuthors(true) {
27 setItemDelegate(new PlaylistItemDelegate(this));
28 setSelectionMode(QAbstractItemView::ExtendedSelection);
30 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
35 setDropIndicatorShown(true);
36 setDragDropMode(QAbstractItemView::DragDrop);
39 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
40 setFrameShape(QFrame::NoFrame);
41 setAttribute(Qt::WA_MacShowFocusRect, false);
42 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 setUniformItemSizes(true);
45 connect(this, SIGNAL(entered(const QModelIndex &)), SLOT(itemEntered(const QModelIndex &)));
46 setMouseTracking(true);
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();
55 setMinimumHeight(PlaylistItemDelegate::thumbHeight * 4);
57 setMinimumWidth(PlaylistItemDelegate::thumbWidth);
59 setMinimumWidth(minimumWidth() + vScrollbar->width());
63 void PlaylistView::itemEntered(const QModelIndex &index) {
64 PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
65 if (listModel) listModel->setHoveredRow(index.row());
68 void PlaylistView::leaveEvent(QEvent *event) {
69 QListView::leaveEvent(event);
70 PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
71 if (listModel) listModel->clearHover();
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);
83 QMetaObject::invokeMethod(model(), "exitAuthorHover");
86 QListView::mouseMoveEvent(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);
99 QListView::mousePressEvent(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();
115 QListView::mouseReleaseEvent(event);
118 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
119 if (!clickableAuthors) return false;
121 const QModelIndex itemIndex = indexAt(event->pos());
122 const QRect itemRect = visualRect(itemIndex);
123 // qDebug() << " itemRect.x()" << itemRect.x();
125 PlaylistItemDelegate *delegate = qobject_cast<PlaylistItemDelegate *>(itemDelegate());
126 if (!delegate) return false;
128 QRect rect = delegate->authorRect(itemIndex);
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();
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();
147 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
148 return model()->rowCount() > 1 && model()->rowCount() == index.row() + 1;