measurements.tsx 14 KB

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