TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicpagequerywidget.cpp
Go to the documentation of this file.
1 #include "musicpagequerywidget.h"
2 #include "musicwidgetheaders.h"
3 #include "musicwidgetutils.h"
4 #include "ttkclickedgroup.h"
5 #include "ttkclickedlabel.h"
6 
7 static constexpr int MAX_PAGE_SIZE = 10;
8 
10  : QObject(parent),
11  m_currentPage(0),
12  m_pageWidget(nullptr)
13 {
14 
15 }
16 
18 {
19  qDeleteAll(m_pageItems);
20  delete m_pageWidget;
21 }
22 
23 QWidget* MusicPageQueryWidget::createPageWidget(QWidget *parent, int total)
24 {
25  m_pageWidget = new QWidget(parent);
26  QHBoxLayout *layout = new QHBoxLayout(m_pageWidget);
27  layout->setContentsMargins(0, 20, 0, 20);
28  layout->setSpacing(12);
29 
30  for(int i = 1; i <= MAX_PAGE_SIZE; ++i)
31  {
32  m_pageItems << (new TTKClickedLabel(QString::number(i), m_pageWidget));
33  }
35 
36  TTKClickedGroup *group = new TTKClickedGroup(this);
37  connect(group, SIGNAL(clicked(int)), SIGNAL(clicked(int)));
38 
39  for(TTKClickedLabel *label : qAsConst(m_pageItems))
40  {
41  QFont font(label->font());
42  font.setPixelSize(14);
43  label->setFont(font);
44  label->setStyleSheet(TTK::UI::ColorStyle04);
45  label->setFixedWidth(TTK::Widget::fontTextWidth(font, label->text()));
46  group->mapped(label);
47  }
48 
49  m_pageItems[MAX_PAGE_SIZE]->hide();
50  if(total <= MAX_PAGE_SIZE)
51  {
52  m_pageItems[MAX_PAGE_SIZE + 1]->hide();
53 
54  for(int i = MAX_PAGE_SIZE - 1; i >= total; --i)
55  {
56  m_pageItems[i]->hide();
57  }
58  }
59 
61  layout->addStretch(1);
62 
63  if(total != 0)
64  {
65  layout->addWidget(m_pageItems[MAX_PAGE_SIZE]);
66 
67  for(int i = 0; i < MAX_PAGE_SIZE; ++i)
68  {
69  layout->addWidget(m_pageItems[i]);
70  }
71 
72  layout->addWidget(m_pageItems[MAX_PAGE_SIZE + 1]);
73  }
74  else
75  {
76  QWidget *func = new QWidget(m_pageWidget);
77  QVBoxLayout *funcLayout = new QVBoxLayout(func);
78 
79  QLabel *icon = new QLabel(func);
80  icon->setPixmap(QPixmap(":/lrc/lb_no_results"));
81  QLabel *text = new QLabel(tr("There is empty"), func);
82  text->setStyleSheet(TTK::UI::ColorStyle04 + TTK::UI::FontStyle04);
83 
84  text->setAlignment(Qt::AlignCenter);
85  funcLayout->addWidget(icon, 0, Qt::AlignCenter);
86  funcLayout->addWidget(text, 0, Qt::AlignCenter);
87  func->setLayout(funcLayout);
88  layout->addWidget(func);
89  }
90 
91  layout->addStretch(1);
92  m_pageWidget->setLayout(layout);
93  return m_pageWidget;
94 }
95 
97 {
98  if(m_pageItems.isEmpty())
99  {
100  return;
101  }
102 
103  m_currentPage = 0;
104  for(TTKClickedLabel *label : qAsConst(m_pageItems))
105  {
106  label->hide();
107  }
108 
109  if(total > MAX_PAGE_SIZE)
110  {
111  total = MAX_PAGE_SIZE;
112  m_pageItems[MAX_PAGE_SIZE + 1]->show();
113  }
114 
115  for(int i = 0; i < total; ++i)
116  {
117  m_pageItems[i]->setText(QString::number(i + 1));
118  m_pageItems[i]->show();
119  }
120 
121  updateStatus();
122 }
123 
124 void MusicPageQueryWidget::page(int index, int total)
125 {
126  if(total <= 0 || m_pageItems.isEmpty())
127  {
128  return;
129  }
130 
131  int page = m_pageItems[0]->text().toInt();
132 
133  switch(index)
134  {
135  case 0:
136  case 1:
137  case 2:
138  case 3:
139  case 4:
140  case 5:
141  case 6:
142  case 7:
143  case 8:
144  case 9:
145  {
146  m_currentPage = index;
147  break;
148  }
149  case 10:
150  {
151  page -= MAX_PAGE_SIZE;
153 
154  if(total <= MAX_PAGE_SIZE)
155  {
156  label->hide();
157  }
158  else
159  {
160  for(int i = 0; i < MAX_PAGE_SIZE; ++i)
161  {
162  m_pageItems[i]->setText(QString::number(page + i));
163  m_pageItems[i]->show();
164  }
165 
166  m_pageItems[0]->text().toInt() < MAX_PAGE_SIZE ? label->hide() : label->show();
167  }
168 
169  m_currentPage = 0;
170  m_pageItems[MAX_PAGE_SIZE + 1]->show();
171  break;
172  }
173  case 11:
174  {
175  page += MAX_PAGE_SIZE;
177  int boundary = total - page + 1;
178  boundary = boundary < MAX_PAGE_SIZE ? boundary : MAX_PAGE_SIZE;
179 
180  for(int i = 0; i < boundary; ++i)
181  {
182  m_pageItems[i]->setText(QString::number(page + i));
183  }
184 
185  if(total - page >= MAX_PAGE_SIZE)
186  {
187  label->show();
188  }
189  else
190  {
191  label->hide();
192  //
193  for(int i = MAX_PAGE_SIZE - 1; i > total - page; --i)
194  {
195  m_pageItems[i]->hide();
196  }
197  }
198 
199  m_currentPage = 0;
200  m_pageItems[MAX_PAGE_SIZE]->show();
201  break;
202  }
203  default: break;
204  }
205 
206  updateStatus();
207 }
208 
210 {
211  return m_pageItems[m_currentPage]->text().toInt();
212 }
213 
215 {
216  for(int i = 0; i < m_pageItems.count() - 2; ++i)
217  {
218  TTKClickedLabel *label = m_pageItems[i];
219  label->setStyleSheet(TTK::UI::ColorStyle04);
220  label->setFixedWidth(TTK::Widget::fontTextWidth(label->font(), label->text()));
221  }
222 
224  label->setStyleSheet(TTK::UI::ColorStyle04 + TTK::UI::FontStyle01);
225  label->setFixedWidth(label->width() + 5);
226 }
void clicked(int index)
static const QString ColorStyle04
Definition: musicuiobject.h:46
The class of the label widget that can click.
static const QString FontStyle01
Font.
MusicPageQueryWidget(QObject *parent=nullptr)
#define qAsConst
Definition: ttkqtglobal.h:53
static constexpr int MAX_PAGE_SIZE
void page(int index, int total)
static const QString FontStyle04
QList< TTKClickedLabel * > m_pageItems
QWidget * createPageWidget(QWidget *parent, int total)
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
The class of the label widget click group mapper.