juk.cpp 13.9 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
    readSettings();
43
    setupLayout();
44
    setupActions();
45 46
    setupPlayer();
    readConfig();
47
    processArgs();
48 49 50 51
}

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

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

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

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

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

71 72 73
    splitter->setFocus();
}

74 75
void JuK::setupActions()
{
76
    // file menu
77 78 79 80
    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");
81 82 83 84 85 86
    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());
87
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
88

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

94
    // play menu
95
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
96 97 98 99
    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");

100
    // playlist menu
101 102 103 104 105 106 107 108 109
    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");
110 111
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), 
					   actionCollection(), "deleteItemPlaylist");
112
    
113 114 115
    // settings menu
    restoreOnLoadAction = new KToggleAction(i18n("Restored Playlists on Load"),  0, actionCollection(), "restoreOnLoad"); 

116 117 118
    playlistChanged(0);
    connect(splitter, SIGNAL(playlistChanged(Playlist *)), this, SLOT(playlistChanged(Playlist *)));

119

120 121 122 123
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
124 125 126 127

    // set the slider to the proper orientation and make it stay that way
    sliderAction->updateOrientation();
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)), sliderAction, SLOT(updateOrientation(QDockWindow *)));
128 129 130 131
}

void JuK::setupPlayer()
{
132 133
    trackPositionDragging = false;
    noSeek = false;
134 135 136
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);

137
    playTimer = new QTimer(this);
138 139 140 141 142 143 144
    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);
145

146 147
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
148 149
}

150

151 152 153 154 155 156 157 158
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

159
    splitter->open(files);
160 161
}

162 163
void JuK::readConfig()
{
164 165 166
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

167 168 169 170 171 172 173
    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);
        }
174 175 176 177
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
178
    }
179
    { // view settings
180 181 182 183 184
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
185 186 187 188 189 190 191 192 193 194 195 196

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
}

void JuK::readSettings()
{
    KConfig *config = KGlobal::config();
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	restore = config->readBoolEntry("RestoreOnLoad", true);
    }
197 198 199 200
}

void JuK::saveConfig()
{
201 202 203 204 205
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
206 207
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
208
    }
209 210 211 212
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
213 214 215 216 217
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
    }
218 219
}

220 221
bool JuK::queryClose()
{
222
    Cache::instance()->save();
223
    saveConfig();
224
    delete(splitter);
225 226 227
    return(true);
}

228 229 230 231
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

232
void JuK::playlistChanged(Playlist *list)
233
{
234 235 236 237 238 239 240 241 242 243 244 245
    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);
    }
246 247
}

248 249 250
////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////
251

252
void JuK::remove()
253
{
254
    PlaylistItemList items(splitter->playlistSelection());
255
    PlaylistItem *item = items.first();
256
    while(item) {
257 258 259
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
260
    }
261 262
    
    splitter->remove();
263 264
}

265 266
void JuK::quit()
{
267 268 269
    // delete(this);
    // kapp->quit();
    close();
270 271
}

272 273 274 275 276 277
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

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

280 281 282 283 284
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
285 286
    }

287
    splitter->clearSelectedItems();
288 289
}

290 291 292 293 294 295
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
296 297 298 299 300 301 302 303
    if(player.paused()) {
        player.play();
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
304 305
    else if(player.playing())
	player.seekPosition(0);
306
    else if(splitter) {
307
        PlaylistItemList items = splitter->playlistSelection();
308
        if(items.count() > 0)
309
            playItem(items.at(0));
310
        else
311
            playItem(splitter->playlistFirstItem());
312
    }
313 314 315 316
}

void JuK::pauseFile()
{
317 318 319
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
320 321 322 323
}

void JuK::stopFile()
{
324 325 326 327 328 329
    playTimer->stop();
    player.stop();
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
330 331
    if(playingItem)
        playingItem->setPixmap(0, 0);
332
    playingItem = 0;
333

334
    statusLabel->clear();
335 336
}

337 338 339 340
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

341 342
void JuK::trackPositionSliderClick()
{
343
    trackPositionDragging = true;
344 345 346 347
}

void JuK::trackPositionSliderRelease()
{
348
    trackPositionDragging = false;
349
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
350 351 352 353
}

void JuK::trackPositionSliderUpdate(int position)
{
354
    if(player.playing() && !trackPositionDragging && !noSeek)
355
        player.seekPosition(position);
356 357 358 359
}

void JuK::pollPlay()
{
360 361
    noSeek = true;
    if(!player.playing()) {
362

363
        playTimer->stop();
364

365
        if(player.paused())
366
            pauseFile();
367
        else if(playingItem) {
368

369
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
370 371
	    playingItem->setPixmap(0, 0);

372 373 374 375 376 377 378 379 380
	    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")));
		}
	    }
381
	    statusLabel->setPlayingItem(playingItem);
382 383 384
	}
	else
	    stopFile();
385
    }
386
    else if(!trackPositionDragging)
387
        sliderAction->getTrackPositionSlider()->setValue(player.position());
388

389 390
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
391

392
    noSeek = false;
393 394 395 396
}

void JuK::setVolume(int volume)
{
397 398 399
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
400
    {
401
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
402 403
    }
}
404 405 406

void JuK::playItem(QListViewItem *item)
{
407
    PlaylistItem *fileListItem = dynamic_cast<PlaylistItem *>(item);
408 409
    if(fileListItem)
        playItem(fileListItem);
410 411
}

412
void JuK::playItem(PlaylistItem *item)
413
{
414 415 416 417 418 419 420 421 422 423 424 425 426
    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()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            sliderAction->getTrackPositionSlider()->setEnabled(true);
            playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
            playTimer->start(pollInterval);
427

428
	    statusLabel->setPlayingItem(playingItem);
429
        }
430 431
    }
}
432

433
#include "juk.moc"