1 /****************************************************************************************
2  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
7  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
8  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
9  * version 3 of the license.                                                            *
10  *                                                                                      *
11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
13  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
14  *                                                                                      *
15  * You should have received a copy of the GNU General Public License along with         *
16  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
17  ****************************************************************************************/
18 
19 #ifndef AMAROK_QUIZPLAY_BIAS_H
20 #define AMAROK_QUIZPLAY_BIAS_H
21 
22 #include "dynamic/BiasFactory.h"
23 #include "dynamic/biases/TagMatchBias.h"
24 #include "widgets/MetaQueryWidget.h"
25 
26 namespace Dynamic
27 {
28     /** This bias will pick tracks that start with a character that the last track ended with. */
29     class QuizPlayBias : public SimpleMatchBias
30     {
31         Q_OBJECT
32 
33         public:
34             enum FollowType
35             {
36                 TitleToTitle,
37                 ArtistToArtist,
38                 AlbumToAlbum
39             };
40 
41             QuizPlayBias();
42 
43             void fromXml( QXmlStreamReader *reader ) override;
44             void toXml( QXmlStreamWriter *writer ) const override;
45 
46             static QString sName();
47             QString name() const override;
48             QString toString() const override;
49 
50             QWidget* widget( QWidget* parent = nullptr ) override;
51 
52             TrackSet matchingTracks( const Meta::TrackList& playlist,
53                                              int contextCount, int finalCount,
54                                              const TrackCollectionPtr &universe ) const override;
55 
56             bool trackMatches( int position,
57                                        const Meta::TrackList& playlist,
58                                        int contextCount ) const override;
59 
60             FollowType follow() const;
61             void setFollow( FollowType value );
62 
63         public Q_SLOTS:
64             void updateFinished() override;
65             void invalidate() override;
66 
67         protected Q_SLOTS:
68             void selectionChanged( int );
69             void newQuery() override;
70 
71         protected:
72             /** A smart function to compute the last character in the string.
73                 It ignores braces and CD texts. */
74             static QChar lastChar( const QString &str );
75             static QString nameForFollow( FollowType match );
76             static FollowType followForName( const QString &name );
77 
78             FollowType m_follow;
79 
80             mutable QChar m_currentCharacter;
81             mutable QHash< QChar, TrackSet > m_characterTrackMap;
82 
83         private:
84             Q_DISABLE_COPY(QuizPlayBias)
85     };
86 
87 
88     class AMAROK_EXPORT QuizPlayBiasFactory : public Dynamic::AbstractBiasFactory
89     {
90         public:
91             QString i18nName() const override;
92             QString name() const override;
93             QString i18nDescription() const override;
94             BiasPtr createBias() override;
95     };
96 
97 }
98 
99 #endif
100 
101