/
mainwindow.cpp
79 lines (64 loc) · 2.26 KB
/
mainwindow.cpp
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
34
35
36
37
38
39
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
#include <iostream>
#include "mainwindow.h"
// how many "steps" the slider has; e.g. it ranges from 0 to this number
#define SLIDER_RESOLUTION 10000
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) {
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
layout = new QVBoxLayout(centralWidget);
treeView = new QTreeView;
layout->addWidget(treeView);
nowPlaying = new NowPlaying;
layout->addWidget(nowPlaying);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, SLIDER_RESOLUTION);
layout->addWidget(slider);
model = new MusicModel(treeView);
treeView->setModel(model);
treeView->setColumnWidth(0, 350);
try {
player = new Player();
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
connect(treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(playSong(QModelIndex)));
connect(player, SIGNAL(endReached()), this, SLOT(playNext()));
connect(player, SIGNAL(positionChanged(float)), this, SLOT(songPositionChanged(float)));
connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(sliderPositionChanged(int)));
this->setMinimumHeight(600);
this->setMinimumWidth(800);
}
MainWindow::~MainWindow() {
delete player;
}
void MainWindow::playSong(QModelIndex index) {
if (!index.isValid()) {
currentIndex = QModelIndex();
currentSong = nullptr;
nowPlaying->setInfo(nullptr);
return;
}
currentIndex = index;
currentSong = model->qModelIndexToSong(index);
try {
player->openFile(currentSong->path);
player->play();
nowPlaying->setInfo(currentSong);
} catch (std::exception& e) {
std::cerr << currentSong->path.toStdString() << ": " << e.what() << std::endl;
}
}
void MainWindow::songPositionChanged(float newPosition) {
// don't change the position of the slider while the user is adjusting it
if (!slider->isSliderDown()) {
slider->setValue(newPosition * SLIDER_RESOLUTION);
}
}
void MainWindow::sliderPositionChanged(int newPosition) {
player->setPosition((float) newPosition / SLIDER_RESOLUTION);
}
void MainWindow::playNext() {
QModelIndex nextIndex = model->index(currentIndex.row() + 1, 0);
playSong(nextIndex);
}