PluginAiOllama.py

changeset 4
7dd1b9cd3150
parent 3
ca28466a186d
child 5
6e8af43d537d
--- a/PluginAiOllama.py	Sun Aug 04 16:57:01 2024 +0200
+++ b/PluginAiOllama.py	Mon Aug 05 18:37:16 2024 +0200
@@ -9,11 +9,24 @@
 
 import os
 
-from PyQt6.QtCore import QObject, Qt, QTranslator
+from PyQt6.QtCore import QObject, Qt, QTranslator, pyqtSignal
+from PyQt6.QtGui import QKeySequence
 
 from eric7 import Preferences
+from eric7.EricGui import EricPixmapCache
+from eric7.EricGui.EricAction import EricAction
 from eric7.EricWidgets.EricApplication import ericApp
 
+try:
+    from eric7.UI.UserInterface import UserInterfaceSide
+
+    _Side = UserInterfaceSide.Right
+except ImportError:
+    # backward compatibility for eric < 24.2
+    from eric7.UI.UserInterface import UserInterface
+
+    _Side = UserInterface.RightSide
+
 # Start-Of-Header
 __header__ = {
     "name": "ollama Interface",
@@ -81,17 +94,24 @@
     """
     Function to clear the private data of the plug-in.
     """
-    # TODO: not implemented yet
-    pass
+    if ollamaInterfacePluginObject is not None:
+        widget = ollamaInterfacePluginObject.getWidget()
+        if widget is not None:
+            widget.clearHistory()
 
 
 class PluginOllamaInterface(QObject):
     """
     Class implementing the ollama Interface plug-in.
+
+    @signal preferencesChanged() emitted to signal a change of preferences. This
+        signal is simply relayed from the main UI.
     """
 
     PreferencesKey = "Ollama"
 
+    preferencesChanged = pyqtSignal()
+
     def __init__(self, ui):
         """
         Constructor
@@ -107,6 +127,7 @@
             "OllamaScheme": "http",
             "OllamaHost": "localhost",
             "OllamaPort": 11434,
+            "OllamaHeartbeatInterval": 5,  # 5 seconds heartbeat time; 0 = disabled
         }
 
         self.__translator = None
@@ -125,9 +146,52 @@
         @return tuple of None and activation status
         @rtype bool
         """
-        global error
+        from OllamaInterface.OllamaWidget import OllamaWidget
+
+        global error, ollamaInterfacePluginObject
         error = ""  # clear previous error
-        # TODO: not implemented yet
+        ollamaInterfacePluginObject = self
+
+        usesDarkPalette = ericApp().usesDarkPalette()
+        iconSuffix = "dark" if usesDarkPalette else "light"
+
+        self.__widget = OllamaWidget(self, fromEric=True)
+        iconName = (
+            "sbOllama96"
+            if self.__ui.getLayoutType() == "Sidebars"
+            else "ollama22-{0}".format(iconSuffix)
+        )
+        self.__ui.addSideWidget(
+            _Side,
+            self.__widget,
+            EricPixmapCache.getIcon(os.path.join("OllamaInterface", "icons", iconName)),
+            self.tr("ollama AI Interface"),
+        )
+
+        self.__activateAct = EricAction(
+            self.tr("ollama AI Interface"),
+            self.tr("ollama AI Interface"),
+            QKeySequence(self.tr("Ctrl+Alt+Shift+O")),
+            0,
+            self,
+            "ollama_interface_activate",
+        )
+        self.__activateAct.setStatusTip(
+            self.tr("Switch the input focus to the ollama AI window.")
+        )
+        self.__activateAct.setWhatsThis(
+            self.tr(
+                """<b>Activate ollama AI Interface</b>"""
+                """<p>This switches the input focus to the ollama AI window.</p>"""
+            )
+        )
+        self.__activateAct.triggered.connect(self.__activateWidget)
+
+        self.__ui.addEricActions([self.__activateAct], "ui")
+        menu = self.__ui.getMenu("subwindow")
+        menu.addAction(self.__activateAct)
+
+        self.__ui.preferencesChanged.connect(self.preferencesChanged)
 
         return None, True
 
@@ -135,8 +199,14 @@
         """
         Public method to deactivate this plug-in.
         """
-        # TODO: not implemented yet
-        pass
+        self.__ui.preferencesChanged.disconnect(self.preferencesChanged)
+
+        menu = self.__ui.getMenu("subwindow")
+        menu.removeAction(self.__activateAct)
+        self.__ui.removeEricActions([self.__activateAct], "ui")
+        self.__ui.removeSideWidget(self.__widget)
+
+        self.__initialize()
 
     def __loadTranslator(self):
         """
@@ -161,6 +231,15 @@
                     )
                     print("Using default.")
 
+    def getWidget(self):
+        """
+        Public method to get a reference to the 'ollama' widget.
+
+        @return reference to the 'ollama' widget
+        @rtype OllamaWidget
+        """
+        return self.__widget
+
     def __activateWidget(self):
         """
         Private slot to handle the activation of the pipx interface.
@@ -188,7 +267,7 @@
         @return the requested setting value
         @rtype Any
         """
-        if key in ("OllamaPort",):
+        if key in ("OllamaPort", "OllamaHeartbeatInterval"):
             return int(
                 Preferences.Prefs.settings.value(
                     self.PreferencesKey + "/" + key, self.__defaults[key]
@@ -234,16 +313,5 @@
         sidebar.setCurrentWidget(widget)
 
 
-def installDependencies(pipInstall):
-    """
-    Function to install dependencies of this plug-in.
-
-    @param pipInstall function to be called with a list of package names.
-    @type function
-    """
-    # TODO: not implemented yet
-    pass
-
-
 #
 # eflag: noqa = M801, U200

eric ide

mercurial