playlistsplitter.cpp 11.1 KB
Newer Older
1 2
/***************************************************************************
    begin                : Fri Sep 13 2002
3
    copyright            : (C) 2002 - 2004 by Scott Wheeler
4
    email                : wheeler@kde.org
Michael Pyne's avatar
Michael Pyne committed
5 6 7

    copyright            : (C) 2009 by Michael Pyne
    email                : michael.pyne@kdemail.net
8 9 10 11 12 13 14 15 16 17 18
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

19 20
#include "playlistsplitter.h"

21
#include <kicon.h>
22
#include <kaction.h>
23
#include <kglobal.h>
24
#include <kactioncollection.h>
25
#include <kdebug.h>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
26
#include <ktoggleaction.h>
27
#include <kconfiggroup.h>
28

29
#include <QEvent>
Laurent Montel's avatar
Laurent Montel committed
30
#include <QVBoxLayout>
31
#include <QLatin1String>
32
#include <QList>
33
#include <QTime>
34
#include <QStackedWidget>
35
#include <QSizePolicy>
36

37
#include "searchwidget.h"
38
#include "playlistsearch.h"
39
#include "actioncollection.h"
40 41
#include "tageditor.h"
#include "collectionlist.h"
42
#include "playermanager.h"
43
#include "nowplaying.h"
44
#include "playlistbox.h"
45
#include "lyricswidget.h"
46
#include "mpris2/mpris2.h"
47

48
////////////////////////////////////////////////////////////////////////////////
49
// public methods
50 51
////////////////////////////////////////////////////////////////////////////////

52
PlaylistSplitter::PlaylistSplitter(PlayerManager *player, QWidget *parent) :
53
    QSplitter(Qt::Horizontal, parent),
54
    m_newVisible(0),
55 56 57
    m_playlistBox(0),
    m_searchWidget(0),
    m_playlistStack(0),
58
    m_editor(0),
59
    m_nowPlaying(0),
60
    m_player(player),
Laurent Montel's avatar
Laurent Montel committed
61 62 63
    m_lyricsWidget(0),
    m_editorSplitter(0)

64
{
65
    setObjectName(QLatin1String("playlistSplitter"));
66

67
    setupActions();
68 69
    setupLayout();
    readConfig();
70 71

    m_editor->slotUpdateCollection();
72
    m_editor->setupObservers();
73
}
74

75
PlaylistSplitter::~PlaylistSplitter()
76
{
77
    saveConfig();
78

79 80 81 82 83
    // TagEditor needs to write its configuration out while it's still valid,
    // destroy it now.

    delete m_editor;

84 85
    delete m_lyricsWidget;

86 87 88 89 90 91
    // NowPlaying depends on the PlaylistCollection, so kill it now.
    delete m_nowPlaying;
    m_nowPlaying = 0;

    delete m_searchWidget; // Take no chances here either.

92 93 94 95
    // Since we want to ensure that the shutdown process for the PlaylistCollection
    // (a base class for PlaylistBox) has a chance to write the playlists to disk
    // before they are deleted we're explicitly deleting the PlaylistBox here.

96
    delete m_playlistBox;
97 98
}

99 100 101 102 103
PlaylistInterface *PlaylistSplitter::playlist() const
{
    return m_playlistBox;
}

104 105 106 107 108 109 110 111 112
bool PlaylistSplitter::eventFilter(QObject *, QEvent *event)
{
    if(event->type() == FocusUpEvent::id) {
        m_searchWidget->setFocus();
        return true;
    }
    return false;
}

113 114 115 116 117 118 119 120 121
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void PlaylistSplitter::setFocus()
{
    m_searchWidget->setFocus();
}

122 123 124 125 126
void PlaylistSplitter::slotFocusCurrentPlaylist()
{
    Playlist *playlist = m_playlistBox->visiblePlaylist();

    if(playlist) {
127
        playlist->setFocus();
Laurent Montel's avatar
Laurent Montel committed
128
        playlist->K3ListView::selectAll(false);
129

130
        // Select the top visible (and matching) item.
131

132
        PlaylistItem *item = static_cast<PlaylistItem *>(playlist->itemAt(QPoint(0, 0)));
133

134 135
        if(!item)
            return;
136

137 138
        // A little bit of a hack to make QListView repaint things properly.  Switch
        // to single selection mode, set the selection and then switch back.
139

Laurent Montel's avatar
Laurent Montel committed
140
        playlist->setSelectionMode(Q3ListView::Single);
141

142 143
        playlist->markItemSelected(item, true);
        playlist->setCurrentItem(item);
144

Laurent Montel's avatar
Laurent Montel committed
145
        playlist->setSelectionMode(Q3ListView::Extended);
146 147 148
    }
}

149
////////////////////////////////////////////////////////////////////////////////
150
// private members
151 152
////////////////////////////////////////////////////////////////////////////////

153 154 155 156 157
Playlist *PlaylistSplitter::visiblePlaylist() const
{
    return m_newVisible ? m_newVisible : m_playlistBox->visiblePlaylist();
}

158
void PlaylistSplitter::setupActions()
159
{
160
    KActionCollection* coll = ActionCollection::actions();
161
    KToggleAction *showSearch =
Pino Toscano's avatar
Pino Toscano committed
162
	 new KToggleAction(KIcon("edit-find"), i18n("Show &Search Bar"), this);
163 164
    coll->addAction("showSearch", showSearch);

Laurent Montel's avatar
Laurent Montel committed
165
    KAction *act = new KAction(KIcon("edit-clear"), i18n("Edit Track Search"), this);
166
    coll->addAction("editTrackSearch", act);
167 168
    act->setShortcut(Qt::Key_F6);
    connect(act, SIGNAL(triggered(bool)), SLOT(setFocus()));
169 170
}

171 172
void PlaylistSplitter::setupLayout()
{
173
    setOpaqueResize(false);
174

Michael Pyne's avatar
Michael Pyne committed
175 176 177 178
    // Disable the GUI until startup is complete (as indicated by PlaylistBox)

    setEnabled(false);

179 180
    // Create a splitter to go between the playlists and the editor.

181 182
    m_editorSplitter = new QSplitter(Qt::Vertical, this);
    m_editorSplitter->setObjectName( QLatin1String("editorSplitter" ));
183

184 185 186 187 188 189
    // Make sure none of the optional widgets are collapsible, this causes the
    // widget to be essentially invisible but logically shown.

    this->setChildrenCollapsible(false);
    m_editorSplitter->setChildrenCollapsible(false);

190
    // Create the playlist and the editor.
191

192
    QWidget *top = new QWidget(m_editorSplitter);
193
    QVBoxLayout *topLayout = new QVBoxLayout(top);
Sebastian Sauer's avatar
Sebastian Sauer committed
194 195
    topLayout->setMargin(0);
    topLayout->setSpacing(0);
196

197
    m_playlistStack = new QStackedWidget(top);
Laurent Montel's avatar
Laurent Montel committed
198
    m_playlistStack->setObjectName( QLatin1String("playlistStack" ));
199
    m_playlistStack->installEventFilter(this);
200
    m_playlistStack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
201
    m_playlistStack->hide(); // Will be shown after CollectionList filled.
202

203
    m_editor = new TagEditor(m_editorSplitter);
Laurent Montel's avatar
Laurent Montel committed
204
    m_editor->setObjectName( QLatin1String("TagEditor" ));
205

206 207 208
    // Create the lyrics widget
    m_lyricsWidget = new LyricsWidget(this);
    insertWidget(2, m_lyricsWidget);
209

210
    // Create the PlaylistBox
211
    m_playlistBox = new PlaylistBox(m_player, this, m_playlistStack);
Laurent Montel's avatar
Laurent Montel committed
212
    m_playlistBox->setObjectName( QLatin1String( "playlistBox" ) );
213

214 215
    connect(m_playlistBox->object(), SIGNAL(signalSelectedItemsChanged()),
            this, SLOT(slotPlaylistSelectionChanged()));
Laurent Montel's avatar
Laurent Montel committed
216 217
    connect(m_playlistBox, SIGNAL(signalPlaylistDestroyed(Playlist*)),
            m_editor, SLOT(slotPlaylistDestroyed(Playlist*)));
Michael Pyne's avatar
Michael Pyne committed
218
    connect(m_playlistBox, SIGNAL(startupComplete()), SLOT(slotEnable()));
219 220 221 222
    connect(m_playlistBox, SIGNAL(startFilePlayback(FileHandle)),
            m_player, SLOT(play(FileHandle)));

    m_player->setPlaylistInterface(m_playlistBox);
223

Michael Pyne's avatar
Michael Pyne committed
224 225 226
    // Let interested parties know we're ready
    connect(m_playlistBox, SIGNAL(startupComplete()), SIGNAL(guiReady()));

227
    insertWidget(0, m_playlistBox);
228

229
    m_nowPlaying = new NowPlaying(top, m_playlistBox);
230 231
    connect(m_player, SIGNAL(signalItemChanged(FileHandle)),
            m_nowPlaying, SLOT(slotUpdate(FileHandle)));
232 233
    connect(m_player, SIGNAL(signalItemChanged(FileHandle)),
            m_lyricsWidget, SLOT(playing(FileHandle)));
234

235
    // Create the search widget -- this must be done after the CollectionList is created.
236

237
    m_searchWidget = new SearchWidget(top);
238 239
    connect(m_searchWidget, SIGNAL(signalQueryChanged()),
            this, SLOT(slotShowSearchResults()));
240 241
    connect(m_searchWidget, SIGNAL(signalDownPressed()),
            this, SLOT(slotFocusCurrentPlaylist()));
242 243 244 245
    connect(m_searchWidget, SIGNAL(signalAdvancedSearchClicked()),
            m_playlistBox->object(), SLOT(slotCreateSearchPlaylist()));
    connect(m_searchWidget, SIGNAL(signalShown(bool)),
            m_playlistBox->object(), SLOT(slotSetSearchEnabled(bool)));
Michael Pyne's avatar
Michael Pyne committed
246 247
    connect(m_searchWidget, SIGNAL(returnPressed()),
            m_playlistBox->object(), SLOT(slotPlayFirst()));
248
    connect(ActionCollection::action<KToggleAction>("showSearch"), SIGNAL(toggled(bool)),
249
            m_searchWidget, SLOT(setEnabled(bool)));
250

251
    topLayout->addWidget(m_nowPlaying);
252
    topLayout->addWidget(m_searchWidget);
Michael Pyne's avatar
Michael Pyne committed
253
    topLayout->insertStretch(-1); // Force search bar to top while playlistStack hides
254
    topLayout->addWidget(m_playlistStack, 1);
255

256 257 258 259 260
    // Now that GUI setup is complete, add some auto-update signals.
    connect(CollectionList::instance(), SIGNAL(signalCollectionChanged()),
            m_editor, SLOT(slotUpdateCollection()));
    connect(m_playlistStack, SIGNAL(currentChanged(int)), this, SLOT(slotPlaylistChanged(int)));

261
    // Show the collection on startup.
262
    m_playlistBox->setSelected(0, true);
263 264 265 266
}

void PlaylistSplitter::readConfig()
{
267
    KConfigGroup config(KGlobal::config(), "Splitter");
268

269
    QList<int> splitterSizes = config.readEntry("PlaylistSplitterSizes",QList<int>());
270 271 272
    if(splitterSizes.isEmpty()) {
        splitterSizes.append(100);
        splitterSizes.append(640);
273
    }
274
    setSizes(splitterSizes);
275

276
    bool showSearch = config.readEntry("ShowSearch", true);
277
    ActionCollection::action<KToggleAction>("showSearch")->setChecked(showSearch);
278
    m_searchWidget->setHidden(!showSearch);
279 280 281 282 283 284 285 286 287 288 289

    splitterSizes = config.readEntry("EditorSplitterSizes",QList<int>());
    if(splitterSizes.isEmpty()) {
        // If no sizes were saved, use default sizes for the playlist and the
        // editor, respectively. The values are just hints for the actual size,
        // m_editorSplitter will distribute the space according to their
        // relative weight.
        splitterSizes.append(300);
        splitterSizes.append(200);
    }
    m_editorSplitter->setSizes(splitterSizes);
290 291
}

292
void PlaylistSplitter::saveConfig()
293
{
294 295
    KConfigGroup config(KGlobal::config(), "Splitter");
    config.writeEntry("PlaylistSplitterSizes", sizes());
296
    config.writeEntry("ShowSearch", ActionCollection::action<KToggleAction>("showSearch")->isChecked());
297
    config.writeEntry("EditorSplitterSizes", m_editorSplitter->sizes());
298 299
}

300
void PlaylistSplitter::slotShowSearchResults()
301 302 303
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());
304 305
    PlaylistSearch search = m_searchWidget->search(playlists);
    visiblePlaylist()->setSearch(search);
306 307
}

308
void PlaylistSplitter::slotPlaylistSelectionChanged()
309
{
310
    m_editor->slotSetItems(visiblePlaylist()->selectedItems());
311 312
}

313
void PlaylistSplitter::slotPlaylistChanged(int i)
314
{
315
    Playlist *p = qobject_cast<Playlist *>(m_playlistStack->widget(i));
316

317
    if(!p)
318
        return;
319 320 321 322

    m_newVisible = p;
    m_searchWidget->setSearch(p->search());
    m_newVisible = 0;
323 324
}

Michael Pyne's avatar
Michael Pyne committed
325 326
void PlaylistSplitter::slotEnable()
{
327 328
    kDebug() << "Enabling GUI";
    QTime stopwatch; stopwatch.start();
Michael Pyne's avatar
Michael Pyne committed
329
    setEnabled(true); // Ready to go.
330
    m_playlistStack->show();
331
    kDebug() << "Finished enabling GUI, took" << stopwatch.elapsed() << "ms";
332 333

    (void) new Mpris2(this);
Michael Pyne's avatar
Michael Pyne committed
334 335
}

336
#include "playlistsplitter.moc"
337 338

// vim: set et sw=4 tw=0 sta: