Skip to content

Commit

Permalink
Add image views
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuellar committed Jan 24, 2024
1 parent 9caa1bf commit 5a5b22c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
17 changes: 16 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
QSplitter,
QGridLayout,
QTabWidget,
QTabBar,
)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction
Expand All @@ -24,6 +25,7 @@
from widgets.mpl_canvas import MplCanvas
from widgets.slider_zoom import SliderZoom
from widgets.mpl_plot_vertical import MplPlotVertical
from widgets.image_viewer import ImageViewer
from utils import open_data
from data import Points, PlotPoints, PlotHorizo, Plot

Expand Down Expand Up @@ -64,7 +66,8 @@ def __init__(self): # pylint: disable=R0915
canvas_workspace.setLayout(canvas_workspace_layout)

# Canvas plot (with tabs)
self.tabs = QTabWidget()
self.tabs = QTabWidget(movable=False, tabsClosable=True)
self.tabs.tabCloseRequested.connect(self.close_tab)
self.canvas_plot = MplCanvas(self, width=10, height=8, dpi=100)
self.canvas_plot_grid = QWidget()
canvas_plot_grid_layout = QGridLayout()
Expand All @@ -79,6 +82,7 @@ def __init__(self): # pylint: disable=R0915
canvas_plot_grid_layout.addWidget(canvas_plot_null, 1, 0, 1, 1)
canvas_plot_grid_layout.addWidget(self.canvas_plot, 0, 1, 1, 1)
self.tabs.addTab(self.canvas_plot_grid, "Matplotlib")
self.tabs.tabBar().setTabButton(0, QTabBar.RightSide, None)

# Splitter Canvas and Settings
self.canvas_workspace_layout_splitter.addWidget(self.tabs)
Expand Down Expand Up @@ -220,6 +224,17 @@ def add_vertical_settings(self):
mpl_plot_vertical.setObjectName("mpl_plot_vertical")
self.controls_layout.insertWidget(3, mpl_plot_vertical)

def open_image(self, file_name):
image = ImageViewer(file_name)
self.tabs.addTab(image, file_name.split("/")[-1])
self.tabs.setCurrentIndex(self.tabs.count() - 1)

def close_tab(self, idx):
self.tabs.removeTab(idx)

def reload_files(self):
self.files_menu.open_folder(self.last_dir_open)


if __name__ == "__main__":
app = QApplication([])
Expand Down
18 changes: 16 additions & 2 deletions widgets/buttons_settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from PyQt6.QtGui import QPainter
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QStyleOption, QStyle, QApplication
from PyQt6.QtCore import Qt, QSize
Expand Down Expand Up @@ -106,8 +107,21 @@ def take_screenshot(self):
self.move_button.set_active(False)
self.zoom_button.set_active(False)
self.active = None
if app.last_dir_open:
app.canvas_plot.fig.savefig(app.last_file_open[:-4])
name_file = (
app.last_dir_open
+ "/"
+ app.plot_points.name.split(".")[0]
+ "-"
+ datetime.now().strftime("%b:%-d-%H:%M:%S")
)
if (
app.last_dir_open
and app.last_file_open
and app.plot_points.name != "Start Plot"
):
app.canvas_plot.fig.savefig(name_file)
app.reload_files()
app.open_image(name_file)

def paintEvent(self, _): # pylint: disable=C0103
o = QStyleOption()
Expand Down
9 changes: 8 additions & 1 deletion widgets/files_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ def _open_folder_recursive(self, box, folder_name, deepth=0):
normal_plot = True
except: # pylint: disable=W0702
print("Unexpected error.")
file_widget = PushButtonMenu(file, normal_plot)
file_widget = PushButtonMenu(file, "normal" if normal_plot else "alt")
file_widget.clicked.connect(
partial(self.win.open_file, file_name=file, file_dir=folder_name)
)
box.addWidget(file_widget)

if file.endswith(".png"):
file_widget = PushButtonMenu(file, "image")
file_widget.clicked.connect(
partial(self.win.open_image, file_name=folder_name + "/" + file)
)
box.addWidget(file_widget)

if (
os.path.isdir(folder_name + "/" + file)
and not "." in file
Expand Down
10 changes: 10 additions & 0 deletions widgets/image_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from PyQt6.QtWidgets import QLabel
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt


class ImageViewer(QLabel):
def __init__(self, filename):
super().__init__()
self.setPixmap(QPixmap(filename))
self.setAlignment(Qt.AlignCenter | Qt.AlignCenter)
6 changes: 4 additions & 2 deletions widgets/push_button_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@


class PushButtonMenu(QPushButton):
def __init__(self, text, normal=False):
def __init__(self, text, file_type="normal"):
icon = qta.icon("fa5.file")
if not normal:
if file_type == "alt":
icon = qta.icon("fa5.file-alt")
if file_type == "image":
icon = qta.icon("fa5.file-image", color="#a86832")
super().__init__(icon, text)
self.filename = text

Expand Down

0 comments on commit 5a5b22c

Please sign in to comment.