379 lines
20 KiB
Python
379 lines
20 KiB
Python
# -*- coding: utf-8 -*-
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
from colourterm import Highlight, from_utf8, translate
|
|
|
|
|
|
class UiSettingsDialog(object):
|
|
def __init__(self):
|
|
"""
|
|
Just to satisfy PEP8/PyLint
|
|
"""
|
|
self.settings_layout = None
|
|
self.splitter = None
|
|
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.setObjectName(from_utf8('splitter'))
|
|
self.section_list_widget = QtGui.QListWidget(self.splitter)
|
|
size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
|
|
size_policy.setHorizontalStretch(0)
|
|
size_policy.setVerticalStretch(0)
|
|
size_policy.setHeightForWidth(self.section_list_widget.sizePolicy().hasHeightForWidth())
|
|
self.section_list_widget.setSizePolicy(size_policy)
|
|
self.section_list_widget.setMinimumSize(QtCore.QSize(100, 0))
|
|
self.section_list_widget.setMaximumSize(QtCore.QSize(200, 16777215))
|
|
self.section_list_widget.setObjectName(from_utf8('section_list_widget'))
|
|
item = QtGui.QListWidgetItem()
|
|
self.section_list_widget.addItem(item)
|
|
item = QtGui.QListWidgetItem()
|
|
self.section_list_widget.addItem(item)
|
|
self.section_stacked_widget = QtGui.QStackedWidget(self.splitter)
|
|
size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
|
|
size_policy.setHorizontalStretch(0)
|
|
size_policy.setVerticalStretch(0)
|
|
size_policy.setHeightForWidth(self.section_stacked_widget.sizePolicy().hasHeightForWidth())
|
|
self.section_stacked_widget.setSizePolicy(size_policy)
|
|
self.section_stacked_widget.setObjectName(from_utf8('section_stacked_widget'))
|
|
self.highlight_page = QtGui.QWidget()
|
|
self.highlight_page.setObjectName(from_utf8('highlight_page'))
|
|
self.vertical_layout = QtGui.QVBoxLayout(self.highlight_page)
|
|
self.vertical_layout.setObjectName(from_utf8('vertical_layout'))
|
|
self.highlight_list_widget = QtGui.QListWidget(self.highlight_page)
|
|
self.highlight_list_widget.setObjectName(from_utf8('highlight_list_widget'))
|
|
self.vertical_layout.addWidget(self.highlight_list_widget)
|
|
self.button_layout = QtGui.QHBoxLayout()
|
|
self.button_layout.setSpacing(8)
|
|
self.button_layout.setContentsMargins(0, -1, -1, -1)
|
|
self.button_layout.setObjectName(from_utf8('button_layout'))
|
|
self.up_button = QtGui.QToolButton(self.highlight_page)
|
|
self.up_button.setIcon(QtGui.QIcon(':/settings/move-up.png'))
|
|
self.up_button.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
|
|
self.up_button.setFocusPolicy(QtCore.Qt.NoFocus)
|
|
self.up_button.setObjectName(from_utf8('up_button'))
|
|
self.button_layout.addWidget(self.up_button)
|
|
self.down_button = QtGui.QToolButton(self.highlight_page)
|
|
self.down_button.setIcon(QtGui.QIcon(':/settings/move-down.png'))
|
|
self.down_button.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
|
|
self.down_button.setFocusPolicy(QtCore.Qt.NoFocus)
|
|
self.down_button.setObjectName(from_utf8('down_button'))
|
|
self.button_layout.addWidget(self.down_button)
|
|
self.button_layout.addItem(QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum))
|
|
self.add_button = QtGui.QPushButton(self.highlight_page)
|
|
self.add_button.setIcon(QtGui.QIcon(':/settings/highlight-add.png'))
|
|
self.add_button.setObjectName(from_utf8('add_button'))
|
|
self.button_layout.addWidget(self.add_button)
|
|
self.edit_button = QtGui.QPushButton(self.highlight_page)
|
|
self.edit_button.setIcon(QtGui.QIcon(':/settings/highlight-edit.png'))
|
|
self.edit_button.setObjectName(from_utf8('edit_button'))
|
|
self.button_layout.addWidget(self.edit_button)
|
|
self.delete_button = QtGui.QPushButton(self.highlight_page)
|
|
self.delete_button.setIcon(QtGui.QIcon(':/settings/highlight-remove.png'))
|
|
self.delete_button.setObjectName(from_utf8('delete_button'))
|
|
self.button_layout.addWidget(self.delete_button)
|
|
self.save_button = QtGui.QPushButton(self.highlight_page)
|
|
self.save_button.setIcon(QtGui.QIcon(':/settings/highlight-save.png'))
|
|
self.save_button.setObjectName(from_utf8('save_button'))
|
|
self.save_button.setVisible(False)
|
|
self.button_layout.addWidget(self.save_button)
|
|
self.discard_button = QtGui.QPushButton(self.highlight_page)
|
|
self.discard_button.setIcon(QtGui.QIcon(':/settings/highlight-discard.png'))
|
|
self.discard_button.setObjectName(from_utf8('discard_button'))
|
|
self.discard_button.setVisible(False)
|
|
self.button_layout.addWidget(self.discard_button)
|
|
self.vertical_layout.addLayout(self.button_layout)
|
|
self.highlight_groupbox = QtGui.QGroupBox(self.highlight_page)
|
|
self.highlight_groupbox.setObjectName(from_utf8('highlight_groupbox'))
|
|
self.highlight_layout = QtGui.QFormLayout(self.highlight_groupbox)
|
|
self.highlight_layout.setMargin(8)
|
|
self.highlight_layout.setSpacing(8)
|
|
self.highlight_layout.setObjectName(from_utf8('highlight_layout'))
|
|
self.regex_label = QtGui.QLabel(self.highlight_groupbox)
|
|
self.regex_label.setObjectName(from_utf8('regex_label'))
|
|
self.highlight_layout.setWidget(0, QtGui.QFormLayout.LabelRole, self.regex_label)
|
|
self.regex_edit = QtGui.QLineEdit(self.highlight_groupbox)
|
|
self.regex_edit.setObjectName(from_utf8('regex_edit'))
|
|
self.highlight_layout.setWidget(0, QtGui.QFormLayout.FieldRole, self.regex_edit)
|
|
self.foreground_label = QtGui.QLabel(self.highlight_groupbox)
|
|
self.foreground_label.setObjectName(from_utf8('foreground_label'))
|
|
self.highlight_layout.setWidget(1, QtGui.QFormLayout.LabelRole, self.foreground_label)
|
|
self.fg_colour_button = QtGui.QPushButton(self.highlight_groupbox)
|
|
self.fg_colour_button.setText(from_utf8(''))
|
|
self.fg_colour_button.setObjectName(from_utf8('fg_colour_button'))
|
|
self.highlight_layout.setWidget(1, QtGui.QFormLayout.FieldRole, self.fg_colour_button)
|
|
self.background_checkbox = QtGui.QCheckBox(self.highlight_groupbox)
|
|
self.background_checkbox.setObjectName(from_utf8('background_checkbox'))
|
|
self.highlight_layout.setWidget(2, QtGui.QFormLayout.LabelRole, self.background_checkbox)
|
|
self.bg_colour_button = QtGui.QPushButton(self.highlight_groupbox)
|
|
self.bg_colour_button.setText(from_utf8(''))
|
|
self.bg_colour_button.setObjectName(from_utf8('bg_colour_button'))
|
|
self.highlight_layout.setWidget(2, QtGui.QFormLayout.FieldRole, self.bg_colour_button)
|
|
self.vertical_layout.addWidget(self.highlight_groupbox)
|
|
self.section_stacked_widget.addWidget(self.highlight_page)
|
|
self.font_page = QtGui.QWidget()
|
|
self.font_page.setObjectName(from_utf8('font_page'))
|
|
self.font_layout = QtGui.QFormLayout(self.font_page)
|
|
self.font_layout.setMargin(8)
|
|
self.font_layout.setSpacing(8)
|
|
self.font_layout.setObjectName(from_utf8('font_layout'))
|
|
self.font_label = QtGui.QLabel(self.font_page)
|
|
self.font_label.setObjectName(from_utf8('font_label'))
|
|
self.font_layout.setWidget(0, QtGui.QFormLayout.LabelRole, self.font_label)
|
|
self.font_combobox = QtGui.QFontComboBox(self.font_page)
|
|
self.font_combobox.setObjectName(from_utf8('font_combobox'))
|
|
self.font_layout.setWidget(0, QtGui.QFormLayout.FieldRole, self.font_combobox)
|
|
self.size_label = QtGui.QLabel(self.font_page)
|
|
self.size_label.setObjectName(from_utf8('size_label'))
|
|
self.font_layout.setWidget(1, QtGui.QFormLayout.LabelRole, self.size_label)
|
|
self.size_spinbox = QtGui.QSpinBox(self.font_page)
|
|
self.size_spinbox.setReadOnly(True)
|
|
self.size_spinbox.setSpecialValueText(from_utf8(''))
|
|
self.size_spinbox.setMinimum(6)
|
|
self.size_spinbox.setMaximum(50)
|
|
self.size_spinbox.setProperty('value', 12)
|
|
self.size_spinbox.setObjectName(from_utf8('size_spinbox'))
|
|
self.font_layout.setWidget(1, QtGui.QFormLayout.FieldRole, self.size_spinbox)
|
|
self.section_stacked_widget.addWidget(self.font_page)
|
|
self.settings_layout.addWidget(self.splitter)
|
|
self.button_box = QtGui.QDialogButtonBox(settings_dialog)
|
|
self.button_box.setOrientation(QtCore.Qt.Horizontal)
|
|
self.button_box.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
|
|
self.button_box.setObjectName(from_utf8('button_box'))
|
|
self.settings_layout.addWidget(self.button_box)
|
|
|
|
self.retranslate_ui(settings_dialog)
|
|
self.button_box.accepted.connect(settings_dialog.accept)
|
|
self.button_box.rejected.connect(settings_dialog.reject)
|
|
self.section_list_widget.currentRowChanged.connect(self.section_stacked_widget.setCurrentIndex)
|
|
settings_dialog.setTabOrder(self.section_list_widget, self.highlight_list_widget)
|
|
settings_dialog.setTabOrder(self.highlight_list_widget, self.add_button)
|
|
settings_dialog.setTabOrder(self.add_button, self.edit_button)
|
|
settings_dialog.setTabOrder(self.edit_button, self.delete_button)
|
|
settings_dialog.setTabOrder(self.delete_button, self.save_button)
|
|
settings_dialog.setTabOrder(self.save_button, self.discard_button)
|
|
settings_dialog.setTabOrder(self.discard_button, self.button_box)
|
|
|
|
def retranslate_ui(self, settings_dialog):
|
|
"""
|
|
Translate the user interface
|
|
"""
|
|
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 = self.section_list_widget.item(1)
|
|
item.setText(translate('SettingsDialog', 'Font'))
|
|
self.section_list_widget.setSortingEnabled(__sortingEnabled)
|
|
self.up_button.setText(translate('SettingsDialog', 'Move Up'))
|
|
self.down_button.setText(translate('SettingsDialog', 'Move Down'))
|
|
self.add_button.setText(translate('SettingsDialog', 'Add'))
|
|
self.edit_button.setText(translate('SettingsDialog', 'Edit'))
|
|
self.delete_button.setText(translate('SettingsDialog', 'Delete'))
|
|
self.save_button.setText(translate('SettingsDialog', 'Save'))
|
|
self.discard_button.setText(translate('SettingsDialog', 'Discard'))
|
|
self.highlight_groupbox.setTitle(translate('SettingsDialog', 'Highlight'))
|
|
self.regex_label.setText(translate('SettingsDialog', 'Regular Expression:'))
|
|
self.foreground_label.setText(translate('SettingsDialog', 'Foreground:'))
|
|
self.background_checkbox.setText(translate('SettingsDialog', 'Background:'))
|
|
self.font_label.setText(translate('SettingsDialog', 'Font:'))
|
|
self.size_label.setText(translate('SettingsDialog', 'Size:'))
|
|
self.size_spinbox.setSuffix(translate('SettingsDialog', 'pt'))
|
|
|
|
|
|
class SettingsDialog(QtGui.QDialog, UiSettingsDialog):
|
|
|
|
def __init__(self):
|
|
QtGui.QDialog.__init__(self)
|
|
self.setup_ui(self)
|
|
self._highlights = {}
|
|
self._current_highlight = None
|
|
self._current_row = -1
|
|
self.highlight_list_widget.itemSelectionChanged.connect(self.on_highlight_item_selected)
|
|
self.regex_edit.textEdited.connect(self.on_regex_edit_text_edited)
|
|
self.fg_colour_button.clicked.connect(self.on_fg_colour_button_clicked)
|
|
self.background_checkbox.toggled.connect(self.on_background_checkbox_toggled)
|
|
self.bg_colour_button.clicked.connect(self.on_bg_colour_button_clicked)
|
|
self.up_button.clicked.connect(self.on_up_button_clicked)
|
|
self.down_button.clicked.connect(self.on_down_button_clicked)
|
|
self.add_button.clicked.connect(self.on_add_button_clicked)
|
|
self.edit_button.clicked.connect(self.on_edit_button_clicked)
|
|
self.delete_button.clicked.connect(self.on_delete_button_clicked)
|
|
self.save_button.clicked.connect(self.on_save_button_clicked)
|
|
self.discard_button.clicked.connect(self.on_discard_button_clicked)
|
|
|
|
def _set_current_highlight(self):
|
|
self.regex_edit.setText(self._current_highlight.pattern)
|
|
if self._current_highlight.background:
|
|
self.background_checkbox.setChecked(True)
|
|
self.bg_colour_button.setEnabled(True)
|
|
self.bg_colour_button.setStyleSheet('background-color: %s' % self._current_highlight.background)
|
|
else:
|
|
self.background_checkbox.setChecked(False)
|
|
self.bg_colour_button.setEnabled(False)
|
|
self.bg_colour_button.setStyleSheet('')
|
|
self.fg_colour_button.setStyleSheet('background-color: %s' % self._current_highlight.foreground)
|
|
self.set_edit_mode(True)
|
|
|
|
def highlights(self):
|
|
return self._highlights.values()
|
|
|
|
def set_highlights(self, highlights):
|
|
self._highlights = {}
|
|
self.highlight_list_widget.clear()
|
|
for index, highlight in enumerate(highlights):
|
|
self._highlights[index] = highlight
|
|
item = QtGui.QListWidgetItem()
|
|
if highlight.background:
|
|
item.setBackgroundColor(QtGui.QColor(highlight.background))
|
|
item.setTextColor(QtGui.QColor(highlight.foreground))
|
|
item.setText(highlight.pattern)
|
|
self.highlight_list_widget.addItem(item)
|
|
|
|
def on_highlight_item_selected(self):
|
|
self._current_row = self.highlight_list_widget.currentRow()
|
|
if self._current_row > -1:
|
|
self._current_highlight = self._highlights[self._current_row]
|
|
|
|
def on_regex_edit_text_edited(self):
|
|
self._current_highlight.set_pattern(unicode(self.regex_edit.text()))
|
|
|
|
def on_background_checkbox_toggled(self, checked):
|
|
self.bg_colour_button.setEnabled(checked)
|
|
if not checked:
|
|
self._current_highlight.background = None
|
|
|
|
def on_fg_colour_button_clicked(self):
|
|
colour = QtGui.QColor(self._current_highlight.foreground)
|
|
colour = QtGui.QColorDialog.getColor(colour, self)
|
|
if colour.isValid():
|
|
self.fg_colour_button.setStyleSheet('background-color: %s' % colour.name())
|
|
self._current_highlight.foreground = colour.name()
|
|
|
|
def on_bg_colour_button_clicked(self):
|
|
if self._current_highlight.background:
|
|
colour = QtGui.QColor(self._current_highlight.background)
|
|
colour = QtGui.QColorDialog.getColor(colour, self)
|
|
else:
|
|
colour = QtGui.QColorDialog.getColor(QtCore.Qt.white, self)
|
|
if colour.isValid():
|
|
self.bg_colour_button.setStyleSheet('background-color: %s' % colour.name())
|
|
self._current_highlight.background = colour.name()
|
|
|
|
def on_up_button_clicked(self):
|
|
if self._current_row == 0:
|
|
return
|
|
row = self._current_row
|
|
item = self.highlight_list_widget.takeItem(row)
|
|
self.highlight_list_widget.insertItem(row - 1, item)
|
|
self._highlights[row - 1], self._highlights[row] = \
|
|
self._highlights[row], self._highlights[row - 1]
|
|
self.highlight_list_widget.setCurrentRow(row - 1)
|
|
self.on_highlight_item_selected()
|
|
|
|
def on_down_button_clicked(self):
|
|
if self._current_row == len(self._highlights) - 1:
|
|
return
|
|
row = self._current_row
|
|
item = self.highlight_list_widget.takeItem(row)
|
|
self.highlight_list_widget.insertItem(row + 1, item)
|
|
self._highlights[row + 1], self._highlights[row] = \
|
|
self._highlights[row], self._highlights[row + 1]
|
|
self.highlight_list_widget.setCurrentRow(row + 1)
|
|
self.on_highlight_item_selected()
|
|
|
|
def on_add_button_clicked(self):
|
|
self._current_row = -1
|
|
self._current_highlight = Highlight(u'', QtGui.QColor(QtGui.QPalette.WindowText).name())
|
|
self._set_current_highlight()
|
|
self.set_edit_mode(True)
|
|
|
|
def on_edit_button_clicked(self):
|
|
self._set_current_highlight()
|
|
self.set_edit_mode(True)
|
|
|
|
def on_delete_button_clicked(self):
|
|
if QtGui.QMessageBox.question(self, translate('SettingsDialog', 'Confirm Delete'),
|
|
translate('SettingsDialog', 'Are you sure you want to delete this highlight?'),
|
|
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
|
|
del self._highlights[self._current_row]
|
|
self.highlight_list_widget.takeItem(self._current_row)
|
|
self._current_row = -1
|
|
self._current_highlight = None
|
|
|
|
def on_save_button_clicked(self):
|
|
if self._current_row < 0:
|
|
new_index = len(self._highlights)
|
|
self._highlights[new_index] = self._current_highlight
|
|
item = QtGui.QListWidgetItem()
|
|
if self._current_highlight.background:
|
|
item.setBackgroundColor(QtGui.QColor(self._current_highlight.background))
|
|
item.setTextColor(QtGui.QColor(self._current_highlight.foreground))
|
|
item.setText(self._current_highlight.pattern)
|
|
self.highlight_list_widget.addItem(item)
|
|
self.highlight_list_widget.setCurrentRow(new_index)
|
|
else:
|
|
self._highlights[self._current_row] = self._current_highlight
|
|
self.set_edit_mode(False)
|
|
|
|
def on_discard_button_clicked(self):
|
|
self.set_edit_mode(False)
|
|
|
|
def set_edit_mode(self, enable):
|
|
for button in self.button_box.buttons():
|
|
button.setEnabled(not enable)
|
|
self.up_button.setVisible(not enable)
|
|
self.down_button.setVisible(not enable)
|
|
self.add_button.setVisible(not enable)
|
|
self.edit_button.setVisible(not enable)
|
|
self.delete_button.setVisible(not enable)
|
|
self.save_button.setVisible(enable)
|
|
self.discard_button.setVisible(enable)
|
|
self.highlight_list_widget.setEnabled(not enable)
|
|
self.regex_edit.setEnabled(enable)
|
|
self.fg_colour_button.setEnabled(enable)
|
|
self.background_checkbox.setEnabled(enable)
|
|
if enable and self.background_checkbox.isChecked():
|
|
self.bg_colour_button.setEnabled(True)
|
|
else:
|
|
self.bg_colour_button.setEnabled(False)
|
|
if not enable:
|
|
self.regex_edit.clear()
|
|
self.fg_colour_button.setStyleSheet('')
|
|
self.bg_colour_button.setStyleSheet('')
|