source.tsx 11 KB

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