1 #include "playlistview.h"
2 #include "playlistmodel.h"
3 #include "playlistitemdelegate.h"
5 PlaylistView::PlaylistView(QWidget *parent) : QListView(parent) {
6 clickableAuthors = true;
8 setItemDelegate(new PlaylistItemDelegate(this));
9 setSelectionMode(QAbstractItemView::ExtendedSelection);
14 setDropIndicatorShown(true);
15 setDragDropMode(QAbstractItemView::DragDrop);
18 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
19 setFrameShape(QFrame::NoFrame);
20 setAttribute(Qt::WA_MacShowFocusRect, false);
21 // setMinimumSize(120, 240);
22 setUniformItemSizes(true);
24 connect(this, SIGNAL(entered(const QModelIndex &)),
25 SLOT(itemEntered(const QModelIndex &)));
26 setMouseTracking(true);
29 void PlaylistView::itemEntered(const QModelIndex &index) {
30 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
31 if (listModel) listModel->setHoveredRow(index.row());
34 void PlaylistView::leaveEvent(QEvent * /* event */) {
35 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
36 if (listModel) listModel->clearHover();
39 void PlaylistView::mouseMoveEvent(QMouseEvent *event) {
40 QListView::mouseMoveEvent(event);
41 // QWidget::mouseMoveEvent(event);
43 if (isHoveringThumbnail(event)) {
44 setCursor(Qt::PointingHandCursor);
45 } else if (isShowMoreItem(indexAt(event->pos()))) {
46 setCursor(Qt::PointingHandCursor);
47 } else if (isHoveringAuthor(event)) {
48 QMetaObject::invokeMethod(model(), "enterAuthorHover");
49 setCursor(Qt::PointingHandCursor);
51 QMetaObject::invokeMethod(model(), "exitAuthorHover");
56 void PlaylistView::mousePressEvent(QMouseEvent *event) {
57 if (event->button() == Qt::LeftButton) {
58 if (isHoveringThumbnail(event)) {
60 } else if (isHoveringAuthor(event)) {
61 QMetaObject::invokeMethod(model(), "enterAuthorPressed");
63 } else QListView::mousePressEvent(event);
64 } else QListView::mousePressEvent(event);
67 void PlaylistView::mouseReleaseEvent(QMouseEvent *event) {
68 if (event->button() == Qt::LeftButton) {
69 QMetaObject::invokeMethod(model(), "exitAuthorPressed");
70 const QModelIndex index = indexAt(event->pos());
71 if (isHoveringThumbnail(event)) {
72 emit activated(index);
74 } else if (isHoveringAuthor(event)) {
75 emit authorPushed(index);
76 } else if (isShowMoreItem(index)) {
77 PlaylistModel *listModel = dynamic_cast<PlaylistModel *>(model());
78 listModel->searchMore();
83 QListView::mousePressEvent(event);
87 bool PlaylistView::isHoveringAuthor(QMouseEvent *event) {
88 if (!clickableAuthors) return false;
90 const QModelIndex itemIndex = indexAt(event->pos());
91 const QRect itemRect = visualRect(itemIndex);
92 // qDebug() << " itemRect.x()" << itemRect.x();
94 PlaylistItemDelegate *delegate = dynamic_cast<PlaylistItemDelegate *>(itemDelegate());
95 if (!delegate) return false;
97 QRect rect = delegate->authorRect(itemIndex);
99 const int x = event->x() - itemRect.x() - rect.x();
100 const int y = event->y() - itemRect.y() - rect.y();
101 bool ret = x > 0 && x < rect.width() && y > 0 && y < rect.height();
106 bool PlaylistView::isHoveringThumbnail(QMouseEvent *event) {
107 const QModelIndex index = indexAt(event->pos());
108 const QRect itemRect = visualRect(index);
109 QRect thumbRect(0, 0, 120, 90);
110 const int x = event->x() - itemRect.x() - thumbRect.x();
111 const int y = event->y() - itemRect.y() - thumbRect.y();
112 return x > 0 && x < thumbRect.width() && y > 0 && y < thumbRect.height();
115 bool PlaylistView::isShowMoreItem(const QModelIndex &index) {
116 return model()->rowCount() > 1 &&
117 model()->rowCount() == index.row() + 1;