Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View .py file in monitr #372

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions plottr/apps/monitr.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ContentType(Enum):
tag = auto()
json = auto()
md = auto()
py = auto()
image = auto()
unknown = auto()

Expand All @@ -105,6 +106,8 @@ def sort(cls, file: Optional[Union[str, Path]] = None) -> "ContentType":
return ContentType.json
elif extension == 'md':
return ContentType.md
elif extension == 'py':
return ContentType.py
elif extension == 'jpg' or extension == 'jpeg' or extension == 'png' or extension == 'image':
return ContentType.image
else:
Expand All @@ -127,7 +130,7 @@ def sort_Qcolor(cls, item: Optional["ContentType"] = None) -> QtGui.QBrush:

class SupportedDataTypes:

valid_types = ['.ddh5', '.md', '.json']
valid_types = ['.ddh5', '.md', '.json', '.py']

@classmethod
def check_valid_data(cls, file_names: Sequence[Union[str, Path]]) -> bool:
Expand Down Expand Up @@ -2057,21 +2060,15 @@ def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
self.setText(self.edit_text)


class TextEditWidget(QtWidgets.QTextEdit):
class TextViewWidget(QtWidgets.QTextEdit):
"""
Widget that displays md files that are in the same folder as a ddh5 file.

It contains a floating button that allows for editing and saving changes done in the editing phase. Text is not
editable before clicking the button.
Widget that displays a text-based file such as .md or .py in the same folder as a ddh5 file.
"""

def __init__(self, path: Path, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
self.path = path

self.floating_button = FloatingButtonWidget(parent=self)
self.floating_button.hide()

size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.setSizePolicy(size_policy)

Expand All @@ -2085,23 +2082,13 @@ def __init__(self, path: Path, *args: Any, **kwargs: Any):
self.setPlainText(self.file_text)
document = QtGui.QTextDocument(self.file_text, parent=self)
self.setDocument(document)
self.text_before_edit = self.toPlainText()
self.floating_button.save_activated.connect(self.save_activated)
self.floating_button.edit_activated.connect(self.edit_activated)
self.document().contentsChanged.connect(self.size_change)

# Arbitrary threshold height.
self.max_threshold_height = 211
self.min_threshold_height = 2
self.size_change()

def resizeEvent(self, event: QtGui.QResizeEvent) -> None:
"""
Called every time the size of the widget changes. Triggers the change in position of the floating button.
"""
super().resizeEvent(event)
self.floating_button.update_position()

def size_change(self) -> None:
"""
Changes the minimum height of the widget. Gets called every time the document changes.
Expand All @@ -2123,6 +2110,29 @@ def sizeHint(self) -> QtCore.QSize:

return QtCore.QSize(width, height)


class TextEditWidget(TextViewWidget):
"""
Widget that displays md files that are in the same folder as a ddh5 file.

It contains a floating button that allows for editing and saving changes done in the editing phase. Text is not
editable before clicking the button.
"""

def __init__(self, path: Path, *args: Any, **kwargs: Any):
super().__init__(path, *args, **kwargs)
self.floating_button = FloatingButtonWidget(parent=self)
self.floating_button.hide()
self.floating_button.save_activated.connect(self.save_activated)
self.floating_button.edit_activated.connect(self.edit_activated)

def resizeEvent(self, event: QtGui.QResizeEvent) -> None:
"""
Called every time the size of the widget changes. Triggers the change in position of the floating button.
"""
super().resizeEvent(event)
self.floating_button.update_position()

def enterEvent(self, *args: Any, **kwargs: Any) -> None:
super().enterEvent(*args, **kwargs)
self.floating_button.show()
Expand Down Expand Up @@ -2713,7 +2723,7 @@ def _fill_dict(self, data_in: Optional[dict], files_dict: Dict[Path, ContentType
if not only_data_files:
if file_type == ContentType.tag:
data_in['tag_labels'].append(prefix_text + str(file.stem))
elif file_type == ContentType.json or file_type == ContentType.md or file_type == ContentType.image:
elif file_type in [ContentType.json, ContentType.md, ContentType.py, ContentType.image]:
# Check if the files exist.
if file.is_file():
data_in['extra_files'].append((file, prefix_text + str(file.name), file_type))
Expand Down Expand Up @@ -3192,6 +3202,23 @@ def add_all_files(self, files_data: List[Tuple[Path, str, ContentType]]) -> None
self.file_windows.append(plain_text_edit)
self.right_side_layout.addWidget(plain_text_edit)

elif file_type == ContentType.py:
expand = True
if file in self.collapsed_state_dictionary:
expand = self.collapsed_state_dictionary[file]
plain_text_edit = Collapsible(widget=TextViewWidget(path=file),
title=name, expanding=expand, icon=get_md_icon())

plain_text_edit.widget.setVisible(expand)
plain_text_edit.btn.setChecked(expand)
if expand:
plain_text_edit.btn.setText(plain_text_edit.expandedTitle)
else:
plain_text_edit.btn.setText(plain_text_edit.collapsedTitle)

self.file_windows.append(plain_text_edit)
self.right_side_layout.addWidget(plain_text_edit)

elif file_type == ContentType.image:
expand = True
if file in self.collapsed_state_dictionary:
Expand Down