| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kloopplaypage.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QDebug> |
| 27 |
#include <QMouseEvent> |
| 28 |
#include <QPainter> |
| 29 |
#include <QScrollArea> |
| 30 |
#include <QScrollBar> |
| 31 |
#include <QStackedLayout> |
| 32 |
#include <QTimer> |
| 33 |
|
| 34 |
namespace kdk |
| 35 |
{ |
| 36 |
|
| 37 |
class Q_DECL_HIDDEN CircleButton : public QPushButton |
| 38 |
{ |
| 39 |
Q_OBJECT |
| 40 |
public: |
| 41 |
enum ButtonType { |
| 42 |
CircleType, |
| 43 |
NormalType |
| 44 |
}; |
| 45 |
|
| 46 |
CircleButton(QWidget *parent = nullptr); |
| 47 |
|
| 48 |
void setButtonType(ButtonType type); |
| 49 |
|
| 50 |
protected: |
| 51 |
void paintEvent(QPaintEvent *event); |
| 52 |
|
| 53 |
private: |
| 54 |
ButtonType m_type; |
| 55 |
}; |
| 56 |
|
| 57 |
class Q_DECL_HIDDEN Widget : public QWidget |
| 58 |
{ |
| 59 |
Q_OBJECT |
| 60 |
public: |
| 61 |
Widget(QWidget *parent = nullptr); |
| 62 |
|
| 63 |
Q_SIGNALS: |
| 64 |
void leftClick(); |
| 65 |
|
| 66 |
protected: |
| 67 |
void mousePressEvent(QMouseEvent *event); |
| 68 |
void paintEvent(QPaintEvent *event); |
| 69 |
}; |
| 70 |
|
| 71 |
class Q_DECL_HIDDEN KLoopPlayPagePrivate : public QObject, public ThemeController |
| 72 |
{ |
| 73 |
Q_OBJECT |
| 74 |
Q_DECLARE_PUBLIC(KLoopPlayPage) |
| 75 |
public: |
| 76 |
KLoopPlayPagePrivate(KLoopPlayPage *parent); |
| 77 |
|
| 78 |
void updateWidget(int i); |
| 79 |
void updateButton(); |
| 80 |
void updateShowPosition(); |
| 81 |
|
| 82 |
private: |
| 83 |
KLoopPlayPage *q_ptr; |
| 84 |
Widget *m_pWidget1; |
| 85 |
Widget *m_pWidget2; |
| 86 |
Widget *m_pWidget3; |
| 87 |
QWidget *m_pScrollWidget; |
| 88 |
QHBoxLayout *m_pWidgetLayout; |
| 89 |
QHBoxLayout *m_pButtonLayout; |
| 90 |
QVBoxLayout *m_pLayout; |
| 91 |
QList<QWidget *> m_WidgetList; |
| 92 |
CircleButton *m_pCircleButton; |
| 93 |
QList<CircleButton *> m_ButtonList; |
| 94 |
QScrollArea *m_pScrollArea; |
| 95 |
int m_index; |
| 96 |
int m_spacing; |
| 97 |
int m_timer; |
| 98 |
QTimer *m_pTimer; |
| 99 |
}; |
| 100 |
|
| 101 |
KLoopPlayPagePrivate::KLoopPlayPagePrivate(KLoopPlayPage *parent) |
| 102 |
: q_ptr(parent) |
| 103 |
, m_index(0) |
| 104 |
, m_spacing(24) |
| 105 |
, m_timer(3000) |
| 106 |
{ |
| 107 |
Q_Q(KLoopPlayPage); |
| 108 |
|
| 109 |
m_pLayout = new QVBoxLayout(q); |
| 110 |
m_pLayout->setSpacing(16); |
| 111 |
|
| 112 |
m_pScrollWidget = new QWidget(); |
| 113 |
m_pScrollWidget->setContentsMargins(0, 0, 0, 0); |
| 114 |
|
| 115 |
m_pScrollArea = new QScrollArea(q); |
| 116 |
m_pScrollArea->setFrameStyle(QFrame::NoFrame); |
| 117 |
m_pScrollArea->setWidgetResizable(true); |
| 118 |
m_pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 119 |
m_pScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 120 |
m_pScrollArea->setWidget(m_pScrollWidget); |
| 121 |
|
| 122 |
m_pWidgetLayout = new QHBoxLayout(m_pScrollWidget); |
| 123 |
m_pWidgetLayout->setSpacing(0); |
| 124 |
|
| 125 |
m_pButtonLayout = new QHBoxLayout(q); |
| 126 |
m_pButtonLayout->setSpacing(6); |
| 127 |
|
| 128 |
m_pLayout->addWidget(m_pScrollArea); |
| 129 |
m_pLayout->addLayout(m_pButtonLayout); |
| 130 |
|
| 131 |
m_pTimer = new QTimer(q); |
| 132 |
m_pTimer->start(m_timer); |
| 133 |
connect(m_pTimer, &QTimer::timeout, this, [=] { |
| 134 |
q->setCurrentWidget(q->currentWidget() + 1); |
| 135 |
}); |
| 136 |
} |
| 137 |
|
| 138 |
void KLoopPlayPagePrivate::updateWidget(int i) |
| 139 |
{ |
| 140 |
Q_Q(KLoopPlayPage); |
| 141 |
if (m_WidgetList.count() < 3) |
| 142 |
return; |
| 143 |
|
| 144 |
QLayoutItem *child; |
| 145 |
while ((child = m_pWidgetLayout->takeAt(0)) != 0) { |
| 146 |
// 删除Stretch(弹簧)等布局 |
| 147 |
if (child->spacerItem()) { |
| 148 |
m_pWidgetLayout->removeItem(child); |
| 149 |
continue; |
| 150 |
} |
| 151 |
// 删除布局 |
| 152 |
m_pWidgetLayout->removeWidget(child->widget()); |
| 153 |
child->widget()->setParent(nullptr); |
| 154 |
delete child; |
| 155 |
child = nullptr; |
| 156 |
} |
| 157 |
|
| 158 |
int prev; |
| 159 |
int next; |
| 160 |
if (i == 0) { |
| 161 |
prev = m_WidgetList.count() - 1; |
| 162 |
next = i + 1; |
| 163 |
} else if (i == m_WidgetList.count() - 1) { |
| 164 |
prev = i - 1; |
| 165 |
next = 0; |
| 166 |
} else { |
| 167 |
prev = i - 1; |
| 168 |
next = i + 1; |
| 169 |
} |
| 170 |
|
| 171 |
m_pWidget1 = new Widget(); |
| 172 |
m_pWidget2 = new Widget(); |
| 173 |
m_pWidget3 = new Widget(); |
| 174 |
|
| 175 |
m_pWidgetLayout->addWidget(m_pWidget1); |
| 176 |
m_pWidgetLayout->addSpacing(m_spacing); |
| 177 |
m_pWidgetLayout->addWidget(m_pWidget2); |
| 178 |
m_pWidgetLayout->addSpacing(m_spacing); |
| 179 |
m_pWidgetLayout->addWidget(m_pWidget3); |
| 180 |
m_pWidgetLayout->setContentsMargins(8, 8, 8, 18); |
| 181 |
|
| 182 |
QHBoxLayout *w1layout = new QHBoxLayout(m_pWidget1); |
| 183 |
w1layout->setContentsMargins(0, 0, 0, 0); |
| 184 |
w1layout->setSpacing(0); |
| 185 |
w1layout->addWidget(m_WidgetList.at(prev)); |
| 186 |
|
| 187 |
QHBoxLayout *w2layout = new QHBoxLayout(m_pWidget2); |
| 188 |
w2layout->setContentsMargins(0, 0, 0, 0); |
| 189 |
w2layout->setSpacing(0); |
| 190 |
w2layout->addWidget(m_WidgetList.at(i)); |
| 191 |
|
| 192 |
QHBoxLayout *w3layout = new QHBoxLayout(m_pWidget3); |
| 193 |
w3layout->setContentsMargins(0, 0, 0, 0); |
| 194 |
w3layout->setSpacing(0); |
| 195 |
w3layout->addWidget(m_WidgetList.at(next)); |
| 196 |
|
| 197 |
updateShowPosition(); |
| 198 |
|
| 199 |
connect(m_pWidget1, &Widget::leftClick, q, [=] { |
| 200 |
q->setCurrentWidget(prev); |
| 201 |
m_pTimer->start(m_timer); |
| 202 |
emit q->leftClick(); |
| 203 |
}); |
| 204 |
connect(m_pWidget2, &Widget::leftClick, q, [=] { |
| 205 |
emit q->centerClick(); |
| 206 |
}); |
| 207 |
connect(m_pWidget3, &Widget::leftClick, q, [=] { |
| 208 |
q->setCurrentWidget(next); |
| 209 |
m_pTimer->start(m_timer); |
| 210 |
emit q->rightClick(); |
| 211 |
}); |
| 212 |
updateButton(); |
| 213 |
} |
| 214 |
|
| 215 |
void KLoopPlayPagePrivate::updateButton() |
| 216 |
{ |
| 217 |
Q_Q(KLoopPlayPage); |
| 218 |
m_ButtonList.clear(); |
| 219 |
QLayoutItem *child; |
| 220 |
while ((child = m_pButtonLayout->takeAt(0)) != 0) { |
| 221 |
// 删除Stretch(弹簧)等布局 |
| 222 |
if (child->spacerItem()) { |
| 223 |
m_pButtonLayout->removeItem(child); |
| 224 |
continue; |
| 225 |
} |
| 226 |
// 删除布局 |
| 227 |
m_pButtonLayout->removeWidget(child->widget()); |
| 228 |
child->widget()->setParent(nullptr); |
| 229 |
delete child; |
| 230 |
child = nullptr; |
| 231 |
} |
| 232 |
m_pButtonLayout->addStretch(); |
| 233 |
m_pButtonLayout->setSpacing(8); |
| 234 |
for (int i = 0; i < m_WidgetList.count(); i++) { |
| 235 |
CircleButton *circleButton = new CircleButton(); |
| 236 |
m_ButtonList.append(circleButton); |
| 237 |
m_pCircleButton = new CircleButton(); |
| 238 |
if (i == q->currentWidget()) { |
| 239 |
m_pCircleButton->setFixedSize(12, 6); |
| 240 |
m_pCircleButton->setButtonType(CircleButton::NormalType); |
| 241 |
} else { |
| 242 |
m_pCircleButton->setFixedSize(6, 6); |
| 243 |
m_pCircleButton->setButtonType(CircleButton::CircleType); |
| 244 |
} |
| 245 |
m_pButtonLayout->addWidget(m_pCircleButton); |
| 246 |
connect(m_pCircleButton, &CircleButton::clicked, this, [=] { |
| 247 |
q->setCurrentWidget(i); |
| 248 |
m_pTimer->start(m_timer); |
| 249 |
}); |
| 250 |
} |
| 251 |
m_pButtonLayout->addStretch(); |
| 252 |
} |
| 253 |
|
| 254 |
void KLoopPlayPagePrivate::updateShowPosition() |
| 255 |
{ |
| 256 |
Q_Q(KLoopPlayPage); |
| 257 |
int viewportWidth = m_pScrollArea->viewport()->width(); |
| 258 |
int scrollValue = (m_pScrollWidget->width() - viewportWidth) / 2; |
| 259 |
if (scrollValue < 0) |
| 260 |
scrollValue = 0; |
| 261 |
m_pScrollArea->horizontalScrollBar()->setValue(scrollValue); |
| 262 |
} |
| 263 |
|
| 264 |
KLoopPlayPage::KLoopPlayPage(QWidget *parent) |
| 265 |
: QWidget(parent) |
| 266 |
, d_ptr(new KLoopPlayPagePrivate(this)) |
| 267 |
{ |
| 268 |
Q_D(KLoopPlayPage); |
| 269 |
} |
| 270 |
|
| 271 |
void KLoopPlayPage::addWidget(QWidget *widget) |
| 272 |
{ |
| 273 |
Q_D(KLoopPlayPage); |
| 274 |
if (!d->m_WidgetList.contains(widget)) |
| 275 |
d->m_WidgetList.append(widget); |
| 276 |
d->updateWidget(d->m_index); |
| 277 |
} |
| 278 |
|
| 279 |
void KLoopPlayPage::removeWidget(QWidget *widget) |
| 280 |
{ |
| 281 |
Q_D(KLoopPlayPage); |
| 282 |
if (d->m_WidgetList.contains(widget)) |
| 283 |
d->m_WidgetList.removeOne(widget); |
| 284 |
d->updateWidget(d->m_index); |
| 285 |
} |
| 286 |
|
| 287 |
void KLoopPlayPage::setCurrentWidget(int index) |
| 288 |
{ |
| 289 |
Q_D(KLoopPlayPage); |
| 290 |
if (index >= d->m_WidgetList.count()) |
| 291 |
d->m_index = 0; |
| 292 |
else if (index < 0) |
| 293 |
d->m_index = d->m_WidgetList.count() - 1; |
| 294 |
else |
| 295 |
d->m_index = index; |
| 296 |
d->updateWidget(d->m_index); |
| 297 |
} |
| 298 |
|
| 299 |
int KLoopPlayPage::currentWidget() |
| 300 |
{ |
| 301 |
Q_D(KLoopPlayPage); |
| 302 |
return d->m_index; |
| 303 |
} |
| 304 |
|
| 305 |
void KLoopPlayPage::setWidgetSpacing(int spacing) |
| 306 |
{ |
| 307 |
Q_D(KLoopPlayPage); |
| 308 |
d->m_spacing = spacing; |
| 309 |
} |
| 310 |
|
| 311 |
void KLoopPlayPage::setLoopTime(int time) |
| 312 |
{ |
| 313 |
Q_D(KLoopPlayPage); |
| 314 |
d->m_timer = time; |
| 315 |
d->m_pTimer->start(time); |
| 316 |
} |
| 317 |
|
| 318 |
void KLoopPlayPage::resizeEvent(QResizeEvent *event) |
| 319 |
{ |
| 320 |
Q_D(KLoopPlayPage); |
| 321 |
QWidget::resizeEvent(event); |
| 322 |
d->updateShowPosition(); |
| 323 |
} |
| 324 |
|
| 325 |
void KLoopPlayPage::paintEvent(QPaintEvent *event) |
| 326 |
{ |
| 327 |
Q_D(KLoopPlayPage); |
| 328 |
QWidget::paintEvent(event); |
| 329 |
} |
| 330 |
|
| 331 |
void KLoopPlayPage::showEvent(QShowEvent *event) |
| 332 |
{ |
| 333 |
Q_D(KLoopPlayPage); |
| 334 |
QWidget::showEvent(event); |
| 335 |
d->updateShowPosition(); |
| 336 |
} |
| 337 |
|
| 338 |
Widget::Widget(QWidget *parent) |
| 339 |
{ |
| 340 |
} |
| 341 |
|
| 342 |
void Widget::mousePressEvent(QMouseEvent *event) |
| 343 |
{ |
| 344 |
if (event->button() == Qt::MouseButton::LeftButton) { |
| 345 |
emit leftClick(); |
| 346 |
} |
| 347 |
return QWidget::mousePressEvent(event); |
| 348 |
} |
| 349 |
|
| 350 |
void Widget::paintEvent(QPaintEvent *event) |
| 351 |
{ |
| 352 |
QPainter painter(this); |
| 353 |
painter.setBrush(Qt::NoBrush); |
| 354 |
painter.setPen(Qt::NoPen); |
| 355 |
painter.drawRect(rect()); |
| 356 |
} |
| 357 |
|
| 358 |
CircleButton::CircleButton(QWidget *parent) |
| 359 |
{ |
| 360 |
m_type = ButtonType::CircleType; |
| 361 |
} |
| 362 |
|
| 363 |
void CircleButton::setButtonType(CircleButton::ButtonType type) |
| 364 |
{ |
| 365 |
m_type = type; |
| 366 |
update(); |
| 367 |
} |
| 368 |
|
| 369 |
void CircleButton::paintEvent(QPaintEvent *event) |
| 370 |
{ |
| 371 |
QPainter painter(this); |
| 372 |
painter.setRenderHint(QPainter::Antialiasing); |
| 373 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 374 |
painter.setPen(Qt::NoPen); |
| 375 |
|
| 376 |
QColor color(18, 18, 18); |
| 377 |
color.setAlphaF(0.2); |
| 378 |
painter.setBrush(color); |
| 379 |
|
| 380 |
switch (m_type) { |
| 381 |
case ButtonType::CircleType: |
| 382 |
painter.drawEllipse(rect()); |
| 383 |
break; |
| 384 |
case ButtonType::NormalType: |
| 385 |
painter.drawRoundedRect(rect(), 3, 3); |
| 386 |
break; |
| 387 |
default: |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
#include "kloopplaypage.moc" |
| 395 |
#include "moc_kloopplaypage.cpp" |