workspacetab.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, os
21 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QGroupBox, QHBoxLayout, QPushButton, \
22  QApplication, QLabel, QScrollArea, QBoxLayout, QFileDialog, QMessageBox
23 from PyQt5.QtCore import Qt
24 from visualstates.configs.rospackage import readWorkspaces
25 from visualstates.configs.rospackage import writeWorkspaces
26 
27 
28 class WorkspaceTab(QWidget):
29  def __init__(self):
30  super(QWidget, self).__init__()
31 
32  layout = QVBoxLayout()
33  self.setLayout(layout)
34 
35  scrollArea = QScrollArea()
36  scrollArea.setMinimumHeight(200)
37  scrollArea.setWidgetResizable(True)
38  scrollArea.setStyleSheet('QScrollArea {border: 0;}')
39  layout.addWidget(scrollArea)
40 
41  workspaceBox = QGroupBox('Catkin Workspaces')
42  scrollArea.setWidget(workspaceBox)
43  self.workspaceLayout = QVBoxLayout()
44  self.workspaceLayout.setDirection(QBoxLayout.TopToBottom)
45  self.workspaceLayout.setAlignment(Qt.AlignTop)
46  workspaceBox.setLayout(self.workspaceLayout)
47 
48  buildRowLayout = QHBoxLayout()
49  self.workspaceLayout.addLayout(buildRowLayout)
50  chooseDirButton = QPushButton('Choose File')
51  chooseDirButton.setMaximumWidth(100)
52  chooseDirButton.setObjectName('choose')
53  chooseDirButton.clicked.connect(self.chooseDir)
54  buildRowLayout.addWidget(chooseDirButton)
55  self.dirLabel = QLabel()
56  self.dirLabel.setMinimumWidth(400)
57  buildRowLayout.addWidget(self.dirLabel)
58 
59  addButton = QPushButton('Add')
60  addButton.setMaximumWidth(100)
61  addButton.setObjectName('build')
62  addButton.clicked.connect(self.addWorkspace)
63  buildRowLayout.addWidget(addButton)
64 
65  # load current workspaces
66  self.workspaceUIs = []
68 
69  def chooseDir(self):
70  selectedDir = QFileDialog.getExistingDirectory(self, 'Choose Catkin Workspace Directory')
71  self.dirLabel.setText(selectedDir)
72 
73  def addWorkspace(self):
74  newDir = self.dirLabel.text()
75  if newDir == '':
76  QMessageBox.warning(self, 'Invalid Workspace', 'This is not a catkin workspace or specified path was not found')
77  elif self.isValidWorkspace(newDir):
78  workspaces = readWorkspaces()
79  if newDir in workspaces:
80  QMessageBox.information(self, 'Workspace present', 'The workspace entered is already present in the list')
81  return
82  workspaces.append(newDir)
83  writeWorkspaces(workspaces)
84  self.drawWorkspaces(workspaces)
85  else:
86  QMessageBox.warning(self, 'Invalid Workspace', 'The catkin workspace dir:' + newDir +
87  ' is not a valid. Please make sure that the directory has src, devel and build directories.')
88 
89  def isValidWorkspace(self, dir):
90  files = os.listdir(dir)
91  return '.catkin_workspace' in files
92 
93  def addWorkspaceUI(self, layout, dir, workspaceUIs, removeCallback):
94  uiData = {}
95  rowLayout = QHBoxLayout()
96  uiData['layout'] = rowLayout
97  layout.addLayout(rowLayout)
98  label = QLabel(dir)
99  label.setStyleSheet('QLabel { border: 1px solid black; }')
100  uiData['label'] = label
101  removeBtn = QPushButton('Remove')
102  removeBtn.setMaximumWidth(80)
103  removeBtn.clicked.connect(removeCallback)
104  removeBtn.setObjectName(dir)
105  uiData['btn'] = removeBtn
106  rowLayout.addWidget(label)
107  rowLayout.addWidget(removeBtn)
108  workspaceUIs.append(uiData)
109 
110  def removeWorkspace(self):
111  dir = self.sender().objectName()
112  workspaces = readWorkspaces()
113  workspaces.remove(dir)
114  writeWorkspaces(workspaces)
115  self.drawWorkspaces(workspaces)
116 
117  def drawWorkspaces(self, workspaces):
118  # remove previous UIs
119  for uiData in self.workspaceUIs:
120  uiData['label'].deleteLater()
121  uiData['btn'].deleteLater()
122  uiData['layout'].deleteLater()
123  self.workspaceUIs = []
124 
125  for dir in workspaces:
126  self.addWorkspaceUI(self.workspaceLayout, dir, self.workspaceUIs, self.removeWorkspace)
127 
128 
129 if __name__ == '__main__':
130  app = QApplication(sys.argv)
131  widget = WorkspaceTab()
132  widget.show()
133  app.exec_()
134 
def addWorkspaceUI(self, layout, dir, workspaceUIs, removeCallback)
Definition: workspacetab.py:93


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