2020-06-09 15:56:54 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:52:30 +00:00
|
|
|
#
|
2020-06-09 15:56:54 +00:00
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2020-06-25 17:52:30 +00:00
|
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
2021-06-30 05:21:14 +00:00
|
|
|
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
|
2019-08-29 13:10:35 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-16 13:47:37 +00:00
|
|
|
import logging
|
2025-02-17 17:11:15 +00:00
|
|
|
from typing import Optional
|
2024-11-30 16:21:50 +00:00
|
|
|
|
2025-01-26 18:35:57 +00:00
|
|
|
from PySide6 import QtCore, QtGui, QtWidgets
|
|
|
|
|
2025-01-26 18:49:58 +00:00
|
|
|
# from .ui import get_window_icon
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2019-09-16 13:47:37 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-09-03 20:04:08 +00:00
|
|
|
|
2019-12-08 00:54:39 +00:00
|
|
|
class ScreenshotWindow(QtWidgets.QLabel):
|
2025-02-17 17:11:15 +00:00
|
|
|
pix: Optional[QtGui.QPixmap] = None
|
2019-12-08 00:54:39 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.setWindowTitle("Screenshot")
|
2025-01-26 18:35:57 +00:00
|
|
|
# TODO : self.setWindowIcon(get_window_icon())
|
2019-12-08 00:54:39 +00:00
|
|
|
|
2023-03-12 07:02:58 +00:00
|
|
|
QtGui.QShortcut(QtCore.Qt.Key.Key_Escape, self, self.hide)
|
|
|
|
self.setContextMenuPolicy(
|
|
|
|
QtCore.Qt.ContextMenuPolicy.ActionsContextMenu
|
|
|
|
)
|
2019-12-08 14:14:06 +00:00
|
|
|
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_original_size = QtGui.QAction("Original size")
|
2019-12-08 14:14:06 +00:00
|
|
|
self.action_original_size.triggered.connect(lambda: self.setScale(1))
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_2x_size = QtGui.QAction("2x size")
|
2019-12-08 14:14:06 +00:00
|
|
|
self.action_2x_size.triggered.connect(lambda: self.setScale(2))
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_3x_size = QtGui.QAction("3x size")
|
2019-12-08 14:14:06 +00:00
|
|
|
self.action_3x_size.triggered.connect(lambda: self.setScale(3))
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_4x_size = QtGui.QAction("4x size")
|
2019-12-08 14:14:06 +00:00
|
|
|
self.action_4x_size.triggered.connect(lambda: self.setScale(4))
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_5x_size = QtGui.QAction("5x size")
|
2019-12-08 14:14:06 +00:00
|
|
|
self.action_5x_size.triggered.connect(lambda: self.setScale(5))
|
|
|
|
|
|
|
|
self.addAction(self.action_original_size)
|
|
|
|
self.addAction(self.action_2x_size)
|
|
|
|
self.addAction(self.action_3x_size)
|
|
|
|
self.addAction(self.action_4x_size)
|
|
|
|
self.addAction(self.action_5x_size)
|
2023-03-12 07:02:58 +00:00
|
|
|
self.action_save_screenshot = QtGui.QAction("Save image")
|
2019-12-08 00:54:39 +00:00
|
|
|
self.action_save_screenshot.triggered.connect(self.saveScreenshot)
|
|
|
|
self.addAction(self.action_save_screenshot)
|
|
|
|
|
|
|
|
def setScreenshot(self, pixmap: QtGui.QPixmap):
|
2025-02-17 17:11:15 +00:00
|
|
|
if ScreenshotWindow.pix is None:
|
2019-12-08 00:54:39 +00:00
|
|
|
self.resize(pixmap.size())
|
2025-02-17 17:11:15 +00:00
|
|
|
ScreenshotWindow.pix = pixmap
|
2020-06-09 15:56:54 +00:00
|
|
|
self.setPixmap(
|
2025-02-17 17:11:15 +00:00
|
|
|
ScreenshotWindow.pix.scaled(
|
2020-06-09 15:56:54 +00:00
|
|
|
self.size(),
|
2023-03-14 18:22:46 +00:00
|
|
|
QtCore.Qt.AspectRatioMode.KeepAspectRatio,
|
|
|
|
QtCore.Qt.TransformationMode.FastTransformation,
|
2023-03-08 08:40:39 +00:00
|
|
|
)
|
|
|
|
)
|
2019-12-08 14:14:06 +00:00
|
|
|
w, h = pixmap.width(), pixmap.height()
|
2020-06-09 15:56:54 +00:00
|
|
|
self.action_original_size.setText(
|
2023-03-08 08:40:39 +00:00
|
|
|
"Original size (" + str(w) + "x" + str(h) + ")"
|
|
|
|
)
|
2020-06-09 15:56:54 +00:00
|
|
|
self.action_2x_size.setText(
|
2023-03-08 08:40:39 +00:00
|
|
|
"2x size (" + str(w * 2) + "x" + str(h * 2) + ")"
|
|
|
|
)
|
2020-06-09 15:56:54 +00:00
|
|
|
self.action_3x_size.setText(
|
2023-03-08 08:40:39 +00:00
|
|
|
"3x size (" + str(w * 3) + "x" + str(h * 3) + ")"
|
|
|
|
)
|
2020-06-09 15:56:54 +00:00
|
|
|
self.action_4x_size.setText(
|
2023-03-08 08:40:39 +00:00
|
|
|
"4x size (" + str(w * 4) + "x" + str(h * 4) + ")"
|
|
|
|
)
|
2020-06-09 15:56:54 +00:00
|
|
|
self.action_5x_size.setText(
|
2023-03-08 08:40:39 +00:00
|
|
|
"5x size (" + str(w * 5) + "x" + str(h * 5) + ")"
|
|
|
|
)
|
2019-12-08 00:54:39 +00:00
|
|
|
|
|
|
|
def saveScreenshot(self):
|
|
|
|
if self.pix is not None:
|
|
|
|
logger.info("Saving screenshot to file...")
|
2020-06-09 15:56:54 +00:00
|
|
|
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
|
2023-03-08 08:40:39 +00:00
|
|
|
parent=self,
|
|
|
|
caption="Save image",
|
|
|
|
filter="PNG (*.png);;All files (*.*)",
|
|
|
|
)
|
2019-12-08 00:54:39 +00:00
|
|
|
|
|
|
|
logger.debug("Filename: %s", filename)
|
|
|
|
if filename != "":
|
2019-12-08 14:14:06 +00:00
|
|
|
self.pixmap().save(filename)
|
2019-12-08 00:54:39 +00:00
|
|
|
else:
|
|
|
|
logger.warning("The user got shown an empty screenshot window?")
|
|
|
|
|
|
|
|
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
|
|
|
|
super().resizeEvent(a0)
|
2025-02-17 17:11:15 +00:00
|
|
|
if ScreenshotWindow.pix is not None:
|
2020-06-09 15:56:54 +00:00
|
|
|
self.setPixmap(
|
2025-02-17 17:11:15 +00:00
|
|
|
ScreenshotWindow.pix.scaled(
|
2020-06-09 15:56:54 +00:00
|
|
|
self.size(),
|
2023-03-14 18:22:46 +00:00
|
|
|
QtCore.Qt.AspectRatioMode.KeepAspectRatio,
|
|
|
|
QtCore.Qt.TransformationMode.FastTransformation,
|
2023-03-08 08:40:39 +00:00
|
|
|
)
|
|
|
|
)
|
2019-12-08 14:14:06 +00:00
|
|
|
|
|
|
|
def setScale(self, scale):
|
2023-03-08 08:40:39 +00:00
|
|
|
width, height = (
|
|
|
|
self.pix.size().width() * scale,
|
|
|
|
self.pix.size().height() * scale,
|
|
|
|
)
|
2019-12-08 14:14:06 +00:00
|
|
|
self.resize(width, height)
|
2025-02-27 19:03:05 +00:00
|
|
|
|
2025-02-27 19:39:46 +00:00
|
|
|
|
2025-02-27 19:03:05 +00:00
|
|
|
class LiveViewWindow(ScreenshotWindow):
|
|
|
|
def __init__(self, qtwidgets: QtWidgets.QTableWidget):
|
|
|
|
super().__init__()
|
|
|
|
self.setWindowTitle("Live View")
|
|
|
|
self.qtwidgets = qtwidgets
|
2025-02-27 19:39:46 +00:00
|
|
|
self.timer = QtCore.QTimer(self)
|
2025-02-27 19:03:05 +00:00
|
|
|
self.timer.timeout.connect(self.update_screenshot)
|
2025-02-27 19:39:46 +00:00
|
|
|
|
2025-02-27 19:03:05 +00:00
|
|
|
def start(self):
|
2025-02-27 19:39:46 +00:00
|
|
|
self.timer.start(
|
|
|
|
2000
|
|
|
|
) # Update every 2000ms (this will not burn the little chip too
|
|
|
|
# much on nanovna & tinysa)
|
|
|
|
|
2025-02-27 19:03:05 +00:00
|
|
|
def update_screenshot(self):
|
2025-02-27 19:39:46 +00:00
|
|
|
if not self.qtwidgets.app.worker.isRunning():
|
2025-02-27 19:03:05 +00:00
|
|
|
pixmap = self.qtwidgets.app.vna.getScreenshot()
|
|
|
|
self.qtwidgets.liveViewWindow.setScreenshot(pixmap)
|
2025-02-27 19:39:46 +00:00
|
|
|
self.qtwidgets.liveViewWindow.show()
|