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 "playlistmodel.h"
23 #include "playlistitemdelegate.h"
24 #include "painterutils.h"
26 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent),
27 clickableAuthors(true) {
28 setItemDelegate(new PlaylistItemDelegate(this));
29 setSelectionMode(QAbstractItemView::ExtendedSelection);
31 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
33 #if QT_VERSION >= 0x050000 && defined(APP_MAC)
42 setDropIndicatorShown(true);
43 setDragDropMode(QAbstractItemView::DragDrop);
46 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
47 setFrameShape(QFrame::NoFrame);
48 setAttribute(Qt::WA_MacShowFocusRect, false);
49 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 setUniformItemSizes(true);
52 connect(this, SIGNAL(entered(const QModelIndex &)),
53 SLOT(itemEntered(const QModelIndex &)));
54 setMouseTracking(true);
57 void PlaylistView::itemEntered(const QModelIndex &index) {
58 PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
59 if (listModel) listModel->setHoveredRow(index.row());
62 void PlaylistView::leaveEvent(QEvent *event) {
63 QListView::leaveEvent(event);
64 PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
65 if (listModel) listModel->clearHover();
68 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
69 if (isHoveringThumbnail(event)) {
70 setCursor(Qt::PointingHandCursor);
71 } else if (isShowMoreItem(indexAt(event->pos()))) {
72 setCursor(Qt::PointingHandCursor);
73 } else if (isHoveringAuthor(event)) {
74 QMetaObject::invokeMethod(model(), "enterAuthorHover");
75 setCursor(Qt::PointingHandCursor);
77 QMetaObject::invokeMethod(model(), "exitAuthorHover");
80 QListView::mouseMoveEvent(event);
83 void PlaylistView::mousePressEvent(QMouseEvent *event) {
84 if (event->button() == Qt::LeftButton) {
85 if (isHoveringAuthor(event)) {
86 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
89 QListView::mousePressEvent(event);
92 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
93 if (event->button() == Qt::LeftButton) {
94 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
95 const QModelIndex index = indexAt(event->pos());
96 if (isHoveringThumbnail(event)) {
97 emit activated(index);
99 } else if (isHoveringAuthor(event)) {
100 emit authorPushed(index);
101 } else if (isShowMoreItem(index)) {
102 PlaylistModel *listModel = qobject_cast<PlaylistModel *>(model());
103 listModel->searchMore();
107 QListView::mouseReleaseEvent(event);
110 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
111 if (!clickableAuthors) return false;
113 const QModelIndex itemIndex = indexAt(event->pos());
114 const QRect itemRect = visualRect(itemIndex);
115 // qDebug() << " itemRect.x()" << itemRect.x();
117 PlaylistItemDelegate *delegate = qobject_cast<PlaylistItemDelegate *>(itemDelegate());
118 if (!delegate) return false;
120 QRect rect = delegate->authorRect(itemIndex);
122 const int x = event->x() - itemRect.x() - rect.x();
123 const int y = event->y() - itemRect.y() - rect.y();
124 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
129 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
130 const QModelIndex index = indexAt(event->pos());
131 const QRect itemRect = visualRect(index);
132 static const QRect thumbRect(0, 0, 160, 90);
133 const int x = event->x() - itemRect.x() - thumbRect.x();
134 const int y = event->y() - itemRect.y() - thumbRect.y();
135 return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
138 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
139 return model()->rowCount() > 1 &&
140 model()->rowCount() == index.row() + 1;