"Fossies" - the Fresh Open Source Software Archive

Member "audacious-3.10.1/src/libaudqt/volumebutton.cc" (26 Dec 2018, 4586 Bytes) of package /linux/misc/audacious-3.10.1.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "volumebutton.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.10_vs_3.10.1.

    1 /*
    2  * volumebutton.cc
    3  * Copyright 2014 William Pitcock
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions are met:
    7  *
    8  * 1. Redistributions of source code must retain the above copyright notice,
    9  *    this list of conditions, and the following disclaimer.
   10  *
   11  * 2. Redistributions in binary form must reproduce the above copyright notice,
   12  *    this list of conditions, and the following disclaimer in the documentation
   13  *    provided with the distribution.
   14  *
   15  * This software is provided "as is" and without any warranty, express or
   16  * implied. In no event shall the authors be liable for any damages arising from
   17  * the use of this software.
   18  */
   19 
   20 #include "libaudqt.h"
   21 
   22 #include <QFrame>
   23 #include <QIcon>
   24 #include <QSlider>
   25 #include <QToolButton>
   26 #include <QVBoxLayout>
   27 #include <QWheelEvent>
   28 
   29 #include <libaudcore/drct.h>
   30 #include <libaudcore/hook.h>
   31 
   32 namespace audqt {
   33 
   34 class VolumeButton : public QToolButton
   35 {
   36 public:
   37     VolumeButton (QWidget * parent = nullptr);
   38 
   39 private:
   40     void updateIcon (int val);
   41     void updateVolume ();
   42     void showSlider ();
   43     void setVolume (int val);
   44     QToolButton * newSliderButton (int delta);
   45 
   46     void wheelEvent (QWheelEvent * e);
   47 
   48     QSlider * m_slider;
   49     QFrame * m_container;
   50 };
   51 
   52 VolumeButton::VolumeButton (QWidget * parent) :
   53     QToolButton (parent)
   54 {
   55     setFocusPolicy (Qt::NoFocus);
   56 
   57     m_container = new QFrame (this, Qt::Popup);
   58     m_container->setFrameShape (QFrame::StyledPanel);
   59 
   60     m_slider = new QSlider (Qt::Vertical, this);
   61     m_slider->setMinimumHeight (audqt::sizes.OneInch);
   62     m_slider->setRange (0, 100);
   63     m_slider->setSingleStep (2);
   64     m_slider->setPageStep (20);
   65 
   66     auto layout = make_vbox (m_container, sizes.TwoPt);
   67     layout->setContentsMargins (margins.TwoPt);
   68 
   69     layout->addWidget (newSliderButton (5));
   70     layout->addWidget (m_slider);
   71     layout->addWidget (newSliderButton (-5));
   72 
   73     int val = aud_drct_get_volume_main ();
   74     m_slider->setValue (val);
   75     updateIcon (val);
   76 
   77     connect (this, & QAbstractButton::clicked, this, & VolumeButton::showSlider);
   78     connect (m_slider, & QAbstractSlider::valueChanged, this, & VolumeButton::setVolume);
   79 
   80     auto timer = new Timer<VolumeButton> (TimerRate::Hz4, this, & VolumeButton::updateVolume);
   81     connect (this, & QObject::destroyed, [timer] () { delete timer; });
   82 
   83     timer->start ();
   84 }
   85 
   86 void VolumeButton::updateIcon (int val)
   87 {
   88     if (val == 0)
   89         setIcon (audqt::get_icon ("audio-volume-muted"));
   90     else if (val < 34)
   91         setIcon (audqt::get_icon ("audio-volume-low"));
   92     else if (val < 67)
   93         setIcon (audqt::get_icon ("audio-volume-medium"));
   94     else
   95         setIcon (audqt::get_icon ("audio-volume-high"));
   96 
   97     setToolTip (QString ("%1 %").arg (val));
   98 }
   99 
  100 void VolumeButton::updateVolume ()
  101 {
  102     if (m_slider->isSliderDown ())
  103         return;
  104 
  105     int val = aud_drct_get_volume_main ();
  106     if (val != m_slider->value ())
  107     {
  108         disconnect (m_slider, nullptr, this, nullptr);
  109         m_slider->setValue (val);
  110         updateIcon (val);
  111         connect (m_slider, & QAbstractSlider::valueChanged, this, & VolumeButton::setVolume);
  112     }
  113 }
  114 
  115 void VolumeButton::showSlider ()
  116 {
  117     QSize button_size = sizeHint ();
  118     QSize container_size = m_container->sizeHint ();
  119 
  120     int dx = container_size.width () / 2 - button_size.width () / 2;
  121     int dy = container_size.height () / 2 - button_size.height () / 2;
  122 
  123     QPoint pos = mapToGlobal (QPoint (0, 0));
  124     pos -= QPoint (dx, dy);
  125     pos.setX(qMax(pos.x(), 0));
  126     pos.setY(qMax(pos.y(), 0));
  127 
  128     m_container->move (pos);
  129     window_bring_to_front (m_container);
  130 }
  131 
  132 void VolumeButton::setVolume (int val)
  133 {
  134     aud_drct_set_volume_main (val);
  135     updateIcon (val);
  136 }
  137 
  138 QToolButton * VolumeButton::newSliderButton (int delta)
  139 {
  140     auto button = new QToolButton (this);
  141     button->setText (delta < 0 ? "-" : "+");
  142     button->setAutoRaise (true);
  143     button->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Preferred);
  144 
  145     connect (button, & QAbstractButton::clicked, [this, delta] () {
  146         int val = aud_drct_get_volume_main ();
  147         m_slider->setValue (val + delta);
  148     });
  149 
  150     return button;
  151 }
  152 
  153 void VolumeButton::wheelEvent (QWheelEvent * e)
  154 {
  155     int val = m_slider->value ();
  156     int y = e->angleDelta ().y ();
  157 
  158     if (y < 0)
  159         m_slider->setValue (-- val);
  160     else
  161         m_slider->setValue (++ val);
  162 }
  163 
  164 EXPORT QToolButton * volume_button_new (QWidget * parent)
  165 {
  166     return new VolumeButton (parent);
  167 }
  168 
  169 } // namespace audqt