source.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as React from 'react';
  8. import { HierarchyRef, ModelRef, TrajectoryRef } from '../../mol-plugin-state/manager/structure/hierarchy-state';
  9. import { StateTransforms } from '../../mol-plugin-state/transforms';
  10. import { CollapsableControls, CollapsableState } from '../base';
  11. import { ActionMenu } from '../controls/action-menu';
  12. import { Button, IconButton } from '../controls/common';
  13. import { ParameterControls } from '../controls/parameters';
  14. import { StructureFocusControls } from './focus';
  15. import { UpdateTransformControl } from '../state/update-transform';
  16. interface StructureSourceControlState extends CollapsableState {
  17. isBusy: boolean,
  18. show?: 'hierarchy' | 'presets'
  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 && model.trajectoryInfo.size! > 1) {
  48. label = `${model.entryId} | ${ref.cell.obj?.label} (Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size})`; break;
  49. } else if (model) {
  50. label = `${model.entryId} | ${ref.cell.obj?.label}`; break;
  51. } else {
  52. label = `${ref.cell.obj?.label}`; break;
  53. }
  54. }
  55. default: label = ref.cell.obj?.label; break;
  56. }
  57. const item: ActionMenu.Item = { kind: 'item', label: label || ref.kind, selected: selected.has(ref.cell.transform.ref), value: [ref] };
  58. return item;
  59. }
  60. getTrajectoryItems = (t: TrajectoryRef): ActionMenu.Items => {
  61. if (t.models.length === 0) return this.item(t);
  62. return [ActionMenu.Header(t.cell.obj?.label!), ...t.models.map(this.getModelItems)];
  63. }
  64. private getModelItems = (m: ModelRef): ActionMenu.Items => {
  65. if (m.structures.length === 0) return this.item(m);
  66. if (m.structures.length === 1) {
  67. const selected = this.plugin.managers.structure.hierarchy.seletionSet;
  68. const ref = m.structures[0];
  69. return { label: `${m.cell.obj?.label} | ${ref.cell.obj?.label}`, selected: selected.has(ref.cell.transform.ref), value: [m, ref] } as ActionMenu.Item;
  70. }
  71. return [ActionMenu.Header(m.cell.obj?.label!), ...m.structures.map(this.item)];
  72. }
  73. get hierarchyItems() {
  74. const mng = this.plugin.managers.structure.hierarchy;
  75. const { current } = mng;
  76. const ret: ActionMenu.Items = [];
  77. if (current.trajectories.length > 1) {
  78. ret.push([
  79. ActionMenu.Header('Trajectories'),
  80. ...current.trajectories.map(this.item)
  81. ]);
  82. }
  83. if (current.models.length > 1 || current.trajectories.length > 1) {
  84. ret.push([
  85. ActionMenu.Header('Models'),
  86. ...current.models.map(this.item)
  87. ])
  88. }
  89. if (current.trajectories.length === 1 && current.models.length === 1) {
  90. ret.push(...current.structures.map(this.item));
  91. } else if (current.structures.length > 0) {
  92. ret.push([
  93. ActionMenu.Header('Structures'),
  94. ...current.structures.map(this.item)
  95. ]);
  96. }
  97. return ret;
  98. }
  99. get isEmpty() {
  100. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.current;
  101. return trajectories.length === 0 && models.length === 0 && structures.length === 0;
  102. }
  103. get label() {
  104. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.selection;
  105. if (structures.length === 1) {
  106. const s = structures[0];
  107. if (s.model?.trajectory?.models && s.model.trajectory.models.length === 1) return s.cell.obj?.data.label;
  108. if (s.model) return `${s.model.cell.obj?.label} | ${s.cell.obj?.data.label}`;
  109. return s.cell.obj?.data.label;
  110. }
  111. if (structures.length > 1) {
  112. const p = structures[0];
  113. const t = p?.model?.trajectory;
  114. let sameTraj = true;
  115. for (const s of structures) {
  116. if (s?.model?.trajectory !== t) {
  117. sameTraj = false;
  118. break;
  119. }
  120. }
  121. return sameTraj && t ? `${t.cell.obj?.label} | ${structures.length} structures` : `${structures.length} structures`;
  122. }
  123. if (models.length > 0) {
  124. const t = models[0].trajectory;
  125. if (models.length === 1) {
  126. const model = models[0].cell.obj?.data;
  127. if (model?.trajectoryInfo.size! > 1) {
  128. return `${t?.cell.obj?.label} | Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size}`
  129. } else {
  130. return `${t?.cell.obj?.label} | Model`
  131. }
  132. }
  133. let sameTraj = true;
  134. for (const m of models) {
  135. if (m.trajectory !== t) {
  136. sameTraj = false;
  137. break;
  138. }
  139. }
  140. return sameTraj ? `${t?.cell.obj?.label} | ${models.length} models` : `${models.length} models`;
  141. }
  142. if (trajectories.length > 0) {
  143. return trajectories.length === 1 ? `${trajectories[0].cell.obj?.label} trajectory` : `${trajectories.length} trajectories`;
  144. }
  145. if (trajectories.length === 0 && models.length === 0 && structures.length === 0) {
  146. return 'Nothing Loaded';
  147. }
  148. return 'Nothing Selected';
  149. }
  150. selectHierarchy: ActionMenu.OnSelectMany = (items) => {
  151. if (!items || items.length === 0) return 0;
  152. const refs: HierarchyRef[] = [];
  153. for (const i of items) {
  154. for (const r of (i.value as HierarchyRef[])) refs.push(r);
  155. }
  156. this.plugin.managers.structure.hierarchy.updateCurrent(refs, items[0].selected ? 'remove' : 'add')
  157. }
  158. toggleHierarchy = () => this.setState({ show: this.state.show !== 'hierarchy' ? 'hierarchy' : void 0 });
  159. togglePreset = () => this.setState({ show: this.state.show !== 'presets' ? 'presets' : void 0 });
  160. get presetActions() {
  161. const actions: ActionMenu.Item[] = [];
  162. const { trajectories } = this.plugin.managers.structure.hierarchy.selection;
  163. if (trajectories.length !== 1) return actions
  164. const providers = this.plugin.builders.structure.hierarchy.getPresets(trajectories[0].cell.obj)
  165. for (const p of providers) {
  166. actions.push(ActionMenu.Item(p.display.name, p));
  167. }
  168. return actions;
  169. }
  170. applyPreset: ActionMenu.OnSelect = item => {
  171. this.setState({ show: void 0 });
  172. if (!item) return;
  173. const mng = this.plugin.managers.structure;
  174. const { trajectories } = mng.hierarchy.selection;
  175. mng.hierarchy.applyPreset(trajectories, item.value as any);
  176. }
  177. updateStructureModel = async (params: any) => {
  178. const { selection } = this.plugin.managers.structure.hierarchy;
  179. const m = selection.structures[0].model!;
  180. this.plugin.state.updateTransform(this.plugin.state.data, m.cell.transform.ref, params, 'Model Index');
  181. // TODO: ?? PluginCommands.Camera.Reset(this.plugin);
  182. }
  183. get modelIndex() {
  184. const { selection } = this.plugin.managers.structure.hierarchy;
  185. if (selection.structures.length !== 1) return null;
  186. const m = selection.structures[0].model;
  187. if (!m || m.cell.transform.transformer !== StateTransforms.Model.ModelFromTrajectory) return null;
  188. if (m.cell.obj?.data.trajectoryInfo.size! <= 1) return null;
  189. const params = m.cell.params?.definition;
  190. if (!params) return null;
  191. return <ParameterControls params={params} values={m.cell.params?.values} onChangeValues={this.updateStructureModel} isDisabled={this.state.isBusy} />
  192. }
  193. updateStructure = (params: any) => {
  194. const { selection } = this.plugin.managers.structure.hierarchy;
  195. const s = selection.structures[0];
  196. return this.plugin.managers.structure.hierarchy.updateStructure(s, params);
  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 <UpdateTransformControl state={s.cell.parent} transform={s.cell.transform} customHeader='none' customUpdate={this.updateStructure} noMargin autoHideApply />
  205. }
  206. renderControls() {
  207. const disabled = this.state.isBusy || this.isEmpty;
  208. const presets = this.presetActions;
  209. const label = this.label;
  210. return <>
  211. <div className='msp-btn-row-group' style={{ marginTop: '1px' }}>
  212. <Button noOverflow flex onClick={this.toggleHierarchy} disabled={disabled} title={label}>{label}</Button>
  213. {presets.length > 0 && <IconButton className='msp-form-control' flex='40px' onClick={this.togglePreset} icon='bookmarks' title='Presets' toggleState={this.state.show === 'presets'} disabled={disabled} />}
  214. </div>
  215. {this.state.show === 'hierarchy' && <ActionMenu items={this.hierarchyItems} onSelect={this.selectHierarchy} multiselect />}
  216. {this.state.show === 'presets' && <ActionMenu items={presets} onSelect={this.applyPreset} />}
  217. {this.modelIndex}
  218. {this.structureType}
  219. <div style={{ marginTop: '6px' }}>
  220. <StructureFocusControls />
  221. </div>
  222. </>;
  223. }
  224. }