TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiccommentswidget.cpp
Go to the documentation of this file.
1 #include "musiccommentswidget.h"
3 #include "musiccoverrequest.h"
6 #include "musicpagequerywidget.h"
7 #include "musicwidgetheaders.h"
8 #include "musicwidgetutils.h"
9 #include "ttkclickedlabel.h"
10 
11 #include <QTextEdit>
12 
14  : QWidget(parent)
15 {
16  setFixedHeight(60);
17  QVBoxLayout *layout = new QVBoxLayout(this);
18  layout->setContentsMargins(10, 0, 10, 0);
19  layout->addSpacing(2);
20  //
21  QWidget *centerWidget = new QWidget(this);
22  QHBoxLayout *centerWidgetLayout = new QHBoxLayout(centerWidget);
23  centerWidgetLayout->setContentsMargins(0, 0, 0, 0);
24  //
25  QWidget *textWidget = new QWidget(centerWidget);
26  QVBoxLayout *textWidgetLayout = new QVBoxLayout(textWidget);
27  textWidgetLayout->setContentsMargins(0, 5, 0, 6);
28  textWidgetLayout->setSpacing(0);
29  //
30  QWidget *userWidget = new QWidget(textWidget);
31  QHBoxLayout *userWidgetLayout = new QHBoxLayout(userWidget);
32  userWidgetLayout->setContentsMargins(0, 0, 0, 0);
33  userWidgetLayout->setSpacing(2);
34  //
35  m_userName = new QLabel(userWidget);
36  m_userCommit = new QTextEdit(userWidget);
37  m_userCommit->setReadOnly(true);
38  m_userCommit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41  m_userName->setStyleSheet(TTK::UI::ColorStyle03);
42  userWidgetLayout->addWidget(m_userName);
43  userWidgetLayout->addWidget(m_userCommit);
44  userWidget->setLayout(userWidgetLayout);
45  //
46  QWidget *operWidget = new QWidget(textWidget);
47  QHBoxLayout *operWidgetLayout = new QHBoxLayout(operWidget);
48  operWidgetLayout->setContentsMargins(0, 0, 0, 0);
49  operWidgetLayout->setSpacing(4);
50 
51  m_timerLabel = new QLabel(operWidget);
52  m_timerLabel->setStyleSheet(TTK::UI::ColorStyle03);
53 
54  TTKClickedLabel *ngReportLabel = new TTKClickedLabel(tr("Report"), operWidget);
55  ngReportLabel->setStyleSheet(TTK::UI::ColorStyle03);
56 
57  QFrame *spliteLine1 = new QFrame(operWidget);
58  spliteLine1->setFixedWidth(1);
59  spliteLine1->setFrameShape(QFrame::VLine);
60  spliteLine1->setStyleSheet(TTK::UI::ColorStyle03);
61 
62  QPushButton *starButton = new QPushButton(operWidget);
63  starButton->setCursor(QCursor(Qt::PointingHandCursor));
64  starButton->setFixedSize(12, 11);
65  starButton->setStyleSheet(TTK::UI::InteriorStar);
66 
67  m_starLabel = new QLabel(operWidget);
68  m_starLabel->setText("(0)");
70 
71  QFrame *spliteLine2 = new QFrame(operWidget);
72  spliteLine2->setFixedWidth(1);
73  spliteLine2->setFrameShape(QFrame::VLine);
74  spliteLine2->setStyleSheet(TTK::UI::ColorStyle03);
75  TTKClickedLabel *ReplyLabel = new TTKClickedLabel(tr("Reply"), operWidget);
76  ReplyLabel->setStyleSheet(TTK::UI::ColorStyle03);
77  //
78 #ifdef Q_OS_UNIX
79  starButton->setFocusPolicy(Qt::NoFocus);
80 #endif
81  //
82  operWidgetLayout->addWidget(m_timerLabel);
83  operWidgetLayout->addStretch(1);
84  operWidgetLayout->addWidget(ngReportLabel);
85  operWidgetLayout->addWidget(spliteLine1);
86  operWidgetLayout->addWidget(starButton);
87  operWidgetLayout->addWidget(m_starLabel);
88  operWidgetLayout->addWidget(spliteLine2);
89  operWidgetLayout->addWidget(ReplyLabel);
90  operWidget->setLayout(operWidgetLayout);
91  //
92  textWidgetLayout->addWidget(userWidget);
93  textWidgetLayout->addWidget(operWidget);
94  textWidget->setLayout(textWidgetLayout);
95  //
96  m_iconLabel = new QLabel(centerWidget);
97  m_iconLabel->setFixedSize(40, 40);
98  m_iconLabel->setPixmap(QPixmap(":/image/lb_default_art").scaled(m_iconLabel->size()));
99  centerWidgetLayout->addWidget(m_iconLabel);
100  centerWidgetLayout->addWidget(textWidget);
101  centerWidget->setLayout(centerWidgetLayout);
102  //
103  QFrame *solidLine = new QFrame(this);
104  solidLine->setFixedHeight(1);
105  solidLine->setStyleSheet(TTK::UI::BorderStyle04 + TTK::UI::ColorStyle03);
106  //
107  layout->addWidget(centerWidget);
108  layout->addWidget(solidLine);
109  setLayout(layout);
110 }
111 
113 {
114  delete m_userName;
115  delete m_userCommit;
116  delete m_timerLabel;
117  delete m_iconLabel;
118  delete m_starLabel;
119 }
120 
122 {
123  m_userName->setText(item.m_nickName + ":");
124  m_userName->setFixedWidth(TTK::Widget::fontTextWidth(m_userName->font(), m_userName->text()));
125  m_timerLabel->setText(TTKDateTime::format(item.m_time.toULongLong(), TTK_DATE_TIMES_FORMAT));
126  m_timerLabel->setFixedWidth(TTK::Widget::fontTextWidth(m_timerLabel->font(), m_timerLabel->text()));
127  m_starLabel->setText(QString("(%1)").arg(item.m_count));
128  m_userCommit->setText(item.m_description);
129 
130  if(TTK::isCoverValid(item.m_coverUrl))
131  {
132  MusicCoverRequest *req = G_DOWNLOAD_QUERY_PTR->makeCoverRequest(this);
133  connect(req, SIGNAL(downloadRawDataChanged(QByteArray)), SLOT(downloadFinished(QByteArray)));
134  req->startToRequest(item.m_coverUrl);
135  }
136 }
137 
138 void MusicCommentsItem::downloadFinished(const QByteArray &bytes)
139 {
140  if(bytes.isEmpty())
141  {
142  TTK_ERROR_STREAM("Input byte data is empty");
143  return;
144  }
145 
146  QPixmap pix;
147  if(pix.loadFromData(bytes))
148  {
149  m_iconLabel->setPixmap(pix.scaled(m_iconLabel->size()));
150  }
151 }
152 
153 
154 
156  : QLabel(parent),
157  m_plain(true),
158  m_topLabel(nullptr),
159  m_commentsLabel(nullptr),
160  m_messageComments(nullptr),
161  m_pageQueryWidget(nullptr),
162  m_networkRequest(nullptr)
163 {
164 
165 }
166 
168 {
170  delete m_topLabel;
171  delete m_commentsLabel;
172  delete m_pageQueryWidget;
173  delete m_messageComments;
174  delete m_networkRequest;
175 }
176 
178 {
179  m_plain = plain;
180 
181  setObjectName(metaObject()->className());
182  setStyleSheet(QString("#%1{ %2 }").arg(objectName(), TTK::UI::BackgroundStyle07));
183 
184  QVBoxLayout *mainLayout = new QVBoxLayout(this);
185  mainLayout->setContentsMargins(0, 0, 0, 0);
186  mainLayout->setSpacing(0);
187 
188  QWidget *topWidget = nullptr;
189  if(plain)
190  {
191  topWidget = new QWidget(this);
192  topWidget->setFixedHeight(40);
193  QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
194  topWidgetLayout->setContentsMargins(0, 0, 10, 0);
195 
196  m_topLabel = new QLabel(topWidget);
197  m_topLabel->setAlignment(Qt::AlignCenter);
198  topWidgetLayout->addWidget(m_topLabel);
199 
200  QPushButton *closeButton = new QPushButton(this);
201  closeButton->setFixedSize(14, 14);
202  closeButton->setStyleSheet(TTK::UI::BtnPClose);
203  closeButton->setCursor(QCursor(Qt::PointingHandCursor));
204 #ifdef Q_OS_UNIX
205  closeButton->setFocusPolicy(Qt::NoFocus);
206 #endif
207  connect(closeButton, SIGNAL(clicked()), SLOT(close()));
208 
209  topWidgetLayout->addWidget(closeButton);
210  topWidget->setLayout(topWidgetLayout);
211  }
212  else
213  {
214  m_topLabel = new QLabel(this);
215  m_topLabel->hide();
216  }
217 
218  QWidget *contentsWidget = new QWidget(this);
219  QVBoxLayout *contentsLayout = new QVBoxLayout(contentsWidget);
220  contentsLayout->setContentsMargins(0, 0, 0 ,0);
221  contentsLayout->setSpacing(0);
222  //
223  QWidget *messageBox = new QWidget(contentsWidget);
224  messageBox->setAttribute(Qt::WA_TranslucentBackground);
225  messageBox->setFixedHeight(60);
226  QVBoxLayout *messageBoxLayout = new QVBoxLayout(messageBox);
227  messageBoxLayout->setContentsMargins(10, 10, 10, 3);
228  m_commentsLabel = new QLabel(messageBox);
229  m_commentsLabel->setStyleSheet(TTK::UI::ColorStyle04);
230  QFrame *solidLine = new QFrame(messageBox);
231  solidLine->setFrameShape(QFrame::HLine);
232  solidLine->setFixedHeight(1);
233  solidLine->setStyleSheet(TTK::UI::ColorStyle03);
234  //
235  messageBoxLayout->addWidget(m_commentsLabel);
236  messageBoxLayout->addWidget(solidLine);
237  messageBox->setLayout(messageBoxLayout);
238  //
239  m_messageComments = new QWidget(contentsWidget);
240  QVBoxLayout *messageCommentsLayout = new QVBoxLayout(m_messageComments);
241  messageCommentsLayout->setContentsMargins(0, 0, 0, 0);
242  m_messageComments->setLayout(messageCommentsLayout);
243 
244  contentsLayout->addWidget(messageBox);
245  contentsLayout->addWidget(m_messageComments);
246  contentsWidget->setLayout(contentsLayout);
247  //
248  if(plain)
249  {
250  QScrollArea *scrollArea = new QScrollArea(this);
251  TTK::Widget::generateVScrollAreaStyle(scrollArea, contentsWidget, false);
252 
253  contentsWidget->setObjectName("ContentsWidget");
254  contentsWidget->setStyleSheet(QString("#%1{ %2 }").arg(contentsWidget->objectName(), TTK::UI::BackgroundStyle01));
255  QWidget *view = scrollArea->viewport();
256  view->setObjectName("Viewport");
257  view->setStyleSheet(QString("#%1{ %2 }").arg(view->objectName(), TTK::UI::BackgroundStyle01));
258  //
259  mainLayout->addWidget(topWidget);
260  mainLayout->addWidget(scrollArea);
261  }
262  else
263  {
264  mainLayout->addWidget(contentsWidget);
265  setFixedHeight(210);
266  }
267 
268  initLabel({}, 0);
269  setLayout(mainLayout);
270 }
271 
273 {
275 
276  TTKEventLoop loop;
277  connect(m_networkRequest, SIGNAL(downloadDataChanged(QString)), &loop, SLOT(quit()));
279  loop.exec();
280 
281  TTKObjectCast(QVBoxLayout*, m_messageComments->layout())->addStretch(1);
283 
285 }
286 
288 {
290  comment->addCellItem(item);
291  m_commentsItems << comment;
292 
293  QVBoxLayout *layout = TTKObjectCast(QVBoxLayout*, m_messageComments->layout());
294  layout->insertWidget(layout->count() - 1, comment);
295 
296  if(!m_plain)
297  {
298  setFixedHeight(height() + comment->height());
299  }
300 }
301 
303 {
305 
308 }
309 
310 void MusicCommentsWidget::mousePressEvent(QMouseEvent *event)
311 {
312  Q_UNUSED(event);
313 }
314 
315 void MusicCommentsWidget::mouseMoveEvent(QMouseEvent *event)
316 {
317  Q_UNUSED(event);
318 }
319 
321 {
322  Q_UNUSED(event);
323 }
324 
325 void MusicCommentsWidget::initLabel(const QString &name, int total)
326 {
327  m_topLabel->setText(tr("<span style='font-size:15pt;color:#AAAAAA;font-style:bold;'>\"%1\" Commit</span>"
328  "<span style='font-size:11pt;color:#666666;font-style:bold;'> (Just %2 Commits)</span>").arg(name).arg(total));
329  m_commentsLabel->setText(tr("All Comments(%1)").arg(total));
330 }
331 
333 {
334  qDeleteAll(m_commentsItems);
335  m_commentsItems.clear();
336 
337  if(!m_plain)
338  {
339  setFixedHeight(210);
340  }
341 }
342 
344 {
346  connect(m_pageQueryWidget, SIGNAL(clicked(int)), SLOT(buttonClicked(int)));
347 
349  m_messageComments->layout()->addWidget(w);
350 }
void initLabel(const QString &name, int total)
virtual void mousePressEvent(QMouseEvent *event) overridefinal
The class of the search result data item.
void createCommentItem(const MusicResultDataItem &item)
static const QString BorderStyle01
border
void buttonClicked(int index)
void downloadFinished(const QByteArray &bytes)
static bool isCoverValid(const QString &url) noexcept
The class of the song comment item.
int pageTotalSize() const noexcept
virtual void startToRequest(const QString &url)=0
QList< MusicCommentsItem * > m_commentsItems
void addCellItem(const MusicResultDataItem &item)
static constexpr const char * BtnPClose
int exec(ProcessEventsFlags flags=AllEvents)
static const QString ColorStyle04
Definition: musicuiobject.h:46
The class of the label widget that can click.
virtual void startToSearch(const QString &value)
static const QString BorderStyle04
The class of the ttk event loop.
Definition: ttkeventloop.h:29
virtual void initialize(bool plain)
const char * name
Definition: http_parser.c:458
static constexpr const char * InteriorStar
static const QString BackgroundStyle01
Background.
Definition: musicuiobject.h:77
void setCurrentSongName(const QString &name)
virtual void mouseMoveEvent(QMouseEvent *event) overridefinal
The class of the page query widget.
#define TTK_DATE_TIMES_FORMAT
Definition: ttkglobal.h:312
virtual void startToPage(int offset)
void page(int index, int total)
TTK_MODULE_EXPORT void generateVScrollAreaStyle(QWidget *widget, QWidget *parent, bool background=true)
static const QString ColorStyle03
Definition: musicuiobject.h:43
virtual void mouseReleaseEvent(QMouseEvent *event) overridefinal
MusicCommentsRequest * m_networkRequest
QWidget * createPageWidget(QWidget *parent, int total)
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
static const QString BackgroundStyle07
#define G_DOWNLOAD_QUERY_PTR
The class of the cover data download request.
static QString format(const QString &time, const QString &format)
Definition: ttktime.cpp:254
MusicCommentsItem(QWidget *parent=nullptr)
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
int totalSize() const noexcept
MusicCommentsWidget(QWidget *parent=nullptr)
MusicPageQueryWidget * m_pageQueryWidget
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:82