diff --git a/lightweight_charts/js/table.js b/lightweight_charts/js/table.js index 6ae05fb..89dd734 100644 --- a/lightweight_charts/js/table.js +++ b/lightweight_charts/js/table.js @@ -57,7 +57,8 @@ if (!window.Table) { } let overflowWrapper = document.createElement('div') - overflowWrapper.style.overflow = 'auto' + overflowWrapper.style.overflowY = 'auto' + overflowWrapper.style.overflowX = 'hidden' overflowWrapper.style.backgroundColor = tableBackgroundColor overflowWrapper.appendChild(this.table) this.container.appendChild(overflowWrapper) @@ -91,15 +92,12 @@ if (!window.Table) { } - addRowEventListener(row, id, isCell= false) { - if (isCell) { - id = `${id};;;${isCell}` - } - row.addEventListener('mouseover', () => row.style.backgroundColor = 'rgba(60, 60, 60, 0.6)') - row.addEventListener('mouseout', () => row.style.backgroundColor = 'transparent') - row.addEventListener('mousedown', () => row.style.backgroundColor = 'rgba(60, 60, 60)') - row.addEventListener('click', () => window.callbackFunction(`${this.callbackName}_~_${id}`)) - row.addEventListener('mouseup', () => row.style.backgroundColor = 'rgba(60, 60, 60, 0.6)') + divToButton(div, callbackString) { + div.addEventListener('mouseover', () => div.style.backgroundColor = 'rgba(60, 60, 60, 0.6)') + div.addEventListener('mouseout', () => div.style.backgroundColor = 'transparent') + div.addEventListener('mousedown', () => div.style.backgroundColor = 'rgba(60, 60, 60)') + div.addEventListener('click', () => window.callbackFunction(callbackString)) + div.addEventListener('mouseup', () => div.style.backgroundColor = 'rgba(60, 60, 60, 0.6)') } newRow(id, returnClickedCell=false) { @@ -111,14 +109,13 @@ if (!window.Table) { cell.style.width = this.widths[i]; cell.style.textAlign = this.alignments[i]; cell.style.border = this.borderWidth+'px solid '+this.borderColor - if (returnClickedCell) { - this.addRowEventListener(cell, id, this.headings[i]) + this.divToButton(cell, `${this.callbackName}_~_${id};;;${this.headings[i]}`) } row[this.headings[i]] = cell } if (!returnClickedCell) { - this.addRowEventListener(row, id, false) + this.divToButton(row, `${this.callbackName}_~_${id}`) } this.rows[id] = row } @@ -139,7 +136,7 @@ if (!window.Table) { this.rows[rowId][column].textContent = val } - makeSection(type, numBoxes) { + makeSection(id, type, numBoxes, func=false) { let section = document.createElement('div') section.style.display = 'flex' section.style.width = '100%' @@ -149,10 +146,15 @@ if (!window.Table) { this[type] = [] for (let i = 0; i < numBoxes; i++) { - this[type].push(document.createElement('div')) - section.appendChild(this[type][i]) - this[type][i].style.flex = '1' - this[type][i].style.textAlign = 'center' + let textBox = document.createElement('div') + section.appendChild(textBox) + textBox.style.flex = '1' + textBox.style.textAlign = 'center' + if (func) { + this.divToButton(textBox, `${id}_~_${i}`) + textBox.style.borderRadius = '2px' + } + this[type].push(textBox) } } } diff --git a/lightweight_charts/table.py b/lightweight_charts/table.py index fd08042..42b4fd9 100644 --- a/lightweight_charts/table.py +++ b/lightweight_charts/table.py @@ -4,16 +4,21 @@ from typing import Union from .util import jbool, Pane, NUM -class Section: +class Section(Pane): def __init__(self, table, section_type): + super().__init__(table.win) self._table = table self.type = section_type - def __call__(self, number_of_text_boxes: int): - self._table.run_script(f'{self._table.id}.makeSection("{self.type}", {number_of_text_boxes})') + def __call__(self, number_of_text_boxes: int, func: callable = None): + if func: + self.win.handlers[self.id] = lambda boxId: func(self._table.id, int(boxId)) + self.run_script(f''' + {self._table.id}.makeSection("{self.id}", "{self.type}", {number_of_text_boxes}, {"true" if func else ""}) + ''') def __setitem__(self, key, value): - self._table.run_script(f'{self._table.id}.{self.type}[{key}].innerText = "{value}"') + self.run_script(f'{self._table.id}.{self.type}[{key}].innerText = "{value}"') class Row(dict): diff --git a/lightweight_charts/widgets.py b/lightweight_charts/widgets.py index 7d7dd42..d43a1b6 100644 --- a/lightweight_charts/widgets.py +++ b/lightweight_charts/widgets.py @@ -13,6 +13,7 @@ try: from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtCore import QObject, pyqtSlot as Slot except ImportError: + PyQt5 = None try: from PySide6.QtWebEngineWidgets import QWebEngineView from PySide6.QtWebChannel import QWebChannel @@ -78,7 +79,8 @@ class QtChart(abstract.AbstractChart): self.web_channel.registerObject('bridge', self.bridge) self.webview.page().setWebChannel(self.web_channel) self.webview.loadFinished.connect(self.win.on_js_load) - self.webview.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu) + if not PyQt5: + self.webview.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu) self._html = f''' {abstract.TEMPLATE[:85]}