strawberry-audio-player-win.../src/playlist/playlistfilter.cpp

103 lines
3.5 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QObject>
#include <QString>
#include <QRegularExpression>
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include "playlist/playlist.h"
2018-02-27 18:06:05 +01:00
#include "playlistfilter.h"
#include "playlistfilterparser.h"
PlaylistFilter::PlaylistFilter(QObject *parent)
: QSortFilterProxyModel(parent),
filter_tree_(new NopFilter),
2021-06-28 00:19:57 +02:00
query_hash_(0) {
2018-02-27 18:06:05 +01:00
setDynamicSortFilter(true);
column_names_["title"] = Playlist::Column_Title;
column_names_["name"] = Playlist::Column_Title;
column_names_["artist"] = Playlist::Column_Artist;
column_names_["album"] = Playlist::Column_Album;
column_names_["albumartist"] = Playlist::Column_AlbumArtist;
column_names_["performer"] = Playlist::Column_Performer;
column_names_["composer"] = Playlist::Column_Composer;
2018-02-27 18:06:05 +01:00
column_names_["year"] = Playlist::Column_Year;
column_names_["originalyear"] = Playlist::Column_OriginalYear;
column_names_["track"] = Playlist::Column_Track;
column_names_["disc"] = Playlist::Column_Disc;
column_names_["length"] = Playlist::Column_Length;
2018-02-27 18:06:05 +01:00
column_names_["genre"] = Playlist::Column_Genre;
column_names_["samplerate"] = Playlist::Column_Samplerate;
column_names_["bitdepth"] = Playlist::Column_Bitdepth;
column_names_["bitrate"] = Playlist::Column_Bitrate;
2018-02-27 18:06:05 +01:00
column_names_["filename"] = Playlist::Column_Filename;
column_names_["grouping"] = Playlist::Column_Grouping;
column_names_["comment"] = Playlist::Column_Comment;
2018-02-27 18:06:05 +01:00
numerical_columns_ << Playlist::Column_Year
<< Playlist::Column_OriginalYear
2018-02-27 18:06:05 +01:00
<< Playlist::Column_Track
<< Playlist::Column_Disc
<< Playlist::Column_Length
2019-12-21 17:55:24 +01:00
<< Playlist::Column_Samplerate
<< Playlist::Column_Bitdepth
<< Playlist::Column_Bitrate;
2018-02-27 18:06:05 +01:00
}
2021-06-21 15:38:58 +02:00
PlaylistFilter::~PlaylistFilter() = default;
2018-02-27 18:06:05 +01:00
void PlaylistFilter::sort(int column, Qt::SortOrder order) {
// Pass this through to the Playlist, it does sorting itself
sourceModel()->sort(column, order);
}
bool PlaylistFilter::filterAcceptsRow(int row, const QModelIndex &parent) const {
2020-07-18 22:37:49 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QString filter = filterRegularExpression().pattern().remove('\\');
#else
2018-02-27 18:06:05 +01:00
QString filter = filterRegExp().pattern();
#endif
2018-02-27 18:06:05 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
size_t hash = qHash(filter);
#else
2018-02-27 18:06:05 +01:00
uint hash = qHash(filter);
#endif
2018-02-27 18:06:05 +01:00
if (hash != query_hash_) {
// Parse the query
FilterParser p(filter, column_names_, numerical_columns_);
filter_tree_.reset(p.parse());
query_hash_ = hash;
}
// Test the row
return filter_tree_->accept(row, parent, sourceModel());
}