-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.py
272 lines (219 loc) · 9.09 KB
/
operators.py
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
import bpy
import os
from bpy.types import Panel, UIList
from bpy.props import IntProperty, CollectionProperty #, StringProperty
from .func import *
###--- UI List items
# ui list item actions
class Uilist_actions(bpy.types.Operator):
bl_idname = "sniptool.list_action"
bl_label = "List Action"
action = bpy.props.EnumProperty(
items=(
('UP', "Up", ""),
('DOWN', "Down", ""),
('REMOVE', "Remove", ""),
('ADD', "Add", ""),
)
)
def invoke(self, context, event):
scn = context.scene
idx = scn.sniptool_index
try:
item = scn.sniptool[idx]
except IndexError:
pass
else:
if self.action == 'DOWN' and idx < len(scn.sniptool) - 1:
item_next = scn.sniptool[idx+1].name
scn.sniptool_index += 1
info = 'Item %d selected' % (scn.sniptool_index + 1)
self.report({'INFO'}, info)
elif self.action == 'UP' and idx >= 1:
item_prev = scn.sniptool[idx-1].name
scn.sniptool_index -= 1
info = 'Item %d selected' % (scn.sniptool_index + 1)
self.report({'INFO'}, info)
elif self.action == 'REMOVE':
info = 'Item %s removed from list' % (scn.sniptool[scn.sniptool_index].name)
scn.sniptool_index -= 1
self.report({'INFO'}, info)
scn.sniptool.remove(idx)
if self.action == 'ADD':
###---mypart
snipname = clipit(context)
if snipname:
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = os.path.splitext(snipname)[0]
scn.sniptool_index = (len(scn.sniptool)-1)
info = '%s added to list' % (item.name)
self.report({'INFO'}, info)
else:
self.report({'warning'}, 'nothing selected')
return {"FINISHED"}
# -------------------------------------------------------------------
# draw
# -------------------------------------------------------------------
# sniptool list
class UL_items(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
#split = layout.split(0.3)
##add to draw index (useless)
#split.label("%d" % (index))
#split.prop(item, "name", text="", emboss=False, translate=False, icon='WORDWRAP_ON')
##delete icon to remove sheets icons (alsouseless)
layout.prop(item, "name", text="", emboss=False, translate=False, icon='WORDWRAP_ON')
def invoke(self, context, event):
pass
# draw the panel
class UIListPanelExample(Panel):
"""Creates a Panel in the Object properties window"""
bl_idname = 'OBJECT_PT_my_panel'
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
bl_label = "Snippets List"
bpy.types.Scene.new_snippets_name = bpy.props.StringProperty(description='name that snippets will take, name will be generated')
def draw(self, context):
layout = self.layout
scn = bpy.context.scene
rows = 2
row = layout.row()
row.operator("sniptool.reload_list", icon="FILE_REFRESH")
row = layout.row()
row.operator("sniptool.template_insert", icon="LIBRARY_DATA_DIRECT")#LIBRARY_DATA_DIRECT RIGHTARROW FORWARD
row = layout.row()
row.template_list("UL_items", "", scn, "sniptool", scn, "sniptool_index", rows=rows)
col = row.column(align=True)
# col.operator("sniptool.list_action", icon='ZOOMIN', text="").action = 'ADD'
# col.operator("sniptool.list_action", icon='ZOOMOUT', text="").action = 'REMOVE'
col.separator()
col.operator("sniptool.list_action", icon='TRIA_UP', text="").action = 'UP'
col.operator("sniptool.list_action", icon='TRIA_DOWN', text="").action = 'DOWN'
row = layout.row()
col = row.column(align=True)
col.separator()
col.prop(context.scene, 'new_snippets_name', text='snippets name')
col.operator("sniptool.save_snippet", icon="SAVE_COPY")
col.operator("sniptool.open_snippet_folder", icon="FILE_FOLDER")
# col.separator()
class Uilist_saveSnippet(bpy.types.Operator):
bl_idname = "sniptool.save_snippet"
bl_label = "save snippet"
bl_description = "save selection to a file named after this"
def execute(self, context):
scn = context.scene
library = locateLibrary()
if library:
snipname = clipit(context)
if snipname:
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = os.path.splitext(snipname)[0]
scn.sniptool_index = (len(scn.sniptool)-1)
info = '%s added to list' % (item.name)
self.report({'INFO'}, info)
else:
self.report({'warning'}, 'nothing selected')
###print all snipsauce :
#for i in scn.sniptool:
# print (i.name, i.id)
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# insert button
class Uilist_insertTemplate(bpy.types.Operator):
bl_idname = "sniptool.template_insert"
bl_label = "insert List Item"
bl_description = "insert Item in textBlock"
def execute(self, context):
scn = context.scene
if locateLibrary():
text = getattr(bpy.context.space_data, "text", None)
if text:
pass
#print(text.name)
else:
pass
#print('no text data')
#context override for the ops.text.insert() function
override = {'window': context.window,
'area' : context.area,
'region': context.region,
'space': context.space_data,
'edit_text' : text
}
snip = scn.sniptool[scn.sniptool_index].name
Loaded_text = load_text(get_snippet(snip))
#get character position in text
charPos = text.current_character
#print ('charPos', charPos)
#indentedText = Loaded_text
if Loaded_text:
FormattedText = Loaded_text
#indent text lines exept first (already at cursor position)
if charPos > 0:
textLines = Loaded_text.split('\n')
if not len(textLines) == 1:
#print("indent subsequent lines")
indentedLines = []
indentedLines.append(textLines[0])
for line in textLines[1:]:
indentedLines.append(' '*charPos + line)
FormattedText = '\n'.join(indentedLines)
# print(FormattedText)
insert_template(override, FormattedText)
else:
print('Fail to load snippet !')
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# relaod button
class Uilist_reloadItems(bpy.types.Operator):
bl_idname = "sniptool.reload_list"
bl_label = "Reload List"
bl_description = "Reload all items in the list"
def execute(self, context):
scn = context.scene
lst = scn.sniptool
current_index = scn.sniptool_index
library = locateLibrary()
if library:
allsnip = reload_folder(library)
if len(lst) > 0:#remove all item in list
# reverse range to remove last item first
for i in range(len(lst)-1,-1,-1):
scn.sniptool.remove(i)
#self.report({'INFO'}, "All items removed")
for snipname in allsnip:#populate list
item = scn.sniptool.add()
item.id = len(scn.sniptool)
item.name = snipname
scn.sniptool_index = (len(scn.sniptool)-1)
#info = '%s added to list' % (item.name)
# else:
# self.report({'INFO'}, "Nothing to add")
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
class OpenSnippetsFolder(bpy.types.Operator):
bl_idname = "sniptool.open_snippet_folder"
bl_label = "open library folder"
bl_description = "open snippets folder location"
def execute(self, context):
scn = context.scene
library = locateLibrary()
if library:
#open folder
openFolder(library)
else:
pathErrorMsg = locateLibrary(True) + ' not found or inaccessible'
self.report({'ERROR'}, pathErrorMsg)
return{'FINISHED'}
# Create sniptool property group
class sniptoolProp(bpy.types.PropertyGroup):
'''name = StringProperty() '''
id = IntProperty()