From d3d50d790af54ad876a47497c16b60c88138b793 Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Thu, 2 May 2024 15:22:38 +0800 Subject: [PATCH] gui.tools: add DoubleClickLineEdit --- artiq/gui/tools.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/artiq/gui/tools.py b/artiq/gui/tools.py index 2171ab915..957823780 100644 --- a/artiq/gui/tools.py +++ b/artiq/gui/tools.py @@ -4,6 +4,40 @@ import logging from PyQt5 import QtCore, QtWidgets +class DoubleClickLineEdit(QtWidgets.QLineEdit): + finished = QtCore.pyqtSignal() + + def __init__(self, init): + QtWidgets.QLineEdit.__init__(self, init) + self.setFrame(False) + self.setReadOnly(True) + self.returnPressed.connect(self._return_pressed) + self.editingFinished.connect(self._editing_finished) + self._text = init + + def mouseDoubleClickEvent(self, event): + if self.isReadOnly(): + self.setReadOnly(False) + self.setFrame(True) + QtWidgets.QLineEdit.mouseDoubleClickEvent(self, event) + + def _return_pressed(self): + self._text = self.text() + + def _editing_finished(self): + self.setReadOnly(True) + self.setFrame(False) + self.setText(self._text) + self.finished.emit() + + def keyPressEvent(self, event): + key = event.key() + if key == QtCore.Qt.Key_Escape and not self.isReadOnly(): + self.editingFinished.emit() + else: + QtWidgets.QLineEdit.keyPressEvent(self, event) + + def log_level_to_name(level): if level >= logging.CRITICAL: return "CRITICAL"