strawberry-audio-player-win.../src/playlist/playlistfilterparser.h

101 lines
3.1 KiB
C
Raw Permalink 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>
*
* 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
*/
#ifndef PLAYLISTFILTERPARSER_H
#define PLAYLISTFILTERPARSER_H
#include "config.h"
#include <QSet>
2020-02-08 15:03:11 +01:00
#include <QMap>
2018-02-27 18:06:05 +01:00
#include <QString>
2020-02-08 15:03:11 +01:00
class QAbstractItemModel;
class QModelIndex;
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
// Structure for filter parse tree
2018-02-27 18:06:05 +01:00
class FilterTree {
public:
2021-06-20 19:04:08 +02:00
FilterTree() = default;
2018-02-27 18:06:05 +01:00
virtual ~FilterTree() {}
virtual bool accept(int row, const QModelIndex &parent, const QAbstractItemModel *const model) const = 0;
2023-02-18 14:09:27 +01:00
enum class FilterType {
2018-02-27 18:06:05 +01:00
Nop = 0,
Or,
And,
Not,
Column,
Term
};
virtual FilterType type() = 0;
2021-06-20 19:04:08 +02:00
private:
Q_DISABLE_COPY(FilterTree)
2018-02-27 18:06:05 +01:00
};
2021-06-20 19:04:08 +02:00
// Trivial filter that accepts *anything*
2018-02-27 18:06:05 +01:00
class NopFilter : public FilterTree {
public:
2020-06-15 21:55:05 +02:00
bool accept(int row, const QModelIndex &parent, const QAbstractItemModel *const model) const override { Q_UNUSED(row); Q_UNUSED(parent); Q_UNUSED(model); return true; }
2023-02-18 14:09:27 +01:00
FilterType type() override { return FilterType::Nop; }
2018-02-27 18:06:05 +01:00
};
// A utility class to parse search filter strings into a decision tree
// that can decide whether a playlist entry matches the filter.
//
// Here's a grammar describing the filters we expect:
//  expr ::= or-group
// or-group ::= and-group ('OR' and-group)*
// and-group ::= sexpr ('AND' sexpr)*
// sexpr ::= sterm | '-' sexpr | '(' or-group ')'
// sterm ::= col ':' sstring | sstring
// sstring ::= prefix? string
// string ::= [^:-()" ]+ | '"' [^"]+ '"'
// prefix ::= '=' | '<' | '>' | '<=' | '>='
// col ::= "title" | "artist" | ...
class FilterParser {
public:
2021-07-11 09:49:38 +02:00
explicit FilterParser(const QString &filter, const QMap<QString, int> &columns, const QSet<int> &numerical_cols);
2018-02-27 18:06:05 +01:00
FilterTree *parse();
private:
void advance();
FilterTree *parseOrGroup();
FilterTree *parseAndGroup();
2021-06-20 19:04:08 +02:00
// Check if iter is at the start of 'AND' if so, step over it and return true if not, return false and leave iter where it was
2018-02-27 18:06:05 +01:00
bool checkAnd();
2021-06-20 19:04:08 +02:00
// Check if iter is at the start of 'OR'
2018-02-27 18:06:05 +01:00
bool checkOr(bool step_over = true);
FilterTree *parseSearchExpression();
FilterTree *parseSearchTerm();
FilterTree *createSearchTermTreeNode(const QString &col, const QString &prefix, const QString &search) const;
QString::const_iterator iter_;
QString::const_iterator end_;
QString buf_;
const QString filterstring_;
const QMap<QString, int> columns_;
const QSet<int> numerical_columns_;
};
#endif // PLAYLISTFILTERPARSER_H