playlistsplitter.cpp 7.53 KB
Newer Older
1 2
/***************************************************************************
    begin                : Fri Sep 13 2002
3
    copyright            : (C) 2002 - 2004 by Scott Wheeler
4
    email                : wheeler@kde.org
5 6 7 8 9 10 11 12 13 14 15
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

16
#include <kaction.h>
17 18
#include <kdebug.h>

19
#include <qlayout.h>
20
#include <qevent.h>
21

22
#include "playlistsplitter.h"
23
#include "searchwidget.h"
24
#include "playlistsearch.h"
25
#include "actioncollection.h"
26 27
#include "tageditor.h"
#include "collectionlist.h"
28
#include "playermanager.h"
29
#include "nowplaying.h"
30 31

using namespace ActionCollection;
32

33
////////////////////////////////////////////////////////////////////////////////
34
// public methods
35 36
////////////////////////////////////////////////////////////////////////////////

37
PlaylistSplitter::PlaylistSplitter(QWidget *parent, const char *name) :
38
    QSplitter(Qt::Horizontal, parent, name),
39
    m_newVisible(0),
40 41 42 43
    m_playlistBox(0),
    m_searchWidget(0),
    m_playlistStack(0),
    m_editor(0)
44
{
45
    setupActions();
46 47
    setupLayout();
    readConfig();
48 49

    m_editor->slotUpdateCollection();
50
    m_editor->setupObservers();
51
}
52

53
PlaylistSplitter::~PlaylistSplitter()
54
{
55
    saveConfig();
56 57 58 59 60

    // 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.

61
    delete m_playlistBox;
62 63
}

64 65 66 67 68 69 70 71 72
bool PlaylistSplitter::eventFilter(QObject *, QEvent *event)
{
    if(event->type() == FocusUpEvent::id) {
        m_searchWidget->setFocus();
        return true;
    }
    return false;
}

73 74 75 76 77 78 79 80 81
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

82 83 84 85 86
void PlaylistSplitter::slotFocusCurrentPlaylist()
{
    Playlist *playlist = m_playlistBox->visiblePlaylist();

    if(playlist) {
87 88
        playlist->setFocus();
        playlist->KListView::selectAll(false);
89

90
        // Select the top visible (and matching) item.
91

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

94 95
        if(!item)
            return;
96

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

100
        playlist->setSelectionMode(QListView::Single);
101

102 103
        playlist->markItemSelected(item, true);
        playlist->setCurrentItem(item);
104

105
        playlist->setSelectionMode(QListView::Extended);
106 107 108
    }
}

109
////////////////////////////////////////////////////////////////////////////////
110
// private members
111 112
////////////////////////////////////////////////////////////////////////////////

113 114 115 116 117
Playlist *PlaylistSplitter::visiblePlaylist() const
{
    return m_newVisible ? m_newVisible : m_playlistBox->visiblePlaylist();
}

118
void PlaylistSplitter::setupActions()
119
{
120 121 122
    KToggleAction *showSearch =
        new KToggleAction(i18n("Show &Search Bar"), "filefind", 0, actions(), "showSearch");
    showSearch->setCheckedState(i18n("Hide &Search Bar"));
123 124

    new KAction(i18n("Edit Track Search"), "edit_clear", "F6", this, SLOT(setFocus()), actions(), "editTrackSearch");
125 126
}

127 128
void PlaylistSplitter::setupLayout()
{
129
    setOpaqueResize(false);
130

131 132 133 134 135
    // Create a splitter to go between the playlists and the editor.

    QSplitter *editorSplitter = new QSplitter(Qt::Vertical, this, "editorSplitter");

    // Create the playlist and the editor.
136

137 138 139 140
    QWidget *top = new QWidget(editorSplitter);
    QVBoxLayout *topLayout = new QVBoxLayout(top);

    m_playlistStack = new QWidgetStack(top, "playlistStack");
141
    m_playlistStack->installEventFilter(this);
142

143
    connect(m_playlistStack, SIGNAL(aboutToShow(QWidget *)), this, SLOT(slotPlaylistChanged(QWidget *)));
144

145
    m_editor = new TagEditor(editorSplitter, "tagEditor");
146 147 148

    // Make the editor as small as possible (or at least as small as recommended)

149
    editorSplitter->setResizeMode(m_editor, QSplitter::FollowSizeHint);
150

151
    // Create the PlaylistBox
152

153
    m_playlistBox = new PlaylistBox(this, m_playlistStack, "playlistBox");
154

155 156
    connect(m_playlistBox->object(), SIGNAL(signalSelectedItemsChanged()),
            this, SLOT(slotPlaylistSelectionChanged()));
157 158
    connect(m_playlistBox, SIGNAL(signalPlaylistDestroyed(Playlist *)),
            m_editor, SLOT(slotPlaylistDestroyed(Playlist *)));
159

160
    moveToFirst(m_playlistBox);
161 162 163

    connect(CollectionList::instance(), SIGNAL(signalCollectionChanged()),
            m_editor, SLOT(slotUpdateCollection()));
164

165
    NowPlaying *nowPlaying = new NowPlaying(top, m_playlistBox);
166

167
    // Create the search widget -- this must be done after the CollectionList is created.
168 169

    m_searchWidget = new SearchWidget(top, "searchWidget");
170 171
    connect(m_searchWidget, SIGNAL(signalQueryChanged()),
            this, SLOT(slotShowSearchResults()));
172 173
    connect(m_searchWidget, SIGNAL(signalDownPressed()),
            this, SLOT(slotFocusCurrentPlaylist()));
174 175 176 177 178
    connect(m_searchWidget, SIGNAL(signalAdvancedSearchClicked()),
            m_playlistBox->object(), SLOT(slotCreateSearchPlaylist()));
    connect(m_searchWidget, SIGNAL(signalShown(bool)),
            m_playlistBox->object(), SLOT(slotSetSearchEnabled(bool)));
    connect(action<KToggleAction>("showSearch"), SIGNAL(toggled(bool)),
179
            m_searchWidget, SLOT(setEnabled(bool)));
180

181
    topLayout->addWidget(nowPlaying);
182 183 184
    topLayout->addWidget(m_searchWidget);
    topLayout->addWidget(m_playlistStack);

185
    // Show the collection on startup.
186
    m_playlistBox->setSelected(0, true);
187 188 189 190
}

void PlaylistSplitter::readConfig()
{
191
    KConfigGroup config(KGlobal::config(), "Splitter");
192

193 194 195 196
    QValueList<int> splitterSizes = config.readIntListEntry("PlaylistSplitterSizes");
    if(splitterSizes.isEmpty()) {
        splitterSizes.append(100);
        splitterSizes.append(640);
197
    }
198
    setSizes(splitterSizes);
199

200 201 202
    bool showSearch = config.readBoolEntry("ShowSearch", true);
    action<KToggleAction>("showSearch")->setChecked(showSearch);
    m_searchWidget->setShown(showSearch);
203 204
}

205
void PlaylistSplitter::saveConfig()
206
{
207 208 209
    KConfigGroup config(KGlobal::config(), "Splitter");
    config.writeEntry("PlaylistSplitterSizes", sizes());
    config.writeEntry("ShowSearch", action<KToggleAction>("showSearch")->isChecked());
210 211
}

212
void PlaylistSplitter::slotShowSearchResults()
213 214 215
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());
216 217
    PlaylistSearch search = m_searchWidget->search(playlists);
    visiblePlaylist()->setSearch(search);
218 219
}

220
void PlaylistSplitter::slotPlaylistSelectionChanged()
221
{
222
    m_editor->slotSetItems(visiblePlaylist()->selectedItems());
223 224
}

225
void PlaylistSplitter::slotPlaylistChanged(QWidget *w)
226
{
227 228
    Playlist *p = dynamic_cast<Playlist *>(w);

229
    if(!p)
230
        return;
231 232 233 234

    m_newVisible = p;
    m_searchWidget->setSearch(p->search());
    m_newVisible = 0;
235 236
}

237
#include "playlistsplitter.moc"