librariesdialog.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 import sys
21 from PyQt5.QtWidgets import QDialog, \
22  QLineEdit, QPushButton, QApplication, \
23  QScrollArea, QVBoxLayout, QGroupBox, QHBoxLayout, \
24  QBoxLayout, QMessageBox
25 from PyQt5.QtCore import pyqtSignal, Qt
26 from visualstates.gui.util.editablestringwidget import EditableStringWidget
27 
28 
29 class LibrariesDialog(QDialog):
30 
31  librariesChanged = pyqtSignal(list)
32 
33  def __init__(self, name, libraries):
34  super(QDialog, self).__init__()
35  self.setWindowTitle(name)
36  self.setMinimumHeight(500)
37  self.setMinimumWidth(500)
38  self.libraries = libraries
39  self.libraryNameEdit = QLineEdit()
40  self.addButton = QPushButton("Add")
41  self.addButton.setMinimumWidth(100)
42 
43  # create gui of
44  verticalLayout = QVBoxLayout()
45  self.setLayout(verticalLayout)
46  scrollArea = QScrollArea()
47  scrollArea.setMinimumHeight(400)
48  scrollArea.setWidgetResizable(True)
49  scrollArea.setStyleSheet('QScrollArea {border: 0;}')
50  verticalLayout.addWidget(scrollArea)
51  groupBox = QGroupBox("Libraries")
52  scrollArea.setWidget(groupBox)
53  self.groupLayout = QVBoxLayout()
54  self.groupLayout.setDirection(QBoxLayout.TopToBottom)
55  self.groupLayout.setAlignment(Qt.AlignTop)
56  groupBox.setLayout(self.groupLayout)
57 
58  # add row to group layout
59  rowLayout = QHBoxLayout()
60  self.groupLayout.addLayout(rowLayout)
61  rowLayout.addWidget(self.libraryNameEdit)
62  rowLayout.addWidget(self.addButton)
63  self.addButton.clicked.connect(self.addClicked)
64 
65  self.libraryUIs = {}
66 
67  # add libraries to the ui
68  for libraryName in self.libraries:
69  self.addLibraryItem(libraryName)
70 
71  def removed(self, libraryName):
72  if libraryName in self.libraryUIs:
73  libRow = self.libraryUIs[libraryName]
74  libRow.disconnect()
75  self.groupLayout.removeWidget(libRow)
76  del self.libraryUIs[libraryName]
77  libRow.deleteLater()
78  self.libraries.remove(libraryName)
79  self.librariesChanged.emit(self.libraries)
80 
81  def updated(self, oldLibraryName, newLibraryName):
82  if oldLibraryName in self.libraryUIs:
83  libRow = self.libraryUIs[oldLibraryName]
84  del self.libraryUIs[oldLibraryName]
85  self.libraryUIs[newLibraryName] = libRow
86  # update library list
87  for i in range(len(self.libraries)):
88  if self.libraries[i] == oldLibraryName:
89  self.libraries[i] = newLibraryName
90  break
91  self.librariesChanged.emit(self.libraries)
92 
93  def addClicked(self):
94  libraryInp = self.libraryNameEdit.text().strip()
95  if libraryInp in self.libraries:
96  QMessageBox.information(self, "Library Present", "Library already present in the list")
97  return
98  if libraryInp:
99  self.libraries.append(self.libraryNameEdit.text())
100  self.librariesChanged.emit(self.libraries)
101  self.addLibraryItem(self.libraryNameEdit.text())
102  self.libraryNameEdit.setText('')
103 
104  def addLibraryItem(self, libraryName):
105  libRow = EditableStringWidget(libraryName)
106  self.libraryUIs[libraryName] = libRow
107  libRow.removed.connect(self.removed)
108  libRow.updated.connect(self.updated)
109  self.groupLayout.addWidget(libRow)
110 
111 if __name__ == '__main__':
112  app = QApplication(sys.argv)
113  dialog = LibrariesDialog('Libraries', ['okan'])
114  dialog.exec_()
115 
116 
117 
118 
119 
def updated(self, oldLibraryName, newLibraryName)


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