Skip to content

Commit

Permalink
make console more user friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
drogenlied committed May 25, 2015
1 parent 522d40c commit 4b9680f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
4 changes: 4 additions & 0 deletions artwork/styles/console/conDark.qss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {color: #FFF; font-family: sans;}
.cmd {color: #FF0; background-color: #444;}
.nocmd {color: #EEE;}

12 changes: 7 additions & 5 deletions gui/console/Console.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, parent=None, namespace=None, historyFile=None, text=None, edi
self.output.setPlainText(text)

self.historyFile = historyFile
self.stylesheet = ''

history = self.loadHistory()
if history is not None:
Expand All @@ -103,6 +104,10 @@ def __init__(self, parent=None, namespace=None, historyFile=None, text=None, edi

self.currentTraceback = None

def applyStyleSheet(self, stylesheet):
self.stylesheet = stylesheet
self.output.document().setDefaultStyleSheet(self.stylesheet)

def loadHistory(self):
"""Return the list of previously-invoked command strings (or None)."""
if self.historyFile is not None:
Expand Down Expand Up @@ -130,7 +135,7 @@ def runCmd(self, cmd):
self.write("<br><b>%s</b>\n"%encCmd, html=True)
self.execMulti(cmd)
else:
self.write("<br><div><b>%s</b>\n"%encCmd, html=True)
self.write("<br><div class='cmd'><b>%s</b>\n"%encCmd, html=True)
self.inCmd = True
self.execSingle(cmd)

Expand Down Expand Up @@ -189,7 +194,6 @@ def execSingle(self, cmd):


def execMulti(self, nextLine):
#self.stdout.write(nextLine+"\n")
if nextLine.strip() != '':
self.multiline += "\n" + nextLine
return
Expand Down Expand Up @@ -224,10 +228,8 @@ def write(self, strn, html=False):
else:
if self.inCmd:
self.inCmd = False
self.output.textCursor().insertHtml("</div><br><div style='font-weight: normal;'>")
#self.stdout.write("</div><br><div style='font-weight: normal; background-color: #FFF;'>")
self.output.textCursor().insertHtml("</div><br><div class='nocmd' style='font-weight: normal;'>")
self.output.insertPlainText(strn)
#self.stdout.write(strn)

def displayException(self):
"""
Expand Down
51 changes: 40 additions & 11 deletions gui/console/ConsoleGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Copyright (C) 2015 Jan M. Binder jan.binder@uni-ulm.de
"""

import os
from gui.GUIBase import GUIBase
import pyqtgraph as pg
import numpy as np
Expand All @@ -36,31 +36,60 @@ def __init__(self, manager, name, config, **kwargs):
"""
c_dict = {'onactivate': self.initUI}
super().__init__(manager, name, config, c_dict)
self.modules = set()

def initUI(self, e=None):
"""Create all UI objects and show the window.
@param object e: Fysom state change notice
"""
namespace = {
if 'stylesheet' in self.getConfiguration():
stylesheetpath = os.path.join(self.get_main_dir(), 'artwork', 'styles', 'console', self.getConfiguration()['stylesheet'])
else:
stylesheetpath = os.path.join(self.get_main_dir(), 'artwork', 'styles', 'console', 'conDark.qss')
if not os.path.isfile(stylesheetpath):
self.logMsg('Stylesheet not found at {0}'.format(stylesheetpath), importance=6, msgType='warning')
self.stylesheet = ''
else:
stylesheetfile = open(stylesheetpath)
self.stylesheet = stylesheetfile.read()
stylesheetfile.close()

self.namespace = {
'pg': pg,
'np': np,
'mod': self._manager.tree['loaded'],
'gui': self._manager.tree['loaded']['gui'],
'logic': self._manager.tree['loaded']['logic'],
'hardware': self._manager.tree['loaded']['hardware'],
'config': self._manager.tree['defined']
'config': self._manager.tree['defined'],
'manager': self._manager
}
text = """
This is an interactive python console. The numpy and pyqtgraph modules have already been imported
as 'np' and 'pg'.
Configuration is in 'config', loaded modules in 'mod' and in 'hardware', 'logic' and 'gui'.
This is an interactive python console. The numpy and pyqtgraph modules have already been imported as 'np' and 'pg'.
Configuration is in 'config', the manager is 'manager' and all loaded modules are in this namespace with their configured name.
View the current namespace with dir().
Go, play.
"""
self._cw = Console.ConsoleWidget(namespace=namespace, text=text)
self.updateModuleList()
self._cw = Console.ConsoleWidget(namespace=self.namespace, text=text)
self._cw.setWindowTitle('qudi: Console')
self._cw.applyStyleSheet(self.stylesheet)
self._manager.sigShowConsole.connect(self.show)
self._manager.sigModulesChanged.connect(self.updateModuleList)
self._cw.show()

def updateModuleList(self):
"""Remove non-existing modules from namespace,
add new modules to namespace, update reloaded modules
"""
currentModules = set()
newNamespace = dict()
for base in ['hardware', 'logic', 'gui']:
for module in self._manager.tree['loaded'][base]:
currentModules.add(module)
newNamespace[module] = self._manager.tree['loaded'][base][module]
discard = self.modules - currentModules
self.namespace.update(newNamespace)
for module in discard:
self.namespace.pop(module, None)
self.modules = currentModules

def show(self):
"""Make sure that the window is visible and at the top.
"""
Expand Down

0 comments on commit 4b9680f

Please sign in to comment.