record-camera-and-screen/RecordTrayIcon.py

98 lines
3.6 KiB
Python
Raw Normal View History

2018-04-17 08:11:26 +00:00
import sys,os
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import RecordType
from RecordType import *
2018-06-24 14:29:59 +00:00
import resource
2018-04-17 08:11:26 +00:00
class RecordTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
super(RecordTrayIcon, self).__init__(parent)
# print((p_menu.actions()))
# self.showAction1 = QAction("显示消息1", self, triggered=self.showM)
# print(type(self.parent))
# p_menu.insertAction(p_menu.actions()[len(p_menu.actions())-1], self.showAction1)
# self.showMenu()
self.createContextMenu(parent)
self.interactive()
# self.update_state(False,None)
def createContextMenu(self, parent):
p_menu = parent.contextMenu
self.setContextMenu(p_menu)
def interactive(self):
self.activated.connect(self.iconClicked)
self.toolTip()
def showMessage():
pass
def get_icon(self, file_name):
data_dir=''
if getattr(sys, 'frozen', False):
# The application is frozen
data_dir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
data_dir = os.path.dirname(__file__)
resource_dir = 'resource'
data_dir = os.path.join(os.path.abspath(data_dir), resource_dir)
print('resource dir: %s' % data_dir)
icon_file_path = os.path.join(data_dir, file_name)
if os.path.isfile(icon_file_path):
return QIcon(icon_file_path)
return None
def update_state(self, recording, record_type):
if recording:
if record_type == RecordType.Camera:
2018-06-24 14:29:59 +00:00
# self.setIcon(self.get_icon('camera_recording_colorful.png'))
self.setIcon(QIcon(':/resource/camera_recording_colorful.png'))
2018-04-17 08:11:26 +00:00
self.setToolTip('正在录制摄像头...')
elif record_type == RecordType.Screen:
2018-06-24 14:29:59 +00:00
# self.setIcon(self.get_icon('screen_recording.png'))
self.setIcon(QIcon(':/resource/screen_recording.png'))
2018-04-17 08:11:26 +00:00
self.setToolTip('正在录制屏幕...')
else:
2018-06-24 14:29:59 +00:00
# self.setIcon(self.get_icon('stop.png'))
# self.setIcon(QIcon(':/resource/stop.png'))
2018-04-17 08:11:26 +00:00
self.setToolTip('软件缩小在这里.')
else:
2018-06-24 14:29:59 +00:00
# self.setIcon(self.get_icon('start.png'))
self.setIcon(QIcon(':/resource/start.png'))
2018-04-17 08:11:26 +00:00
self.setToolTip('软件缩小在这里.')
def iconClicked(self, reason):
#"鼠标点击icon传递的信号会带有一个整形的值1是表示单击右键2是双击3是单击左键4是用鼠标中键点击"
if reason == 2 or reason == 3:
print('clicked.')
2018-04-17 08:11:26 +00:00
pw = self.parent()
# print('trayicon is activateWindow?:%s' % self.isActiveWindow())
if pw.isVisible(): #and pw.isActiveWindow()
# print('pw is active?%s' % pw.isActiveWindow())
if not pw.isActiveWindow():
pw.activateWindow()
# pw.raise_()
else:
pw.hide()
# pw.hide()
pass
2018-04-17 08:11:26 +00:00
else:
pw.show()
# pw.raise_()
pw.activateWindow()
# print('parent is active window? %s' % pw.isActiveWindow())
# print(reason)