juk.cpp 12.9 KB
Newer Older
1 2 3 4 5 6
/***************************************************************************
                          juk.cpp  -  description
                             -------------------
    begin                : Mon Feb  4 23:40:41 EST 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : scott@slackorama.net
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 22
#include <kdebug.h>

23 24
#include <qinputdialog.h>

25
#include "juk.h"
26 27 28
#include "playlist.h"
#include "playlistsplitter.h"
#include "collectionlist.h"
29 30 31 32 33

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

34
JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name, WDestructiveClose)
35
{
36 37
    // Expect segfaults if you change this order.

38
    setupLayout();
39
    setupActions();
40 41
    setupPlayer();
    readConfig();
42
    processArgs();
43 44 45 46
}

JuK::~JuK()
{
47 48
    saveConfig();
    delete(playTimer);
49 50 51 52 53 54
}

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

55 56 57 58 59 60 61 62 63 64 65 66
void JuK::setupLayout()
{
    PlaylistSplitter::initialize(this);
    splitter = PlaylistSplitter::instance();
    setCentralWidget(splitter);

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

    splitter->setFocus();
}

67 68
void JuK::setupActions()
{
69
    // file menu
70 71 72 73
    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");
74 75 76 77 78 79
    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());
80
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
81

82 83
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");
84 85
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
86

87
    // play menu
88
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
89 90 91 92
    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");

93
    // playlist menu
94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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 *)));

108

109 110 111 112
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
113 114 115 116

    // set the slider to the proper orientation and make it stay that way
    sliderAction->updateOrientation();
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)), sliderAction, SLOT(updateOrientation(QDockWindow *)));
117 118 119 120
}

void JuK::setupPlayer()
{
121 122
    trackPositionDragging = false;
    noSeek = false;
123 124 125
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);

126
    playTimer = new QTimer(this);
127 128 129 130 131 132 133
    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);
134

135 136
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
137 138
}

139 140 141 142 143 144 145 146
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

147
    splitter->open(files);
148 149
}

150 151
void JuK::readConfig()
{
152 153 154
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

155 156 157 158 159 160 161
    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);
        }
162 163 164 165
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
166
    }
167 168 169 170 171 172
    { // view Settings
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
173 174 175 176
}

void JuK::saveConfig()
{
177 178 179 180 181
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
182 183
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
184
    }
185 186 187 188
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
189 190
}

191 192 193 194
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

195
void JuK::playlistChanged(Playlist *list)
196
{
197 198 199 200 201 202 203 204 205 206 207 208
    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);
    }
209 210
}

211 212 213
////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////
214

215
void JuK::remove()
216
{
217
    PlaylistItemList items(splitter->playlistSelection());
218
    PlaylistItem *item = items.first();
219
    while(item) {
220 221 222
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
223
    }
224 225
    
    splitter->remove();
226 227
}

228 229
void JuK::quit()
{
230 231 232
    // delete(this);
    // kapp->quit();
    close();
233 234
}

235 236 237 238 239 240
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

void JuK::cut()
{
241
    PlaylistItemList items = splitter->playlistSelection();
242

243 244 245 246 247
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
248 249
    }

250
    splitter->clearSelectedItems();
251 252
}

253 254 255 256 257 258
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
259 260 261 262 263 264 265 266 267
    if(player.paused()) {
        player.play();
        if(player.playing()) {
            playAction->setEnabled(false);
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
268
    else if(splitter) {
269
        PlaylistItemList items = splitter->playlistSelection();
270
        if(items.count() > 0)
271
            playItem(items.at(0));
272
        else
273
            playItem(splitter->playlistFirstItem());
274
    }
275 276 277 278
}

void JuK::pauseFile()
{
279 280 281 282
    playTimer->stop();
    player.pause();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
283 284 285 286
}

void JuK::stopFile()
{
287 288 289 290 291 292 293
    playTimer->stop();
    player.stop();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
294 295
    if(playingItem)
        playingItem->setPixmap(0, 0);
296 297
}

298 299 300 301
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

302 303
void JuK::trackPositionSliderClick()
{
304
    trackPositionDragging = true;
305 306 307 308
}

void JuK::trackPositionSliderRelease()
{
309
    trackPositionDragging = false;
310
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
311 312 313 314
}

void JuK::trackPositionSliderUpdate(int position)
{
315
    if(player.playing() && !trackPositionDragging && !noSeek)
316
        player.seekPosition(position);
317 318 319 320
}

void JuK::pollPlay()
{
321 322 323
    noSeek = true;
    if(!player.playing()) {
        playTimer->stop();
324
        if(player.paused())
325
            pauseFile();
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        else if(playingItem) {
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
	    if(next) {
		playingItem->setPixmap(0, 0);
		playingItem = next;
		sliderAction->getTrackPositionSlider()->setValue(0);
		player.play(playingItem->absFilePath(), player.getVolume());
		if(player.playing()) {
		    playTimer->start(pollInterval);
		    playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
		}
	    }
	}
	else
	    stopFile();
341
    }
342
    else if(!trackPositionDragging)
343
        sliderAction->getTrackPositionSlider()->setValue(player.position());
344

345 346
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
347

348
    noSeek = false;
349 350 351 352
}

void JuK::setVolume(int volume)
{
353 354 355
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
356
    {
357
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
358 359
    }
}
360 361 362

void JuK::playItem(QListViewItem *item)
{
363
    PlaylistItem *fileListItem = dynamic_cast<PlaylistItem *>(item);
364 365
    if(fileListItem)
        playItem(fileListItem);
366 367
}

368
void JuK::playItem(PlaylistItem *item)
369
{
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    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);
        }
385 386
    }
}
387

388
#include "juk.moc"