-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
259 lines (209 loc) · 9.08 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
248
249
250
251
252
253
254
255
256
257
258
259
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent){
mediaPlayer = new QMediaPlayer(this);
mediaPlayer->setAudioRole(QAudio::MusicRole);
mediaPlaylist = new QMediaPlaylist(this);
mediaPlayer->setPlaylist(mediaPlaylist);
mediaButtons = new MediaButtons(this);
combinedLayout = new QWidget(this);
repeat = false;
//Menu Bar creation
open = new QAction(tr("Load Playlist"),this);
save = new QAction(tr("Save Playlist"),this);
add = new QAction(tr("Add Song"),this);
remove = new QAction(tr("Remove Song"),this);
clear = new QAction(tr("Clear Songs"),this);
connect(open,SIGNAL(triggered()),this,SLOT(loadPlaylist()));
connect(save,SIGNAL(triggered()),this,SLOT(savePlaylist()));
connect(add,SIGNAL(triggered()),this,SLOT(addSong()));
connect(remove,SIGNAL(triggered()),this,SLOT(removeSong()));
connect(clear,SIGNAL(triggered()),this,SLOT(clearSong()));
file = menuBar()->addMenu("File");
file->addAction(save);
file->addAction(open);
edit = menuBar()->addMenu("Edit");
edit->addAction(add);
edit->addAction(remove);
edit->addAction(clear);
//Menu Bar creation end
//playlist table creation and properties
playlistTable = new QTableWidget(0,4,this);
QStringList labels;
labels << tr("Title") << tr("Artist") <<tr("Album") <<tr("Length");
playlistTable->setHorizontalHeaderLabels(labels);
playlistTable->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
playlistTable->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Stretch);
playlistTable->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch);
playlistTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
playlistTable->setSelectionMode(QAbstractItemView::SingleSelection);
playlistTable->setSelectionBehavior(QAbstractItemView::SelectRows);
playlistTable->verticalHeader()->hide();
//seek slider
seekSlider = new QSlider(Qt::Horizontal,this);
seekSlider->setRange(0,mediaPlayer->duration() / 1000);
//labels for songName & duration
currentSongDuration = new QLabel(this);
currentSongName = new QLabel(this);
currentSongName->setText("");
QFont k;
k.setPixelSize(20);
currentSongName->setFont(k);
/******************Connections**********************/
connect(mediaPlaylist,&QMediaPlaylist::currentIndexChanged,[&](){
playlistTable->selectRow(mediaPlaylist->currentIndex());
updateSongLabel();
});
connect(mediaButtons,&MediaButtons::repeatBegin,[&](){repeat = true;});
connect(mediaButtons,&MediaButtons::repeatEnd,[&](){repeat = false;});
connect(mediaPlayer,&QMediaPlayer::mediaStatusChanged,[&](QMediaPlayer::MediaStatus currentStatus){
if(currentStatus == QMediaPlayer::EndOfMedia && repeat == true){
mediaPlayer->setPosition(0);
mediaPlayer->play();
}
});
connect(mediaPlayer, &QMediaPlayer::stateChanged, mediaButtons, &MediaButtons::setState);
connect(mediaButtons, &MediaButtons::play,[&](){
mediaPlayer->play();
updateSongLabel();
});
connect(mediaButtons, &MediaButtons::pause,mediaPlayer, &QMediaPlayer::pause);
connect(mediaButtons, &MediaButtons::stop,[&](){
mediaPlayer->stop();
currentSongName->setText("");
});
connect(mediaButtons, &MediaButtons::next,mediaPlaylist,&QMediaPlaylist::next);
connect(mediaButtons, &MediaButtons::previous,mediaPlaylist,&QMediaPlaylist::previous);
connect(mediaButtons,&MediaButtons::muteToggle,mediaPlayer,&QMediaPlayer::setMuted);
connect(mediaButtons,&MediaButtons::volumeLevel,mediaPlayer,&QMediaPlayer::setVolume);
connect(seekSlider,&QSlider::sliderMoved,this,&MainWindow::seek);
connect(mediaPlayer,&QMediaPlayer::positionChanged,this,&MainWindow::sliderMoved);
connect(mediaPlayer,&QMediaPlayer::durationChanged,[&](quint64 dur){
seekSlider->setMaximum(dur/1000);
});
connect(playlistTable,&QTableWidget::doubleClicked,[&](const QModelIndex &index){
mediaPlaylist->setCurrentIndex(index.row());
mediaPlayer->play();
});
/*****************Layout creation and arrangement***************/
QHBoxLayout *seekLayout = new QHBoxLayout();
seekLayout->addWidget(seekSlider);
seekLayout->addWidget(currentSongDuration);
QBoxLayout *tableLayout = new QHBoxLayout();
tableLayout->addWidget(playlistTable);
QBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setMargin(0);
buttonLayout->addWidget(mediaButtons);
buttonLayout->addStretch(1);
buttonLayout->addWidget(currentSongName);
QBoxLayout *layoutCombination = new QVBoxLayout();
layoutCombination->addLayout(buttonLayout);
layoutCombination->addLayout(seekLayout);
layoutCombination->addLayout(tableLayout);
combinedLayout->setLayout(layoutCombination);
setCentralWidget(combinedLayout);
playlistTable->setFocus();
}
MainWindow::~MainWindow(){}
void MainWindow::sliderMoved(quint64 pos){
if(!seekSlider->isSliderDown())
seekSlider->setValue(pos/1000);
quint64 currentInfo = pos/1000;
currentSongDuration->setText(formatIntoTime(currentInfo));
playlistTable->setFocus();
}
void MainWindow::updateSongLabel(){
if(mediaPlaylist->currentIndex() != -1)
currentSongName->setText(playlistTable->item(mediaPlaylist->currentIndex(),0)->text());
else
currentSongName->setText("");
}
//sets position of song according to where seek slider is moved
void MainWindow::seek(int seconds){
mediaPlayer->setPosition(seconds * 1000);
}
//Polls for keypress event
void MainWindow::keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_Return){
mediaPlaylist->setCurrentIndex(playlistTable->currentRow());
mediaPlayer->play();
}
}
//opens chosen textfile located in Playlist folder and adds contents to mediaplaylist and table
void MainWindow::loadPlaylist(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Load File"),"Playlists",tr("Text files (*.txt)"));
QFile inputFile(fileName);
QFileInfo f;
if (inputFile.open(QIODevice::ReadOnly)){
QTextStream in(&inputFile);
in.setCodec("UTF-8");
while (!in.atEnd()){
QString line = in.readLine();
f.setFile(line);
mediaPlaylist->addMedia(QUrl(f.absoluteFilePath()));
parseMetadata(line);
}
inputFile.close();
playlistTable->selectRow(mediaPlaylist->currentIndex() + 1);
}
playlistTable->setFocus();
}
//Opens dialog and saves playlist to playlist folder in UTF-8 format
void MainWindow::savePlaylist(){
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"Playlists",tr("Text files (*.txt)"));
QFile f(fileName);
if(f.open(QIODevice::WriteOnly)){
QTextStream stream(&f);
stream.setCodec("UTF-8");
for(int i = 0 ; i < mediaPlaylist->mediaCount();i++){
stream<<mediaPlaylist->media(i).canonicalUrl().path()<<endl;
}
}
}
//Opens dialog to open X amount of media files and add them to playlist & table
void MainWindow::addSong(){
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"),"",tr("Audio files (*.mp3 *.flac *.ogg)"));
if(files.empty())
return;
for(int i = 0; i < files.count();i++){
QFileInfo f(files.at(i));
mediaPlaylist->addMedia(QUrl(f.absoluteFilePath()));
parseMetadata(files.at(i));
}
playlistTable->setFocus();
}
//Removes selected index from mediaPlaylist & table
void MainWindow::removeSong(){
int index = playlistTable->currentRow();
mediaPlaylist->removeMedia(index);
playlistTable->removeRow(index);
updateSongLabel();
}
//Removes all songs from mediaPlaylist and table
void MainWindow::clearSong(){
mediaPlaylist->clear();
updateSongLabel();
//use setrowcount over clear, since clear removes header labels.
playlistTable->setRowCount(0);
}
//Formats input from seekSlider and parser and returns formatted string.
QString MainWindow::formatIntoTime(quint64 input)
{
//HH:MM:SS
QTime currentTime((input / 3600) % 60, (input / 60) % 60,input % 60);
QString format = "mm:ss";
QString outputString = currentTime.toString(format);
return outputString;
}
//Parses metadata and inserts parsed data into row of playlistTable
//If a nonvalid filepath is passed program crashes, need to assert correct filepath?
void MainWindow::parseMetadata(QString s){
int currentRow = playlistTable->rowCount();
playlistTable->insertRow(currentRow);
TagLib::FileRef fileRef( reinterpret_cast<const wchar_t*>(s.constData()) );
qint64 seconds = fileRef.audioProperties()->lengthInSeconds();
playlistTable->setItem(currentRow,0,new QTableWidgetItem(fileRef.tag()->title().toCString()));
playlistTable->setItem(currentRow,1,new QTableWidgetItem(fileRef.tag()->artist().toCString()));
playlistTable->setItem(currentRow,2,new QTableWidgetItem(fileRef.tag()->album().toCString()));
playlistTable->setItem(currentRow,3,new QTableWidgetItem(formatIntoTime(seconds)));
}