- table heading/footer callbacks

- fix Qt contextMenuPolicy bug on PyQt5
This commit is contained in:
louisnw
2023-10-09 16:50:38 +01:00
parent ab251a7600
commit 8b6a92be62
3 changed files with 32 additions and 23 deletions

View File

@ -57,7 +57,8 @@ if (!window.Table) {
} }
let overflowWrapper = document.createElement('div') let overflowWrapper = document.createElement('div')
overflowWrapper.style.overflow = 'auto' overflowWrapper.style.overflowY = 'auto'
overflowWrapper.style.overflowX = 'hidden'
overflowWrapper.style.backgroundColor = tableBackgroundColor overflowWrapper.style.backgroundColor = tableBackgroundColor
overflowWrapper.appendChild(this.table) overflowWrapper.appendChild(this.table)
this.container.appendChild(overflowWrapper) this.container.appendChild(overflowWrapper)
@ -91,15 +92,12 @@ if (!window.Table) {
} }
addRowEventListener(row, id, isCell= false) { divToButton(div, callbackString) {
if (isCell) { div.addEventListener('mouseover', () => div.style.backgroundColor = 'rgba(60, 60, 60, 0.6)')
id = `${id};;;${isCell}` div.addEventListener('mouseout', () => div.style.backgroundColor = 'transparent')
} div.addEventListener('mousedown', () => div.style.backgroundColor = 'rgba(60, 60, 60)')
row.addEventListener('mouseover', () => row.style.backgroundColor = 'rgba(60, 60, 60, 0.6)') div.addEventListener('click', () => window.callbackFunction(callbackString))
row.addEventListener('mouseout', () => row.style.backgroundColor = 'transparent') div.addEventListener('mouseup', () => div.style.backgroundColor = 'rgba(60, 60, 60, 0.6)')
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)')
} }
newRow(id, returnClickedCell=false) { newRow(id, returnClickedCell=false) {
@ -111,14 +109,13 @@ if (!window.Table) {
cell.style.width = this.widths[i]; cell.style.width = this.widths[i];
cell.style.textAlign = this.alignments[i]; cell.style.textAlign = this.alignments[i];
cell.style.border = this.borderWidth+'px solid '+this.borderColor cell.style.border = this.borderWidth+'px solid '+this.borderColor
if (returnClickedCell) { if (returnClickedCell) {
this.addRowEventListener(cell, id, this.headings[i]) this.divToButton(cell, `${this.callbackName}_~_${id};;;${this.headings[i]}`)
} }
row[this.headings[i]] = cell row[this.headings[i]] = cell
} }
if (!returnClickedCell) { if (!returnClickedCell) {
this.addRowEventListener(row, id, false) this.divToButton(row, `${this.callbackName}_~_${id}`)
} }
this.rows[id] = row this.rows[id] = row
} }
@ -139,7 +136,7 @@ if (!window.Table) {
this.rows[rowId][column].textContent = val this.rows[rowId][column].textContent = val
} }
makeSection(type, numBoxes) { makeSection(id, type, numBoxes, func=false) {
let section = document.createElement('div') let section = document.createElement('div')
section.style.display = 'flex' section.style.display = 'flex'
section.style.width = '100%' section.style.width = '100%'
@ -149,10 +146,15 @@ if (!window.Table) {
this[type] = [] this[type] = []
for (let i = 0; i < numBoxes; i++) { for (let i = 0; i < numBoxes; i++) {
this[type].push(document.createElement('div')) let textBox = document.createElement('div')
section.appendChild(this[type][i]) section.appendChild(textBox)
this[type][i].style.flex = '1' textBox.style.flex = '1'
this[type][i].style.textAlign = 'center' textBox.style.textAlign = 'center'
if (func) {
this.divToButton(textBox, `${id}_~_${i}`)
textBox.style.borderRadius = '2px'
}
this[type].push(textBox)
} }
} }
} }

View File

@ -4,16 +4,21 @@ from typing import Union
from .util import jbool, Pane, NUM from .util import jbool, Pane, NUM
class Section: class Section(Pane):
def __init__(self, table, section_type): def __init__(self, table, section_type):
super().__init__(table.win)
self._table = table self._table = table
self.type = section_type self.type = section_type
def __call__(self, number_of_text_boxes: int): def __call__(self, number_of_text_boxes: int, func: callable = None):
self._table.run_script(f'{self._table.id}.makeSection("{self.type}", {number_of_text_boxes})') 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): 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): class Row(dict):

View File

@ -13,6 +13,7 @@ try:
from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtCore import QObject, pyqtSlot as Slot from PyQt5.QtCore import QObject, pyqtSlot as Slot
except ImportError: except ImportError:
PyQt5 = None
try: try:
from PySide6.QtWebEngineWidgets import QWebEngineView from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWebChannel import QWebChannel from PySide6.QtWebChannel import QWebChannel
@ -78,7 +79,8 @@ class QtChart(abstract.AbstractChart):
self.web_channel.registerObject('bridge', self.bridge) self.web_channel.registerObject('bridge', self.bridge)
self.webview.page().setWebChannel(self.web_channel) self.webview.page().setWebChannel(self.web_channel)
self.webview.loadFinished.connect(self.win.on_js_load) 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''' self._html = f'''
{abstract.TEMPLATE[:85]} {abstract.TEMPLATE[:85]}
<script src="qrc:///qtwebchannel/qwebchannel.js"></script> <script src="qrc:///qtwebchannel/qwebchannel.js"></script>