Skip to content

Commit

Permalink
add connection status hint in tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Nov 18, 2023
1 parent 658cec5 commit 294d123
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 13 deletions.
5 changes: 4 additions & 1 deletion COMTool/conn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class COMM(QObject):
onInit
onWidget
onUiInitDone
isConnected
isConnected or getConnStatus
send
getConfig
onDel
Expand Down Expand Up @@ -58,6 +58,9 @@ def send(self, data : bytes):
def isConnected(self):
raise NotImplementedError()

def getConnStatus(self):
raise NotImplementedError()

def disconnect(self):
raise NotImplementedError()

Expand Down
8 changes: 6 additions & 2 deletions COMTool/conn/conn_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Serial(COMM):
onInit
onWidget
onUiInitDone
isConnected
isConnected or getConnStatus
send
getConfig
'''
Expand Down Expand Up @@ -461,6 +461,7 @@ def receiveDataProcess(self):
except Exception:
pass
waitingReconnect = True
self.status = ConnectionStatus.LOSE
self.onConnectionStatus.emit(ConnectionStatus.LOSE, _("Connection lose!"))
self.showSwitchSignal.emit(ConnectionStatus.LOSE)

Expand All @@ -469,7 +470,10 @@ def send(self, data : bytes):
self.com.write(data)

def isConnected(self):
return self.status == ConnectionStatus.CONNECTED
return (self.status == ConnectionStatus.CONNECTED) or (self.status == ConnectionStatus.LOSE)

def getConnStatus(self):
return self.status

if __name__ == "__main__":
from PyQt5.QtWidgets import QApplication
Expand Down
12 changes: 11 additions & 1 deletion COMTool/conn/conn_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def isConnected(self):
return False
return not self.channel.closed

def getConnStatus(self):
if not self.channel:
return ConnectionStatus.DISCONNECTED
if self.channel.closed:
return ConnectionStatus.DISCONNECTED
return ConnectionStatus.CONNECTED

def resize(self, w, h):
if self.channel:
self.channel.resize_pty(width=w, height=h)
Expand All @@ -76,7 +83,7 @@ class SSH(COMM):
onInit
onWidget
onUiInitDone
isConnected
isConnected or getConnStatus
send
getConfig
'''
Expand Down Expand Up @@ -415,6 +422,9 @@ def send(self, data : bytes):
def isConnected(self):
return self.status == ConnectionStatus.CONNECTED

def getConnStatus(self):
return self.status

def ctrl(self, k, v):
if not self.conn:
return
Expand Down
5 changes: 4 additions & 1 deletion COMTool/conn/conn_tcp_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TCP_UDP(COMM):
onInit
onWidget
onUiInitDone
isConnected
isConnected or getConnStatus
send
getConfig
'''
Expand Down Expand Up @@ -597,3 +597,6 @@ def send(self, data : bytes):
def isConnected(self):
return self.status == ConnectionStatus.CONNECTED

def getConnStatus(self):
return self.status

36 changes: 32 additions & 4 deletions COMTool/main2.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ def addItem(self, pluginClass, nameSaved = None, setCurrent = False, connsConfig
item = PluginItem(name, pluginClass,
conns, connsConfigs,
self.config, pluginConfig,
self.hintSignal, self.reloadWindowSignal)
self.hintSignal, self.reloadWindowSignal,
self.onConnChnaged)
self.tabAddItem(item)
self.items.append(item)
if setCurrent:
Expand All @@ -211,6 +212,23 @@ def tabAddItem(self, item):
self.tabWidget.addTab(item.widget, item.name)
self.tabWidget.setTabToolTip(self.tabWidget.count() - 1, item.name + _(", Double click to detach as a window"))

def onConnChnaged(self, plugin, status:ConnectionStatus, msg):
for item in self.items:
if item.plugin == plugin:
for i in range(self.tabWidget.count()):
if self.tabWidget.widget(i) == item.widget:
self.setTabIcon(status, i)
break
item.widget.setWindowTitle(item.name + " - {}".format(_("Connected" if status == ConnectionStatus.CONNECTED else _("Connection lose") if status == ConnectionStatus.LOSE else _("Disconnected"))))

def setTabIcon(self, status:ConnectionStatus, i:int):
if status == ConnectionStatus.CONNECTED:
self.tabWidget.setTabIcon(i, qta.icon("fa.circle", color="#4caf50"))
elif status == ConnectionStatus.LOSE:
self.tabWidget.setTabIcon(i, qta.icon("fa.circle", color="orange"))
else:
self.tabWidget.setTabIcon(i, QIcon())

def addPluginInfo(self, pluginClass):
self.pluginsSelector.insertItem(self.pluginsSelector.count() - 1,
f'{pluginClass.name} - {pluginClass.id}')
Expand Down Expand Up @@ -459,11 +477,19 @@ def onTabDoubleClicked(self, idx):

def recoverTab(self, item, parent):
# prevent close and add this widget to tab
for i, _item in enumerate(self.items):
if _item == item:
idx = self.items.index(item)
for i in range(self.tabWidget.count()):
widget = self.tabWidget.widget(i)
for _item in self.items:
if _item.widget == widget:
idx2 = self.items.index(_item)
break
if idx2 > idx:
self.tabWidget.insertTab(i, item.widget, item.name)
self.tabWidget.setCurrentIndex(i)
item.widget.setWindowFlag(Qt.Window, False)
status = item.plugin.getConnStatus()
self.setTabIcon(status, i)
break


Expand Down Expand Up @@ -567,10 +593,12 @@ def keyReleaseEvent(self,event):
item = self.getCurrentItem()
item.onKeyReleaseEvent(event)

def getCurrentItem(self):
def getCurrentItem(self, idx = False):
widget = self.tabWidget.currentWidget()
for item in self.items:
if item.widget == widget:
if idx:
return self.tabWidget.indexOf(widget), item
return item

def toggleSettings(self):
Expand Down
10 changes: 8 additions & 2 deletions COMTool/pluginItems.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
try:
from i18n import _
from Combobox import ComboBox
from conn import ConnectionStatus, conns
from parameters import log, configFilePath
except ImportError:
from COMTool.Combobox import ComboBox
from COMTool.conn import ConnectionStatus, conns
from COMTool.i18n import _
from COMTool.parameters import log, configFilePath

Expand All @@ -25,7 +27,8 @@ class PluginItem:
def __init__(self, name, pluginClass,
connClasses, connsConfigs,
globalConfig, itemConfig,
hintSignal, reloadWindowSignal):
hintSignal, reloadWindowSignal,
connCallback):
'''
item show name, e.g. dbg-1
'''
Expand All @@ -50,6 +53,7 @@ def __init__(self, name, pluginClass,
self.plugin.ctrlConn = self.ctrlConn
self.plugin.hintSignal = self.hintSignal
self.plugin.reloadWindowSignal = self.reloadWindowSignal
self.plugin.connCallbak = connCallback
self.plugin.onInit(config=itemConfig)
if not "version" in itemConfig:
raise Exception("{} {}".format(_("version not found in config of plugin:"), self.plugin.id))
Expand Down Expand Up @@ -203,6 +207,7 @@ def _setConn(self, idx):
self.connsParent.layout().addWidget(self.currConnWidget)
self.conns[idx].onReceived = self.onReceived
self.plugin.isConnected = self.conns[idx].isConnected
self.plugin.getConnStatus = self.conns[idx].getConnStatus
self.conns[idx].onConnectionStatus.connect(self.onConnStatus)
self.connsConfigs["currConn"] = self.conns[idx].id
self.currConnIdx = idx
Expand All @@ -223,11 +228,12 @@ def initEvent(self):
def onConnChanged(self, idx):
self.conns[self.currConnIdx].disconnect()
self._setConn(idx)
self.onConnStatus(ConnectionStatus.CLOSED, _("Change connection, auto close old connection"))

def ctrlConn(self, k, v):
self.conns[self.currConnIdx].ctrl(k, v)

def onConnStatus(self, status, msg):
def onConnStatus(self, status:ConnectionStatus, msg):
self.plugin.onConnChanged(status, msg)
if self.sendProcess is None:
self.sendProcess = threading.Thread(target=self.sendDataProcess)
Expand Down
8 changes: 6 additions & 2 deletions COMTool/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ class Plugin_Base(QObject):
onWidget
onUiInitDone
onActive
send
onReceived
onConnChanged -> connCallbak # in UI thread
send # can call in UI thread directly
onReceived # in receive thread
onDel
'''
# vars set by caller
isConnected = lambda o: False
getConnStatus = lambda o: ConnectionStatus.CLOSED
connCallbak = lambda status,msg:None
send = lambda o,x,y:None # send(data_bytes=None, file_path=None, callback=lambda ok,msg:None), can call in UI thread directly
ctrlConn = lambda o,k,v:None # call ctrl func of connection
hintSignal = None # hintSignal.emit(type(error, warning, info), title, msg)
Expand Down Expand Up @@ -82,6 +85,7 @@ def onConnChanged(self, status:ConnectionStatus, msg:str):
self.statusBar.setMsg("warning", '{} {}'.format(_("Connection lose"), msg))
else:
self.statusBar.setMsg("warning", msg)
self.connCallbak(self, status, msg)

def onWidgetMain(self, parent):
'''
Expand Down
1 change: 1 addition & 0 deletions COMTool/plugins/myplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Plugin(Plugin_Base):
updateSignal = pyqtSignal(str, str)

def onConnChanged(self, status:ConnectionStatus, msg:str):
super().onConnChanged(status, msg)
print("-- connection changed: {}, msg: {}".format(status, msg))

def onWidgetMain(self, parent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Plugin(Plugin_Base):
updateSignal = pyqtSignal(str, str)

def onConnChanged(self, status:ConnectionStatus, msg:str):
super().onConnChanged(status, msg)
print("-- connection changed: {}, msg: {}".format(status, msg))

def onWidgetMain(self, parent):
Expand Down

0 comments on commit 294d123

Please sign in to comment.