From 48ac04318ff6ed840d5f25c26bd8cc692d96dc9c Mon Sep 17 00:00:00 2001 From: justforlxz Date: Fri, 26 Oct 2018 15:11:00 +0800 Subject: [PATCH] feat(DMPRIS): music title support tick effect Change-Id: I69f07b52bf96e44b85846e08b86edc4a31757a59 --- src/widgets/dmpriscontrol.cpp | 71 +++++++++++++++++---------- src/widgets/dtickeffect.cpp | 12 +++-- src/widgets/private/dmpriscontrol_p.h | 24 +++++---- 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/widgets/dmpriscontrol.cpp b/src/widgets/dmpriscontrol.cpp index b2c455935..80cbc7db0 100644 --- a/src/widgets/dmpriscontrol.cpp +++ b/src/widgets/dmpriscontrol.cpp @@ -83,9 +83,7 @@ void DMPRISControl::setPictureSize(const QSize &size) } DMPRISControlPrivate::DMPRISControlPrivate(DMPRISControl *q) - : DObjectPrivate(q), - - m_mprisInter(nullptr) + : DObjectPrivate(q), m_mprisInter(nullptr) { } @@ -93,23 +91,39 @@ void DMPRISControlPrivate::init() { D_Q(DMPRISControl); - m_mprisMonitor = new DMPRISMonitor(q); + m_mprisMonitor = new DMPRISMonitor(q); + m_titleScrollArea = new QScrollArea(q); + m_title = new QLabel; + m_picture = new QLabel; + m_pictureVisible = true; + m_controlWidget = new QWidget; + m_prevBtn = new DImageButton; + m_pauseBtn = new DImageButton; + m_playBtn = new DImageButton; + m_nextBtn = new DImageButton; + m_tickEffect = new DTickEffect(m_title, m_title); - m_title = new QLabel; m_title->setAlignment(Qt::AlignCenter); - m_picture = new QLabel; m_picture->setFixedSize(200, 200); - m_pictureVisible = true; - m_controlWidget = new QWidget; - m_prevBtn = new DImageButton; m_prevBtn->setObjectName("PrevBtn"); - m_pauseBtn = new DImageButton; m_pauseBtn->setObjectName("PauseBtn"); - m_playBtn = new DImageButton; m_playBtn->setObjectName("PlayBtn"); - m_nextBtn = new DImageButton; m_nextBtn->setObjectName("NextBtn"); + m_tickEffect->setDirection(DTickEffect::RightToLeft); + m_tickEffect->setDuration(3000); + + m_titleScrollArea->setWidget(m_title); + m_titleScrollArea->setObjectName("scrollarea"); + m_titleScrollArea->setWidgetResizable(true); + m_titleScrollArea->setFocusPolicy(Qt::NoFocus); + m_titleScrollArea->setFrameStyle(QFrame::NoFrame); + m_titleScrollArea->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding); + m_titleScrollArea->setContentsMargins(0, 0, 0, 0); + m_titleScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_titleScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_titleScrollArea->setStyleSheet("background-color:transparent;"); + #ifdef QT_DEBUG m_title->setText("MPRIS Title"); m_nextBtn->setNormalPic("://images/arrow_right_normal.png"); @@ -130,7 +144,7 @@ void DMPRISControlPrivate::init() QVBoxLayout *centralLayout = new QVBoxLayout; - centralLayout->addWidget(m_title); + centralLayout->addWidget(m_titleScrollArea); centralLayout->addWidget(m_picture); centralLayout->setAlignment(m_picture, Qt::AlignCenter); // centralLayout->addLayout(controlLayout); @@ -183,24 +197,29 @@ void DMPRISControlPrivate::_q_onNextClicked() void DMPRISControlPrivate::_q_onMetaDataChanged() { - if (!m_mprisInter) - return; + if (!m_mprisInter) return; - const auto &meta = m_mprisInter->metadata(); - const QString &title = meta.value("xesam:title").toString(); - const QString &artist = meta.value("xesam:artist").toString(); - const QUrl &pictureUrl = meta.value("mpris:artUrl").toString(); - const QSize &pictureSize = m_picture->size(); - const QPixmap &picture = QPixmap(pictureUrl.toLocalFile()).scaled(pictureSize, Qt::IgnoreAspectRatio); + const auto & meta = m_mprisInter->metadata(); + const QString &title = meta.value("xesam:title").toString(); + const QString &artist = meta.value("xesam:artist").toString(); + const QUrl & pictureUrl = meta.value("mpris:artUrl").toString(); + const QSize & pictureSize = m_picture->size(); + const QPixmap &picture = QPixmap(pictureUrl.toLocalFile()) + .scaled(pictureSize, Qt::IgnoreAspectRatio); - if (title.isEmpty()) + if (title.isEmpty()) { m_title->clear(); - else - { - if (artist.isEmpty()) + m_tickEffect->stop(); + } + else { + if (artist.isEmpty()) { m_title->setText(title); - else + } + else { m_title->setText(QString("%1 - %2").arg(title).arg(artist)); + } + m_title->adjustSize(); + m_tickEffect->play(); } m_picture->setPixmap(picture); diff --git a/src/widgets/dtickeffect.cpp b/src/widgets/dtickeffect.cpp index e99b10e2e..b44822a2f 100644 --- a/src/widgets/dtickeffect.cpp +++ b/src/widgets/dtickeffect.cpp @@ -84,20 +84,22 @@ void DTickEffect::draw(QPainter *painter) else pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset, QGraphicsEffect::NoPad); - QPoint p = d->runAnimation->currentValue().toPoint(); + const QPoint p { d->runAnimation->currentValue().toPoint() }; + const QSize size { pixmap.size() / d->content->devicePixelRatioF() }; + switch (d->direction) { case LeftToRight: - offset = QPoint(-pixmap.width() + p.x(), p.y()); + offset = QPoint(-size.width() + p.x(), p.y()); break; case RightToLeft: - offset = QPoint(pixmap.width() + p.x(), p.y()); + offset = QPoint(size.width() + p.x(), p.y()); break; case TopToBottom: - offset = QPoint(p.x(), -pixmap.height() + p.y()); + offset = QPoint(p.x(), -size.height() + p.y()); break; case BottomToTop: - offset = QPoint(p.x(), pixmap.height() + p.y()); + offset = QPoint(p.x(), size.height() + p.y()); break; default: break; diff --git a/src/widgets/private/dmpriscontrol_p.h b/src/widgets/private/dmpriscontrol_p.h index 5bba3d440..12ef92f0e 100644 --- a/src/widgets/private/dmpriscontrol_p.h +++ b/src/widgets/private/dmpriscontrol_p.h @@ -20,15 +20,17 @@ #include -#include "dmpriscontrol.h" #include "dimagebutton.h" -#include "private/mpris/dmprismonitor.h" +#include "dmpriscontrol.h" +#include "dtickeffect.h" #include "private/mpris/dbusmpris.h" +#include "private/mpris/dmprismonitor.h" + +#include DWIDGET_BEGIN_NAMESPACE -class DMPRISControlPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate -{ +class DMPRISControlPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate { D_DECLARE_PUBLIC(DMPRISControl) public: @@ -52,12 +54,14 @@ public Q_SLOTS: public: DMPRISMonitor *m_mprisMonitor; - DBusMPRIS *m_mprisInter; + DBusMPRIS * m_mprisInter; - QLabel *m_title; - QLabel *m_picture; + QScrollArea *m_titleScrollArea; + QLabel * m_title; + QLabel * m_picture; + DTickEffect *m_tickEffect; - QWidget *m_controlWidget; + QWidget * m_controlWidget; DImageButton *m_prevBtn; DImageButton *m_playBtn; DImageButton *m_pauseBtn; @@ -65,10 +69,10 @@ public Q_SLOTS: bool m_pictureVisible; - QString m_lastPath; + QString m_lastPath; QStringList m_mprisPaths; }; DWIDGET_END_NAMESPACE -#endif // DMPRISCONTROL_P_H +#endif // DMPRISCONTROL_P_H