class TabBar(QTabBar): ' custom tab bar ' def __init__(self, parent): ' init class custom tab bar ' QTabBar.__init__(self, parent) self._editor = QLineEdit(self) self._editor.setToolTip(' Type a Tab Name ') self._editor.setWindowFlags(Qt.Popup) self._editor.setFocusProxy(self) self._editor.editingFinished.connect(self.handleEditingFinished) self._editor.installEventFilter(self) def eventFilter(self, widget, event): ' filter mouse, esc key, events ' if ((event.type() == QEvent.MouseButtonPress and not self._editor.geometry().contains(event.globalPos())) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape)): self._editor.hide() return True return QTabBar.eventFilter(self, widget, event) def mouseDoubleClickEvent(self, event): ' handle double click ' index = self.tabAt(event.pos()) if index >= 0: self.editTab(index) def editTab(self, index): ' handle the editor ' rect = self.tabRect(index) self.setTabTextColor(index, QColorDialog.getColor()) self._editor.setFixedSize(rect.size()) self._editor.move(self.parent().mapToGlobal(rect.topLeft())) self._editor.setText(self.tabText(index)) if not self._editor.isVisible(): self._editor.show() def handleEditingFinished(self): ' set text when editing has finished ' index = self.currentIndex() if index >= 0: self._editor.hide() self.setTabText(index, self._editor.text())
class EditLabel(QWidget): textoModificado = pyqtSignal(str) visivelModificado = pyqtSignal() def __init__(self, texto, parent=None): super().__init__(parent) self._configurarGui(texto) self._edit.returnPressed.connect(self.mudarVisivel) self._label.doubleClicked.connect(self.mudarVisivel) self._label.textoModificado.connect(self.setTexto) def _configurarGui(self, texto): layout = QVBoxLayout() layout.setMargin(0) self._label = LabelDrop(texto) self._edit = QLineEdit(texto) self._edit.setVisible(False) layout.addWidget(self._label) layout.addWidget(self._edit) self.setLayout(layout) def setTexto(self, texto): self._label.setText(texto) self._edit.setText(texto) self.textoModificado.emit(texto) def getTexto(self): return self.edit.text() def mudarVisivel(self): self._label.setVisible(not self._label.isVisible()) self._edit.setVisible(not self._edit.isVisible()) if self._edit.text() != self._label.text(): self.setTexto(self._edit.text()) self.visivelModificado.emit()