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
|
#include <QComboBox>
#include <QGroupBox>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QSlider>
#include <QTreeView>
#include <QSplitter>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QSortFilterProxyModel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "playerwidget.h"
PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent){
setupGui();
}
void PlayerWidget::setupGui(){
//filter
QGroupBox *filterGB = new QGroupBox(tr("Filter"));
mFilter = new QLineEdit;
connect(mFilter, SIGNAL(returnPressed()), this, SLOT(doFilter()));
QPushButton *filterB = new QPushButton(tr("Go!"));
connect(filterB, SIGNAL(clicked()), this, SLOT(doFilter()));
QHBoxLayout *filterLayout = new QHBoxLayout;
filterLayout->addWidget(mFilter);
filterLayout->addWidget(filterB);
filterGB->setLayout(filterLayout);
//show by
QGroupBox *sortByGB = new QGroupBox(tr("Show by"));
mSortBy = new QComboBox;
mSortBy->addItems(QStringList() << tr("Artists") << tr("Albums") << tr("Genres"));
connect(mSortBy, SIGNAL(currentIndexChanged(QString)), this, SLOT(populateBy(QString)));
QPushButton *refreshSortB = new QPushButton(tr("Refresh"));
connect(refreshSortB, SIGNAL(clicked()), this, SLOT(refreshSort()));
QHBoxLayout *sortByL = new QHBoxLayout;
sortByL->addWidget(mSortBy);
sortByL->addWidget(refreshSortB);
sortByL->setStretchFactor(mSortBy, 3);
sortByGB->setLayout(sortByL);
//THE view
mView = new QTreeView;
mViewModel = new QStandardItemModel;
mViewProxy = new QSortFilterProxyModel;
mViewProxy->setSourceModel(mViewModel);
mView->setModel(mViewProxy);
//left widget
QWidget *leftWidget = new QWidget;
QVBoxLayout *leftWidgetL = new QVBoxLayout;
leftWidgetL->addWidget(sortByGB);
leftWidgetL->addWidget(filterGB);
leftWidgetL->addWidget(mView);
leftWidget->setLayout(leftWidgetL);
//now playing label
mNowPlayingL = new QLabel;
mNowPlayingL->setAlignment(Qt::AlignCenter);
mNowPlayingL->setFont(QFont("courier new", 20, QFont::Bold));
mNowPlayingL->setText(tr("(none)"));
//slider
mSlider = new QSlider;
mSlider->setOrientation(Qt::Horizontal);
//current info
QGroupBox *currentInfoGB = new QGroupBox(tr("Current"));
mCurrentTE = new QTextEdit;
mCurrentTE->setReadOnly(true);
QVBoxLayout *currentInfoL = new QVBoxLayout;
currentInfoL->addWidget(mCurrentTE);
currentInfoGB->setLayout(currentInfoL);
//center widget
QWidget *centerWidget = new QWidget;
QVBoxLayout *centerWidgetL = new QVBoxLayout;
centerWidgetL->addWidget(mNowPlayingL);
centerWidgetL->addWidget(mSlider);
centerWidgetL->addWidget(currentInfoGB);
centerWidget->setLayout(centerWidgetL);
//playlist
mPlayListModel = new QStandardItemModel;
mPlayListView = new QTreeView;
mPlayListView->setModel(mPlayListModel);
QGroupBox *playListGB = new QGroupBox(tr("Playlist"));
QVBoxLayout *playListL = new QVBoxLayout;
playListL->addWidget(mPlayListView);
playListGB->setLayout(playListL);
//right widget
QWidget *rightWidget = new QWidget;
QVBoxLayout *rightWidgetL = new QVBoxLayout;
rightWidgetL->addWidget(playListGB);
rightWidget->setLayout(rightWidgetL);
//put it all together
QSplitter *splitter = new QSplitter;
splitter->addWidget(leftWidget);
splitter->addWidget(centerWidget);
splitter->addWidget(rightWidget);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(splitter);
setLayout(mainLayout);
refreshSort();
}
void PlayerWidget::populateBy(QString selector){
if(selector == tr("Artists")){
populateByArtist();
}
}
void PlayerWidget::refreshSort(){
populateBy(mSortBy->currentText());
}
void PlayerWidget::populateByArtist(){
//prepare Queries
QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
QSqlQuery artistsQ(db);
artistsQ.prepare("SELECT iartists_id, tartists_name FROM artists ORDER BY tartists_name ASC");
QSqlQuery albumQ(db);
albumQ.prepare("SELECT DISTINCT(songs.ialbums_id), talbum_name, siyear FROM songs, albums WHERE songs.iartists_id = :artistid AND songs.ialbums_id = albums.ialbums_id ORDER BY siyear ASC");
QSqlQuery songQ(db);
songQ.prepare("SELECT sipos, ttitle, tfullpath FROM songs WHERE ialbums_id = :alid AND iartists_id = :arid ORDER BY sipos ASC");
//reset view+model
mView->setSortingEnabled(false);
mViewModel->clear();
mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Name"));
QStandardItem *root = mViewModel->invisibleRootItem();
//read data
artistsQ.exec();
while(artistsQ.next()){
QStandardItem *curArtist = new QStandardItem;
curArtist->setFont(QFont("courier"));
QString artistText = QString(tr("%1 %2")).arg(QString("🅐")).arg(artistsQ.value(1).toString()); //I guess this will bite me!
curArtist->setText(artistText);
curArtist->setData(Artist, TypeRole);
curArtist->setData(artistsQ.value(0).toInt(), IdRole);
root->appendRow(curArtist);
albumQ.bindValue(":artistid", artistsQ.value(0));
albumQ.exec();
while(albumQ.next()){
QStandardItem *curAlbum = new QStandardItem;
curAlbum->setFont(QFont("courier"));
QString albumText = QString(tr("%1 %2 - %3")).arg(QChar(0x29BE)).arg(QString::number(albumQ.value(2).toInt())).arg(albumQ.value(1).toString());
curAlbum->setText(albumText);
curAlbum->setData(Album, TypeRole);
curAlbum->setData(albumQ.value(0), IdRole);
curArtist->appendRow(curAlbum);
songQ.bindValue(":alid", albumQ.value(0));
songQ.bindValue(":arid", artistsQ.value(0));
songQ.exec();
while(songQ.next()){
QStandardItem *curSong = new QStandardItem;
curSong->setFont(QFont("courier"));
QString songText = QString(tr("%1 %2 - %3")).arg(QChar(0x266C)).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString());
curSong->setText(songText);
curSong->setData(Song, TypeRole);
curSong->setData(songQ.value(0), IdRole);
curSong->setData(songQ.value(2), FullPathRole);
curAlbum->appendRow(curSong);
}
}
}
}
|