Skip to content

Commit

Permalink
Add trayicon
Browse files Browse the repository at this point in the history
  • Loading branch information
hjc981024 committed Apr 2, 2018
1 parent c1972bb commit 7743242
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 17 deletions.
2 changes: 0 additions & 2 deletions bwh.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@
file.close()
ma = mainwindow(TAR,head,web_payload)
ma.show()
if stat == 3:
sys.exit()
sys.exit(app.exec())

37 changes: 33 additions & 4 deletions bwh_ma.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import PyQt5
import base64
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from bwh_stat import bwh_stat
from bwh_ctr import bwh_controls

class mainwindow(QDialog):
class mainwindow(QWidget):
def __init__(self,TAR,head,web_payload,parent=None):
super().__init__()
self.tray = QSystemTrayIcon(self)
#self.timer = QTimer(self)
#self.timer.timeout.connect(self.warn)
#self.timer.start(20*1000)
self.quitAction = QAction("&Quit",self,triggered=qApp.quit)
self.showAction = QAction("&Show",self,triggered=self.showNormal)
self.Tray_icon_menu = QMenu(self)
self.Tray_icon_menu.addAction(self.showAction)
self.Tray_icon_menu.addAction(self.quitAction)
self.tray.setContextMenu(self.Tray_icon_menu)
self.tray.setIcon(QIcon('final.ico' ))
self.tray.activated.connect(self.db_cilcked)
self.tray.messageClicked.connect(self.showNormal)
self.tray.show()
self.bwh_stat = bwh_stat(TAR,head,web_payload)
self.bwh_control =bwh_controls(TAR,head,web_payload)
self.tray.setToolTip("IP : %s\nRAM : %s%%\nSwap : %s%% \nBandwidth : %s%% "%(self.bwh_stat.info_data['ip_addresses'][0],str(self.bwh_stat.ram_stat_value),\
str(self.bwh_stat.swap_stat_value),str(self.bwh_stat.data_usage_value)))
self.setWindowTitle(self.tr("bwh_contorler"))
self.setWindowFlags(Qt.MSWindowsFixedSizeDialogHint|Qt.Window)
self.tabwidget = QTabWidget(self)
self.tabwidget.addTab(self.bwh_stat,"bwh_stat")
self.tabwidget.addTab(self.bwh_control,"bwh_control")
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.tabwidget)
self.setLayout(self.vbox)
self.resize(800,480)
self.resize(640,480)
self.setFixedSize(640,480)


def hideEvent(self, event):
self.hide()
def closeEvent(self, QCloseEvent):
global stat
stat =3
self.tray.hide()
return super().closeEvent(QCloseEvent)
def db_cilcked(self,reason):
if reason == 2:
self.showNormal()
def warn(self):
if int((float(self.bwh_stat.live_data['plan_ram']/1024-self.bwh_stat.live_data['mem_available_kb'])/float(self.bwh_stat.live_data['plan_ram']/1024))*100) >= 50:
self.tray.showMessage("",'Ram too high')

94 changes: 83 additions & 11 deletions bwh_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class res_gui(QThread):
up = pyqtSignal()
def __int__(self):
super(WorkThread, self).__init__()
def run(self):
self.up.emit()

class bwh_stat(QWidget):
def __init__(self,TAR,head,payload):
Expand All @@ -15,12 +21,17 @@ def __init__(self,TAR,head,payload):
self.head = head
self.web_payload = payload
self.timer = QTimer(self)
self.auto_res = QTimer(self)
self.res_thread = res_gui()
self.initUI()
def initUI(self):
self.timer.timeout.connect(self.res_label_event)
self.auto_res.timeout.connect(self.start_update)
self.auto_res.start(60*1000)
self.res_thread.up.connect(self.update_data)
self.setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
self.res_button=QPushButton("Refresh")
self.res_label=QLabel("Refresh success.")
self.res_label=QLabel("Success.")
self.res_label.setVisible(False)
self.get_info = 'getServiceInfo'
self.get_live_info = 'getLiveServiceInfo'
Expand All @@ -37,12 +48,12 @@ def initUI(self):
self.res_layout = QHBoxLayout()
self.res_layout.addWidget(self.res_label,Qt.AlignRight)
self.res_layout.addWidget(self.res_button,Qt.AlignRight)
self.res_layout.setContentsMargins(600,0,0,0)
self.res_layout.setContentsMargins(450,0,0,0)
self.status = self.live_data['ve_status']
self.ft = QFont()
self.ft.setPointSize(15)
self.label_dict={}
self.res_button.clicked.connect(self.update_data)
self.res_button.clicked.connect(self.start_update)

for (x,y) in zip(self.info_res, num):
tmplayout = QFormLayout()
Expand All @@ -53,9 +64,16 @@ def initUI(self):
if x == 'data_usage':
label2 = QProgressBar(self)
if self.status != 'Stopped':
label2.setValue(int(self.info_data['data_counter']/self.info_data['plan_monthly_data']*100))
self.data_usage_value = int(self.info_data['data_counter']/self.info_data['plan_monthly_data']*100)
label2.setValue(self.data_usage_value)
label2.setFormat('%sG / %sG'%(str(round(self.info_data['data_counter']/1024/1024/1024,2)),str(round(self.info_data['plan_monthly_data']/1024/1024/1024,2))))
label2.setAlignment(Qt.AlignRight)
if self.data_usage_value <= 70:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.data_usage_value > 70 and self.data_usage_value <= 90:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
label2.setValue(0)
label2.setFormat('%s'%('VPS Stopped'))
Expand All @@ -81,27 +99,48 @@ def initUI(self):
if x == 'disk_usage':
label2 = QProgressBar(self)
if self.status != 'Stopped':
label2.setValue(int(self.live_data['ve_used_disk_space_b']/self.info_data['plan_disk']*100))
self.disk_usage_value = int(self.live_data['ve_used_disk_space_b']/self.info_data['plan_disk']*100)
label2.setValue(self.disk_usage_value)
label2.setFormat('%sG / %sG'%(str(round(self.live_data['ve_used_disk_space_b']/1024/1024/1024,2)),str(round(self.live_data['plan_disk']/1024/1024/1024,2))))
label2.setAlignment(Qt.AlignRight)
if self.disk_usage_value <= 70:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.disk_usage_value > 70 and self.disk_usage_value <= 90:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
label2.setValue(0)
label2.setFormat('%s'%('VPS Stopped'))
elif x == 'ram_stat':
label2 = QProgressBar(self)
if self.status != 'Stopped':
label2.setValue(int((float(self.live_data['plan_ram']/1024-self.live_data['mem_available_kb'])/float(self.live_data['plan_ram']/1024))*100))
self.ram_stat_value = int((float(self.live_data['plan_ram']/1024-self.live_data['mem_available_kb'])/float(self.live_data['plan_ram']/1024))*100)
label2.setValue(self.ram_stat_value)
label2.setFormat('%sM / %sM'%(str(round((self.live_data['plan_ram']/1024/1024-self.live_data['mem_available_kb']/1024),2)),str(round(self.live_data['plan_ram']/1024/1024,2))))
label2.setAlignment(Qt.AlignRight)
if self.ram_stat_value <= 70:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.ram_stat_value > 70 and self.ram_stat_value <= 90:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
label2.setValue(0)
label2.setFormat('%s'%('VPS Stopped'))
elif x == 'swap_stat':
label2 = QProgressBar(self)
if self.status != 'Stopped':
label2.setValue(int( ((self.live_data['swap_total_kb'] - self.live_data['swap_available_kb']) /self.live_data['swap_total_kb'])*100 ))
self.swap_stat_value = int( ((self.live_data['swap_total_kb'] - self.live_data['swap_available_kb']) /self.live_data['swap_total_kb'])*100 )
label2.setValue(self.swap_stat_value)
label2.setFormat('%sM / %sM'%(str(round(self.live_data['swap_total_kb']/1024 - self.live_data['swap_available_kb']/1024,2)),str(round(self.live_data['swap_total_kb']/1024,2))))
label2.setAlignment(Qt.AlignRight)
if self.swap_stat_value <= 70:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.swap_stat_value > 70 and self.swap_stat_value <= 90:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
label2.setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
label2.setValue(0)
label2.setFormat('%s'%('VPS Stopped'))
Expand All @@ -120,6 +159,8 @@ def initUI(self):
self.setLayout(self.MainLayout)
self.show()

def start_update(self):
self.res_thread.start()
def update_data(self):
self.res_label.setVisible(True)
self.timer.start(10*1000)
Expand All @@ -130,9 +171,16 @@ def update_data(self):
for (x,y) in zip(self.info_res, num):
if x == 'data_usage':
if self.status != 'Stopped':
self.label_dict[x].setValue(int(self.info_data['data_counter']/self.info_data['plan_monthly_data']*100))
self.data_usage_value = int(self.info_data['data_counter']/self.info_data['plan_monthly_data']*100)
self.label_dict[x].setValue(self.data_usage_value)
self.label_dict[x].setFormat('%sG / %sG'%(str(round(self.info_data['data_counter']/1024/1024/1024,2)),str(round(self.info_data['plan_monthly_data']/1024/1024/1024,2))))
self.label_dict[x].setAlignment(Qt.AlignRight)
if self.data_usage_value <= 70:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.data_usage_value > 70 and self.data_usage_value <= 90:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setValue(0)
self.label_dict[x].setFormat('%s'%('VPS Stopped'))
Expand All @@ -144,17 +192,31 @@ def update_data(self):
for (x,y) in zip(self.live_res, num):
if x == 'disk_usage':
if self.status != 'Stopped':
self.label_dict[x] .setValue(int(self.live_data['ve_used_disk_space_b']/self.info_data['plan_disk']*100))
self.disk_usage_value = int(self.live_data['ve_used_disk_space_b']/self.info_data['plan_disk']*100)
self.label_dict[x] .setValue(self.disk_usage_value)
self.label_dict[x] .setFormat('%sG / %sG'%(str(round(self.live_data['ve_used_disk_space_b']/1024/1024/1024,2)),str(round(self.live_data['plan_disk']/1024/1024/1024,2))))
self.label_dict[x] .setAlignment(Qt.AlignRight)
if self.disk_usage_value <= 70:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.disk_usage_value > 70 and self.disk_usage_value <= 90:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setValue(0)
self.label_dict[x].setFormat('%s'%('VPS Stopped'))
elif x == 'ram_stat':
if self.status != 'Stopped':
self.label_dict[x] .setValue(int((float(self.live_data['plan_ram']/1024-self.live_data['mem_available_kb'])/float(self.live_data['plan_ram']/1024))*100))
self.ram_stat_value = int((float(self.live_data['plan_ram']/1024-self.live_data['mem_available_kb'])/float(self.live_data['plan_ram']/1024))*100)
self.label_dict[x] .setValue(self.ram_stat_value)
self.label_dict[x] .setFormat('%sM / %sM'%(str(round((self.live_data['plan_ram']/1024/1024-self.live_data['mem_available_kb']/1024),2)),str(round(self.live_data['plan_ram']/1024/1024,2))))
self.label_dict[x] .setAlignment(Qt.AlignRight)
if self.ram_stat_value <= 70:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.ram_stat_value > 70 and self.ram_stat_value <= 90:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setValue(0)
self.label_dict[x].setFormat('%s'%('VPS Stopped'))
Expand All @@ -164,8 +226,15 @@ def update_data(self):
self.label_dict[x].setValue(0)
self.label_dict[x].setFormat('%s'%('SWAP Unloaded'))
else:
self.label_dict[x] .setValue(int( ((self.live_data['swap_total_kb'] - self.live_data['swap_available_kb']) /self.live_data['swap_total_kb'])*100 ))
self.swap_stat_value = int( ((self.live_data['swap_total_kb'] - self.live_data['swap_available_kb']) /self.live_data['swap_total_kb'])*100 )
self.label_dict[x] .setValue(self.swap_stat_value)
self.label_dict[x] .setFormat('%sM / %sM'%(str(round(self.live_data['swap_total_kb']/1024 - self.live_data['swap_available_kb']/1024,2)),str(round(self.live_data['swap_total_kb']/1024,2))))
if self.swap_stat_value <= 70:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(153, 204, 255)}QProgressBar{text-align:center;}')
elif self.swap_stat_value > 70 and self.swap_stat_value <= 90:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,255,0)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setStyleSheet('QProgressBar::chunk{background:rgb(255,0,51)}QProgressBar{text-align:center;}')
else:
self.label_dict[x].setValue(0)
self.label_dict[x].setFormat('%s'%('VPS Stopped'))
Expand All @@ -174,3 +243,6 @@ def update_data(self):
self.update()
def res_label_event(self):
self.res_label.setVisible(False)



Binary file added final.ico
Binary file not shown.

0 comments on commit 7743242

Please sign in to comment.