Skip to content

Commit 6eaae80

Browse files
committed
#484 fixed randomization color, selection through control panel, selection in general
1 parent 0ae227f commit 6eaae80

6 files changed

Lines changed: 85 additions & 65 deletions

File tree

webapp/components/general/ControlPanelTreeItem.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Shuffle from '@material-ui/icons/Shuffle';
1212
import { ChromePicker } from 'react-color';
1313
import { useDispatch, useSelector } from 'react-redux';
1414
import { experimentLabelColor } from '../../theme';
15-
import { changeInstanceColor } from '../../redux/actions/general';
15+
import { changeInstanceColor, selectInstances } from '../../redux/actions/general';
1616

1717
const useStyles = makeStyles((theme) => ({
1818
networkItem: {
@@ -62,26 +62,39 @@ const ControlPanelTreeItem = (props) => {
6262
setColor(_color.rgb);
6363
};
6464

65+
const getRandomColor = () => ({
66+
r: parseFloat((Math.random() * 255).toFixed(2)),
67+
g: parseFloat((Math.random() * 255).toFixed(2)),
68+
b: parseFloat((Math.random() * 255).toFixed(2)),
69+
a: 1,
70+
});
71+
6572
const generateRandomColor = (event, nodeId) => {
66-
const newInstances = instances.filter((instance) => !(instance.instancePath.startsWith(nodeId)));
67-
const randomColor = {
68-
r: parseFloat((Math.random() * 255).toFixed(2)),
69-
g: parseFloat((Math.random() * 255).toFixed(2)),
70-
b: parseFloat((Math.random() * 255).toFixed(2)),
71-
a: 1,
72-
};
73+
const children = window.Instances.getInstance(nodeId).getChildren().map((instance) => instance.getInstancePath());
74+
// const newInstances = instances.filter((instance) => !(instance.instancePath.startsWith(nodeId)));
75+
const newInstances = instances.filter((instance) => {
76+
let condition = true;
77+
children.forEach((child) => {
78+
if (instance.instancePath.startsWith(child)) {
79+
condition = false;
80+
}
81+
});
82+
return condition;
83+
});
7384

74-
newInstances.push({
75-
instancePath: nodeId,
76-
color: {
77-
r: randomColor.r / 255,
78-
g: randomColor.g / 255,
79-
b: randomColor.b / 255,
80-
a: randomColor.a,
81-
},
85+
children.forEach((child) => {
86+
const randomColor = getRandomColor();
87+
newInstances.push({
88+
instancePath: child,
89+
color: {
90+
r: randomColor.r / 255,
91+
g: randomColor.g / 255,
92+
b: randomColor.b / 255,
93+
a: randomColor.a,
94+
},
95+
});
8296
});
8397
dispatch(changeInstanceColor(newInstances));
84-
setColor(randomColor);
8598
};
8699

87100
const changeVisibility = (event, nodeId) => {
@@ -126,6 +139,7 @@ const ControlPanelTreeItem = (props) => {
126139
onNodeSelect,
127140
onVisibilityClick,
128141
children,
142+
disableRandom,
129143
...other
130144
} = props;
131145

@@ -153,7 +167,7 @@ const ControlPanelTreeItem = (props) => {
153167
<IconButton onClick={(event) => changeVisibility(event, nodeId)}>
154168
{ visibility ? <Visibility /> : <VisibilityOff /> }
155169
</IconButton>
156-
<IconButton onClick={(event) => generateRandomColor(event, nodeId)}><Shuffle /></IconButton>
170+
<IconButton disabled={disableRandom} onClick={(event) => generateRandomColor(event, nodeId)}><Shuffle /></IconButton>
157171
<IconButton onClick={() => setShowColorPicker(true)}><ColorLens /></IconButton>
158172
{
159173
showColorPicker

webapp/components/general/ExperimentControlPanel.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/* eslint-disable no-nested-ternary */
22
import * as React from 'react';
3+
import { useDispatch, useSelector } from 'react-redux';
34
import { makeStyles } from '@material-ui/core/styles';
45
import Box from '@material-ui/core/Box';
56
import Typography from '@material-ui/core/Typography';
67
import TextField from '@material-ui/core/TextField';
78
import TreeView from '@material-ui/lab/TreeView';
8-
import TreeItem from '@material-ui/lab/TreeItem';
99
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
1010
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
1111
import ControlPanelTreeItem from './ControlPanelTreeItem';
1212
import { experimentLabelColor } from '../../theme';
13-
14-
import { MODEL_STATE } from '../../constants';
13+
import { selectInstances } from '../../redux/actions/general';
1514

1615
const useStyles = makeStyles(() => ({
1716
header: {
@@ -24,9 +23,11 @@ const useStyles = makeStyles(() => ({
2423

2524
const ExperimentControlPanel = (props) => {
2625
const classes = useStyles();
26+
const dispatch = useDispatch();
27+
const instances = useSelector((state) => state.general.instances);
2728
const [filter, setFilter] = React.useState('');
2829
const onNodeSelect = (nodeId) => {
29-
console.log(`Node with id ${nodeId} clicked`);
30+
dispatch(selectInstances(instances, [nodeId]));
3031
};
3132
const instancesMap = new Map();
3233

@@ -71,7 +72,7 @@ const ExperimentControlPanel = (props) => {
7172
};
7273

7374
const getTreeItemsFromData = (treeItems) => treeItems.map((treeItemData) => {
74-
let children;
75+
let children = [];
7576
if (treeItemData.getChildren() && treeItemData.getChildren().length > 0) {
7677
children = getTreeItemsFromData(treeItemData.getChildren());
7778
}
@@ -84,6 +85,7 @@ const ExperimentControlPanel = (props) => {
8485
type={treeItemData.getType().getId()}
8586
onNodeSelect={onNodeSelect}
8687
onVisibilityClick={onVisibilityClick}
88+
disableRandom={children.length === 0}
8789
>
8890
{children}
8991
</ControlPanelTreeItem>
@@ -110,12 +112,9 @@ const ExperimentControlPanel = (props) => {
110112
defaultCollapseIcon={<ExpandMoreIcon />}
111113
defaultExpandIcon={<ChevronRightIcon />}
112114
>
113-
<TreeItem nodeId="network" label="network_netpyne">
114-
{filter === ''
115-
? getTreeItemsFromData(window.Instances.network.getChildren())
116-
: getFlatFilteredList(window.Instances.network.getChildren())
117-
}
118-
</TreeItem>
115+
{filter === ''
116+
? getTreeItemsFromData([window.Instances.getInstance('network')])
117+
: getFlatFilteredList([window.Instances.getInstance('network')])}
119118
</TreeView>
120119
</Box>
121120
)

webapp/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { openBackendErrorDialog, closeBackendErrorDialog } from '../redux/action
1616
import {
1717
updateCards, editModel, simulateNetwork, createNetwork, closeDialog,
1818
createAndSimulateNetwork, showNetwork, pythonCall, modelLoaded, deleteNetParamsObj, resetModel,
19-
setDefaultWidgets, changeInstanceColor, openConfirmationDialog, closeConfirmationDialog,
19+
setDefaultWidgets, changeInstanceColor, openConfirmationDialog, closeConfirmationDialog, selectInstances,
2020
} from '../redux/actions/general';
2121

2222
import {
@@ -250,7 +250,7 @@ export const NetPyNEInstantiated = connect(
250250
data: state.general.instances,
251251
}),
252252
(dispatch) => ({
253-
selectInstances: (instances) => dispatch(changeInstanceColor(instances)),
253+
selectInstances: (instances, selectedInstances) => dispatch(selectInstances(instances, selectedInstances)),
254254
}),
255255
)(_NetPyNEInstantiated);
256256

webapp/components/instantiation/NetPyNEInstantiated.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ class NetPyNEInstantiated extends React.Component {
5050
this.canvasRef = React.createRef();
5151

5252
this.onSelection = this.onSelection.bind(this);
53-
this.applySelection = this.applySelection.bind(this);
5453
this.mapToCanvasData = this.mapToCanvasData.bind(this);
5554
}
5655

5756
onSelection (selectedInstances) {
5857
const { selectInstances, data } = this.props;
59-
selectInstances(this.applySelection(data, selectedInstances));
58+
selectInstances(data, selectedInstances);
6059
}
6160

6261
updateBtnsWithTheme = (removeClass, addClass) => {
@@ -78,38 +77,6 @@ class NetPyNEInstantiated extends React.Component {
7877
));
7978
}
8079

81-
applySelection (data, selectedInstances) {
82-
const smap = new Map(selectedInstances.map((i) => [i, true]));
83-
const newData = data.map((item) => {
84-
if (smap.get(item.instancePath)) {
85-
return {
86-
...item,
87-
selected: !item.selected,
88-
};
89-
}
90-
return { ...item };
91-
});
92-
const dmap = new Map(newData.map((i) => [i.instancePath, true]));
93-
94-
smap.forEach((value, key) => {
95-
const item = dmap.get(key);
96-
if (!item) {
97-
newData.push({
98-
instancePath: key,
99-
color: undefined,
100-
selected: true,
101-
});
102-
}
103-
});
104-
const canvasData = newData.filter((item) => {
105-
if ((item?.selected !== undefined && item?.selected === false) && item?.color === undefined) {
106-
return false;
107-
}
108-
return true;
109-
});
110-
return canvasData;
111-
}
112-
11380
render () {
11481
const { cameraOptions } = this.state;
11582
const { data } = this.props;

webapp/redux/actions/general.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const SET_THEME = 'SET_THEME';
2525
export const ADD_CANVAS_INSTANCES = 'ADD_CANVAS_INSTANCES';
2626
export const CHANGE_INSTANCE_COLOR = 'CHANGE_INSTANCE_COLOR';
2727
export const REMOVE_CANVAS_INSTANCES = 'REMOVE_CANVAS_INSTANCES';
28+
export const SELECT_INSTANCE = 'SELECT_INSTANCE';
2829

2930
// Actions
3031
export const updateCards = { type: UPDATE_CARDS };
@@ -112,3 +113,11 @@ export const removeInstancesFromCanvas = (instances) => ({
112113
type: REMOVE_CANVAS_INSTANCES,
113114
instances,
114115
});
116+
117+
export const selectInstances = (instance, selectedInstances) => ({
118+
type: SELECT_INSTANCE,
119+
data: {
120+
instance,
121+
selectedInstances,
122+
},
123+
});

webapp/redux/reducers/general.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,33 @@ export const GENERAL_DEFAULT_STATE = {
2121
instances: [],
2222
};
2323

24+
const applySelection = (data, selectedInstances) => {
25+
const smap = new Map(selectedInstances.map((i) => [i, true]));
26+
const newData = data.map((item) => ({
27+
...item,
28+
selected: false,
29+
}));
30+
const dmap = new Map(newData.map((i) => [i.instancePath, true]));
31+
32+
smap.forEach((value, key) => {
33+
const item = dmap.get(key);
34+
if (!item) {
35+
newData.push({
36+
instancePath: key,
37+
color: undefined,
38+
selected: true,
39+
});
40+
}
41+
});
42+
const canvasData = newData.filter((item) => {
43+
if ((item?.selected !== undefined && item?.selected === false) && item?.color === undefined) {
44+
return false;
45+
}
46+
return true;
47+
});
48+
return canvasData;
49+
};
50+
2451
// reducer function
2552
export default function reduceGeneral (state = GENERAL_DEFAULT_STATE, action) {
2653
switch (action.type) {
@@ -76,6 +103,10 @@ export default function reduceGeneral (state = GENERAL_DEFAULT_STATE, action) {
76103
case Actions.REMOVE_CANVAS_INSTANCES: {
77104
return { ...state };
78105
}
106+
case Actions.SELECT_INSTANCE: {
107+
const newData = applySelection(action.data.instance, action.data.selectedInstances);
108+
return { ...state, instances: [...newData] };
109+
}
79110
default: {
80111
return state;
81112
}

0 commit comments

Comments
 (0)