source: src-qt4/pc-mixer/MixerTray.cpp @ 89ae974

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1
Last change on this file since 89ae974 was 89ae974, checked in by Ken Moore <ken@…>, 12 months ago

Make the tray portion of pc-mixer also "L/R channel" aware. It reports whichever value is higher on the tray slider, but keeps the difference between the two channels the same when modified.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1#include "MixerTray.h"
2
3MixerTray::MixerTray() : QSystemTrayIcon(){
4  starting = true;
5  //Initialize the settings backend
6  settings = new QSettings("PCBSD", "pc-mixer");
7  //Initialize all the widgets
8  isMuted = false;
9  slider = new QSlider(Qt::Vertical, 0);
10    slider->setRange(0,100);
11    slider->setTickPosition(QSlider::NoTicks);
12  slideA  = new QWidgetAction(0);
13        slideA->setDefaultWidget(slider);
14  mute = new QToolButton(0);
15  muteA = new QWidgetAction(0);
16        muteA->setDefaultWidget(mute);
17  mixer = new QToolButton(0);
18        mixer->setText(tr("Mixer"));
19        mixer->setIcon(QIcon(":icons/configure.png"));
20        mixer->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
21  mixerA = new QWidgetAction(0);
22        mixerA->setDefaultWidget(mixer);
23  actionMenu = new QMenu(0);
24        actionMenu->addAction(slideA);
25        actionMenu->addAction(muteA);
26        actionMenu->addSeparator();
27        actionMenu->addAction(mixerA);
28  //Now initialize the GUI
29  GUI = new MixerGUI(settings);
30 
31  //Connect the signals/slots
32  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated()) );
33  connect(mixer, SIGNAL(clicked()), this, SLOT(openMixerGUI()) );
34  connect(mute, SIGNAL(clicked()), this, SLOT(muteClicked()) );
35  connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)) );
36  connect(GUI, SIGNAL(updateTray()), this, SLOT(loadVol()) );
37 
38  //Show a quick icon to prevent a warning message
39  this->setIcon(QIcon(":icons/audio-volume-high.png"));
40  CVOL = 101; //quick default to maximize it
41  CDIFF = 0;
42  //Make sure no single-instance events for 30 seconds
43  QTimer::singleShot(30000, this, SLOT(doneStarting()) );
44  QTimer::singleShot(10, this, SLOT(loadVol()) ); //Update the UI
45}
46
47MixerTray::~MixerTray(){
48       
49}
50
51//==============
52//        PRIVATE
53//==============
54void MixerTray::loadVol(){
55  //Generally only used to update the tray to match the backend setting (or if tray device changed)
56  QString val = Mixer::getValues(settings->value("tray-device","vol").toString());
57  if(val.isEmpty()){
58    //invalid device - return to default "vol"
59    settings->setValue("tray-device","vol");
60    val = Mixer::getValues("vol");
61  }
62  int L = val.section(":",0,0).toInt();
63  int R = val.section(":",1,1).toInt();
64  //Record the different between L/R channels (if any)
65  CDIFF = L - R; // - for L lower, + for R lower
66  //Just use the largest value for the moment
67  if(L > R){ R = L; }
68  else if(L < R){ L = R; }
69  //Now just run the changeVol function to update everything (better than duplication)
70  changeVol(R, false);
71}
72
73void MixerTray::changeVol(int percent, bool modify){
74  //Determine the percentage to actually use and adjust internal value
75  if(percent < 0 && isMuted){ percent = CVOL; }   //Return to CVOL value
76  else if(percent < 0){ percent = 0; } //Don't overwrite CVOL if mute clicked
77  else if(percent > 100){ percent = 100; CVOL = 100;}
78  else{ CVOL = percent; }
79  //Now update the backend mixer to the proper value
80  if(modify){
81    if(CDIFF==0){
82      Mixer::setValues(settings->value("tray-device","vol").toString(), percent);
83    }else if(CDIFF < 0){
84      //L channel lower
85      Mixer::setValues(settings->value("tray-device","vol").toString(), percent + CDIFF, percent);
86    }else{
87      //R channel lower
88      Mixer::setValues(settings->value("tray-device","vol").toString(), percent, percent - CDIFF);
89    }
90         
91  }
92  //Now update the display appropriately
93  if(percent == 0){
94    isMuted = true;
95    mute->setIcon( QIcon::fromTheme("audio-volume-high", QIcon(":icons/audio-volume-high.png")) );
96  }else{
97    isMuted = false;
98    if(percent < 33){
99      this->setIcon( QIcon::fromTheme("audio-volume-low", QIcon("icons/audio-volume-low.png")) );
100    }else if(percent < 66){
101      this->setIcon( QIcon::fromTheme("audio-volume-medium", QIcon("icons/audio-volume-medium.png")) );
102    }else{
103      this->setIcon( QIcon::fromTheme("audio-volume-high", QIcon("icons/audio-volume-high.png")) );
104    }
105    mute->setIcon( QIcon::fromTheme("audio-volume-muted", QIcon(":icons/audio-volume-muted.png")) );
106  }
107 
108  slider->disconnect(); //Make sure this slider change does not trigger the signal/slot
109  slider->setValue(percent);
110  connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)) ); //reconnect it
111 
112  this->setToolTip(QString::number(percent)+"%");
113}
114
Note: See TracBrowser for help on using the repository browser.