华荣三照明、合信、荣欣八组合馈电
This commit is contained in:
175
QT5_Project/Shared_CODE/DialogModifyValue.py
Normal file
175
QT5_Project/Shared_CODE/DialogModifyValue.py
Normal file
@ -0,0 +1,175 @@
|
||||
import sys
|
||||
import os
|
||||
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QLabel, QWidget, QPushButton, QMessageBox, QComboBox, QDialogButtonBox, QShortcut
|
||||
from PyQt5 import uic
|
||||
from PyQt5.QtGui import QKeySequence
|
||||
from PyQt5.QtCore import Qt, QEvent, QObject, QTimer
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
||||
ui_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../Shared_UI'))
|
||||
ui_file_path = os.path.join(ui_path, "DialogModifyValue.ui")
|
||||
|
||||
def modify_string(string, index, new_char):
|
||||
string_list = list(string)
|
||||
string_list[index] = new_char
|
||||
modified_string = ''.join(string_list)
|
||||
return modified_string
|
||||
|
||||
def chr_adjust(old_chr, dif : int) :
|
||||
new_chr = old_chr
|
||||
if old_chr == '+':
|
||||
new_chr = '-'
|
||||
elif old_chr == '-':
|
||||
new_chr = '+'
|
||||
elif dif > 0:
|
||||
new_chr = chr(ord(old_chr) + 1)
|
||||
if new_chr > '9' :
|
||||
new_chr = '0'
|
||||
else :
|
||||
new_chr = chr(ord(old_chr) - 1)
|
||||
if new_chr < '0' :
|
||||
new_chr = '9'
|
||||
return new_chr
|
||||
|
||||
|
||||
class DialogModifyValue(QDialog):
|
||||
def __init__(self, main_window, parent=None):
|
||||
super(DialogModifyValue, self).__init__(parent)
|
||||
# Load UI file
|
||||
uic.loadUi(ui_file_path, self)
|
||||
|
||||
self.value_origin = 1000.3
|
||||
self.value_modify = 1000.3
|
||||
self.format = "%05.1f"
|
||||
self.scale = 1.0
|
||||
self.offset = 0.0
|
||||
self.select_bit = 0
|
||||
self.main_window = main_window
|
||||
|
||||
self.setWindowTitle("参数修改")
|
||||
self.setWindowFlag(Qt.FramelessWindowHint)
|
||||
ok_button = self.buttonBox.button(QDialogButtonBox.Ok)
|
||||
if ok_button is None:
|
||||
pass
|
||||
else:
|
||||
ok_button.setText('')
|
||||
Cancel_button = self.buttonBox.button(QDialogButtonBox.Cancel)
|
||||
if Cancel_button is None:
|
||||
pass
|
||||
else:
|
||||
Cancel_button.setText('')
|
||||
|
||||
self.buttonBox.clicked.connect(self.dialog_button_ok_clicked)
|
||||
|
||||
modify_value = self.value_origin * self.scale + self.offset
|
||||
self.update_modify_info(self.format, modify_value, "缺省参数修改", self.offset)
|
||||
|
||||
#让定时器周期刷修改字符串, 防止出现焦点切换时选中消失的情况
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self.timeout_modify_string_refresh)
|
||||
self.timer.start(100)
|
||||
|
||||
#定义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)
|
||||
|
||||
home_button = self.findChild(QPushButton, "KEY_HOME")
|
||||
if home_button:
|
||||
home_button.clicked.connect(self.key_escape_process)
|
||||
|
||||
end_button = self.findChild(QPushButton, "KEY_END")
|
||||
if end_button:
|
||||
end_button.clicked.connect(self.key_enter_process)
|
||||
|
||||
up_button = self.findChild(QPushButton, "KEY_UP")
|
||||
if up_button:
|
||||
up_button.clicked.connect(self.key_increase_parameter)
|
||||
|
||||
down_button = self.findChild(QPushButton, "KEY_DOWN")
|
||||
if down_button:
|
||||
down_button.clicked.connect(self.key_decrease_parameter)
|
||||
@property
|
||||
def value(self):
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
text : str = line_modify.text()
|
||||
return text
|
||||
|
||||
def timeout_modify_string_refresh(self) :
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
modify_str : str = line_modify.text()
|
||||
self.update_modify_string(modify_str)
|
||||
|
||||
def update_modify_info(self, format_str, value, caption : str, offset = 0) :
|
||||
self.value_origin = value
|
||||
self.format = format_str
|
||||
self.offset = offset
|
||||
if hasattr(self, "label") :
|
||||
label : QLabel = self.label
|
||||
label.setText(caption)
|
||||
if self.format != "" :
|
||||
modify_str = self.format%(value)
|
||||
else :
|
||||
modify_str = str(value)
|
||||
self.update_modify_string(modify_str)
|
||||
|
||||
def update_modify_string(self, modify_str : str) :
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
line_modify.setText(modify_str)
|
||||
|
||||
if self.select_bit < len(modify_str) :
|
||||
if modify_str[self.select_bit] == ' ':
|
||||
self.key_enter_process()
|
||||
|
||||
# 设置选择从第self.select_bit个字符开始,长度为1
|
||||
line_modify.setSelection(self.select_bit, 1)
|
||||
|
||||
def key_increase_parameter(self):
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
text : str = line_modify.text()
|
||||
if self.select_bit < len(text) :
|
||||
old_chr = text[self.select_bit]
|
||||
new_chr = chr_adjust(old_chr, 1)
|
||||
new_modify_text = modify_string(text, self.select_bit, new_chr)
|
||||
self.update_modify_string(new_modify_text)
|
||||
|
||||
def key_decrease_parameter(self):
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
text : str = line_modify.text()
|
||||
if self.select_bit < len(text) :
|
||||
old_chr = text[self.select_bit]
|
||||
new_chr = chr_adjust(old_chr, -1)
|
||||
new_modify_text = modify_string(text, self.select_bit, new_chr)
|
||||
self.update_modify_string(new_modify_text)
|
||||
|
||||
def key_enter_process(self):
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
text : str = line_modify.text()
|
||||
|
||||
if self.select_bit < len(text) - 1:
|
||||
self.select_bit += 1
|
||||
if not str.isdigit(text[self.select_bit]) :
|
||||
self.key_enter_process()
|
||||
else :
|
||||
self.update_modify_string(text)
|
||||
else :
|
||||
self.buttonBox.button(QDialogButtonBox.Ok).click()
|
||||
|
||||
def key_escape_process(self):
|
||||
self.buttonBox.button(QDialogButtonBox.Cancel).click()
|
||||
|
||||
def dialog_button_ok_clicked(self, button):
|
||||
if button == self.buttonBox.button(QDialogButtonBox.Ok) :
|
||||
line_modify : QLineEdit = self.lineEdit
|
||||
text : str = line_modify.text()
|
||||
self.value_modify = float(text)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
dialog = DialogModifyValue()
|
||||
dialog.show()
|
||||
|
||||
sys.exit(dialog.exec_())
|
||||
Reference in New Issue
Block a user