TTKMusicPlayer  4.1.3.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();
96 
98  setColumnCount(1);
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  if(!m_networkRequest)
145  {
146  m_networkRequest = G_DOWNLOAD_QUERY_PTR->makeQueryRequest(this);
147  connect(m_networkRequest, SIGNAL(downLoadDataChanged(QString)), SLOT(downLoadNormalFinished()));
148  }
149 
150  m_ui->loadingLabel->run(true);
151  controlEnabled(true);
152  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), name, Qt::ElideRight, 200));
153 
156 }
157 
159 {
160  m_info = info;
162 
163  m_ui->loadingLabel->run(true);
164  controlEnabled(true);
165  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(info.m_artistName, info.m_songName), Qt::ElideRight, 200));
166 
167  if(m_info.m_songProps.isEmpty())
168  {
169  close();
170  MusicToastLabel::popup(tr("No resource found"));
171  }
172  else
173  {
174  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
176  }
177 }
178 
180 {
181  const TTK::MusicSongInformationList &songInfos = request->items();
182  if(row >= songInfos.count())
183  {
184  return;
185  }
186 
187  m_info = songInfos[row];
188  m_queryType = request->queryType();
189  m_networkRequest = request;
190 
191  m_ui->loadingLabel->run(true);
192  controlEnabled(true);
193  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(m_info.m_artistName, m_info.m_songName), Qt::ElideRight, 200));
194 
196 }
197 
198 bool MusicDownloadWidget::startToRequestMusic(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
199 {
200  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
201  {
202  return false;
203  }
204 
206  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
207  {
208  if(p.m_bitrate == bitrate)
209  {
210  prop = p;
211  break;
212  }
213  }
214 
215  if(prop.isEmpty())
216  {
217  return false;
218  }
219 
220  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
221  const QString &baseName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
222 
223  int index = 1;
224  QString downloadPath;
225  QString fileName = baseName;
226 
227  do
228  {
229  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
230  if(!QFile::exists(downloadPath))
231  {
232  break;
233  }
234 
235  fileName = baseName + QString("(%1)").arg(index++);
236  } while(index < 99);
237 
240  {
241  MusicSongList records;
242  manager.readBuffer(records);
243 
244  MusicSong record;
245  record.setName(fileName);
246  record.setPath(QFileInfo(downloadPath).absoluteFilePath());
247  record.setSizeStr(prop.m_size);
248  record.setAddTimeStr("-1");
249  records << record;
250 
251  manager.reset();
252  manager.writeBuffer(records);
253  }
254 
255  MusicDownloadMetaDataRequest *d = new MusicDownloadMetaDataRequest(prop.m_url, downloadPath, parent);
256  connect(d, SIGNAL(downLoadDataChanged(QString)), parent, SLOT(downloadFinished()));
257 
258  MusicSongMeta meta;
259  meta.setComment(info.m_coverUrl);
260  meta.setTitle(info.m_songName);
261  meta.setArtist(info.m_artistName);
262  meta.setAlbum(info.m_albumName);
263  meta.setTrackNum(info.m_trackNumber);
264  meta.setYear(info.m_year);
265 
266  d->setSongMeta(meta);
267  d->startToRequest();
268  return true;
269 }
270 
271 bool MusicDownloadWidget::startToRequestMovie(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
272 {
273  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
274  {
275  return false;
276  }
277 
279  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
280  {
281  if(p.m_bitrate == bitrate)
282  {
283  prop = p;
284  break;
285  }
286  }
287 
288  if(prop.isEmpty())
289  {
290  return false;
291  }
292 
293  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
294  QString fileName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
295  QString downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
296 
297  if(QFile::exists(downloadPath))
298  {
299  for(int i = 1; i < 99; ++i)
300  {
301  if(!QFile::exists(downloadPath))
302  {
303  break;
304  }
305 
306  if(i != 1)
307  {
308  fileName.chop(3);
309  }
310 
311  fileName += QString("(%1)").arg(i);
312  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
313  }
314  }
315 
316  MusicDownloadDataRequest *d = new MusicDownloadDataRequest(prop.m_url, downloadPath, TTK::Download::Video, parent);
317  connect(d, SIGNAL(downLoadDataChanged(QString)), parent, SLOT(downloadFinished()));
318  d->startToRequest();
319  return true;
320 }
321 
323 {
324  if(!G_NETWORK_PTR->isOnline())
325  {
326  return;
327  }
328 
329  m_ui->viewArea->removeItems();
330 
331  const TTK::MusicSongInformationList &songInfos = m_networkRequest->items();
332  if(songInfos.isEmpty())
333  {
334  return;
335  }
336 
337  const QString &fileName = m_networkRequest->queryValue();
338  const QString &songName = TTK::generateSongTitle(fileName);
339  const QString &artistName = TTK::generateSongArtist(fileName);
340 
341  for(const TTK::MusicSongInformation &var : qAsConst(songInfos))
342  {
343  if(var.m_artistName.contains(artistName, Qt::CaseInsensitive) && var.m_songName.contains(songName, Qt::CaseInsensitive))
344  {
345  m_info = var;
346  break;
347  }
348  }
349 
351 
352  if(m_info.m_songProps.isEmpty())
353  {
354  close();
355  MusicToastLabel::popup(tr("No resource found"));
356  }
357  else
358  {
359  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
361  }
362 }
363 
365 {
367  m_networkRequest = nullptr;
368 
369  if(m_info.m_songProps.isEmpty())
370  {
371  close();
372  MusicToastLabel::popup(tr("No resource found"));
373  }
374  else
375  {
376  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
378  }
379 }
380 
381 void MusicDownloadWidget::addCellItems(const TTK::MusicSongPropertyList &props)
382 {
383  for(const TTK::MusicSongProperty &prop : qAsConst(props))
384  {
387  {
388  m_ui->viewArea->addCellItem(prop, QObject::tr("SD"), QString(":/quality/lb_sd_quality"));
389  }
390  else if((prop.m_bitrate == TTK_BN_192 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
392  {
393  m_ui->viewArea->addCellItem(prop, QObject::tr("HQ"), QString(":/quality/lb_hd_quality"));
394  }
395  else if((prop.m_bitrate == TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
397  {
398  m_ui->viewArea->addCellItem(prop, QObject::tr("SQ"), QString(":/quality/lb_sq_quality"));
399  }
400  else if((prop.m_bitrate > TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
402  {
403  m_ui->viewArea->addCellItem(prop, QObject::tr("CD"), QString(":/quality/lb_cd_quality"));
404  }
405  else
406  {
407  continue;
408  }
409  }
410 
411  m_ui->loadingLabel->run(false);
412 
413  int delta = m_ui->viewArea->rowCount();
414  delta = ((delta == 0) ? 0 : (delta - 1) * TTK_ITEM_SIZE_S) - 2 * TTK_ITEM_SIZE_S;
415 
416  setFixedHeightWidget(this, delta);
417  setFixedHeightWidget(m_ui->backgroundMask, delta);
418  setFixedHeightWidget(m_ui->background, delta);
419  setFixedHeightWidget(m_ui->viewArea, delta + 2 * TTK_ITEM_SIZE_S);
420 
421  setMoveWidget(m_ui->label2, delta);
422  setMoveWidget(m_ui->fileNameLabel, delta);
423  setMoveWidget(m_ui->downloadButton, delta);
424 
427 }
428 
429 void MusicDownloadWidget::setFixedHeightWidget(QWidget *w, int height)
430 {
431  w->setFixedHeight(w->height() + height);
432 }
433 
434 void MusicDownloadWidget::setMoveWidget(QWidget *w, int pos)
435 {
436  const QRect &rect = w->geometry();
437  w->move(rect.x(), rect.y() + pos);
438 }
439 
441 {
442  const int bitrate = m_ui->viewArea->bitrate();
443  if(bitrate == TTK_BN_0)
444  {
445  MusicToastLabel::popup(tr("Please select one item first"));
446  return;
447  }
448 
449  hide();
450 
452  {
454  }
456  {
458  }
459  controlEnabled(false);
460 }
461 
463 {
464  Q_EMIT dataDownloadChanged();
465  close();
466 }
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:121
static bool startToRequestMovie(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
void setTrackNum(const QString &track) noexcept
The class of the download widget.
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:225
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:297
The class of the download record manager.
void addCellItems(const TTK::MusicSongPropertyList &props)
#define TTK_ITEM_SIZE_S
Definition: ttkglobal.h:246
TTK_MODULE_EXPORT QString generateSongTitle(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:110
#define TTK_BN_1000
Definition: ttkglobal.h:359
virtual bool readBuffer(MusicSongList &items) overridefinal
void setFixedHeightWidget(QWidget *w, int height)
#define TTK_BN_250
Definition: ttkglobal.h:355
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
void initialize(const QString &name)
void setBackgroundPixmap(const QSize &size)
#define TTK_BN_500
Definition: ttkglobal.h:357
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:51
const char * name
Definition: http_parser.c:458
MusicAbstractQueryRequest * m_networkRequest
MusicAbstractQueryRequest::QueryType m_queryType
#define TTK_SIGNLE_SHOT(...)
Definition: ttkqtglobal.h:193
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:238
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:349
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:356
#define TTK_SLOT
Definition: ttkqtglobal.h:181
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:354
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)
TTK::MusicSongInformation m_info
#define TTK_DATA_ROLE
The class of the music song information.
Definition: musicobject.h:283
#define TTK_BN_750
Definition: ttkglobal.h:358
#define TTK_BN_128
Definition: ttkglobal.h:353
#define G_SETTING_PTR
The class of the moving widget base.