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

Add screenshot capability for CNApy maps #501

Merged
merged 2 commits into from
Feb 23, 2024
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
34 changes: 33 additions & 1 deletion cnapy/gui_elements/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Any, Dict
import openpyxl

from qtpy.QtCore import QFileInfo, Qt, Slot, QTimer, QSignalBlocker
from qtpy.QtCore import QFileInfo, Qt, Slot, QTimer, QSignalBlocker, QSize
from qtpy.QtGui import QColor, QIcon, QKeySequence
from qtpy.QtWidgets import (QAction, QActionGroup, QApplication, QFileDialog, QStyle,
QMainWindow, QMessageBox, QToolBar, QShortcut, QStatusBar, QLabel)
Expand Down Expand Up @@ -66,6 +66,7 @@ def __init__(self, appdata: AppData):
self.onoff_action = QAction("On/Off coloring", self)
self.onoff_action.setIcon(QIcon(":/icons/onoff.png"))
self.onoff_action.triggered.connect(self.set_onoff)

self.central_widget = CentralWidget(self)
self.setCentralWidget(self.central_widget)

Expand Down Expand Up @@ -296,6 +297,10 @@ def __init__(self, appdata: AppData):
self.dec_box_size_action.setEnabled(False)
self.cnapy_map_actions.addAction(self.dec_box_size_action)

self.cnapy_screenshot_action = QAction("Take map view screenshot...", self)
self.cnapy_screenshot_action.triggered.connect(self.take_screenshot_cnapy)
self.cnapy_map_actions.addAction(self.cnapy_screenshot_action)

escher_export_svg_action = QAction("Export as SVG...")
escher_export_svg_action.triggered.connect(
lambda: self.centralWidget().map_tabs.currentWidget().page().runJavaScript("builder.map.save_svg()"))
Expand Down Expand Up @@ -331,6 +336,7 @@ def __init__(self, appdata: AppData):
self.escher_map_actions.setVisible(False)
self.map_menu.addActions(self.escher_map_actions.actions())


self.analysis_menu = self.menu.addMenu("Analysis")

fba_action = QAction("Flux Balance Analysis (FBA)", self)
Expand Down Expand Up @@ -1477,6 +1483,32 @@ def delete_maps(self):
self.centralWidget().map_tabs.widget(i).deleteLater()
self.centralWidget().map_tabs.clear()

def take_screenshot_cnapy(self):
dialog = QFileDialog(self)
filename: str = dialog.getSaveFileName(
directory=self.appdata.work_directory,
filter="*.png",
)[0]
if not filename or len(filename) == 0:
return
elif len(filename) <= 4 or filename[-4:] != ".png":
filename += ".png"

self.setCursor(Qt.BusyCursor)
scale_factor = 10.0
view = self.centralWidget().map_tabs.currentWidget()
original_size = QSize(view.size())

view.resize(original_size * scale_factor)
view.setTransform(view.transform().scale(scale_factor, scale_factor))

pixmap = view.grab()
pixmap.save(filename, "PNG")

view.setTransform(view.transform().scale(1/scale_factor, 1/scale_factor))
view.resize(original_size)
self.setCursor(Qt.ArrowCursor)

def on_tab_change(self, idx):
if idx >= 0:
self.change_map_name_action.setEnabled(True)
Expand Down
Loading