source: geniuswebcore/geniusweb/simplerunner/gui/SettingsPanel.py@ 92

Last change on this file since 92 was 90, checked in by Bart Vastenhouw, 3 years ago

Refactor to help reusing partiesserver.

File size: 4.5 KB
Line 
1import json
2import traceback
3
4from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel, QComboBox, QSpinBox, \
5 QFrame, QSplitter, QLineEdit, QPushButton, QTableView, QScrollArea, QVBoxLayout, \
6 QTextEdit, QMessageBox
7from pyson.ObjectMapper import ObjectMapper
8from uri.uri import URI
9
10from geniusweb.deadline.Deadline import Deadline
11from geniusweb.deadline.DeadlineRounds import DeadlineRounds
12from geniusweb.deadline.DeadlineTime import DeadlineTime
13from geniusweb.protocol.NegoSettings import NegoSettings
14from geniusweb.protocol.session.TeamInfo import TeamInfo
15from geniusweb.protocol.session.saop.SAOPSettings import SAOPSettings
16from geniusweb.references.Parameters import Parameters
17from geniusweb.references.PartyRef import PartyRef
18from geniusweb.references.PartyWithParameters import PartyWithParameters
19from geniusweb.references.PartyWithProfile import PartyWithProfile
20from geniusweb.references.ProfileRef import ProfileRef
21from geniusweb.simplerunner.gui.MyFileChooser import MyFileChooser
22from geniusweb.simplerunner.gui.SelectionModel import SelectionModel
23
24
25class SettingsPanel(QWidget):
26
27 def __init__(self):
28 super().__init__()
29 self.jackson = ObjectMapper()
30 self.protocolcombo = QComboBox()
31 self.protocolcombo.addItems([ "SAOP" ])
32 self.timepanel = TimePanel()
33 self.nextparty = QLineEdit()
34 self.nextparameters = QLineEdit()
35
36 self.nextprofile = MyFileChooser()
37 self.addButton = QPushButton()
38 self.addButton.setText("add")
39 self.selectedparties = SelectionModel()
40
41 layout = QGridLayout()
42 self.setLayout(layout)
43
44 layout.addWidget(WithLabel("protocol", self.protocolcombo))
45 layout.addWidget(WithLabel("deadline", self.timepanel))
46 layout.addWidget(Separator())
47
48 partic = QLabel()
49 partic.setText("participants")
50 layout.addWidget(partic)
51
52 layout.addWidget(WithLabel("party:", self.nextparty))
53 layout.addWidget(WithLabel("parameters:", self.nextparameters))
54 layout.addWidget(WithLabel("profile:", self.nextprofile))
55
56 layout.addWidget(Separator())
57
58 layout.addWidget(self.addButton)
59
60 label = QLabel()
61 label.setText("Selected Profiles, parties for the session")
62 layout.addWidget(label)
63
64 table = QTableView()
65 table.setModel(self.selectedparties)
66 layout.addWidget(table)
67 self.addButton.clicked.connect(self.addparty);
68
69 def addparty(self):
70 try:
71 self.addparty1()
72 except ValueError as e:
73 traceback.print_exc()
74 mbox = QMessageBox()
75 mbox.setText(str(e))
76 mbox.setWindowTitle("Failed to add party")
77 mbox.setIcon(QMessageBox.Icon.Warning)
78 mbox.exec_()
79
80 def addparty1(self):
81 try:
82 partyref = PartyRef(URI("pythonpath:" + self.nextparty.text()))
83 except Exception as e:
84 raise ValueError("Party not set correctly:" + str(e))
85
86 try:
87 parameters = self.jackson.parse(json.loads("{" + self.nextparameters.text() + "}"),
88 Parameters)
89 except Exception as e:
90 raise ValueError("Parameters not set correctly:" + str(e))
91
92 try:
93 profile = ProfileRef(URI("file:" + self.nextprofile.getFile()))
94 except Exception as e:
95 raise ValueError("Profile not set correctly:" + str(e))
96
97 partywithparams = PartyWithParameters(partyref, parameters);
98 partywithprofile = PartyWithProfile(partywithparams, profile);
99 teaminfo = TeamInfo([partywithprofile])
100 self.selectedparties.addTeam(teaminfo)
101
102 def getSettings(self) -> NegoSettings:
103 return SAOPSettings(self.selectedparties.getTeams(),
104 self.timepanel.getDeadline())
105
106
107class WithLabel (QWidget):
108 '''
109 adds label to left of component
110 '''
111
112 def __init__(self, label:str, component:QWidget):
113 super().__init__()
114 layout = QGridLayout()
115 self.setLayout(layout)
116
117 qlabel = QLabel()
118 qlabel.setText(label)
119
120 layout.addWidget(qlabel, 0, 0)
121 layout.addWidget(component, 0, 1)
122
123
124class TimePanel(QWidget):
125 DEFAULTDEADLINE = 10000 # millisecs
126
127 def __init__(self):
128 super().__init__()
129 self.unitcombo = QComboBox()
130 self.unitcombo.addItems(["seconds", "rounds"])
131 self.time = QSpinBox()
132 self.time.setMinimum(1)
133 self.time.setMaximum(1000)
134 self.time.setValue(10)
135
136 layout = QGridLayout()
137 self.setLayout(layout)
138
139 layout.addWidget(self.time, 0, 0)
140 layout.addWidget(self.unitcombo, 0, 1)
141
142 def getDeadline(self) -> Deadline:
143 if self.unitcombo.currentIndex() == 0: # time
144 return DeadlineTime(self.time.value() * 1000);
145 return DeadlineRounds(self.time.value(), self.DEFAULTDEADLINE)
146
147
148class Separator(QFrame):
149 '''
150 Equivalent of java's JSeparator: just a horizontal line for layout
151 '''
152
153 def __init__(self):
154 super().__init__()
155 self.setFrameShape(QFrame.Shape.HLine)
156 self.setFrameShadow(QFrame.Shadow.Sunken)
157
Note: See TracBrowser for help on using the repository browser.