Permalink
Browse files

Major Python2->3 and PyQt4->5 rework and reformat.

  • Loading branch information...
cederom committed Feb 13, 2017
1 parent 79bb5dc commit bba64ffa1488b57cd375ee28938f47e20f50f78a
View
@@ -38,7 +38,7 @@ def __init__(self, **params):
#Create the GUI MainWindow
import iCeDeROM.ui.qt.QtMainWindow
module=iCeDeROM.ui.qt.QtMainWindow.module(iCeDeROM=self, argv=sys.argv)
self.modules['log'].log.info('Loading '+module.name+' module...')
self.modules['log'].log.info('Loading '+module.name+' ('+module.description+') module...')
module.setup(iCeDeROM=self)
self.modules[module.name]=module
#Load example mdiWindow module
@@ -82,7 +82,5 @@ def __init__(self, **params):
self.modules['log'].log.info('iCeDeROM %s shutdown...', self.release)
self.modules['log'].stop()
if __name__ == '__main__':
iCD=iCeDeROM(ui='qt')
View
@@ -14,6 +14,7 @@ class module(object):
"""
Provides interactive Python shell with Qt GUI capabilities.
"""
def __init__(self, **params):
"""Creates logger."""
self.name='python'
View
@@ -7,13 +7,13 @@
# (C) 2014-2017 CeDeROM Tomasz Bolesław CEDRO (http://www.tomek.cedro.info)
# All rights reserved, so far :-)
from PyQt5 import QtCore, QtWidgets
from PyQt5 import QtCore, QtWidgets, QtGui
class module(object):
"""
Provides Qt Widget for modules.cli.python iCeDeROM module.
"""
Provides Qt Widget for modules.cli.python iCeDeROM module.
"""
def __init__(self, **params):
"""Create Qt Widget for Python CLI."""
@@ -74,69 +74,65 @@ def setupQtWidget(self, **params):
def keyPressEvent(self, QKeyEvent):
"""Extend the QTextEdit key press event, add routines, then call the original method."""
# TODO: FIX MISSING CURSOR CODE
pass
# cursor=self.texts[self.name].textCursor()
cursor=self.texts[self.name].textCursor()
if QKeyEvent.key() == QtCore.Qt.Key_Return:
# Handle Return key -> execute python command
# cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
self.texts[self.name].moveCursor(QtWidgets.QTextCursor.End, QtWidgets.QTextCursor.MoveAnchor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].moveCursor(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
self.texts[self.name].setTextCursor(cursor)
self.command = unicode(self.texts[self.name].textCursor().selectedText())
self.texts[self.name].moveCursor(QtWidgets.QTextCursor.End, QtWidgets.QTextCursor.MoveAnchor)
self.command = self.texts[self.name].textCursor().selectedText()
self.texts[self.name].moveCursor(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
self.texts[self.name].insertPlainText('\n')
self.texts[self.name].moveCursor(QtWidgets.QTextCursor.End, QtWidgets.QTextCursor.MoveAnchor)
self.texts[self.name].moveCursor(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
self.historyAppend(self.command)
self.execute(self.command)
elif QKeyEvent.key() == QtCore.Qt.Key_Up:
# Display history item
if self.history_index > 0: self.history_index -= 1
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
self.texts[self.name].insertPlainText(self.history[self.history_index])
# Highlight last history item
if self.history_index == len(self.history) - 1:
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
elif QKeyEvent.key() == QtCore.Qt.Key_Down:
# Display history item
if self.history_index < len(self.history) - 1:
self.history_index += 1
else:
self.history_index = len(self.history) - 1
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
self.texts[self.name].insertPlainText(self.history[self.history_index])
# Highlight last history item
if self.history_index == len(self.history) - 1:
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
elif QKeyEvent.key() == QtCore.Qt.Key_PageUp:
# Display history item
self.history_index = 0
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
self.texts[self.name].insertPlainText(self.history[self.history_index])
elif QKeyEvent.key() == QtCore.Qt.Key_PageDown:
# Display history item
self.history_index = len(self.history) - 1
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
self.texts[self.name].insertPlainText(self.history[self.history_index])
# Highlight last history item
if self.history_index == len(self.history) - 1:
cursor.select(QtWidgets.QTextCursor.LineUnderCursor)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.texts[self.name].setTextCursor(cursor)
else:
# No more key extensions, call the original handler
self.texts[self.name].keyPressEventOrig(QKeyEvent)
def write(self, data):
'''STDOUT write wrapper.'''
# TODO: FIX MISSING CURSOR CODE
return
"""STDOUT write wrapper."""
self.texts[self.name].insertPlainText(data)
self.texts[self.name].moveCursor(QtWidgets.QTextCursor.End, QtWidgets.QTextCursor.MoveAnchor)
self.texts[self.name].moveCursor(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
def execute(self, command):
"""Super class should replace this with python evaluation routine."""
View
@@ -12,10 +12,10 @@ class module(object):
def __init__(self, **params):
"""
Create Module and QtWidget if necessary.
Parameters:
iCeDeROM module reference (mandatory).
"""
Create Module and QtWidget if necessary.
Parameters:
iCeDeROM module reference (mandatory).
"""
self.name = 'terminal'
if not 'iCeDeROM' in params:
raise KeyError('iCeDeROM parameter reference mandatory!')
@@ -74,4 +74,5 @@ def logFileStop(self):
self.logFileEnabled = False
def logFileWrite(self, data):
if self.logFileEnabled: self.logFile.write(data)
#TODO: FIX HARDCODED UTF ENCODING / ADD TERMINAL ENCODING
if self.logFileEnabled: self.logFile.write(bytearray(source=data, encoding="UTF-8"))
@@ -8,13 +8,14 @@
# All rights reserved, so far :-)
import os
from PyQt5 import QtCore, QtWidgets
from PyQt5 import Qt, QtGui, QtCore, QtWidgets
READ_BYTES_CHUNK_SIZE = 256
class module(object):
"""
Provides Qt Widget for modules.cli.terminal iCeDeROM module.
"""
Provides Qt Widget for modules.cli.terminal iCeDeROM module.
"""
def __init__(self, **params):
"""Create Qt Widget for Terminal CLI."""
@@ -23,6 +24,8 @@ def __init__(self, **params):
raise KeyError('iCeDeROM parameter reference mandatory!')
if not 'gui' in params['iCeDeROM'].modules:
raise RuntimeError('Terminal QtWidget requires GUI running!')
if not 'parent' in params:
raise RuntimeError('Terminal requires Parent to run!')
self.iCeDeROM = params['iCeDeROM']
self.parent = params['parent'] if 'parent' in params else None
self.windows = dict()
@@ -69,9 +72,7 @@ def createQtWidget(self, **params):
def createQtWidgetMdiWindow(self, **params):
self.windows[self.name] = QtWidgets.QMainWindow()
self.texts[self.name] = QtWidgets.QTextEdit(self.windows[self.name])
# TODO: FIX COMPONENT CODE
# self.fonts[self.name]=QtWidgets.QFont('courier')
self.fonts[self.name]=QtGui.QFont('courier')
def createQtWidgetMenu(self, **params):
self.menus[self.name] = QtWidgets.QMenu('Terminal')
@@ -95,9 +96,8 @@ def setupQtWidgetMdiWindow(self, **params):
self.windows[self.name].setWindowTitle('Terminal')
self.windows[self.name].setCentralWidget(self.texts[self.name])
self.windows[self.name].statusBar()
self.texts[self.name].setReadOnly(False)
# TODO: FIX COMPONENT CODE / SEE ABOVE FONT INIT
# self.texts[self.name].setFont(self.fonts[self.name])
#self.texts[self.name].setReadOnly(False)
self.texts[self.name].setFont(self.fonts[self.name])
self.texts[self.name].setAcceptRichText(False)
self.texts[self.name].keyPressEvent = self.keyPressEvent
self.texts[self.name].insertFromMimeData = self.insertFromMimeData
@@ -132,17 +132,13 @@ def setupQtWidgetConfigTab(self, **params):
'Stream Terminal data to a local file.'])
self.buttons['logtofile'] = QtWidgets.QCheckBox()
self.buttons['logtofile'].setTristate(False)
# TODO: FIX SIGNALLING CODE
# self.buttons['logtofile'].connect(
# self.buttons['logtofile'],QtCore.SIGNAL('stateChanged(int)'),self.logFileToggle)
self.buttons['logtofile'].stateChanged.connect(self.logFileToggle)
self.trees['config'].setItemWidget(self.trees['logtofile'], 1, self.buttons['logtofile'])
self.trees['logfilename'] = QtWidgets.QTreeWidgetItem(self.trees['logfile'],
['Filename', '', self.logFileName])
self.buttons['logfilename'] = QtWidgets.QPushButton('Select')
self.trees['config'].setItemWidget(self.trees['logfilename'], 1, self.buttons['logfilename'])
# TODO: FIX SIGNALLING CODE
# self.buttons['logfilename'].connect(
# self.buttons['logfilename'],QtCore.SIGNAL('clicked()'),self.logFileSelect)
self.buttons['logfilename'].clicked.connect(self.logFileSelect)
# Display branch
self.trees['display'] = QtWidgets.QTreeWidgetItem(self.trees['config'], ['Display'])
self.trees['autoscroll'] = QtWidgets.QTreeWidgetItem(self.trees['display'],
@@ -154,8 +150,7 @@ def setupQtWidgetConfigTab(self, **params):
self.trees['clear'] = QtWidgets.QTreeWidgetItem(self.trees['display'],
['Clear', '', 'Crear Terminal.'])
self.buttons['clear'] = QtWidgets.QPushButton('Clear')
# TODO: FIX SIGNALLIG CODE
# self.buttons['clear'].connect(self.buttons['clear'],QtCore.SIGNAL('clicked()'),self.clearTerminal)
self.buttons['clear'].clicked.connect(self.clearTerminal)
self.trees['config'].setItemWidget(self.trees['clear'], 1, self.buttons['clear'])
self.trees['config'].expandAll()
@@ -180,35 +175,36 @@ def logFileToggle(self):
def logFileSelect(self):
filename = self.iCeDeROM.modules['gui'].dialogs['file'].getSaveFileName(
self.iCeDeROM.modules['gui'].window, 'Save File')
if filename == '': return
self.logFileName = str(filename)
if filename[0] == '': return
self.logFileName = str(filename[0])
self.trees['logfilename'].setText(2, self.logFileName)
def keyPressEvent(self, QKeyEvent):
'''Handle keystrokes.'''
if self.iCeDeROM.modules['interface'].device == None: return
"""Handle keystrokes."""
if self.iCeDeROM.modules['interface'].device is None: return
try:
self.parent.write(QKeyEvent.text())
except:
self.iCeDeROM.modules['log'].log.exception('Write failed!')
def insertFromMimeData(self, QMimeData):
'''Handle Paste-From-Clipboard and Drag-And-Drop.'''
if self.iCeDeROM.modules['interface'].device == None: return
"""Handle Paste-From-Clipboard and Drag-And-Drop."""
if self.iCeDeROM.modules['interface'].device is None: return
try:
self.parent.write(QMimeData.text())
except:
self.iCeDeROM.modules['log'].log.exception('Paste failed!')
def timerEvent(self, QTimerEvent):
if self.iCeDeROM.modules['interface'].device == None: return
self.write(self.parent.read(32))
if self.iCeDeROM.modules['interface'].device is None: return
self.write(self.parent.read(READ_BYTES_CHUNK_SIZE))
def write(self, data):
if data == '': return
if data=='' or data==b'': return
if type(data) == type(b''): data=str(data.decode("UTF-8"))
self.texts[self.name].insertPlainText(data)
if self.buttons['autoscroll'].isChecked():
self.texts[self.name].moveCursor(QtWidgets.QTextCursor.End, QtWidgets.QTextCursor.MoveAnchor)
self.texts[self.name].moveCursor(Qt.QTextCursor.End, Qt.QTextCursor.MoveAnchor)
def clearTerminal(self):
self.texts[self.name].setPlainText('')
View
@@ -17,13 +17,13 @@
class module(object):
"""
Provides UART comms with FTDI based interfaces.
"""
Provides UART comms with FTDI based interfaces.
"""
def __init__(self, **params):
"""Create FTDI UART Device.
Note: Use Setup routine to connect to a physical Device.
"""
Note: Use Setup routine to connect to a physical Device.
"""
self.name = 'ftdi.uart'
if not 'iCeDeROM' in params:
raise KeyError('iCeDeROM parameter reference mandatory!')
@@ -36,24 +36,24 @@ def __init__(self, **params):
self.baudrates = [300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400]
# Try to run GUI if possible
if self.iCeDeROM.ui == 'qt':
import ftdi_qt
import iCeDeROM.interface.ftdi_qt as ftdi_qt
params['parent'] = self
self.ui['qt'] = ftdi_qt.module(**params)
def setup(self, **params):
"""Connect and/or Setup the pylibftdi device.
Parameters:
vid/pid is the USB VID and/or PID number of your interface
serial is the serial number of the device
mode is the connect mode ('t'ext or 'b'inary, default 'b')
encoding is the encoding to use for operation (default latin1)
index is the device index for equal VID/PID (default 0)
interface is the device in case of multi-interface devices
baudrate is the transmission speed in bauds
Returns:
True when successful
Note: Some parameters are mandatory to connect to a physical device!
"""
Parameters:
vid/pid is the USB VID and/or PID number of your interface
serial is the serial number of the device
mode is the connect mode ('t'ext or 'b'inary, default 'b')
encoding is the encoding to use for operation (default latin1)
index is the device index for equal VID/PID (default 0)
interface is the device in case of multi-interface devices
baudrate is the transmission speed in bauds
Returns:
True when successful
Note: Some parameters are mandatory to connect to a physical device!
"""
cfg = dict()
try:
if 'vid' in params:
Oops, something went wrong.

0 comments on commit bba64ff

Please sign in to comment.