Skip to content

E-Log Post Modal

ElogPostModal(parent=None, image_bytes=None)

Bases: QDialog

Modal widget for creating a new E-Log entry.

This widget provides a comprehensive interface for creating a new E-Log entry.

Source code in trace/widgets/elog_post_modal.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(
    self,
    parent: QWidget = None,
    image_bytes: bytes | None = None,
):
    super().__init__(parent)

    self.setModal(True)

    main_layout = QVBoxLayout()
    self.setLayout(main_layout)

    modal_label = SettingsTitle(self, "New Elog Entry", size=14)
    main_layout.addWidget(modal_label)

    if image_bytes is not None:
        pixmap = QPixmap()
        pixmap.loadFromData(image_bytes)
        image_label = QLabel()
        image_label.setPixmap(pixmap)
        image_label.setScaledContents(True)
        image_label.setFixedSize(400, 300)
        main_layout.addWidget(image_label)

    self.title_edit = QLineEdit(self)
    self.title_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
    self.title_edit.setMinimumWidth(250)
    title_row = SettingsRowItem(self, "Title\n(required)", self.title_edit)
    main_layout.addLayout(title_row)

    self.body_edit = QTextEdit(self)
    body_row = SettingsRowItem(self, "Body\n(optional)", self.body_edit)
    main_layout.addLayout(body_row)

    self.logbook_list = QListWidget(self)
    self.logbook_list.setSelectionMode(QListWidget.MultiSelection)
    logbook_row = SettingsRowItem(self, "Logbooks\n(required)", self.logbook_list)
    main_layout.addLayout(logbook_row)

    self.logbook_readback = QLabel()
    self.logbook_readback.setWordWrap(True)
    self.logbook_readback.setMinimumWidth(250)
    self.logbook_readback.setMaximumWidth(350)
    logbook_readback_row = SettingsRowItem(self, "Selected Logbooks", self.logbook_readback)
    self.logbook_list.itemSelectionChanged.connect(
        lambda: self.logbook_readback.setText(", ".join(item.text() for item in self.logbook_list.selectedItems()))
    )
    main_layout.addLayout(logbook_readback_row)

    self.attach_config_checkbox = QCheckBox(self)
    attach_config_row = SettingsRowItem(self, "Attach Config", self.attach_config_checkbox)
    main_layout.addLayout(attach_config_row)

    buttons = QDialogButtonBox()
    send_button = buttons.addButton("Send", QDialogButtonBox.AcceptRole)
    cancel_button = buttons.addButton(QDialogButtonBox.Cancel)

    cancel_button.clicked.connect(self.reject)
    send_button.clicked.connect(self.on_submit)
    main_layout.addWidget(buttons)

on_submit()

Handles the submission of the dialog. This method is called when the user clicks the 'Send' button. It retrieves the inputs, validates, and closes the dialog.

Source code in trace/widgets/elog_post_modal.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def on_submit(self) -> None:
    """Handles the submission of the dialog. This method is called
    when the user clicks the 'Send' button. It retrieves the inputs,
    validates, and closes the dialog.
    """
    title, _, logbooks, _ = self.get_inputs()
    if not title:
        QMessageBox.warning(self, "Input Error", "Title is required.")
        return
    if not logbooks:
        QMessageBox.warning(self, "Input Error", "At least one logbook must be selected.")
        return

    self.accept()

get_inputs()

Returns the inputs from the dialog as a tuple.

Returns:

Type Description
tuple[str, str, list[str], bool]

The inputs from the dialog (title, body, logbooks, attach_config)

Source code in trace/widgets/elog_post_modal.py
102
103
104
105
106
107
108
109
110
111
112
113
114
def get_inputs(self) -> tuple[str, str, list[str], bool]:
    """Returns the inputs from the dialog as a tuple.

    Returns
    -------
    tuple[str, str, list[str], bool]
        The inputs from the dialog (title, body, logbooks, attach_config)
    """
    title = self.title_edit.text().strip()
    body = self.body_edit.toPlainText().strip()
    logbooks = [item.text() for item in self.logbook_list.selectedItems()]
    attach_config = self.attach_config_checkbox.isChecked()
    return title, body, logbooks, attach_config

maybe_create(parent=None, image_bytes=None) classmethod

Creates and shows the ElogPostModal dialog if the logbook list can be populated. If the logbook list cannot be populated, an error message is shown and None is returned.

Parameters:

Name Type Description Default
parent QWidget

The parent widget

None
image_bytes bytes

The image bytes to be attached to the entry

None

Returns:

Type Description
ElogPostModal | None

The ElogPostModal dialog if the logbook list can be populated, None otherwise

Source code in trace/widgets/elog_post_modal.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
@classmethod
def maybe_create(cls, parent: QWidget = None, image_bytes: bytes | None = None) -> "ElogPostModal | None":
    """Creates and shows the ElogPostModal dialog if the logbook list
    can be populated. If the logbook list cannot be populated, an error
    message is shown and None is returned.

    Parameters
    ----------
    parent : QWidget, optional
        The parent widget
    image_bytes : bytes, optional
        The image bytes to be attached to the entry

    Returns
    -------
    ElogPostModal | None
        The ElogPostModal dialog if the logbook list can be populated, None otherwise
    """
    status_code, logbooks = get_logbooks()
    if status_code != 200:
        QMessageBox.critical(
            parent,
            "Elog Access Error",
            f"Unable to fetch logbooks. \n\nError code: {status_code}",
        )
        return None

    modal = cls(parent, image_bytes=image_bytes)
    modal.logbook_list.addItems(logbooks)
    return modal