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 <kactioncollection.h>
Laurent Montel's avatar
Laurent Montel committed
21
#include <ktoggleaction.h>
22
#include <kacceleratormanager.h>
Michael Pyne's avatar
Michael Pyne committed
23 24 25
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
26

Michael Pyne's avatar
Michael Pyne committed
27 28
#include <QIcon>
#include <QAction>
29
#include <QEvent>
Laurent Montel's avatar
Laurent Montel committed
30
#include <QVBoxLayout>
31
#include <QLatin1String>
32
#include <QList>
33
#include <QStackedWidget>
34
#include <QSizePolicy>
35
#include <QKeySequence>
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"
Michael Pyne's avatar
Michael Pyne committed
47
#include "juk_debug.h"
48

49
////////////////////////////////////////////////////////////////////////////////
50
// public methods
51 52
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

    delete m_editor;

85 86
    delete m_lyricsWidget;

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

    delete m_searchWidget; // Take no chances here either.

93 94 95 96
    // 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.

97
    delete m_playlistBox;
98 99
}

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

105 106 107 108 109 110 111 112 113
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

114 115 116 117 118
void PlaylistSplitter::slotFocusCurrentPlaylist()
{
    Playlist *playlist = m_playlistBox->visiblePlaylist();

    if(playlist) {
119
        playlist->setFocus();
120
        playlist->clearSelection();
121

122
        // Select the top visible (and matching) item.
123

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

126 127
        if(!item)
            return;
128

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

132
        playlist->setSelectionMode(QTreeWidget::SingleSelection);
133

134
        playlist->setCurrentItem(item);
135

136
        playlist->setSelectionMode(QTreeWidget::ExtendedSelection);
137 138 139
    }
}

140
////////////////////////////////////////////////////////////////////////////////
141
// private members
142 143
////////////////////////////////////////////////////////////////////////////////

144 145 146 147 148
Playlist *PlaylistSplitter::visiblePlaylist() const
{
    return m_newVisible ? m_newVisible : m_playlistBox->visiblePlaylist();
}

149
void PlaylistSplitter::setupActions()
150
{
151
    KActionCollection* coll = ActionCollection::actions();
152
    KToggleAction *showSearch =
Michael Pyne's avatar
Michael Pyne committed
153
	 new KToggleAction(QIcon::fromTheme(QStringLiteral("edit-find")), i18n("Show &Search Bar"), this);
154 155
    coll->addAction("showSearch", showSearch);

Michael Pyne's avatar
Michael Pyne committed
156
    QAction *act = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear")), i18n("Edit Track Search"), this);
157
    coll->addAction("editTrackSearch", act);
158
    coll->setDefaultShortcut(act, Qt::Key_F6);
159
    connect(act, SIGNAL(triggered(bool)), SLOT(setFocus()));
160 161
}

162 163
void PlaylistSplitter::setupLayout()
{
164
    setOpaqueResize(false);
165

Michael Pyne's avatar
Michael Pyne committed
166 167 168 169
    // Disable the GUI until startup is complete (as indicated by PlaylistBox)

    setEnabled(false);

170 171
    // Create a splitter to go between the playlists and the editor.

172
    m_editorSplitter = new QSplitter(Qt::Vertical, this);
173
    m_editorSplitter->setObjectName(QLatin1String("editorSplitter"));
174

175 176 177 178 179 180
    // 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);

181
    // Create the playlist and the editor.
182

183
    QWidget *top = new QWidget(m_editorSplitter);
184
    QVBoxLayout *topLayout = new QVBoxLayout(top);
Sebastian Sauer's avatar
Sebastian Sauer committed
185 186
    topLayout->setMargin(0);
    topLayout->setSpacing(0);
187

188
    m_playlistStack = new QStackedWidget(top);
189
    m_playlistStack->setObjectName(QLatin1String("playlistStack"));
190
    m_playlistStack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
191
    m_playlistStack->hide(); // Will be shown after CollectionList filled.
192

193
    m_editor = new TagEditor(m_editorSplitter);
194
    m_editor->setObjectName(QLatin1String("TagEditor"));
195

196 197 198
    // Create the lyrics widget
    m_lyricsWidget = new LyricsWidget(this);
    insertWidget(2, m_lyricsWidget);
199

200
    // Create the PlaylistBox
201
    m_playlistBox = new PlaylistBox(m_player, this, m_playlistStack);
202
    m_playlistBox->setObjectName(QLatin1String("playlistBox"));
203

204 205
    connect(m_playlistBox->object(), SIGNAL(signalSelectedItemsChanged()),
            this, SLOT(slotPlaylistSelectionChanged()));
Laurent Montel's avatar
Laurent Montel committed
206 207
    connect(m_playlistBox, SIGNAL(signalPlaylistDestroyed(Playlist*)),
            m_editor, SLOT(slotPlaylistDestroyed(Playlist*)));
Michael Pyne's avatar
Michael Pyne committed
208
    connect(m_playlistBox, SIGNAL(startupComplete()), SLOT(slotEnable()));
209 210
    connect(m_playlistBox, &QTreeWidget::currentItemChanged,
            this,          &PlaylistSplitter::slotCurrentPlaylistChanged);
211 212 213 214
    connect(m_playlistBox, SIGNAL(startFilePlayback(FileHandle)),
            m_player, SLOT(play(FileHandle)));

    m_player->setPlaylistInterface(m_playlistBox);
215

Michael Pyne's avatar
Michael Pyne committed
216 217 218
    // Let interested parties know we're ready
    connect(m_playlistBox, SIGNAL(startupComplete()), SIGNAL(guiReady()));

219
    insertWidget(0, m_playlistBox);
220

221
    m_nowPlaying = new NowPlaying(top, m_playlistBox);
222 223
    connect(m_player, SIGNAL(signalItemChanged(FileHandle)),
            m_nowPlaying, SLOT(slotUpdate(FileHandle)));
224 225
    connect(m_player, SIGNAL(signalItemChanged(FileHandle)),
            m_lyricsWidget, SLOT(playing(FileHandle)));
226

227
    // Create the search widget -- this must be done after the CollectionList is created.
228

229
    m_searchWidget = new SearchWidget(top);
230 231 232 233

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

234 235
    connect(m_searchWidget, SIGNAL(signalQueryChanged()),
            this, SLOT(slotShowSearchResults()));
236 237
    connect(m_searchWidget, SIGNAL(signalDownPressed()),
            this, SLOT(slotFocusCurrentPlaylist()));
238 239
    connect(m_searchWidget, SIGNAL(signalShown(bool)),
            m_playlistBox->object(), SLOT(slotSetSearchEnabled(bool)));
Michael Pyne's avatar
Michael Pyne committed
240 241
    connect(m_searchWidget, SIGNAL(returnPressed()),
            m_playlistBox->object(), SLOT(slotPlayFirst()));
242
    connect(ActionCollection::action<KToggleAction>("showSearch"), SIGNAL(toggled(bool)),
243
            m_searchWidget, SLOT(setEnabled(bool)));
244 245
    connect(m_playlistBox, &PlaylistBox::signalMoveFocusAway,
            m_searchWidget, &SearchWidget::setFocus);
246

247
    topLayout->addWidget(m_nowPlaying);
248
    topLayout->addWidget(m_searchWidget);
Michael Pyne's avatar
Michael Pyne committed
249
    topLayout->insertStretch(-1); // Force search bar to top while playlistStack hides
250
    topLayout->addWidget(m_playlistStack, 1);
251

252 253 254 255 256
    // 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)));

257
    // Show the collection on startup.
258
    m_playlistBox->setCurrentItem(m_playlistBox->topLevelItem(0));
259 260 261 262
}

void PlaylistSplitter::readConfig()
{
263
    KConfigGroup config(KSharedConfig::openConfig(), "Splitter");
264

265
    QList<int> splitterSizes = config.readEntry("PlaylistSplitterSizes",QList<int>());
266 267 268
    if(splitterSizes.isEmpty()) {
        splitterSizes.append(100);
        splitterSizes.append(640);
269
    }
270
    setSizes(splitterSizes);
271

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

    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);
286 287
}

288
void PlaylistSplitter::saveConfig()
289
{
290
    KConfigGroup config(KSharedConfig::openConfig(), "Splitter");
291
    config.writeEntry("PlaylistSplitterSizes", sizes());
292
    config.writeEntry("ShowSearch", ActionCollection::action<KToggleAction>("showSearch")->isChecked());
293
    config.writeEntry("EditorSplitterSizes", m_editorSplitter->sizes());
294 295
}

296
void PlaylistSplitter::slotShowSearchResults()
297 298 299
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());
300 301
    PlaylistSearch search = m_searchWidget->search(playlists);
    visiblePlaylist()->setSearch(search);
302 303
}

304
void PlaylistSplitter::slotPlaylistSelectionChanged()
305
{
306
    m_editor->slotSetItems(visiblePlaylist()->selectedItems());
307 308
}

309
void PlaylistSplitter::slotPlaylistChanged(int i)
310
{
311
    Playlist *p = qobject_cast<Playlist *>(m_playlistStack->widget(i));
312

313
    if(!p)
314
        return;
315 316 317 318

    m_newVisible = p;
    m_searchWidget->setSearch(p->search());
    m_newVisible = 0;
319 320
}

321 322 323 324 325 326
void PlaylistSplitter::slotCurrentPlaylistChanged(QTreeWidgetItem *item)
{
    auto pItem = static_cast<PlaylistBox::Item*>(item);
    emit currentPlaylistChanged(*(pItem->playlist()));
}

Michael Pyne's avatar
Michael Pyne committed
327 328 329
void PlaylistSplitter::slotEnable()
{
    setEnabled(true); // Ready to go.
330
    m_playlistStack->show();
331 332

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

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