File indexing completed on 2024-05-19 04:49:42

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Jones <danielcjones@gmail.com>                             *
0003  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
0004  * Copyright (c) 2010, 2011 Ralf Engels <ralf-engels@gmx.de>                            *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0009  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0010  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0011  * version 3 of the license.                                                            *
0012  *                                                                                      *
0013  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0016  *                                                                                      *
0017  * You should have received a copy of the GNU General Public License along with         *
0018  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0019  ****************************************************************************************/
0020 
0021 #ifndef AMAROK_TAGMATCHBIAS_H
0022 #define AMAROK_TAGMATCHBIAS_H
0023 
0024 #include "amarok_export.h"
0025 #include "core/collections/QueryMaker.h"
0026 #include "dynamic/Bias.h"
0027 #include "dynamic/BiasFactory.h"
0028 #include "widgets/MetaQueryWidget.h"
0029 
0030 #include <QDateTime>
0031 
0032 class QWidget;
0033 class QCheckBox;
0034 
0035 namespace Dynamic
0036 {
0037 
0038     class TagMatchBias;
0039 
0040     /** An abstract bias that will check matching tracks against the results from a query maker.
0041         You can use this base class for writing your own biases.
0042         In all cases you have to implement newQuery which creates a
0043         QueryMaker and starts it.
0044     */
0045     class AMAROK_EXPORT SimpleMatchBias : public AbstractBias
0046     {
0047         Q_OBJECT
0048 
0049         public:
0050             SimpleMatchBias();
0051 
0052             void fromXml( QXmlStreamReader *reader ) override;
0053             void toXml( QXmlStreamWriter *writer ) const override;
0054 
0055             TrackSet matchingTracks( const Meta::TrackList& playlist,
0056                                              int contextCount, int finalCount,
0057                                              const TrackCollectionPtr &universe ) const override;
0058 
0059             bool trackMatches( int position,
0060                                        const Meta::TrackList& playlist,
0061                                        int contextCount ) const override;
0062 
0063             /** Is the result inverted (e.g. does NOT contain) */
0064             bool isInvert() const;
0065             void setInvert( bool value );
0066 
0067         public Q_SLOTS:
0068             void invalidate() override;
0069 
0070         protected Q_SLOTS:
0071             /** Called when we get new uids from the query maker */
0072             virtual void updateReady( const QStringList &uids );
0073 
0074             /** Called when the querymaker is finished */
0075             virtual void updateFinished();
0076 
0077             /** Creates a new query to get matching tracks. */
0078             virtual void newQuery() = 0;
0079 
0080         protected:
0081             MetaQueryWidget::Filter m_filter;
0082 
0083             mutable QScopedPointer<Collections::QueryMaker> m_qm;
0084 
0085             mutable TrackSet m_tracks;
0086 
0087             /** Time when we got the query result.
0088                 We don't want results to be valid forever.
0089                 Fix 311906 */
0090             QDateTime m_tracksTime;
0091 
0092             /** Returns true if the tracks in m_tracks reflect the
0093                 current filter.
0094                 Tracks are only valid for a short time.
0095             */
0096             bool tracksValid() const;
0097 
0098             /** The results are reported inverted (tracks that not match) */
0099             bool m_invert;
0100 
0101         private:
0102             Q_DISABLE_COPY(SimpleMatchBias)
0103     };
0104 
0105     /** A bias widget for the TagMatchBias */
0106     class TagMatchBiasWidget : public QWidget
0107     {
0108         Q_OBJECT
0109 
0110         public:
0111             explicit TagMatchBiasWidget( Dynamic::TagMatchBias* bias, QWidget* parent = nullptr );
0112 
0113         private Q_SLOTS:
0114             void syncControlsToBias();
0115             void syncBiasToControls();
0116 
0117         private:
0118             QCheckBox* m_invertBox;
0119             MetaQueryWidget* m_queryWidget;
0120 
0121             Dynamic::TagMatchBias* m_bias;
0122     };
0123 
0124 
0125     /** A bias that matches tracks against a MetaQueryWidget filter. */
0126     class TagMatchBias : public SimpleMatchBias
0127     {
0128         Q_OBJECT
0129 
0130         public:
0131             TagMatchBias();
0132 
0133             void fromXml( QXmlStreamReader *reader ) override;
0134             void toXml( QXmlStreamWriter *writer ) const override;
0135 
0136             static QString sName();
0137             QString name() const override;
0138             QString toString() const override;
0139 
0140             QWidget* widget( QWidget* parent = nullptr ) override;
0141 
0142             bool trackMatches( int position,
0143                                        const Meta::TrackList& playlist,
0144                                        int contextCount ) const override;
0145 
0146 
0147             MetaQueryWidget::Filter filter() const;
0148             void setFilter( const MetaQueryWidget::Filter &filter );
0149 
0150         protected Q_SLOTS:
0151             void newQuery() override;
0152 
0153         protected:
0154             static QString nameForCondition( MetaQueryWidget::FilterCondition cond );
0155             static MetaQueryWidget::FilterCondition conditionForName( const QString &name );
0156 
0157             bool matches( const Meta::TrackPtr &track ) const;
0158 
0159         private:
0160             Q_DISABLE_COPY(TagMatchBias)
0161     };
0162 
0163 
0164     class AMAROK_EXPORT TagMatchBiasFactory : public Dynamic::AbstractBiasFactory
0165     {
0166         public:
0167             QString i18nName() const override;
0168             QString name() const override;
0169             QString i18nDescription() const override;
0170             BiasPtr createBias() override;
0171     };
0172 
0173 }
0174 
0175 #endif
0176