forked from deenbee/lastbeatradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
247 lines (207 loc) · 7.17 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
197
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileSystemModel"
#include "QTreeWidget"
#include "QDateTime"
#include "QDebug"
#include "QFileDialog"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
init();
go_up = false;
stopaftercurrent = false;
total_length = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init(){
//get current date
QDate date = QDate::currentDate();
QString dateString = date.toString("dddd, d 'de' MMMM 'de' yyyy");
ui->label_date->setText(dateString);
//get current time and clock start
clock = new DigitalClock();
connect(clock,SIGNAL(updateTime(QString)),this,SLOT(updateClockLabel(QString)));
AudioPlayer audioplayer;
QStringList playlist_names;
treeView_tracks = new TrackListView(this);
ui->verticalLayout_6->addWidget(treeView_tracks);
browseview = new BrowseView(this);
ui->verticalLayout_browsemusic->addWidget(browseview);
createActions();
}
void MainWindow::createActions()
{
connect(&audioplayer,SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&audioplayer,SIGNAL(update_current_track(int)), this, SLOT(currentMediaChanged(int)));
connect(treeView_tracks,SIGNAL(add_drop_track(QString)),this, SLOT(add_drop_track(QString)));
}
void MainWindow::updateClockLabel(QString text_time)
{
// QString text = "hh:mm:ss";
ui->label_clock->setText(text_time);
}
void MainWindow::on_pushButton_AddFile_clicked()
{
QMediaResource mediaresource;
if(playlist_names.empty()){
playlist_names = QFileDialog::getOpenFileNames(this, tr("Audio"),QDir::rootPath(),"Audio (*.mp3 *.wav *.aiff *.flac *.ogg *.wma)");
for (int i = 0; i < playlist_names.size(); ++i){
QString file = playlist_names.at(i);
mediaresource = QMediaResource(QUrl(file));
audioplayer.addMedia(file);
treeView_tracks->add_track(file,i);
}
audioplayer.setCurrentIndex(0);
}
else{
QStringList aux = QFileDialog::getOpenFileNames(this, tr("Audio"),QDir::rootPath(),"Audio (*.mp3 *.wav *.aiff *.flac *.ogg *.wma))");
int prev_numtracks = playlist_names.count();
playlist_names = playlist_names+aux;
for (int i = 0; i < aux.size(); ++i){
QString file = aux.at(i);
audioplayer.addMedia(file);
treeView_tracks->add_track(file,i+prev_numtracks);
}
}
calculate_total_length();
}
void MainWindow::add_drop_track(QString filename){
playlist_names.append(filename);
audioplayer.addMedia(filename);
}
void MainWindow::on_pushButton_RemoveFile_clicked()
{
if(!playlist_names.empty()){
playlist_names.removeAt(treeView_tracks->currentIndex().row());
audioplayer.removeMedia(treeView_tracks->currentIndex().row());
treeView_tracks->remove_track();
calculate_total_length();
}
}
void MainWindow::on_pushButton_Stop_clicked()
{
if (ui->pushButton_Play->isChecked()){
ui->pushButton_Play->toggle();
}
audioplayer.stop();
}
void MainWindow::on_pushButton_Play_toggled(bool checked)
{
if(checked){
audioplayer.setVolume(ui->dial_volume->value());
audioplayer.play();
}
else{
audioplayer.pause();
}
}
void MainWindow::positionChanged(qint64 time){
ui->horizontalSlider_audio_track_position->setValue(time);
ui->label_audio_time_position->setText(audioplayer.getPosition());
ui->label_remain_duration->setText(audioplayer.getRemainingTime());
}
void MainWindow::currentMediaChanged(int index){
if(stopaftercurrent){
on_pushButton_Stop_clicked();
ui->pushButton_stopaftercurrent->setChecked(false);
stopaftercurrent = false;
clear_mark_tracks();
}
else{
if(index > -1){
QString track = playlist_names.at(index);
ui->label_remain_duration->setText(audioplayer.getDuration_formated());
ui->horizontalSlider_audio_track_position->setMaximum(audioplayer.getDuration());
if(index>0){
treeView_tracks->changeRowColor(index-1,Qt::white);
}
if(go_up & index+2 < playlist_names.count()){
go_up = false;
treeView_tracks->changeRowColor(index+2,Qt::white);
}
treeView_tracks->changeRowColor(index,Qt::green);
QString name = track.remove(0,track.lastIndexOf("/")+1);
ui->lineEdit_playing_track->setText(name.remove(track.lastIndexOf("."),track.size()));
if(audioplayer.getNextIndex()!= -1){
track = playlist_names.at(index+1);
treeView_tracks->changeRowColor(index+1,QColor(100,149,237));
QString name = track.remove(0,track.lastIndexOf("/")+1);
ui->lineEdit_next_track->setText(name.remove(track.lastIndexOf("."),track.size()));
}
else{
ui->lineEdit_next_track->setText("");
}
}
else{
clear_mark_tracks();
ui->lineEdit_playing_track->setText("");
ui->pushButton_Play->setChecked(false);
audioplayer.stop();
}
}
}
void MainWindow::clear_mark_tracks(){
for(int i=0; i<playlist_names.count();i++){
treeView_tracks->changeRowColor(i,Qt::white);
}
}
void MainWindow::on_pushButton_NextTrack_clicked()
{
audioplayer.next_track();
}
void MainWindow::on_pushButton_PreviousTrack_clicked()
{
go_up = true;
audioplayer.previous_track();
}
void MainWindow::on_dial_volume_valueChanged(int value)
{
audioplayer.setVolume(value);
}
void MainWindow::on_actionQuit_triggered()
{
this->close();
}
void MainWindow::calculate_total_length(){
for(int i =0; i<playlist_names.count(); i++){
QString filename = playlist_names.at(i);
#ifdef Q_OS_WIN
const wchar_t * encodedName = reinterpret_cast<const wchar_t*>(filename.utf16());
#else
const char * encodedName = QFile::encodeName(filename).constData();
#endif
TagLib::FileRef fr(encodedName, true, TagLib::AudioProperties::Accurate);
total_length += fr.audioProperties()->length();
}
ui->label_totallength->setText(convert_time(total_length));
}
QString MainWindow::convert_time(qint64 secs){
QString formattedTime;
int hours, minutes, seconds;
seconds = secs;
hours = seconds / (60*60);
seconds %= 60*60;
minutes = seconds / 60;
seconds %= 60;
formattedTime.append(QString("%1").arg(hours, 2, 10, QLatin1Char('0')) + ":" +
QString( "%1" ).arg(minutes, 2, 10, QLatin1Char('0')) + ":" +
QString( "%1" ).arg(seconds, 2, 10, QLatin1Char('0')));
//QString("%1").arg(hours, 2, 10, QLatin1Char('0')) + ":" +
//QString( "%1" ).arg(milliseconds, 3, 10, QLatin1Char('0'))
return formattedTime;
}
void MainWindow::on_horizontalSlider_audio_track_position_sliderMoved(int position)
{
audioplayer.setPosition(position);
}
void MainWindow::on_pushButton_stopaftercurrent_toggled(bool checked)
{
stopaftercurrent = checked;
}