TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicttkfmradioplaywidget.cpp
Go to the documentation of this file.
2 #include "ui_musicttkfmradioplaywidget.h"
3 #include "ui_musicttkfmradioinformationwidget.h"
4 #include "musiccoremplayer.h"
5 #include "musictoastlabel.h"
6 #include "musicmessagebox.h"
7 #include "ttkitemdelegate.h"
8 
9 #include <QScrollBar>
10 
11 #define FMRADIO_PATH RESOURCE_DIR_FULL + "fmlist"
12 
14  : MusicAbstractMoveDialog(parent),
16  m_readOnly(true)
17 {
18  m_ui->setupUi(this);
19  setFixedSize(size());
20  setBackgroundLabel(m_ui->background);
21 
22  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
23  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
24  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
25  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
26  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
27 
28  m_ui->button->setStyleSheet(TTK::UI::PushButtonStyle04);
29 #ifdef Q_OS_UNIX
30  m_ui->button->setFocusPolicy(Qt::NoFocus);
31 #endif
32  connect(m_ui->button, SIGNAL(clicked()), SLOT(accept()));
33 }
34 
36 {
37  delete m_ui;
38 }
39 
41 {
42  m_ui->pathEdit->setText(channel.m_url);
43  m_ui->nameEdit->setText(channel.m_name);
44 }
45 
47 {
48  return {m_ui->nameEdit->text(), m_ui->pathEdit->text()};
49 }
50 
52 {
53  m_readOnly = mode;
54  m_ui->pathEdit->setReadOnly(mode);
55  m_ui->nameEdit->setReadOnly(mode);
56 }
57 
58 
59 
61  : MusicAbstractMoveWidget(parent),
62  m_ui(new Ui::MusicTTKFMRadioPlayWidget),
63  m_isPlaying(false),
64  m_statusChanged(false),
65  m_currentIndex(0)
66 {
67  m_ui->setupUi(this);
68  setFixedSize(size());
69  setBackgroundLabel(m_ui->background);
70 
71  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
72  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
73  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
74  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
75  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
76 
77  m_ui->playButton->setIcon(QIcon(":/functions/btn_play_hover"));
78  m_ui->previousButton->setIcon(QIcon(":/functions/btn_previous_hover"));
79  m_ui->nextButton->setIcon(QIcon(":/functions/btn_next_hover"));
80 
81  m_ui->playButton->setStyleSheet(TTK::UI::BackgroundStyle01);
82  m_ui->previousButton->setStyleSheet(TTK::UI::BackgroundStyle01);
83  m_ui->nextButton->setStyleSheet(TTK::UI::BackgroundStyle01);
84  m_ui->addButton->setStyleSheet(TTK::UI::PushButtonStyle04);
85  m_ui->deleteButton->setStyleSheet(TTK::UI::PushButtonStyle04);
86  m_ui->infoButton->setStyleSheet(TTK::UI::PushButtonStyle04);
87 
88 #ifdef Q_OS_UNIX
89  m_ui->playButton->setFocusPolicy(Qt::NoFocus);
90  m_ui->previousButton->setFocusPolicy(Qt::NoFocus);
91  m_ui->nextButton->setFocusPolicy(Qt::NoFocus);
92  m_ui->addButton->setFocusPolicy(Qt::NoFocus);
93  m_ui->deleteButton->setFocusPolicy(Qt::NoFocus);
94  m_ui->infoButton->setFocusPolicy(Qt::NoFocus);
95 #endif
96 
97  m_ui->playButton->setIconSize(QSize(31, 31));
98  m_ui->previousButton->setIconSize(QSize(31, 31));
99  m_ui->nextButton->setIconSize(QSize(31, 31));
100 
101  m_ui->playButton->setCursor(QCursor(Qt::PointingHandCursor));
102  m_ui->previousButton->setCursor(QCursor(Qt::PointingHandCursor));
103  m_ui->nextButton->setCursor(QCursor(Qt::PointingHandCursor));
104  m_ui->addButton->setCursor(QCursor(Qt::PointingHandCursor));
105  m_ui->deleteButton->setCursor(QCursor(Qt::PointingHandCursor));
106  m_ui->infoButton->setCursor(QCursor(Qt::PointingHandCursor));
107 
108  m_ui->volumeSlider->setStyleSheet(TTK::UI::SliderStyle10);
109  m_ui->volumeSlider->setRange(0, 100);
110  m_ui->volumeSlider->setValue(100);
111 
112  m_ui->itemTree->setHeaderLabels({{}, {}});
113  m_ui->itemTree->setColumnWidth(0, 320);
114 
115  m_ui->itemTree->header()->setVisible(false);
116  m_ui->itemTree->setStyleSheet(TTK::UI::ColorStyle02);
117  m_ui->itemTree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
118  m_ui->itemTree->verticalScrollBar()->setStyleSheet(TTK::UI::ScrollBarStyle03);
119  m_ui->itemTree->setFocusPolicy(Qt::NoFocus);
120 
121  TTKLabelItemDelegate *delegateTitle = new TTKLabelItemDelegate(this);
123  delegateTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
125  m_ui->itemTree->setItemDelegateForColumn(0, delegateTitle);
126 
127  initialize();
129 
130  connect(m_ui->playButton, SIGNAL(clicked()), SLOT(radioPlay()));
131  connect(m_ui->previousButton, SIGNAL(clicked()), SLOT(radioPrevious()));
132  connect(m_ui->nextButton, SIGNAL(clicked()), SLOT(radioNext()));
133  connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addButtonClicked()));
134  connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(deleteButtonClicked()));
135  connect(m_ui->infoButton, SIGNAL(clicked()), SLOT(infoButtonClicked()));
136  connect(m_ui->volumeSlider, SIGNAL(valueChanged(int)), SLOT(radioVolume(int)));
137  connect(m_ui->itemTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), SLOT(radioItemChanged(QTreeWidgetItem*,int)));
138 }
139 
141 {
142  delete m_player;
143  delete m_ui;
144 }
145 
146 void MusicTTKFMRadioPlayWidget::radioItemChanged(QTreeWidgetItem *item, int column)
147 {
148  if(item && column == 0)
149  {
150  const int index = indexValid(item);
151  if(index < 0)
152  {
153  return;
154  }
155 
156  m_currentIndex = index;
157  startCoreModule();
158  }
159 }
160 
162 {
163  if(TTK_LOW_LEVEL == code)
164  {
165  startCoreModule();
166  }
167 }
168 
170 {
171  if(!m_player)
172  {
173  return;
174  }
175 
177  m_ui->playButton->setIcon(QIcon(m_isPlaying ? ":/functions/btn_pause_hover" : ":/functions/btn_play_hover"));
178  m_player->play();
179 }
180 
182 {
183  if(--m_currentIndex < 0)
184  {
185  m_currentIndex = 0;
186  }
187 
188  startCoreModule();
189 }
190 
192 {
193  if(++m_currentIndex >= m_items.count())
194  {
195  m_currentIndex = 0;
196  }
197 
198  startCoreModule();
199 }
200 
202 {
203  if(m_player)
204  {
205  m_player->setVolume(num);
206  }
207 }
208 
210 {
211  delete m_player;
212  m_player = nullptr;
213 
214  MusicFMCategoryList categorys;
215  MusicFMConfigManager manager;
216  if(m_statusChanged && manager.fromFile(FMRADIO_PATH))
217  {
218  manager.readBuffer(categorys);
219 
220  if(!categorys.isEmpty())
221  {
222  categorys.back().m_items = m_favItems;
223  manager.reset();
224  manager.writeBuffer(categorys);
225  }
226  }
227 
228  QWidget::closeEvent(event);
229 }
230 
232 {
233  MusicFMCategoryList categorys;
234  {
235  MusicFMConfigManager manager;
236  if(manager.fromFile(FMRADIO_PATH))
237  {
238  manager.readBuffer(categorys);
239  }
240  }
241 
242  int index = 0;
243  for(const MusicFMCategory &category : qAsConst(categorys))
244  {
245  QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->itemTree);
246  item->setData(0, TTK_DISPLAY_ROLE, category.m_category);
247  m_ui->itemTree->addTopLevelItem(item);
248 
249  for(const MusicFMChannel &channel : qAsConst(category.m_items))
250  {
251  QTreeWidgetItem *it = new QTreeWidgetItem(item);
252  it->setData(0, TTK_DATA_ROLE, index++);
253  it->setData(0, TTK_DISPLAY_ROLE, channel.m_name);
254  }
255  m_items << category.m_items;
256  }
257 
258  if(!categorys.isEmpty())
259  {
260  m_favItems = categorys.back().m_items;
261  }
262 
264 
266  const int v = m_ui->volumeSlider->value();
267  m_ui->volumeSlider->setValue(0);
268  m_ui->volumeSlider->setValue(v);
270  m_ui->gifLabel_1->setType(MusicGifLabelWidget::Module::RadioBlue);
271  m_ui->gifLabel_2->setType(MusicGifLabelWidget::Module::RadioBlue);
272  m_ui->gifLabel_3->setType(MusicGifLabelWidget::Module::RadioBlue);
273  m_ui->gifLabel_4->setType(MusicGifLabelWidget::Module::RadioBlue);
274  m_ui->gifLabel_5->setType(MusicGifLabelWidget::Module::RadioBlue);
275 }
276 
278 {
279  m_player = new MusicCoreMPlayer(this);
280 
281  connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
282  connect(m_player, SIGNAL(finished(int)), SLOT(mediaPlayFinished(int)));
283 }
284 
286 {
287  m_isPlaying = false;
288  if(!m_player)
289  {
291  }
292 
294  radioPlay();
295 
296  m_ui->titleWidget->setText(m_items[m_currentIndex].m_name);
297  m_ui->gifLabel_1->start();
298  m_ui->gifLabel_2->start();
299  m_ui->gifLabel_3->start();
300  m_ui->gifLabel_4->start();
301  m_ui->gifLabel_5->start();
302 
303  positionChanged(0);
304  m_isPlaying = true;
305 }
306 
307 int MusicTTKFMRadioPlayWidget::indexValid(QTreeWidgetItem *item) const
308 {
309  if(!item)
310  {
311  return -1;
312  }
313 
314  bool ok = false;
315  const int index = item->data(0, TTK_DATA_ROLE).toInt(&ok);
316  if(!ok || index < 0 || index >= m_items.count())
317  {
318  return -1;
319  }
320 
321  return index;
322 }
323 
325 {
326  if(!m_player)
327  {
328  return;
329  }
330 
331  m_ui->positionLabel->setText(TTKTime::formatDuration(position * TTK_DN_S2MS));
332 }
333 
335 {
337  w.setReadOnly(false);
338  if(w.exec())
339  {
340  const MusicFMChannel &channel = w.channelInformation();
341  if(channel.m_name.isEmpty() || channel.m_url.isEmpty())
342  {
343  return;
344  }
345 
346  m_items << channel;
347  m_favItems << channel;
348  QTreeWidgetItem *item = m_ui->itemTree->topLevelItem(m_ui->itemTree->topLevelItemCount() - 1);
349  if(!item)
350  {
351  return;
352  }
353 
354  QTreeWidgetItem *it = new QTreeWidgetItem(item);
355  it->setData(0, TTK_DATA_ROLE, m_items.count() - 1);
356  it->setData(0, TTK_DISPLAY_ROLE, channel.m_name);
357 
358  m_statusChanged = true;
359  MusicToastLabel::popup(tr("Add current channel success"));
360  }
361 }
362 
364 {
365  MusicMessageBox message;
366  message.setText(tr("Are you sure to delete?"));
367  if(!message.exec())
368  {
369  return;
370  }
371 
372  QTreeWidgetItem *item = m_ui->itemTree->currentItem();
373  const int index = indexValid(item);
374  if(index < 0)
375  {
376  return;
377  }
378 
379  const int offset = m_items.count() - m_favItems.count();
380  if(index < offset)
381  {
382  MusicToastLabel::popup(tr("The current channel can not be deleted"));
383  return;
384  }
385 
386  for(int i = index - offset + 1; i < item->parent()->childCount(); ++i)
387  {
388  item->parent()->child(i)->setData(0, TTK_DATA_ROLE, offset + i - 1);
389  }
390 
391  item->parent()->removeChild(item);
392  m_items.removeAt(index);
393  m_favItems.removeAt(index - offset);
394 
395  if(m_currentIndex == index)
396  {
397  radioPrevious();
398  }
399 
400  m_statusChanged = true;
401  MusicToastLabel::popup(tr("Delete current channel success"));
402 }
403 
405 {
406  const int index = indexValid(m_ui->itemTree->currentItem());
407  if(index < 0)
408  {
409  return;
410  }
411 
413  widget.setChannelInformation(m_items[index]);
414  widget.exec();
415 }
void setBackgroundLabel(QLabel *label)
void setMedia(Module type, const QString &url, int winId=-1)
The class of the label item delegate.
void setText(const QString &text) const
#define TTK_DISPLAY_ROLE
#define TTK_LOW_LEVEL
Definition: ttkglobal.h:258
static const QString SliderStyle10
bool fromFile(const QString &name)
The class of the fm config manager.
virtual void closeEvent(QCloseEvent *event) overridefinal
voidpf void uLong size
Definition: ioapi.h:136
void setStyleSheet(const QString &style) const
MusicTTKFMRadioInformationWidget(QWidget *parent=nullptr)
MusicTTKFMRadioPlayWidget(QWidget *parent=nullptr)
Ui::MusicTTKFMRadioInformationWidget * m_ui
The class of the messagebox widget.
The class of the web music radio information widget.
void setVolume(int value)
void setChannelInformation(const MusicFMChannel &channel)
int indexValid(QTreeWidgetItem *item) const
static void popup(const QString &text)
void setAlignment(Qt::Alignment alignment) const
static const QString ScrollBarStyle03
voidpf uLong offset
Definition: ioapi.h:142
#define qAsConst
Definition: ttkqtglobal.h:51
static const QString ColorStyle02
Definition: musicuiobject.h:40
static const QString BackgroundStyle01
Background.
Definition: musicuiobject.h:77
The class of the fm channel item.
virtual bool writeBuffer(const MusicFMCategoryList &items) overridefinal
TTK_MODULE_EXPORT void adjustWidgetPosition(QWidget *widget)
static const QString PushButtonStyle04
Ui::MusicTTKFMRadioPlayWidget * m_ui
The class of the moving dialog base.
The class of the mplayer core.
Definition: inftrees.h:24
void radioItemChanged(QTreeWidgetItem *item, int column)
#define FMRADIO_PATH
#define TTK_DN_S2MS
Definition: ttkglobal.h:281
The class of the web music radio widget.
static const QString ToolButtonStyle04
The class of the fm category item.
void setModuleMode(TTKUInt8 mode)
const char int mode
Definition: ioapi.h:135
virtual bool readBuffer(MusicFMCategoryList &items) overridefinal
#define TTK_DATA_ROLE
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123
The class of the moving widget base.