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 QListView::mouseMoveEvent(event);
65 if (isHoveringThumbnail(event)) {
66 setCursor(Qt::PointingHandCursor);
67 } else if (isShowMoreItem(indexAt(event->pos()))) {
68 setCursor(Qt::PointingHandCursor);
69 } else if (isHoveringAuthor(event)) {
70 QMetaObject::invokeMethod(model(), "enterAuthorHover");
71 setCursor(Qt::PointingHandCursor);
73 QMetaObject::invokeMethod(model(), "exitAuthorHover");
78 void PlaylistView::mousePressEvent(QMouseEvent *event) {
79 if (event->button() == Qt::LeftButton) {
80 if (isHoveringThumbnail(event)) {
82 } else if (isHoveringAuthor(event)) {
83 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
85 } else QListView::mousePressEvent(event);
86 } else QListView::mousePressEvent(event);
89 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
90 if (event->button() == Qt::LeftButton) {
91 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
92 const QModelIndex index = indexAt(event->pos());
93 if (isHoveringThumbnail(event)) {
94 emit activated(index);
96 } else if (isHoveringAuthor(event)) {
97 emit authorPushed(index);
98 } else if (isShowMoreItem(index)) {
99 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
100 listModel->searchMore();
105 QListView::mousePressEvent(event);
109 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
110 if (!clickableAuthors) return false;
112 const QModelIndex itemIndex = indexAt(event->pos());
113 const QRect itemRect = visualRect(itemIndex);
114 // qDebug() << " itemRect.x()" << itemRect.x();
116 PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
117 if (!delegate) return false;
119 QRect rect = delegate->authorRect(itemIndex);
121 const int x = event->x() - itemRect.x() - rect.x();
122 const int y = event->y() - itemRect.y() - rect.y();
123 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
128 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
129 const QModelIndex index = indexAt(event->pos());
130 const QRect itemRect = visualRect(index);
131 static const QRect thumbRect(0, 0, 160, 90);
132 const int x = event->x() - itemRect.x() - thumbRect.x();
133 const int y = event->y() - itemRect.y() - thumbRect.y();
134 return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
137 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
138 return model()->rowCount() > 1 &&
139 model()->rowCount() == index.row() + 1;
142 void PlaylistView::paintEvent(QPaintEvent *event) {
143 QListView::paintEvent(event);
144 PainterUtils::topShadow(viewport());