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 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
59 if (listModel) listModel->clearHover();
62 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
63 if (isHoveringThumbnail(event)) {
64 setCursor(Qt::PointingHandCursor);
65 } else if (isShowMoreItem(indexAt(event->pos()))) {
66 setCursor(Qt::PointingHandCursor);
67 } else if (isHoveringAuthor(event)) {
68 QMetaObject::invokeMethod(model(), "enterAuthorHover");
69 setCursor(Qt::PointingHandCursor);
71 QMetaObject::invokeMethod(model(), "exitAuthorHover");
74 QListView::mouseMoveEvent(event);
77 void PlaylistView::mousePressEvent(QMouseEvent *event) {
78 if (event->button() == Qt::LeftButton) {
79 if (isHoveringAuthor(event)) {
80 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
83 QListView::mousePressEvent(event);
86 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
87 if (event->button() == Qt::LeftButton) {
88 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
89 const QModelIndex index = indexAt(event->pos());
90 if (isHoveringThumbnail(event)) {
91 emit activated(index);
93 } else if (isHoveringAuthor(event)) {
94 emit authorPushed(index);
95 } else if (isShowMoreItem(index)) {
96 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
97 listModel->searchMore();
101 QListView::mouseReleaseEvent(event);
104 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
105 if (!clickableAuthors) return false;
107 const QModelIndex itemIndex = indexAt(event->pos());
108 const QRect itemRect = visualRect(itemIndex);
109 // qDebug() << " itemRect.x()" << itemRect.x();
111 PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
112 if (!delegate) return false;
114 QRect rect = delegate->authorRect(itemIndex);
116 const int x = event->x() - itemRect.x() - rect.x();
117 const int y = event->y() - itemRect.y() - rect.y();
118 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
123 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
124 const QModelIndex index = indexAt(event->pos());
125 const QRect itemRect = visualRect(index);
126 static const QRect thumbRect(0, 0, 160, 90);
127 const int x = event->x() - itemRect.x() - thumbRect.x();
128 const int y = event->y() - itemRect.y() - thumbRect.y();
129 return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
132 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
133 return model()->rowCount() > 1 &&
134 model()->rowCount() == index.row() + 1;
137 void PlaylistView::paintEvent(QPaintEvent *event) {
138 QListView::paintEvent(event);
140 PainterUtils::topShadow(viewport());