Changeset View
Changeset View
Standalone View
Standalone View
src/context/applets/albums/plugin/AlbumsEngine.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2017 Malte Veerman <malte.veerman@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License along | ||||
15 | * with this program; if not, write to the Free Software Foundation, Inc., | ||||
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "AlbumsEngine.h" | ||||
20 | #include "AlbumsDefs.h" | ||||
21 | #include "AlbumItem.h" | ||||
22 | #include "TrackItem.h" | ||||
23 | | ||||
24 | #include <capabilities/ActionsCapability.h> | ||||
25 | #include <collections/QueryMaker.h> | ||||
26 | #include <core/support/Amarok.h> | ||||
27 | #include <core-impl/collections/support/CollectionManager.h> | ||||
28 | #include <Debug.h> | ||||
29 | #include <dialogs/TagDialog.h> | ||||
30 | #include <EngineController.h> | ||||
31 | #include <playlist/PlaylistController.h> | ||||
32 | | ||||
33 | #include <QMenu> | ||||
34 | | ||||
35 | #include <KLocalizedString> | ||||
36 | | ||||
37 | AlbumsEngine::AlbumsEngine( QObject *parent ) | ||||
38 | : QObject( parent ) | ||||
39 | , m_lastQueryMaker( Q_NULLPTR ) | ||||
40 | , m_model( new AlbumsModel( this ) ) | ||||
41 | , m_proxyModel( new AlbumsProxyModel( this ) ) | ||||
42 | { | ||||
43 | EngineController* engine = The::engineController(); | ||||
44 | | ||||
45 | connect( engine, &EngineController::trackPlaying, | ||||
46 | this, &AlbumsEngine::slotTrackChanged ); | ||||
47 | connect( engine, &EngineController::stopped, | ||||
48 | this, &AlbumsEngine::stopped ); | ||||
49 | connect( engine, &EngineController::trackMetadataChanged, | ||||
50 | this, &AlbumsEngine::slotTrackMetadataChanged ); | ||||
51 | | ||||
52 | m_model->setColumnCount( 1 ); | ||||
53 | m_proxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive ); | ||||
54 | m_proxyModel->setSortLocaleAware( true ); | ||||
55 | m_proxyModel->setDynamicSortFilter( true ); | ||||
56 | m_proxyModel->setSourceModel( m_model ); | ||||
57 | m_proxyModel->setFilterRole( NameRole ); | ||||
58 | } | ||||
59 | | ||||
60 | void AlbumsEngine::slotTrackMetadataChanged( Meta::TrackPtr track ) | ||||
61 | { | ||||
62 | if( !track || !track->album() || !track->album()->albumArtist() ) | ||||
63 | return; | ||||
64 | | ||||
65 | if( track->album()->albumArtist() == m_artist ) | ||||
66 | return; | ||||
67 | | ||||
68 | m_artist = track->album()->albumArtist(); | ||||
69 | update(); | ||||
70 | } | ||||
71 | | ||||
72 | void AlbumsEngine::slotTrackChanged( Meta::TrackPtr track ) | ||||
73 | { | ||||
74 | if( !track || track == m_currentTrack ) | ||||
75 | return; | ||||
76 | | ||||
77 | m_currentTrack = track; | ||||
78 | slotTrackMetadataChanged( track ); | ||||
79 | } | ||||
80 | | ||||
81 | | ||||
82 | void AlbumsEngine::stopped() | ||||
83 | { | ||||
84 | m_currentTrack.clear(); | ||||
85 | m_artist.clear(); | ||||
86 | | ||||
87 | // Collect data for the recently added albums | ||||
88 | Collections::QueryMaker *qm = CollectionManager::instance()->queryMaker(); | ||||
89 | qm->setAutoDelete( true ); | ||||
90 | qm->setQueryType( Collections::QueryMaker::Album ); | ||||
91 | qm->excludeFilter( Meta::valAlbum, QString(), true, true ); | ||||
92 | qm->orderBy( Meta::valCreateDate, true ); | ||||
93 | qm->limitMaxResultSize( Amarok::config("Albums Applet").readEntry("RecentlyAdded", 5) ); | ||||
94 | | ||||
95 | connect( qm, &Collections::QueryMaker::newAlbumsReady, | ||||
96 | this, &AlbumsEngine::resultReady, Qt::QueuedConnection ); | ||||
97 | | ||||
98 | m_lastQueryMaker = qm; | ||||
99 | qm->run(); | ||||
100 | } | ||||
101 | | ||||
102 | void AlbumsEngine::update() | ||||
103 | { | ||||
104 | DEBUG_BLOCK | ||||
105 | | ||||
106 | // -- search the collection for albums with the same artist | ||||
107 | Collections::QueryMaker *qm = CollectionManager::instance()->queryMaker(); | ||||
108 | qm->setAutoDelete( true ); | ||||
109 | qm->addFilter( Meta::valArtist, m_artist->name(), true, true ); | ||||
110 | qm->setAlbumQueryMode( Collections::QueryMaker::AllAlbums ); | ||||
111 | qm->setQueryType( Collections::QueryMaker::Album ); | ||||
112 | | ||||
113 | connect( qm, &Collections::QueryMaker::newAlbumsReady, | ||||
114 | this, &AlbumsEngine::resultReady, Qt::QueuedConnection ); | ||||
115 | | ||||
116 | m_lastQueryMaker = qm; | ||||
117 | qm->run(); | ||||
118 | } | ||||
119 | | ||||
120 | void AlbumsEngine::resultReady( const Meta::AlbumList &albums ) | ||||
121 | { | ||||
122 | if( sender() != m_lastQueryMaker ) | ||||
123 | return; | ||||
124 | | ||||
125 | m_model->clear(); | ||||
126 | m_proxyModel->setMode( m_currentTrack ? AlbumsProxyModel::SortByYear : AlbumsProxyModel::SortByCreateDate ); | ||||
127 | | ||||
128 | for( auto album : albums ) | ||||
129 | { | ||||
130 | // do not show all tracks without an album from the collection, this takes ages | ||||
131 | // TODO: show all tracks from this artist that are not part of an album | ||||
132 | if( album->name().isEmpty() ) | ||||
133 | continue; | ||||
134 | | ||||
135 | Meta::TrackList tracks = album->tracks(); | ||||
136 | if( tracks.isEmpty() ) | ||||
137 | continue; | ||||
138 | | ||||
139 | AlbumItem *albumItem = new AlbumItem(); | ||||
140 | albumItem->setIconSize( 50 ); | ||||
141 | albumItem->setAlbum( album ); | ||||
142 | albumItem->setShowArtist( !m_currentTrack ); | ||||
143 | | ||||
144 | int numberOfDiscs = 0; | ||||
145 | int childRow = 0; | ||||
146 | | ||||
147 | std::stable_sort( tracks.begin(), tracks.end(), Meta::Track::lessThan ); | ||||
148 | | ||||
149 | QMultiHash< int, TrackItem* > trackItems; // hash of tracks items for each disc | ||||
150 | for( const auto &track : tracks ) | ||||
151 | { | ||||
152 | if( numberOfDiscs < track->discNumber() ) | ||||
153 | numberOfDiscs = track->discNumber(); | ||||
154 | | ||||
155 | TrackItem *trackItem = new TrackItem(); | ||||
156 | trackItem->setTrack( track ); | ||||
157 | | ||||
158 | // bold the current track to make it more visible | ||||
159 | if( m_currentTrack && m_currentTrack == track ) | ||||
160 | { | ||||
161 | trackItem->bold(); | ||||
162 | } | ||||
163 | | ||||
164 | // If compilation and same artist, then highlight, but only if there's a current track | ||||
165 | if( m_currentTrack | ||||
166 | && m_currentTrack->artist() && track->artist() | ||||
167 | && album->isCompilation() ) | ||||
168 | { | ||||
169 | trackItem->italicise(); | ||||
170 | } | ||||
171 | trackItems.insert( track->discNumber(), trackItem ); | ||||
172 | } | ||||
173 | | ||||
174 | for( int i = 0; i <= numberOfDiscs; ++i ) | ||||
175 | { | ||||
176 | QList<TrackItem*> items = trackItems.values( i ); | ||||
177 | if( !items.isEmpty() ) | ||||
178 | { | ||||
179 | const TrackItem *item = items.first(); | ||||
180 | QStandardItem *discItem( 0 ); | ||||
181 | if( numberOfDiscs > 1 ) | ||||
182 | { | ||||
183 | discItem = new QStandardItem( i18n("Disc %1", item->track()->discNumber()) ); | ||||
184 | albumItem->setChild( childRow++, discItem ); | ||||
185 | int discChildRow = 0; | ||||
186 | foreach( TrackItem *trackItem, items ) | ||||
187 | discItem->setChild( discChildRow++, trackItem ); | ||||
188 | } | ||||
189 | else | ||||
190 | { | ||||
191 | foreach( TrackItem *trackItem, items ) | ||||
192 | albumItem->setChild( childRow++, trackItem ); | ||||
193 | } | ||||
194 | } | ||||
195 | } | ||||
196 | m_model->appendRow( albumItem ); | ||||
197 | } | ||||
198 | | ||||
199 | m_proxyModel->sort( 0 ); | ||||
200 | } | ||||
201 | | ||||
202 | QString AlbumsEngine::filterPattern() const | ||||
203 | { | ||||
204 | return m_proxyModel->filterRegExp().pattern(); | ||||
205 | } | ||||
206 | | ||||
207 | void AlbumsEngine::setFilterPattern( const QString &pattern ) | ||||
208 | { | ||||
209 | if( m_proxyModel->filterRegExp().pattern() == pattern ) | ||||
210 | return; | ||||
211 | | ||||
212 | m_proxyModel->setFilterRegExp( QRegExp(pattern, Qt::CaseInsensitive) ); | ||||
213 | emit filterPatternChanged(); | ||||
214 | } | ||||
215 | | ||||
216 | void AlbumsEngine::clear() | ||||
217 | { | ||||
218 | qDeleteAll( m_model->findItems( QLatin1String( "*" ), Qt::MatchWildcard ) ); | ||||
219 | m_model->clear(); | ||||
220 | } | ||||
221 | | ||||
222 | void AlbumsEngine::appendSelected( const QModelIndexList& indexes ) const | ||||
223 | { | ||||
224 | Meta::TrackList selected = getSelectedTracks( indexes ); | ||||
225 | The::playlistController()->insertOptioned( selected, Playlist::OnAppendToPlaylistAction ); | ||||
226 | } | ||||
227 | | ||||
228 | void AlbumsEngine::replaceWithSelected( const QModelIndexList& indexes ) const | ||||
229 | { | ||||
230 | Meta::TrackList selected = getSelectedTracks( indexes ); | ||||
231 | The::playlistController()->insertOptioned( selected, Playlist::OnReplacePlaylistAction ); | ||||
232 | } | ||||
233 | | ||||
234 | void AlbumsEngine::queueSelected( const QModelIndexList& indexes ) const | ||||
235 | { | ||||
236 | Meta::TrackList selected = getSelectedTracks( indexes ); | ||||
237 | The::playlistController()->insertOptioned( selected, Playlist::OnQueueToPlaylistAction ); | ||||
238 | } | ||||
239 | | ||||
240 | void AlbumsEngine::editSelected( const QModelIndexList& indexes ) const | ||||
241 | { | ||||
242 | Meta::TrackList selected = getSelectedTracks( indexes ); | ||||
243 | if( !selected.isEmpty() ) | ||||
244 | { | ||||
245 | TagDialog *dialog = new TagDialog( selected ); | ||||
246 | dialog->show(); | ||||
247 | } | ||||
248 | } | ||||
249 | | ||||
250 | void AlbumsEngine::showContextMenu( const QModelIndexList &indexes, const QModelIndex &mouseOverIndex ) const | ||||
251 | { | ||||
252 | if( indexes.isEmpty() || !mouseOverIndex.isValid() ) | ||||
253 | return; | ||||
254 | | ||||
255 | QMenu menu; | ||||
256 | QAction *appendAction = new QAction( QIcon::fromTheme( "media-track-add-amarok" ), i18n( "&Add to Playlist" ), &menu ); | ||||
257 | QAction *loadAction = new QAction( QIcon::fromTheme( "folder-open" ), i18nc( "Replace the currently loaded tracks with these", "&Replace Playlist" ), &menu ); | ||||
258 | QAction *queueAction = new QAction( QIcon::fromTheme( "media-track-queue-amarok" ), i18n( "&Queue" ), &menu ); | ||||
259 | QAction *editAction = new QAction( QIcon::fromTheme( "media-track-edit-amarok" ), i18n( "Edit Track Details" ), &menu ); | ||||
260 | | ||||
261 | menu.addAction( appendAction ); | ||||
262 | menu.addAction( loadAction ); | ||||
263 | menu.addAction( queueAction ); | ||||
264 | menu.addAction( editAction ); | ||||
265 | | ||||
266 | connect( appendAction, &QAction::triggered, this, [this, indexes] () { appendSelected( indexes ); } ); | ||||
267 | connect( loadAction, &QAction::triggered, this, [this, indexes] () { replaceWithSelected( indexes ); } ); | ||||
268 | connect( queueAction, &QAction::triggered, this, [this, indexes] () { queueSelected( indexes ); } ); | ||||
269 | connect( editAction, &QAction::triggered, this, [this, indexes] () { editSelected( indexes ); } ); | ||||
270 | | ||||
271 | QMenu menuCover( i18n( "Album" ), &menu ); | ||||
272 | const QStandardItem *item = m_model->itemFromIndex( m_proxyModel->mapToSource( mouseOverIndex ) ); | ||||
273 | if( item->type() == AlbumType ) | ||||
274 | { | ||||
275 | Meta::AlbumPtr album = static_cast<const AlbumItem*>( item )->album(); | ||||
276 | QScopedPointer<Capabilities::ActionsCapability> ac( album->create<Capabilities::ActionsCapability>() ); | ||||
277 | if( ac ) | ||||
278 | { | ||||
279 | QList<QAction *> actions = ac->actions(); | ||||
280 | if( !actions.isEmpty() ) | ||||
281 | { | ||||
282 | // ensure that the actions are cleaned up afterwards | ||||
283 | foreach( QAction *action, actions ) | ||||
284 | { | ||||
285 | if( !action->parent() ) | ||||
286 | action->setParent( &menuCover ); | ||||
287 | } | ||||
288 | | ||||
289 | menuCover.addActions( actions ); | ||||
290 | menuCover.setIcon( QIcon::fromTheme( "filename-album-amarok" ) ); | ||||
291 | menu.addMenu( &menuCover ); | ||||
292 | } | ||||
293 | } | ||||
294 | } | ||||
295 | menu.exec( QCursor::pos() ); | ||||
296 | } | ||||
297 | | ||||
298 | Meta::TrackList AlbumsEngine::getSelectedTracks( const QModelIndexList& indexes ) const | ||||
299 | { | ||||
300 | Meta::TrackList selected; | ||||
301 | | ||||
302 | for( const QModelIndex &index : indexes ) | ||||
303 | { | ||||
304 | if( index.isValid() ) | ||||
305 | { | ||||
306 | const QModelIndex &srcIndex = m_proxyModel->mapToSource( index ); | ||||
307 | const QStandardItem *item = m_model->itemFromIndex( srcIndex ); | ||||
308 | if( item->type() == AlbumType ) | ||||
309 | { | ||||
310 | selected << static_cast<const AlbumItem*>( item )->album()->tracks(); | ||||
311 | } | ||||
312 | else if( item->type() == TrackType ) | ||||
313 | { | ||||
314 | selected << static_cast<const TrackItem*>( item )->track(); | ||||
315 | } | ||||
316 | else if( m_model->hasChildren( srcIndex ) ) // disc type | ||||
317 | { | ||||
318 | for( int i = m_model->rowCount( srcIndex ) - 1; i >= 0; --i ) | ||||
319 | { | ||||
320 | const QStandardItem *trackItem = m_model->itemFromIndex( srcIndex.child(i, 0) ); | ||||
321 | selected << static_cast<const TrackItem*>( trackItem )->track(); | ||||
322 | } | ||||
323 | } | ||||
324 | } | ||||
325 | } | ||||
326 | | ||||
327 | return selected; | ||||
328 | } |