source.tsx 12 KB

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