|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see guidata/LICENSE for details) |
| 5 | + |
| 6 | +""" |
| 7 | +Button item test |
| 8 | +
|
| 9 | +This item is tested separately from other items because it is special: contrary to |
| 10 | +other items, it is purely GUI oriented and has no sense in a non-GUI context. |
| 11 | +""" |
| 12 | + |
| 13 | +# guitest: show |
| 14 | + |
| 15 | +import os.path as osp |
| 16 | +import re |
| 17 | + |
| 18 | +from qtpy import QtCore as QC |
| 19 | +from qtpy import QtWidgets as QW |
| 20 | + |
| 21 | +import guidata.dataset as gds |
| 22 | +from guidata.env import execenv |
| 23 | +from guidata.qthelpers import exec_dialog, qt_app_context |
| 24 | + |
| 25 | + |
| 26 | +def information_selectable(parent: QW.QWidget, title: str, text: str) -> int: |
| 27 | + """Show an information message box with selectable text.""" |
| 28 | + box = QW.QMessageBox(parent) |
| 29 | + box.setIcon(QW.QMessageBox.Information) |
| 30 | + box.setWindowTitle(title) |
| 31 | + if re.search(r"<[a-zA-Z/][^>]*>", text): |
| 32 | + box.setTextFormat(QC.Qt.RichText) |
| 33 | + box.setTextInteractionFlags(QC.Qt.TextBrowserInteraction) |
| 34 | + else: |
| 35 | + box.setTextFormat(QC.Qt.PlainText) |
| 36 | + box.setTextInteractionFlags( |
| 37 | + QC.Qt.TextSelectableByMouse | QC.Qt.TextSelectableByKeyboard |
| 38 | + ) |
| 39 | + box.setText(text) |
| 40 | + box.setStandardButtons(QW.QMessageBox.Ok) |
| 41 | + return exec_dialog(box) |
| 42 | + |
| 43 | + |
| 44 | +class Parameters(gds.DataSet): |
| 45 | + """ |
| 46 | + DataSet test |
| 47 | + The following text is the DataSet 'comment': <br>Plain text or |
| 48 | + <b>rich text<sup>2</sup></b> are both supported, |
| 49 | + as well as special characters (α, β, γ, δ, ...) |
| 50 | + """ |
| 51 | + |
| 52 | + def button_callback(dataset, item, value, parent): |
| 53 | + execenv.print(f"Button clicked: {dataset}, {item}, {value}, {parent}") |
| 54 | + text = "<br>".join( |
| 55 | + [ |
| 56 | + f"<b>Dataset</b>: {'<br>' + '<br>'.join(str(dataset).splitlines())}", |
| 57 | + f"<b>Item</b>: {item}", |
| 58 | + f"<b>Value</b>: {value}", |
| 59 | + ] |
| 60 | + ) |
| 61 | + information_selectable(parent, "Button Clicked", text) |
| 62 | + |
| 63 | + dir = gds.DirectoryItem("Directory", osp.dirname(__file__)) |
| 64 | + preview = gds.TextItem("File names preview") |
| 65 | + button = gds.ButtonItem("Click me", button_callback, "MessageBoxInformation") |
| 66 | + |
| 67 | + |
| 68 | +def test_button_item(): |
| 69 | + """Test button item""" |
| 70 | + with qt_app_context(): |
| 71 | + prm = Parameters() |
| 72 | + execenv.print(prm) |
| 73 | + if prm.edit(): |
| 74 | + execenv.print(prm) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + test_button_item() |
0 commit comments