juk.cpp 8.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/***************************************************************************
                          juk.cpp  -  description
                             -------------------
    begin                : Mon Feb  4 23:40:41 EST 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : scott@slackorama.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <keditcl.h>
#include <kfiledialog.h>
#include <kdebug.h>

#include "juk.h"

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

JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name)
{
  setupActions();
  setupLayout();
  setupPlayer();
34
  readConfig();
35 36 37 38
}

JuK::~JuK()
{
39
  saveConfig();
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

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

void JuK::setupActions()
{
  // 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());
  (void) new KAction(i18n("Delete"), "edittrash", 0, this, SLOT(deleteFile()), actionCollection(), "deleteFile");
  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());
 
  // play menu
  (void) new KAction(i18n("&Add to Playlist"), "enqueue", 0, this, SLOT(addToPlaylist()), actionCollection(), "addToPlaylist");
  (void) new KAction(i18n("&Remove from Playlist"), "dequeue", 0, this, SLOT(removeFromPlaylist()), actionCollection(), "removeFromPlaylist");
  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");

  // function menu

  (void) new KAction(i18n("Tagger"), "tag", 0, this, SLOT(showTagger()), actionCollection(), "showTagger");
  (void) new KAction(i18n("Playlist Editor"), "edit", 0, this, SLOT(showPlaylist()), actionCollection(), "showPlaylist");
  

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

  createGUI();
}

void JuK::setupLayout()
{
  // automagically save and restore settings
  this->setAutoSaveSettings();

  // create the main widgets
  tagger = new TaggerWidget(this);
  playlist = new PlaylistWidget(this);
  
  showTagger();

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

void JuK::setupPlayer()
{
  trackPositionDragging=false;
  noSeek=false;
  pauseAction->setEnabled(false);
  stopAction->setEnabled(false);

  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);

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

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void JuK::readConfig()
{
  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);
    }
  }
}

void JuK::saveConfig()
{
  KConfig *config = KGlobal::config();
  { // player settings
    KConfigGroupSaver saver(config, "Player");
    if(sliderAction && sliderAction->getVolumeSlider())
      config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
  }  
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

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

void JuK::openFile()
{
  QStringList files = KFileDialog::getOpenFileNames(QString::null, "*.mp3|MPEG Audio (*.mp3)");
  tagger->add(&files);
}

void JuK::openDirectory()
{
  tagger->add(KFileDialog::getExistingDirectory());
}

156 157 158 159 160 161
void JuK::saveFile()
{
  if(tagger && tagger->isVisible())
    tagger->save();
}

162 163
void JuK::quit()
{
164
  delete(this);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

////////////////////////////////////////////////////////////////////////////////
// function menu
////////////////////////////////////////////////////////////////////////////////

void JuK::showTagger()
{
  playlist->hide();
  tagger->show();
  setCentralWidget(tagger);
}

void JuK::showPlaylist()
{
  tagger->hide();
  playlist->show();
  setCentralWidget(playlist);
}

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


void JuK::playFile()
{
  if(tagger && tagger->getSelectedItem()) {
    // get the volume to start with
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());

    playTimer=new QTimer(this);
197
    player.play(tagger->getSelectedItem()->absFilePath(), volume);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    if(player.playing()) {
      playAction->setEnabled(false);
      pauseAction->setEnabled(true);
      stopAction->setEnabled(true);
      sliderAction->getTrackPositionSlider()->setEnabled(true);
      connect(playTimer, SIGNAL(timeout()), this, SLOT(pollPlay()));
      playTimer->start(800);
    }
  }
}

void JuK::pauseFile()
{
  playTimer->stop();
  delete(playTimer);
  player.pause();
  playAction->setEnabled(true);
  pauseAction->setEnabled(false);
}

void JuK::stopFile()
{
  playTimer->stop();
  delete(playTimer);
  player.stop();
  playAction->setEnabled(true);
  pauseAction->setEnabled(false);
  stopAction->setEnabled(false);
  sliderAction->getTrackPositionSlider()->setValue(0);
  sliderAction->getTrackPositionSlider()->setEnabled(false);
}

void JuK::trackPositionSliderClick()
{
  trackPositionDragging=true;
}

void JuK::trackPositionSliderRelease()
{
  trackPositionDragging=false;
  player.seekPosition(sliderAction->getTrackPositionSlider()->value());
}

void JuK::trackPositionSliderUpdate(int position)
{
  if(player.playing() && !trackPositionDragging && !noSeek) {
    player.seekPosition(position);
  }
}

void JuK::pollPlay()
{
  noSeek=true;
  if(!player.playing()) {
    playTimer->stop();
    if(player.paused()) {
      pauseFile();
    }
    else {
      stopFile();
    }
  }
  else if(!trackPositionDragging) {
    //    kdDebug() << player.position() << " - " << sliderAction->getTrackPositionSlider()->maxValue() << endl;
    sliderAction->getTrackPositionSlider()->setValue(player.position());
  }
  noSeek=false;
}

void JuK::setVolume(int volume)
{
  if(sliderAction && sliderAction->getVolumeSlider() && 
     sliderAction->getVolumeSlider()->maxValue() > 0 &&
     volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
    {
      player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
    }
}