From 92dc2db5d5814832c445360f4ab3ecede67ac4be Mon Sep 17 00:00:00 2001
From: Srikanth Ravipati <rsri131@gmail.com>
Date: Mon, 15 Mar 2021 15:11:23 +0000
Subject: [PATCH] Added necessary files for memory widget

---
 .../workbench/workbench/plugins/memoryinfo.py | 16 +++++
 .../workbench/plugins/memorypresenter.py      | 21 +++++++
 .../workbench/workbench/plugins/memoryview.py | 59 +++++++++++++++++++
 .../workbench/plugins/memorywidget.py         | 44 ++++++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 qt/applications/workbench/workbench/plugins/memoryinfo.py
 create mode 100644 qt/applications/workbench/workbench/plugins/memorypresenter.py
 create mode 100644 qt/applications/workbench/workbench/plugins/memoryview.py
 create mode 100644 qt/applications/workbench/workbench/plugins/memorywidget.py

diff --git a/qt/applications/workbench/workbench/plugins/memoryinfo.py b/qt/applications/workbench/workbench/plugins/memoryinfo.py
new file mode 100644
index 00000000000..d320160bec4
--- /dev/null
+++ b/qt/applications/workbench/workbench/plugins/memoryinfo.py
@@ -0,0 +1,16 @@
+# Mantid Repository : https://github.com/mantidproject/mantid
+#
+# Copyright &copy; 2021 ISIS Rutherford Appleton Laboratory UKRI,
+#   NScD Oak Ridge National Laboratory, European Spallation Source,
+#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
+# SPDX - License - Identifier: GPL - 3.0 +
+#  This file is part of the mantidqt package
+#
+#
+from psutil import virtual_memory
+
+def getMemoryUsed():
+    memory_used  = virtual_memory().used
+    memory_available = virtual_memory().available
+    memory_free_percent = int(round(memory_used * 100 / memory_available))
+    return memory_free_percent
diff --git a/qt/applications/workbench/workbench/plugins/memorypresenter.py b/qt/applications/workbench/workbench/plugins/memorypresenter.py
new file mode 100644
index 00000000000..e6593ef8523
--- /dev/null
+++ b/qt/applications/workbench/workbench/plugins/memorypresenter.py
@@ -0,0 +1,21 @@
+from qtpy.QtCore import QTimer
+
+from random import random
+from workbench.plugins.memoryinfo import getMemoryUsed
+
+class MemoryPresenter(object):
+    def __init__(self, view):
+
+        self.view = view
+        self.updateMemoryUsage()
+        self.view.updateSignal.connect(self.updateMemoryUsage)
+
+        self.timer = QTimer()
+        self.timer.timeout.connect(self.view.onUpdateRequest)
+        self.timer.start(10)
+
+    def updateMemoryUsage(self):
+        memory_used = getMemoryUsed()
+        #memory_used = int(random() * 100)
+        self.view.setValue(memory_used)
+
diff --git a/qt/applications/workbench/workbench/plugins/memoryview.py b/qt/applications/workbench/workbench/plugins/memoryview.py
new file mode 100644
index 00000000000..b9ed45c25df
--- /dev/null
+++ b/qt/applications/workbench/workbench/plugins/memoryview.py
@@ -0,0 +1,59 @@
+# Mantid Repository : https://github.com/mantidproject/mantid
+#
+# Copyright &copy; 2017 ISIS Rutherford Appleton Laboratory UKRI,
+#   NScD Oak Ridge National Laboratory, European Spallation Source,
+#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
+# SPDX - License - Identifier: GPL - 3.0 +
+#  This file is part of the mantid workbench.
+#
+#
+from qtpy.QtWidgets import QWidget, QProgressBar
+from qtpy.QtCore import Signal
+from mantidqt.utils.qt import load_ui
+
+NORMAL_STYLE = """
+QProgressBar::chunk {
+    background-color: lightgreen;
+}
+"""
+
+CRITICAL_STYLE = """
+QProgressBar::chunk {
+    background-color: red;
+}
+"""
+
+def fromNormalToCritical(critical, currentValue, newValue)->bool:
+    return (currentValue < critical and newValue >= critical)
+
+def fromCriticalToNormal(critical, currentValue, newValue)->bool:
+    return (currentValue >= critical and newValue < critical)
+
+class MemoryView(QWidget):
+
+    updateSignal = Signal()
+
+    def __init__(self, parent):
+        super(MemoryView, self).__init__(parent)
+
+        self.critical = 90
+        # For the future use, if needed
+        #self.ui = load_ui(__file__, 'memorybar.ui', baseinstance=self)
+        self.memory_bar = QProgressBar()
+
+    def setBarColor(self, currentValue, newValue):
+        if (fromNormalToCritical(self.critical, currentValue, newValue)):
+            self.memory_bar.setStyleSheet(CRITICAL_STYLE)
+        elif (fromCriticalToNormal(self.critical, currentValue, newValue)):
+            self.memory_bar.setStyleSheet(NORMAL_STYLE)
+        else:
+            pass
+
+    def setValue(self, newValue):
+        currentValue = self.memory_bar.value()
+        if currentValue != newValue:
+            self.setBarColor(currentValue, newValue)
+            self.memory_bar.setValue(newValue)
+
+    def onUpdateRequest(self):
+        self.updateSignal.emit()
diff --git a/qt/applications/workbench/workbench/plugins/memorywidget.py b/qt/applications/workbench/workbench/plugins/memorywidget.py
new file mode 100644
index 00000000000..8a66ee1133e
--- /dev/null
+++ b/qt/applications/workbench/workbench/plugins/memorywidget.py
@@ -0,0 +1,44 @@
+# Mantid Repository : https://github.com/mantidproject/mantid
+#
+# Copyright &copy; 2017 ISIS Rutherford Appleton Laboratory UKRI,
+#   NScD Oak Ridge National Laboratory, European Spallation Source,
+#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
+# SPDX - License - Identifier: GPL - 3.0 +
+#  This file is part of the mantid workbench.
+#
+#
+from qtpy.QtWidgets import QVBoxLayout
+
+from workbench.plugins.base import PluginWidget
+from workbench.plugins.memoryview import MemoryView
+from workbench.plugins.memorypresenter import MemoryPresenter
+
+
+class MemoryWidget(PluginWidget):
+    def __init__(self, parent, view=None):
+        super(MemoryWidget, self).__init__(parent)
+
+        view = MemoryView(self)
+        self.presenter = MemoryPresenter(view)
+
+        self.view = view if view else MemoryView(self)
+        
+        layout = QVBoxLayout()
+        layout.addWidget(view.memory_bar)
+
+        self.setLayout(layout)
+        self.setWindowTitle(self.get_plugin_title())
+
+    # ----------------- Plugin API --------------------
+
+    def get_plugin_title(self):
+        return "Memory"
+
+    def readSettings(self, _):
+        pass
+
+    def writeSettings(self, _):
+        pass
+
+    def register_plugin(self, menu=None):
+        self.main.add_dockwidget(self)
\ No newline at end of file
-- 
GitLab