From 05b2fc9c8bd2da30b52f8ccc1395239be0a22ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=8D=9A=E6=96=87?= Date: Tue, 2 Aug 2016 20:14:45 +0800 Subject: [PATCH] sound plugin add slider Change-Id: I41bc596be98d8f0a668e11bdf245d93af29fbb04 --- plugins/datetime/datetimeplugin.cpp | 2 +- plugins/datetime/datetimeplugin.h | 12 ++--- .../{ => componments}/horizontalseparator.cpp | 0 .../{ => componments}/horizontalseparator.h | 0 plugins/sound/componments/volumeslider.cpp | 50 +++++++++++++++++++ plugins/sound/componments/volumeslider.h | 24 +++++++++ plugins/sound/sound.pro | 6 ++- plugins/sound/soundapplet.cpp | 16 +++--- plugins/sound/soundapplet.h | 3 +- plugins/sound/sounditem.cpp | 5 +- plugins/trash/popupcontrolwidget.cpp | 2 +- 11 files changed, 97 insertions(+), 23 deletions(-) rename plugins/sound/{ => componments}/horizontalseparator.cpp (100%) rename plugins/sound/{ => componments}/horizontalseparator.h (100%) create mode 100644 plugins/sound/componments/volumeslider.cpp create mode 100644 plugins/sound/componments/volumeslider.h diff --git a/plugins/datetime/datetimeplugin.cpp b/plugins/datetime/datetimeplugin.cpp index 251843a0d..d57bbdd79 100644 --- a/plugins/datetime/datetimeplugin.cpp +++ b/plugins/datetime/datetimeplugin.cpp @@ -38,7 +38,7 @@ void DatetimePlugin::init(PluginProxyInterface *proxyInter) m_proxyInter->itemAdded(this, QString()); } -int DatetimePlugin::itemSortKey(const QString &itemKey) const +int DatetimePlugin::itemSortKey(const QString &itemKey) { Q_UNUSED(itemKey); diff --git a/plugins/datetime/datetimeplugin.h b/plugins/datetime/datetimeplugin.h index 096b79143..977ab34eb 100644 --- a/plugins/datetime/datetimeplugin.h +++ b/plugins/datetime/datetimeplugin.h @@ -17,15 +17,15 @@ public: explicit DatetimePlugin(QObject *parent = 0); ~DatetimePlugin(); - const QString pluginName() const; - void init(PluginProxyInterface *proxyInter); + const QString pluginName() const override; + void init(PluginProxyInterface *proxyInter) override; - int itemSortKey(const QString &itemKey) const; + int itemSortKey(const QString &itemKey) override; - QWidget *itemWidget(const QString &itemKey); - QWidget *itemTipsWidget(const QString &itemKey); + QWidget *itemWidget(const QString &itemKey) override; + QWidget *itemTipsWidget(const QString &itemKey) override; - const QString itemCommand(const QString &itemKey); + const QString itemCommand(const QString &itemKey) override; private slots: void updateCurrentTimeString(); diff --git a/plugins/sound/horizontalseparator.cpp b/plugins/sound/componments/horizontalseparator.cpp similarity index 100% rename from plugins/sound/horizontalseparator.cpp rename to plugins/sound/componments/horizontalseparator.cpp diff --git a/plugins/sound/horizontalseparator.h b/plugins/sound/componments/horizontalseparator.h similarity index 100% rename from plugins/sound/horizontalseparator.h rename to plugins/sound/componments/horizontalseparator.h diff --git a/plugins/sound/componments/volumeslider.cpp b/plugins/sound/componments/volumeslider.cpp new file mode 100644 index 000000000..903768ded --- /dev/null +++ b/plugins/sound/componments/volumeslider.cpp @@ -0,0 +1,50 @@ +#include "volumeslider.h" + +#include +#include +#include + +VolumeSlider::VolumeSlider(QWidget *parent) + : QSlider(Qt::Horizontal, parent), + m_pressed(false) +{ + setTickInterval(50); + setPageStep(50); + setTickPosition(QSlider::NoTicks); +} + +void VolumeSlider::setValue(const int value) +{ + if (m_pressed) + return; + + blockSignals(true); + QSlider::setValue(value); + blockSignals(false); +} + +void VolumeSlider::mousePressEvent(QMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) + { + if (!rect().contains(e->pos())) + return; + m_pressed = true; + QSlider::setValue(minimum() + (double((maximum()) - minimum()) * e->x() / rect().width())); + } +} + +void VolumeSlider::mouseMoveEvent(QMouseEvent *e) +{ + const int value = minimum() + (double((maximum()) - minimum()) * e->x() / rect().width()); + + QSlider::setValue(std::max(std::min(1000, value), 0)); + emit valueChanged(std::max(std::min(1000, value), 0)); +} + +void VolumeSlider::mouseReleaseEvent(QMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) + m_pressed = false; +// QTimer::singleShot(100, [this] {m_pressed = false;}); +} diff --git a/plugins/sound/componments/volumeslider.h b/plugins/sound/componments/volumeslider.h new file mode 100644 index 000000000..783056073 --- /dev/null +++ b/plugins/sound/componments/volumeslider.h @@ -0,0 +1,24 @@ +#ifndef VOLUMESLIDER_H +#define VOLUMESLIDER_H + +#include + +class VolumeSlider : public QSlider +{ + Q_OBJECT + +public: + explicit VolumeSlider(QWidget *parent = 0); + + void setValue(const int value); + +protected: + void mousePressEvent(QMouseEvent *e); + void mouseMoveEvent(QMouseEvent *e); + void mouseReleaseEvent(QMouseEvent *e); + +private: + bool m_pressed; +}; + +#endif // VOLUMESLIDER_H diff --git a/plugins/sound/sound.pro b/plugins/sound/sound.pro index d66092c5f..43b06a85b 100644 --- a/plugins/sound/sound.pro +++ b/plugins/sound/sound.pro @@ -16,7 +16,8 @@ HEADERS += \ soundapplet.h \ dbus/dbusaudio.h \ dbus/dbussink.h \ - horizontalseparator.h + componments/horizontalseparator.h \ + componments/volumeslider.h SOURCES += \ soundplugin.cpp \ @@ -24,7 +25,8 @@ SOURCES += \ soundapplet.cpp \ dbus/dbusaudio.cpp \ dbus/dbussink.cpp \ - horizontalseparator.cpp + componments/horizontalseparator.cpp \ + componments/volumeslider.cpp target.path = $${PREFIX}/lib/dde-dock/plugins/ INSTALLS += target diff --git a/plugins/sound/soundapplet.cpp b/plugins/sound/soundapplet.cpp index f907b74f1..164e94383 100644 --- a/plugins/sound/soundapplet.cpp +++ b/plugins/sound/soundapplet.cpp @@ -1,5 +1,5 @@ #include "soundapplet.h" -#include "horizontalseparator.h" +#include "componments/horizontalseparator.h" #include #include @@ -13,7 +13,7 @@ SoundApplet::SoundApplet(QWidget *parent) m_centeralWidget(new QWidget), m_appControlWidget(new QWidget), m_volumeIcon(new QLabel), - m_volumeSlider(new QSlider(Qt::Horizontal)), + m_volumeSlider(new VolumeSlider), m_audioInter(new DBusAudio(this)), m_defSinkInter(nullptr) @@ -53,7 +53,7 @@ SoundApplet::SoundApplet(QWidget *parent) m_volumeIcon->setFixedSize(ICON_SIZE, ICON_SIZE); m_volumeSlider->setMinimum(0); - m_volumeSlider->setMaximum(100); + m_volumeSlider->setMaximum(1000); m_appControlWidget->setLayout(appLayout); @@ -72,7 +72,7 @@ SoundApplet::SoundApplet(QWidget *parent) setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setStyleSheet("background-color:transparent;"); - connect(m_volumeSlider, &QSlider::valueChanged, this, &SoundApplet::volumeSliderValueChanged); + connect(m_volumeSlider, &VolumeSlider::valueChanged, this, &SoundApplet::volumeSliderValueChanged); connect(this, static_cast(&SoundApplet::defaultSinkChanged), this, &SoundApplet::onVolumeChanged); QMetaObject::invokeMethod(this, "defaultSinkChanged", Qt::QueuedConnection); @@ -93,12 +93,10 @@ void SoundApplet::defaultSinkChanged() void SoundApplet::onVolumeChanged() { - const bool mute = m_defSinkInter->mute(); const double volmue = m_defSinkInter->volume(); + const bool mute = m_defSinkInter->mute() || volmue < 0.001; - m_volumeSlider->blockSignals(true); - m_volumeSlider->setValue(std::min(100.0, volmue * 100)); - m_volumeSlider->blockSignals(false); + m_volumeSlider->setValue(std::min(1000.0, volmue * 1000)); QString volumeString; if (mute) @@ -116,5 +114,5 @@ void SoundApplet::onVolumeChanged() void SoundApplet::volumeSliderValueChanged() { - m_defSinkInter->SetVolume(double(m_volumeSlider->value()) / 100 + 0.5, false); + m_defSinkInter->SetVolume(double(m_volumeSlider->value()) / 1000, false); } diff --git a/plugins/sound/soundapplet.h b/plugins/sound/soundapplet.h index cefe39f78..2983ba37d 100644 --- a/plugins/sound/soundapplet.h +++ b/plugins/sound/soundapplet.h @@ -1,6 +1,7 @@ #ifndef SOUNDAPPLET_H #define SOUNDAPPLET_H +#include "componments/volumeslider.h" #include "dbus/dbusaudio.h" #include "dbus/dbussink.h" @@ -28,7 +29,7 @@ private: QWidget *m_centeralWidget; QWidget *m_appControlWidget; QLabel *m_volumeIcon; - QSlider *m_volumeSlider; + VolumeSlider *m_volumeSlider; QVBoxLayout *m_centeralLayout; DBusAudio *m_audioInter; diff --git a/plugins/sound/sounditem.cpp b/plugins/sound/sounditem.cpp index a8d09dfa4..6ca35bafc 100644 --- a/plugins/sound/sounditem.cpp +++ b/plugins/sound/sounditem.cpp @@ -48,16 +48,15 @@ void SoundItem::refershIcon() return; const double volmue = m_sinkInter->volume(); - const bool mute = m_sinkInter->mute(); + const bool mute = m_sinkInter->mute() || volmue < 0.001; const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); - QString iconString; if (displayMode == Dock::Fashion) { QString volumeString; if (volmue >= 1.0) - volumeString = "010"; + volumeString = "100"; else volumeString = QString("0") + ('0' + int(volmue * 10)) + "0"; diff --git a/plugins/trash/popupcontrolwidget.cpp b/plugins/trash/popupcontrolwidget.cpp index f9d403224..7e756489c 100644 --- a/plugins/trash/popupcontrolwidget.cpp +++ b/plugins/trash/popupcontrolwidget.cpp @@ -56,7 +56,7 @@ void PopupControlWidget::clearTrashFloder() if (item.isFile()) QFile(item.fileName()).remove(); else if (item.isDir()) - QDir(item.path()).removeRecursively(); + QDir(item.absoluteFilePath()).removeRecursively(); } }