TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicsongdailywidget.cpp
Go to the documentation of this file.
1 #include "musicsongdailywidget.h"
2 #include "musiccibarequest.h"
3 #include "musicwidgetheaders.h"
4 #include "musicwidgetutils.h"
5 #include "musicimageutils.h"
6 
8  : QFrame(parent)
9 {
10  setObjectName(MusicSongDailyWidget::metaObject()->className());
11  setStyleSheet(QString("#%1{ %2 }").arg(objectName(), TTK::UI::BackgroundStyle10));
12 
13  m_container = new QLabel(this);
14 
15  QVBoxLayout *layout = new QVBoxLayout(this);
16  layout->addWidget(m_container);
17  setLayout(layout);
18 
19  m_note = new QLabel(m_container);
20  m_content = new QLabel(m_container);
21 
23  connect(m_networkRequest, SIGNAL(downloadRawDataChanged(QByteArray)), this, SLOT(downloadImageFinished(QByteArray)));
24 
26 }
27 
29 {
30  delete m_note;
31  delete m_content;
32  delete m_container;
33  delete m_networkRequest;
34 }
35 
37 {
38  if(m_image.isNull())
39  {
40  return;
41  }
42 
43  const double scale = G_SETTING_PTR->value(MusicSettingManager::WidgetSize).toSize().width() * 1.0 / WINDOW_WIDTH_MIN;
44 
45  QFont font = m_note->font();
46  font.setPixelSize(20.0 * scale);
47  const int noteHeight = TTK::Widget::fontTextHeight(font);
48 
49  m_note->setFont(font);
50  m_note->setGeometry(50, 50, TTK::Widget::fontTextWidth(font, m_note->text()), noteHeight);
51 
52  font = m_content->font();
53  font.setPixelSize(18.0 * scale);
54  const int contentHeight = TTK::Widget::fontTextHeight(font);
55 
56  m_content->setFont(font);
57  m_content->setGeometry(50, 50 + noteHeight, TTK::Widget::fontTextWidth(font, m_content->text()), contentHeight);
58 
59  m_container->setPixmap(QPixmap::fromImage(m_image).scaled(m_container->size(), Qt::KeepAspectRatioByExpanding));
60 }
61 
62 void MusicSongDailyWidget::downloadImageFinished(const QByteArray &bytes)
63 {
64  if(bytes.isEmpty())
65  {
66  TTK_ERROR_STREAM("Input byte data is empty");
67  return;
68  }
69 
70  m_image.loadFromData(bytes);
71  if(m_image.isNull())
72  {
73  return;
74  }
75 
76  QRgb r = 0, g = 0, b = 0;
77  for(int w = 0; w < m_image.width(); ++w)
78  {
79  for(int h = 0; h < m_image.height(); ++h)
80  {
81  const QRgb rgb = m_image.pixel(w, h);
82  r += qRed(rgb);
83  g += qGreen(rgb);
84  b += qBlue(rgb);
85  }
86  }
87 
88  const int size = m_image.width() * m_image.height();
89  r /= size;
90  g /= size;
91  b /= size;
92 
93  const QRgb rgb = TTK::Image::colorContrast(qRgb(r, g, b));
94  const QString &style = QString("color:rgb(%1, %2, %3)").arg(qRed(rgb)).arg(qGreen(rgb)).arg(qBlue(rgb));
95 
96  m_note->setStyleSheet(style);
97  m_content->setStyleSheet(style);
98 
99  m_note->setText(m_networkRequest->note());
100  m_content->setText(m_networkRequest->content());
101 
102  resizeGeometry();
103 }
104 
105 void MusicSongDailyWidget::resizeEvent(QResizeEvent *event)
106 {
107  QWidget::resizeEvent(event);
108  resizeGeometry();
109 }
virtual void resizeEvent(QResizeEvent *event) overridefinal
voidpf void uLong size
Definition: ioapi.h:136
QString note() const noexcept
virtual void resizeGeometry() overridefinal
The class of the ciba request.
static const QString BackgroundStyle10
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
TTK_MODULE_EXPORT unsigned int colorContrast(const unsigned int color)
#define WINDOW_WIDTH_MIN
Definition: musicobject.h:172
void downloadImageFinished(const QByteArray &bytes)
TTK_MODULE_EXPORT int fontTextHeight(const QFont &font)
MusicSongDailyWidget(QWidget *parent=nullptr)
QString content() const noexcept
MusicCiBaRequest * m_networkRequest
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
#define G_SETTING_PTR