-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexturewindow.py
More file actions
266 lines (226 loc) · 10.3 KB
/
texturewindow.py
File metadata and controls
266 lines (226 loc) · 10.3 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
# Window to add / update texture to a wall
import os
from PySide6.QtCore import QThreadPool, Signal
from PySide6.QtWidgets import QMainWindow, QMessageBox
from PySide6.QtUiTools import QUiLoader
from lcconfig import LCConfig
from gconfig import GConfig
import copy
loader = QUiLoader()
basedir = os.path.dirname(__file__)
app_title = "Wall texture"
class TextureWindowUI(QMainWindow):
# Dictionary of lists
# First entry is the title - user friendly string
# Remaining are tuples with setting name, text to be displayed in the gui
# followed by default values
# 0 = "Texture name", 1 = "field 1" etc.
# field 1 & 2 are both 4 digits, 3 is 2 digit (etch)
# These are currently fixed maximum - but if need texture with additional then it will
# need an update to the ui as well as search for "len(self.labels)"
textures = {
"none": ["None"],
"brick": [
"Brick",
("brick_height", "Brick height", "65"),
("brick_width", "Brick width", "103"),
("brick_etch", "Brick etch", "10")
],
"wood": [
"Wood",
("wood_height", "Wood height", "150"),
("wood_width", "Wood width", "2000"),
("wood_etch", "Wood etch", "10")
],
"tile": [
"Tile",
("tile_height", "Tile height", "120"),
("tile_width", "Tile width", "300"),
("tile_etch", "Tile etch", "7")
]
}
def __init__(self, parent, config, gconfig, builder):
super().__init__()
# Connect signal handler
#self.load_complete_signal.connect(self.load_complete)
#self.name_warning_signal.connect(self.name_warning)
self.parent = parent
self.gui = parent # same as self.parent but helps keep consistancy for history
# Wall should be set when choosing edit properties (launching this window)
self.wall = None
# Texture may be None if no textures
# Otherwise holds the wall that is being edited
self.texture = None
self.ui = loader.load(os.path.join(basedir, "texturewindow.ui"), None)
self.ui.setWindowTitle(app_title)
self.config = config
self.gconfig = gconfig
self.builder = builder
# Set wall type pull down menu
for texture_key in self.textures.keys():
this_texture = self.textures[texture_key][0]
self.ui.textureCombo.addItem(this_texture)
# Set to 0 - None
# If already set then need to change
self.ui.textureCombo.setCurrentIndex(0)
self.ui.textureCombo.activated.connect(self.update_fields)
self.ui.buttonBox.rejected.connect(self.cancel)
self.ui.buttonBox.accepted.connect(self.accept)
# shortcuts to the input fields as list for easy index
# currently fixed number of fields - if change that then find
# anywhere that looks for len(self.labels)
self.labels = [self.ui.texture_label_1, self.ui.texture_label_2, self.ui.texture_label_3]
self.inputs = [self.ui.texture_input_1, self.ui.texture_input_2, self.ui.texture_input_3]
self.typicals = [self.ui.typical_label_1, self.ui.typical_label_2, self.ui.typical_label_3]
# Set validator for inputs, only allow digits (typically 4)
# Mask does not work - instead validate when OK pressed
#self.ui.texture_input_1.setInputMask("0000")
#self.ui.texture_input_2.setInputMask("0000")
#self.ui.texture_input_3.setInputMask("00")
self.texture_select ("none")
print ("Texture window setup")
# Give the texture type title (eg. Brick)
# Returns dictionary key (eg. brick)
def texture_to_key (self, texture):
for key in self.textures.keys():
if self.textures[key][0] == texture:
return key
return None
# Return index position in the textures
# used to get position of the pull-down from the name
def key_to_index (self, key):
i = 0
for this_key in self.textures.keys():
if this_key == key:
return i
i += 1
return -1
# Reset back to default state - called after cancel or OK
# so next time window opened it's back to defaults
def reset(self):
# Set to 0 None
self.ui.textureCombo.setCurrentIndex(0)
self.update_fields()
# Set fields to blank
self.ui.texture_input_1.setText("")
self.ui.texture_input_2.setText("")
self.ui.texture_input_3.setText("")
# Update field names if combo changed
def update_fields (self):
# Get the selected entry
selected = self.ui.textureCombo.currentText()
# Get the key
key = self.texture_to_key(selected)
self.texture_select(key)
# Update fields based on texture
def texture_select (self, key):
# get number of fields (excluding title)
num_fields = len(self.textures[key]) - 1
for i in range (0, len(self.labels)):
if i >= num_fields:
self.labels[i].setText("")
self.inputs[i].hide()
self.typicals[i].hide()
else:
self.labels[i].setText(self.textures[key][i+1][1])
self.typicals[i].setText(self.textures[key][i+1][2])
self.inputs[i].show()
self.typicals[i].show()
# Use when using the window to edit existing texture instead of new
# Note that can only edit one texture - but for future support pass textures list
def edit_properties (self, wall):
self.wall = wall
# clear any previous data
self.reset()
if wall.textures == None or len(wall.textures)<1:
self.texture = None
#print ("No textures")
self.ui.show()
return
# Only edit first texture
self.texture = wall.textures[0]
menu_pos = self.key_to_index(self.texture.style)
# This should not be the case but if style is not valid then
# current texture is corrupt so leave it empty
if menu_pos < 0:
return
self.ui.textureCombo.setCurrentIndex(menu_pos)
self.texture_select(self.texture.style)
# Set values based on texture
num_fields = len(self.textures[self.texture.style])-1
for i in range (0, num_fields):
this_key = self.textures[self.texture.style][i+1][0]
this_value = self.texture.get_setting_str(this_key)
self.inputs[i].setText(str(this_value))
self.ui.show()
# Show entire window - for add new window
def new(self):
self.textures = None
self.ui.show()
self.ui.activateWindow()
self.ui.raise_()
# hide entire windows
def hide(self):
self.ui.hide()
# Cancel button is pressed
def cancel(self):
self.reset()
self.hide()
# Accept button is pressed
# If we don't have an existing texture then this is new
# Otherwise we need to update the existing texture
def accept(self):
#print (f"Wall is {id(self.wall)}")
# Validate data
# Add to builder, then reset and hide window
# Validates entries
style = self.texture_to_key(self.ui.textureCombo.currentText())
# Iterate over the fields in the style
style_settings = {}
num_fields = len(self.textures[style]) - 1
for i in range (0, len(self.labels)):
if i >= num_fields:
break
# temp variable to shorten and simplify the code as used multiple times
# [i+1] skips the first entry which is title of the texture style
this_texture_setting = self.textures[style][i+1]
text_value = self.inputs[i].text()
# First check for an empty string - which is treated as 0,
# Otherwise needs to be a number
if text_value == "":
num_value = 0
else:
try:
num_value = int(text_value)
except ValueError:
QMessageBox.warning(self, f"{this_texture_setting[1]} is not a valid number", f"{this_texture_setting[1]} is not a valid number. Please provide a valid size in mm.")
return
# Simple check to test for a valid number - only a very basic check that don't end up with less than 0 or a ridiculously large number
# 0 would count as default if appropriate
if num_value < 0 or num_value > 100000:
# Message is slightly different from if not a number
QMessageBox.warning(self, f"{this_texture_setting[1]} value is not valid", f"{this_texture_setting[1]} value is not valid. Please provide a valid size in mm.")
return
style_settings[this_texture_setting[0]] = num_value
new_params = {"wall": self.wall, "fullwall": True, "points": [], "style": style, "settings": copy.copy(style_settings)}
# If existing texture then update
if self.texture != None:
# Store old settings for history
old_params = {"wall": self.wall, "fullwall": self.texture.fullwall, "points": self.texture.points, "style": self.texture.style, "settings": copy.copy(self.texture.settings)}
self.texture.change_texture(style, style_settings)
new_params['texture'] = self.texture
self.gui.history.add (f"Change texture for {self.wall.name}", "Change texture", old_params, new_params)
else:
# otherwise this is a new texture
old_params = None
# Create new (note [] denotes full wall - which is only option at the moment
this_texture = self.wall.add_texture(style, [], style_settings)
new_params['texture'] = this_texture
self.gui.history.add (f"Add texture for {self.wall.name}", "Change texture", old_params, new_params)
# Note need to update edit view as well
self.wall.update_etches()
# Update parent
self.parent.update_all_views()
# Reset the window and hide
self.reset()
self.hide()