source.tsx 12 KB

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