playlistsplitter.cpp 7.94 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 <kicon.h>
17
#include <kaction.h>
18
#include <kglobal.h>
19
#include <kdebug.h>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
20
#include <ktoggleaction.h>
21

22
#include <QLayout>
23
#include <qevent.h>
24

Laurent Montel's avatar
Laurent Montel committed
25
#include <QVBoxLayout>
26
#include <QList>
27

28
#include "playlistsplitter.h"
29
#include "searchwidget.h"
30
#include "playlistsearch.h"
31
#include "actioncollection.h"
32 33
#include "tageditor.h"
#include "collectionlist.h"
34
#include "playermanager.h"
35
#include "nowplaying.h"
36

37
////////////////////////////////////////////////////////////////////////////////
38
// public methods
39 40
////////////////////////////////////////////////////////////////////////////////

41
PlaylistSplitter::PlaylistSplitter(QWidget *parent, const char *name) :
42
    QSplitter(Qt::Horizontal, parent),
43
    m_newVisible(0),
44 45 46 47
    m_playlistBox(0),
    m_searchWidget(0),
    m_playlistStack(0),
    m_editor(0)
48
{
49 50
    setObjectName(name);

51
    setupActions();
52 53
    setupLayout();
    readConfig();
54 55

    m_editor->slotUpdateCollection();
56
    m_editor->setupObservers();
57
}
58

59
PlaylistSplitter::~PlaylistSplitter()
60
{
61
    saveConfig();
62 63 64 65 66

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

67
    delete m_playlistBox;
68 69
}

70 71 72 73 74 75 76 77 78
bool PlaylistSplitter::eventFilter(QObject *, QEvent *event)
{
    if(event->type() == FocusUpEvent::id) {
        m_searchWidget->setFocus();
        return true;
    }
    return false;
}

79 80 81 82 83 84 85 86 87
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

88 89 90 91 92
void PlaylistSplitter::slotFocusCurrentPlaylist()
{
    Playlist *playlist = m_playlistBox->visiblePlaylist();

    if(playlist) {
93
        playlist->setFocus();
Laurent Montel's avatar
Laurent Montel committed
94
        playlist->K3ListView::selectAll(false);
95

96
        // Select the top visible (and matching) item.
97

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

100 101
        if(!item)
            return;
102

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

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

108 109
        playlist->markItemSelected(item, true);
        playlist->setCurrentItem(item);
110

Laurent Montel's avatar
Laurent Montel committed
111
        playlist->setSelectionMode(Q3ListView::Extended);
112 113 114
    }
}

115
////////////////////////////////////////////////////////////////////////////////
116
// private members
117 118
////////////////////////////////////////////////////////////////////////////////

119 120 121 122 123
Playlist *PlaylistSplitter::visiblePlaylist() const
{
    return m_newVisible ? m_newVisible : m_playlistBox->visiblePlaylist();
}

124
void PlaylistSplitter::setupActions()
125
{
126
    KToggleAction *showSearch =
127
	 new KToggleAction(KIcon("filefind"), i18n("Show &Search Bar"), ActionCollection::actions(), "showSearch");
Laurent Montel's avatar
Laurent Montel committed
128
    showSearch->setCheckedState(KGuiItem(i18n("Hide &Search Bar")));
129

130 131 132
    KAction *act = new KAction(KIcon("edit_clear"), i18n("Edit Track Search"), ActionCollection::actions(), "editTrackSearch");
    act->setShortcut(Qt::Key_F6);
    connect(act, SIGNAL(triggered(bool)), SLOT(setFocus()));
133 134
}

135 136
void PlaylistSplitter::setupLayout()
{
137
    setOpaqueResize(false);
138

139 140
    // Create a splitter to go between the playlists and the editor.

141 142
    QSplitter *editorSplitter = new QSplitter(Qt::Vertical, this);
    editorSplitter->setObjectName("editorSplitter");
143 144

    // Create the playlist and the editor.
145

146 147 148
    QWidget *top = new QWidget(editorSplitter);
    QVBoxLayout *topLayout = new QVBoxLayout(top);

Laurent Montel's avatar
Laurent Montel committed
149
    m_playlistStack = new Q3WidgetStack(top, "playlistStack");
150
    m_playlistStack->installEventFilter(this);
151

152
    connect(m_playlistStack, SIGNAL(aboutToShow(QWidget *)), this, SLOT(slotPlaylistChanged(QWidget *)));
153

Dirk Mueller's avatar
Dirk Mueller committed
154 155
    m_editor = new TagEditor(editorSplitter);
    m_editor->setObjectName("TagEditor");
156 157 158

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

159
    editorSplitter->setStretchFactor(editorSplitter->indexOf(m_editor), 2);
160

161
    // Create the PlaylistBox
162

163 164
    m_playlistBox = new PlaylistBox(this, m_playlistStack);
    m_playlistStack->setObjectName( "playlistBox" );
165

166 167
    connect(m_playlistBox->object(), SIGNAL(signalSelectedItemsChanged()),
            this, SLOT(slotPlaylistSelectionChanged()));
168 169
    connect(m_playlistBox, SIGNAL(signalPlaylistDestroyed(Playlist *)),
            m_editor, SLOT(slotPlaylistDestroyed(Playlist *)));
170

171
    insertWidget(0, m_playlistBox);
172 173 174

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

176
    NowPlaying *nowPlaying = new NowPlaying(top, m_playlistBox);
177

178
    // Create the search widget -- this must be done after the CollectionList is created.
179 180

    m_searchWidget = new SearchWidget(top, "searchWidget");
181 182
    connect(m_searchWidget, SIGNAL(signalQueryChanged()),
            this, SLOT(slotShowSearchResults()));
183 184
    connect(m_searchWidget, SIGNAL(signalDownPressed()),
            this, SLOT(slotFocusCurrentPlaylist()));
185 186 187 188
    connect(m_searchWidget, SIGNAL(signalAdvancedSearchClicked()),
            m_playlistBox->object(), SLOT(slotCreateSearchPlaylist()));
    connect(m_searchWidget, SIGNAL(signalShown(bool)),
            m_playlistBox->object(), SLOT(slotSetSearchEnabled(bool)));
189
    connect(ActionCollection::action<KToggleAction>("showSearch"), SIGNAL(toggled(bool)),
190
            m_searchWidget, SLOT(setEnabled(bool)));
191

192
    topLayout->addWidget(nowPlaying);
193 194 195
    topLayout->addWidget(m_searchWidget);
    topLayout->addWidget(m_playlistStack);

196
    // Show the collection on startup.
197
    m_playlistBox->setSelected(0, true);
198 199 200 201
}

void PlaylistSplitter::readConfig()
{
202
    KConfigGroup config(KGlobal::config(), "Splitter");
203

204
    QList<int> splitterSizes = config.readEntry("PlaylistSplitterSizes",QList<int>());
205 206 207
    if(splitterSizes.isEmpty()) {
        splitterSizes.append(100);
        splitterSizes.append(640);
208
    }
209
    setSizes(splitterSizes);
210

211
    bool showSearch = config.readEntry("ShowSearch", true);
212
    ActionCollection::action<KToggleAction>("showSearch")->setChecked(showSearch);
213
    m_searchWidget->setHidden(!showSearch);
214 215
}

216
void PlaylistSplitter::saveConfig()
217
{
218 219
    KConfigGroup config(KGlobal::config(), "Splitter");
    config.writeEntry("PlaylistSplitterSizes", sizes());
220
    config.writeEntry("ShowSearch", ActionCollection::action<KToggleAction>("showSearch")->isChecked());
221 222
}

223
void PlaylistSplitter::slotShowSearchResults()
224 225 226
{
    PlaylistList playlists;
    playlists.append(visiblePlaylist());
227 228
    PlaylistSearch search = m_searchWidget->search(playlists);
    visiblePlaylist()->setSearch(search);
229 230
}

231
void PlaylistSplitter::slotPlaylistSelectionChanged()
232
{
233
    m_editor->slotSetItems(visiblePlaylist()->selectedItems());
234 235
}

236
void PlaylistSplitter::slotPlaylistChanged(QWidget *w)
237
{
238 239
    Playlist *p = dynamic_cast<Playlist *>(w);

240
    if(!p)
241
        return;
242 243 244 245

    m_newVisible = p;
    m_searchWidget->setSearch(p->search());
    m_newVisible = 0;
246 247
}

248
#include "playlistsplitter.moc"
249 250

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