source.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { HierarchyRef, ModelRef, TrajectoryRef } from '../../mol-plugin-state/manager/structure/hierarchy-state';
  8. import { CollapsableControls, CollapsableState } from '../base';
  9. import { ActionMenu } from '../controls/action-menu';
  10. import { IconButton } from '../controls/common';
  11. import { ParameterControls } from '../controls/parameters';
  12. import { PluginCommands } from '../../mol-plugin/commands';
  13. import { StateTransforms } from '../../mol-plugin-state/transforms';
  14. import { memoize1 } from '../../mol-util/memoize';
  15. import { StructureHierarchyManager } from '../../mol-plugin-state/manager/structure/hierarchy';
  16. interface StructureSourceControlState extends CollapsableState {
  17. isBusy: boolean,
  18. show?: 'hierarchy' | 'actions'
  19. }
  20. export class StructureSourceControls extends CollapsableControls<{}, StructureSourceControlState> {
  21. protected defaultState(): StructureSourceControlState {
  22. return {
  23. header: 'Structure',
  24. isCollapsed: false,
  25. isBusy: false
  26. };
  27. }
  28. componentDidMount() {
  29. this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.selection, () => this.forceUpdate());
  30. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  31. this.setState({ isBusy: v })
  32. });
  33. }
  34. private item = (ref: HierarchyRef) => {
  35. const selected = this.plugin.managers.structure.hierarchy.seletionSet;
  36. let label;
  37. switch (ref.kind) {
  38. case 'model': {
  39. const model = ref.cell.obj?.data;
  40. if (model?.trajectoryInfo.size! > 1) {
  41. label = `${ref.cell.obj?.data.entryId} | Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size}`;
  42. }
  43. label = `${ref.cell.obj?.data.entryId} | ${ref.cell.obj?.label}`; break;
  44. }
  45. case 'structure': {
  46. const model = ref.cell.obj?.data.models[0];
  47. if (model?.trajectoryInfo.size! > 1) {
  48. label = `${ref.cell.obj?.data.models[0].entryId} | ${ref.cell.obj?.label} (Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size})`; break;
  49. } else {
  50. label = `${ref.cell.obj?.data.models[0].entryId} | ${ref.cell.obj?.label}`; break;
  51. }
  52. }
  53. default: label = ref.cell.obj?.label; break;
  54. }
  55. const item: ActionMenu.Item = { kind: 'item', label: label || ref.kind, selected: selected.has(ref.cell.transform.ref), value: [ref] };
  56. return item;
  57. }
  58. getTrajectoryItems = (t: TrajectoryRef): ActionMenu.Items => {
  59. if (t.models.length === 0) return this.item(t);
  60. return [ActionMenu.Header(t.cell.obj?.label!), ...t.models.map(this.getModelItems)];
  61. }
  62. private getModelItems = (m: ModelRef): ActionMenu.Items => {
  63. if (m.structures.length === 0) return this.item(m);
  64. if (m.structures.length === 1) {
  65. const selected = this.plugin.managers.structure.hierarchy.seletionSet;
  66. const ref = m.structures[0];
  67. return { label: `${m.cell.obj?.label} | ${ref.cell.obj?.label}`, selected: selected.has(ref.cell.transform.ref), value: [m, ref] } as ActionMenu.Item;
  68. }
  69. return [ActionMenu.Header(m.cell.obj?.label!), ...m.structures.map(this.item)];
  70. }
  71. get hierarchyItems() {
  72. const mng = this.plugin.managers.structure.hierarchy;
  73. const { current } = mng;
  74. const ret: ActionMenu.Items = [];
  75. if (current.trajectories.length > 1) {
  76. ret.push([
  77. ActionMenu.Header('Trajectories'),
  78. ...current.trajectories.map(this.item)
  79. ]);
  80. }
  81. if (current.models.length > 1) {
  82. ret.push([
  83. ActionMenu.Header('Models'),
  84. ...current.models.map(this.item)
  85. ])
  86. }
  87. if (current.trajectories.length === 1 && current.models.length === 1) {
  88. ret.push(...current.structures.map(this.item));
  89. } else if (current.structures.length > 0) {
  90. ret.push([
  91. ActionMenu.Header('Structures'),
  92. ...current.structures.map(this.item)
  93. ]);
  94. }
  95. return ret;
  96. }
  97. get isEmpty() {
  98. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.current;
  99. return trajectories.length === 0 && models.length === 0 && structures.length === 0;
  100. }
  101. get label() {
  102. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.state.selection;
  103. if (structures.length === 1) {
  104. const s = structures[0];
  105. if (s.model?.trajectory?.models && s.model.trajectory.models.length === 1) return s.cell.obj?.data.label;
  106. if (s.model) return `${s.model.cell.obj?.label} | ${s.cell.obj?.data.label}`;
  107. return s.cell.obj?.data.label;
  108. }
  109. if (structures.length > 1) {
  110. const p = structures[0];
  111. const t = p?.model?.trajectory;
  112. let sameTraj = true;
  113. for (const s of structures) {
  114. if (s?.model?.trajectory !== t) {
  115. sameTraj = false;
  116. break;
  117. }
  118. }
  119. return sameTraj && t ? `${t.cell.obj?.label} | ${structures.length} structures` : `${structures.length} structures`;
  120. }
  121. if (models.length > 0) {
  122. const t = models[0].trajectory;
  123. if (models.length === 1) {
  124. const model = models[0].cell.obj?.data;
  125. if (model?.trajectoryInfo.size! > 1) {
  126. return `${t?.cell.obj?.label} | Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size}`
  127. } else {
  128. return `${t?.cell.obj?.label} | Model`
  129. }
  130. }
  131. let sameTraj = true;
  132. for (const m of models) {
  133. if (m.trajectory !== t) {
  134. sameTraj = false;
  135. break;
  136. }
  137. }
  138. return sameTraj ? `${t?.cell.obj?.label} | ${models.length} models` : `${models.length} models`;
  139. }
  140. if (trajectories.length > 0) {
  141. return trajectories.length === 1 ? `${trajectories[0].cell.obj?.label} trajectory` : `${trajectories.length} trajectories`;
  142. }
  143. if (trajectories.length === 0 && models.length === 0 && structures.length === 0) {
  144. return 'Nothing Loaded';
  145. }
  146. return 'Nothing Selected';
  147. }
  148. selectHierarchy: ActionMenu.OnSelectMany = (items) => {
  149. if (!items || items.length === 0) return 0;
  150. const refs: HierarchyRef[] = [];
  151. for (const i of items) {
  152. for (const r of (i.value as HierarchyRef[])) refs.push(r);
  153. }
  154. this.plugin.managers.structure.hierarchy.updateCurrent(refs, items[0].selected ? 'remove' : 'add')
  155. }
  156. toggleHierarchy = () => this.setState({ show: this.state.show !== 'hierarchy' ? 'hierarchy' : void 0 });
  157. toggleActions = () => this.setState({ show: this.state.show !== 'actions' ? 'actions' : void 0 });
  158. actions = memoize1((sel: StructureHierarchyManager['selection']) => this._actions);
  159. get _actions() {
  160. const ret: ActionMenu.Items = [];
  161. const { selection } = this.plugin.managers.structure.hierarchy;
  162. if (selection.trajectories.some(t => t.cell.obj?.data.length! > 1)) {
  163. ret.push(ActionMenu.Item('Load all models', () => this.plugin.managers.structure.hierarchy.createModels(selection.trajectories, 'all')));
  164. }
  165. if (selection.trajectories.some(t => t.models.length > 1)) {
  166. ret.push(ActionMenu.Item('Load single model', () => this.plugin.managers.structure.hierarchy.createModels(selection.trajectories, 'single')));
  167. }
  168. // TODO: remove actions?
  169. return ret;
  170. }
  171. selectAction: ActionMenu.OnSelect = item => {
  172. if (!item) return;
  173. this.setState({ show: void 0 });
  174. (item?.value as any)();
  175. }
  176. updateStructureModel = async (params: any) => {
  177. const { selection } = this.plugin.managers.structure.hierarchy;
  178. const m = selection.structures[0].model!;
  179. this.plugin.state.updateTransform(this.plugin.state.data, m.cell.transform.ref, params, 'Model Index');
  180. // TODO: ?? PluginCommands.Camera.Reset(this.plugin);
  181. }
  182. get modelIndex() {
  183. const { selection } = this.plugin.managers.structure.hierarchy;
  184. if (selection.structures.length !== 1) return null;
  185. const m = selection.structures[0].model;
  186. if (!m || m.cell.transform.transformer !== StateTransforms.Model.ModelFromTrajectory) return null;
  187. if (m.cell.obj?.data.trajectoryInfo.size! <= 1) return null;
  188. const params = m.cell.params?.definition;
  189. if (!params) return null;
  190. return <ParameterControls params={params} values={m.cell.params?.values} onChangeValues={this.updateStructureModel} isDisabled={this.state.isBusy} />
  191. }
  192. updateStructure = async (params: any) => {
  193. const { selection } = this.plugin.managers.structure.hierarchy;
  194. const s = selection.structures[0];
  195. await this.plugin.state.updateTransform(this.plugin.state.data, s.cell.transform.ref, params, 'Structure Type');
  196. PluginCommands.Camera.Reset(this.plugin);
  197. }
  198. get structureType() {
  199. const { selection } = this.plugin.managers.structure.hierarchy;
  200. if (selection.structures.length !== 1) return null;
  201. const s = selection.structures[0];
  202. const params = s.cell.params?.definition;
  203. if (!params) return null;
  204. return <ParameterControls params={params} values={s.cell.params?.values} onChangeValues={this.updateStructure} isDisabled={this.state.isBusy} />
  205. }
  206. renderControls() {
  207. const disabled = this.state.isBusy || this.isEmpty;
  208. const actions = this.actions(this.plugin.managers.structure.hierarchy.selection);
  209. const label = this.label;
  210. return <>
  211. <div className='msp-btn-row-group' style={{ marginTop: '1px' }}>
  212. <button className='msp-btn msp-form-control msp-flex-item' onClick={this.toggleHierarchy} style={{ overflow: 'hidden', textOverflow: 'ellipsis' }} disabled={disabled} title={label}>
  213. {label}
  214. </button>
  215. {actions.length > 0 && <IconButton customClass='msp-form-control' style={{ flex: '0 0 32px' }} onClick={this.toggleActions} icon='dot-3' title='Actions' toggleState={this.state.show === 'actions'} disabled={disabled} />}
  216. </div>
  217. {this.state.show === 'hierarchy' && <ActionMenu items={this.hierarchyItems} onSelect={this.selectHierarchy} multiselect />}
  218. {this.state.show === 'actions' && <ActionMenu items={actions} onSelect={this.selectAction} />}
  219. {this.modelIndex}
  220. {this.structureType}
  221. </>;
  222. }
  223. }