Skip to content

Commit a9f2980

Browse files
committed
context menu "Add to List": selection added to editor and shell
1 parent 31a31ac commit a9f2980

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

larray_editor/editor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,40 @@ def __init__(self, index, parent=None):
257257
layout.addWidget(self.advanced_button)
258258
self.setLayout(layout)
259259

260+
def show_context_menu(self, position):
261+
menu = QMenu(self)
262+
263+
# Only show the "Add to list" UI element if the current node does not have children
264+
index = self.tree.currentIndex()
265+
node = index.internalPointer()
266+
if not node.children:
267+
add_to_list_action = QAction("Add to list", self)
268+
add_to_list_action.triggered.connect(self.add_to_list)
269+
menu.addAction(add_to_list_action)
270+
271+
# Display the context menu at the position of the mouse click
272+
global_position = self.tree.viewport().mapToGlobal(position)
273+
menu.exec_(global_position)
274+
275+
276+
def add_to_list(self):
277+
from larray_eurostat import eurostat_get
278+
index = self.tree.currentIndex()
279+
node = index.internalPointer()
280+
281+
if not node.children: # should only work on leaf nodes
282+
# custom tree model where each node was represented by tuple (name, code,...) => data[1] = code
283+
code = self.tree.currentIndex().internalPointer().data[1]
284+
arr = eurostat_get(code, cache_dir='__array_cache__')
285+
286+
# Add indicator to the list
287+
editor = self.parent()
288+
new_data = editor.data.copy()
289+
new_data[code] = arr
290+
editor.update_mapping(new_data)
291+
editor.kernel.shell.user_ns[code] = arr
292+
293+
260294
def view_eurostat_indicator(self, index):
261295
from larray_eurostat import eurostat_get
262296

0 commit comments

Comments
 (0)