Skip to content

Commit 74e11e7

Browse files
committed
modify example hotkeys to use relative function file paths
Makes the basic example much more robust, and able to run without a specific cwd
1 parent 6672d0c commit 74e11e7

File tree

3 files changed

+59
-42
lines changed

3 files changed

+59
-42
lines changed

examples/basic_example.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
import os
43
import signal
4+
from pathlib import Path
55

66
from Qt import QtCore, QtWidgets
77

@@ -12,11 +12,12 @@
1212
NodesPaletteWidget
1313
)
1414

15-
# import example nodes from the "example_nodes" package
16-
from nodes import basic_nodes, custom_ports_node, group_node, widget_nodes
15+
# import example nodes from the "nodes" sub-package
16+
from examples.nodes import basic_nodes, custom_ports_node, group_node, widget_nodes
1717

18-
if __name__ == '__main__':
18+
BASE_PATH = Path(__file__).parent.resolve()
1919

20+
def main():
2021
# handle SIGINT to make the app terminate on CTRL+C
2122
signal.signal(signal.SIGINT, signal.SIG_DFL)
2223

@@ -26,7 +27,8 @@
2627
graph = NodeGraph()
2728

2829
# set up context menu for the node graph.
29-
graph.set_context_menu_from_file('../examples/hotkeys/hotkeys.json')
30+
hotkey_path = Path(BASE_PATH, 'hotkeys', 'hotkeys.json')
31+
graph.set_context_menu_from_file(hotkey_path, 'graph')
3032

3133
# registered example nodes.
3234
graph.register_nodes([
@@ -53,9 +55,7 @@
5355
# create node and set a custom icon.
5456
n_basic_b = graph.create_node(
5557
'nodes.basic.BasicNodeB', name='custom icon')
56-
n_basic_b.set_icon(
57-
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'star.png')
58-
)
58+
n_basic_b.set_icon(Path(BASE_PATH, 'star.png'))
5959

6060
# create node with the custom port shapes.
6161
n_custom_ports = graph.create_node(
@@ -140,3 +140,7 @@ def display_properties_bin(node):
140140
# nodes_palette.show()
141141

142142
app.exec()
143+
144+
145+
if __name__ == '__main__':
146+
main()

examples/hotkeys/hotkey_functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ def clear_session(graph):
9393
if graph.question_dialog('Clear Current Session?', 'Clear Session'):
9494
graph.clear_session()
9595

96+
def quit_qt(graph):
97+
"""
98+
Quit the Qt application.
99+
"""
100+
from qtpy import QtCore
101+
QtCore.QCoreApplication.quit()
96102

97103
def clear_undo(graph):
98104
"""

examples/hotkeys/hotkeys.json

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,44 @@
66
{
77
"type":"command",
88
"label":"Open...",
9-
"file":"../examples/hotkeys/hotkey_functions.py",
9+
"file":"./hotkey_functions.py",
1010
"function_name":"open_session",
1111
"shortcut":"QtGui.QKeySequence.Open"
1212
},
1313
{
1414
"type":"command",
1515
"label":"Import...",
16-
"file":"../examples/hotkeys/hotkey_functions.py",
16+
"file":"./hotkey_functions.py",
1717
"function_name":"import_session",
1818
"shortcut":""
1919
},
2020
{
2121
"type":"command",
2222
"label":"Clear Session...",
23-
"file":"../examples/hotkeys/hotkey_functions.py",
23+
"file":"./hotkey_functions.py",
2424
"function_name":"clear_session",
2525
"shortcut":""
2626
},
2727
{
2828
"type":"command",
2929
"label":"Save...",
30-
"file":"../examples/hotkeys/hotkey_functions.py",
30+
"file":"./hotkey_functions.py",
3131
"function_name":"save_session",
3232
"shortcut":"QtGui.QKeySequence.Save"
3333
},
3434
{
3535
"type":"command",
3636
"label":"Save As...",
37-
"file":"../examples/hotkeys/hotkey_functions.py",
37+
"file":"./hotkey_functions.py",
3838
"function_name":"save_session_as",
3939
"shortcut":"Ctrl+Shift+S"
40+
},
41+
{
42+
"type":"command",
43+
"label":"Exit",
44+
"file":"./hotkey_functions.py",
45+
"function_name":"quit_qt",
46+
"shortcut":"Ctrl+Shift+Q"
4047
}
4148
]
4249
},
@@ -47,14 +54,14 @@
4754
{
4855
"type":"command",
4956
"label":"Clear Undo History",
50-
"file":"../examples/hotkeys/hotkey_functions.py",
57+
"file":"./hotkey_functions.py",
5158
"function_name":"clear_undo",
5259
"shortcut":""
5360
},
5461
{
5562
"type":"command",
5663
"label":"Show Undo History",
57-
"file":"../examples/hotkeys/hotkey_functions.py",
64+
"file":"./hotkey_functions.py",
5865
"function_name":"show_undo_view",
5966
"shortcut":""
6067
},
@@ -64,28 +71,28 @@
6471
{
6572
"type":"command",
6673
"label":"Copy",
67-
"file":"../examples/hotkeys/hotkey_functions.py",
74+
"file":"./hotkey_functions.py",
6875
"function_name":"copy_nodes",
6976
"shortcut":"QtGui.QKeySequence.Copy"
7077
},
7178
{
7279
"type":"command",
7380
"label":"Cut",
74-
"file":"../examples/hotkeys/hotkey_functions.py",
81+
"file":"./hotkey_functions.py",
7582
"function_name":"cut_nodes",
7683
"shortcut":"QtGui.QKeySequence.Cut"
7784
},
7885
{
7986
"type":"command",
8087
"label":"Paste",
81-
"file":"../examples/hotkeys/hotkey_functions.py",
88+
"file":"./hotkey_functions.py",
8289
"function_name":"paste_nodes",
8390
"shortcut":"QtGui.QKeySequence.Paste"
8491
},
8592
{
8693
"type":"command",
8794
"label":"Delete",
88-
"file":"../examples/hotkeys/hotkey_functions.py",
95+
"file":"./hotkey_functions.py",
8996
"function_name":"delete_nodes",
9097
"shortcut":"QtGui.QKeySequence.Delete"
9198
},
@@ -95,55 +102,55 @@
95102
{
96103
"type":"command",
97104
"label":"Select All",
98-
"file":"../examples/hotkeys/hotkey_functions.py",
105+
"file":"./hotkey_functions.py",
99106
"function_name":"select_all_nodes",
100107
"shortcut":"Ctrl+A"
101108
},
102109
{
103110
"type":"command",
104111
"label":"Unselect All",
105-
"file":"../examples/hotkeys/hotkey_functions.py",
112+
"file":"./hotkey_functions.py",
106113
"function_name":"clear_node_selection",
107114
"shortcut":"Ctrl+Shift+A"
108115
},
109116
{
110117
"type":"command",
111118
"label":"Invert Selection",
112-
"file":"../examples/hotkeys/hotkey_functions.py",
119+
"file":"./hotkey_functions.py",
113120
"function_name":"invert_node_selection"
114121
},
115122
{
116123
"type":"command",
117124
"label":"Enable/Disable",
118-
"file":"../examples/hotkeys/hotkey_functions.py",
125+
"file":"./hotkey_functions.py",
119126
"function_name":"disable_nodes",
120127
"shortcut":"D"
121128
},
122129
{
123130
"type":"command",
124131
"label":"Duplicate",
125-
"file":"../examples/hotkeys/hotkey_functions.py",
132+
"file":"./hotkey_functions.py",
126133
"function_name":"duplicate_nodes",
127134
"shortcut":"Alt+C"
128135
},
129136
{
130137
"type":"command",
131138
"label":"Extract",
132-
"file":"../examples/hotkeys/hotkey_functions.py",
139+
"file":"./hotkey_functions.py",
133140
"function_name":"extract_nodes",
134141
"shortcut":"Ctrl+Shift+X"
135142
},
136143
{
137144
"type":"command",
138145
"label":"Clear Connections",
139-
"file":"../examples/hotkeys/hotkey_functions.py",
146+
"file":"./hotkey_functions.py",
140147
"function_name":"clear_node_connections",
141148
"shortcut":"Ctrl+D"
142149
},
143150
{
144151
"type":"command",
145152
"label":"Fit to Selection",
146-
"file":"../examples/hotkeys/hotkey_functions.py",
153+
"file":"./hotkey_functions.py",
147154
"function_name":"fit_to_selection",
148155
"shortcut":"F"
149156
},
@@ -153,21 +160,21 @@
153160
{
154161
"type":"command",
155162
"label":"Zoom In",
156-
"file":"../examples/hotkeys/hotkey_functions.py",
163+
"file":"./hotkey_functions.py",
157164
"function_name":"zoom_in",
158165
"shortcut":"="
159166
},
160167
{
161168
"type":"command",
162169
"label":"Zoom Out",
163-
"file":"../examples/hotkeys/hotkey_functions.py",
170+
"file":"./hotkey_functions.py",
164171
"function_name":"zoom_out",
165172
"shortcut":"-"
166173
},
167174
{
168175
"type":"command",
169176
"label":"Reset Zoom",
170-
"file":"../examples/hotkeys/hotkey_functions.py",
177+
"file":"./hotkey_functions.py",
171178
"function_name":"reset_zoom",
172179
"shortcut":"H"
173180
}
@@ -187,21 +194,21 @@
187194
{
188195
"type":"command",
189196
"label":"None",
190-
"file":"../examples/hotkeys/hotkey_functions.py",
197+
"file":"./hotkey_functions.py",
191198
"function_name":"bg_grid_none",
192199
"shortcut":"Alt+1"
193200
},
194201
{
195202
"type":"command",
196203
"label":"Lines",
197-
"file":"../examples/hotkeys/hotkey_functions.py",
204+
"file":"./hotkey_functions.py",
198205
"function_name":"bg_grid_lines",
199206
"shortcut":"Alt+2"
200207
},
201208
{
202209
"type":"command",
203210
"label":"Dots",
204-
"file":"../examples/hotkeys/hotkey_functions.py",
211+
"file":"./hotkey_functions.py",
205212
"function_name":"bg_grid_dots",
206213
"shortcut":"Alt+3"
207214
}
@@ -214,14 +221,14 @@
214221
{
215222
"type":"command",
216223
"label":"Horizontal",
217-
"file":"../examples/hotkeys/hotkey_functions.py",
224+
"file":"./hotkey_functions.py",
218225
"function_name":"layout_h_mode",
219226
"shortcut":"Shift+1"
220227
},
221228
{
222229
"type":"command",
223230
"label":"Vertical",
224-
"file":"../examples/hotkeys/hotkey_functions.py",
231+
"file":"./hotkey_functions.py",
225232
"function_name":"layout_v_mode",
226233
"shortcut":"Shift+2"
227234
}
@@ -236,7 +243,7 @@
236243
{
237244
"type":"command",
238245
"label":"Node Search",
239-
"file":"../examples/hotkeys/hotkey_functions.py",
246+
"file":"./hotkey_functions.py",
240247
"function_name":"toggle_node_search",
241248
"shortcut":"Tab"
242249
},
@@ -246,14 +253,14 @@
246253
{
247254
"type":"command",
248255
"label":"Auto Layout Up Stream",
249-
"file":"../examples/hotkeys/hotkey_functions.py",
256+
"file":"./hotkey_functions.py",
250257
"function_name":"layout_graph_up",
251258
"shortcut":"L"
252259
},
253260
{
254261
"type":"command",
255262
"label":"Auto Layout Down Stream",
256-
"file":"../examples/hotkeys/hotkey_functions.py",
263+
"file":"./hotkey_functions.py",
257264
"function_name":"layout_graph_down",
258265
"shortcut":"Ctrl+L"
259266
},
@@ -263,7 +270,7 @@
263270
{
264271
"type":"command",
265272
"label":"Expand Group Node",
266-
"file":"../examples/hotkeys/hotkey_functions.py",
273+
"file":"./hotkey_functions.py",
267274
"function_name":"expand_group_node",
268275
"shortcut":"Alt+Enter"
269276
}
@@ -276,21 +283,21 @@
276283
{
277284
"type":"command",
278285
"label":"Curved",
279-
"file":"../examples/hotkeys/hotkey_functions.py",
286+
"file":"./hotkey_functions.py",
280287
"function_name":"curved_pipe",
281288
"shortcut":"Ctrl+1"
282289
},
283290
{
284291
"type":"command",
285292
"label":"Straight",
286-
"file":"../examples/hotkeys/hotkey_functions.py",
293+
"file":"./hotkey_functions.py",
287294
"function_name":"straight_pipe",
288295
"shortcut":"Ctrl+2"
289296
},
290297
{
291298
"type":"command",
292299
"label":"Angle",
293-
"file":"../examples/hotkeys/hotkey_functions.py",
300+
"file":"./hotkey_functions.py",
294301
"function_name":"angle_pipe",
295302
"shortcut":"Ctrl+3"
296303
}

0 commit comments

Comments
 (0)