File: musiclibraryproxymodel.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (149 lines) | stat: -rw-r--r-- 4,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 */
/*
 * Copyright (c) 2008 Sander Knopper (sander AT knopper DOT tk) and
 *                    Roeland Douma (roeland AT rullzer DOT com)
 *
 * This file is part of QtMPC.
 *
 * QtMPC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * QtMPC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QtMPC.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "musiclibraryproxymodel.h"
#include "musiclibraryitem.h"
#include "musiclibraryitemalbum.h"
#include "musiclibraryitemartist.h"
#include "musiclibraryitemsong.h"

MusicLibraryProxyModel::MusicLibraryProxyModel(QObject* parent)
	: ProxyModel(parent)
{
	setDynamicSortFilter(true);
	setFilterCaseSensitivity(Qt::CaseInsensitive);
	setSortCaseSensitivity(Qt::CaseInsensitive);
	setSortLocaleAware(true);
}

bool MusicLibraryProxyModel::filterAcceptsRoot(const MusicLibraryItem* item) const
{
	for (const MusicLibraryItem* i : static_cast<const MusicLibraryItemContainer*>(item)->childItems()) {
		if (MusicLibraryItem::Type_Artist == i->itemType() && filterAcceptsArtist(i)) {
			return true;
		}
		else if (MusicLibraryItem::Type_Song == i->itemType() && filterAcceptsSong(i)) {
			return true;
		}
	}

	return false;
}

bool MusicLibraryProxyModel::filterAcceptsArtist(const MusicLibraryItem* item) const
{
	for (const MusicLibraryItem* i : static_cast<const MusicLibraryItemContainer*>(item)->childItems()) {
		if (filterAcceptsAlbum(i)) {
			return true;
		}
	}

	return false;
}

bool MusicLibraryProxyModel::filterAcceptsAlbum(const MusicLibraryItem* item) const
{
	for (const MusicLibraryItem* i : static_cast<const MusicLibraryItemContainer*>(item)->childItems()) {
		if (filterAcceptsSong(i)) {
			return true;
		}
	}

	return false;
}

bool MusicLibraryProxyModel::filterAcceptsSong(const MusicLibraryItem* item) const
{
	return matchesFilter(static_cast<const MusicLibraryItemSong*>(item)->song());
}

bool MusicLibraryProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
	if (!filterEnabled) {
		return true;
	}
	if (!isChildOfRoot(sourceParent)) {
		return true;
	}

	const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
	const MusicLibraryItem* item = static_cast<const MusicLibraryItem*>(index.internalPointer());

	if (filter) {
		if (item == filter) {// Accept top-level item!
			return true;
		}
		const MusicLibraryItem* p = item->parentItem();
		while (p && p->parentItem()) {
			p = p->parentItem();
		}
		if (p != filter) {// Accept all items that are not children of top-level item!
			return true;
		}
	}

	if (filterStrings.isEmpty()) {
		return true;
	}

	switch (item->itemType()) {
	case MusicLibraryItem::Type_Root:
		return filterAcceptsRoot(item);
	case MusicLibraryItem::Type_Artist:
		return filterAcceptsArtist(item);
	case MusicLibraryItem::Type_Album:
		return filterAcceptsAlbum(item);
	case MusicLibraryItem::Type_Song:
		return filterAcceptsSong(item);
	default:
		break;
	}

	return false;
}

bool MusicLibraryProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
	if (left.row() < 0 || right.row() < 0) {
		return left.row() < 0;
	}

	if (static_cast<MusicLibraryItem*>(left.internalPointer())->itemType() == MusicLibraryItem::Type_Song) {
		MusicLibraryItemSong* l = static_cast<MusicLibraryItemSong*>(left.internalPointer());
		MusicLibraryItemSong* r = static_cast<MusicLibraryItemSong*>(right.internalPointer());
		return l->song() < r->song();
	}
	else if (static_cast<MusicLibraryItem*>(left.internalPointer())->itemType() == MusicLibraryItem::Type_Album) {
		return MusicLibraryItemAlbum::lessThan(static_cast<MusicLibraryItem*>(left.internalPointer()), static_cast<MusicLibraryItem*>(right.internalPointer()));
	}
	else if (static_cast<MusicLibraryItem*>(left.internalPointer())->itemType() == MusicLibraryItem::Type_Artist) {
		return MusicLibraryItemArtist::lessThan(static_cast<MusicLibraryItem*>(left.internalPointer()), static_cast<MusicLibraryItem*>(right.internalPointer()));
	}

	return QSortFilterProxyModel::lessThan(left, right);
}

#include "moc_musiclibraryproxymodel.cpp"