juk.cpp 11.4 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 19

/***************************************************************************
 *                                                                         *
 *   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>
#include <kfiledialog.h>
20
#include <kiconloader.h>
21
#include <kcmdlineargs.h>
22 23 24 25 26 27 28 29 30 31
#include <kdebug.h>

#include "juk.h"

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

JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name)
{
32 33
    // Expect segfaults if you change this order.

34 35 36 37
    setupActions();
    setupLayout();
    setupPlayer();
    readConfig();
38
    processArgs();
39 40 41 42
}

JuK::~JuK()
{
43 44
    saveConfig();
    delete(playTimer);
45 46 47 48 49 50 51 52
}

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

void JuK::setupActions()
{
53 54 55 56
    // file menu
    KStdAction::open(this, SLOT(openFile()), actionCollection());
    (void) new KAction(i18n("Open &Directory..."), "fileopen", 0, this, SLOT(openDirectory()), actionCollection(), "openDirectory");
    KStdAction::save(this, SLOT(saveFile()), actionCollection());
57
    (void) new KAction(i18n("Delete"), "edittrash", 0, this, SLOT(remove()), actionCollection(), "remove");
58 59 60 61 62 63 64 65
    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());
    KStdAction::selectAll(this, SLOT(selectAll()), actionCollection());

66 67 68
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");

69 70 71 72 73 74 75 76 77
    // play menu
    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");

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

    createGUI();
78 79 80 81
}

void JuK::setupLayout()
{
82
    // automagically save and restore settings
83
    setAutoSaveSettings();
84

85 86
    splitter = new PlaylistSplitter(this, "playlistSplitter");
    setCentralWidget(splitter);
87
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
88

89 90 91
    // set the slider to the proper orientation and make it stay that way
    sliderAction->updateOrientation();
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)), sliderAction, SLOT(updateOrientation(QDockWindow *)));
92

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

    splitter->setFocus();
97 98 99 100
}

void JuK::setupPlayer()
{
101 102
    trackPositionDragging = false;
    noSeek = false;
103 104 105 106 107 108 109 110 111 112 113
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);

    playTimer=new QTimer(this);
    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);
114

115 116
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
117 118
}

119 120 121 122 123 124 125 126
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

127
    splitter->open(files);
128 129
}

130 131
void JuK::readConfig()
{
132 133 134 135 136 137 138
    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);
        }
139
    }
140 141 142 143 144 145
    { // view Settings
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
146 147 148 149
}

void JuK::saveConfig()
{
150 151 152 153 154 155
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
    }
156 157 158 159
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////

void JuK::openFile()
{
174
    QStringList files = KFileDialog::getOpenFileNames(QString::null, "*.mp3|MPEG Audio (*.mp3)");
175
    splitter->open(files);
176 177 178 179
}

void JuK::openDirectory()
{
180
    splitter->open(KFileDialog::getExistingDirectory());
181 182
}

183 184
void JuK::saveFile()
{
185
    splitter->save();
186 187
}

188
void JuK::remove()
189
{
190 191
    QPtrList<PlaylistItem> items(splitter->playlistSelection());
    PlaylistItem *item = items.first();
192
    while(item) {
193 194 195
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
196
    }
197 198
    
    splitter->remove();
199 200
}

201 202
void JuK::quit()
{
203
    delete(this);
204 205
}

206 207 208 209 210 211
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

void JuK::cut()
{
212
    QPtrList<PlaylistItem> items = splitter->playlistSelection();
Scott Wheeler's avatar
Scott Wheeler committed
213

214 215 216 217 218
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
219 220
    }

221
    splitter->clearSelectedItems();
222 223
}

224
void JuK::selectAll(bool select)
225
{
226
    splitter->selectAll(select);
227 228 229 230 231 232 233 234
}

////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
235 236 237 238 239 240 241 242 243
    if(player.paused()) {
        player.play();
        if(player.playing()) {
            playAction->setEnabled(false);
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
244 245
    else if(splitter) {
        QPtrList<PlaylistItem> items = splitter->playlistSelection();
246
        if(items.count() > 0)
247
            playItem(items.at(0));
248
        else
249
            playItem(splitter->playlistFirstItem());
250
    }
251 252 253 254
}

void JuK::pauseFile()
{
255 256 257 258
    playTimer->stop();
    player.pause();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
259 260 261 262
}

void JuK::stopFile()
{
263 264 265 266 267 268 269
    playTimer->stop();
    player.stop();
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
270 271
    if(playingItem)
        playingItem->setPixmap(0, 0);
272 273 274 275
}

void JuK::trackPositionSliderClick()
{
276
    trackPositionDragging=true;
277 278 279 280
}

void JuK::trackPositionSliderRelease()
{
281 282
    trackPositionDragging=false;
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
283 284 285 286
}

void JuK::trackPositionSliderUpdate(int position)
{
287
    if(player.playing() && !trackPositionDragging && !noSeek)
288
        player.seekPosition(position);
289 290 291 292
}

void JuK::pollPlay()
{
293 294 295
    noSeek = true;
    if(!player.playing()) {
        playTimer->stop();
296
        if(player.paused())
297 298
            pauseFile();
        else {
299
            if(playingItem && dynamic_cast<PlaylistItem *>(playingItem->itemBelow())) {
300
                playingItem->setPixmap(0, 0);
301
                playingItem = dynamic_cast<PlaylistItem *>(playingItem->itemBelow());
302 303 304 305 306 307 308 309 310 311
                sliderAction->getTrackPositionSlider()->setValue(0);
                player.play(playingItem->absFilePath(), player.getVolume());
                if(player.playing()) {
                    playTimer->start(pollInterval);
                    playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
                }
            }
            else
                stopFile();
        }
312
    }
313
    else if(!trackPositionDragging)
314
        sliderAction->getTrackPositionSlider()->setValue(player.position());
315

316 317
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
318

319
    noSeek=false;
320 321 322 323
}

void JuK::setVolume(int volume)
{
324 325 326
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
327
    {
328
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
329 330
    }
}
331 332 333

void JuK::playItem(QListViewItem *item)
{
334
    PlaylistItem *fileListItem = dynamic_cast<PlaylistItem *>(item);
335 336
    if(fileListItem)
        playItem(fileListItem);
337 338
}

339
void JuK::playItem(PlaylistItem *item)
340
{
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    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);
        }
356 357
    }
}
358 359 360 361 362 363

void JuK::playTaggerItem(QListViewItem *item)
{

}

364
void JuK::playTaggerItem(PlaylistItem *item)
365 366 367
{

}
368 369

#include "juk.moc"