editablestringwidget.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 
21 import sys
22 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLineEdit, QPushButton, QLabel, \
23  QApplication
24 from PyQt5.QtCore import pyqtSignal
25 
26 
27 class EditableStringWidget(QWidget):
28 
29  removed = pyqtSignal(str)
30  updated = pyqtSignal(str, str)
31 
32  def __init__(self, text):
33  super(QWidget, self).__init__()
34  self.layout = QHBoxLayout()
35  self.setLayout(self.layout)
36  # by default show the text in label
37  self.label = QLabel(text)
38  self.label.setMinimumWidth(200)
39  self.layout.addWidget(self.label)
40 
41  self.lineEdit = QLineEdit(text)
42  self.lineEdit.setMinimumWidth(200)
43  self.layout.addWidget(self.lineEdit)
44  self.lineEdit.setVisible(False)
45 
46  self.editButton = QPushButton("Edit")
47  self.editButton.setMaximumWidth(80)
48  self.editButton.setMinimumWidth(80)
49  self.editButton.clicked.connect(self.editClicked)
50  self.layout.addWidget(self.editButton)
51  self.removeButton = QPushButton("Remove")
52  self.removeButton.setMaximumWidth(80)
53  self.removeButton.setMinimumWidth(80)
54  self.removeButton.clicked.connect(self.removeClicked)
55  self.layout.addWidget(self.removeButton)
56 
57  # save button
58  self.saveButton = QPushButton("Save")
59  self.saveButton.setMinimumWidth(80)
60  self.saveButton.setMaximumWidth(80)
61  self.saveButton.clicked.connect(self.saveClicked)
62  self.layout.addWidget(self.saveButton)
63  self.saveButton.setVisible(False)
64 
65  # cancel button
66  self.cancelButton = QPushButton("Cancel")
67  self.cancelButton.setMinimumWidth(80)
68  self.cancelButton.setMaximumWidth(80)
69  self.cancelButton.clicked.connect(self.cancelClicked)
70  self.layout.addWidget(self.cancelButton)
71  self.cancelButton.setVisible(False)
72 
73  self.label.setStyleSheet('QLabel { border: 1px solid black; }')
74  self.setContentsMargins(0, 0, 0, 0)
75  self.layout.setContentsMargins(0, 0, 0, 0)
76 
77  def editClicked(self):
78  self.removeButton.setVisible(False)
79  self.editButton.setVisible(False)
80  self.label.setVisible(False)
81 
82  self.lineEdit.setVisible(True)
83  self.saveButton.setVisible(True)
84  self.cancelButton.setVisible(True)
85 
86  def removeClicked(self):
87  self.removed.emit(self.label.text())
88 
89  def saveClicked(self):
90  self.updated.emit(self.label.text(), self.lineEdit.text())
91  self.label.setText(self.lineEdit.text())
92  self.label.setVisible(True)
93  self.editButton.setVisible(True)
94  self.removeButton.setVisible(True)
95 
96  self.lineEdit.setVisible(False)
97  self.cancelButton.setVisible(False)
98  self.saveButton.setVisible(False)
99 
100  def cancelClicked(self):
101  self.cancelButton.setVisible(False)
102  self.saveButton.setVisible(False)
103  self.lineEdit.setVisible(False)
104 
105  self.label.setVisible(True)
106  self.removeButton.setVisible(True)
107  self.editButton.setVisible(True)
108 
109 
110 if __name__ == '__main__':
111  app = QApplication(sys.argv)
112  widget = EditableStringWidget('okan')
113  widget.show()
114  app.exec_()


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20