From c8fe616c44be0c5185a7d1cffbb8280bb288981d Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Mon, 18 Jan 2021 10:35:48 +0100
Subject: [PATCH] [communicator] add signal for plotting errors and use it

---
 nixview/communicator.py  |  2 ++
 nixview/ui/mainwindow.py | 41 +++++++++++++++++++++-------------------
 nixview/ui/plotscreen.py | 11 ++++++-----
 3 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/nixview/communicator.py b/nixview/communicator.py
index 8246e5f..1cd553f 100644
--- a/nixview/communicator.py
+++ b/nixview/communicator.py
@@ -8,5 +8,7 @@ class Communicate(QObject):
     open_recent = pyqtSignal(str)
     
     item_selected = pyqtSignal(NixTreeItem)
+    
+    plot_error = pyqtSignal(str)
      
 communicator = Communicate()
\ No newline at end of file
diff --git a/nixview/ui/mainwindow.py b/nixview/ui/mainwindow.py
index cab9e94..4045471 100644
--- a/nixview/ui/mainwindow.py
+++ b/nixview/ui/mainwindow.py
@@ -1,7 +1,6 @@
-import os
 import sys
 from PyQt5.QtWidgets import QWidget, QFileDialog, QMainWindow, QMenuBar, QToolBar, QAction, QStatusBar, QSizePolicy
-from PyQt5.QtGui import QIcon, QKeySequence
+from PyQt5.QtGui import QKeySequence
 from PyQt5.QtCore import QSize, QSettings, Qt
 
 from nixview.util.file_handler import FileHandler, NodeType
@@ -13,7 +12,7 @@ from nixview.ui.helpdialog import HelpDialog
 
 
 class NixView(QMainWindow):
-    
+
     def __init__(self, *args, **kwargs):
         super(NixView, self).__init__(*args, **kwargs)
         self._current_item = None
@@ -24,12 +23,13 @@ class NixView(QMainWindow):
         self.setStatusBar(QStatusBar(self))
         self.setMenuBar(QMenuBar(self))
         self.create_actions()
-        
+
         self._cw = CentralWidget(self)
         self.setCentralWidget(self._cw)
-        
+
         comm.communicator.open_recent.connect(self.on_open_recent)
         comm.communicator.item_selected.connect(self.on_item_selected)
+        comm.communicator.plot_error.connect(self.on_plot_error)
         self.show()
 
     def on_open_recent(self, event):
@@ -41,51 +41,51 @@ class NixView(QMainWindow):
         enable = current_item_type == NodeType.MultiTag or current_item_type == NodeType.DataArray or current_item_type == NodeType.Tag
         self._plot_action.setEnabled(enable)
         self._table_action.setEnabled(enable)
-        
+
     def create_actions(self):
         self._file_open_action = QAction(cnst.get_icon("nixview_open"), "Open", self)
         self._file_open_action.setStatusTip("Open nix file")
         self._file_open_action.setShortcut(QKeySequence("Ctrl+o"))
         self._file_open_action.triggered.connect(self.on_file_open)
-        
+
         self._file_close_action = QAction(cnst.get_icon("nixview_close"), "Close", self)
         self._file_close_action.setStatusTip("Close current nix file")
         self._file_close_action.setShortcut(QKeySequence("Ctrl+w"))
         self._file_close_action.setEnabled(False)
         self._file_close_action.triggered.connect(self.on_file_close)
-        
+
         self._quit_action = QAction(cnst.get_icon("nixview_quit"), "Quit", self)
         self._quit_action.setStatusTip("Close current file and quit")
         self._quit_action.setShortcut(QKeySequence("Ctrl+q"))
         self._quit_action.triggered.connect(self.on_quit)
-        
+
         self._plot_action = QAction(cnst.get_icon("nix_data_array"), "Plot", self)
         self._plot_action.setStatusTip("Plot currently selected entity")
         self._plot_action.setShortcut(QKeySequence("Ctrl+p"))
         self._plot_action.setEnabled(False)
         self._plot_action.triggered.connect(self.on_item_plot)
-        
+
         self._table_action = QAction(cnst.get_icon("nix_data_frame"), "Show table", self)
         self._table_action.setStatusTip("Show data as table")
         self._table_action.setShortcut(QKeySequence("Ctrl+t"))
         self._table_action.setEnabled(False)
         # self._table_action.triggered.connect(self.on_file_close)
-        
+
         self._about_action = QAction("about")
         self._about_action.setStatusTip("Show about dialog")
         self._about_action.setEnabled(True)
         self._about_action.triggered.connect(self.on_about)
-        
+
         self._help_action = QAction(cnst.get_icon("nixview_help"), "help")
         self._help_action.setStatusTip("Show help dialog")
         self._table_action.setShortcut(QKeySequence("F1"))
 
         self._help_action.setEnabled(True)
         self._help_action.triggered.connect(self.on_help)
-        
+
         self.create_toolbar()
         self.create_menu() 
-    
+
     def create_toolbar(self):
         self._toolbar = QToolBar("My main toolbar")
         #self._toolbar.setStyleSheet("QToolButton:!hover {background-color:none}")
@@ -123,7 +123,7 @@ class NixView(QMainWindow):
         help_menu.addAction(self._about_action)
         help_menu.addAction(self._help_action)
         self.setMenuBar(menu)
-    
+
     def _update_recent_files(self, filename):
         settings = QSettings(cnst.organization, cnst.application)
         recent_file_max_count = settings.value(cnst.settings_recent_file_max_count_key, 10, type=int)
@@ -134,7 +134,7 @@ class NixView(QMainWindow):
         new_filenames.extend(filenames)
         settings.setValue(cnst.settings_recent_files_key, new_filenames[:recent_file_max_count])
         del settings
-    
+
     def open_file(self, filename):
         self._current_item = None
         success, msg = self._file_handler.open(filename)
@@ -151,7 +151,7 @@ class NixView(QMainWindow):
         if dlg.exec_():
             filenames = dlg.selectedFiles()
             self.open_file(filenames[0])
-            
+
     def on_file_close(self, s):
         self._file_handler.close()
         self._cw.reset()
@@ -162,15 +162,18 @@ class NixView(QMainWindow):
     def on_quit(self, s):
         self._file_handler.close()
         sys.exit()
-        
+
     def on_item_plot(self, s):
         if self._current_item is not None:
             self._cw.plot_item(self._current_item.node_descriptor)
 
+    def on_plot_error(self, s):
+        self.statusBar().showMessage(s, 5000)
+
     def on_about(self, e):
         about = AboutDialog(self)
         about.show()
-    
+
     def on_help(self, e):
         about = HelpDialog(self)
         about.show()
\ No newline at end of file
diff --git a/nixview/ui/plotscreen.py b/nixview/ui/plotscreen.py
index 62d88f4..66bacda 100644
--- a/nixview/ui/plotscreen.py
+++ b/nixview/ui/plotscreen.py
@@ -11,6 +11,7 @@ from matplotlib.widgets import Slider
 
 from nixview.util.file_handler import FileHandler
 from nixview.util.dataview import DataView
+from nixview.communicator import communicator
 
 
 def create_label(entity):
@@ -337,19 +338,19 @@ class PlotScreen(QWidget):
         
         self.layout().addWidget(close_btn)
         self._data_view = None
-    
+
     def on_close(self):
         self.close_signal.emit()
-        
+
     def plot(self, item):
         print("plot!", item)
         print(item.entity_type, item.shape)
         try:
             self._data_view = DataView(item, self._file_handler)
         except ValueError as e:
-            print("error in plotscreen.plot", e)
+            communicator.plot_error.emit("error in plotscreen.plot %s" % e)
             return
+        
         self._data_view.request_more() # TODO this is just a test, needs to be removed
         print(self._data_view)
-        
-    
\ No newline at end of file
+