measurements.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react';
  7. import { Loci } from '../../mol-model/loci';
  8. import { StructureElement } from '../../mol-model/structure';
  9. import { StructureMeasurementCell, StructureMeasurementOptions, StructureMeasurementParams } from '../../mol-plugin-state/manager/structure/measurement';
  10. import { StructureSelectionHistoryEntry } from '../../mol-plugin-state/manager/structure/selection';
  11. import { PluginStateObject } from '../../mol-plugin-state/objects';
  12. import { PluginCommands } from '../../mol-plugin/commands';
  13. import { angleLabel, dihedralLabel, distanceLabel, lociLabel } from '../../mol-theme/label';
  14. import { FiniteArray } from '../../mol-util/type-helpers';
  15. import { PurePluginUIComponent, CollapsableControls } from '../base';
  16. import { ActionMenu } from '../controls/action-menu';
  17. import { ExpandGroup, IconButton, ToggleButton, Button } from '../controls/common';
  18. import { Icon } from '../controls/icons';
  19. import { ParameterControls } from '../controls/parameters';
  20. import { UpdateTransformControl } from '../state/update-transform';
  21. // TODO details, options (e.g. change text for labels)
  22. export class StructureMeasurementsControls extends CollapsableControls {
  23. defaultState() {
  24. return {
  25. isCollapsed: false,
  26. header: 'Measurements',
  27. }
  28. }
  29. renderControls() {
  30. return <>
  31. <MeasurementControls />
  32. <MeasurementList />
  33. </>
  34. }
  35. }
  36. export class MeasurementList extends PurePluginUIComponent {
  37. componentDidMount() {
  38. this.subscribe(this.plugin.managers.structure.measurement.behaviors.state, () => {
  39. this.forceUpdate();
  40. });
  41. }
  42. renderGroup(cells: ReadonlyArray<StructureMeasurementCell>, header: string) {
  43. const group: JSX.Element[] = [];
  44. for (const cell of cells) {
  45. if (cell.obj) group.push(<MeasurementEntry key={cell.obj.id} cell={cell} />)
  46. }
  47. return group.length ? <ExpandGroup header={header} initiallyExpanded={true}>{group}</ExpandGroup> : null;
  48. }
  49. render() {
  50. const measurements = this.plugin.managers.structure.measurement.state;
  51. return <div style={{ marginTop: '6px' }}>
  52. {this.renderGroup(measurements.labels, 'Labels')}
  53. {this.renderGroup(measurements.distances, 'Distances')}
  54. {this.renderGroup(measurements.angles, 'Angles')}
  55. {this.renderGroup(measurements.dihedrals, 'Dihedrals')}
  56. {this.renderGroup(measurements.orientations, 'Orientations')}
  57. </div>;
  58. }
  59. }
  60. export class MeasurementControls extends PurePluginUIComponent<{}, { isBusy: boolean, action?: 'add' | 'options' }> {
  61. state = { isBusy: false, action: void 0 as 'add' | 'options' | undefined }
  62. componentDidMount() {
  63. this.subscribe(this.selection.events.additionsHistoryUpdated, () => {
  64. this.forceUpdate();
  65. });
  66. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  67. this.setState({ isBusy: v });
  68. });
  69. }
  70. get selection() {
  71. return this.plugin.managers.structure.selection;
  72. }
  73. measureDistance = () => {
  74. const loci = this.plugin.managers.structure.selection.additionsHistory;
  75. this.plugin.managers.structure.measurement.addDistance(loci[0].loci, loci[1].loci);
  76. }
  77. measureAngle = () => {
  78. const loci = this.plugin.managers.structure.selection.additionsHistory;
  79. this.plugin.managers.structure.measurement.addAngle(loci[0].loci, loci[1].loci, loci[2].loci);
  80. }
  81. measureDihedral = () => {
  82. const loci = this.plugin.managers.structure.selection.additionsHistory;
  83. this.plugin.managers.structure.measurement.addDihedral(loci[0].loci, loci[1].loci, loci[2].loci, loci[3].loci);
  84. }
  85. addLabel = () => {
  86. const loci = this.plugin.managers.structure.selection.additionsHistory;
  87. this.plugin.managers.structure.measurement.addLabel(loci[0].loci);
  88. }
  89. addOrientation = () => {
  90. // TODO: this should be possible to add for the whole selection
  91. const loci = this.plugin.managers.structure.selection.additionsHistory;
  92. this.plugin.managers.structure.measurement.addOrientation(loci[0].loci);
  93. }
  94. get actions(): ActionMenu.Items {
  95. const history = this.selection.additionsHistory;
  96. const ret: ActionMenu.Item[] = [
  97. { kind: 'item', label: `Label ${history.length === 0 ? ' (1 selection required)' : ' (1st selection)'}`, value: this.addLabel, disabled: history.length === 0 },
  98. { kind: 'item', label: `Orientation ${history.length === 0 ? ' (1 selection required)' : ' (1st selection)'}`, value: this.addOrientation, disabled: history.length === 0 },
  99. { kind: 'item', label: `Distance ${history.length < 2 ? ' (2 selections required)' : ' (top 2 selections)'}`, value: this.measureDistance, disabled: history.length < 2 },
  100. { kind: 'item', label: `Angle ${history.length < 3 ? ' (3 selections required)' : ' (top 3 selections)'}`, value: this.measureAngle, disabled: history.length < 3 },
  101. { kind: 'item', label: `Dihedral ${history.length < 4 ? ' (4 selections required)' : ' (top 4 selections)'}`, value: this.measureDihedral, disabled: history.length < 4 },
  102. ];
  103. return ret;
  104. }
  105. selectAction: ActionMenu.OnSelect = item => {
  106. this.toggleAdd();
  107. if (!item) return;
  108. (item?.value as any)();
  109. }
  110. toggleAdd = () => this.setState({ action: this.state.action === 'add' ? void 0 : 'add' });
  111. toggleOptions = () => this.setState({ action: this.state.action === 'options' ? void 0 : 'options' });
  112. highlight(loci: StructureElement.Loci) {
  113. this.plugin.managers.interactivity.lociHighlights.highlightOnly({ loci }, false);
  114. }
  115. moveHistory(e: StructureSelectionHistoryEntry, direction: 'up' | 'down') {
  116. this.plugin.managers.structure.selection.modifyHistory(e, direction, 4);
  117. }
  118. focusLoci(loci: StructureElement.Loci) {
  119. this.plugin.managers.camera.focusLoci(loci);
  120. }
  121. historyEntry(e: StructureSelectionHistoryEntry, idx: number) {
  122. const history = this.plugin.managers.structure.selection.additionsHistory;
  123. return <div className='msp-flex-row' key={e.id}>
  124. <Button noOverflow title='Click to focus. Hover to highlight.' onClick={() => this.focusLoci(e.loci)} style={{ width: 'auto', textAlign: 'left' }} onMouseEnter={() => this.highlight(e.loci)} onMouseLeave={this.plugin.managers.interactivity.lociHighlights.clearHighlights}>
  125. {idx}. <span dangerouslySetInnerHTML={{ __html: e.label }} />
  126. </Button>
  127. {history.length > 1 && <IconButton small={true} className='msp-form-control' onClick={() => this.moveHistory(e, 'up')} icon='up-thin' flex='20px' title={'Move up'} />}
  128. {history.length > 1 && <IconButton small={true} className='msp-form-control' onClick={() => this.moveHistory(e, 'down')} icon='down-thin' flex='20px' title={'Move down'} />}
  129. <IconButton small={true} className='msp-form-control' onClick={() => this.plugin.managers.structure.selection.modifyHistory(e, 'remove')} icon='remove' flex title={'Remove'} />
  130. </div>;
  131. }
  132. add() {
  133. const history = this.plugin.managers.structure.selection.additionsHistory;
  134. const entries: JSX.Element[] = [];
  135. for (let i = 0, _i = Math.min(history.length, 4); i < _i; i++) {
  136. entries.push(this.historyEntry(history[i], i + 1));
  137. }
  138. return <>
  139. <ActionMenu items={this.actions} onSelect={this.selectAction} />
  140. {entries.length > 0 && <div className='msp-control-offset'>
  141. {entries}
  142. </div>}
  143. {entries.length === 0 && <div className='msp-control-offset msp-help-text'>
  144. <div className='msp-help-description'><Icon name='help-circle' />Add one or more selections</div>
  145. </div>}
  146. </>
  147. }
  148. render() {
  149. return <>
  150. <div className='msp-flex-row'>
  151. <ToggleButton icon='plus' label='Add' toggle={this.toggleAdd} isSelected={this.state.action === 'add'} disabled={this.state.isBusy} />
  152. <ToggleButton icon='cog' label='' title='Options' toggle={this.toggleOptions} isSelected={this.state.action === 'options'} disabled={this.state.isBusy} style={{ flex: '0 0 40px', padding: 0 }} />
  153. </div>
  154. {this.state.action === 'add' && this.add()}
  155. {this.state.action === 'options' && <MeasurementsOptions />}
  156. </>
  157. }
  158. }
  159. class MeasurementsOptions extends PurePluginUIComponent<{}, { isDisabled: boolean }> {
  160. state = { isDisabled: false }
  161. componentDidMount() {
  162. this.subscribe(this.plugin.managers.structure.measurement.behaviors.state, () => {
  163. this.forceUpdate();
  164. });
  165. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  166. console.log('isBusy', 'measurement opt', v)
  167. this.setState({ isDisabled: v })
  168. });
  169. }
  170. changed = (options: StructureMeasurementOptions) => {
  171. this.plugin.managers.structure.measurement.setOptions(options);
  172. }
  173. render() {
  174. const measurements = this.plugin.managers.structure.measurement.state;
  175. return <div className='msp-control-offset'>
  176. <ParameterControls params={StructureMeasurementParams} values={measurements.options} onChangeValues={this.changed} isDisabled={this.state.isDisabled} />
  177. </div>;
  178. }
  179. }
  180. class MeasurementEntry extends PurePluginUIComponent<{ cell: StructureMeasurementCell }, { showUpdate: boolean }> {
  181. state = { showUpdate: false }
  182. componentDidMount() {
  183. this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
  184. this.forceUpdate();
  185. });
  186. }
  187. get selections() {
  188. return this.props.cell.obj?.data.source as PluginStateObject.Molecule.Structure.Selections | undefined;
  189. }
  190. delete = () => {
  191. PluginCommands.State.RemoveObject(this.plugin, { state: this.props.cell.parent, ref: this.props.cell.transform.parent, removeParentGhosts: true });
  192. };
  193. toggleVisibility = (e: React.MouseEvent<HTMLElement>) => {
  194. e.preventDefault();
  195. PluginCommands.State.ToggleVisibility(this.plugin, { state: this.props.cell.parent, ref: this.props.cell.transform.parent });
  196. e.currentTarget.blur();
  197. }
  198. highlight = () => {
  199. const selections = this.selections;
  200. if (!selections) return;
  201. this.plugin.managers.interactivity.lociHighlights.clearHighlights();
  202. for (const d of selections.data) {
  203. this.plugin.managers.interactivity.lociHighlights.highlight({ loci: d.loci }, false);
  204. }
  205. this.plugin.managers.interactivity.lociHighlights.highlight({ loci: this.props.cell.obj?.data.repr.getLoci()! }, false);
  206. }
  207. clearHighlight = () => {
  208. this.plugin.managers.interactivity.lociHighlights.clearHighlights();
  209. }
  210. toggleUpdate = () => this.setState({ showUpdate: !this.state.showUpdate });
  211. focus = () => {
  212. const selections = this.selections;
  213. if (!selections) return;
  214. const sphere = Loci.getBundleBoundingSphere(toLociBundle(selections.data))
  215. if (sphere) {
  216. this.plugin.managers.camera.focusSphere(sphere);
  217. }
  218. }
  219. get label() {
  220. const selections = this.selections;
  221. switch (selections?.data.length) {
  222. case 1: return lociLabel(selections.data[0].loci, { condensed: true })
  223. case 2: return distanceLabel(toLociBundle(selections.data), { condensed: true, unitLabel: this.plugin.managers.structure.measurement.state.options.distanceUnitLabel })
  224. case 3: return angleLabel(toLociBundle(selections.data), { condensed: true })
  225. case 4: return dihedralLabel(toLociBundle(selections.data), { condensed: true })
  226. default: return ''
  227. }
  228. }
  229. get actions(): ActionMenu.Items {
  230. this.props.cell.sourceRef
  231. return [ActionMenu.Item('Select This', 'flash', () => this.plugin.managers.structure.selection.fromSelections(this.props.cell.sourceRef!))];
  232. }
  233. selectAction: ActionMenu.OnSelect = item => {
  234. if (!item) return;
  235. this.setState({ showUpdate: false });
  236. (item?.value as any)();
  237. }
  238. render() {
  239. const { cell } = this.props;
  240. const { obj } = cell;
  241. if (!obj) return null;
  242. return <>
  243. <div className='msp-flex-row' key={obj.id} onMouseEnter={this.highlight} onMouseLeave={this.clearHighlight}>
  244. <button className='msp-form-control msp-control-button-label msp-no-overflow' title='Click to focus. Hover to highlight.' onClick={this.focus} style={{ width: 'auto', textAlign: 'left' }}>
  245. <span dangerouslySetInnerHTML={{ __html: this.label }} />
  246. </button>
  247. <IconButton small className='msp-form-control' onClick={this.toggleVisibility} icon='eye' flex title={cell.state.isHidden ? 'Show' : 'Hide'} toggleState={!cell.state.isHidden} />
  248. <IconButton small className='msp-form-control' onClick={this.delete} icon='remove' flex title='Delete' />
  249. <IconButton className='msp-form-control' onClick={this.toggleUpdate} icon='dot-3' flex title='Actions' toggleState={this.state.showUpdate} />
  250. </div>
  251. {this.state.showUpdate && <>
  252. <ActionMenu items={this.actions} onSelect={this.selectAction} />
  253. <div className='msp-control-offset'>
  254. <ExpandGroup header='Options' noOffset>
  255. <UpdateTransformControl state={cell.parent} transform={cell.transform} customHeader='none' autoHideApply />
  256. </ExpandGroup>
  257. </div>
  258. </>}
  259. </>
  260. }
  261. }
  262. function toLociBundle(data: FiniteArray<{ loci: Loci }, any>): { loci: FiniteArray<Loci, any> } {
  263. return { loci: (data.map(d => d.loci) as unknown as FiniteArray<Loci, any>) }
  264. }