File: searchmodel.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 (337 lines) | stat: -rw-r--r-- 9,178 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program 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.
 *
 * This program 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 this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "searchmodel.h"
#include "gui/settings.h"
#include "playqueuemodel.h"
#include "roles.h"
#include "widgets/icons.h"
#include <QLocale>
#include <QMimeData>
#include <QString>
#include <QUrl>
#include <QVariant>

QString SearchModel::headerText(int col)
{
	switch (col) {
	case COL_TRACK: return PlayQueueModel::headerText(PlayQueueModel::COL_TRACK);
	case COL_DISC: return PlayQueueModel::headerText(PlayQueueModel::COL_DISC);
	case COL_TITLE: return PlayQueueModel::headerText(PlayQueueModel::COL_TITLE);
	case COL_ARTIST: return PlayQueueModel::headerText(PlayQueueModel::COL_ARTIST);
	case COL_ALBUM: return PlayQueueModel::headerText(PlayQueueModel::COL_ALBUM);
	case COL_LENGTH: return PlayQueueModel::headerText(PlayQueueModel::COL_LENGTH);
	case COL_YEAR: return PlayQueueModel::headerText(PlayQueueModel::COL_YEAR);
	case COL_ORIGYEAR: return PlayQueueModel::headerText(PlayQueueModel::COL_ORIGYEAR);
	case COL_GENRE: return PlayQueueModel::headerText(PlayQueueModel::COL_GENRE);
	case COL_COMPOSER: return PlayQueueModel::headerText(PlayQueueModel::COL_COMPOSER);
	case COL_PERFORMER: return PlayQueueModel::headerText(PlayQueueModel::COL_PERFORMER);
	case COL_RATING: return PlayQueueModel::headerText(PlayQueueModel::COL_RATING);
	default: return QString();
	}
}

SearchModel::SearchModel(QObject* parent)
	: ActionModel(parent), multiCol(false)
{
	alignments[COL_TITLE] = alignments[COL_ARTIST] = alignments[COL_ALBUM] = alignments[COL_GENRE] = alignments[COL_COMPOSER] = alignments[COL_PERFORMER] = int(Qt::AlignVCenter | Qt::AlignLeft);
	alignments[COL_TRACK] = alignments[COL_LENGTH] = alignments[COL_DISC] = alignments[COL_YEAR] = alignments[COL_ORIGYEAR] = int(Qt::AlignVCenter | Qt::AlignRight);
	alignments[COL_RATING] = int(Qt::AlignVCenter | Qt::AlignHCenter);
}

SearchModel::~SearchModel()
{
	clear();
}

QModelIndex SearchModel::index(int row, int col, const QModelIndex& parent) const
{
	if (!hasIndex(row, col, parent)) {
		return QModelIndex();
	}

	return row < songList.count() ? createIndex(row, col, (void*)(&songList.at(row))) : QModelIndex();
}

QModelIndex SearchModel::parent(const QModelIndex& index) const
{
	Q_UNUSED(index)
	return QModelIndex();
}

QVariant SearchModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (Qt::Horizontal == orientation) {
		switch (role) {
		case Qt::DisplayRole:
			return headerText(section);
		case Cantata::Role_ContextMenuText:
			return COL_TRACK == section ? tr("# (Track Number)") : headerText(section);
		case Qt::TextAlignmentRole:
			return alignments[section];
		case Cantata::Role_InitiallyHidden:
			return COL_YEAR == section || COL_ORIGYEAR == section || COL_DISC == section || COL_GENRE == section || COL_COMPOSER == section || COL_PERFORMER == section || COL_RATING == section;
		case Cantata::Role_Hideable:
			return COL_TITLE != section && COL_ARTIST != section;
		case Cantata::Role_Width:
			switch (section) {
			case COL_TRACK: return 0.075;
			case COL_DISC: return 0.03;
			case COL_TITLE: return 0.3;
			case COL_ARTIST: return 0.27;
			case COL_ALBUM: return 0.27;
			case COL_LENGTH: return 0.05;
			case COL_YEAR: return 0.05;
			case COL_ORIGYEAR: return 0.05;
			case COL_GENRE: return 0.1;
			case COL_COMPOSER: return 0.2;
			case COL_PERFORMER: return 0.2;
			case COL_RATING: return 0.08;
			}
		default:
			break;
		}
	}
	return QVariant();
}

bool SearchModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)
{
	if (Qt::Horizontal == orientation && Qt::TextAlignmentRole == role && section >= 0) {
		int al = value.toInt() | Qt::AlignVCenter;
		if (al != alignments[section]) {
			alignments[section] = al;
			return true;
		}
	}
	return false;
}

int SearchModel::rowCount(const QModelIndex& parent) const
{
	return parent.isValid() ? 0 : songList.count();
}

QVariant SearchModel::data(const QModelIndex& index, int role) const
{
	if (!index.isValid() && Cantata::Role_RatingCol == role) {
		return COL_RATING;
	}

	const Song* song = toSong(index);

	if (!song) {
		return QVariant();
	}

	switch (role) {
	case Qt::DecorationRole:
		if (multiCol) {
			return QVariant();
		}
		return Song::Playlist == song->type
				? Icons::self()->playlistListIcon
				: song->isStream()
				? Icons::self()->streamIcon
				: Icons::self()->audioListIcon;
	case Qt::TextAlignmentRole:
		return alignments[index.column()];
	case Qt::DisplayRole:
		if (multiCol) {
			switch (index.column()) {
			case COL_TITLE:
				return song->title.isEmpty() ? Utils::getFile(song->file) : song->title;
			case COL_ARTIST:
				return song->artist.isEmpty() ? Song::unknown() : song->artist;
			case COL_ALBUM:
				if (song->isStream() && song->album.isEmpty()) {
					QString n = song->name();
					if (!n.isEmpty()) {
						return n;
					}
				}
				return song->album;
			case COL_TRACK:
				if (song->track <= 0) {
					return QVariant();
				}
				return song->track;
			case COL_LENGTH:
				return Utils::formatTime(song->time);
			case COL_DISC:
				if (song->disc <= 0) {
					return QVariant();
				}
				return song->disc;
			case COL_YEAR:
				if (song->year <= 0) {
					return QVariant();
				}
				return song->year;
			case COL_ORIGYEAR:
				if (song->origYear <= 0) {
					return QVariant();
				}
				return song->origYear;
			case COL_GENRE:
				return song->displayGenre();
			case COL_COMPOSER:
				return song->composer();
			case COL_PERFORMER:
				return song->performer();
			default:
				break;
			}
			break;
		}
		return song->entryName();
	case Qt::ToolTipRole:
		if (!Settings::self()->infoTooltips()) {
			return QVariant();
		}
		return song->toolTip();
	case Cantata::Role_MainText:
		return song->title.isEmpty() ? song->file : song->trackAndTitleStr();
	case Cantata::Role_SubText: {
		QString resp = song->artist;
		QString al = song->displayAlbum();
		if (!al.isEmpty()) {
			if (!resp.isEmpty()) {
				resp += Song::constSep;
			}
			resp += al;
		}
		if (song->time > 0) {
			if (!resp.isEmpty()) {
				resp += Song::constSep;
			}
			resp += Utils::formatTime(song->time);
		}
		return resp;
	}
	case Cantata::Role_ListImage:
		return true;
	case Cantata::Role_CoverSong: {
		QVariant v;
		v.setValue(*song);
		return v;
	}
	case Cantata::Role_Song: {
		QVariant var;
		var.setValue(*song);
		return var;
	}
	default:
		return ActionModel::data(index, role);
	}
	return QVariant();
}

Qt::ItemFlags SearchModel::flags(const QModelIndex& index) const
{
	if (index.isValid()) {
		return Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled;
	}
	else {
		return Qt::NoItemFlags;
	}
}

QStringList SearchModel::filenames(const QModelIndexList& indexes, bool allowPlaylists) const
{
	QList<Song> list = songs(indexes, allowPlaylists);
	QStringList fnames;
	for (const Song& s : list) {
		fnames.append(s.file);
	}
	return fnames;
}

QList<Song> SearchModel::songs(const QModelIndexList& indexes, bool allowPlaylists) const
{
	QList<Song> list;
	QSet<QString> files;
	for (const QModelIndex& index : indexes) {
		if (!index.isValid() || 0 != index.column()) {
			continue;
		}
		Song* song = static_cast<Song*>(index.internalPointer());
		if ((allowPlaylists || Song::Playlist != song->type) && !files.contains(song->file)) {
			Song s = *song;
			fixPath(s);
			list << s;
			files << s.file;
		}
	}
	return list;
}

QMimeData* SearchModel::mimeData(const QModelIndexList& indexes) const
{
	QMimeData* mimeData = new QMimeData();
	PlayQueueModel::encode(*mimeData, PlayQueueModel::constFileNameMimeType, filenames(indexes, true));
	return mimeData;
}

QStringList SearchModel::mimeTypes() const
{
	QStringList types;
	types << PlayQueueModel::constFileNameMimeType;
	return types;
}

void SearchModel::refresh()
{
	QString k = currentKey;
	QString v = currentValue;
	clear();
	search(k, v);
}

void SearchModel::clear()
{
	if (!songList.isEmpty()) {
		beginRemoveRows(QModelIndex(), 0, songList.count() - 1);
		songList.clear();
		endRemoveRows();
	}
	currentKey = currentValue = QString();
	emit statsUpdated(0, 0);
}

void SearchModel::results(const QList<Song>& songs)
{
	beginResetModel();
	songList.clear();
	songList = songs;
	endResetModel();
	quint32 time = 0;
	for (const Song& s : songList) {
		time += s.time;
	}

	emit statsUpdated(songList.size(), time);
	emit searched();
}

#include "moc_searchmodel.cpp"