Skip to content

Add a layer to a viewer with drag and drop #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions glue_jupyterlab/glue_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ def _init_ydoc(self) -> None:
ydoc=self._sessionYDoc,
)

def add_viewer_layer(self, tab_name: str, viewer_name: str, data_name: str) -> None:
"""Add a layer with new dataset to a viewer"""
viewer = (
self._viewers.get(tab_name, {}).get(viewer_name, {}).get("widget", None)
)
if not viewer:
return
data = self._data[data_name]
viewer.add_data(data)

def add_data(self, file_path: str) -> None:
"""Add a new data file to the session"""
relative_path = Path(file_path).relative_to(Path(self._path).parent)
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export namespace CommandIDs {
export const openControlPanel = 'glue-control:open-control-panel';

export const closeControlPanel = 'glue-control:close-control-panel';

export const addViewerLayer = 'glue-control:add-viewer-layer';
}

export interface INewViewerArgs {
Expand Down
17 changes: 17 additions & 0 deletions src/leftPanel/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ function addCommands(
controlModel.clearConfig();
}
});

commands.addCommand(CommandIDs.addViewerLayer, {
execute: async args => {
const kernel = controlModel.currentSessionKernel();
if (kernel === undefined) {
// TODO Show an error dialog
return;
}

const code = `
GLUE_SESSION.add_viewer_layer("${args.tab}", "${args.viewer}", "${args.data}")
`;

const future = kernel.requestExecute({ code }, false);
await future.done;
}
});
}

export const controlPanel: JupyterFrontEndPlugin<void> = {
Expand Down
36 changes: 33 additions & 3 deletions src/viewPanel/gridStackItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Panel, Widget } from '@lumino/widgets';
import { Toolbar, ToolbarButton, closeIcon } from '@jupyterlab/ui-components';
import { Message } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
import { DATASET_MIME } from '../types';

export class GridStackItem extends Panel {
constructor(options: GridStackItem.IOptions) {
Expand All @@ -10,12 +12,13 @@ export class GridStackItem extends Panel {
this.addClass('grid-stack-item');
this.addClass('glue-item');

const { cellIdentity, cell, itemTitle = '', pos, size } = options;
const { cellIdentity, cell, itemTitle = '', pos, size, tabName } = options;
this._cellOutput = cell;
this.cellIdentity = cellIdentity;
this._pos = pos;
this._size = size;
this._title = itemTitle;
this._tabName = tabName;

const content = new Panel();
content.addClass('grid-stack-item-content');
Expand Down Expand Up @@ -63,10 +66,35 @@ export class GridStackItem extends Panel {
this._size = value;
}

get tabName(): string {
return this._tabName;
}

set tabName(value: string) {
this._tabName = value;
}
get changed(): ISignal<GridStackItem, GridStackItem.IChange> {
return this._changed;
}

protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
this.node.addEventListener('drop', this._ondrop.bind(this));
}

protected onBeforeDetach(msg: Message): void {
this.node.removeEventListener('drop', this._ondrop.bind(this));
super.onBeforeDetach(msg);
}

private async _ondrop(event: DragEvent) {
const datasetId = event.dataTransfer?.getData(DATASET_MIME);
if (!datasetId) {
return;
}
this._changed.emit({ action: 'layer', dataLayer: datasetId });
}

private _createToolbar(itemTitle: string): Toolbar {
const toolbar = new Toolbar();
toolbar.node.addEventListener('click', () => {
Expand Down Expand Up @@ -97,7 +125,7 @@ export class GridStackItem extends Panel {
private _size: number[];
private _title: string;
private _cellOutput: Widget;

private _tabName: string;
private _changed: Signal<GridStackItem, GridStackItem.IChange>;
}

Expand All @@ -108,9 +136,11 @@ export namespace GridStackItem {
itemTitle?: string;
pos: number[];
size: number[];
tabName: string;
}

export interface IChange {
action: 'close' | 'lock' | 'edit';
action: 'close' | 'lock' | 'edit' | 'layer';
dataLayer?: string;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really satisfied by this but this was the easiest way to manage the change.
We could find something more generic since other futur changes may require arguments.

}
}
8 changes: 7 additions & 1 deletion src/viewPanel/sessionWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ export class SessionWidget extends BoxPanel {
}

private async _ondrop(event: DragEvent) {
const datasetId = event.dataTransfer?.getData(DATASET_MIME);
const target = event.target as HTMLElement;
const viewer = target.closest('.grid-stack-item.glue-item');
// No-op if the target is a viewer, it will be managed by the viewer itself.
if (viewer) {
return;
}

const datasetId = event.dataTransfer?.getData(DATASET_MIME);
const items: IDict<string> = {
Histogram: CommandIDs.new1DHistogram,
'1D Profile': CommandIDs.new1DProfile,
Expand Down
21 changes: 21 additions & 0 deletions src/viewPanel/tabLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ export class TabLayout extends Layout {
gridItem: item as any
});
}

/**
* Request to add a layer with a dataset to the viewer.
*
* @param item - the viewer where to add a layer.
* @param dataName - the data to add to the viewer.
*/
private _addViewerLayer(item: GridStackItem, dataName: string): void {
this._commands.execute(CommandIDs.addViewerLayer, {
tab: item.tabName,
viewer: item.cellIdentity,
data: dataName
});
}

/**
* Handle change-event messages sent to from gridstack.
*/
Expand Down Expand Up @@ -376,6 +391,12 @@ export class TabLayout extends Layout {
case 'edit':
this._handleEdit(sender);
break;
case 'layer':
if (!change.dataLayer) {
return;
}
this._addViewerLayer(sender, change.dataLayer);
break;
default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/viewPanel/tabView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export class TabView extends Widget {
cell: out,
itemTitle: itemTitle.match(/($[a-z])|[A-Z][^A-Z]+/g)?.join(' '),
pos: viewerData.pos,
size: viewerData.size
size: viewerData.size,
tabName: tabName
});
const cellOutput = item.cellOutput as SimplifiedOutputArea;
if (this._context) {
Expand Down