-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingtrend.py
146 lines (128 loc) · 4.51 KB
/
pingtrend.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
##\package pingtrend
# \brief Simple tool to trend ping times
#
# Vegard Fiksdal (C) 2024
#
# Import QT modules
from PyQt6.QtWidgets import QApplication, QMainWindow, QSplitter, QTreeView, QFrame, QVBoxLayout, QScrollArea
from PyQt6.QtGui import QStandardItemModel, QStandardItem, QFont
from PyQt6.QtCore import Qt, QTimer
import sys
# Import local modules
from qpinger import *
from qplot import *
from qlog import *
# Simple identification
appversion='0.7.1'
appname='PingTrend'
pkgstring=appname+' '+appversion
aboutstring=pkgstring+'\n'
aboutstring+='Simple tool to trend and log ping times\n'
aboutstring+='[email protected]\n'
##\class StandardItem
# \brief Define how a standard treeview item should look like
class StandardItem(QStandardItem):
##\brief Configures StandardItem
# \param txt Text to display
# \param font_size Size of font
# \param set_bold Wether to have bold font
# \param color Color of the text
def __init__(self, txt='', font_size=12, set_bold=False, color=None):
super().__init__()
if color!=None: self.setForeground(color)
fnt = QFont('Open Sans', font_size)
fnt.setBold(set_bold)
self.setEditable(False)
self.setFont(fnt)
self.setText(txt)
##\class Frame
# \brief Holds a selectable frame
class Frame():
##\brief Configures StandardItem
# \param object Frame object
# \param label Frame label
def __init__(self,object,label):
self.object=object
self.label=label
self.item=StandardItem(label)
object.setVisible(False)
##\class MainWindow
# \brief Main Application class
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
# Create panels
self.pinger=QPinger()
self.trend=QStyledPlot('Ping Trend','Measurement time','Response time (ms)')
self.log=QLog()
self.frames=[]
self.frames.append(Frame(self.pinger,'Config'))
self.frames.append(Frame(self.trend,'Trend'))
self.frames.append(Frame(self.log,'Log'))
# Present application info
for string in aboutstring.split('\n'):
self.log.addText(string)
self.log.addText('')
# Build treeview
self.treeview=QTreeView()
self.treeview.setHeaderHidden(True)
treemodel=QStandardItemModel()
rootnode=treemodel.invisibleRootItem()
for frame in self.frames:
rootnode.appendRow(frame.item)
# Wrap up treeview
self.treeview.setModel(treemodel)
self.treeview.expandAll()
self.treeview.clicked.connect(self.TreeviewClick)
index=treemodel.indexFromItem(self.frames[0].item)
self.treeview.setCurrentIndex(index)
self.frames[0].object.setVisible(True)
# Use a timer to process data from the queue
self.timer=QTimer()
self.timer.timeout.connect(self.Process)
self.timer.start(100)
# Show window
layout=QVBoxLayout()
widget=QFrame()
scrollarea=QScrollArea()
for frame in self.frames:
layout.addWidget(frame.object)
widget.setLayout(layout)
scrollarea.setWidgetResizable(True)
scrollarea.setWidget(widget)
splitter=QSplitter(Qt.Orientation.Horizontal)
splitter.addWidget(self.treeview)
splitter.addWidget(scrollarea)
splitter.setSizes([100,600])
self.setCentralWidget(splitter)
self.setWindowTitle(pkgstring)
self.resize(800,600)
self.showMaximized()
##\brief Stop background processes upon terminating the application
# \param event Not used
def closeEvent(self, event):
#self.timer.stop()
super().close()
##\brief Respond to user clicking on the treeview
# \param Value The clicked item
def TreeviewClick(self,Value):
# Show/hide dynamic panels
title=Value.data()
for frame in self.frames:
frame.object.setVisible(title==frame.label)
##\brief Timer event to update plots
def Process(self):
# Update pinger
xdata,ydata,legend=self.pinger.updatePing()
# Update plot
if self.trend.mplstyle!=self.pinger.ctrl_style.combo.currentText():
self.trend.setStyle(self.pinger.ctrl_style.combo.currentText())
if xdata!=None:
self.trend.plotXY(xdata,ydata,legend)
# Update logger
self.log.updateLog()
# Start application
if __name__ == "__main__":
app=QApplication(sys.argv)
window=MainWindow()
app.exec()