TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicsongsearchpopwidget.cpp
Go to the documentation of this file.
3 #include "musicwidgetheaders.h"
4 
5 static constexpr int MAX_ITEM_COUNT = 7;
6 
9 {
10  setColumnCount(2);
11 
12  QHeaderView *headerView = horizontalHeader();
13  headerView->resizeSection(0, 215);
14  headerView->resizeSection(1, 62);
15 
16  TTK::Widget::setTransparent(this, 255);
17 }
18 
20 {
21  removeItems();
22 }
23 
25 {
26  const int row = rowCount();
27  if(row <= 0)
28  {
29  return;
30  }
31 
32  int index = currentRow();
33  if(up)
34  {
35  --index;
36  if(index < 0)
37  {
38  index = 0;
39  }
40  }
41  else
42  {
43  ++index;
44  if(index >= row)
45  {
46  index = row - 1;
47  }
48  }
49 
50  MusicAbstractTableWidget::selectRow(index);
51  Q_EMIT setText(item(index, 0)->toolTip().trimmed(), false);
52 }
53 
54 void MusicSongSearchPopTableWidget::addCellItem(int index, const QString &name, const QString &time)
55 {
56  setRowHeight(index, TTK_ITEM_SIZE_M);
57 
58  QHeaderView *headerView = horizontalHeader();
59  QTableWidgetItem *item = new QTableWidgetItem(TTK::Widget::elidedText(font(), " " + name, Qt::ElideRight, headerView->sectionSize(0) - 20));
60  item->setToolTip(name);
61  item->setForeground(QColor(TTK::UI::Color02));
62  QtItemSetTextAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
63  setItem(index, 0, item);
64 
65  item = new QTableWidgetItem(time);
66  item->setForeground(QColor(0xAA, 0xAA, 0xAA));
67  QtItemSetTextAlignment(item, Qt::AlignCenter);
68  setItem(index, 1, item);
69 }
70 
72 {
74  Q_EMIT setText(item(row, 0)->toolTip().trimmed(), true);
75 }
76 
78 {
80  setColumnCount(2);
81 }
82 
83 
84 
86  : QWidget(parent)
87 {
88  move(405, 45);
89 
90  QVBoxLayout *layout = new QVBoxLayout(this);
91  layout->setContentsMargins(1, 1, 1, 1);
92  layout->setSpacing(0);
93 
95  m_tableWidget->setFixedWidth(285);
96 
97  m_clearButton = new QPushButton(" " + tr("Clear History"), this);
98  m_clearButton->setFocusPolicy(Qt::ClickFocus);
99  m_clearButton->setCursor(Qt::PointingHandCursor);
100  m_clearButton->setFixedHeight(35);
102 
103  m_frame = new QFrame(this);
104  m_frame->setFixedHeight(1);
105  m_frame->setStyleSheet(TTK::UI::ColorStyle05);
106  m_frame->setFrameShape(QFrame::HLine);
107 
108  layout->addWidget(m_tableWidget);
109  layout->addWidget(m_frame);
110  layout->addWidget(m_clearButton);
111  setLayout(layout);
112 
113  connect(m_clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked()));
114 }
115 
117 {
118  delete m_tableWidget;
119  delete m_clearButton;
120 }
121 
123 {
124  if(parent)
125  {
126  connect(m_tableWidget, SIGNAL(setText(QString,bool)), parent, SLOT(selectedTextChanged(QString,bool)), Qt::UniqueConnection);
127  }
128 
129  setControlEnabled(true);
131 
133  if(!manager.fromFile(SEARCH_PATH_FULL))
134  {
135  return;
136  }
137 
138  MusicSearchRecordList records;
139  manager.readBuffer(records);
140 
141  const int count = records.count();
142  m_tableWidget->setRowCount(count);
143 
144  resize(m_tableWidget->width() + 2, count == 0 ? 0 : (count < MAX_ITEM_COUNT ? count * TTK_ITEM_SIZE_M + 45 : (MAX_ITEM_COUNT + 1) * TTK_ITEM_SIZE_M + 8));
145 
146  for(int i = 0; i < count; ++i)
147  {
148  m_tableWidget->addCellItem(i, records[i].m_name, utcTimeToLocal(records[i].m_timestamp));
149  }
150 }
151 
153 {
155 }
156 
158 {
159  if(enabled)
160  {
161  m_frame->show();
162  m_clearButton->show();
163  }
164  else
165  {
166 
167  m_frame->hide();
168  m_clearButton->hide();
169  }
170 }
171 
172 void MusicSongSearchPopWidget::addCellItems(const QStringList &names)
173 {
174  setControlEnabled(false);
176 
177  const int count = names.count();
178  m_tableWidget->setRowCount(count);
179 
180  resize(m_tableWidget->width() + 2, count == 0 ? 0 : (count * TTK_ITEM_SIZE_M + 8));
181 
182  for(int i = 0; i < count; ++i)
183  {
184  m_tableWidget->addCellItem(i, names[i], {});
185  }
186 }
187 
188 QString MusicSongSearchPopWidget::utcTimeToLocal(const QString &time) const
189 {
190  const qint64 t = (TTKDateTime::currentTimestamp() - time.toLongLong()) / TTK_DN_S2MS;
191  if(t < TTK_DN_M2S)
192  {
193  return QString::number(t) + tr("ss");
194  }
195  else if(TTK_DN_M2S <= t && t < TTK_DN_H2S)
196  {
197  return QString::number(t / TTK_DN_M2S) + tr("mm");
198  }
199  else if(TTK_DN_H2S <= t && t < TTK_DN_D2S)
200  {
201  return QString::number(t / TTK_DN_H2S) + tr("hh");
202  }
203  else
204  {
205  return QString::number(t / TTK_DN_D2S) + tr("day");
206  }
207 }
208 
210 {
212  if(!manager.load(SEARCH_PATH_FULL))
213  {
214  return;
215  }
216 
217  manager.writeBuffer(MusicSearchRecordList());
218  close();
219 }
220 
221 void MusicSongSearchPopWidget::paintEvent(QPaintEvent *event)
222 {
223  QWidget::paintEvent(event);
224 
225  QPainter painter(this);
226  painter.setPen(Qt::gray);
227  painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
228 }
virtual bool writeBuffer(const MusicSearchRecordList &items) overridefinal
static const QString BorderStyle01
border
MusicSongSearchPopWidget(QWidget *parent=nullptr)
static constexpr int MAX_ITEM_COUNT
bool fromFile(const QString &name)
The class of the table widget base.
void addCellItem(int index, const QString &name, const QString &time)
void addCellItems(const QStringList &names)
MusicSongSearchPopTableWidget(QWidget *parent=nullptr)
#define QtItemSetTextAlignment(p, a)
Item text alignment.
Definition: ttkqtcompat.h:45
static const QString ColorStyle05
Definition: musicuiobject.h:49
static constexpr unsigned int Color02
Definition: musicuiobject.h:34
The class of the search history Config.
virtual void itemCellClicked(int row, int column) overridefinal
virtual bool readBuffer(MusicSearchRecordList &items) overridefinal
const char * name
Definition: http_parser.c:458
MusicSongSearchPopTableWidget * m_tableWidget
virtual void paintEvent(QPaintEvent *event) overridefinal
QString utcTimeToLocal(const QString &time) const
void setText(const QString &text)
TTK_MODULE_EXPORT void setTransparent(QWidget *widget, int alpha)
virtual void removeItems() overridefinal
virtual void itemCellClicked(int row, int column)
TTK_MODULE_EXPORT QString elidedText(const QFont &font, const QString &text, Qt::TextElideMode mode, int width)
void setText(const QString &text, bool update)
#define TTK_DN_H2S
Definition: ttkglobal.h:288
static const QString BackgroundStyle10
#define TTK_DN_D2S
Definition: ttkglobal.h:296
static const QString ColorStyle03
Definition: musicuiobject.h:43
static qint64 currentTimestamp()
Definition: ttktime.cpp:249
#define SEARCH_PATH_FULL
Definition: musicobject.h:138
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
static const QString FontStyle02
bool load(const QString &name)
static const QString CustomStyle01
Custom.
#define TTK_DN_M2S
Definition: ttkglobal.h:281
The class of the search popup table widget.
#define TTK_ITEM_SIZE_M
Definition: ttkglobal.h:242