Skip to content

Commit

Permalink
Improved random mode
Browse files Browse the repository at this point in the history
Improved save playlist dialog
  • Loading branch information
niktischenko committed Sep 28, 2010
1 parent 6bc16ab commit bd0e545
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 26 deletions.
9 changes: 6 additions & 3 deletions someplayer.pro
Expand Up @@ -118,7 +118,8 @@ SOURCES += src/main.cpp\
src/taglib/wavpack/wavpackfile.cpp \
src/edittagsdialog.cpp \
src/timerdialog.cpp \
src/equalizerdialog.cpp
src/equalizerdialog.cpp \
src/saveplaylistdialog.cpp

HEADERS += src/mainwindow.h \
src/player/player.h \
Expand Down Expand Up @@ -214,7 +215,8 @@ HEADERS += src/mainwindow.h \
src/taglib/wavpack/wavpackfile.h \
src/edittagsdialog.h \
src/timerdialog.h \
src/equalizerdialog.h
src/equalizerdialog.h \
src/saveplaylistdialog.h

FORMS += src/ui/mainwindow.ui \
src/ui/playerform.ui \
Expand All @@ -223,7 +225,8 @@ FORMS += src/ui/mainwindow.ui \
src/ui/playlistdialog.ui \
src/ui/edittagsdialog.ui \
src/ui/timerdialog.ui \
src/ui/equalizerdialog.ui
src/ui/equalizerdialog.ui \
src/ui/saveplaylistdialog.ui

CONFIG += mobility
MOBILITY =
Expand Down
48 changes: 27 additions & 21 deletions src/mainwindow.cpp
Expand Up @@ -29,6 +29,7 @@
#include "library.h"
#include "timerdialog.h"
#include "equalizerdialog.h"
#include "saveplaylistdialog.h"

using namespace SomePlayer::DataObjects;
using namespace SomePlayer::Storage;
Expand Down Expand Up @@ -125,30 +126,35 @@ void MainWindow::_add_directory() {
}

void MainWindow::_save_playlist() {
QString name = QInputDialog::getText(this, "Playlist name", "Name:");
QList<QString> playlists = _library->getPlaylistsNames();
bool append = false;
if (playlists.contains(name)) {
if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
"Dow you want to append current playlist to it?",
QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
append = true;
} else {
append = false;
playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
SavePlaylistDialog dialog(this);
dialog.setPlaylistNames(playlists);
if (dialog.exec() == QDialog::Accepted) {
QString name = dialog.selectedName();
bool append = false;
if (playlists.contains(name)) {
if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
"Dow you want to append current playlist to it?",
QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
append = true;
} else {
append = false;
}
}
}
if (append) {
Playlist cur = _library->getCurrentPlaylist();
Playlist target = _library->getPlaylist(name);
QList<Track> tracks = cur.tracks();
foreach (Track track, tracks) {
target.addTrack(track);
if (append) {
Playlist cur = _library->getCurrentPlaylist();
Playlist target = _library->getPlaylist(name);
QList<Track> tracks = cur.tracks();
foreach (Track track, tracks) {
target.addTrack(track);
}
_library->savePlaylist(target);
} else {
Playlist playlist = _library->getCurrentPlaylist();
playlist.setName(name);
_library->savePlaylist(playlist);
}
_library->savePlaylist(target);
} else {
Playlist playlist = _library->getCurrentPlaylist();
playlist.setName(name);
_library->savePlaylist(playlist);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mediascanner.cpp
Expand Up @@ -27,7 +27,7 @@ using namespace SomePlayer::Storage;
MediaScanner::MediaScanner(QObject *parent) :
QThread(parent), _stopped(false), _initialized(false)
{
REGISTERED_FILE_EXTENSIONS << "mp3" << "flac" << "wma" << "acc" << "ogg";
REGISTERED_FILE_EXTENSIONS << "mp3" << "flac" << "wma" << "aac" << "ogg";
}

void MediaScanner::run() {
Expand Down
42 changes: 41 additions & 1 deletion src/player/player.cpp
Expand Up @@ -29,6 +29,40 @@ using namespace SomePlayer::Playback;
using namespace SomePlayer::DataObjects;
using namespace SomePlayer::Storage;

int Randomizer::next() {
int res = 0;
if (_current == _rand.count()) {
_shuffle();
_current = 0;
res = next();
} else {
res = _rand.at(_current);
}
++_current;
return res;
}

void Randomizer::setPlaylist(QList<int> pl) {
_playlist = pl;
_current = 0;
_shuffle();
}

void Randomizer::_shuffle() {
_rand.clear();
// Fisher-Yates algorithm:
_rand = _playlist;
int cnt = _playlist.count();
int j = 0;
int tmp = 0;
for (int i = cnt-1; i > 0; i--) {
j = qrand() % (i+1);
tmp = _rand[i];
_rand[i] = _rand[j];
_rand[j] = tmp;
}
}

Player::Player(QObject *parent) :
QObject(parent)
{
Expand Down Expand Up @@ -105,7 +139,7 @@ void Player::next() {
_current = _prev_history.pop();
} else {
if (_random) {
_current = (count + (qrand() + qrand() + qrand()) % count) % count;
_current = _randomizer.next();
} else {
_current = _current + 1;
}
Expand Down Expand Up @@ -180,6 +214,12 @@ void Player::setPlaylist(Playlist playlist) {
_history.clear();
_prev_history.clear();
_queue.clear();
QList<int> ids;
int count = playlist.tracks().count();
for (int i = 0; i < count; i++) {
ids.append(i);
}
_randomizer.setPlaylist(ids);
}

void Player::seek(int s) {
Expand Down
12 changes: 12 additions & 0 deletions src/player/player.h
Expand Up @@ -45,6 +45,17 @@ namespace SomePlayer {

enum PlayerState { PLAYER_STOPPED, PLAYER_PLAYING, PLAYER_PAUSED, PLAYER_LOADING, PLAYER_DONE, PLAYER_ERROR };

class Randomizer {
public:
void setPlaylist(QList<int>);
int next();
private:
QList<int> _playlist;
QList<int> _rand;
void _shuffle();
int _current;
};

class Player : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -85,6 +96,7 @@ namespace SomePlayer {
void _stateChanged(Phonon::State, Phonon::State);
void _tick(qint64);
private:
Randomizer _randomizer;
int _current;
Track _track; // current track (workaround)
bool _random;
Expand Down
52 changes: 52 additions & 0 deletions src/saveplaylistdialog.cpp
@@ -0,0 +1,52 @@
/*
* SomePlayer - An alternate music player for Maemo 5
* Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "saveplaylistdialog.h"
#include "ui_saveplaylistdialog.h"

SavePlaylistDialog::SavePlaylistDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SavePlaylistDialog)
{
ui->setupUi(this);
selectedItem = "";
connect(ui->listWidget, SIGNAL(activated(QModelIndex)), this, SLOT(_select_item(QModelIndex)));
}

SavePlaylistDialog::~SavePlaylistDialog()
{
delete ui;
}

void SavePlaylistDialog::setPlaylistNames(QList<QString> names) {
ui->listWidget->addItems(names);
ui->lineEdit->setText("New playlist");
}

QString SavePlaylistDialog::selectedName() {
if (selectedItem.isEmpty())
return ui->lineEdit->text();
else return ui->listWidget->selectedItems().at(0)->text();
}

void SavePlaylistDialog::_select_item(QModelIndex id) {
selectedItem = id.data().toString();
done(QDialog::Accepted);
}

48 changes: 48 additions & 0 deletions src/saveplaylistdialog.h
@@ -0,0 +1,48 @@
/*
* SomePlayer - An alternate music player for Maemo 5
* Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef SAVEPLAYLISTDIALOG_H
#define SAVEPLAYLISTDIALOG_H

#include <QDialog>
#include <QList>
#include <QModelIndex>

namespace Ui {
class SavePlaylistDialog;
}

class SavePlaylistDialog : public QDialog
{
Q_OBJECT

public:
explicit SavePlaylistDialog(QWidget *parent = 0);
~SavePlaylistDialog();
void setPlaylistNames(QList<QString>);
QString selectedName();

private slots:
void _select_item(QModelIndex);
private:
Ui::SavePlaylistDialog *ui;
QString selectedItem;
};

#endif // SAVEPLAYLISTDIALOG_H
9 changes: 9 additions & 0 deletions src/tagresolver.cpp
Expand Up @@ -19,6 +19,7 @@

#include "tagresolver.h"
#include <QFile>
#include <QFileInfo>
#include <tag.h>
#include <fileref.h>

Expand All @@ -45,6 +46,14 @@ void TagResolver::decode(QStringList files) {
emit decoded(track);
}
}
} else { // workaround
TrackMetadata meta;
meta.setLength(0);
QFileInfo fi(filename);
meta.setArtist(fi.suffix().toUpper());
meta.setTitle(fi.baseName());
Track track(0, meta, filename);
emit decoded(track);
}
}
emit done();
Expand Down

0 comments on commit bd0e545

Please sign in to comment.