1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "gui/covers.h"
25 #include "groupedview.h"
26 #include "mpd-interface/mpdstatus.h"
27 #include "mpd-interface/song.h"
28 #include "basicitemdelegate.h"
29 #include "itemview.h"
30 #include "config.h"
31 #include "widgets/icons.h"
32 #include "widgets/ratingwidget.h"
33 #include "support/gtkstyle.h"
34 #ifdef Q_OS_MAC
35 #include "support/osxstyle.h"
36 #endif
37 #include "support/utils.h"
38 #include "models/roles.h"
39 #include <QStyledItemDelegate>
40 #include <QApplication>
41 #include <QFontMetrics>
42 #include <QPainter>
43 #include <QLinearGradient>
44 #include <QAction>
45 #include <QDropEvent>
46 #include <QPixmap>
47
48 static int constCoverSize=32;
49 static int constIconSize=16;
50 static int constBorder=1;
51 static double sizeAdjust=1.25;
52
setup()53 void GroupedView::setup()
54 {
55 int height=QApplication::fontMetrics().height();
56 sizeAdjust=1.25;
57
58 if (height>17) {
59 constCoverSize=(((int)((height*2)/4))*4);
60 constIconSize=Icon::stdSize(((int)(height/4))*4);
61 constBorder=constCoverSize>48 ? 2 : 1;
62 } else {
63 constCoverSize=32;
64 constIconSize=16;
65 constBorder=1;
66 }
67 constCoverSize*=sizeAdjust;
68 }
69
coverSize()70 int GroupedView::coverSize()
71 {
72 return constCoverSize;
73 }
74
borderSize()75 int GroupedView::borderSize()
76 {
77 return constBorder;
78 }
79
iconSize()80 int GroupedView::iconSize()
81 {
82 return constIconSize;
83 }
84
drawPlayState(QPainter * painter,const QStyleOptionViewItem & option,const QRect & r,int state)85 void GroupedView::drawPlayState(QPainter *painter, const QStyleOptionViewItem &option, const QRect &r, int state)
86 {
87 if (state) {
88 int size=constIconSize-7;
89 int hSize=size/2;
90 QRect ir(r.x()-(size+6), r.y()+(((r.height()-size)/2.0)+0.5), size, size);
91 QColor inside(option.palette.color(QPalette::Text));
92 QColor border=inside.red()>100 && inside.blue()>100 && inside.green()>100 ? Qt::black : Qt::white;
93 if (QApplication::isRightToLeft()) {
94 ir.adjust(r.width()-size, 0, r.width()-size, 0);
95 }
96 switch (state) {
97 case GroupedView::State_Stopped:
98 painter->fillRect(ir, border);
99 painter->fillRect(ir.adjusted(1, 1, -1, -1), inside);
100 break;
101 case GroupedView::State_StopAfter: {
102 ir.adjust(2, 0, -2, 0);
103 QPoint p1[5]={ QPoint(ir.x()-2, ir.y()-1), QPoint(ir.x(), ir.y()-1), QPoint(ir.x()+(size-hSize), ir.y()+hSize), QPoint(ir.x(), ir.y()+(ir.height())), QPoint(ir.x()-2, ir.y()+(ir.height())) };
104 QPoint p2[5]={ QPoint(ir.x()-1, ir.y()), QPoint(ir.x(), ir.y()), QPoint(ir.x()+(size-hSize)-1, ir.y()+hSize), QPoint(ir.x(), ir.y()+ir.height()-1), QPoint(ir.x()-1, ir.y()+ir.height()-1) };
105 QPoint p3[3]={ QPoint(ir.x(), ir.y()+1), QPoint(ir.x()+(size-hSize)-2, ir.y()+hSize), QPoint(ir.x(), ir.y()+ir.height()-2) };
106 painter->save();
107 painter->setPen(border);
108 painter->drawPolygon(p1, 5);
109 painter->drawPolygon(p3, 3);
110 painter->setPen(inside);
111 painter->drawPolygon(p2, 5);
112 painter->restore();
113 break;
114 }
115 case GroupedView::State_StopAfterTrack:
116 painter->setPen(border);
117 painter->drawRect(ir.adjusted(0, 0, -1, -1));
118 painter->drawRect(ir.adjusted(2, 2, -3, -3));
119 painter->setPen(inside);
120 painter->drawRect(ir.adjusted(1, 1, -2, -2));
121 break;
122 case GroupedView::State_Paused: {
123 int blockSize=hSize-1;
124 painter->fillRect(ir, border);
125 painter->fillRect(ir.x()+1, ir.y()+1, blockSize, size-2, inside);
126 painter->fillRect(ir.x()+size-blockSize-1, ir.y()+1, blockSize, size-2, inside);
127 break;
128 }
129 case GroupedView::State_Playing: {
130 ir.adjust(2, 0, -2, 0);
131 QPoint p1[5]={ QPoint(ir.x()-2, ir.y()-1), QPoint(ir.x(), ir.y()-1), QPoint(ir.x()+(size-hSize), ir.y()+hSize), QPoint(ir.x(), ir.y()+(ir.height()-1)), QPoint(ir.x()-2, ir.y()+(ir.height()-1)) };
132 QPoint p2[5]={ QPoint(ir.x()-2, ir.y()-1), QPoint(ir.x(), ir.y()-1), QPoint(ir.x()+(size-hSize), ir.y()+hSize), QPoint(ir.x(), ir.y()+ir.height()), QPoint(ir.x()-2, ir.y()+ir.height()) };
133 painter->save();
134 painter->setBrush(border);
135 painter->setPen(border);
136 painter->drawPolygon(p1, 5);
137 painter->setBrush(inside);
138 painter->drawPolygon(p2, 5);
139 painter->restore();
140 break;
141 }
142 }
143 }
144 }
145
146 enum Type {
147 AlbumHeader,
148 AlbumTrack
149 };
150
getType(const QModelIndex & index)151 static Type getType(const QModelIndex &index)
152 {
153 QModelIndex prev=index.row()>0 ? index.sibling(index.row()-1, 0) : QModelIndex();
154 quint16 thisKey=index.data(Cantata::Role_Key).toUInt();
155 quint16 prevKey=prev.isValid() ? prev.data(Cantata::Role_Key).toUInt() : (quint16)Song::Null_Key;
156
157 return thisKey==prevKey ? AlbumTrack : AlbumHeader;
158 }
159
isAlbumHeader(const QModelIndex & index)160 static bool isAlbumHeader(const QModelIndex &index)
161 {
162 return !index.data(Cantata::Role_IsCollection).toBool() && AlbumHeader==getType(index);
163 }
164
streamText(const Song & song,const QString & trackTitle,bool useName=true)165 static QString streamText(const Song &song, const QString &trackTitle, bool useName=true)
166 {
167 if (song.album.isEmpty() && song.albumArtist().isEmpty()) {
168 QString songName=song.name();
169 return song.title.isEmpty() && songName.isEmpty()
170 ? song.file
171 : !useName || songName.isEmpty()
172 ? song.title
173 : song.title.isEmpty()
174 ? songName
175 : (song.title + Song::constSep + songName);
176 } else if (!song.title.isEmpty() && !song.artist.isEmpty()) {
177 QString name=song.name();
178 return song.artist + Song::constSep + (!useName || name.isEmpty()
179 ? song.title
180 : song.title.isEmpty()
181 ? name
182 : (song.title + Song::constSep + name));
183 } else {
184 return trackTitle;
185 }
186 }
187
GroupedViewDelegate(GroupedView * p)188 GroupedViewDelegate::GroupedViewDelegate(GroupedView *p)
189 : ActionItemDelegate(p)
190 , view(p)
191 , ratingPainter(nullptr)
192 {
193 }
194
~GroupedViewDelegate()195 GroupedViewDelegate::~GroupedViewDelegate()
196 {
197 delete ratingPainter;
198 }
199
sizeHint(int type,bool isCollection) const200 QSize GroupedViewDelegate::sizeHint(int type, bool isCollection) const
201 {
202 int textHeight = QApplication::fontMetrics().height()*sizeAdjust;
203
204 if (isCollection || AlbumHeader==type) {
205 return QSize(64, qMax(constCoverSize, (qMax(constIconSize, textHeight)*2)+constBorder)+(2*constBorder));
206 }
207 return QSize(64, qMax(constIconSize, textHeight)+(2*constBorder));
208 }
209
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const210 QSize GroupedViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
211 {
212 if (0==index.column()) {
213 return sizeHint(getType(index), index.data(Cantata::Role_IsCollection).toBool());
214 }
215 return QStyledItemDelegate::sizeHint(option, index);
216 }
217
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const218 void GroupedViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
219 {
220 if (!index.isValid()) {
221 return;
222 }
223
224 Type type=getType(index);
225 bool isCollection=index.data(Cantata::Role_IsCollection).toBool();
226 Song song=index.data(Cantata::Role_SongWithRating).value<Song>();
227 int state=index.data(Cantata::Role_Status).toInt();
228 quint32 collection=index.data(Cantata::Role_CollectionId).toUInt();
229 bool selected=option.state&QStyle::State_Selected;
230 bool mouseOver=underMouse && option.state&QStyle::State_MouseOver;
231 bool gtk=mouseOver && GtkStyle::isActive();
232 bool rtl=QApplication::isRightToLeft();
233
234 if (!isCollection && AlbumHeader==type) {
235 if (mouseOver && gtk) {
236 GtkStyle::drawSelection(option, painter, (selected ? 0.75 : 0.25)*0.75);
237 } else {
238 painter->save();
239 painter->setOpacity(0.75);
240 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, view);
241 painter->restore();
242 }
243 painter->save();
244 painter->setClipRect(option.rect.adjusted(0, option.rect.height()/2, 0, 0), Qt::IntersectClip);
245 if (mouseOver && gtk) {
246 GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25);
247 } else {
248 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, view);
249 }
250 painter->restore();
251 if (!state && !view->isExpanded(song.key, collection) && view->isCurrentAlbum(song.key)) {
252 QVariant cs=index.data(Cantata::Role_CurrentStatus);
253 if (cs.isValid()) {
254 state=cs.toInt();
255 }
256 }
257 } else {
258 if (mouseOver && gtk) {
259 GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25);
260 } else {
261 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, view);
262 }
263 }
264 QString title;
265 QString track;
266 QString duration=song.time>0 ? Utils::formatTime(song.time) : QString();
267 bool stream=!isCollection && song.isStandardStream();
268 bool isEmpty=song.title.isEmpty() && song.artist.isEmpty() && !song.file.isEmpty();
269 QString trackTitle=isEmpty
270 ? song.file
271 : song.diffArtist()
272 ? song.artistSong()
273 : song.title;
274 QFont f(QApplication::font());
275 if (stream) {
276 f.setItalic(true);
277 }
278 QFontMetrics fm(f);
279 int textHeight=fm.height()*sizeAdjust;
280
281
282 if (isCollection) {
283 title=index.data(Qt::DisplayRole).toString();
284 } else if (AlbumHeader==type) {
285 if (stream) {
286 QModelIndex next=index.sibling(index.row()+1, 0);
287 quint16 nextKey=next.isValid() ? next.data(Cantata::Role_Key).toUInt() : (quint16)Song::Null_Key;
288 if (nextKey!=song.key && !song.name().isEmpty()) {
289 title=song.name();
290 track=streamText(song, trackTitle, false);
291 } else {
292 title=song.isCdda() ? tr("Audio CD") : tr("Streams");
293 track=streamText(song, trackTitle);
294 }
295 } else if (isEmpty) {
296 title=Song::unknown();
297 track=trackTitle;
298 } else if (song.album.isEmpty()) {
299 title=song.albumArtistOrComposer();
300 track=song.trackAndTitleStr();
301 } else {
302 if (song.isFromOnlineService()) {
303 title=Song::displayAlbum(song.albumName(), Song::albumYear(song));
304 } else {
305 title=song.albumArtistOrComposer()+Song::constSep+Song::displayAlbum(song.albumName(), Song::albumYear(song));
306 }
307 track=song.trackAndTitleStr();
308 }
309 } else {
310 if (stream) {
311 track=streamText(song, trackTitle);
312 } else {
313 track=song.trackAndTitleStr();
314 }
315 }
316
317 if (song.priority>0) {
318 track=track+QLatin1String(" [")+QString::number(song.priority)+QChar(']');
319 }
320
321 painter->save();
322 painter->setFont(f);
323 #ifdef Q_OS_WIN
324 QColor textColor(option.palette.color(QPalette::Text));
325 #else
326 QColor textColor(option.palette.color(option.state&QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text));
327 #endif
328 QTextOption textOpt(Qt::AlignVCenter);
329 QRect r(option.rect.adjusted(constBorder+4, constBorder, -(constBorder+4), -constBorder));
330
331 if (state && GroupedView::State_StopAfterTrack!=state) {
332 QRectF border(option.rect.x()+1, option.rect.y()+1, option.rect.width()-3, option.rect.height()-3);
333 if (!title.isEmpty()) {
334 border.adjust(0, (border.height()/2)+1, 0, 0);
335 }
336 #ifdef Q_OS_MAC
337 QColor gradCol(OSXStyle::self()->viewPalette().color(QPalette::Highlight));
338 QColor borderCol(OSXStyle::self()->viewPalette().color(selected ? QPalette::HighlightedText : QPalette::Highlight));
339 #else
340 QColor gradCol(QApplication::palette().color(QPalette::Highlight));
341 QColor borderCol(QApplication::palette().color(selected ? QPalette::HighlightedText : QPalette::Highlight));
342 #endif
343 if (!selected) {
344 borderCol.setAlphaF(0.5);
345 }
346 gradCol.setAlphaF(selected ? 0.4 : 0.25);
347 painter->fillRect(border, gradCol);
348 painter->setPen(QPen(borderCol, 1));
349 painter->drawRect(border);
350 }
351
352 painter->setPen(textColor);
353 bool showTrackDuration=!duration.isEmpty();
354
355 if (isCollection || AlbumHeader==type) {
356 QPixmap pix;
357 // Draw cover...
358 if (isCollection) {
359 pix=index.data(Qt::DecorationRole).value<QIcon>().pixmap(constCoverSize, constCoverSize,
360 selected && textColor==qApp->palette().color(QPalette::HighlightedText)
361 ? QIcon::Selected : QIcon::Normal);
362 } else {
363 QPixmap *cover=/*stream ? 0 : */Covers::self()->get(song, constCoverSize);
364 pix=cover ? *cover : (stream && !song.isCdda() ? Icons::self()->streamIcon : Icons::self()->albumIcon(constCoverSize)).pixmap(constCoverSize, constCoverSize);
365 }
366
367 int maxSize=constCoverSize*pix.DEVICE_PIXEL_RATIO();
368
369 if (pix.width()>maxSize) {
370 pix=pix.scaled(maxSize, maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
371 }
372
373 QSize pixSize = pix.isNull() ? QSize(0, 0) : (pix.size() / pix.DEVICE_PIXEL_RATIO());
374
375 if (rtl) {
376 painter->drawPixmap(r.x()+r.width()-(pixSize.width()-constBorder), r.y()+((r.height()-pixSize.height())/2), pixSize.width(), pixSize.height(), pix);
377 r.adjust(0, 0, -(constCoverSize+constBorder), 0);
378 } else {
379 painter->drawPixmap(r.x()-2, r.y()+((r.height()-pixSize.height())/2), pixSize.width(), pixSize.height(), pix);
380 r.adjust(constCoverSize+constBorder, 0, 0, 0);
381 }
382
383 int td=index.data(Cantata::Role_AlbumDuration).toUInt();
384 QString totalDuration=td>0 && td!=song.time ? Utils::formatTime(td) : QString();
385 QRect duratioRect(r.x(), r.y(), r.width(), textHeight);
386 int totalDurationWidth=fm.horizontalAdvance(totalDuration)+8;
387 QRect textRect(r.x(), r.y(), r.width()-(rtl ? (4*constBorder) : totalDurationWidth), textHeight);
388 QFont tf(f);
389 tf.setBold(true);
390 title = QFontMetrics(tf).elidedText(title, Qt::ElideRight, textRect.width(), QPalette::WindowText);
391 painter->setFont(tf);
392 painter->drawText(textRect, title, textOpt);
393 if (!totalDuration.isEmpty()) {
394 painter->drawText(duratioRect, totalDuration, QTextOption(Qt::AlignVCenter|Qt::AlignRight));
395 }
396 BasicItemDelegate::drawLine(painter, r.adjusted(0, 0, 0, -r.height()/2), textColor, rtl, !rtl, 0.45);
397 r.adjust(0, textHeight+constBorder, 0, 0);
398 r=QRect(r.x(), r.y()+r.height()-(textHeight+1), r.width(), textHeight);
399 painter->setFont(f);
400 if (rtl) {
401 r.adjust(0, 0, (constCoverSize-(constBorder*3)), 0);
402 }
403
404 if (isCollection || !view->isExpanded(song.key, collection)) {
405 showTrackDuration=false;
406 track=tr("%n Track(s)", "", index.data(Cantata::Role_SongCount).toUInt());
407 }
408 } else if (rtl) {
409 r.adjust(0, 0, -(constBorder*4), 0);
410 } else {
411 r.adjust(constCoverSize+constBorder, 0, 0, 0);
412 }
413
414 GroupedView::drawPlayState(painter, option, r, state);
415 QVariant col = index.data(Cantata::Role_TextColor);
416 if (col.isValid()) {
417 textColor = col.value<QColor>();
418 }
419 painter->setPen(textColor);
420
421 int ratingsStart=rtl ? 0 : drawRatings(painter, song, r, fm, option.palette.color(QPalette::Active, QPalette::Text)); // TODO!!!
422
423 int durationWidth=showTrackDuration ? fm.horizontalAdvance(duration)+8 : 0;
424 QRect duratioRect(r.x(), r.y(), r.width(), textHeight);
425 QRect textRect(r.x(), r.y(), r.width()-durationWidth, textHeight);
426 if (ratingsStart>0) {
427 textRect.setWidth(ratingsStart-r.x());
428 }
429 track = fm.elidedText(track, Qt::ElideRight, textRect.width(), QPalette::WindowText);
430 painter->drawText(textRect, track, textOpt);
431 if (showTrackDuration) {
432 painter->drawText(duratioRect, duration, QTextOption(Qt::AlignVCenter|Qt::AlignRight));
433 }
434
435 if (mouseOver) {
436 drawIcons(painter, option.rect, mouseOver, rtl, AlbumHeader==type || isCollection ? AP_HBottom : AP_HMiddle, index);
437 }
438 BasicItemDelegate::drawLine(painter, option.rect, textColor);
439 painter->restore();
440 }
441
drawRatings(QPainter * painter,const Song & song,const QRect & r,const QFontMetrics & fm,const QColor & col) const442 int GroupedViewDelegate::drawRatings(QPainter *painter, const Song &song, const QRect &r, const QFontMetrics &fm, const QColor &col) const
443 {
444 if (song.rating>0 && song.rating<=Song::Rating_Max) {
445 if (!ratingPainter) {
446 ratingPainter=new RatingPainter(r.height()-(constBorder*8));
447 ratingPainter->setColor(col);
448 }
449
450 painter->save();
451 painter->setOpacity(painter->opacity()*0.75);
452 const QSize &ratingSize=ratingPainter->size();
453 int spacing=constBorder*2;
454 int durationWidth=fm.horizontalAdvance("0:00:00")+spacing;
455 QRect ratingRect(r.x()+r.width()-(durationWidth+ratingSize.width()+spacing),
456 r.y()+(r.height()-ratingSize.height())/2,
457 ratingSize.width(), ratingSize.height());
458 ratingPainter->paint(painter, ratingRect, song.rating);
459 painter->restore();
460 return ratingRect.x()-spacing;
461 }
462 return 0;
463 }
464
GroupedView(QWidget * parent,bool isPlayQueue)465 GroupedView::GroupedView(QWidget *parent, bool isPlayQueue)
466 : TreeView(parent, isPlayQueue)
467 , allowClose(true)
468 , startClosed(allowClose)
469 , autoExpand(true)
470 , filterActive(false)
471 , isMultiLevel(false)
472 , currentAlbum(Song::Null_Key)
473 {
474 setContextMenuPolicy(Qt::CustomContextMenu);
475 setAcceptDrops(true);
476 setDragDropOverwriteMode(false);
477 setDragDropMode(QAbstractItemView::DragDrop);
478 setSelectionMode(QAbstractItemView::ExtendedSelection);
479 setDropIndicatorShown(true);
480 setHeaderHidden(true);
481 setRootIsDecorated(false);
482 setSelectionBehavior(SelectRows);
483 setForceSingleColumn(true);
484 connect(this, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
485 GroupedViewDelegate *delegate=new GroupedViewDelegate(this);
486 setItemDelegate(delegate);
487 if (isPlayQueue) {
488 // 'underMouse' is used to work-around mouse over issues with some styles.
489 // PlayQueue does not have mouse over - so we never need to check this.
490 delegate->setUnderMouse(true);
491 }
492 }
493
~GroupedView()494 GroupedView::~GroupedView()
495 {
496 }
497
setModel(QAbstractItemModel * model)498 void GroupedView::setModel(QAbstractItemModel *model)
499 {
500 TreeView::setModel(model);
501 if (model) {
502 if (startClosed) {
503 updateCollectionRows();
504 }
505 connect(Covers::self(), SIGNAL(loaded(Song,int)), this, SLOT(coverLoaded(Song,int)));
506 } else {
507 controlledAlbums.clear();
508 disconnect(Covers::self(), SIGNAL(loaded(Song,int)), this, SLOT(coverLoaded(Song,int)));
509 }
510 }
511
setFilterActive(bool f)512 void GroupedView::setFilterActive(bool f)
513 {
514 if (f==filterActive) {
515 return;
516 }
517 filterActive=f;
518 if (filterActive && model()) {
519 quint32 count=model()->rowCount();
520 for (quint32 i=0; i<count; ++i) {
521 setRowHidden(i, QModelIndex(), false);
522 if (isMultiLevel) {
523 QModelIndex idx=model()->index(i, 0);
524 if (model()->hasChildren(idx)) {
525 quint32 childCount=model()->rowCount(idx);
526 for (quint32 c=0; c<childCount; ++c) {
527 setRowHidden(c, idx, false);
528 }
529 }
530 }
531 }
532 }
533 }
534
setStartClosed(bool sc)535 void GroupedView::setStartClosed(bool sc)
536 {
537 if (!allowClose || sc==startClosed) {
538 return;
539 }
540 controlledAlbums.clear();
541 startClosed=sc;
542 if (startClosed) {
543 updateCollectionRows();
544 }
545 }
546
updateRows(qint32 row,quint16 curAlbum,bool scroll,const QModelIndex & parent,bool forceScroll)547 void GroupedView::updateRows(qint32 row, quint16 curAlbum, bool scroll, const QModelIndex &parent, bool forceScroll)
548 {
549 currentAlbum=curAlbum;
550
551 if (row<0) {
552 row=0;
553 }
554
555 if (!model() || row>model()->rowCount(parent)) {
556 return;
557 }
558
559 if (filterActive && MPDState_Playing==MPDStatus::self()->state()) {
560 if (scroll) {
561 scrollTo(model()->index(row, 0, parent), QAbstractItemView::PositionAtCenter);
562 }
563 return;
564 }
565
566 updateRows(parent);
567 if (scroll && (MPDState_Playing==MPDStatus::self()->state() || forceScroll)) {
568 scrollTo(model()->index(row, 0, parent), QAbstractItemView::PositionAtCenter);
569 }
570 }
571
updateRows(const QModelIndex & parent)572 void GroupedView::updateRows(const QModelIndex &parent)
573 {
574 if (!model()) {
575 return;
576 }
577
578 qint32 count=model()->rowCount(parent);
579 quint16 lastKey=Song::Null_Key;
580 quint32 collection=parent.data(Cantata::Role_CollectionId).toUInt();
581 QSet<quint16> keys;
582
583 for (qint32 i=0; i<count; ++i) {
584 quint16 key=model()->index(i, 0, parent).data(Cantata::Role_Key).toUInt();
585 keys.insert(key);
586 bool hide=key==lastKey &&
587 !(key==currentAlbum && autoExpand) &&
588 ( ( startClosed && !controlledAlbums[collection].contains(key)) ||
589 ( !startClosed && controlledAlbums[collection].contains(key)));
590 setRowHidden(i, parent, hide);
591 lastKey=key;
592 }
593
594 // Check that 'controlledAlbums' only contains valid keys...
595 controlledAlbums[collection].intersect(keys);
596 }
597
updateCollectionRows()598 void GroupedView::updateCollectionRows()
599 {
600 if (!model()) {
601 return;
602 }
603
604 currentAlbum=Song::Null_Key;
605 qint32 count=model()->rowCount();
606 for (int i=0; i<count; ++i) {
607 updateRows(model()->index(i, 0));
608 }
609 }
610
toggle(const QModelIndex & idx)611 void GroupedView::toggle(const QModelIndex &idx)
612 {
613 if (!allowClose) {
614 return;
615 }
616
617 quint16 indexKey=idx.data(Cantata::Role_Key).toUInt();
618
619 if (indexKey==currentAlbum && autoExpand) {
620 return;
621 }
622
623 quint32 collection=idx.data(Cantata::Role_CollectionId).toUInt();
624 bool toBeHidden=false;
625 if (controlledAlbums[collection].contains(indexKey)) {
626 controlledAlbums[collection].remove(indexKey);
627 toBeHidden=startClosed;
628 } else {
629 controlledAlbums[collection].insert(indexKey);
630 toBeHidden=!startClosed;
631 }
632
633 if (model()) {
634 QModelIndex parent=idx.parent();
635 quint32 count=model()->rowCount(idx.parent());
636 for (quint32 i=0; i<count; ++i) {
637 QModelIndex index=model()->index(i, 0, parent);
638 quint16 key=index.data(Cantata::Role_Key).toUInt();
639 if (indexKey==key) {
640 if (isAlbumHeader(index)) {
641 dataChanged(index, index);
642 } else {
643 setRowHidden(i, parent, toBeHidden);
644 }
645 }
646 }
647 }
648 }
649
650 // Calculate list of selected indexes. If a collapsed album is selected, we also pretend all of its tracks
651 // are selected.
selectedIndexes(bool sorted) const652 QModelIndexList GroupedView::selectedIndexes(bool sorted) const
653 {
654 QModelIndexList indexes = TreeView::selectedIndexes(sorted);
655 QSet<QModelIndex> indexSet;
656 QModelIndexList sel;
657
658 for (const QModelIndex &idx: indexes) {
659 if (!indexSet.contains(idx)) {
660 indexSet.insert(idx);
661 sel.append(idx);
662 }
663 if (!idx.data(Cantata::Role_IsCollection).toBool()) {
664 quint16 key=idx.data(Cantata::Role_Key).toUInt();
665 quint32 collection=idx.data(Cantata::Role_CollectionId).toUInt();
666 if (!isExpanded(key, collection)) {
667 quint32 rowCount=model()->rowCount(idx.parent());
668 for (quint32 i=idx.row()+1; i<rowCount; ++i) {
669 QModelIndex next=idx.sibling(i, 0);
670 if (next.isValid()) {
671 quint16 nextKey=next.data(Cantata::Role_Key).toUInt();
672 quint32 nextCollection=idx.data(Cantata::Role_CollectionId).toUInt();
673
674 if (nextCollection==collection && nextKey==key) {
675 if (!indexSet.contains(next)) {
676 indexSet.insert(next);
677 sel.append(next);
678 }
679 } else {
680 break;
681 }
682 } else {
683 break;
684 }
685 }
686 }
687 }
688 }
689 return sel;
690 }
691
692 // If we are dropping onto a collapsed album, and we are in the bottom 1/2 of the row - then
693 // we need to drop the item at the end of the album's tracks. So, we adjust the 'drop row'
694 // by the amount of tracks.
dropEvent(QDropEvent * event)695 void GroupedView::dropEvent(QDropEvent *event)
696 {
697 QModelIndex parent;
698 quint32 dropRowAdjust=0;
699 if (model() && viewport()->rect().contains(event->pos())) {
700 // Dont allow to drop on an already selected row - as this seems to cuase a crash!!!
701 QModelIndex idx=TreeView::indexAt(event->pos());
702 if (idx.isValid() && selectionModel() && selectionModel()->isSelected(idx)) {
703 return;
704 }
705 if (idx.isValid() && isAlbumHeader(idx)) {
706 QRect rect(visualRect(idx));
707 if (event->pos().y()>(rect.y()+(rect.height()/2))) {
708 quint16 key=idx.data(Cantata::Role_Key).toUInt();
709 quint32 collection=idx.data(Cantata::Role_CollectionId).toUInt();
710 if (!isExpanded(key, collection)) {
711 parent=idx.parent();
712 quint32 rowCount=model()->rowCount(parent);
713 for (quint32 i=idx.row()+1; i<rowCount; ++i) {
714 QModelIndex next=idx.sibling(i, 0);
715 if (next.isValid()) {
716 quint16 nextKey=next.data(Cantata::Role_Key).toUInt();
717 if (nextKey==key) {
718 dropRowAdjust++;
719 } else {
720 break;
721 }
722 } else {
723 break;
724 }
725 }
726 model()->setData(parent, dropRowAdjust, Cantata::Role_DropAdjust);
727 }
728 }
729 }
730 }
731
732 TreeView::dropEvent(event);
733 model()->setData(parent, 0, Cantata::Role_DropAdjust);
734 }
735
coverLoaded(const Song & song,int size)736 void GroupedView::coverLoaded(const Song &song, int size)
737 {
738 if (filterActive || !isVisible() || size!=constCoverSize || song.isArtistImageRequest() || song.isComposerImageRequest()) {
739 return;
740 }
741 quint32 count=model()->rowCount();
742 quint16 lastKey=Song::Null_Key;
743 QString albumArtist=song.albumArtist();
744 QString album=song.album;
745
746 for (quint32 i=0; i<count; ++i) {
747 QModelIndex index=model()->index(i, 0);
748 if (!index.isValid()) {
749 continue;
750 }
751 if (isMultiLevel && model()->hasChildren(index)) {
752 lastKey=Song::Null_Key;
753 quint32 childCount=model()->rowCount(index);
754 for (quint32 c=0; c<childCount; ++c) {
755 QModelIndex child=model()->index(c, 0, index);
756 if (!child.isValid()) {
757 continue;
758 }
759 quint16 key=child.data(Cantata::Role_Key).toUInt();
760
761 if (key!=lastKey && !isRowHidden(c, index)) {
762 Song song=child.data(Cantata::Role_Song).value<Song>();
763 if (song.albumArtist()==albumArtist && song.album==album) {
764 dataChanged(child, child);
765 }
766 }
767 lastKey=key;
768 }
769 } else {
770 quint16 key=index.data(Cantata::Role_Key).toUInt();
771
772 if (key!=lastKey && !isRowHidden(i, QModelIndex())) {
773 Song song=index.data(Cantata::Role_Song).value<Song>();
774 if (song.albumArtist()==albumArtist && song.album==album) {
775 dataChanged(index, index);
776 }
777 }
778 lastKey=key;
779 }
780 }
781 }
782
collectionRemoved(quint32 key)783 void GroupedView::collectionRemoved(quint32 key)
784 {
785 controlledAlbums.remove(key);
786 }
787
itemClicked(const QModelIndex & idx)788 void GroupedView::itemClicked(const QModelIndex &idx)
789 {
790 if (isAlbumHeader(idx)) {
791 QRect indexRect(visualRect(idx));
792 QRect icon(indexRect.x()+constBorder+4, indexRect.y()+constBorder+((indexRect.height()-constCoverSize)/2),
793 constCoverSize, constCoverSize);
794 QRect header(indexRect);
795 header.setHeight(header.height()/2);
796 header.moveTo(viewport()->mapToGlobal(QPoint(header.x(), header.y())));
797 icon.moveTo(viewport()->mapToGlobal(QPoint(icon.x(), icon.y())));
798 if (allowClose && icon.contains(QCursor::pos())) {
799 toggle(idx);
800 } else if (header.contains(QCursor::pos())) {
801 QModelIndexList list;
802 unsigned int key=idx.data(Cantata::Role_Key).toUInt();
803 QModelIndex i=idx.sibling(idx.row()+1, 0);
804 // QModelIndexList sel=selectedIndexes();
805 QItemSelectionModel *selModel=selectionModel();
806 QModelIndexList unsel;
807
808 while (i.isValid() && i.data(Cantata::Role_Key).toUInt()==key) {
809 #if 0 // The following does not seem to work from the grouped playlist view - the 2nd row never get selected!
810 if (!sel.contains(i)) {
811 unsel.append(i);
812 }
813 #else
814 unsel.append(i);
815 #endif
816 list.append(i);
817 i=i.sibling(i.row()+1, 0);
818 }
819 if (list.count()) {
820 #if 0 // Commendted out as (as noted below) unselection is not working, and we always add to 'unsel' above (because of playlists)
821 if (unsel.isEmpty()) { // TODO: This is not working!!! CHECK selModel if re-add!!!
822 for (const QModelIndex &i: list) {
823 selModel->select(i, QItemSelectionModel::Deselect|QItemSelectionModel::Rows);
824 }
825 } else {
826 for (const QModelIndex &i: unsel) {
827 selModel->select(i, QItemSelectionModel::Select|QItemSelectionModel::Rows);
828 }
829 }
830 #else
831 if (!unsel.isEmpty() && selModel) {
832 for (const QModelIndex &i: unsel) {
833 selModel->select(i, QItemSelectionModel::Select|QItemSelectionModel::Rows);
834 }
835 }
836 #endif
837 }
838 }
839 }
840 }
841
expand(const QModelIndex & idx,bool singleOnly)842 void GroupedView::expand(const QModelIndex &idx, bool singleOnly)
843 {
844 if (allowClose && idx.isValid()) {
845 if (idx.data(Cantata::Role_IsCollection).toBool()) {
846 setExpanded(idx, true);
847 if (!singleOnly) {
848 quint32 count=model()->rowCount(idx);
849 for (quint32 i=0; i<count; ++i) {
850 expand(model()->index(i, 0, idx));
851 }
852 }
853 } else if (AlbumHeader==getType(idx)) {
854 quint16 indexKey=idx.data(Cantata::Role_Key).toUInt();
855 quint32 collection=idx.data(Cantata::Role_CollectionId).toUInt();
856 if (!isExpanded(indexKey, collection)) {
857 toggle(idx);
858 }
859 }
860 }
861 }
862
collapse(const QModelIndex & idx,bool singleOnly)863 void GroupedView::collapse(const QModelIndex &idx, bool singleOnly)
864 {
865 if (allowClose && idx.isValid()) {
866 if (idx.data(Cantata::Role_IsCollection).toBool()) {
867 setExpanded(idx, false);
868 if (!singleOnly) {
869 quint32 count=model()->rowCount(idx);
870 for (quint32 i=0; i<count; ++i) {
871 collapse(model()->index(i, 0, idx));
872 }
873 }
874 }
875 }
876 }
877
drawBranches(QPainter *,const QRect &,const QModelIndex &) const878 void GroupedView::drawBranches(QPainter *, const QRect &, const QModelIndex &) const
879 {
880 // Don't want any branch lines drawn!
881 }
882
883 #include "moc_groupedview.cpp"
884