TTKMusicPlayer  4.3.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(160, 0, 160, 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_exitRequired = false;
123  m_networkRequest = nullptr;
125 
126  connect(m_ui->downloadButton, SIGNAL(clicked()), SLOT(startToRequest()));
127 
129 }
130 
132 {
133  delete m_ui;
134  delete m_networkRequest;
135 }
136 
138 {
139  m_ui->topTitleCloseButton->setEnabled(enabled);
140  m_ui->downloadButton->setEnabled(enabled);
141 }
142 
144 {
145  if(!m_networkRequest)
146  {
147  m_networkRequest = G_DOWNLOAD_QUERY_PTR->makeSongRequest(this);
148  connect(m_networkRequest, SIGNAL(downloadDataChanged(QString)), SLOT(downloadNormalFinished()));
149  }
150 
151  m_ui->loadingLabel->execute(true);
152  controlEnabled(true);
153  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), name, Qt::ElideRight, 200));
154 
157 }
158 
160 {
161  m_info = info;
163 
164  m_ui->loadingLabel->execute(true);
165  controlEnabled(true);
166  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(info.m_artistName, info.m_songName), Qt::ElideRight, 200));
167 
168  if(m_info.m_songProps.isEmpty())
169  {
170  MusicAbstractMoveWidget::close();
171  MusicToastLabel::popup(tr("No resource found"));
172  }
173  else
174  {
175  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
177  }
178 }
179 
181 {
182  const TTK::MusicSongInformationList &songInfos = request->items();
183  if(row >= songInfos.count())
184  {
185  return;
186  }
187 
188  m_info = songInfos[row];
189  m_queryType = request->queryType();
190  m_networkRequest = request;
191 
192  m_ui->loadingLabel->execute(true);
193  controlEnabled(true);
194  m_ui->downloadName->setText(TTK::Widget::elidedText(font(), QString("%1 - %2").arg(m_info.m_artistName, m_info.m_songName), Qt::ElideRight, 200));
195 
197 }
198 
199 bool MusicDownloadWidget::startToRequestMusic(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
200 {
201  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
202  {
203  return false;
204  }
205 
207  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
208  {
209  if(p.m_bitrate == bitrate)
210  {
211  prop = p;
212  break;
213  }
214  }
215 
216  if(prop.isEmpty())
217  {
218  return false;
219  }
220 
221  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
222  const QString &baseName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
223 
224  int index = 1;
225  QString downloadPath;
226  QString fileName = baseName;
227 
228  do
229  {
230  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
231  if(!QFile::exists(downloadPath))
232  {
233  break;
234  }
235 
236  fileName = baseName + QString("(%1)").arg(index++);
237  } while(index < 99);
238 
241  {
242  MusicSongList records;
243  manager.readBuffer(records);
244 
245  MusicSong record;
246  record.setName(fileName);
247  record.setPath(QFileInfo(downloadPath).absoluteFilePath());
248  record.setSizeStr(prop.m_size);
249  record.setAddTimeStr("-1");
250  records << record;
251 
252  manager.reset();
253  manager.writeBuffer(records);
254  }
255 
256  MusicDownloadMetaDataRequest *req = new MusicDownloadMetaDataRequest(prop.m_url, downloadPath, parent);
257  connect(req, SIGNAL(downloadDataChanged(QString)), parent, SLOT(downloadFinished()));
258 
259  MusicSongMeta meta;
260  meta.setComment(info.m_coverUrl);
261  meta.setTitle(info.m_songName);
262  meta.setArtist(info.m_artistName);
263  meta.setAlbum(info.m_albumName);
264  meta.setTrackNum(info.m_trackNumber);
265  meta.setYear(info.m_year);
266 
267  req->setSongMeta(meta);
268  req->startToRequest();
269  return true;
270 }
271 
272 bool MusicDownloadWidget::startToRequestMovie(const TTK::MusicSongInformation &info, int bitrate, QObject *parent)
273 {
274  if(!G_NETWORK_PTR->isOnline() || info.m_songProps.isEmpty())
275  {
276  return false;
277  }
278 
280  for(const TTK::MusicSongProperty &p : qAsConst(info.m_songProps))
281  {
282  if(p.m_bitrate == bitrate)
283  {
284  prop = p;
285  break;
286  }
287  }
288 
289  if(prop.isEmpty())
290  {
291  return false;
292  }
293 
294  const QString &downloadPrefix = G_SETTING_PTR->value(MusicSettingManager::DownloadMusicDirPath).toString();
295  QString fileName = MusicRulesAnalysis::parse(info, G_SETTING_PTR->value(MusicSettingManager::DownloadFileNameRule).toString());
296  QString downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
297 
298  if(QFile::exists(downloadPath))
299  {
300  for(int i = 1; i < 99; ++i)
301  {
302  if(!QFile::exists(downloadPath))
303  {
304  break;
305  }
306 
307  if(i != 1)
308  {
309  fileName.chop(3);
310  }
311 
312  fileName += QString("(%1)").arg(i);
313  downloadPath = QString("%1%2.%3").arg(downloadPrefix, fileName, prop.m_format);
314  }
315  }
316 
317  MusicDownloadDataRequest *req = new MusicDownloadDataRequest(prop.m_url, downloadPath, TTK::Download::Video, parent);
318  connect(req, SIGNAL(downloadDataChanged(QString)), parent, SLOT(downloadFinished()));
319  req->startToRequest();
320  return true;
321 }
322 
324 {
325  hide();
326  m_exitRequired = true;
327 }
328 
330 {
331  if(!G_NETWORK_PTR->isOnline())
332  {
333  return;
334  }
335 
336  m_ui->viewArea->removeItems();
337 
338  const TTK::MusicSongInformationList &songInfos = m_networkRequest->items();
339  if(songInfos.isEmpty())
340  {
341  return;
342  }
343 
344  const QString &fileName = m_networkRequest->queryValue();
345  const QString &songName = TTK::generateSongTitle(fileName);
346  const QString &artistName = TTK::generateSongArtist(fileName);
347 
348  for(const TTK::MusicSongInformation &var : qAsConst(songInfos))
349  {
350  if(var.m_artistName.contains(artistName, Qt::CaseInsensitive) && var.m_songName.contains(songName, Qt::CaseInsensitive))
351  {
352  m_info = var;
353  break;
354  }
355  }
356 
358 
359  if(m_info.m_songProps.isEmpty())
360  {
361  MusicAbstractMoveWidget::close();
362  MusicToastLabel::popup(tr("No resource found"));
363  }
364  else
365  {
366  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
368  }
369 
370  if(m_exitRequired)
371  {
372  MusicAbstractMoveWidget::close();
373  }
374 }
375 
377 {
379  m_networkRequest = nullptr;
380 
381  if(m_info.m_songProps.isEmpty())
382  {
383  MusicAbstractMoveWidget::close();
384  MusicToastLabel::popup(tr("No resource found"));
385  }
386  else
387  {
388  std::sort(m_info.m_songProps.begin(), m_info.m_songProps.end()); //to find out the min bitrate
390  }
391 
392  if(m_exitRequired)
393  {
394  MusicAbstractMoveWidget::close();
395  }
396 }
397 
398 void MusicDownloadWidget::addCellItems(const TTK::MusicSongPropertyList &props)
399 {
400  for(const TTK::MusicSongProperty &prop : qAsConst(props))
401  {
404  {
405  m_ui->viewArea->addCellItem(prop, QObject::tr("SD"), QString(":/quality/lb_sd_quality"));
406  }
407  else if((prop.m_bitrate == TTK_BN_192 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
409  {
410  m_ui->viewArea->addCellItem(prop, QObject::tr("HQ"), QString(":/quality/lb_hd_quality"));
411  }
412  else if((prop.m_bitrate == TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
414  {
415  m_ui->viewArea->addCellItem(prop, QObject::tr("SQ"), QString(":/quality/lb_sq_quality"));
416  }
417  else if((prop.m_bitrate > TTK_BN_320 && m_queryType == MusicAbstractQueryRequest::QueryType::Music) ||
419  {
420  m_ui->viewArea->addCellItem(prop, QObject::tr("CD"), QString(":/quality/lb_cd_quality"));
421  }
422  else
423  {
424  continue;
425  }
426  }
427 
428  m_ui->loadingLabel->execute(false);
429 
430  int delta = m_ui->viewArea->rowCount();
431  delta = ((delta == 0) ? 0 : (delta - 1) * TTK_ITEM_SIZE_S) - 2 * TTK_ITEM_SIZE_S;
432 
433  setFixedHeightWidget(this, delta);
434  setFixedHeightWidget(m_ui->backgroundMask, delta);
435  setFixedHeightWidget(m_ui->background, delta);
436  setFixedHeightWidget(m_ui->viewArea, delta + 2 * TTK_ITEM_SIZE_S);
437 
438  setMoveWidget(m_ui->label2, delta);
439  setMoveWidget(m_ui->fileNameLabel, delta);
440  setMoveWidget(m_ui->downloadButton, delta);
441 
444 }
445 
446 void MusicDownloadWidget::setFixedHeightWidget(QWidget *w, int height)
447 {
448  w->setFixedHeight(w->height() + height);
449 }
450 
451 void MusicDownloadWidget::setMoveWidget(QWidget *w, int pos)
452 {
453  const QRect &rect = w->geometry();
454  w->move(rect.x(), rect.y() + pos);
455 }
456 
458 {
459  const int bitrate = m_ui->viewArea->bitrate();
460  if(bitrate == TTK_BN_0)
461  {
462  MusicToastLabel::popup(tr("Please select one item first"));
463  return;
464  }
465 
466  hide();
467 
469  {
471  }
473  {
475  }
476 
477  controlEnabled(false);
478 }
479 
481 {
482  Q_EMIT dataDownloadChanged();
483  close();
484 }
const TTK::MusicSongInformationList & items() const noexcept
void setInformation(const QString &info)
TTK_MODULE_EXPORT QString generateSongArtist(const QString &name, const QString &key=TTK_DEFAULT_STR)
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
void setSongMeta(MusicSongMeta &meta) noexcept
bool fromFile(const QString &name)
The class of the table widget base.
The class of the music song property.
Definition: musicobject.h:242
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:314
The class of the download record manager.
void addCellItems(const TTK::MusicSongPropertyList &props)
#define TTK_ITEM_SIZE_S
Definition: ttkglobal.h:320
TTK_MODULE_EXPORT QString generateSongTitle(const QString &name, const QString &key=TTK_DEFAULT_STR)
#define TTK_BN_1000
Definition: ttkglobal.h:434
void setBackgroundLabel(QLabel *label) noexcept
virtual bool readBuffer(MusicSongList &items) overridefinal
void setFixedHeightWidget(QWidget *w, int height)
#define TTK_BN_250
Definition: ttkglobal.h:430
virtual void startToRequest() overridefinal
MusicDownloadTableWidget(QWidget *parent=nullptr)
void setName(const QString &n) noexcept
Definition: musicsong.h:77
QString queryValue() const noexcept
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:432
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate)
void setTitle(const QString &title) noexcept
void setAddTimeStr(const QString &t) noexcept
Definition: musicsong.h:60
#define qAsConst
Definition: ttkqtglobal.h:57
const char * name
Definition: http_parser.c:458
MusicAbstractQueryRequest * m_networkRequest
MusicAbstractQueryRequest::QueryType m_queryType
#define TTK_SIGNLE_SHOT(...)
Definition: ttkqtglobal.h:189
TTK_MODULE_EXPORT void setTransparent(QWidget *widget, int alpha)
void setSizeStr(const QString &s) noexcept
Definition: musicsong.h:68
bool isEmpty() const noexcept
Definition: musicobject.h:255
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:424
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:431
#define TTK_SLOT
Definition: ttkqtglobal.h:177
void setIcon(const QString &name)
TTK_MODULE_EXPORT QString toString(Record type) noexcept
void setText(const QString &text)
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:85
virtual void startToSearch(const QString &value)=0
The class of the abstract query data from net.
QueryType queryType() const noexcept
void setAlbum(const QString &album) noexcept
The class of the download table item.
#define TTK_BN_192
Definition: ttkglobal.h:429
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:300
#define TTK_BN_750
Definition: ttkglobal.h:433
#define TTK_BN_128
Definition: ttkglobal.h:428
#define G_SETTING_PTR
The class of the moving widget base.