Skip to content

Commit

Permalink
Added iPod-like behavior to previous button
Browse files Browse the repository at this point in the history
  • Loading branch information
asiviero committed Jan 6, 2015
1 parent a61eac1 commit 1120f97
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QSortFilterProxyModel>
#include <QtDebug>
#include <QtConcurrentRun>
#include <QSettings>

#include "config.h"
#include "core/application.h"
Expand All @@ -50,6 +51,8 @@

using std::shared_ptr;

const char* Player::kSettingsGroup = "Player";

Player::Player(Application* app, QObject* parent)
: PlayerInterface(parent),
app_(app),
Expand All @@ -58,7 +61,8 @@ Player::Player(Application* app, QObject* parent)
stream_change_type_(Engine::First),
last_state_(Engine::Empty),
nb_errors_received_(0),
volume_before_mute_(50) {
volume_before_mute_(50),
last_pressed_previous_(QDateTime::currentDateTime()) {
settings_.beginGroup("Player");

SetVolume(settings_.value("volume", 50).toInt());
Expand Down Expand Up @@ -90,7 +94,16 @@ void Player::Init() {
#endif
}

void Player::ReloadSettings() { engine_->ReloadSettings(); }
void Player::ReloadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);

menu_previousmode_ = PreviousBehaviour(
s.value("menu_previousmode", PreviousBehaviour_DontRestart).toInt());
s.endGroup();

engine_->ReloadSettings();
}

void Player::HandleLoadResult(const UrlHandler::LoadResult& result) {
switch (result.type_) {
Expand Down Expand Up @@ -290,6 +303,18 @@ void Player::Previous() { PreviousItem(Engine::Manual); }
void Player::PreviousItem(Engine::TrackChangeFlags change) {
const bool ignore_repeat_track = change & Engine::Manual;

if (menu_previousmode_ == PreviousBehaviour_Restart) {
// Check if it has been over two seconds since previous button was pressed
QDateTime now = QDateTime::currentDateTime();
if (last_pressed_previous_.isValid() &&
last_pressed_previous_.secsTo(now) >= 2) {
last_pressed_previous_ = now;
PlayAt(app_->playlist_manager()->active()->current_row(), change, false);
return;
}
last_pressed_previous_ = now;
}

int i = app_->playlist_manager()->active()->previous_row(ignore_repeat_track);
app_->playlist_manager()->active()->set_current_row(i);
if (i == -1) {
Expand Down
14 changes: 13 additions & 1 deletion src/core/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <QObject>
#include <QSettings>
#include <QDateTime>

#include "config.h"
#include "core/song.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ class PlayerInterface : public QObject {
virtual void Play() = 0;
virtual void ShowOSD() = 0;

signals:
signals:
void Playing();
void Paused();
void Stopped();
Expand Down Expand Up @@ -119,6 +120,14 @@ class Player : public PlayerInterface {
explicit Player(Application* app, QObject* parent = nullptr);
~Player();

static const char* kSettingsGroup;

// Don't change the values
enum PreviousBehaviour {
PreviousBehaviour_DontRestart = 1,
PreviousBehaviour_Restart = 2
};

void Init();

EngineBase* engine() const { return engine_.get(); }
Expand Down Expand Up @@ -197,6 +206,9 @@ class Player : public PlayerInterface {
QUrl loading_async_;

int volume_before_mute_;

QDateTime last_pressed_previous_;
PreviousBehaviour menu_previousmode_;
};

#endif // CORE_PLAYER_H_
12 changes: 12 additions & 0 deletions src/ui/behavioursettingspage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "behavioursettingspage.h"
#include "mainwindow.h"
#include "core/player.h"
#include "ui_behavioursettingspage.h"
#include "playlist/playlist.h"
#include "playlist/playlisttabbar.h"
Expand Down Expand Up @@ -50,6 +51,9 @@ BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog* dialog)
ui_->menu_playmode->setItemData(1, MainWindow::PlayBehaviour_IfStopped);
ui_->menu_playmode->setItemData(2, MainWindow::PlayBehaviour_Always);

ui_->menu_previousmode->setItemData(0, Player::PreviousBehaviour_DontRestart);
ui_->menu_previousmode->setItemData(1, Player::PreviousBehaviour_Restart);

// Populate the language combo box. We do this by looking at all the
// compiled in translations.
QDir dir(":/translations/");
Expand Down Expand Up @@ -180,6 +184,10 @@ void BehaviourSettingsPage::Save() {
MainWindow::PlayBehaviour menu_playmode = MainWindow::PlayBehaviour(
ui_->menu_playmode->itemData(ui_->menu_playmode->currentIndex()).toInt());

Player::PreviousBehaviour menu_previousmode = Player::PreviousBehaviour(
ui_->menu_previousmode->itemData(ui_->menu_previousmode->currentIndex())
.toInt());

Playlist::Path path = Playlist::Path_Automatic;
if (ui_->b_automatic_path->isChecked()) {
path = Playlist::Path_Automatic;
Expand All @@ -202,6 +210,10 @@ void BehaviourSettingsPage::Save() {
ui_->resume_after_start_->isChecked());
s.endGroup();

s.beginGroup(Player::kSettingsGroup);
s.setValue("menu_previousmode", menu_previousmode);
s.endGroup();

s.beginGroup("General");
s.setValue("language", language_map_.contains(ui_->language->currentText())
? language_map_[ui_->language->currentText()]
Expand Down
26 changes: 26 additions & 0 deletions src/ui/behavioursettingspage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_previous">
<property name="title">
<string>Pressing "Previous" in player will...</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_18">
<item>
<widget class="QComboBox" name="menu_previousmode">
<property name="currentIndex">
<number>0</number>
</property>
<item>
<property name="text">
<string>Jump to previous song right away</string>
</property>
</item>
<item>
<property name="text">
<string>Restart song, then jump to previous if pressed again (iPod behavior)</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_7">
<property name="title">
Expand Down
1 change: 1 addition & 0 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ MainWindow::MainWindow(Application* app, SystemTrayIcon* tray_icon, OSD* osd,
int volume = app_->player()->GetVolume();
ui_->volume->setValue(volume);
VolumeChanged(volume);
app_->player()->ReloadSettings();

// Initialise the global search widget
StyleHelper::setBaseColor(palette().color(QPalette::Highlight).darker());
Expand Down

0 comments on commit 1120f97

Please sign in to comment.