Converted code to PEP8 style

This commit is contained in:
Raoul Snyman 2013-12-10 14:56:49 +02:00
commit 3804d38040
7 changed files with 763 additions and 664 deletions

1
.bzrignore Normal file
View File

@ -0,0 +1 @@
.idea

View File

@ -15,8 +15,8 @@ if __name__ == "__main__":
app.setOrganizationDomain(u'snyman.info') app.setOrganizationDomain(u'snyman.info')
app.setApplicationName(u'ColourTerm') app.setApplicationName(u'ColourTerm')
init_resources() init_resources()
mainWindow = MainWindow() main_window = MainWindow()
mainWindow.show() main_window.show()
exit_code = app.exec_() exit_code = app.exec_()
cleanup_resources() cleanup_resources()
sys.exit(exit_code) sys.exit(exit_code)

View File

@ -3,9 +3,9 @@ import re
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
try: try:
fromUtf8 = QtCore.QString.fromUtf8 from_utf8 = QtCore.QString.fromUtf8
except AttributeError: except AttributeError:
fromUtf8 = lambda s: s from_utf8 = lambda s: s
class SComboBox(QtGui.QComboBox): class SComboBox(QtGui.QComboBox):
@ -29,7 +29,7 @@ class Highlight(object):
self.foreground = foreground self.foreground = foreground
self.background = background self.background = background
def setPattern(self, pattern): def set_pattern(self, pattern):
self.pattern = pattern self.pattern = pattern
try: try:
self.regex = re.compile(pattern) self.regex = re.compile(pattern)
@ -38,8 +38,7 @@ class Highlight(object):
def translate(context, string, description=None): def translate(context, string, description=None):
return QtGui.QApplication.translate(context, string, description, return QtGui.QApplication.translate(context, string, description, QtGui.QApplication.UnicodeUTF8)
QtGui.QApplication.UnicodeUTF8)
def create_default_highlights(): def create_default_highlights():
@ -50,8 +49,8 @@ def create_default_highlights():
] ]
from settingsdialog import SettingsDialog from colourterm.settingsdialog import SettingsDialog
from connectdialog import ConnectDialog from colourterm.connectdialog import ConnectDialog
__all__ = ['SettingsDialog', 'ConnectDialog', 'SComboBox', 'Highlight', 'translate', 'fromUtf8'] __all__ = ['SettingsDialog', 'ConnectDialog', 'SComboBox', 'Highlight', 'translate', 'from_utf8']

View File

@ -5,7 +5,7 @@ from serial import Serial, FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS, PARITY_NONE,
PARITY_SPACE, STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO, SerialException PARITY_SPACE, STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO, SerialException
from serial.tools import list_ports from serial.tools import list_ports
from colourterm import fromUtf8, translate from colourterm import from_utf8, translate
DATA_BITS = { DATA_BITS = {
@ -28,166 +28,196 @@ STOP_BITS = {
} }
class Ui_ConnectDialog(object): class UiConnectDialog(object):
def setupUi(self, connectDialog):
self.deviceLayout = QtGui.QGridLayout(connectDialog)
self.deviceLayout.setSpacing(8)
self.deviceLayout.setContentsMargins(8, 8, 8, 0)
self.deviceLayout.setObjectName(fromUtf8('deviceLayout'))
self.portLabel = QtGui.QLabel(connectDialog)
self.portLabel.setObjectName(fromUtf8('portLabel'))
self.deviceLayout.addWidget(self.portLabel, 0, 0, 1, 1)
self.portEdit = QtGui.QComboBox(connectDialog)
self.portEdit.setObjectName(fromUtf8('portEdit'))
self.portEdit.setEditable(True)
self.deviceLayout.addWidget(self.portEdit, 0, 1, 1, 1)
self.baudLabel = QtGui.QLabel(connectDialog)
self.baudLabel.setObjectName(fromUtf8('baudLabel'))
self.deviceLayout.addWidget(self.baudLabel, 1, 0, 1, 1)
self.baudComboBox = QtGui.QComboBox(connectDialog)
self.baudComboBox.setObjectName(fromUtf8('baudComboBox'))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.baudComboBox.addItem(fromUtf8(''))
self.deviceLayout.addWidget(self.baudComboBox, 1, 1, 1, 1)
self.dataBitsLabel = QtGui.QLabel(connectDialog)
self.dataBitsLabel.setObjectName(fromUtf8('dataBitsLabel'))
self.deviceLayout.addWidget(self.dataBitsLabel, 2, 0, 1, 1)
self.dataBitsComboBox = QtGui.QComboBox(connectDialog)
self.dataBitsComboBox.setObjectName(fromUtf8('dataBitsComboBox'))
self.dataBitsComboBox.addItem(fromUtf8(''))
self.dataBitsComboBox.addItem(fromUtf8(''))
self.dataBitsComboBox.addItem(fromUtf8(''))
self.dataBitsComboBox.addItem(fromUtf8(''))
self.deviceLayout.addWidget(self.dataBitsComboBox, 2, 1, 1, 1)
self.stopBitsLabel = QtGui.QLabel(connectDialog)
self.stopBitsLabel.setObjectName(fromUtf8('stopBitsLabel'))
self.deviceLayout.addWidget(self.stopBitsLabel, 3, 0, 1, 1)
self.stopBitsComboBox = QtGui.QComboBox(connectDialog)
self.stopBitsComboBox.setObjectName(fromUtf8('stopBitsComboBox'))
self.stopBitsComboBox.addItem(fromUtf8(''))
self.stopBitsComboBox.addItem(fromUtf8(''))
self.stopBitsComboBox.addItem(fromUtf8(''))
self.deviceLayout.addWidget(self.stopBitsComboBox, 3, 1, 1, 1)
self.parityLabel = QtGui.QLabel(connectDialog)
self.parityLabel.setObjectName(fromUtf8('parityLabel'))
self.deviceLayout.addWidget(self.parityLabel, 4, 0, 1, 1)
self.parityComboBox = QtGui.QComboBox(connectDialog)
self.parityComboBox.setObjectName(fromUtf8('parityComboBox'))
self.parityComboBox.addItem(fromUtf8(''))
self.parityComboBox.addItem(fromUtf8(''))
self.parityComboBox.addItem(fromUtf8(''))
self.parityComboBox.addItem(fromUtf8(''))
self.parityComboBox.addItem(fromUtf8(''))
self.deviceLayout.addWidget(self.parityComboBox, 4, 1, 1, 1)
self.handshakeLabel = QtGui.QLabel(connectDialog)
self.handshakeLabel.setObjectName(fromUtf8('handshakeLabel'))
self.deviceLayout.addWidget(self.handshakeLabel, 0, 2, 1, 1)
self.softwareCheckBox = QtGui.QCheckBox(connectDialog)
self.softwareCheckBox.setObjectName(fromUtf8('softwareCheckBox'))
self.deviceLayout.addWidget(self.softwareCheckBox, 0, 3, 1, 1)
self.hardwareCheckBox = QtGui.QCheckBox(connectDialog)
self.hardwareCheckBox.setObjectName(fromUtf8('hardwareCheckBox'))
self.deviceLayout.addWidget(self.hardwareCheckBox, 1, 3, 1, 1)
self.openModeLabel = QtGui.QLabel(connectDialog)
self.openModeLabel.setObjectName(fromUtf8('openModeLabel'))
self.deviceLayout.addWidget(self.openModeLabel, 2, 2, 1, 1)
self.readingCheckBox = QtGui.QCheckBox(connectDialog)
self.readingCheckBox.setChecked(True)
self.readingCheckBox.setObjectName(fromUtf8('readingCheckBox'))
self.deviceLayout.addWidget(self.readingCheckBox, 2, 3, 1, 1)
self.writingCheckBox = QtGui.QCheckBox(connectDialog)
self.writingCheckBox.setChecked(True)
self.writingCheckBox.setObjectName(fromUtf8('writingCheckBox'))
self.deviceLayout.addWidget(self.writingCheckBox, 3, 3, 1, 1)
self.buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
self.deviceLayout.addWidget(self.buttonBox, 5, 0, 1, 4)
self.retranslateUi(connectDialog)
self.baudComboBox.setCurrentIndex(4)
QtCore.QMetaObject.connectSlotsByName(connectDialog)
self.buttonBox.accepted.connect(connectDialog.accept)
self.buttonBox.rejected.connect(connectDialog.reject)
def retranslateUi(self, connectDialog):
connectDialog.setWindowTitle(translate('ConnectDialog', 'Open Port'))
self.portLabel.setText(translate('ConnectDialog', 'Port:'))
self.baudLabel.setText(translate('ConnectDialog', 'Baude rate:'))
self.baudComboBox.setItemText(0, translate('ConnectDialog', '921600'))
self.baudComboBox.setItemText(1, translate('ConnectDialog', '576000'))
self.baudComboBox.setItemText(2, translate('ConnectDialog', '460800'))
self.baudComboBox.setItemText(3, translate('ConnectDialog', '230400'))
self.baudComboBox.setItemText(4, translate('ConnectDialog', '115200'))
self.baudComboBox.setItemText(5, translate('ConnectDialog', '57600'))
self.baudComboBox.setItemText(6, translate('ConnectDialog', '38400'))
self.baudComboBox.setItemText(7, translate('ConnectDialog', '19200'))
self.baudComboBox.setItemText(8, translate('ConnectDialog', '9600'))
self.baudComboBox.setItemText(9, translate('ConnectDialog', '4800'))
self.dataBitsLabel.setText(translate('ConnectDialog', 'Data bits:'))
self.dataBitsComboBox.setItemText(0, translate('ConnectDialog', '8'))
self.dataBitsComboBox.setItemText(1, translate('ConnectDialog', '7'))
self.dataBitsComboBox.setItemText(2, translate('ConnectDialog', '6'))
self.dataBitsComboBox.setItemText(3, translate('ConnectDialog', '5'))
self.stopBitsLabel.setText(translate('ConnectDialog', 'Stop bits:'))
self.stopBitsComboBox.setItemText(0, translate('ConnectDialog', '1'))
self.stopBitsComboBox.setItemText(1, translate('ConnectDialog', '1.5'))
self.stopBitsComboBox.setItemText(2, translate('ConnectDialog', '2'))
self.parityLabel.setText(translate('ConnectDialog', 'Parity:'))
self.parityComboBox.setItemText(0, translate('ConnectDialog', 'None'))
self.parityComboBox.setItemText(1, translate('ConnectDialog', 'Odd'))
self.parityComboBox.setItemText(2, translate('ConnectDialog', 'Even'))
self.parityComboBox.setItemText(3, translate('ConnectDialog', 'Mark'))
self.parityComboBox.setItemText(4, translate('ConnectDialog', 'Space'))
self.handshakeLabel.setText(translate('ConnectDialog', 'Handshake:'))
self.softwareCheckBox.setText(translate('ConnectDialog', 'Software'))
self.hardwareCheckBox.setText(translate('ConnectDialog', 'Hardware'))
self.openModeLabel.setText(translate('ConnectDialog', 'Open mode:'))
self.readingCheckBox.setText(translate('ConnectDialog', 'Reading'))
self.writingCheckBox.setText(translate('ConnectDialog', 'Writing'))
class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
def __init__(self): def __init__(self):
QtGui.QDialog.__init__(self) """
self.setupUi(self) Just to satisfy PEP8/PyLint
"""
self.device_layout = None
self.port_label = None
self.port_edit = None
self.baud_label = None
self.baud_combobox = None
self.data_bits_label = None
self.data_bits_combobox = None
self.stop_bits_label = None
self.stop_bits_combobox = None
self.parity_label = None
self.parity_combobox = None
self.handshake_label = None
self.software_checkbox = None
self.hardware_checkbox = None
self.open_mode_label = None
self.reading_checkbox = None
self.writing_checkbox = None
self.button_box = None
def getPort(self): def setup_ui(self, connect_dialog):
return unicode(self.portEdit.currentText()) """
Set up the user interface
"""
self.device_layout = QtGui.QGridLayout(connect_dialog)
self.device_layout.setSpacing(8)
self.device_layout.setContentsMargins(8, 8, 8, 0)
self.device_layout.setObjectName(from_utf8('device_layout'))
self.port_label = QtGui.QLabel(connect_dialog)
self.port_label.setObjectName(from_utf8('port_label'))
self.device_layout.addWidget(self.port_label, 0, 0, 1, 1)
self.port_edit = QtGui.QComboBox(connect_dialog)
self.port_edit.setObjectName(from_utf8('port_edit'))
self.port_edit.setEditable(True)
self.device_layout.addWidget(self.port_edit, 0, 1, 1, 1)
self.baud_label = QtGui.QLabel(connect_dialog)
self.baud_label.setObjectName(from_utf8('baud_label'))
self.device_layout.addWidget(self.baud_label, 1, 0, 1, 1)
self.baud_combobox = QtGui.QComboBox(connect_dialog)
self.baud_combobox.setObjectName(from_utf8('baud_combobox'))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.baud_combobox.addItem(from_utf8(''))
self.device_layout.addWidget(self.baud_combobox, 1, 1, 1, 1)
self.data_bits_label = QtGui.QLabel(connect_dialog)
self.data_bits_label.setObjectName(from_utf8('data_bits_label'))
self.device_layout.addWidget(self.data_bits_label, 2, 0, 1, 1)
self.data_bits_combobox = QtGui.QComboBox(connect_dialog)
self.data_bits_combobox.setObjectName(from_utf8('data_bits_combobox'))
self.data_bits_combobox.addItem(from_utf8(''))
self.data_bits_combobox.addItem(from_utf8(''))
self.data_bits_combobox.addItem(from_utf8(''))
self.data_bits_combobox.addItem(from_utf8(''))
self.device_layout.addWidget(self.data_bits_combobox, 2, 1, 1, 1)
self.stop_bits_label = QtGui.QLabel(connect_dialog)
self.stop_bits_label.setObjectName(from_utf8('stop_bits_label'))
self.device_layout.addWidget(self.stop_bits_label, 3, 0, 1, 1)
self.stop_bits_combobox = QtGui.QComboBox(connect_dialog)
self.stop_bits_combobox.setObjectName(from_utf8('stop_bits_combobox'))
self.stop_bits_combobox.addItem(from_utf8(''))
self.stop_bits_combobox.addItem(from_utf8(''))
self.stop_bits_combobox.addItem(from_utf8(''))
self.device_layout.addWidget(self.stop_bits_combobox, 3, 1, 1, 1)
self.parity_label = QtGui.QLabel(connect_dialog)
self.parity_label.setObjectName(from_utf8('parity_label'))
self.device_layout.addWidget(self.parity_label, 4, 0, 1, 1)
self.parity_combobox = QtGui.QComboBox(connect_dialog)
self.parity_combobox.setObjectName(from_utf8('parity_combobox'))
self.parity_combobox.addItem(from_utf8(''))
self.parity_combobox.addItem(from_utf8(''))
self.parity_combobox.addItem(from_utf8(''))
self.parity_combobox.addItem(from_utf8(''))
self.parity_combobox.addItem(from_utf8(''))
self.device_layout.addWidget(self.parity_combobox, 4, 1, 1, 1)
self.handshake_label = QtGui.QLabel(connect_dialog)
self.handshake_label.setObjectName(from_utf8('handshake_label'))
self.device_layout.addWidget(self.handshake_label, 0, 2, 1, 1)
self.software_checkbox = QtGui.QCheckBox(connect_dialog)
self.software_checkbox.setObjectName(from_utf8('software_checkbox'))
self.device_layout.addWidget(self.software_checkbox, 0, 3, 1, 1)
self.hardware_checkbox = QtGui.QCheckBox(connect_dialog)
self.hardware_checkbox.setObjectName(from_utf8('hardware_checkbox'))
self.device_layout.addWidget(self.hardware_checkbox, 1, 3, 1, 1)
self.open_mode_label = QtGui.QLabel(connect_dialog)
self.open_mode_label.setObjectName(from_utf8('open_mode_label'))
self.device_layout.addWidget(self.open_mode_label, 2, 2, 1, 1)
self.reading_checkbox = QtGui.QCheckBox(connect_dialog)
self.reading_checkbox.setChecked(True)
self.reading_checkbox.setObjectName(from_utf8('reading_checkbox'))
self.device_layout.addWidget(self.reading_checkbox, 2, 3, 1, 1)
self.writing_checkbox = QtGui.QCheckBox(connect_dialog)
self.writing_checkbox.setChecked(True)
self.writing_checkbox.setObjectName(from_utf8('writing_checkbox'))
self.device_layout.addWidget(self.writing_checkbox, 3, 3, 1, 1)
self.button_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
self.device_layout.addWidget(self.button_box, 5, 0, 1, 4)
def getBaud(self): self.retranslate_ui(connect_dialog)
return int(unicode(self.baudComboBox.currentText())) self.baud_combobox.setCurrentIndex(4)
self.button_box.accepted.connect(connect_dialog.accept)
self.button_box.rejected.connect(connect_dialog.reject)
def getDataBits(self): def retranslate_ui(self, connect_dialog):
return DATA_BITS[unicode(self.dataBitsComboBox.currentText())] """
Translate the user interface.
"""
connect_dialog.setWindowTitle(translate('ConnectDialog', 'Open Port'))
self.port_label.setText(translate('ConnectDialog', 'Port:'))
self.baud_label.setText(translate('ConnectDialog', 'Baude rate:'))
self.baud_combobox.setItemText(0, translate('ConnectDialog', '921600'))
self.baud_combobox.setItemText(1, translate('ConnectDialog', '576000'))
self.baud_combobox.setItemText(2, translate('ConnectDialog', '460800'))
self.baud_combobox.setItemText(3, translate('ConnectDialog', '230400'))
self.baud_combobox.setItemText(4, translate('ConnectDialog', '115200'))
self.baud_combobox.setItemText(5, translate('ConnectDialog', '57600'))
self.baud_combobox.setItemText(6, translate('ConnectDialog', '38400'))
self.baud_combobox.setItemText(7, translate('ConnectDialog', '19200'))
self.baud_combobox.setItemText(8, translate('ConnectDialog', '9600'))
self.baud_combobox.setItemText(9, translate('ConnectDialog', '4800'))
self.data_bits_label.setText(translate('ConnectDialog', 'Data bits:'))
self.data_bits_combobox.setItemText(0, translate('ConnectDialog', '8'))
self.data_bits_combobox.setItemText(1, translate('ConnectDialog', '7'))
self.data_bits_combobox.setItemText(2, translate('ConnectDialog', '6'))
self.data_bits_combobox.setItemText(3, translate('ConnectDialog', '5'))
self.stop_bits_label.setText(translate('ConnectDialog', 'Stop bits:'))
self.stop_bits_combobox.setItemText(0, translate('ConnectDialog', '1'))
self.stop_bits_combobox.setItemText(1, translate('ConnectDialog', '1.5'))
self.stop_bits_combobox.setItemText(2, translate('ConnectDialog', '2'))
self.parity_label.setText(translate('ConnectDialog', 'Parity:'))
self.parity_combobox.setItemText(0, translate('ConnectDialog', 'None'))
self.parity_combobox.setItemText(1, translate('ConnectDialog', 'Odd'))
self.parity_combobox.setItemText(2, translate('ConnectDialog', 'Even'))
self.parity_combobox.setItemText(3, translate('ConnectDialog', 'Mark'))
self.parity_combobox.setItemText(4, translate('ConnectDialog', 'Space'))
self.handshake_label.setText(translate('ConnectDialog', 'Handshake:'))
self.software_checkbox.setText(translate('ConnectDialog', 'Software'))
self.hardware_checkbox.setText(translate('ConnectDialog', 'Hardware'))
self.open_mode_label.setText(translate('ConnectDialog', 'Open mode:'))
self.reading_checkbox.setText(translate('ConnectDialog', 'Reading'))
self.writing_checkbox.setText(translate('ConnectDialog', 'Writing'))
def getStopBits(self):
return STOP_BITS[unicode(self.stopBitsComboBox.currentText())]
def getParity(self): class ConnectDialog(QtGui.QDialog, UiConnectDialog):
return PARITY_BITS[unicode(self.parityComboBox.currentText())]
def getSoftwareHandshake(self): def __init__(self, parent):
return self.softwareCheckBox.isChecked() #super(ConnectDialog, self).__init__()
QtGui.QDialog.__init__(self, parent=parent)
self.setup_ui(self)
def getHardwareHandshake(self): def get_port(self):
return self.hardwareCheckBox.isChecked() return unicode(self.port_edit.currentText())
def updatePortCombobox(self): def get_baud(self):
self.portEdit.clear() return int(unicode(self.baud_combobox.currentText()))
def get_data_bits(self):
return DATA_BITS[str(self.data_bits_combobox.currentText())]
def get_stop_bits(self):
return STOP_BITS[str(self.stop_bits_combobox.currentText())]
def get_parity(self):
return PARITY_BITS[str(self.parity_combobox.currentText())]
def get_software_handshake(self):
return self.software_checkbox.isChecked()
def get_hardware_handshake(self):
return self.hardware_checkbox.isChecked()
def update_port_combobox(self):
self.port_edit.clear()
ports = [] ports = []
for port in self._getSerialPorts(): for port in self._get_serial_ports():
ports.append(port) ports.append(port)
ports.sort() ports.sort()
self.portEdit.addItems(ports) self.port_edit.addItems(ports)
def _getSerialPorts(self): def _get_serial_ports(self):
""" """
Returns a generator for all available serial ports Returns a generator for all available serial ports
""" """

View File

@ -3,6 +3,7 @@ This module contains a child class of QWebView in order to reimplement the wheel
""" """
from PyQt4 import QtCore, QtWebKit from PyQt4 import QtCore, QtWebKit
class CWebView(QtWebKit.QWebView): class CWebView(QtWebKit.QWebView):
""" """
This is a reimplementation of QWebView in order to override the wheelEvent method. This is a reimplementation of QWebView in order to override the wheelEvent method.

View File

@ -5,284 +5,317 @@ import threading
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from serial import Serial, SerialException from serial import Serial, SerialException
from colourterm import SettingsDialog, ConnectDialog, SComboBox, Highlight, fromUtf8, translate, create_default_highlights from colourterm import SettingsDialog, ConnectDialog, SComboBox, Highlight, from_utf8, translate, \
create_default_highlights
from colourterm.cwebview import CWebView from colourterm.cwebview import CWebView
class Ui_MainWindow(object): class UiMainWindow(object):
def setupUi(self, mainWindow): def __init__(self):
mainWindow.setObjectName(fromUtf8('MainWindow')) """
mainWindow.resize(800, 600) Just to satisfy PEP8/PyLint
self.centralwidget = QtGui.QWidget(mainWindow) """
self.centralwidget.setObjectName(fromUtf8('centralwidget')) self.central_widget = None
self.centralLayout = QtGui.QVBoxLayout(self.centralwidget) self.central_layout = None
self.centralLayout.setSpacing(8) self.output_browser = None
self.centralLayout.setContentsMargins(0, 0, 0, 8) self.send_layout = None
self.centralLayout.setObjectName(fromUtf8('centralLayout')) self.send_combobox = None
self.outputBrowser = CWebView(self.centralwidget) self.send_button = None
self.outputBrowser.setHtml('<html><head><style>body { color: %s; font-family: monospace; margin: 0; padding: 0; }</style></head><body><pre></pre></body></html>' % str(QtGui.QApplication.palette().color(QtGui.QPalette.Text).name())) self.status_bar = None
self.outputBrowser.setObjectName(fromUtf8('outputBrowser')) self.tool_bar = None
self.centralLayout.addWidget(self.outputBrowser) self.open_action = None
self.sendLayout = QtGui.QHBoxLayout() self.close_action = None
self.sendLayout.setSpacing(8) self.capture_action = None
self.sendLayout.setObjectName(fromUtf8('sendLayout')) self.follow_action = None
self.sendComboBox = SComboBox(self.centralwidget) self.configure_action = None
self.sendComboBox.setEditable(True) self.exit_action = None
self.sendComboBox.setEnabled(False)
self.sendComboBox.setObjectName(fromUtf8('sendComboBox'))
self.sendLayout.addWidget(self.sendComboBox)
self.sendButton = QtGui.QPushButton(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sendButton.sizePolicy().hasHeightForWidth())
self.sendButton.setSizePolicy(sizePolicy)
self.sendButton.setMaximumSize(QtCore.QSize(100, 16777215))
self.sendButton.setObjectName(fromUtf8('sendButton'))
self.sendLayout.addWidget(self.sendButton)
self.centralLayout.addLayout(self.sendLayout)
mainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(mainWindow)
self.statusbar.setObjectName(fromUtf8('statusbar'))
mainWindow.setStatusBar(self.statusbar)
self.toolBar = QtGui.QToolBar(mainWindow)
self.toolBar.setMovable(False)
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.toolBar.setFloatable(False)
self.toolBar.setObjectName(fromUtf8('toolBar'))
mainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.openAction = QtGui.QAction(mainWindow)
connectIcon = QtGui.QIcon()
connectIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/network-connect.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.openAction.setIcon(connectIcon)
self.openAction.setObjectName(fromUtf8('openAction'))
self.closeAction = QtGui.QAction(mainWindow)
disconnectIcon = QtGui.QIcon()
disconnectIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/network-disconnect.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.closeAction.setIcon(disconnectIcon)
self.closeAction.setObjectName(fromUtf8('closeAction'))
self.captureAction = QtGui.QAction(mainWindow)
captureIcon = QtGui.QIcon()
captureIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/capture-to-disk.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.captureAction.setIcon(captureIcon)
self.captureAction.setCheckable(True)
self.captureAction.setChecked(False)
self.captureAction.setObjectName(fromUtf8('captureAction'))
self.followAction = QtGui.QAction(mainWindow)
self.followAction.setShortcut(QtCore.Qt.Key_F)
followIcon = QtGui.QIcon()
followIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/follow-output.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.followAction.setIcon(followIcon)
self.followAction.setCheckable(True)
self.followAction.setChecked(True)
self.followAction.setObjectName(fromUtf8('followAction'))
self.configureAction = QtGui.QAction(mainWindow)
configureIcon = QtGui.QIcon()
configureIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/configure.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.configureAction.setIcon(configureIcon)
self.configureAction.setObjectName(fromUtf8('configureAction'))
self.exitAction = QtGui.QAction(mainWindow)
exitIcon = QtGui.QIcon()
exitIcon.addPixmap(QtGui.QPixmap(fromUtf8(':/toolbar/application-exit.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.exitAction.setIcon(exitIcon)
self.exitAction.setObjectName(fromUtf8('exitAction'))
self.toolBar.addAction(self.openAction)
self.toolBar.addAction(self.closeAction)
self.toolBar.addAction(self.captureAction)
self.toolBar.addAction(self.followAction)
self.toolBar.addAction(self.configureAction)
self.toolBar.addAction(self.exitAction)
self.retranslateUi(mainWindow) def setup_ui(self, main_window):
QtCore.QMetaObject.connectSlotsByName(mainWindow) """
Set up the user interface
"""
main_window.setObjectName(from_utf8('MainWindow'))
main_window.resize(800, 600)
self.central_widget = QtGui.QWidget(main_window)
self.central_widget.setObjectName(from_utf8('central_widget'))
self.central_layout = QtGui.QVBoxLayout(self.central_widget)
self.central_layout.setSpacing(8)
self.central_layout.setContentsMargins(0, 0, 0, 8)
self.central_layout.setObjectName(from_utf8('centralLayout'))
self.output_browser = CWebView(self.central_widget)
self.output_browser.setHtml('<html><head><style>body { color: %s; font-family: monospace; margin: 0; '
'padding: 0; }</style></head><body><pre></pre></body></html>' %
str(QtGui.QApplication.palette().color(QtGui.QPalette.Text).name()))
self.output_browser.setObjectName(from_utf8('outputBrowser'))
self.central_layout.addWidget(self.output_browser)
self.send_layout = QtGui.QHBoxLayout()
self.send_layout.setSpacing(8)
self.send_layout.setObjectName(from_utf8('sendLayout'))
self.send_combobox = SComboBox(self.central_widget)
self.send_combobox.setEditable(True)
self.send_combobox.setEnabled(False)
self.send_combobox.setObjectName(from_utf8('sendComboBox'))
self.send_layout.addWidget(self.send_combobox)
self.send_button = QtGui.QPushButton(self.central_widget)
size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
size_policy.setHorizontalStretch(0)
size_policy.setVerticalStretch(0)
size_policy.setHeightForWidth(self.send_button.sizePolicy().hasHeightForWidth())
self.send_button.setSizePolicy(size_policy)
self.send_button.setMaximumSize(QtCore.QSize(100, 16777215))
self.send_button.setObjectName(from_utf8('sendButton'))
self.send_layout.addWidget(self.send_button)
self.central_layout.addLayout(self.send_layout)
main_window.setCentralWidget(self.central_widget)
self.status_bar = QtGui.QStatusBar(main_window)
self.status_bar.setObjectName(from_utf8('status_bar'))
main_window.setStatusBar(self.status_bar)
self.tool_bar = QtGui.QToolBar(main_window)
self.tool_bar.setMovable(False)
self.tool_bar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.tool_bar.setFloatable(False)
self.tool_bar.setObjectName(from_utf8('tool_bar'))
main_window.addToolBar(QtCore.Qt.TopToolBarArea, self.tool_bar)
self.open_action = QtGui.QAction(main_window)
connect_icon = QtGui.QIcon()
connect_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/network-connect.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.open_action.setIcon(connect_icon)
self.open_action.setObjectName(from_utf8('open_action'))
self.close_action = QtGui.QAction(main_window)
disconnect_icon = QtGui.QIcon()
disconnect_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/network-disconnect.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.close_action.setIcon(disconnect_icon)
self.close_action.setObjectName(from_utf8('close_action'))
self.capture_action = QtGui.QAction(main_window)
capture_icon = QtGui.QIcon()
capture_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/capture-to-disk.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.capture_action.setIcon(capture_icon)
self.capture_action.setCheckable(True)
self.capture_action.setChecked(False)
self.capture_action.setObjectName(from_utf8('capture_action'))
self.follow_action = QtGui.QAction(main_window)
self.follow_action.setShortcut(QtCore.Qt.Key_F)
follow_icon = QtGui.QIcon()
follow_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/follow-output.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.follow_action.setIcon(follow_icon)
self.follow_action.setCheckable(True)
self.follow_action.setChecked(True)
self.follow_action.setObjectName(from_utf8('follow_action'))
self.configure_action = QtGui.QAction(main_window)
configure_icon = QtGui.QIcon()
configure_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/configure.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.configure_action.setIcon(configure_icon)
self.configure_action.setObjectName(from_utf8('configure_action'))
self.exit_action = QtGui.QAction(main_window)
exit_icon = QtGui.QIcon()
exit_icon.addPixmap(QtGui.QPixmap(from_utf8(':/toolbar/application-exit.png')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.exit_action.setIcon(exit_icon)
self.exit_action.setObjectName(from_utf8('exit_action'))
self.tool_bar.addAction(self.open_action)
self.tool_bar.addAction(self.close_action)
self.tool_bar.addAction(self.capture_action)
self.tool_bar.addAction(self.follow_action)
self.tool_bar.addAction(self.configure_action)
self.tool_bar.addAction(self.exit_action)
def retranslateUi(self, mainWindow): self.retranslate_ui(main_window)
mainWindow.setWindowTitle(translate('MainWindow', 'ColourTerm'))
self.sendButton.setText(translate('MainWindow', 'Send')) def retranslate_ui(self, main_window):
self.toolBar.setWindowTitle(translate('MainWindow', 'toolBar')) """
self.openAction.setText(translate('MainWindow', 'Open...')) Translate the user interface
self.openAction.setToolTip(translate('MainWindow', 'Open...')) """
self.closeAction.setText(translate('MainWindow', 'Close')) main_window.setWindowTitle(translate('MainWindow', 'ColourTerm'))
self.closeAction.setToolTip(translate('MainWindow', 'Close')) self.send_button.setText(translate('MainWindow', 'Send'))
self.captureAction.setText(translate('MainWindow', 'Capture')) self.tool_bar.setWindowTitle(translate('MainWindow', 'tool_bar'))
self.captureAction.setToolTip(translate('MainWindow', 'Capture to File')) self.open_action.setText(translate('MainWindow', 'Open...'))
self.followAction.setText(translate('MainWindow', '&Follow')) self.open_action.setToolTip(translate('MainWindow', 'Open...'))
self.configureAction.setText(translate('MainWindow', 'Configure...')) self.close_action.setText(translate('MainWindow', 'Close'))
self.configureAction.setToolTip(translate('MainWindow', 'Configure...')) self.close_action.setToolTip(translate('MainWindow', 'Close'))
self.exitAction.setText(translate('MainWindow', 'Exit')) self.capture_action.setText(translate('MainWindow', 'Capture'))
self.capture_action.setToolTip(translate('MainWindow', 'Capture to File'))
self.follow_action.setText(translate('MainWindow', '&Follow'))
self.configure_action.setText(translate('MainWindow', 'Configure...'))
self.configure_action.setToolTip(translate('MainWindow', 'Configure...'))
self.exit_action.setText(translate('MainWindow', 'Exit'))
class MainWindow(QtGui.QMainWindow, Ui_MainWindow): class MainWindow(QtGui.QMainWindow, UiMainWindow):
updateOutput = QtCore.pyqtSignal(str) updateOutput = QtCore.pyqtSignal(str)
def __init__(self): def __init__(self):
QtGui.QMainWindow.__init__(self) super(MainWindow, self).__init__()
self.terminalLines = [] self.terminal_lines = []
self.maxLines = 5000 self.max_lines = 5000
self.setupUi(self) self.setup_ui(self)
self.device = None self.device = None
self.deviceClosed = True self.device_closed = True
self.followOutput = True self.follow_output = True
self.captureFile = None self.capture_file = None
self.captureFileName = u'' self.capture_filename = u''
self.highlights = self.loadHighlights() self.highlights = self.load_highlights()
if not self.highlights: if not self.highlights:
self.highlights = create_default_highlights() self.highlights = create_default_highlights()
self.settingsDialog = SettingsDialog() self.settings_dialog = SettingsDialog()
self.connectDialog = ConnectDialog() self.connect_dialog = ConnectDialog(self)
self.openAction.triggered.connect(self.onOpenActionTriggered) self.open_action.triggered.connect(self.on_open_action_triggered)
self.closeAction.triggered.connect(self.onCloseActionTriggered) self.close_action.triggered.connect(self.on_close_action_triggered)
self.captureAction.toggled.connect(self.onCaptureActionToggled) self.capture_action.toggled.connect(self.on_capture_action_toggled)
self.followAction.toggled.connect(self.onFollowActionToggled) self.follow_action.toggled.connect(self.on_follow_action_toggled)
self.configureAction.triggered.connect(self.onConfigureActionTriggered) self.configure_action.triggered.connect(self.on_configure_action_triggered)
self.exitAction.triggered.connect(self.close) self.exit_action.triggered.connect(self.close)
self.sendComboBox.keyPressed.connect(self.onSendComboBoxKeyPressed) self.send_combobox.keyPressed.connect(self.on_send_combobox_key_pressed)
self.sendButton.clicked.connect(self.onSendButtonClicked) self.send_button.clicked.connect(self.on_send_button_clicked)
self.outputBrowser.page().mainFrame().contentsSizeChanged.connect(self.onContentsSizeChanged) self.output_browser.page().mainFrame().contentsSizeChanged.connect(self.on_contents_size_changed)
self.outputBrowser.onScroll.connect(self.onOutputBrowserScrolled) self.output_browser.onScroll.connect(self.on_output_browser_scrolled)
self.updateOutput.connect(self.onUpdateOutput) self.updateOutput.connect(self.on_update_output)
def close(self): def close(self):
if not self.deviceClosed: if not self.device_closed:
self.deviceClosed = True self.device_closed = True
if self.captureFile: if self.capture_file:
self.captureFile.flush() self.capture_file.flush()
self.captureFile.close() self.capture_file.close()
return QtGui.QMainWindow.close(self) return QtGui.QMainWindow.close(self)
def documentBody(self): def document_body(self):
return self.outputBrowser.page().mainFrame().documentElement().findFirst('pre') return self.output_browser.page().mainFrame().documentElement().findFirst('pre')
def receiveText(self): def receive_text(self):
output = '' output = ''
while not self.deviceClosed: while not self.device_closed:
output += self.device.read(1) output += self.device.read(1)
if output.endswith('\r\n'): if output.endswith('\r\n'):
#self.terminalLines.append(output.strip('\r\n')) #self.terminal_lines.append(output.strip('\r\n'))
#if len(self.terminalLines) > self.maxLines: #if len(self.terminal_lines) > self.max_lines:
# self.terminalLines = self.terminalLines[-self.maxLines:] # self.terminal_lines = self.terminal_lines[-self.max_lines:]
# self.refreshOutput() # self.refreshOutput()
#else: #else:
self.updateOutput.emit(output.strip('\r\n')) self.updateOutput.emit(output.strip('\r\n'))
output = '' output = ''
def onOpenActionTriggered(self): def on_open_action_triggered(self):
self.connectDialog.updatePortCombobox() self.connect_dialog.update_port_combobox()
if self.connectDialog.exec_() == QtGui.QDialog.Accepted: if self.connect_dialog.exec_() == QtGui.QDialog.Accepted:
if not self.deviceClosed: if not self.device_closed:
self.deviceClosed = True self.device_closed = True
self.device.close() self.device.close()
try: try:
self.device = Serial( self.device = Serial(
port=self.connectDialog.getPort(), port=self.connect_dialog.get_port(),
baudrate=self.connectDialog.getBaud(), baudrate=self.connect_dialog.get_baud(),
bytesize=self.connectDialog.getDataBits(), bytesize=self.connect_dialog.get_data_bits(),
parity=self.connectDialog.getParity(), parity=self.connect_dialog.get_parity(),
stopbits=self.connectDialog.getStopBits(), stopbits=self.connect_dialog.get_stop_bits(),
timeout=0, timeout=0,
xonxoff=self.connectDialog.getSoftwareHandshake(), xonxoff=self.connect_dialog.get_software_handshake(),
rtscts=self.connectDialog.getHardwareHandshake(), rtscts=self.connect_dialog.get_hardware_handshake(),
dsrdtr=None dsrdtr=None
) )
self.deviceClosed = False self.device_closed = False
if not self.device.isOpen(): if not self.device.isOpen():
self.device.open() self.device.open()
outputThread = threading.Thread(target=self.receiveText) output_thread = threading.Thread(target=self.receive_text)
outputThread.start() output_thread.start()
except SerialException, e: except SerialException as e:
QtGui.QMessageBox.critical(self, 'Error opening port', e.args[0]) QtGui.QMessageBox.critical(self, 'Error opening port', e.args[0])
self.sendComboBox.setEnabled(not self.deviceClosed) self.send_combobox.setEnabled(not self.device_closed)
if self.sendComboBox.isEnabled(): if self.send_combobox.isEnabled():
self.sendComboBox.setFocus() self.send_combobox.setFocus()
def onCloseActionTriggered(self): def on_close_action_triggered(self):
self.deviceClosed = True self.device_closed = True
if self.device.isOpen(): if self.device.isOpen():
self.device.close() self.device.close()
self.sendComboBox.setEnabled(not self.deviceClosed) self.send_combobox.setEnabled(not self.device_closed)
def onCaptureActionToggled(self, enabled): def on_capture_action_toggled(self, enabled):
if enabled and not self.captureFile: if enabled and not self.capture_file:
if self.captureFileName: if self.capture_filename:
baseDir = os.path.basename(self.captureFileName) base_dir = os.path.basename(self.capture_filename)
else: else:
baseDir = u'' base_dir = u''
self.captureFileName = QtGui.QFileDialog.getSaveFileName(self, u'Capture To File', self.capture_filename = QtGui.QFileDialog.getSaveFileName(self, u'Capture To File', base_dir,
baseDir, u'Text files (*.txt *.log);;All files (*)') u'Text files (*.txt *.log);;All files (*)')
self.captureFile = open(self.captureFileName, u'w') self.capture_file = open(self.capture_filename, u'w')
self.statusbar.showMessage(self.captureFileName) self.status_bar.showMessage(self.capture_filename)
elif self.captureFile and not enabled: elif self.capture_file and not enabled:
self.captureFileName = u'' self.capture_filename = u''
self.captureFile.flush() self.capture_file.flush()
self.captureFile.close() self.capture_file.close()
self.captureFile = None self.capture_file = None
self.statusbar.clearMessage() self.status_bar.clearMessage()
def onFollowActionToggled(self, enabled): def on_follow_action_toggled(self, enabled):
self.followOutput = enabled self.follow_output = enabled
if enabled: if enabled:
self.outputBrowser.page().mainFrame().scroll(0, self.output_browser.page().mainFrame().scroll(
self.outputBrowser.page().mainFrame().contentsSize().height()) 0, self.output_browser.page().mainFrame().contentsSize().height())
def onConfigureActionTriggered(self): def on_configure_action_triggered(self):
self.settingsDialog.setHighlights(self.highlights) self.settings_dialog.set_highlights(self.highlights)
self.settingsDialog.exec_() self.settings_dialog.exec_()
self.highlights = self.settingsDialog.highlights() self.highlights = self.settings_dialog.highlights()
self.saveHighlights(self.highlights) self.save_highlights(self.highlights)
self.refreshOutput() self.refresh_output()
def onSendComboBoxKeyPressed(self, key): def on_send_combobox_key_pressed(self, key):
if key == QtCore.Qt.Key_Return or key == QtCore.Qt.Key_Enter: if key == QtCore.Qt.Key_Return or key == QtCore.Qt.Key_Enter:
self.onSendButtonClicked() self.on_send_button_clicked()
def onSendButtonClicked(self): def on_send_button_clicked(self):
if self.device.isOpen(): if self.device.isOpen():
output = str(self.sendComboBox.currentText()) output = str(self.send_combobox.currentText())
self.sendComboBox.insertItem(0, output) self.send_combobox.insertItem(0, output)
self.sendComboBox.setCurrentIndex(0) self.send_combobox.setCurrentIndex(0)
self.sendComboBox.clearEditText() self.send_combobox.clearEditText()
self.device.write(output + '\r\n') self.device.write(output + '\r\n')
def onContentsSizeChanged(self, size): def on_contents_size_changed(self, size):
if self.followOutput: if self.follow_output:
self.outputBrowser.page().mainFrame().scroll(0, size.height()) self.output_browser.page().mainFrame().scroll(0, size.height())
self.outputBrowser.update() self.output_browser.update()
def onUpdateOutput(self, output): def on_update_output(self, output):
#self.terminalLines.append(output) #self.terminal_lines.append(output)
if self.captureFile: if self.capture_file:
self.captureFile.write(output + u'\n') self.capture_file.write(output + '\n')
self.captureFile.flush() self.capture_file.flush()
#if len(self.terminalLines) > 5000: #if len(self.terminal_lines) > 5000:
# self.terminalLines = self.terminalLines[-5000:] # self.terminal_lines = self.terminal_lines[-5000:]
# self.refreshOutput() # self.refreshOutput()
#else: #else:
output = self.styleOutput(output) output = self.style_output(output)
self.documentBody().appendInside(output) self.document_body().appendInside(output)
def onOutputBrowserScrolled(self): def on_output_browser_scrolled(self):
scrollValue = self.outputBrowser.page().mainFrame().scrollBarValue(QtCore.Qt.Vertical) scroll_value = self.output_browser.page().mainFrame().scrollBarValue(QtCore.Qt.Vertical)
scrollMax = self.outputBrowser.page().mainFrame().scrollBarMaximum(QtCore.Qt.Vertical) scroll_max = self.output_browser.page().mainFrame().scrollBarMaximum(QtCore.Qt.Vertical)
if scrollValue < scrollMax: if scroll_value < scroll_max:
self.onFollowActionToggled(False) self.on_follow_action_toggled(False)
self.followAction.setChecked(False) self.follow_action.setChecked(False)
else: else:
self.onFollowActionToggled(True) self.on_follow_action_toggled(True)
self.followAction.setChecked(True) self.follow_action.setChecked(True)
def refreshOutput(self): def refresh_output(self):
elements = self.outputBrowser.page().mainFrame().findAllElements('div') elements = self.output_browser.page().mainFrame().findAllElements('div')
lines = [unicode(element.toPlainText()) for element in elements] lines = [unicode(element.toPlainText()) for element in elements]
pre = self.outputBrowser.page().mainFrame().findFirstElement('pre') pre = self.output_browser.page().mainFrame().findFirstElement('pre')
pre.setInnerXml('') pre.setInnerXml('')
for line in lines: for line in lines:
output = self.styleOutput(line) output = self.style_output(line)
self.documentBody().appendInside(output) self.document_body().appendInside(output)
self.outputBrowser.page().mainFrame().scroll(0, self.outputBrowser.page().mainFrame().contentsSize().height()) self.output_browser.page().mainFrame().scroll(0, self.output_browser.page().mainFrame().contentsSize().height())
self.outputBrowser.update() self.output_browser.update()
def styleOutput(self, output): def style_output(self, output):
style = u'font-family: Ubuntu Mono; ' style = u'font-family: \'Ubuntu Mono\', monospace; '
if not output: if not output:
output = u'&nbsp;' output = u'&nbsp;'
for highlight in self.highlights: for highlight in self.highlights:
@ -301,7 +334,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
output = u'<div>%s</div>' % output output = u'<div>%s</div>' % output
return output return output
def saveHighlights(self, highlights): def save_highlights(self, highlights):
settings = QtCore.QSettings() settings = QtCore.QSettings()
settings.setValue(u'highlights/count', len(highlights)) settings.setValue(u'highlights/count', len(highlights))
for index, highlight in enumerate(highlights): for index, highlight in enumerate(highlights):
@ -315,17 +348,17 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
settings.remove(u'background') settings.remove(u'background')
settings.endGroup() settings.endGroup()
def loadHighlights(self): def load_highlights(self):
settings = QtCore.QSettings() settings = QtCore.QSettings()
highlight_count = settings.value(u'highlights/count', 0).toInt()[0] highlight_count = settings.value(u'highlights/count', 0).toInt()[0]
highlights = [] highlights = []
for index in range(highlight_count): for index in range(highlight_count):
settings.beginGroup('highlight-%s' % index) settings.beginGroup(u'highlight-%s' % index)
pattern = unicode(settings.value('pattern', '').toString()) pattern = unicode(settings.value(u'pattern', u'').toString())
foreground = unicode(settings.value('foreground', '').toString()) foreground = unicode(settings.value(u'foreground', u'').toString())
background = None background = None
if settings.contains('background'): if settings.contains(u'background'):
background = unicode(settings.value('background', '').toString()) background = unicode(settings.value(u'background', u'').toString())
settings.endGroup() settings.endGroup()
highlights.append(Highlight(pattern, foreground, background)) highlights.append(Highlight(pattern, foreground, background))
return highlights return highlights

View File

@ -1,224 +1,261 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from colourterm import Highlight, fromUtf8, translate from colourterm import Highlight, from_utf8, translate
class Ui_SettingsDialog(object):
def setupUi(self, settingsDialog): class UiSettingsDialog(object):
settingsDialog.setObjectName(fromUtf8('SettingsDialog')) def __init__(self):
settingsDialog.resize(587, 390) """
self.settingsLayout = QtGui.QVBoxLayout(settingsDialog) Just to satisfy PEP8/PyLint
self.settingsLayout.setSpacing(8) """
self.settingsLayout.setMargin(8) self.settings_layout = None
self.settingsLayout.setObjectName(fromUtf8('settingsLayout')) self.splitter = None
self.splitter = QtGui.QSplitter(settingsDialog) self.section_list_widget = None
self.section_stacked_widget = None
self.highlight_page = None
self.vertical_layout = None
self.highlight_list_widget = None
self.button_layout = None
self.up_button = None
self.down_button = None
self.add_button = None
self.edit_button = None
self.delete_button = None
self.save_button = None
self.discard_button = None
self.highlight_groupbox = None
self.highlight_layout = None
self.regex_label = None
self.regex_edit = None
self.foreground_label = None
self.fg_colour_button = None
self.background_checkbox = None
self.bg_colour_button = None
self.font_page = None
self.font_layout = None
self.font_label = None
self.font_combobox = None
self.size_label = None
self.size_spinbox = None
self.button_box = None
def setup_ui(self, settings_dialog):
settings_dialog.setObjectName(from_utf8('SettingsDialog'))
settings_dialog.resize(587, 390)
self.settings_layout = QtGui.QVBoxLayout(settings_dialog)
self.settings_layout.setSpacing(8)
self.settings_layout.setMargin(8)
self.settings_layout.setObjectName(from_utf8('settings_layout'))
self.splitter = QtGui.QSplitter(settings_dialog)
self.splitter.setOrientation(QtCore.Qt.Horizontal) self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName(fromUtf8('splitter')) self.splitter.setObjectName(from_utf8('splitter'))
self.sectionListWidget = QtGui.QListWidget(self.splitter) self.section_list_widget = QtGui.QListWidget(self.splitter)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding) size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0) size_policy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0) size_policy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sectionListWidget.sizePolicy().hasHeightForWidth()) size_policy.setHeightForWidth(self.section_list_widget.sizePolicy().hasHeightForWidth())
self.sectionListWidget.setSizePolicy(sizePolicy) self.section_list_widget.setSizePolicy(size_policy)
self.sectionListWidget.setMinimumSize(QtCore.QSize(100, 0)) self.section_list_widget.setMinimumSize(QtCore.QSize(100, 0))
self.sectionListWidget.setMaximumSize(QtCore.QSize(200, 16777215)) self.section_list_widget.setMaximumSize(QtCore.QSize(200, 16777215))
self.sectionListWidget.setObjectName(fromUtf8('sectionListWidget')) self.section_list_widget.setObjectName(from_utf8('section_list_widget'))
item = QtGui.QListWidgetItem() item = QtGui.QListWidgetItem()
self.sectionListWidget.addItem(item) self.section_list_widget.addItem(item)
item = QtGui.QListWidgetItem() item = QtGui.QListWidgetItem()
self.sectionListWidget.addItem(item) self.section_list_widget.addItem(item)
self.sectionStackedWidget = QtGui.QStackedWidget(self.splitter) self.section_stacked_widget = QtGui.QStackedWidget(self.splitter)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0) size_policy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0) size_policy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sectionStackedWidget.sizePolicy().hasHeightForWidth()) size_policy.setHeightForWidth(self.section_stacked_widget.sizePolicy().hasHeightForWidth())
self.sectionStackedWidget.setSizePolicy(sizePolicy) self.section_stacked_widget.setSizePolicy(size_policy)
self.sectionStackedWidget.setObjectName(fromUtf8('sectionStackedWidget')) self.section_stacked_widget.setObjectName(from_utf8('section_stacked_widget'))
self.highlightPage = QtGui.QWidget() self.highlight_page = QtGui.QWidget()
self.highlightPage.setObjectName(fromUtf8('highlightPage')) self.highlight_page.setObjectName(from_utf8('highlight_page'))
self.verticalLayout = QtGui.QVBoxLayout(self.highlightPage) self.vertical_layout = QtGui.QVBoxLayout(self.highlight_page)
self.verticalLayout.setObjectName(fromUtf8('verticalLayout')) self.vertical_layout.setObjectName(from_utf8('vertical_layout'))
self.highlightListWidget = QtGui.QListWidget(self.highlightPage) self.highlight_list_widget = QtGui.QListWidget(self.highlight_page)
self.highlightListWidget.setObjectName(fromUtf8('highlightListWidget')) self.highlight_list_widget.setObjectName(from_utf8('highlight_list_widget'))
self.verticalLayout.addWidget(self.highlightListWidget) self.vertical_layout.addWidget(self.highlight_list_widget)
self.buttonLayout = QtGui.QHBoxLayout() self.button_layout = QtGui.QHBoxLayout()
self.buttonLayout.setSpacing(8) self.button_layout.setSpacing(8)
self.buttonLayout.setContentsMargins(0, -1, -1, -1) self.button_layout.setContentsMargins(0, -1, -1, -1)
self.buttonLayout.setObjectName(fromUtf8('buttonLayout')) self.button_layout.setObjectName(from_utf8('button_layout'))
self.upButton = QtGui.QToolButton(self.highlightPage) self.up_button = QtGui.QToolButton(self.highlight_page)
self.upButton.setIcon(QtGui.QIcon(':/settings/move-up.png')) self.up_button.setIcon(QtGui.QIcon(':/settings/move-up.png'))
self.upButton.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly) self.up_button.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.upButton.setFocusPolicy(QtCore.Qt.NoFocus) self.up_button.setFocusPolicy(QtCore.Qt.NoFocus)
self.upButton.setObjectName(fromUtf8('upButton')) self.up_button.setObjectName(from_utf8('up_button'))
self.buttonLayout.addWidget(self.upButton) self.button_layout.addWidget(self.up_button)
self.downButton = QtGui.QToolButton(self.highlightPage) self.down_button = QtGui.QToolButton(self.highlight_page)
self.downButton.setIcon(QtGui.QIcon(':/settings/move-down.png')) self.down_button.setIcon(QtGui.QIcon(':/settings/move-down.png'))
self.downButton.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly) self.down_button.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.downButton.setFocusPolicy(QtCore.Qt.NoFocus) self.down_button.setFocusPolicy(QtCore.Qt.NoFocus)
self.downButton.setObjectName(fromUtf8('downButton')) self.down_button.setObjectName(from_utf8('down_button'))
self.buttonLayout.addWidget(self.downButton) self.button_layout.addWidget(self.down_button)
self.buttonLayout.addItem(QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)) self.button_layout.addItem(QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum))
self.addButton = QtGui.QPushButton(self.highlightPage) self.add_button = QtGui.QPushButton(self.highlight_page)
self.addButton.setIcon(QtGui.QIcon(':/settings/highlight-add.png')) self.add_button.setIcon(QtGui.QIcon(':/settings/highlight-add.png'))
self.addButton.setObjectName(fromUtf8('addButton')) self.add_button.setObjectName(from_utf8('add_button'))
self.buttonLayout.addWidget(self.addButton) self.button_layout.addWidget(self.add_button)
self.editButton = QtGui.QPushButton(self.highlightPage) self.edit_button = QtGui.QPushButton(self.highlight_page)
self.editButton.setIcon(QtGui.QIcon(':/settings/highlight-edit.png')) self.edit_button.setIcon(QtGui.QIcon(':/settings/highlight-edit.png'))
self.editButton.setObjectName(fromUtf8('editButton')) self.edit_button.setObjectName(from_utf8('edit_button'))
self.buttonLayout.addWidget(self.editButton) self.button_layout.addWidget(self.edit_button)
self.deleteButton = QtGui.QPushButton(self.highlightPage) self.delete_button = QtGui.QPushButton(self.highlight_page)
self.deleteButton.setIcon(QtGui.QIcon(':/settings/highlight-remove.png')) self.delete_button.setIcon(QtGui.QIcon(':/settings/highlight-remove.png'))
self.deleteButton.setObjectName(fromUtf8('deleteButton')) self.delete_button.setObjectName(from_utf8('delete_button'))
self.buttonLayout.addWidget(self.deleteButton) self.button_layout.addWidget(self.delete_button)
self.saveButton = QtGui.QPushButton(self.highlightPage) self.save_button = QtGui.QPushButton(self.highlight_page)
self.saveButton.setIcon(QtGui.QIcon(':/settings/highlight-save.png')) self.save_button.setIcon(QtGui.QIcon(':/settings/highlight-save.png'))
self.saveButton.setObjectName(fromUtf8('saveButton')) self.save_button.setObjectName(from_utf8('save_button'))
self.saveButton.setVisible(False) self.save_button.setVisible(False)
self.buttonLayout.addWidget(self.saveButton) self.button_layout.addWidget(self.save_button)
self.discardButton = QtGui.QPushButton(self.highlightPage) self.discard_button = QtGui.QPushButton(self.highlight_page)
self.discardButton.setIcon(QtGui.QIcon(':/settings/highlight-discard.png')) self.discard_button.setIcon(QtGui.QIcon(':/settings/highlight-discard.png'))
self.discardButton.setObjectName(fromUtf8('discardButton')) self.discard_button.setObjectName(from_utf8('discard_button'))
self.discardButton.setVisible(False) self.discard_button.setVisible(False)
self.buttonLayout.addWidget(self.discardButton) self.button_layout.addWidget(self.discard_button)
self.verticalLayout.addLayout(self.buttonLayout) self.vertical_layout.addLayout(self.button_layout)
self.highlightGroupBox = QtGui.QGroupBox(self.highlightPage) self.highlight_groupbox = QtGui.QGroupBox(self.highlight_page)
self.highlightGroupBox.setObjectName(fromUtf8('highlightGroupBox')) self.highlight_groupbox.setObjectName(from_utf8('highlight_groupbox'))
self.highlightLayout = QtGui.QFormLayout(self.highlightGroupBox) self.highlight_layout = QtGui.QFormLayout(self.highlight_groupbox)
self.highlightLayout.setMargin(8) self.highlight_layout.setMargin(8)
self.highlightLayout.setSpacing(8) self.highlight_layout.setSpacing(8)
self.highlightLayout.setObjectName(fromUtf8('highlightLayout')) self.highlight_layout.setObjectName(from_utf8('highlight_layout'))
self.regexLabel = QtGui.QLabel(self.highlightGroupBox) self.regex_label = QtGui.QLabel(self.highlight_groupbox)
self.regexLabel.setObjectName(fromUtf8('regexLabel')) self.regex_label.setObjectName(from_utf8('regex_label'))
self.highlightLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.regexLabel) self.highlight_layout.setWidget(0, QtGui.QFormLayout.LabelRole, self.regex_label)
self.regexEdit = QtGui.QLineEdit(self.highlightGroupBox) self.regex_edit = QtGui.QLineEdit(self.highlight_groupbox)
self.regexEdit.setObjectName(fromUtf8('regexEdit')) self.regex_edit.setObjectName(from_utf8('regex_edit'))
self.highlightLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.regexEdit) self.highlight_layout.setWidget(0, QtGui.QFormLayout.FieldRole, self.regex_edit)
self.foregroundLabel = QtGui.QLabel(self.highlightGroupBox) self.foreground_label = QtGui.QLabel(self.highlight_groupbox)
self.foregroundLabel.setObjectName(fromUtf8('foregroundLabel')) self.foreground_label.setObjectName(from_utf8('foreground_label'))
self.highlightLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.foregroundLabel) self.highlight_layout.setWidget(1, QtGui.QFormLayout.LabelRole, self.foreground_label)
self.fgColourButton = QtGui.QPushButton(self.highlightGroupBox) self.fg_colour_button = QtGui.QPushButton(self.highlight_groupbox)
self.fgColourButton.setText(fromUtf8('')) self.fg_colour_button.setText(from_utf8(''))
self.fgColourButton.setObjectName(fromUtf8('fgColourButton')) self.fg_colour_button.setObjectName(from_utf8('fg_colour_button'))
self.highlightLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.fgColourButton) self.highlight_layout.setWidget(1, QtGui.QFormLayout.FieldRole, self.fg_colour_button)
self.backgroundCheckBox = QtGui.QCheckBox(self.highlightGroupBox) self.background_checkbox = QtGui.QCheckBox(self.highlight_groupbox)
self.backgroundCheckBox.setObjectName(fromUtf8('backgroundCheckBox')) self.background_checkbox.setObjectName(from_utf8('background_checkbox'))
self.highlightLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.backgroundCheckBox) self.highlight_layout.setWidget(2, QtGui.QFormLayout.LabelRole, self.background_checkbox)
self.bgColourButton = QtGui.QPushButton(self.highlightGroupBox) self.bg_colour_button = QtGui.QPushButton(self.highlight_groupbox)
self.bgColourButton.setText(fromUtf8('')) self.bg_colour_button.setText(from_utf8(''))
self.bgColourButton.setObjectName(fromUtf8('bgColourButton')) self.bg_colour_button.setObjectName(from_utf8('bg_colour_button'))
self.highlightLayout.setWidget(2, QtGui.QFormLayout.FieldRole, self.bgColourButton) self.highlight_layout.setWidget(2, QtGui.QFormLayout.FieldRole, self.bg_colour_button)
self.verticalLayout.addWidget(self.highlightGroupBox) self.vertical_layout.addWidget(self.highlight_groupbox)
self.sectionStackedWidget.addWidget(self.highlightPage) self.section_stacked_widget.addWidget(self.highlight_page)
self.fontPage = QtGui.QWidget() self.font_page = QtGui.QWidget()
self.fontPage.setObjectName(fromUtf8('fontPage')) self.font_page.setObjectName(from_utf8('font_page'))
self.fontLayout = QtGui.QFormLayout(self.fontPage) self.font_layout = QtGui.QFormLayout(self.font_page)
self.fontLayout.setMargin(8) self.font_layout.setMargin(8)
self.fontLayout.setSpacing(8) self.font_layout.setSpacing(8)
self.fontLayout.setObjectName(fromUtf8('fontLayout')) self.font_layout.setObjectName(from_utf8('font_layout'))
self.fontLabel = QtGui.QLabel(self.fontPage) self.font_label = QtGui.QLabel(self.font_page)
self.fontLabel.setObjectName(fromUtf8('fontLabel')) self.font_label.setObjectName(from_utf8('font_label'))
self.fontLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.fontLabel) self.font_layout.setWidget(0, QtGui.QFormLayout.LabelRole, self.font_label)
self.fontComboBox = QtGui.QFontComboBox(self.fontPage) self.font_combobox = QtGui.QFontComboBox(self.font_page)
self.fontComboBox.setObjectName(fromUtf8('fontComboBox')) self.font_combobox.setObjectName(from_utf8('font_combobox'))
self.fontLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.fontComboBox) self.font_layout.setWidget(0, QtGui.QFormLayout.FieldRole, self.font_combobox)
self.sizeLabel = QtGui.QLabel(self.fontPage) self.size_label = QtGui.QLabel(self.font_page)
self.sizeLabel.setObjectName(fromUtf8('sizeLabel')) self.size_label.setObjectName(from_utf8('size_label'))
self.fontLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.sizeLabel) self.font_layout.setWidget(1, QtGui.QFormLayout.LabelRole, self.size_label)
self.sizeSpinBox = QtGui.QSpinBox(self.fontPage) self.size_spinbox = QtGui.QSpinBox(self.font_page)
self.sizeSpinBox.setReadOnly(True) self.size_spinbox.setReadOnly(True)
self.sizeSpinBox.setSpecialValueText(fromUtf8('')) self.size_spinbox.setSpecialValueText(from_utf8(''))
self.sizeSpinBox.setMinimum(6) self.size_spinbox.setMinimum(6)
self.sizeSpinBox.setMaximum(50) self.size_spinbox.setMaximum(50)
self.sizeSpinBox.setProperty('value', 12) self.size_spinbox.setProperty('value', 12)
self.sizeSpinBox.setObjectName(fromUtf8('sizeSpinBox')) self.size_spinbox.setObjectName(from_utf8('size_spinbox'))
self.fontLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.sizeSpinBox) self.font_layout.setWidget(1, QtGui.QFormLayout.FieldRole, self.size_spinbox)
self.sectionStackedWidget.addWidget(self.fontPage) self.section_stacked_widget.addWidget(self.font_page)
self.settingsLayout.addWidget(self.splitter) self.settings_layout.addWidget(self.splitter)
self.buttonBox = QtGui.QDialogButtonBox(settingsDialog) self.button_box = QtGui.QDialogButtonBox(settings_dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.button_box.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) self.button_box.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(fromUtf8('buttonBox')) self.button_box.setObjectName(from_utf8('button_box'))
self.settingsLayout.addWidget(self.buttonBox) self.settings_layout.addWidget(self.button_box)
self.retranslateUi(settingsDialog) self.retranslate_ui(settings_dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(fromUtf8('accepted()')), settingsDialog.accept) self.button_box.accepted.connect(settings_dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(fromUtf8('rejected()')), settingsDialog.reject) self.button_box.rejected.connect(settings_dialog.reject)
QtCore.QObject.connect(self.sectionListWidget, QtCore.SIGNAL(fromUtf8('currentRowChanged(int)')), self.sectionStackedWidget.setCurrentIndex) self.section_list_widget.currentRowChanged.connect(self.section_stacked_widget.setCurrentIndex)
QtCore.QMetaObject.connectSlotsByName(settingsDialog) settings_dialog.setTabOrder(self.section_list_widget, self.highlight_list_widget)
settingsDialog.setTabOrder(self.sectionListWidget, self.highlightListWidget) settings_dialog.setTabOrder(self.highlight_list_widget, self.add_button)
settingsDialog.setTabOrder(self.highlightListWidget, self.addButton) settings_dialog.setTabOrder(self.add_button, self.edit_button)
settingsDialog.setTabOrder(self.addButton, self.editButton) settings_dialog.setTabOrder(self.edit_button, self.delete_button)
settingsDialog.setTabOrder(self.editButton, self.deleteButton) settings_dialog.setTabOrder(self.delete_button, self.save_button)
settingsDialog.setTabOrder(self.deleteButton, self.saveButton) settings_dialog.setTabOrder(self.save_button, self.discard_button)
settingsDialog.setTabOrder(self.saveButton, self.discardButton) settings_dialog.setTabOrder(self.discard_button, self.button_box)
settingsDialog.setTabOrder(self.discardButton, self.buttonBox)
def retranslateUi(self, settingsDialog): def retranslate_ui(self, settings_dialog):
settingsDialog.setWindowTitle(translate('SettingsDialog', 'Settings')) """
__sortingEnabled = self.sectionListWidget.isSortingEnabled() Translate the user interface
self.sectionListWidget.setSortingEnabled(False) """
item = self.sectionListWidget.item(0) settings_dialog.setWindowTitle(translate('SettingsDialog', 'Settings'))
__sortingEnabled = self.section_list_widget.isSortingEnabled()
self.section_list_widget.setSortingEnabled(False)
item = self.section_list_widget.item(0)
item.setText(translate('SettingsDialog', 'Highlighting')) item.setText(translate('SettingsDialog', 'Highlighting'))
item = self.sectionListWidget.item(1) item = self.section_list_widget.item(1)
item.setText(translate('SettingsDialog', 'Font')) item.setText(translate('SettingsDialog', 'Font'))
self.sectionListWidget.setSortingEnabled(__sortingEnabled) self.section_list_widget.setSortingEnabled(__sortingEnabled)
self.upButton.setText(translate('SettingsDialog', 'Move Up')) self.up_button.setText(translate('SettingsDialog', 'Move Up'))
self.downButton.setText(translate('SettingsDialog', 'Move Down')) self.down_button.setText(translate('SettingsDialog', 'Move Down'))
self.addButton.setText(translate('SettingsDialog', 'Add')) self.add_button.setText(translate('SettingsDialog', 'Add'))
self.editButton.setText(translate('SettingsDialog', 'Edit')) self.edit_button.setText(translate('SettingsDialog', 'Edit'))
self.deleteButton.setText(translate('SettingsDialog', 'Delete')) self.delete_button.setText(translate('SettingsDialog', 'Delete'))
self.saveButton.setText(translate('SettingsDialog', 'Save')) self.save_button.setText(translate('SettingsDialog', 'Save'))
self.discardButton.setText(translate('SettingsDialog', 'Discard')) self.discard_button.setText(translate('SettingsDialog', 'Discard'))
self.highlightGroupBox.setTitle(translate('SettingsDialog', 'Highlight')) self.highlight_groupbox.setTitle(translate('SettingsDialog', 'Highlight'))
self.regexLabel.setText(translate('SettingsDialog', 'Regular Expression:')) self.regex_label.setText(translate('SettingsDialog', 'Regular Expression:'))
self.foregroundLabel.setText(translate('SettingsDialog', 'Foreground:')) self.foreground_label.setText(translate('SettingsDialog', 'Foreground:'))
self.backgroundCheckBox.setText(translate('SettingsDialog', 'Background:')) self.background_checkbox.setText(translate('SettingsDialog', 'Background:'))
self.fontLabel.setText(translate('SettingsDialog', 'Font:')) self.font_label.setText(translate('SettingsDialog', 'Font:'))
self.sizeLabel.setText(translate('SettingsDialog', 'Size:')) self.size_label.setText(translate('SettingsDialog', 'Size:'))
self.sizeSpinBox.setSuffix(translate('SettingsDialog', 'pt')) self.size_spinbox.setSuffix(translate('SettingsDialog', 'pt'))
class SettingsDialog(QtGui.QDialog, Ui_SettingsDialog): class SettingsDialog(QtGui.QDialog, UiSettingsDialog):
def __init__(self): def __init__(self):
QtGui.QDialog.__init__(self) QtGui.QDialog.__init__(self)
self.setupUi(self) self.setup_ui(self)
self._highlights = {} self._highlights = {}
self._currentHighlight = None self._current_highlight = None
self._currentRow = -1 self._current_row = -1
self.highlightListWidget.itemSelectionChanged.connect(self.onHighlightItemSelected) self.highlight_list_widget.itemSelectionChanged.connect(self.on_highlight_item_selected)
self.regexEdit.textEdited.connect(self.onRegexEditTextEdited) self.regex_edit.textEdited.connect(self.on_regex_edit_text_edited)
self.fgColourButton.clicked.connect(self.onFgColourButtonClicked) self.fg_colour_button.clicked.connect(self.on_fg_colour_button_clicked)
self.backgroundCheckBox.toggled.connect(self.onBackgroundCheckboxToggled) self.background_checkbox.toggled.connect(self.on_background_checkbox_toggled)
self.bgColourButton.clicked.connect(self.onBgColourButtonClicked) self.bg_colour_button.clicked.connect(self.on_bg_colour_button_clicked)
self.upButton.clicked.connect(self.onUpButtonClicked) self.up_button.clicked.connect(self.on_up_button_clicked)
self.downButton.clicked.connect(self.onDownButtonClicked) self.down_button.clicked.connect(self.on_down_button_clicked)
self.addButton.clicked.connect(self.onAddButtonClicked) self.add_button.clicked.connect(self.on_add_button_clicked)
self.editButton.clicked.connect(self.onEditButtonClicked) self.edit_button.clicked.connect(self.on_edit_button_clicked)
self.deleteButton.clicked.connect(self.onDeleteButtonClicked) self.delete_button.clicked.connect(self.on_delete_button_clicked)
self.saveButton.clicked.connect(self.onSaveButtonClicked) self.save_button.clicked.connect(self.on_save_button_clicked)
self.discardButton.clicked.connect(self.onDiscardButtonClicked) self.discard_button.clicked.connect(self.on_discard_button_clicked)
def _setCurrentHighlight(self): def _set_current_highlight(self):
self.regexEdit.setText(self._currentHighlight.pattern) self.regex_edit.setText(self._current_highlight.pattern)
if self._currentHighlight.background: if self._current_highlight.background:
self.backgroundCheckBox.setChecked(True) self.background_checkbox.setChecked(True)
self.bgColourButton.setEnabled(True) self.bg_colour_button.setEnabled(True)
self.bgColourButton.setStyleSheet('background-color: %s' % self._currentHighlight.background) self.bg_colour_button.setStyleSheet('background-color: %s' % self._current_highlight.background)
else: else:
self.backgroundCheckBox.setChecked(False) self.background_checkbox.setChecked(False)
self.bgColourButton.setEnabled(False) self.bg_colour_button.setEnabled(False)
self.bgColourButton.setStyleSheet('') self.bg_colour_button.setStyleSheet('')
self.fgColourButton.setStyleSheet('background-color: %s' % self._currentHighlight.foreground) self.fg_colour_button.setStyleSheet('background-color: %s' % self._current_highlight.foreground)
self.setEditMode(True) self.set_edit_mode(True)
def highlights(self): def highlights(self):
return self._highlights.values() return self._highlights.values()
def setHighlights(self, highlights): def set_highlights(self, highlights):
self._highlights = {} self._highlights = {}
self.highlightListWidget.clear() self.highlight_list_widget.clear()
for index, highlight in enumerate(highlights): for index, highlight in enumerate(highlights):
self._highlights[index] = highlight self._highlights[index] = highlight
item = QtGui.QListWidgetItem() item = QtGui.QListWidgetItem()
@ -226,118 +263,116 @@ class SettingsDialog(QtGui.QDialog, Ui_SettingsDialog):
item.setBackgroundColor(QtGui.QColor(highlight.background)) item.setBackgroundColor(QtGui.QColor(highlight.background))
item.setTextColor(QtGui.QColor(highlight.foreground)) item.setTextColor(QtGui.QColor(highlight.foreground))
item.setText(highlight.pattern) item.setText(highlight.pattern)
self.highlightListWidget.addItem(item) self.highlight_list_widget.addItem(item)
def onHighlightItemSelected(self): def on_highlight_item_selected(self):
self._currentRow = self.highlightListWidget.currentRow() self._current_row = self.highlight_list_widget.currentRow()
if self._currentRow > -1: if self._current_row > -1:
self._currentHighlight = self._highlights[self._currentRow] self._current_highlight = self._highlights[self._current_row]
def onRegexEditTextEdited(self): def on_regex_edit_text_edited(self):
self._currentHighlight.setPattern(unicode(self.regexEdit.text())) self._current_highlight.set_pattern(unicode(self.regex_edit.text()))
def onBackgroundCheckboxToggled(self, checked): def on_background_checkbox_toggled(self, checked):
self.bgColourButton.setEnabled(checked) self.bg_colour_button.setEnabled(checked)
if not checked: if not checked:
self._currentHighlight.background = None self._current_highlight.background = None
def onFgColourButtonClicked(self): def on_fg_colour_button_clicked(self):
colour = QtGui.QColor(self._currentHighlight.foreground) colour = QtGui.QColor(self._current_highlight.foreground)
colour = QtGui.QColorDialog.getColor(colour, self) colour = QtGui.QColorDialog.getColor(colour, self)
if colour.isValid(): if colour.isValid():
self.fgColourButton.setStyleSheet('background-color: %s' % colour.name()) self.fg_colour_button.setStyleSheet('background-color: %s' % colour.name())
self._currentHighlight.foreground = colour.name() self._current_highlight.foreground = colour.name()
def onBgColourButtonClicked(self): def on_bg_colour_button_clicked(self):
if self._currentHighlight.background: if self._current_highlight.background:
colour = QtGui.QColor(self._currentHighlight.background) colour = QtGui.QColor(self._current_highlight.background)
colour = QtGui.QColorDialog.getColor(colour, self) colour = QtGui.QColorDialog.getColor(colour, self)
else: else:
colour = QtGui.QColorDialog.getColor(QtCore.Qt.white, self) colour = QtGui.QColorDialog.getColor(QtCore.Qt.white, self)
if colour.isValid(): if colour.isValid():
self.bgColourButton.setStyleSheet('background-color: %s' % colour.name()) self.bg_colour_button.setStyleSheet('background-color: %s' % colour.name())
self._currentHighlight.background = colour.name() self._current_highlight.background = colour.name()
def onUpButtonClicked(self): def on_up_button_clicked(self):
if self._currentRow == 0: if self._current_row == 0:
return return
row = self._currentRow row = self._current_row
item = self.highlightListWidget.takeItem(row) item = self.highlight_list_widget.takeItem(row)
self.highlightListWidget.insertItem(row - 1, item) self.highlight_list_widget.insertItem(row - 1, item)
self._highlights[row - 1], self._highlights[row] = \ self._highlights[row - 1], self._highlights[row] = \
self._highlights[row], self._highlights[row - 1] self._highlights[row], self._highlights[row - 1]
self.highlightListWidget.setCurrentRow(row - 1) self.highlight_list_widget.setCurrentRow(row - 1)
self.onHighlightItemSelected() self.on_highlight_item_selected()
def onDownButtonClicked(self): def on_down_button_clicked(self):
if self._currentRow == len(self._highlights) - 1: if self._current_row == len(self._highlights) - 1:
return return
row = self._currentRow row = self._current_row
item = self.highlightListWidget.takeItem(row) item = self.highlight_list_widget.takeItem(row)
self.highlightListWidget.insertItem(row + 1, item) self.highlight_list_widget.insertItem(row + 1, item)
self._highlights[row + 1], self._highlights[row] = \ self._highlights[row + 1], self._highlights[row] = \
self._highlights[row], self._highlights[row + 1] self._highlights[row], self._highlights[row + 1]
self.highlightListWidget.setCurrentRow(row + 1) self.highlight_list_widget.setCurrentRow(row + 1)
self.onHighlightItemSelected() self.on_highlight_item_selected()
def onAddButtonClicked(self): def on_add_button_clicked(self):
self._currentRow = -1 self._current_row = -1
self._currentHighlight = Highlight(u'', QtGui.QColor(QtGui.QPalette.WindowText).name()) self._current_highlight = Highlight(u'', QtGui.QColor(QtGui.QPalette.WindowText).name())
self._setCurrentHighlight() self._set_current_highlight()
self.setEditMode(True) self.set_edit_mode(True)
def onEditButtonClicked(self): def on_edit_button_clicked(self):
self._setCurrentHighlight() self._set_current_highlight()
self.setEditMode(True) self.set_edit_mode(True)
def onDeleteButtonClicked(self): def on_delete_button_clicked(self):
if QtGui.QMessageBox.question(self, if QtGui.QMessageBox.question(self, translate('SettingsDialog', 'Confirm Delete'),
translate('SettingsDialog', 'Confirm Delete'), translate('SettingsDialog', 'Are you sure you want to delete this highlight?'),
translate('SettingsDialog', 'Are you sure you want to delete '
'this highlight?'),
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes: QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
del self._highlights[self._currentRow] del self._highlights[self._current_row]
self.highlightListWidget.takeItem(self._currentRow) self.highlight_list_widget.takeItem(self._current_row)
self._currentRow = -1 self._current_row = -1
self._currentHighlight = None self._current_highlight = None
def onSaveButtonClicked(self): def on_save_button_clicked(self):
if self._currentRow < 0: if self._current_row < 0:
new_index = len(self._highlights) new_index = len(self._highlights)
self._highlights[new_index] = self._currentHighlight self._highlights[new_index] = self._current_highlight
item = QtGui.QListWidgetItem() item = QtGui.QListWidgetItem()
if self._currentHighlight.background: if self._current_highlight.background:
item.setBackgroundColor(QtGui.QColor(self._currentHighlight.background)) item.setBackgroundColor(QtGui.QColor(self._current_highlight.background))
item.setTextColor(QtGui.QColor(self._currentHighlight.foreground)) item.setTextColor(QtGui.QColor(self._current_highlight.foreground))
item.setText(self._currentHighlight.pattern) item.setText(self._current_highlight.pattern)
self.highlightListWidget.addItem(item) self.highlight_list_widget.addItem(item)
self.highlightListWidget.setCurrentRow(new_index) self.highlight_list_widget.setCurrentRow(new_index)
else: else:
self._highlights[self._currentRow] = self._currentHighlight self._highlights[self._current_row] = self._current_highlight
self.setEditMode(False) self.set_edit_mode(False)
def onDiscardButtonClicked(self): def on_discard_button_clicked(self):
self.setEditMode(False) self.set_edit_mode(False)
def setEditMode(self, enable): def set_edit_mode(self, enable):
for button in self.buttonBox.buttons(): for button in self.button_box.buttons():
button.setEnabled(not enable) button.setEnabled(not enable)
self.upButton.setVisible(not enable) self.up_button.setVisible(not enable)
self.downButton.setVisible(not enable) self.down_button.setVisible(not enable)
self.addButton.setVisible(not enable) self.add_button.setVisible(not enable)
self.editButton.setVisible(not enable) self.edit_button.setVisible(not enable)
self.deleteButton.setVisible(not enable) self.delete_button.setVisible(not enable)
self.saveButton.setVisible(enable) self.save_button.setVisible(enable)
self.discardButton.setVisible(enable) self.discard_button.setVisible(enable)
self.highlightListWidget.setEnabled(not enable) self.highlight_list_widget.setEnabled(not enable)
self.regexEdit.setEnabled(enable) self.regex_edit.setEnabled(enable)
self.fgColourButton.setEnabled(enable) self.fg_colour_button.setEnabled(enable)
self.backgroundCheckBox.setEnabled(enable) self.background_checkbox.setEnabled(enable)
if enable and self.backgroundCheckBox.isChecked(): if enable and self.background_checkbox.isChecked():
self.bgColourButton.setEnabled(True) self.bg_colour_button.setEnabled(True)
else: else:
self.bgColourButton.setEnabled(False) self.bg_colour_button.setEnabled(False)
if not enable: if not enable:
self.regexEdit.clear() self.regex_edit.clear()
self.fgColourButton.setStyleSheet('') self.fg_colour_button.setStyleSheet('')
self.bgColourButton.setStyleSheet('') self.bg_colour_button.setStyleSheet('')