TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicplaylistbackupwidget.cpp
Go to the documentation of this file.
3 #include "musicbackupmodule.h"
5 #include "musicmessagebox.h"
6 #include "musicfileutils.h"
8 
9 #define ROOT_PATH APPBACKUP_DIR_FULL + "playlist/"
10 
13 {
14  setColumnCount(2);
15 
16  QHeaderView *headerView = horizontalHeader();
17  headerView->resizeSection(0, 372);
18  headerView->resizeSection(1, 45);
19 
20  connect(this, SIGNAL(cellDoubleClicked(int,int)), SLOT(itemDoubleClicked(int,int)));
21 }
22 
24 {
25  removeItems();
26 }
27 
28 void MusicPlaylistBackupTableWidget::updateSongsList(const MusicSongList &songs)
29 {
30  setRowCount(songs.count());
31 
32  QHeaderView *headerView = horizontalHeader();
33  for(int i = 0; i < songs.count(); ++i)
34  {
35  const MusicSong &v = songs[i];
36 
37  QTableWidgetItem *item = new QTableWidgetItem;
38  item->setToolTip(v.name());
39  item->setText(TTK::Widget::elidedText(font(), v.name(), Qt::ElideRight, headerView->sectionSize(0) - 10));
40  item->setForeground(QColor(TTK::UI::Color01));
41  QtItemSetTextAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
42  setItem(i, 0, item);
43 
44  item = new QTableWidgetItem(v.duration());
45  item->setForeground(QColor(TTK::UI::Color01));
46  QtItemSetTextAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
47  setItem(i, 1, item);
48  }
49 }
50 
52 {
53  Q_UNUSED(row);
54  Q_UNUSED(column);
55 
56  if(!isValid())
57  {
58  return;
59  }
60 
61  const QString &path = m_songs->at(currentRow()).path();
62  MusicSongsContainerWidget::instance()->addSongToPlaylist(QFile::exists(path) ? QStringList(path) : QStringList());
63 }
64 
66 {
67  MusicAbstractSongsListTableWidget::contextMenuEvent(event);
68 
69  QMenu menu(this);
70  menu.setStyleSheet(TTK::UI::MenuStyle02);
71  menu.addAction(QIcon(":/contextMenu/btn_play"), tr("Play"), this, SLOT(playClicked()));
72  menu.addSeparator();
73 
74  createMoreMenu(&menu);
75 
76  const bool status = !(m_songs->isEmpty() || TTK::String::isNetworkUrl(currentSongPath()));
77  menu.addAction(tr("Song Info..."), this, SLOT(showFileInformation()))->setEnabled(status);
78  menu.addAction(QIcon(":/contextMenu/btn_local_file"), tr("Open File Dir"), this, SLOT(openFileDir()))->setEnabled(status);
79  menu.addAction(QIcon(":/contextMenu/btn_ablum"), tr("Ablum"), this, SLOT(showAlbumQueryWidget()));
80  menu.addSeparator();
81 
82  menu.exec(QCursor::pos());
83 }
84 
85 
86 
88  : QWidget(parent)
89 {
91 
92  QVBoxLayout *layout = new QVBoxLayout(this);
93  layout->setSpacing(0);
94  layout->setContentsMargins(0, 0, 0, 0);
95 
96  QWidget *mainWidget = new QWidget(this);
97  QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
98  mainLayout->setSpacing(0);
99  mainLayout->setContentsMargins(0, 0, 0, 0);
100  mainWidget->setLayout(mainLayout);
101  layout->addWidget(mainWidget);
102  setLayout(layout);
103  //
104  QWidget *topWidget = new QWidget(mainWidget);
105  topWidget->setFixedHeight(60);
106  QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
107  topWidgetLayout->setContentsMargins(15, 10, 20, 10);
108 
109  QLabel *pLabel = new QLabel(tr("Playlist Backup"), topWidget);
110  QFont pLabelFont = pLabel->font();
111  pLabelFont.setPixelSize(20);
112  pLabel->setFont(pLabelFont);
113  pLabel->setStyleSheet(TTK::UI::ColorStyle01);
114 
115  QLabel *iLabel = new QLabel(tr("Automatically backup your music"), topWidget);
116  QFont iLabelFont = iLabel->font();
117  iLabelFont.setPixelSize(15);
118  iLabel->setFont(iLabelFont);
119  iLabel->setStyleSheet(TTK::UI::ColorStyle03);
120 
121  QPushButton *exportButton = new QPushButton(topWidget);
122  exportButton->setIcon(QIcon(":/toolSets/btn_detect_download_normal"));
123  exportButton->setCursor(QCursor(Qt::PointingHandCursor));
124  exportButton->setStyleSheet(TTK::UI::BorderStyle02);
125  exportButton->setFixedSize(25, 25);
126 
127  m_dateBox = new QComboBox(topWidget);
128  m_dateBox->setFixedSize(95, 25);
130 
131  m_timeBox = new QComboBox(topWidget);
132  m_timeBox->setFixedSize(80, 25);
134 
135  topWidgetLayout->addWidget(pLabel);
136  topWidgetLayout->addWidget(iLabel);
137  topWidgetLayout->addStretch(1);
138  topWidgetLayout->addWidget(exportButton);
139  topWidgetLayout->addWidget(m_dateBox);
140  topWidgetLayout->addWidget(m_timeBox);
141  topWidget->setLayout(topWidgetLayout);
142  mainLayout->addWidget(topWidget);
143  //
144  QFrame *frame = new QFrame(mainWidget);
145  frame->setFixedHeight(1);
146  frame->setFrameShape(QFrame::HLine);
147  frame->setFrameShadow(QFrame::Plain);
148  frame->setStyleSheet(TTK::UI::ColorStyle05);
149  mainLayout->addWidget(frame);
150  //
151  QWidget *functionWidget = new QWidget(mainWidget);
152  QHBoxLayout *functionWidgetLayout = new QHBoxLayout(functionWidget);
153  functionWidgetLayout->setContentsMargins(10, 0, 10, 0);
154  functionWidget->setLayout(functionWidgetLayout);
155  mainLayout->addWidget(functionWidget);
156  //
157  QWidget *listWidget = new QWidget(functionWidget);
158  listWidget->setFixedWidth(200);
159  QHBoxLayout *listWidgetLayout = new QHBoxLayout(listWidget);
160  listWidgetLayout->setContentsMargins(0, 10, 0, 10);
161  listWidget->setLayout(listWidgetLayout);
162 
163  m_listWidget = new QListWidget(listWidget);
165  m_listWidget->verticalScrollBar()->setStyleSheet(TTK::UI::ScrollBarStyle03);
166  m_listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
167  listWidgetLayout->addWidget(m_listWidget);
168  //
169  QFrame *hFrame = new QFrame(functionWidget);
170  hFrame->setFixedWidth(1);
171  hFrame->setFrameShape(QFrame::VLine);
172  hFrame->setFrameShadow(QFrame::Plain);
173  hFrame->setStyleSheet(TTK::UI::ColorStyle05);
174  //
175  QWidget *containerWidget = new QWidget(functionWidget);
176  QVBoxLayout *containerWidgetLayout = new QVBoxLayout(containerWidget);
177  containerWidgetLayout->setContentsMargins(10, 10, 10, 10);
178  containerWidget->setLayout(containerWidgetLayout);
179 
180  functionWidgetLayout->addWidget(listWidget);
181  functionWidgetLayout->addWidget(hFrame);
182  functionWidgetLayout->addWidget(containerWidget);
183  //
184  QWidget *containerTopWidget = new QWidget(containerWidget);
185  containerTopWidget->setFixedHeight(30);
186  m_tableWidget = new MusicPlaylistBackupTableWidget(containerWidget);
187 
188  containerWidgetLayout->addWidget(containerTopWidget);
189  containerWidgetLayout->addWidget(m_tableWidget);
190 
191  QHBoxLayout *containerTopWidgetLayout = new QHBoxLayout(containerTopWidget);
192  containerTopWidgetLayout->setContentsMargins(0, 0, 0, 0);
193  containerTopWidget->setLayout(containerTopWidgetLayout);
194  //
195  QFrame *blueFrame = new QFrame(containerTopWidget);
196  blueFrame->setFixedWidth(3);
197  blueFrame->setFrameShape(QFrame::VLine);
198  blueFrame->setFrameShadow(QFrame::Plain);
199  blueFrame->setStyleSheet(TTK::UI::ColorStyle08);
200 
201  m_titleLabel = new QLabel(containerTopWidget);
202  pLabelFont = m_titleLabel->font();
203  pLabelFont.setPixelSize(16);
204  m_titleLabel->setFont(pLabelFont);
205  //
206  QPushButton *restoreButton = new QPushButton(tr("Restore"), containerTopWidget);
207  restoreButton->setStyleSheet(TTK::UI::PushButtonStyle03);
208  restoreButton->setCursor(QCursor(Qt::PointingHandCursor));
209  restoreButton->setFixedSize(84, 26);
210  //
211  containerTopWidgetLayout->addWidget(blueFrame);
212  containerTopWidgetLayout->addWidget(m_titleLabel);
213  containerTopWidgetLayout->addStretch(1);
214  containerTopWidgetLayout->addWidget(restoreButton);
215 
216 #ifdef Q_OS_UNIX
217  exportButton->setFocusPolicy(Qt::NoFocus);
218  restoreButton->setFocusPolicy(Qt::NoFocus);
219 #endif
220 
221  connect(exportButton, SIGNAL(clicked()), SLOT(exportButtonClicked()));
222  connect(restoreButton, SIGNAL(clicked()), SLOT(restoreButtonClicked()));
223  connect(m_dateBox, SIGNAL(currentTextChanged(QString)), SLOT(currentDateChanged(QString)));
224  connect(m_timeBox, SIGNAL(currentIndexChanged(int)), SLOT(currentTimeChanged(int)));
225  connect(m_listWidget, SIGNAL(currentRowChanged(int)), SLOT(currentItemChanged(int)));
226 
227  initialize();
228 }
229 
231 {
232  delete m_dateBox;
233  delete m_timeBox;
234  delete m_titleLabel;
235  delete m_listWidget;
236  delete m_tableWidget;
237 }
238 
240 {
241  const int width = G_SETTING_PTR->value(MusicSettingManager::WidgetSize).toSize().width();
242  const int offset = width - WINDOW_WIDTH_MIN;
243  QHeaderView *headerView = m_tableWidget->horizontalHeader();
244  headerView->resizeSection(0, 372 + offset);
245 
246  const QString &text = m_titleLabel->toolTip();
247  if(!text.isEmpty())
248  {
249  m_titleLabel->setText(TTK::Widget::elidedTitleText(m_titleLabel->font(), text, 320 + offset));
250  }
251 }
252 
254 {
255  MusicMessageBox message;
256  message.setText(tr("Do you want to export the all backup list?"));
257  if(!message.exec())
258  {
259  return;
260  }
261 
262  const QString &path = TTK::File::getExistingDirectory(this);
263  if(!path.isEmpty())
264  {
265  TTK::File::copyPath(ROOT_PATH, path + "/playlist", true);
266  }
267 }
268 
270 {
271  MusicMessageBox message;
272  message.setText(tr("Do you want to restore the original list after the restoration is overwritten?"));
273  if(!message.exec())
274  {
275  return;
276  }
277 
278  int index = m_listWidget->currentRow();
279  if(index < 0 || index > m_items.count())
280  {
281  return;
282  }
283 
285 }
286 
288 {
289  m_timeBox->blockSignals(true);
290  m_timeBox->clear();
291  m_timeBox->blockSignals(false);
292 
293  QDir dir(ROOT_PATH + text);
294  for(const QFileInfo &fin : dir.entryInfoList(QDir::Files, QDir::Time | QDir::Reversed))
295  {
296  m_timeBox->addItem(TTKDateTime::format(fin.baseName().toULongLong(), TTK_TIMES_FORMAT), fin.baseName());
297  }
298 
299  m_listWidget->setCurrentRow(0);
300 }
301 
303 {
304  if(index < 0 || index >= m_timeBox->count())
305  {
306  return;
307  }
308 
309  m_items.clear();
310  m_listWidget->blockSignals(true);
311  m_listWidget->clear();
312  m_listWidget->blockSignals(false);
313 
314  MusicTKPLConfigManager manager;
315  if(!manager.fromFile(ROOT_PATH + m_dateBox->currentText() + TTK_SEPARATOR + m_timeBox->itemData(index).toString() + TKF_FILE))
316  {
317  TTK_LOG_STREAM("Backup playlist read error");
318  return;
319  }
320 
321  manager.readBuffer(m_items);
322 
323  for(const MusicSongItem &item : qAsConst(m_items))
324  {
325  const QString &text = QString("%1[%2]").arg(item.m_itemName).arg(item.m_songs.count());
326  QListWidgetItem *it = new QListWidgetItem(m_listWidget);
327  it->setToolTip(text);
328  it->setText(TTK::Widget::elidedTitleText(it->font(), text, TTKStaticCast(QWidget*, m_listWidget->parent())->width() - 5));
329  m_listWidget->addItem(it);
330  }
331 
332  m_listWidget->setCurrentRow(0);
333 }
334 
336 {
337  if(index < 0 || index >= m_items.count())
338  {
339  return;
340  }
341 
342  MusicSongItem &item = m_items[index];
344 
345  const int width = G_SETTING_PTR->value(MusicSettingManager::WidgetSize).toSize().width();
346  const QString &text = QString("%1[%2]").arg(item.m_itemName).arg(item.m_songs.count());
347  m_titleLabel->setToolTip(text);
348  m_titleLabel->setText(TTK::Widget::elidedTitleText(m_titleLabel->font(), text, 320 + width - WINDOW_WIDTH_MIN));
349 }
350 
352 {
353  QDir dir(ROOT_PATH);
354  if(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time | QDir::Reversed).isEmpty())
355  {
357  module.runBackup();
358  }
359 
360  m_dateBox->addItems(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time | QDir::Reversed));
361 }
TTK_MODULE_EXPORT QString getExistingDirectory(QWidget *parent)
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
static const QString ListWidgetStyle02
void setText(const QString &text) const
#define TTK_TIMES_FORMAT
Definition: ttkglobal.h:229
virtual void contextMenuEvent(QContextMenuEvent *event) overridefinal
TTK_MODULE_EXPORT void generateComboBoxStyle(QComboBox *widget, const QString &style={})
bool fromFile(const QString &name)
The class of the messagebox widget.
#define QtItemSetTextAlignment(p, a)
Item text alignment.
Definition: ttkqtcompat.h:45
static const QString ColorStyle05
Definition: musicuiobject.h:49
virtual void resizeWidget() overridefinal
#define ROOT_PATH
void updateSongItem(const MusicSongItem &item)
TTK_MODULE_EXPORT bool copyPath(const QString &srcPath, const QString &dstPath, bool overwrite)
virtual bool readBuffer(MusicSongItemList &items) overridefinal
static const QString ScrollBarStyle03
static const QString ColorStyle01
Color.
Definition: musicuiobject.h:37
void addSongToPlaylist(const QStringList &items)
voidpf uLong offset
Definition: ioapi.h:142
#define qAsConst
Definition: ttkqtglobal.h:51
static const QString ColorStyle02
Definition: musicuiobject.h:40
virtual void updateSongsList(const MusicSongList &songs) overridefinal
static const QString PushButtonStyle03
virtual void setSongsList(MusicSongList *songs)
The class of the playlist backup module.
MusicPlaylistBackupTableWidget * m_tableWidget
virtual void runBackup() overridefinal
#define TTK_SEPARATOR
Definition: ttkglobal.h:196
static MusicSongsContainerWidget * instance()
#define TTK_LOG_STREAM(msg)
Definition: ttklogger.h:64
TTK_MODULE_EXPORT QString elidedText(const QFont &font, const QString &text, Qt::TextElideMode mode, int width)
void itemDoubleClicked(int row, int column)
static const QString ColorStyle08
Definition: musicuiobject.h:58
QString duration() const noexcept
Definition: musicsong.h:106
static const QString BackgroundStyle10
QString name() const noexcept
Definition: musicsong.h:82
static const QString MenuStyle02
static const QString ColorStyle03
Definition: musicuiobject.h:43
The class of the music song item.
Definition: musicsong.h:161
The class of the playlist backup table widget.
The class of the tkpl config manager.
MusicSongList m_songs
Definition: musicsong.h:167
static const QString BorderStyle02
MusicPlaylistBackupTableWidget(QWidget *parent=nullptr)
TTK_MODULE_EXPORT QString elidedTitleText(const QFont &font, const QString &text, int width)
#define WINDOW_WIDTH_MIN
Definition: musicobject.h:155
QString m_itemName
Definition: musicsong.h:165
static constexpr unsigned int Color01
Color QRgb.
Definition: musicuiobject.h:32
The class of the music song info.
Definition: musicsong.h:28
void currentDateChanged(const QString &text)
static QString format(const QString &time, const QString &format)
Definition: ttktime.cpp:254
MusicPlaylistBackupWidget(QWidget *parent=nullptr)
The class of the songs list abstract table widget.
TTK_MODULE_EXPORT bool isNetworkUrl(const QString &path)
#define TKF_FILE
Definition: musicobject.h:58
#define G_SETTING_PTR