Package node_manager_fkie :: Package editor :: Module line_edit
[frames] | no frames]

Source Code for Module node_manager_fkie.editor.line_edit

 1  from python_qt_binding.QtCore import Qt 
 2  from python_qt_binding.QtGui import QIcon 
 3  try: 
 4      from python_qt_binding.QtGui import QLineEdit, QToolButton, QStyle 
 5  except: 
 6      from python_qt_binding.QtWidgets import QLineEdit, QToolButton, QStyle 
 7   
 8   
9 -class EnchancedLineEdit(QLineEdit):
10
11 - def __init__(self, parent=None):
12 QLineEdit.__init__(self, parent) 13 # Create a clear button with icon 14 self.clearBtn = clearBtn = QToolButton(self) 15 icon = QIcon.fromTheme("edit-clear", QIcon(":/icons/crystal_clear_button_close.png")) 16 clearBtn.setIcon(icon) 17 clearBtn.setCursor(Qt.ArrowCursor) 18 clearBtn.setStyleSheet("QToolButton { border: none; padding: 0px; }") 19 clearBtn.hide() 20 21 # signals, clear lineEdit if btn pressed; change btn visibility on input 22 clearBtn.clicked.connect(self.clear) 23 self.textChanged[str].connect(self.update_close_button) 24 25 frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) 26 self.setStyleSheet("QLineEdit { padding-right: %dpx; } " % (clearBtn.sizeHint().width() + frameWidth + 1)) 27 msz = self.minimumSizeHint() 28 self.setMinimumSize(max(msz.width(), clearBtn.sizeHint().height() + frameWidth * 2 + 2), 29 max(msz.height(), clearBtn.sizeHint().height() + frameWidth * 2 + 2))
30
31 - def resizeEvent(self, event):
32 sz = self.clearBtn.sizeHint() 33 frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) 34 self.clearBtn.move(self.rect().right() - frameWidth - sz.width(), 35 (self.rect().bottom() + 1 - sz.height()) / 2)
36
37 - def update_close_button(self, text):
38 self.clearBtn.setVisible(True if text else False)
39
40 - def keyPressEvent(self, event):
41 ''' 42 Enable the ESC handling 43 ''' 44 if event.key() == Qt.Key_Escape and self.text(): 45 self.setText('') 46 else: 47 event.accept() 48 QLineEdit.keyPressEvent(self, event)
49