juk.cpp 13.4 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          juk.cpp  -  description
                             -------------------
    begin                : Mon Feb  4 23:40:41 EST 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
***************************************************************************/
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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <klocale.h>
19
#include <kiconloader.h>
20
#include <kcmdlineargs.h>
21
#include <kstatusbar.h>
22 23
#include <kdebug.h>

24 25
#include <qinputdialog.h>

26
#include "juk.h"
27 28 29
#include "playlist.h"
#include "playlistsplitter.h"
#include "collectionlist.h"
30
#include "slideraction.h"
31
#include "cache.h"
32
#include "statuslabel.h"
33 34 35 36 37

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

38
JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name, WDestructiveClose)
39
{
40 41
    // Expect segfaults if you change this order.

42
    setupLayout();
43
    setupActions();
44 45
    setupPlayer();
    readConfig();
46
    processArgs();
47 48 49 50
}

JuK::~JuK()
{
51
    delete(playTimer);
52 53 54 55 56 57
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

58 59
void JuK::setupLayout()
{
60
    splitter = new PlaylistSplitter(this, "playlistSplitter");
61 62 63 64 65
    setCentralWidget(splitter);

    // playlist item activation connection
    connect(splitter, SIGNAL(playlistDoubleClicked(QListViewItem *)), this, SLOT(playItem(QListViewItem *)));

66
    // create status bar
67
    statusLabel = new StatusLabel(statusBar());
68 69
    statusBar()->addWidget(statusLabel, 1);

70 71 72
    splitter->setFocus();
}

73 74
void JuK::setupActions()
{
75
    // file menu
76 77 78 79
    KStdAction::open(splitter, SLOT(open()), actionCollection());
    new KAction(i18n("Open &Directory..."), "fileopen", 0, splitter, SLOT(openDirectory()), actionCollection(), "openDirectory");
    KStdAction::save(splitter, SLOT(save()), actionCollection());
    new KAction(i18n("Delete"), "editdelete", 0, this, SLOT(remove()), actionCollection(), "remove");
80 81 82 83 84 85
    KStdAction::quit(this, SLOT(quit()), actionCollection());

    // edit menu
    KStdAction::cut(this, SLOT(cut()), actionCollection());
    KStdAction::copy(this, SLOT(copy()), actionCollection());
    KStdAction::paste(this, SLOT(paste()), actionCollection());
86
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
87

88 89
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");
90 91
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
92

93
    // play menu
94
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
95 96 97 98
    playAction = new KAction(i18n("&Play"), "1rightarrow", 0, this, SLOT(playFile()), actionCollection(), "playFile");
    pauseAction = new KAction(i18n("P&ause"), "player_pause", 0, this, SLOT(pauseFile()), actionCollection(), "pauseFile");
    stopAction = new KAction(i18n("&Stop"), "player_stop", 0, this, SLOT(stopFile()), actionCollection(), "stopFile");

99
    // playlist menu
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    new KAction(i18n("New..."), "filenew", 0, splitter, SLOT(createPlaylist()), actionCollection(), "createPlaylist");
    new KAction(i18n("Open..."), "fileopen", 0, splitter, SLOT(openPlaylist()), actionCollection(), "openPlaylist");

    savePlaylistAction = new KAction(i18n("Save"), "filesave", 0, splitter, SLOT(savePlaylist()), actionCollection(), "savePlaylist");
    saveAsPlaylistAction = new KAction(i18n("Save As..."), "filesaveas", 0, splitter, SLOT(saveAsPlaylist()), 
				       actionCollection(), "saveAsPlaylist");
    renamePlaylistAction = new KAction(i18n("Rename..."), 0, splitter, SLOT(renamePlaylist()), 
				       actionCollection(), "renamePlaylist");
    new KAction(i18n("Duplicate..."), "editcopy", 0, splitter, SLOT(duplicatePlaylist()), actionCollection(), "duplicatePlaylist");
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), actionCollection(), "deleteItemPlaylist");
    
    playlistChanged(0);
    connect(splitter, SIGNAL(playlistChanged(Playlist *)), this, SLOT(playlistChanged(Playlist *)));

114

115 116 117 118
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
119 120 121 122

    // set the slider to the proper orientation and make it stay that way
    sliderAction->updateOrientation();
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)), sliderAction, SLOT(updateOrientation(QDockWindow *)));
123 124 125 126
}

void JuK::setupPlayer()
{
127 128
    trackPositionDragging = false;
    noSeek = false;
129 130 131
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);

132
    playTimer = new QTimer(this);
133 134 135 136 137 138 139
    connect(playTimer, SIGNAL(timeout()), this, SLOT(pollPlay()));

    if(sliderAction && sliderAction->getTrackPositionSlider() && sliderAction->getVolumeSlider()) {
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(valueChanged(int)), this, SLOT(trackPositionSliderUpdate(int)));
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(sliderPressed()), this, SLOT(trackPositionSliderClick()));
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(sliderReleased()), this, SLOT(trackPositionSliderRelease()));
        sliderAction->getTrackPositionSlider()->setEnabled(false);
140

141 142
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
143 144
}

145

146 147 148 149 150 151 152 153
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

154
    splitter->open(files);
155 156
}

157 158
void JuK::readConfig()
{
159 160 161
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

162 163 164 165 166 167 168
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider()) {
            int volume = config->readNumEntry("Volume", sliderAction->getVolumeSlider()->maxValue());
            sliderAction->getVolumeSlider()->setValue(volume);
        }
169 170 171 172
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
173
    }
174 175 176 177 178 179
    { // view Settings
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
180 181 182 183
}

void JuK::saveConfig()
{
184 185 186 187 188
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
189 190
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
191
    }
192 193 194 195
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
196 197
}

198 199
bool JuK::queryClose()
{
200
    Cache::instance()->save();
201
    saveConfig();
202
    delete(splitter);
203 204 205
    return(true);
}

206 207 208 209
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

210
void JuK::playlistChanged(Playlist *list)
211
{
212 213 214 215 216 217 218 219 220 221 222 223
    if(!list || list == CollectionList::instance()) {
	savePlaylistAction->setEnabled(false);
	saveAsPlaylistAction->setEnabled(false);
	renamePlaylistAction->setEnabled(false);
	deleteItemPlaylistAction->setEnabled(false);	
    }
    else {
	savePlaylistAction->setEnabled(true);
	saveAsPlaylistAction->setEnabled(true);
	renamePlaylistAction->setEnabled(true);
	deleteItemPlaylistAction->setEnabled(true);
    }
224 225
}

226 227 228
////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////
229

230
void JuK::remove()
231
{
232
    PlaylistItemList items(splitter->playlistSelection());
233
    PlaylistItem *item = items.first();
234
    while(item) {
235 236 237
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
238
    }
239 240
    
    splitter->remove();
241 242
}

243 244
void JuK::quit()
{
245 246 247
    // delete(this);
    // kapp->quit();
    close();
248 249
}

250 251 252 253 254 255
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

void JuK::cut()
{
256
    PlaylistItemList items = splitter->playlistSelection();
Scott Wheeler's avatar
Scott Wheeler committed
257

258 259 260 261 262
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
263 264
    }

265
    splitter->clearSelectedItems();
266 267
}

268 269 270 271 272 273
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
274 275 276 277 278 279 280 281 282
    if(player.paused()) {
        player.play();
        if(player.playing()) {
            playAction->setEnabled(false);
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
283
    else if(splitter) {
284
        PlaylistItemList items = splitter->playlistSelection();
285
        if(items.count() > 0)
286
            playItem(items.at(0));
287
        else
288
            playItem(splitter->playlistFirstItem());
289
    }
290 291 292 293
}

void JuK::pauseFile()
{
294 295 296 297
    playTimer->stop();
    player.pause();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
298 299 300 301
}

void JuK::stopFile()
{
302 303 304 305 306 307 308
    playTimer->stop();
    player.stop();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
309 310
    if(playingItem)
        playingItem->setPixmap(0, 0);
311
    playingItem = 0;
312

313
    statusLabel->clear();
314 315
}

316 317 318 319
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

320 321
void JuK::trackPositionSliderClick()
{
322
    trackPositionDragging = true;
323 324 325 326
}

void JuK::trackPositionSliderRelease()
{
327
    trackPositionDragging = false;
328
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
329 330 331 332
}

void JuK::trackPositionSliderUpdate(int position)
{
333
    if(player.playing() && !trackPositionDragging && !noSeek)
334
        player.seekPosition(position);
335 336 337 338
}

void JuK::pollPlay()
{
339 340
    noSeek = true;
    if(!player.playing()) {
341

342
        playTimer->stop();
343

344
        if(player.paused())
345
            pauseFile();
346
        else if(playingItem) {
347

348
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
349 350
	    playingItem->setPixmap(0, 0);

351 352 353 354 355 356 357 358 359
	    if(next) {
		playingItem = next;
		sliderAction->getTrackPositionSlider()->setValue(0);
		player.play(playingItem->absFilePath(), player.getVolume());
		if(player.playing()) {
		    playTimer->start(pollInterval);
		    playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
		}
	    }
360
	    statusLabel->setPlayingItem(playingItem);
361 362 363
	}
	else
	    stopFile();
364
    }
365
    else if(!trackPositionDragging)
366
        sliderAction->getTrackPositionSlider()->setValue(player.position());
367

368 369
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
370

371
    noSeek = false;
372 373 374 375
}

void JuK::setVolume(int volume)
{
376 377 378
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
379
    {
380
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
381 382
    }
}
383 384 385

void JuK::playItem(QListViewItem *item)
{
386
    PlaylistItem *fileListItem = dynamic_cast<PlaylistItem *>(item);
387 388
    if(fileListItem)
        playItem(fileListItem);
389 390
}

391
void JuK::playItem(PlaylistItem *item)
392
{
393 394 395 396 397 398 399 400 401 402 403 404 405 406
    if(player.playing() || player.paused())
        stopFile();

    if(item) {
        playingItem = item;
        float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
        player.play(playingItem->absFilePath(), volume);
        if(player.playing()) {
            playAction->setEnabled(false);
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            sliderAction->getTrackPositionSlider()->setEnabled(true);
            playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
            playTimer->start(pollInterval);
407

408
	    statusLabel->setPlayingItem(playingItem);
409
        }
410 411
    }
}
412

413
#include "juk.moc"