juk.cpp 13.7 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 33 34 35 36

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

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

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

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

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

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

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

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

69 70 71
    splitter->setFocus();
}

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

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

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

98
    // playlist menu
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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 *)));

113

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

    createGUI();
118 119 120 121

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

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

131
    playTimer = new QTimer(this);
132 133 134 135 136 137 138
    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);
139

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

144

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

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

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

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

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

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

205 206 207 208
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

209
void JuK::playlistChanged(Playlist *list)
210
{
211 212 213 214 215 216 217 218 219 220 221 222
    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);
    }
223 224
}

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

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

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

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

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

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

264
    splitter->clearSelectedItems();
265 266
}

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

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

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

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

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    updateStatusLabel();
}

void JuK::updateStatusLabel()
{
    if(playingItem) {
	Playlist *p = static_cast<Playlist *>(playingItem->listView());
	if(p && p->playlistBoxItem()) {
	    QString label = p->playlistBoxItem()->text() 
		+ " / " + playingItem->text(PlaylistItem::ArtistColumn) 
		+ " - " + playingItem->text(PlaylistItem::TrackColumn);
	    statusLabel->setText(label);
	}
	else
	    statusLabel->clear();
    }
    else
	statusLabel->clear();
330 331
}

332 333 334 335
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

336 337
void JuK::trackPositionSliderClick()
{
338
    trackPositionDragging = true;
339 340 341 342
}

void JuK::trackPositionSliderRelease()
{
343
    trackPositionDragging = false;
344
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
345 346 347 348
}

void JuK::trackPositionSliderUpdate(int position)
{
349
    if(player.playing() && !trackPositionDragging && !noSeek)
350
        player.seekPosition(position);
351 352 353 354
}

void JuK::pollPlay()
{
355 356
    noSeek = true;
    if(!player.playing()) {
357

358
        playTimer->stop();
359

360
        if(player.paused())
361
            pauseFile();
362
        else if(playingItem) {
363

364
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
365 366
	    playingItem->setPixmap(0, 0);

367 368 369 370 371 372 373 374 375
	    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")));
		}
	    }
376
	    updateStatusLabel();
377 378 379
	}
	else
	    stopFile();
380
    }
381
    else if(!trackPositionDragging)
382
        sliderAction->getTrackPositionSlider()->setValue(player.position());
383

384 385
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
386

387
    noSeek = false;
388 389 390 391
}

void JuK::setVolume(int volume)
{
392 393 394
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
395
    {
396
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
397 398
    }
}
399 400 401

void JuK::playItem(QListViewItem *item)
{
402
    PlaylistItem *fileListItem = dynamic_cast<PlaylistItem *>(item);
403 404
    if(fileListItem)
        playItem(fileListItem);
405 406
}

407
void JuK::playItem(PlaylistItem *item)
408
{
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    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);
423

424
	    updateStatusLabel();
425
        }
426 427
    }
}
428

429
#include "juk.moc"