strawberry-audio-player-win.../src/internet/internetsearchview.h

222 lines
6.2 KiB
C
Raw Normal View History

2018-08-09 18:10:03 +02:00
/*
* Strawberry Music Player
* This code was part of Clementine (GlobalSearch)
* Copyright 2012, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-08-09 18:10:03 +02: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/>.
*
*/
#ifndef INTERNETSEARCHVIEW_H
#define INTERNETSEARCHVIEW_H
2018-08-09 18:10:03 +02:00
#include "config.h"
2020-04-13 06:30:40 +02:00
#include <QtGlobal>
2020-02-09 02:29:35 +01:00
#include <QObject>
2018-08-09 18:10:03 +02:00
#include <QWidget>
2020-04-13 06:30:40 +02:00
#include <QSet>
#include <QPair>
2018-08-09 18:10:03 +02:00
#include <QList>
2020-04-13 06:30:40 +02:00
#include <QMap>
2018-08-09 18:10:03 +02:00
#include <QString>
2020-04-13 06:30:40 +02:00
#include <QStringList>
#include <QUrl>
#include <QImage>
2018-08-09 18:10:03 +02:00
#include <QPixmap>
2020-04-13 06:30:40 +02:00
#include <QMetaType>
2018-08-09 18:10:03 +02:00
#include "core/scoped_ptr.h"
#include "core/shared_ptr.h"
2020-02-09 02:29:35 +01:00
#include "core/song.h"
2018-08-09 18:10:03 +02:00
#include "collection/collectionmodel.h"
#include "covermanager/albumcoverloaderresult.h"
2018-08-09 18:10:03 +02:00
class QSortFilterProxyModel;
class QMimeData;
class QTimer;
class QMenu;
class QAction;
class QActionGroup;
class QEvent;
class QKeyEvent;
class QShowEvent;
class QContextMenuEvent;
2020-04-13 06:30:40 +02:00
class QTimerEvent;
2018-08-09 18:10:03 +02:00
class Application;
2020-02-09 02:29:35 +01:00
class MimeData;
2018-08-09 18:10:03 +02:00
class GroupByDialog;
2020-04-13 06:30:40 +02:00
class InternetService;
class InternetSearchModel;
class Ui_InternetSearchView;
2018-08-09 18:10:03 +02:00
class InternetSearchView : public QWidget {
2018-08-09 18:10:03 +02:00
Q_OBJECT
public:
2020-04-07 16:49:15 +02:00
explicit InternetSearchView(QWidget *parent = nullptr);
2020-06-15 21:55:05 +02:00
~InternetSearchView() override;
2018-08-09 18:10:03 +02:00
2023-02-18 14:09:27 +01:00
enum class SearchType {
Artists = 1,
Albums = 2,
Songs = 3
2020-04-13 06:30:40 +02:00
};
struct Result {
Song metadata_;
QString pixmap_cache_key_;
};
2022-10-13 22:39:31 +02:00
using ResultList = QList<Result>;
void Init(Application *app, SharedPtr<InternetService> service);
2018-08-09 18:10:03 +02:00
bool SearchFieldHasFocus() const;
void FocusSearchField();
2021-06-22 13:56:27 +02:00
void LazyLoadAlbumCover(const QModelIndex &proxy_index);
2018-08-09 18:10:03 +02:00
2021-07-11 09:49:38 +02:00
protected:
2020-04-13 06:30:40 +02:00
struct PendingState {
PendingState() : orig_id_(-1) {}
2021-06-20 19:04:08 +02:00
PendingState(int orig_id, const QStringList &tokens) : orig_id_(orig_id), tokens_(tokens) {}
2020-04-13 06:30:40 +02:00
int orig_id_;
QStringList tokens_;
bool operator<(const PendingState &b) const {
return orig_id_ < b.orig_id_;
}
bool operator==(const PendingState &b) const {
return orig_id_ == b.orig_id_;
}
};
2020-06-15 21:55:05 +02:00
void showEvent(QShowEvent *e) override;
bool eventFilter(QObject *object, QEvent *e) override;
void timerEvent(QTimerEvent *e) override;
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
// These functions treat queries in the same way as CollectionQuery.
// They're useful for figuring out whether you got a result because it matched in the song title or the artist/album name.
static QStringList TokenizeQuery(const QString &query);
static bool Matches(const QStringList &tokens, const QString &string);
private:
struct DelayedSearch {
int id_;
QString query_;
SearchType type_;
};
bool SearchKeyEvent(QKeyEvent *e);
bool ResultsContextMenuEvent(QContextMenuEvent *e);
MimeData *SelectedMimeData();
void SetSearchType(const SearchType type);
int SearchAsync(const QString &query, SearchType type);
void SearchAsync(const int id, const QString &query, const SearchType type);
void SearchError(const int id, const QString &error);
void CancelSearch(const int id);
QString PixmapCacheKey(const Result &result) const;
bool FindCachedPixmap(const Result &result, QPixmap *pixmap) const;
int LoadAlbumCoverAsync(const Result &result);
2018-08-09 18:10:03 +02:00
signals:
2020-04-13 06:30:40 +02:00
void AddToPlaylist(QMimeData*);
void AddArtistsSignal(const SongList &songs);
void AddAlbumsSignal(const SongList &songs);
void AddSongsSignal(const SongList &songs);
2018-08-09 18:10:03 +02:00
private slots:
void SwapModels();
void TextEdited(const QString &text);
2020-04-13 06:30:40 +02:00
void StartSearch(const QString &query);
void SearchDone(const int service_id, const SongMap &songs, const QString &error);
2020-04-13 06:30:40 +02:00
2020-06-14 18:58:24 +02:00
void UpdateStatus(const int service_id, const QString &text);
void ProgressSetMaximum(const int service_id, const int max);
void UpdateProgress(const int service_id, const int progress);
2021-03-21 04:47:11 +01:00
void AddResults(const int service_id, const InternetSearchView::ResultList &results);
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
void FocusOnFilter(QKeyEvent *e);
2018-08-09 18:10:03 +02:00
void AddSelectedToPlaylist();
void LoadSelected();
void OpenSelectedInNewPlaylist();
void AddSelectedToPlaylistEnqueue();
2020-04-13 06:30:40 +02:00
void AddArtists();
void AddAlbums();
void AddSongs();
2018-08-09 18:10:03 +02:00
void SearchForThis();
2020-04-13 06:30:40 +02:00
void OpenSettingsDialog();
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
void SearchArtistsClicked(const bool);
void SearchAlbumsClicked(const bool);
void SearchSongsClicked(const bool);
2018-08-09 18:10:03 +02:00
void GroupByClicked(QAction *action);
2021-06-20 19:04:08 +02:00
void SetGroupBy(const CollectionModel::Grouping g);
2018-08-09 18:10:03 +02:00
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &albumcover_result);
2020-04-13 06:30:40 +02:00
public slots:
void ReloadSettings();
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
private:
static const int kSwapModelsTimeoutMsec;
static const int kDelayedSearchTimeoutMs;
static const int kArtHeight;
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
private:
2018-08-09 18:10:03 +02:00
Application *app_;
SharedPtr<InternetService> service_;
Ui_InternetSearchView *ui_;
ScopedPtr<GroupByDialog> group_by_dialog_;
2018-08-09 18:10:03 +02:00
QMenu *context_menu_;
QList<QAction*> context_actions_;
QActionGroup *group_by_actions_;
// Like graphics APIs have a front buffer and a back buffer, there's a front model and a back model
// The front model is the one that's shown in the UI and the back model is the one that lies in wait.
// current_model_ will point to either the front or the back model.
InternetSearchModel *front_model_;
InternetSearchModel *back_model_;
InternetSearchModel *current_model_;
2018-08-09 18:10:03 +02:00
QSortFilterProxyModel *front_proxy_;
QSortFilterProxyModel *back_proxy_;
QSortFilterProxyModel *current_proxy_;
QTimer *swap_models_timer_;
bool use_pretty_covers_;
2020-04-13 06:30:40 +02:00
SearchType search_type_;
bool search_error_;
int last_search_id_;
int searches_next_id_;
QMap<int, DelayedSearch> delayed_searches_;
QMap<int, PendingState> pending_searches_;
QMap<quint64, QPair<QModelIndex, QString>> cover_loader_tasks_;
2018-08-09 18:10:03 +02:00
};
2020-04-13 06:30:40 +02:00
Q_DECLARE_METATYPE(InternetSearchView::Result)
Q_DECLARE_METATYPE(InternetSearchView::ResultList)
2018-08-09 18:10:03 +02:00
#endif // INTERNETSEARCHVIEW_H