playlistsplitter.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
 * Copyright (C) 2009 Michael Pyne <mpyne@kde.org>
 *
 * 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.
 *
 * This program 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
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18 19
#include "playlistsplitter.h"

20
#include <kicon.h>
21
#include <kaction.h>
22
#include <kglobal.h>
23
#include <kactioncollection.h>
24
#include <kdebug.h>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
25
#include <ktoggleaction.h>
26
#include <kconfiggroup.h>
27
#include <kacceleratormanager.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();
128
        playlist->clearSelection();
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

140
        playlist->setSelectionMode(QTreeWidget::SingleSelection);
141

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

145
        playlist->setSelectionMode(QTreeWidget::ExtendedSelection);
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 240 241

    // auto-shortcuts don't seem to work and aren't needed anyway.
    KAcceleratorManager::setNoAccel(m_searchWidget);

242 243
    connect(m_searchWidget, SIGNAL(signalQueryChanged()),
            this, SLOT(slotShowSearchResults()));
244 245
    connect(m_searchWidget, SIGNAL(signalDownPressed()),
            this, SLOT(slotFocusCurrentPlaylist()));
246 247 248 249
    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
250 251
    connect(m_searchWidget, SIGNAL(returnPressed()),
            m_playlistBox->object(), SLOT(slotPlayFirst()));
252
    connect(ActionCollection::action<KToggleAction>("showSearch"), SIGNAL(toggled(bool)),
253
            m_searchWidget, SLOT(setEnabled(bool)));
254

255
    topLayout->addWidget(m_nowPlaying);
256
    topLayout->addWidget(m_searchWidget);
Michael Pyne's avatar
Michael Pyne committed
257
    topLayout->insertStretch(-1); // Force search bar to top while playlistStack hides
258
    topLayout->addWidget(m_playlistStack, 1);
259

260 261 262 263 264
    // 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)));

265
    // Show the collection on startup.
266
    m_playlistBox->setCurrentItem(m_playlistBox->topLevelItem(0));
267 268 269 270
}

void PlaylistSplitter::readConfig()
{
271
    KConfigGroup config(KGlobal::config(), "Splitter");
272

273
    QList<int> splitterSizes = config.readEntry("PlaylistSplitterSizes",QList<int>());
274 275 276
    if(splitterSizes.isEmpty()) {
        splitterSizes.append(100);
        splitterSizes.append(640);
277
    }
278
    setSizes(splitterSizes);
279

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

    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);
294 295
}

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

304
void PlaylistSplitter::slotShowSearchResults()
305 306 307
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());
308 309
    PlaylistSearch search = m_searchWidget->search(playlists);
    visiblePlaylist()->setSearch(search);
310 311
}

312
void PlaylistSplitter::slotPlaylistSelectionChanged()
313
{
314
    m_editor->slotSetItems(visiblePlaylist()->selectedItems());
315 316
}

317
void PlaylistSplitter::slotPlaylistChanged(int i)
318
{
319
    Playlist *p = qobject_cast<Playlist *>(m_playlistStack->widget(i));
320

321
    if(!p)
322
        return;
323 324 325 326

    m_newVisible = p;
    m_searchWidget->setSearch(p->search());
    m_newVisible = 0;
327 328
}

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

    (void) new Mpris2(this);
Michael Pyne's avatar
Michael Pyne committed
338 339
}

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