130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QLabel, QVBoxLayout, QPushButton, QMessageBox, QComboBox, QDialogButtonBox, QShortcut
|
|
from PyQt5 import uic
|
|
from PyQt5.QtGui import QKeySequence
|
|
from PyQt5.QtCore import Qt, QEvent, QObject
|
|
import json
|
|
from print_color import *
|
|
|
|
sys_path = sys.path[0].replace("\\", "/")
|
|
ui_file_path = sys_path + "/DialogModifyAlias.ui"
|
|
|
|
class DialogModifyAlias(QDialog):
|
|
def __init__(self, main_window, parent=None):
|
|
super(DialogModifyAlias, self).__init__(parent)
|
|
# Load UI file
|
|
uic.loadUi(ui_file_path, self)
|
|
combo : QComboBox = self.comboBox
|
|
|
|
combo.clear()
|
|
self.alias_item_dict = {}
|
|
self.main_window = main_window
|
|
|
|
self.setWindowTitle("别名参数修改")
|
|
self.setWindowFlag(Qt.FramelessWindowHint)
|
|
self.buttonBox.button(QDialogButtonBox.Ok).setText('确定')
|
|
self.buttonBox.button(QDialogButtonBox.Cancel).setText('取消')
|
|
self.buttonBox.clicked.connect(self.dialog_button_ok_clicked)
|
|
|
|
#定义4个快捷键, Key_Enter, Key_Escape经常被各类程序模块使用, 用Key_End, 与Key_Home来代替
|
|
QShortcut(QKeySequence(Qt.Key_Up), self, activated=self.key_increase_parameter)
|
|
QShortcut(QKeySequence(Qt.Key_Down), self, activated=self.key_decrease_parameter)
|
|
QShortcut(QKeySequence(Qt.Key_PageDown), self, activated=self.key_enter_process)
|
|
QShortcut(QKeySequence(Qt.Key_PageUp), self, activated=self.key_escape_process)
|
|
QShortcut(QKeySequence(Qt.Key_End), self, activated=self.key_enter_process)
|
|
QShortcut(QKeySequence(Qt.Key_Home), self, activated=self.key_escape_process)
|
|
|
|
def set_caption_name(self, name : str) :
|
|
label : QLabel = self.label
|
|
label.setText(name)
|
|
|
|
def key_increase_parameter(self):
|
|
combo : QComboBox = self.comboBox
|
|
index = combo.currentIndex()
|
|
index += 1
|
|
if index >= combo.count() :
|
|
index = 0
|
|
combo.setCurrentIndex(index)
|
|
|
|
def key_decrease_parameter(self):
|
|
combo : QComboBox = self.comboBox
|
|
index = combo.currentIndex()
|
|
index -= 1
|
|
if index < 0 :
|
|
index = combo.count() - 1
|
|
combo.setCurrentIndex(index)
|
|
|
|
def get_alias_key_str(self):
|
|
combo : QComboBox = self.comboBox
|
|
select_text = combo.currentText()
|
|
alias_key = None
|
|
for alias_key, alias_str in self.alias_item_dict.items() :
|
|
if alias_str == select_text :
|
|
return alias_key
|
|
return alias_key
|
|
|
|
@property
|
|
def value(self):
|
|
alias_key = self.get_alias_key_str()
|
|
return alias_key
|
|
|
|
def modify_alias_value(self, modify_str) :
|
|
if self.main_window != None :
|
|
if hasattr(self.main_window, "func_call_back_on_mqtt_param_modify") :
|
|
self.main_window.func_call_back_on_mqtt_param_modify(modify_str)
|
|
|
|
def key_enter_process(self):
|
|
alias_key = self.get_alias_key_str()
|
|
self.modify_alias_value(alias_key)
|
|
self.buttonBox.button(QDialogButtonBox.Ok).click()
|
|
|
|
def key_escape_process(self):
|
|
self.buttonBox.button(QDialogButtonBox.Cancel).click()
|
|
|
|
def set_alias_item_info(self, alias_item_dict, key_value, inform_message : str = "系统参数") :
|
|
combo : QComboBox = self.comboBox
|
|
|
|
self.alias_item_dict = alias_item_dict
|
|
combo.clear()
|
|
current_index = 0
|
|
select_index = -1
|
|
|
|
self.set_caption_name(inform_message)
|
|
|
|
for key, value_str in alias_item_dict.items() :
|
|
combo.addItem(value_str)
|
|
if key_value == key :
|
|
select_index = current_index
|
|
current_index += 1
|
|
|
|
if select_index >= 0 :
|
|
combo.setCurrentIndex(select_index)
|
|
else :
|
|
combo.setCurrentText(key_value)
|
|
|
|
def dialog_button_ok_clicked(self, button):
|
|
if button == self.buttonBox.button(QDialogButtonBox.Ok) :
|
|
alias_key = self.get_alias_key_str()
|
|
self.modify_alias_value(alias_key)
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication(sys.argv)
|
|
alias_select = {0 : "380V", 1 : "660V", 2: "1140V", 3 : "3300V"}
|
|
json_msg : str = json.dumps(alias_select, ensure_ascii=False)
|
|
print_inform_msg(json_msg)
|
|
|
|
alias_2 = {"alias_voltage" : {0 : "380V", 1 : "660V", 2: "1140V", 3 : "3300V"}}
|
|
json_msg2 : str = json.dumps(alias_2, ensure_ascii=False)
|
|
print_inform_msg(json_msg2)
|
|
|
|
|
|
dialog = DialogModifyAlias(None)
|
|
dialog.set_alias_item_info(alias_select, 1)
|
|
dialog.show()
|
|
result = dialog.exec_()
|
|
|
|
if dialog.result() == QDialog.Accepted:
|
|
print_inform_msg(dialog.get_alias_key_str()) # 输出: Button clicked
|
|
|
|
sys.exit(0)
|