source.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 { StructureHierarchyRef, 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, ExpandGroup } from '../controls/common';
  13. import { ParameterControls } from '../controls/parameters';
  14. import { StructureFocusControls } from './focus';
  15. import { UpdateTransformControl } from '../state/update-transform';
  16. import { StructureSelectionStatsControls } from './selection';
  17. import { StateSelection } from '../../mol-state';
  18. import BookmarksOutlined from '@material-ui/icons/BookmarksOutlined';
  19. import { MoleculeSvg } from '../controls/icons';
  20. interface StructureSourceControlState extends CollapsableState {
  21. isBusy: boolean,
  22. show?: 'hierarchy' | 'presets'
  23. }
  24. export class StructureSourceControls extends CollapsableControls<{}, StructureSourceControlState> {
  25. protected defaultState(): StructureSourceControlState {
  26. return {
  27. header: 'Structure',
  28. isCollapsed: false,
  29. isBusy: false,
  30. brand: { accent: 'purple', svg: MoleculeSvg }
  31. };
  32. }
  33. componentDidMount() {
  34. this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.selection, () => this.forceUpdate());
  35. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  36. this.setState({ isBusy: v });
  37. });
  38. }
  39. private item = (ref: StructureHierarchyRef) => {
  40. const selected = this.plugin.managers.structure.hierarchy.seletionSet;
  41. let label;
  42. switch (ref.kind) {
  43. case 'model': {
  44. const model = ref.cell.obj?.data;
  45. if (model?.trajectoryInfo.size! > 1) {
  46. label = `${ref.cell.obj?.data.entryId} | Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size}`;
  47. }
  48. label = `${ref.cell.obj?.data.entryId} | ${ref.cell.obj?.label}`; break;
  49. }
  50. case 'structure': {
  51. const model = ref.cell.obj?.data.models[0];
  52. if (model && model.trajectoryInfo.size! > 1) {
  53. label = `${model.entryId} | ${ref.cell.obj?.label} (Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size})`; break;
  54. } else if (model) {
  55. label = `${model.entryId} | ${ref.cell.obj?.label}`; break;
  56. } else {
  57. label = `${ref.cell.obj?.label}`; break;
  58. }
  59. }
  60. default: label = ref.cell.obj?.label; break;
  61. }
  62. const item: ActionMenu.Item = { kind: 'item', label: label || ref.kind, selected: selected.has(ref.cell.transform.ref), value: [ref] };
  63. return item;
  64. }
  65. getTrajectoryItems = (t: TrajectoryRef): ActionMenu.Items => {
  66. if (t.models.length === 0) return this.item(t);
  67. return [ActionMenu.Header(t.cell.obj?.label!), ...t.models.map(this.getModelItems)];
  68. }
  69. private getModelItems = (m: ModelRef): ActionMenu.Items => {
  70. if (m.structures.length === 0) return this.item(m);
  71. if (m.structures.length === 1) {
  72. const selected = this.plugin.managers.structure.hierarchy.seletionSet;
  73. const ref = m.structures[0];
  74. return { label: `${m.cell.obj?.label} | ${ref.cell.obj?.label}`, selected: selected.has(ref.cell.transform.ref), value: [m, ref] } as ActionMenu.Item;
  75. }
  76. return [ActionMenu.Header(m.cell.obj?.label!), ...m.structures.map(this.item)];
  77. }
  78. get hierarchyItems() {
  79. const mng = this.plugin.managers.structure.hierarchy;
  80. const { current } = mng;
  81. const ret: ActionMenu.Items = [];
  82. if (current.trajectories.length > 1) {
  83. ret.push([
  84. ActionMenu.Header('Trajectories'),
  85. ...current.trajectories.map(this.item)
  86. ]);
  87. }
  88. if (current.models.length > 1 || current.trajectories.length > 1) {
  89. ret.push([
  90. ActionMenu.Header('Models'),
  91. ...current.models.map(this.item)
  92. ]);
  93. }
  94. if (current.trajectories.length === 1 && current.models.length === 1) {
  95. ret.push(...current.structures.map(this.item));
  96. } else if (current.structures.length > 0) {
  97. ret.push([
  98. ActionMenu.Header('Structures'),
  99. ...current.structures.map(this.item)
  100. ]);
  101. }
  102. return ret;
  103. }
  104. get isEmpty() {
  105. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.current;
  106. return trajectories.length === 0 && models.length === 0 && structures.length === 0;
  107. }
  108. get label() {
  109. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.selection;
  110. if (structures.length === 1) {
  111. const s = structures[0];
  112. if (s.model?.trajectory?.models && s.model.trajectory.models.length === 1) return s.cell.obj?.data.label;
  113. if (s.model) return `${s.model.cell.obj?.label} | ${s.cell.obj?.data.label}`;
  114. return s.cell.obj?.data.label;
  115. }
  116. if (structures.length > 1) {
  117. const p = structures[0];
  118. const t = p?.model?.trajectory;
  119. let sameTraj = true;
  120. for (const s of structures) {
  121. if (s?.model?.trajectory !== t) {
  122. sameTraj = false;
  123. break;
  124. }
  125. }
  126. return sameTraj && t ? `${t.cell.obj?.label} | ${structures.length} structures` : `${structures.length} structures`;
  127. }
  128. if (models.length > 0) {
  129. const t = models[0].trajectory;
  130. if (models.length === 1) {
  131. const model = models[0].cell.obj?.data;
  132. if (model?.trajectoryInfo.size! > 1) {
  133. return `${t?.cell.obj?.label} | Model ${model?.trajectoryInfo.index! + 1} of ${model?.trajectoryInfo.size}`;
  134. } else {
  135. return `${t?.cell.obj?.label} | Model`;
  136. }
  137. }
  138. let sameTraj = true;
  139. for (const m of models) {
  140. if (m.trajectory !== t) {
  141. sameTraj = false;
  142. break;
  143. }
  144. }
  145. return sameTraj ? `${t?.cell.obj?.label} | ${models.length} models` : `${models.length} models`;
  146. }
  147. if (trajectories.length > 0) {
  148. return trajectories.length === 1 ? `${trajectories[0].cell.obj?.label} trajectory` : `${trajectories.length} trajectories`;
  149. }
  150. if (trajectories.length === 0 && models.length === 0 && structures.length === 0) {
  151. return 'Nothing Loaded';
  152. }
  153. return 'Nothing Selected';
  154. }
  155. selectHierarchy: ActionMenu.OnSelectMany = (items) => {
  156. if (!items || items.length === 0) return;
  157. const refs: StructureHierarchyRef[] = [];
  158. for (const i of items) {
  159. for (const r of (i.value as StructureHierarchyRef[])) refs.push(r);
  160. }
  161. this.plugin.managers.structure.hierarchy.updateCurrent(refs, items[0].selected ? 'remove' : 'add');
  162. }
  163. toggleHierarchy = () => this.setState({ show: this.state.show !== 'hierarchy' ? 'hierarchy' : void 0 });
  164. togglePreset = () => this.setState({ show: this.state.show !== 'presets' ? 'presets' : void 0 });
  165. get presetActions() {
  166. const actions: ActionMenu.Item[] = [];
  167. const { trajectories } = this.plugin.managers.structure.hierarchy.selection;
  168. if (trajectories.length !== 1) return actions;
  169. const providers = this.plugin.builders.structure.hierarchy.getPresets(trajectories[0].cell.obj);
  170. for (const p of providers) {
  171. actions.push(ActionMenu.Item(p.display.name, p, { description: p.display.description }));
  172. }
  173. return actions;
  174. }
  175. applyPreset: ActionMenu.OnSelect = item => {
  176. this.setState({ show: void 0 });
  177. if (!item) return;
  178. const mng = this.plugin.managers.structure;
  179. const { trajectories } = mng.hierarchy.selection;
  180. mng.hierarchy.applyPreset(trajectories, item.value as any);
  181. }
  182. updateStructureModel = async (params: any) => {
  183. const { selection } = this.plugin.managers.structure.hierarchy;
  184. const m = selection.structures[0].model!;
  185. this.plugin.state.updateTransform(this.plugin.state.data, m.cell.transform.ref, params, 'Model Index');
  186. // TODO: ?? PluginCommands.Camera.Reset(this.plugin);
  187. }
  188. get modelIndex() {
  189. const { selection } = this.plugin.managers.structure.hierarchy;
  190. if (selection.structures.length !== 1) return null;
  191. const m = selection.structures[0].model;
  192. if (!m || m.cell.transform.transformer !== StateTransforms.Model.ModelFromTrajectory) return null;
  193. if (m.cell.obj?.data.trajectoryInfo.size! <= 1) return null;
  194. const params = m.cell.params?.definition;
  195. if (!params) return null;
  196. return <ParameterControls params={params} values={m.cell.params?.values} onChangeValues={this.updateStructureModel} isDisabled={this.state.isBusy} />;
  197. }
  198. updateStructure = (params: any) => {
  199. const { selection } = this.plugin.managers.structure.hierarchy;
  200. const s = selection.structures[0];
  201. return this.plugin.managers.structure.hierarchy.updateStructure(s, params);
  202. }
  203. get structureType() {
  204. const { selection } = this.plugin.managers.structure.hierarchy;
  205. if (selection.structures.length !== 1) return null;
  206. const s = selection.structures[0];
  207. const params = s.cell.params?.definition;
  208. if (!params || !s.cell.parent) return null;
  209. return <UpdateTransformControl state={s.cell.parent} transform={s.cell.transform} customHeader='none' customUpdate={this.updateStructure} noMargin autoHideApply />;
  210. }
  211. get transform() {
  212. const { selection } = this.plugin.managers.structure.hierarchy;
  213. if (selection.structures.length !== 1) return null;
  214. const pivot = selection.structures[0];
  215. const t = StateSelection.tryFindDecorator(this.plugin.state.data, pivot.cell.transform.ref, StateTransforms.Model.TransformStructureConformation);
  216. if (!t) return;
  217. return <ExpandGroup header={`Conformation Transform`}>
  218. <UpdateTransformControl state={t.parent!} transform={t.transform} customHeader='none' noMargin autoHideApply />
  219. </ExpandGroup>;
  220. }
  221. renderControls() {
  222. const disabled = this.state.isBusy || this.isEmpty;
  223. const presets = this.presetActions;
  224. const label = this.label;
  225. return <>
  226. <div className='msp-flex-row' style={{ marginTop: '1px' }}>
  227. <Button noOverflow flex onClick={this.toggleHierarchy} disabled={disabled} title={label}>{label}</Button>
  228. {presets.length > 0 && <IconButton svg={BookmarksOutlined} className='msp-form-control' flex='40px' onClick={this.togglePreset} title='Apply a structure presets to the current hierarchy.' toggleState={this.state.show === 'presets'} disabled={disabled} />}
  229. </div>
  230. {this.state.show === 'hierarchy' && <ActionMenu items={this.hierarchyItems} onSelect={this.selectHierarchy} multiselect />}
  231. {this.state.show === 'presets' && <ActionMenu items={presets} onSelect={this.applyPreset} />}
  232. {this.modelIndex}
  233. {this.structureType}
  234. {this.transform}
  235. <div style={{ marginTop: '6px' }}>
  236. <StructureFocusControls />
  237. <StructureSelectionStatsControls hideOnEmpty />
  238. </div>
  239. </>;
  240. }
  241. }