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);
37 setDropIndicatorShown(true);
38 setDragDropMode(QAbstractItemView::DragDrop);
41 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
42 setFrameShape(QFrame::NoFrame);
43 setAttribute(Qt::WA_MacShowFocusRect, false);
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setUniformItemSizes(true);
47 connect(this, SIGNAL(entered(const QModelIndex &)),
48 SLOT(itemEntered(const QModelIndex &)));
49 setMouseTracking(true);
52 void PlaylistView::itemEntered(const QModelIndex &index) {
53 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
54 if (listModel) listModel->setHoveredRow(index.row());
57 void PlaylistView::leaveEvent(QEvent *event) {
58 QListView::leaveEvent(event);
59 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
60 if (listModel) listModel->clearHover();
63 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
64 if (isHoveringThumbnail(event)) {
65 setCursor(Qt::PointingHandCursor);
66 } else if (isShowMoreItem(indexAt(event->pos()))) {
67 setCursor(Qt::PointingHandCursor);
68 } else if (isHoveringAuthor(event)) {
69 QMetaObject::invokeMethod(model(), "enterAuthorHover");
70 setCursor(Qt::PointingHandCursor);
72 QMetaObject::invokeMethod(model(), "exitAuthorHover");
75 QListView::mouseMoveEvent(event);
78 void PlaylistView::mousePressEvent(QMouseEvent *event) {
79 if (event->button() == Qt::LeftButton) {
80 if (isHoveringAuthor(event)) {
81 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
84 QListView::mousePressEvent(event);
87 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
88 if (event->button() == Qt::LeftButton) {
89 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
90 const QModelIndex index = indexAt(event->pos());
91 if (isHoveringThumbnail(event)) {
92 emit activated(index);
94 } else if (isHoveringAuthor(event)) {
95 emit authorPushed(index);
96 } else if (isShowMoreItem(index)) {
97 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
98 listModel->searchMore();
102 QListView::mouseReleaseEvent(event);
105 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
106 if (!clickableAuthors) return false;
108 const QModelIndex itemIndex = indexAt(event->pos());
109 const QRect itemRect = visualRect(itemIndex);
110 // qDebug() << " itemRect.x()" << itemRect.x();
112 PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
113 if (!delegate) return false;
115 QRect rect = delegate->authorRect(itemIndex);
117 const int x = event->x() - itemRect.x() - rect.x();
118 const int y = event->y() - itemRect.y() - rect.y();
119 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
124 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
125 const QModelIndex index = indexAt(event->pos());
126 const QRect itemRect = visualRect(index);
127 static const QRect thumbRect(0, 0, 160, 90);
128 const int x = event->x() - itemRect.x() - thumbRect.x();
129 const int y = event->y() - itemRect.y() - thumbRect.y();
130 return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
133 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
134 return model()->rowCount() > 1 &&
135 model()->rowCount() == index.row() + 1;
138 void PlaylistView::paintEvent(QPaintEvent *event) {
139 QListView::paintEvent(event);
141 PainterUtils::topShadow(viewport());