TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiclrcphotomanagerwidget.cpp
Go to the documentation of this file.
2 #include "ui_musiclrcphotomanagerwidget.h"
4 #include "musictoastlabel.h"
5 #include "musicfileutils.h"
6 #include "ttktime.h"
7 
9  : QLabel(parent),
10  m_isSelected(false)
11 {
12  setFixedSize(137, 100);
13  setCursor(Qt::PointingHandCursor);
14 }
15 
16 void MusicLrcPhotoItem::updatePixmap(const QString &path)
17 {
18  m_path = path;
19  setPixmap(QPixmap(path).scaled(size()));
20 }
21 
22 void MusicLrcPhotoItem::setSelected(bool v) noexcept
23 {
24  m_isSelected = v;
25 }
26 
27 void MusicLrcPhotoItem::mousePressEvent(QMouseEvent *event)
28 {
29  Q_UNUSED(event);
31  update();
32 }
33 
34 void MusicLrcPhotoItem::paintEvent(QPaintEvent *event)
35 {
36  QLabel::paintEvent(event);
37  if(m_isSelected)
38  {
39  QPainter painter(this);
40  painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
41  painter.fillRect(rect(), QColor(0, 0, 0, 155));
42  painter.drawPixmap(width() - 16 - 4, height() - 16 - 4, 16, 16, QPixmap(":/lrc/lb_photo_checked"));
43  }
44 }
45 
46 
48  : QWidget(parent)
49 {
50  m_gridLayout = new QGridLayout(this);
51  m_gridLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
52  m_gridLayout->setContentsMargins(7, 0, 7, 0);
53  setLayout(m_gridLayout);
54 
55  initialize();
56 }
57 
59 {
60  qDeleteAll(m_items);
61  delete m_gridLayout;
62 }
63 
64 void MusicLrcPhotoWidget::addCellItem(const QString &path)
65 {
66  MusicLrcPhotoItem *item = new MusicLrcPhotoItem(this);
67  item->updatePixmap(path);
68 
69  m_gridLayout->addWidget(item, m_items.count() / MIN_ITEM_COUNT, m_items.count() % MIN_ITEM_COUNT, Qt::AlignLeft | Qt::AlignTop);
70  m_items << item;
71 }
72 
74 {
75  if(m_items.count() >= MAX_IMAGE_COUNT)
76  {
77  MusicToastLabel::popup(tr("Exceeded the maximum number limit"));
78  return;
79  }
80 
81  const QStringList &files = TTK::File::getOpenFileNames(this);
82  if(files.isEmpty())
83  {
84  return;
85  }
86 
87  int offset = MAX_IMAGE_COUNT - m_items.count();
88  if(files.count() < offset)
89  {
90  offset = files.count();
91  }
92 
93  for(int i = 0; i < offset; ++i)
94  {
95  const QString &name = G_BACKGROUND_PTR->artistName();
96  const QString &path = BACKGROUND_DIR_FULL + name + "-" + QString::number(m_items.count()) + SKN_FILE;
97  QFile::copy(files[i], path);
98 
99  addCellItem(path);
100  }
101 
102  G_BACKGROUND_PTR->updateArtistImageList();
103 }
104 
106 {
107  if(!isValid())
108  {
109  MusicToastLabel::popup(tr("Please select one item first"));
110  return;
111  }
112 
113  for(int i = 0; i < m_items.count(); ++i)
114  {
115  MusicLrcPhotoItem *item = m_items[i];
116  if(item->isSelected())
117  {
118  QFile::remove(item->path());
119  m_gridLayout->removeWidget(item);
120  m_items.takeAt(i--)->deleteLater();
121  }
122  }
123 
124  for(int i = 0; i < m_items.count(); ++i)
125  {
126  MusicLrcPhotoItem *item = m_items[i];
127  const QString &before = item->path();
128  const QString &after = item->pathRef().replace(before.length() - 4 - 1, 1, QString::number(i));
129  QFile::rename(before, after);
130  m_gridLayout->addWidget(item, i / MIN_ITEM_COUNT, i % MIN_ITEM_COUNT, Qt::AlignLeft | Qt::AlignTop);
131  }
132 
133  G_BACKGROUND_PTR->updateArtistImageList();
134  MusicToastLabel::popup(tr("Delete current file success"));
135 }
136 
138 {
139  if(!isValid())
140  {
141  MusicToastLabel::popup(tr("Please select one item first"));
142  return;
143  }
144 
145  const QString &path = TTK::File::getExistingDirectory(this);
146  if(path.isEmpty())
147  {
148  return;
149  }
150 
151  const QString &dir = path + "/images/";
152  QDir().mkpath(dir);
153 
154  for(MusicLrcPhotoItem *item : qAsConst(m_items))
155  {
156  if(item->isSelected())
157  {
158  QString suffix = JPG_FILE;
159  QFile file(item->path());
160  if(!file.open(QIODevice::ReadOnly))
161  {
162  continue;
163  }
164 
165  const QByteArray &header = file.read(4);
166  if(header.startsWith(QByteArray::fromHex("FFD8")))
167  {
168  suffix = JPG_FILE;
169  }
170  else if(header.startsWith(QByteArray::fromHex("89504E47")))
171  {
172  suffix = PNG_FILE;
173  }
174 
175  file.copy(dir + QString::number(TTKDateTime::currentTimestamp()) + suffix);
176  file.close();
177 
179  }
180  }
181 
182  MusicToastLabel::popup(tr("Export current file success"));
183 }
184 
186 {
187  for(const QString &path : G_BACKGROUND_PTR->artistImageList())
188  {
189  addCellItem(path);
190  }
191 }
192 
194 {
195  for(MusicLrcPhotoItem *item : qAsConst(m_items))
196  {
197  if(item->isSelected())
198  {
199  return true;
200  }
201  }
202  return false;
203 }
204 
205 
207  : MusicAbstractMoveDialog(parent),
208  m_ui(new Ui::MusicLrcPhotoManagerWidget)
209 {
210  m_ui->setupUi(this);
211  setFixedSize(size());
212  setBackgroundLabel(m_ui->background);
213 
214  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
215  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
216  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
217  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
218  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
219 
220  m_ui->addButton->setStyleSheet(TTK::UI::PushButtonStyle04);
221  m_ui->deleteButton->setStyleSheet(TTK::UI::PushButtonStyle04);
222  m_ui->exportButton->setStyleSheet(TTK::UI::PushButtonStyle04);
223  m_ui->okButton->setStyleSheet(TTK::UI::PushButtonStyle04);
224 #ifdef Q_OS_UNIX
225  m_ui->addButton->setFocusPolicy(Qt::NoFocus);
226  m_ui->deleteButton->setFocusPolicy(Qt::NoFocus);
227  m_ui->exportButton->setFocusPolicy(Qt::NoFocus);
228  m_ui->okButton->setFocusPolicy(Qt::NoFocus);
229 #endif
230  m_ui->artTextLabel->setText(G_BACKGROUND_PTR->artistName());
231  m_ui->artTextLabel->setStyleSheet(TTK::UI::ColorStyle07);
232 
233  m_widget = new MusicLrcPhotoWidget(this);
235 
236  connect(m_ui->addButton, SIGNAL(clicked()), m_widget, SLOT(addButtonClicked()));
237  connect(m_ui->deleteButton, SIGNAL(clicked()), m_widget, SLOT(deleteButtonClicked()));
238  connect(m_ui->exportButton, SIGNAL(clicked()), m_widget, SLOT(exportButtonClicked()));
239  connect(m_ui->okButton, SIGNAL(clicked()), SLOT(close()));
240 }
241 
243 {
244  delete m_ui;
245  delete m_widget;
246 }
void setSelected(bool v) noexcept
TTK_MODULE_EXPORT QString getExistingDirectory(QWidget *parent)
The class of the lrc photo widget.
The class of the lrc art photo upload.
#define PNG_FILE
Definition: musicobject.h:72
static qint64 currentTimestamp() noexcept
Definition: ttktime.cpp:249
#define G_BACKGROUND_PTR
void addCellItem(const QString &path)
QString path() const noexcept
bool isSelected() const noexcept
voidpf void uLong size
Definition: ioapi.h:136
The class of the lrc photo pixmap item.
void updatePixmap(const QString &path)
void setBackgroundLabel(QLabel *label) noexcept
virtual void paintEvent(QPaintEvent *event) overridefinal
static void popup(const QString &text)
voidpf uLong offset
Definition: ioapi.h:142
QList< MusicLrcPhotoItem * > m_items
#define qAsConst
Definition: ttkqtglobal.h:57
const char * name
Definition: http_parser.c:458
TTK_MODULE_EXPORT QString suffix(const QString &name)
#define MAX_IMAGE_COUNT
QString & pathRef() noexcept
TTK_MODULE_EXPORT void sleep(int ms)
static const QString PushButtonStyle04
MusicLrcPhotoItem(QWidget *parent=nullptr)
#define BACKGROUND_DIR_FULL
Definition: musicobject.h:148
TTK_MODULE_EXPORT void generateVScrollAreaStyle(QWidget *widget, QWidget *parent, bool background=true)
The class of the moving dialog base.
static const QString ColorStyle07
Definition: musicuiobject.h:55
Ui::MusicLrcPhotoManagerWidget * m_ui
MusicLrcPhotoWidget(QWidget *parent=nullptr)
#define MIN_ITEM_COUNT
virtual void mousePressEvent(QMouseEvent *event) overridefinal
static const QString ToolButtonStyle04
TTK_MODULE_EXPORT QStringList getOpenFileNames(QWidget *parent, const QString &filter="Image Files (*.png *.bmp *.jpg)")
MusicLrcPhotoManagerWidget(QWidget *parent=nullptr)
#define SKN_FILE
Definition: musicobject.h:70
#define JPG_FILE
Definition: musicobject.h:71
#define TTK_DN_MS
Definition: ttkglobal.h:349