-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbalanced.py
More file actions
executable file
·382 lines (325 loc) · 13.6 KB
/
balanced.py
File metadata and controls
executable file
·382 lines (325 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
# vim: set expandtab tabstop=4 shiftwidth=4:
# Copyright (c) 2018, 2022, CJ Kucera
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the development team nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL CJ KUCERA BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Line(object):
def __init__(self, parent, container, index, top, initial_weight=None):
self.parent = parent
self.container = container
self.index = index
self.rescaling = False
# Checkbox to lock
# (not going to do this at the moment -- I think it's a bit superfluous)
#self.lockbox = QtWidgets.QCheckBox('Lock', parent)
#self.lockbox.stateChanged.connect(self.lock_switched)
#self.container.addWidget(self.lockbox, self.index, 0)
# Label
self.description = QtWidgets.QLineEdit('Description', parent)
self.description.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.container.addWidget(self.description, self.index, 0)
# Main Slider
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal, parent)
self.slider.setMinimum(0)
self.slider.setMaximum(int(top*100))
if initial_weight is None:
self.slider.setValue(int(top*100))
else:
self.slider.setValue(int(initial_weight*100))
self.slider.valueChanged.connect(self.slider_changed)
self.container.addWidget(self.slider, self.index, 1)
# Textbox for setting the weight
self.weightbox = QtWidgets.QLineEdit('', parent)
self.weightbox.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.weightbox.setMaximumWidth(50)
if initial_weight is not None:
self.weightbox.setText(str(initial_weight))
if self.parent.restrict_target():
self.disable_weightbox()
self.weightbox.editingFinished.connect(self.weight_edited)
self.container.addWidget(self.weightbox, self.index, 2)
# Perecentage display label
self.percentlabel = QtWidgets.QLabel('', parent)
self.percentlabel.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
self.percentlabel.setMinimumWidth(40)
self.percentlabel.setMaximumWidth(40)
self.container.addWidget(self.percentlabel, self.index, 3)
# Remove button
self.remove_button = QtWidgets.QPushButton('Remove', parent)
self.remove_button.clicked.connect(self.remove)
self.container.addWidget(self.remove_button, self.index, 4)
def remove(self):
if len(self.parent.lines) > 1:
for widget in [
self.remove_button,
self.slider,
self.weightbox,
self.percentlabel,
self.description,
#self.lockbox,
]:
self.container.removeWidget(widget)
widget.deleteLater()
widget = None
self.parent.removed_line(self)
#def lock_switched(self):
# if self.lockbox.isChecked():
# self.slider.setDisabled(True)
# self.weightbox.setDisabled(True)
# else:
# self.slider.setDisabled(False)
# self.weightbox.setDisabled(False)
def enable_remove(self):
self.remove_button.setDisabled(False)
def disable_remove(self):
self.remove_button.setDisabled(True)
def enable_weightbox(self):
self.weightbox.setReadOnly(False)
def disable_weightbox(self):
self.weightbox.setReadOnly(True)
def slider_changed(self, value):
if not self.rescaling:
self.parent.update_weights()
def value(self):
return self.slider.value()/100
def weight(self):
try:
return float(self.weightbox.text())
except ValueError:
return 100
def set_weight(self, weight):
if weight == 0:
self.weightbox.setText('0')
elif weight < 1:
self.weightbox.setText('{}'.format(round(weight, 2)))
elif weight < 10:
self.weightbox.setText('{}'.format(round(weight, 1)))
else:
self.weightbox.setText('{}'.format(round(weight)))
def set_percent(self, percent):
percent *= 100
if percent == 0:
self.percentlabel.setText('0%')
elif percent < 1:
self.percentlabel.setText('{}%'.format(round(percent, 2)))
elif percent < 10:
self.percentlabel.setText('{}%'.format(round(percent, 1)))
else:
self.percentlabel.setText('{}%'.format(round(percent)))
def set_new_max(self, new_top):
self.rescaling = True
self.slider.setMaximum(int(new_top*100))
self.rescaling = False
def rescale_to_new_absolute(self, new_top, value):
self.rescaling = True
self.slider.setMaximum(int(new_top*100))
self.slider.setValue(int(value*100))
self.rescaling = False
def rescale_to_new_max(self, new_top, percent):
self.rescaling = True
self.slider.setMaximum(int(new_top*100))
self.slider.setValue(int(new_top*100*percent))
self.rescaling = False
def weight_edited(self):
try:
new_value = float(self.weightbox.text())
except ValueError:
return
new_value *= 100
if new_value < 0:
new_value = 0
elif new_value > self.slider.maximum():
new_value = self.slider.maximum()
self.slider.setValue(int(new_value))
class Gui(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.lines = []
self.initUI()
def initUI(self):
# Basic UI
self.setMinimumSize(500, 400)
self.setWindowTitle('Balanced Weight Generator')
# Menu
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction('&Quit', self.close, 'Ctrl+Q')
# Set up a main vbox to use
layout = QtWidgets.QVBoxLayout()
vbox = QtWidgets.QWidget()
vbox.setLayout(layout)
self.setCentralWidget(vbox)
# Frame for our top controls
frame = QtWidgets.QFrame()
frame.setFrameShadow(frame.Raised)
frame.setFrameShape(frame.Panel)
frame.setLineWidth(2)
layout.addWidget(frame, 0, QtCore.Qt.AlignTop)
# Set up an hbox for the top row
hlayout = QtWidgets.QHBoxLayout()
frame.setLayout(hlayout)
# Checkbox for if we're locking to target
self.target_box = QtWidgets.QCheckBox('Target Weight:', self)
self.target_box.setChecked(False)
self.target_box.stateChanged.connect(self.target_weight_toggled)
hlayout.addWidget(self.target_box)
# Input for our Target Weight
self.target_weight = QtWidgets.QLineEdit('100', self)
self.target_weight.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.target_weight.setDisabled(True)
self.target_weight.setMinimumWidth(50)
self.target_weight.setMaximumWidth(50)
self.target_weight.textEdited.connect(self.target_updated)
hlayout.addWidget(self.target_weight)
# Spacer
spacer = QtWidgets.QLabel()
spacer.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
hlayout.addWidget(spacer)
# Add Item button
self.add_item_button = QtWidgets.QPushButton('Add Item', self)
self.add_item_button.clicked.connect(self.add_line)
hlayout.addWidget(self.add_item_button)
# Now a separate GridLayout to hold our items
self.itembox = QtWidgets.QGridLayout()
widget = QtWidgets.QWidget()
widget.setLayout(self.itembox)
layout.addWidget(widget, 0, QtCore.Qt.AlignTop)
# Spacer at the bottom
spacer_bottom = QtWidgets.QLabel()
spacer_bottom.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
layout.addWidget(spacer_bottom)
# Add a single line to start with
self.add_line()
# Show ourselves
self.show()
def target(self):
try:
return float(self.target_weight.text())
except ValueError:
return 100
def get_max_and_top_value(self):
"""
Returns a tuple...
"""
if self.restrict_target():
return (None, 10000)
else:
if len(self.lines) > 0:
max_weight = max([line.value() for line in self.lines])
else:
max_weight = 100
return (max_weight, max_weight*2)
def add_line(self):
(max_weight, top) = self.get_max_and_top_value()
if self.restrict_target():
initial_weight = None
else:
initial_weight = max_weight
for line in self.lines:
line.set_new_max(top)
line = Line(self, self.itembox, self.itembox.rowCount(), top, initial_weight=initial_weight)
self.lines.append(line)
self.update_remove_buttons()
self.update_weights()
def removed_line(self, line):
self.lines.remove(line)
self.update_remove_buttons()
if not self.restrict_target():
(max_weight, top) = self.get_max_and_top_value()
for line in self.lines:
line.set_new_max(top)
self.update_weights()
def update_remove_buttons(self):
if len(self.lines) < 2:
for line in self.lines:
line.disable_remove()
else:
for line in self.lines:
line.enable_remove()
def update_weights(self):
# First calculate percentages, which works the same whether we're
# limiting our total weight or not
total = sum([line.value() for line in self.lines])
if total > 0:
percents = [line.value()/total for line in self.lines]
else:
percents = [0 for line in self.lines]
for (line, percent) in zip(self.lines, percents):
line.set_percent(percent)
# Now see if we do anything with the weights. If we're restricting
# the total weight, all our weights might change. Otherwise, just
# keep 'em the way they are.
if self.restrict_target():
target = self.target()
weights = [p * target for p in percents]
for (line, weight) in zip(self.lines, weights):
line.set_weight(weight)
else:
for line in self.lines:
line.set_weight(line.value())
# Also update our locked weight box
if total == 0:
self.target_weight.setText('100')
elif total < 1:
self.target_weight.setText('{}'.format(round(total, 2)))
elif total < 10:
self.target_weight.setText('{}'.format(round(total, 1)))
else:
self.target_weight.setText('{}'.format(round(total)))
def target_weight_toggled(self):
if self.restrict_target():
for line in self.lines:
line.disable_weightbox()
self.target_weight.setDisabled(False)
total = sum([line.value() for line in self.lines])
if total > 0:
percents = [line.value()/total for line in self.lines]
else:
percents = [0 for line in self.lines]
(max_weight, top) = self.get_max_and_top_value()
for (line, percent) in zip(self.lines, percents):
line.rescale_to_new_max(top, percent)
self.update_weights()
else:
for line in self.lines:
line.enable_weightbox()
self.target_weight.setDisabled(True)
real_max = max([line.weight() for line in self.lines])
if real_max == 0:
real_max = 100
for line in self.lines:
line.rescale_to_new_absolute(real_max*2, line.weight())
def restrict_target(self):
return self.target_box.isChecked()
def target_updated(self):
self.update_weights()
def main():
app = QtWidgets.QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())
if __name__ == '__main__':
main()