-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtray_icon.py
89 lines (72 loc) · 3 KB
/
tray_icon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
import utils
from volume_thread import VolumeThread
import logging
from pycaw.pycaw import AudioUtilities
import re
import webbrowser
logger = logging.getLogger("root")
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
self.icon = icon
self.setToolTip("Windows Volume Slider Manager")
# Setup the error window
self.err_box = None
# Setup the context menu when you right click the tray icon.
menu = QtWidgets.QMenu(parent)
reload_ = menu.addAction("Reload mapping")
reload_.triggered.connect(self.reload)
showdevices = menu.addAction("Show sound devices")
showdevices.triggered.connect(self.show_devices)
open_config = menu.addAction("Open configuration file")
open_config.triggered.connect(self.open_config_file)
exit_ = menu.addAction("Exit")
exit_.triggered.connect(self.exit)
self.setContextMenu(menu)
self.activated.connect(self.onClick)
self.thread = VolumeThread()
def std_err_post(self, msg):
"""
This method receives stderr text strings as a pyqtSlot.
"""
if self.err_box is None:
self.err_box = QMessageBox()
# Both OK and window delete fire the 'finished' signal
self.err_box.finished.connect(self.clear_err_box)
# A single error is sent as a string of separate stderr .write() messages,
# so concatenate them.
self.err_box.setWindowTitle("Runtime Error")
self.err_box.setIcon(QMessageBox.Critical)
self.err_box.setText(self.err_box.text() + msg)
# .show() is used here because .exec() or .exec_() create multiple
# MessageBoxes.
self.err_box.show()
def clear_err_box(self):
self.err_box.setText("")
def onClick(self, reason):
if reason == self.Trigger: # LMB
self.reload()
def reload(self):
self.showMessage("Volume Slider Manager", "Reloading slider mappings...", self.icon)
self.thread.control.get_mapping()
def exit(self):
logger.info("Quitting application.")
sys.exit(0)
def show_devices(self):
sound_devices = utils.get_devices()
text = "Note that these are both input and output devices!\n\n" + "\n".join(sorted(set(sound_devices)))
QMessageBox.information(None, "Sound devices", text)
@staticmethod
def open_config_file(self):
webbrowser.open(utils.get_appdata_path() / "mapping.txt")
def start_app(self):
"""
Create a global volume control object "vc" and a systray object to control the system tray in other places.
Start the event loop to parse incoming data from the volume sliders. Stops the vc thread and systray thread once
:return:
"""
logger.info("Starting application.")
self.thread.start()