137 lines
5.1 KiB
Python
137 lines
5.1 KiB
Python
import sys
|
||
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 = sys.path[0].replace("\\", "/")
|
||
|
||
ui_file_path = sys_path + "/DialogModifyText.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 DialogModifyText(QDialog):
|
||
def __init__(self, main_window, parent=None):
|
||
super(DialogModifyText, self).__init__(parent)
|
||
# Load UI file
|
||
uic.loadUi(ui_file_path, self)
|
||
|
||
self.Text = "2019-09-10 10:22:23"
|
||
self.select_bit = 0
|
||
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)
|
||
|
||
modify_text = self.Text
|
||
self.update_modify_info(modify_text, "缺省多字符修改")
|
||
|
||
#让定时器周期刷修改字符串, 防止出现焦点切换时选中消失的情况
|
||
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)
|
||
|
||
@property
|
||
def value(self):
|
||
return self.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, modify_str, caption : str) :
|
||
if hasattr(self, "label") :
|
||
label : QLabel = self.label
|
||
label.setText(caption)
|
||
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
|
||
self.Text = line_modify.text()
|
||
|
||
if __name__ == '__main__':
|
||
app = QApplication(sys.argv)
|
||
dialog = DialogModifyText(None)
|
||
dialog.show()
|
||
dialog.exec_()
|
||
sys.exit(0)
|