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

releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since caf88c8 was caf88c8, checked in by Ken Moore <ken@…>, 7 months ago

Add a new utility: pc-mixer
This utility is a tray application that is a front-end to the "mixer" CLI tool, granting control over volumes and other audio settings.
The tray portion of it is complete, still need to finish filling out the full mixer GUI tomorrow.

  • Property mode set to 100644
File size: 3.6 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  mixerA = new QWidgetAction(0);
20        mixerA->setDefaultWidget(mixer);
21  actionMenu = new QMenu(0);
22        actionMenu->addAction(slideA);
23        actionMenu->addAction(muteA);
24        actionMenu->addSeparator();
25        actionMenu->addAction(mixerA);
26  //Now initialize the GUI
27  GUI = new MixerGUI(settings);
28 
29  //Connect the signals/slots
30  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated()) );
31  connect(mixer, SIGNAL(clicked()), this, SLOT(openMixerGUI()) );
32  connect(mute, SIGNAL(clicked()), this, SLOT(muteClicked()) );
33  connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)) );
34  connect(GUI, SIGNAL(updateTray()), this, SLOT(loadVol()) );
35 
36  //Show a quick icon to prevent a warning message
37  this->setIcon(QIcon(":icons/audio-volume-high.png"));
38  CVOL = 101; //quick default to maximize it
39 
40  //Make sure no single-instance events for 30 seconds
41  QTimer::singleShot(30000, this, SLOT(doneStarting()) );
42  QTimer::singleShot(10, this, SLOT(loadVol()) ); //Update the UI
43}
44
45MixerTray::~MixerTray(){
46       
47}
48
49//==============
50//        PRIVATE
51//==============
52void MixerTray::loadVol(){
53  //Generally only used to update the tray to match the backend setting (or if tray device changed)
54  QString val = Mixer::getValues(settings->value("tray-device","vol").toString());
55  if(val.isEmpty()){
56    //invalid device - return to default "vol"
57    settings->setValue("tray-device","vol");
58    val = Mixer::getValues("vol");
59  }
60  int L = val.section(":",0,0).toInt();
61  int R = val.section(":",1,1).toInt();
62  //Just use the largest value for the moment
63  if(L > R){ R = L; }
64  else if(L < R){ L = R; }
65  //Now just run the changeVol function to update everything (better than duplication)
66  changeVol(R);
67}
68
69void MixerTray::changeVol(int percent){
70  //Determine the percentage to actually use and adjust internal value
71  if(percent < 0 && isMuted){ percent = CVOL; }   //Return to CVOL value
72  else if(percent < 0){ percent = 0; } //Don't overwrite CVOL if mute clicked
73  else if(percent > 100){ percent = 100; CVOL = 100;}
74  else{ CVOL = percent; }
75  //Now update the backend mixer to the proper value
76  Mixer::setValues(settings->value("tray-device","vol").toString(), percent);
77  //Now update the display appropriately
78  if(percent == 0){
79    isMuted = true;
80    mute->setIcon( QIcon::fromTheme("audio-volume-high", QIcon(":icons/audio-volume-high.png")) );
81  }else{
82    isMuted = false;
83    if(percent < 33){
84      this->setIcon( QIcon::fromTheme("audio-volume-low", QIcon("icons/audio-volume-low.png")) );
85    }else if(percent < 66){
86      this->setIcon( QIcon::fromTheme("audio-volume-medium", QIcon("icons/audio-volume-medium.png")) );
87    }else{
88      this->setIcon( QIcon::fromTheme("audio-volume-high", QIcon("icons/audio-volume-high.png")) );
89    }
90    mute->setIcon( QIcon::fromTheme("audio-volume-muted", QIcon(":icons/audio-volume-muted.png")) );
91  }
92 
93  slider->disconnect(); //Make sure this slider change does not trigger the signal/slot
94  slider->setValue(percent);
95  connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)) ); //reconnect it
96 
97  this->setToolTip(QString::number(percent)+"%");
98}
99
Note: See TracBrowser for help on using the repository browser.