TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicdownloadwidget.cpp
Go to the documentation of this file.
1 #include "musicdownloadwidget.h"
2 #include "ui_musicdownloadwidget.h"
6 #include "musictoastlabel.h"
7 #include "musicwidgetheaders.h"
8 #include "musicrulesanalysis.h"
9 
11  : QWidget(parent)
12 {
13  m_icon = new QLabel(this);
14  m_text = new QLabel(this);
15  m_information = new QLabel(this);
16 
17  m_text->setGeometry(0, 0, 60, TTK_ITEM_SIZE_S);
18  m_icon->setGeometry(70, 0, 30, TTK_ITEM_SIZE_S);
19  m_information->setGeometry(170, 0, 150, TTK_ITEM_SIZE_S);
20 }
21 
23 {
24  delete m_information;
25  delete m_icon;
26  delete m_text;
27 }
28 
30 {
31  m_icon->setPixmap(QPixmap(name).scaled(28, 18));
32 }
33 
34 void MusicDownloadTableItem::setInformation(const QString &info)
35 {
36  m_information->setText(info);
37 }
38 
39 void MusicDownloadTableItem::setText(const QString &text)
40 {
41  m_text->setText(text);
42 }
43 
44 
45 
47  : MusicAbstractTableWidget(parent)
48 {
49  setColumnCount(1);
50 
51  QHeaderView *headerView = horizontalHeader();
52  headerView->resizeSection(0, 400);
53 
54  TTK::Widget::setTransparent(this, 255);
55 }
56 
58 {
59  removeItems();
60 }
61 
62 void MusicDownloadTableWidget::addCellItem(const TTK::MusicSongProperty &prop, const QString &type, const QString &icon)
63 {
64  const int index = rowCount();
65  setRowCount(index + 1);
66  setRowHeight(index, TTK_ITEM_SIZE_S);
67 
68  QTableWidgetItem *it = new QTableWidgetItem;
69  it->setData(TTK_DATA_ROLE, prop.m_bitrate);
70  setItem(index, 0, it);
71 
73  item->setIcon(icon);
74  item->setInformation(QString("%1/%2KBPS/%3").arg(prop.m_size).arg(prop.m_bitrate).arg(prop.m_format.toUpper()));
75  item->setText(type);
76  m_items << item;
77 
78  setCellWidget(index, 0, item);
79 }
80 
82 {
83  const int row = currentRow();
84  if(row == -1)
85  {
86  return TTK_BN_0;
87  }
88 
89  return item(row, 0)->data(TTK_DATA_ROLE).toInt();
90 }
91 
93 {
94  qDeleteAll(m_items);
95  m_items.clear();
97  setColumnCount(1);
98 }
99 
100 
101 
103  : MusicAbstractMoveWidget(parent),
104  m_ui(new Ui::MusicDownloadWidget)
105 {
106  m_ui->setupUi(this);
107  setFixedSize(size());
108  setAttribute(Qt::WA_DeleteOnClose);
109  setBackgroundLabel(m_ui->background);
110 
111  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
112  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
113  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
114  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
115  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
116 
117  m_ui->downloadButton->setStyleSheet(TTK::UI::PushButtonStyle05);
118 #ifdef Q_OS_UNIX
119  m_ui->downloadButton->setFocusPolicy(Qt::NoFocus);
120 #endif
121 
122  m_networkRequest = nullptr;
124 
125  connect(m_ui->downloadButton, SIGNAL(clicked()), SLOT(startToRequest()));
126 
128 }
129 
131 {
132  delete m_ui;
133  delete m_networkRequest;
134 }
135 
137 {
138  m_ui->topTitleCloseButton->setEnabled(enabled);
139  m_ui->downloadButton->setEnabled(enabled);
140 }
141 
143 {
144  const TTK::MusicSongInformationList songInfos(request->items());
145  if(row >= songInfos.count())
146  {
147  return;
148  }
149 
150  m_songInfo = songInfos[row];
151  m_queryType = request->queryType();
152  m_networkRequest = request;
153 
154  m_ui->loadingLabel->run(true);
155  controlEnabled(true);
156  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(m_songInfo.m_artistName, m_songInfo.m_songName), Qt::ElideRight, 200));
157 
159 }
160 
162 {
163  m_queryType = type;
164 
165  if(!m_networkRequest)
166  {
167  m_networkRequest = G_DOWNLOAD_QUERY_PTR->makeQueryRequest(this);
168  connect(m_networkRequest, SIGNAL(downLoadDataChanged(QString)), SLOT(downLoadNormalFinished()));
169  }
170 
171  m_ui->loadingLabel->run(true);
172  controlEnabled(true);
173  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), name, Qt::ElideRight, 200));
174 
177 }
178 
180 {
181  m_songInfo = info;
182  m_queryType = type;
183 
184  m_ui->loadingLabel->run(true);
185  controlEnabled(true);
186  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(info.m_artistName, info.m_songName), Qt::ElideRight, 200));
187 
188  if(m_songInfo.m_songProps.isEmpty())
189  {
190  close();
191  MusicToastLabel::popup(tr("No resource found"));
192  }
193  else
194  {
195  std::sort(m_songInfo.m_songProps.begin(), m_songInfo.m_songProps.end()); //to find out the min bitrate
197  }
198 }
199 
200 bool MusicDownloadWidget::startToRequestMusic(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
201 {
202  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
203  {
204  return false;
205  }
206 
208  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
209  {
210  if(p.m_bitrate == bitrate)
211  {
212  prop = p;
213  break;
214  }
215  }
216 
217  if(prop.isEmpty())
218  {
219  return false;
220  }
221 
222  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
223  QString fileName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
224  QString downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
225 
228  {
229  return false;
230  }
231 
232  MusicSongList records;
233  manager.readBuffer(records);
234 
235  MusicSong record;
236  record.setName(fileName);
237  record.setPath(QFileInfo(downloadPath).absoluteFilePath());
238  record.setSizeStr(prop.m_size);
239  record.setAddTimeStr("-1");
240  records << record;
241 
242  manager.reset();
243  manager.writeBuffer(records);
244 
245  if(QFile::exists(downloadPath))
246  {
247  for(int i = 1; i < 99; ++i)
248  {
249  if(!QFile::exists(downloadPath))
250  {
251  break;
252  }
253 
254  if(i != 1)
255  {
256  fileName.chop(3);
257  }
258 
259  fileName += QString("(%1)").arg(i);
260  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
261  }
262  }
263 
264  MusicDownloadMetaDataRequest *d = new MusicDownloadMetaDataRequest(prop.m_url, downloadPath, parent);
265  connect(d, SIGNAL(downLoadDataChanged(QString)), parent, SLOT(downloadFinished()));
266 
267  MusicSongMeta meta;
268  meta.setComment(info.m_coverUrl);
269  meta.setTitle(info.m_songName);
270  meta.setArtist(info.m_artistName);
271  meta.setAlbum(info.m_albumName);
272  meta.setTrackNum(info.m_trackNumber);
273  meta.setYear(info.m_year);
274 
275  d->setSongMeta(meta);
276  d->startToRequest();
277  return true;
278 }
279 
280 bool MusicDownloadWidget::startToRequestMovie(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
281 {
282  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
283  {
284  return false;
285  }
286 
288  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
289  {
290  if(p.m_bitrate == bitrate)
291  {
292  prop = p;
293  break;
294  }
295  }
296 
297  if(prop.isEmpty())
298  {
299  return false;
300  }
301 
302  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
303  QString fileName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
304  QString downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
305 
306  if(QFile::exists(downloadPath))
307  {
308  for(int i = 1; i < 99; ++i)
309  {
310  if(!QFile::exists(downloadPath))
311  {
312  break;
313  }
314 
315  if(i != 1)
316  {
317  fileName.chop(3);
318  }
319 
320  fileName += QString("(%1)").arg(i);
321  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
322  }
323  }
324 
325  MusicDownloadDataRequest *d = new MusicDownloadDataRequest(prop.m_url, downloadPath, TTK::Download::Video, parent);
326  connect(d, SIGNAL(downLoadDataChanged(QString)), parent, SLOT(downloadFinished()));
327  d->startToRequest();
328  return true;
329 }
330 
332 {
333  if(!G_NETWORK_PTR->isOnline())
334  {
335  return;
336  }
337 
338  m_ui->viewArea->removeItems();
339 
340  const TTK::MusicSongInformationList songInfos(m_networkRequest->items());
341  if(songInfos.isEmpty())
342  {
343  return;
344  }
345 
346  const QString &fileName = m_networkRequest->queryValue();
347  const QString &songName = TTK::generateSongTitle(fileName);
348  const QString &artistName = TTK::generateSongArtist(fileName);
349 
350  for(const TTK::MusicSongInformation &var : qAsConst(songInfos))
351  {
352  if(var.m_artistName.contains(artistName, Qt::CaseInsensitive) && var.m_songName.contains(songName, Qt::CaseInsensitive))
353  {
354  m_songInfo = var;
355  break;
356  }
357  }
358 
360 
361  if(m_songInfo.m_songProps.isEmpty())
362  {
363  close();
364  MusicToastLabel::popup(tr("No resource found"));
365  }
366  else
367  {
368  std::sort(m_songInfo.m_songProps.begin(), m_songInfo.m_songProps.end()); //to find out the min bitrate
370  }
371 }
372 
374 {
376  m_networkRequest = nullptr;
377 
378  if(m_songInfo.m_songProps.isEmpty())
379  {
380  close();
381  MusicToastLabel::popup(tr("No resource found"));
382  }
383  else
384  {
385  std::sort(m_songInfo.m_songProps.begin(), m_songInfo.m_songProps.end()); //to find out the min bitrate
387  }
388 }
389 
390 void MusicDownloadWidget::addCellItems(const TTK::MusicSongPropertyList &props)
391 {
392  for(const TTK::MusicSongProperty &prop : qAsConst(props))
393  {
396  {
397  m_ui->viewArea->addCellItem(prop, QObject::tr("SD"), QString(":/quality/lb_sd_quality"));
398  }
399  else if((prop.m_bitrate == TTK_BN_192 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
401  {
402  m_ui->viewArea->addCellItem(prop, QObject::tr("HQ"), QString(":/quality/lb_hd_quality"));
403  }
404  else if((prop.m_bitrate == TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
406  {
407  m_ui->viewArea->addCellItem(prop, QObject::tr("SQ"), QString(":/quality/lb_sq_quality"));
408  }
409  else if((prop.m_bitrate > TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
411  {
412  m_ui->viewArea->addCellItem(prop, QObject::tr("CD"), QString(":/quality/lb_cd_quality"));
413  }
414  else
415  {
416  continue;
417  }
418  }
419 
420  m_ui->loadingLabel->run(false);
421 
422  int delta = m_ui->viewArea->rowCount();
423  delta = ((delta == 0) ? 0 : (delta - 1) * TTK_ITEM_SIZE_S) - 2 * TTK_ITEM_SIZE_S;
424 
425  setFixedHeightWidget(this, delta);
426  setFixedHeightWidget(m_ui->backgroundMask, delta);
427  setFixedHeightWidget(m_ui->background, delta);
428  setFixedHeightWidget(m_ui->viewArea, delta + 2 * TTK_ITEM_SIZE_S);
429 
430  setMoveWidget(m_ui->label2, delta);
431  setMoveWidget(m_ui->fileNameLabel, delta);
432  setMoveWidget(m_ui->downloadButton, delta);
433 
436 }
437 
438 void MusicDownloadWidget::setFixedHeightWidget(QWidget *w, int height)
439 {
440  w->setFixedHeight(w->height() + height);
441 }
442 
443 void MusicDownloadWidget::setMoveWidget(QWidget *w, int pos)
444 {
445  const QRect &rect = w->geometry();
446  w->move(rect.x(), rect.y() + pos);
447 }
448 
450 {
451  const int bitrate = m_ui->viewArea->bitrate();
452  if(bitrate == TTK_BN_0)
453  {
454  MusicToastLabel::popup(tr("Please select one item first"));
455  return;
456  }
457 
458  hide();
459 
461  {
463  }
465  {
467  }
468  controlEnabled(false);
469 }
470 
472 {
473  Q_EMIT dataDownloadChanged();
474  close();
475 }
void setBackgroundLabel(QLabel *label)
void setInformation(const QString &info)
TTK_MODULE_EXPORT QString generateSongArtist(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:128
static bool startToRequestMovie(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
void setTrackNum(const QString &track) noexcept
The class of the download widget.
void initialize(MusicAbstractQueryRequest *request, int row)
Ui::MusicDownloadWidget * m_ui
virtual bool writeBuffer(const MusicSongList &items) overridefinal
bool fromFile(const QString &name)
The class of the table widget base.
The class of the music song property.
Definition: musicobject.h:223
The class of the download the type of data.
voidpf void uLong size
Definition: ioapi.h:136
void controlEnabled(bool enabled)
static bool startToRequestMusic(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
MusicSongPropertyList m_songProps
Definition: musicobject.h:295
The class of the download record manager.
void addCellItems(const TTK::MusicSongPropertyList &props)
#define TTK_ITEM_SIZE_S
Definition: ttkglobal.h:241
TTK_MODULE_EXPORT QString generateSongTitle(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:117
#define TTK_BN_1000
Definition: ttkglobal.h:354
virtual bool readBuffer(MusicSongList &items) overridefinal
void setFixedHeightWidget(QWidget *w, int height)
#define TTK_BN_250
Definition: ttkglobal.h:350
virtual void startToRequest() overridefinal
MusicDownloadTableWidget(QWidget *parent=nullptr)
void setName(const QString &n) noexcept
Definition: musicsong.h:78
static void popup(const QString &text)
void setYear(const QString &year) noexcept
TTK::MusicSongInformation m_songInfo
void setBackgroundPixmap(const QSize &size)
#define TTK_BN_500
Definition: ttkglobal.h:352
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate)
void setTitle(const QString &title) noexcept
void setAddTimeStr(const QString &t) noexcept
Definition: musicsong.h:61
#define qAsConst
Definition: ttkqtglobal.h:53
const char * name
Definition: http_parser.c:458
MusicAbstractQueryRequest * m_networkRequest
MusicAbstractQueryRequest::QueryType m_queryType
#define TTK_SIGNLE_SHOT(...)
Definition: ttkqtglobal.h:177
TTK_MODULE_EXPORT void setTransparent(QWidget *widget, int alpha)
void setSizeStr(const QString &s) noexcept
Definition: musicsong.h:69
bool isEmpty() const noexcept
Definition: musicobject.h:236
void addCellItem(const TTK::MusicSongProperty &prop, const QString &type, const QString &icon)
TTK_MODULE_EXPORT void adjustWidgetPosition(QWidget *widget)
#define TTK_BN_0
Definition: ttkglobal.h:344
virtual void removeItems() overridefinal
TTK_MODULE_EXPORT QString elidedText(const QFont &font, const QString &text, Qt::TextElideMode mode, int width)
#define TTK_BN_320
Definition: ttkglobal.h:351
#define TTK_SLOT
Definition: ttkqtglobal.h:165
void setIcon(const QString &name)
void setText(const QString &text)
TTK_MODULE_EXPORT QString toString(Record type)
MusicDownloadTableItem(QWidget *parent=nullptr)
#define G_NETWORK_PTR
static const QString PushButtonStyle05
void setComment(const QString &comment) noexcept
static const QString ToolButtonStyle04
#define G_DOWNLOAD_QUERY_PTR
The class of the download the type of data by custom tags.
void setMoveWidget(QWidget *w, int pos)
The class of the music song meta.
Definition: musicsongmeta.h:30
The class of the music song info.
Definition: musicsong.h:28
void setPath(const QString &p) noexcept
Definition: musicsong.h:86
virtual void startToSearch(const QString &value)=0
The class of the abstract query download data from net.
void setAlbum(const QString &album) noexcept
The class of the download table item.
#define TTK_BN_192
Definition: ttkglobal.h:349
const TTK::MusicSongInformationList & items() const
static QString parse(const TTK::MusicSongInformation &info, const QString &value)
virtual void startToRequest() override
void setArtist(const QString &artist) noexcept
MusicDownloadWidget(QWidget *parent=nullptr)
#define TTK_DATA_ROLE
The class of the music song information.
Definition: musicobject.h:281
#define TTK_BN_750
Definition: ttkglobal.h:353
#define TTK_BN_128
Definition: ttkglobal.h:348
#define G_SETTING_PTR
The class of the moving widget base.