You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PROFIBUS/UI/MainWindow.py

359 lines
12 KiB
Python

import sys
import json
import collections
import time
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QMdiArea, QAction, QInputDialog, QDialog, QFormLayout, QLineEdit, \
QMdiSubWindow, QDialogButtonBox, QWidget, QComboBox, QTabBar, QTabWidget, QGridLayout, QLabel, QPushButton, QSpacerItem, QSizePolicy, QHBoxLayout, QTableWidget, QScrollArea
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap, QIcon
from UI.DeviceWidget import DeviceDialog
from UI.AreaSettingWidget import AreaSettingWidget
from UI.DelAreaWidget import DelAreaWidget
from model.ProjectModel.DeviceManage import DevicesManange, Device
from protocol.ModBus.ModBusThread import MyThread
from utils.DBModels.BaseModel import *
from utils.DBModels.DeviceModels import DeviceDB
from model.ClientModel.Client import Client
class CommonHelper:
def __init__(self):
pass
@staticmethod
def readQss(style):
with open(style,"r") as f:
return f.read()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.sub_windows = [] #存储设备widget
self.nowValue = [] #储存当前值
self.areaLabel = [] #存储通道信息文本
self.arealist = {}
self.widgetList= []
self.initUI()
self.initAreaWidget()
def initUI(self):
self.toolbar = QToolBar()
self.addToolBar(self.toolbar)
# self.setWindowState(self.windowState() | QtCore.Qt.WindowMaximized)
self.action1 = QAction("添加设备", self)
self.action1.setObjectName("action1")
self.action1.triggered.connect(self.createDeciveWidget)
self.action2 = QAction("开始通讯", self)
self.action2.setObjectName("action2")
self.action2.triggered.connect(self.startProtocol)
self.toolbar.addAction(self.action1)
self.toolbar.addAction(self.action2)
self.mdi_area = QMdiArea()
# self.scrollArea = QScrollArea()
# self.scrollArea.setWidget(self.mdi_area)
self.setCentralWidget(self.mdi_area)
self.setWindowIcon(QIcon('Static/zhjt.ico'))
# self.mdi_area.setViewMode(QMdiArea.TabbedView)
# self.mdi_area.setTabsClosable(False)
# self.mdi_area.setTabsMovable(True)
self.setWindowTitle("PROFIBUS")
self.setGeometry(1000, 500, 800, 600)
def deviceWidget(self, windowTitle):
sub_window = AreaQMdiSubWindow()# 创建一个子窗口
sub_window.setObjectName('sub_window')
areaLayout1 = QGridLayout()
areaLayout2 = QGridLayout()
horizontalLayout = QHBoxLayout()
horizontalLayout.setObjectName("horizontalLayout")
# tableWidget = QTableWidget()
widget = QWidget()
widget.setLayout(areaLayout1)
widgetArea = QWidget()
widgetArea.setLayout(areaLayout2)
newbtn = QPushButton('添加通道')
delbth = QPushButton('删除通道')
self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
horizontalLayout.addWidget(newbtn)
horizontalLayout.addItem(self.horizontalSpacer)
horizontalLayout.addWidget(delbth)
newbtn.clicked.connect(lambda: self.newAreaWidget(sub_window))
delbth.clicked.connect(lambda: self.delAreaWidget(sub_window))
areaLayout1.addLayout(horizontalLayout, 0, 0, 1, 1)
areaLayout1.addWidget(widgetArea, 1, 0, 1, 1)
areaLayout1.addItem(self.verticalSpacer)
scroll_widget = QScrollArea()
scroll_widget.setWidgetResizable(True)
scroll_widget.setWidget(widget)
sub_window.setWindowTitle(windowTitle)
sub_window.setGeometry(0, 0, 600, 500)
sub_window.setWidget(scroll_widget)
sub_window.setWindowIcon(QIcon('Static/zhjt.ico'))
self.mdi_area.addSubWindow(sub_window) # 将子窗口添加到MDI区域
# self.mdi_area.subWindowActivated.connect(self.adjustMdiAreaSize)
sub_window.show()
self.sub_windows.append(sub_window)
return sub_window
def initAreaWidget(self):
alldevices = DevicesManange().getAllDevice()
for devices in alldevices:
layout = self.deviceWidget(devices[0]).widget().widget().layout().itemAt(1).widget().layout()
areas = devices[3]
number = 0
masterSlaveModel = devices[2]
deviceName = devices[0]
if areas is not None:
areas = json.loads(areas)
for area in areas:
varType = area["type"]
channelNums = area["nums"]
channelBytes = area["bytes"]
areaID = area['id']
if masterSlaveModel == '主站':
self.addChannelWidget(deviceName, number, channelNums, channelBytes, varType, layout, areaID)
else:
self.addChannelWidget(deviceName, number, channelNums, channelBytes, varType, layout, areaID)
number = number + int(channelNums)
def createDeciveWidget(self):
dialog = DeviceDialog()
if dialog.exec_() == QDialog.Accepted:
deviceName, proType, masterSlaveModel, pvUpperLimit, pvLowerLimit, pvUnit = dialog.getParameters()
windowTitle = deviceName + ' ' + proType + masterSlaveModel + ' ' + pvLowerLimit + '-' + pvUpperLimit + pvUnit
DeviceDB().addDevice(deviceName = windowTitle, proType = proType, masterSlaveModel = masterSlaveModel, pvUpperLimit=pvUpperLimit, pvLowerLimit=pvLowerLimit, pvUnit=pvUnit)
else:
return
self.deviceWidget(windowTitle)
def newAreaWidget(self, sub_window):
deviceName = sub_window.windowTitle()
dialog = AreaSettingWidget()
if dialog.exec_() == QDialog.Accepted:
varType, channelNums, channelBytes = dialog.getParameters()
number = DevicesManange.getChannelLength(deviceName)
Device().addAreas(varType, channelNums, channelBytes, deviceName)
areaId = Device().getAreaID(deviceName)[-1]
# Device().addDevice(deviceName, channelNums, channelBytes)
else:
return
layout = sub_window.widget().widget().layout().itemAt(1).widget().layout() #获取sub_window的widgetArea的areaLayout2
self.addChannelWidget(deviceName, number, channelNums, channelBytes, varType, layout, areaId)
number += int(channelNums)
def addChannelWidget(self,deviceNames, numbers, channelNums, channelBytes , varTypes, layouts ,ID):
deviceName = deviceNames
number = numbers
channelNum = channelNums
channelByte = channelBytes
varType = varTypes
layout = layouts
ID = ID
widgetList = []
arealists = []
for i in range(int(channelNum)):
if (i + number) % 2 == 0:
areaLabel = QLabel(varType + str(i + 1 ) + ": " + channelByte + 'Byte' )
areaLabel2 =QLabel('0')
areaLineEdit = QLineEdit('0')
editbtn = QPushButton('强制')
layout.addWidget(areaLabel, (i + number)//2, (i + number) % 2)
layout.addWidget(areaLabel2, (i + number)//2, (i + number) % 2 + 1 )
layout.addWidget(areaLineEdit, (i + number)//2, (i + number) % 2 + 2 )
layout.addWidget(editbtn, (i + number) // 2, (i + number) % 2 + 3)
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
widgetList.append([layout, ID, editbtn, areaLabel, areaLabel2, areaLineEdit])
# self.nowValue.append([deviceName, layout, ID, editbtn, areaLabel, areaLabel2, areaLineEdit, (i + number)//2, (i + number) % 2 + 1])
else:
areaLabel = QLabel(varType + str(i + 1) + ": " + channelByte + 'Byte')
areaLabel2 = QLabel('0')
areaLineEdit = QLineEdit('0')
editbtn = QPushButton('强制')
layout.addWidget(areaLabel, (i + number) // 2, (i + number) % 2 + 3)
layout.addWidget(areaLabel2, (i + number) // 2, (i + number) % 2 + 4)
layout.addWidget(areaLineEdit, (i + number) // 2, (i + number) % 2 + 5)
layout.addWidget(editbtn, (i + number) // 2, (i + number) % 2 + 6)
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
widgetList.append([layout, ID, editbtn, areaLabel, areaLabel2, areaLineEdit])
arealists.append(widgetList)
self.arealist[deviceName] = arealists
# self.nowValue.append([deviceName, layout, ID, editbtn, areaLabel, areaLabel2, areaLineEdit, (i + number) // 2, (i + number) % 2 + 4])
self.nowValue.append(self.arealist)
print(self.nowValue,'aaaaaa')
def forceEdit(self, btn):
pass
# for item in self.nowValue:
# if btn in item:
# item[4].setText(item[5].text())
def delAreaWidget(self, sub_window):
subwindow = sub_window
if self.nowValue:
self.deviceName = subwindow.windowTitle()
delAreaWidget = DelAreaWidget(self.deviceName)
if delAreaWidget.exec_() == QDialog.Accepted:
rowAndColumn = delAreaWidget.getDelAreaRowAndColunm()
if rowAndColumn == []:
return
else:
self.getAreaWidget(rowAndColumn, subwindow)
Device().delAreas(self.deviceName,rowAndColumn)
else:
return
else:
return
def getAreaWidget(self, rowAndColumn, sub_window):
subwindow = sub_window
areaLayout = subwindow.widget().widget().layout().itemAt(1).widget().layout()
deviceName = subwindow.windowTitle()
for areaId in rowAndColumn:
for devicelist in self.nowValue:
areas = devicelist[deviceName]
print(areas,'sssss')
area = areas[areaId - 1]
index = areas.index(area)
print(index,'index')
print(area,'ssss')
# for area in areas:
# for widget in range(2, 6):
# print(widget)
# print(area[widget])
# area[widget].deleteLater()
# areaLayout.removeWidget(area[widget])
# self.nowValue.remove(areaS)
# # print(self.nowValue)
# print(rowAndColumn,'sssss')
# for areaId in rowAndColumn:
# for nowWidget in self.nowValue:
# if nowWidget[0] == deviceName and nowWidget[2] == areaId:
# for widget in range(3, 7):
#
# areaLayout.removeWidget(nowWidget[widget])
# nowWidget[widget].deleteLater()
# delAreaWidgetList.append(nowWidget)
# for widget in delAreaWidgetList: #从列表中删除已删除的部件
# self.nowValue.remove(widget)
#重置area的id
# i = 1
# j = 1
# for nowWidget in self.nowValue:
# if nowWidget[0] == deviceName:
# if nowWidget[2] == j:
# index = nowWidget.index(nowWidget[2])
# nowWidget[index] = i
# else:
# j = nowWidget[2]
# i += 1
# index = nowWidget.index(nowWidget[2])
# nowWidget[index] = i
def startProtocol(self):
a = []
for i in range(len(self.nowValue)):
a.append(i)
for i in a :
for j in self.nowValue:
j[5].setText(str(i))
print(a)
def closeEvent(self, event):
pass
def deleteAreaWidget(self):
pass
class AreaQMdiSubWindow(QMdiSubWindow):
def __init__(self):
super().__init__()
self.number = 0
def closeEvent(self, event):
DeviceDB.deleteDevice(deviceName = self.windowTitle())
if __name__ == '__main__':
app = QApplication(sys.argv)
Client.initDB()
window = MainWindow()
window.show()
sys.exit(app.exec_())