Improved preferences, added options to change qt style and save toolbar state.

This commit is contained in:
Ricky 2016-11-14 00:29:18 -08:00
parent d558eb9f29
commit 75f017b60d
3 changed files with 54 additions and 24 deletions

View File

@ -36,6 +36,8 @@ CONFIG_DEFAULT_OPTIONS = (('relay.server', ''),
('relay.autoconnect', 'off'), ('relay.autoconnect', 'off'),
('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)), ('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)),
('look.debug', 'off'), ('look.debug', 'off'),
('look.style', ''),
('look.toolbar', 'on'),
('look.statusbar', 'off')) ('look.statusbar', 'off'))
# Default colors for WeeChat color options (option name, #rgb value) # Default colors for WeeChat color options (option name, #rgb value)

View File

@ -20,7 +20,6 @@
import qt_compat import qt_compat
import config import config
from buffer import GenericListWidget
QtCore = qt_compat.import_module('QtCore') QtCore = qt_compat.import_module('QtCore')
QtGui = qt_compat.import_module('QtGui') QtGui = qt_compat.import_module('QtGui')
@ -29,30 +28,33 @@ QtGui = qt_compat.import_module('QtGui')
class PreferencesDialog(QtGui.QDialog): class PreferencesDialog(QtGui.QDialog):
"""Preferences dialog.""" """Preferences dialog."""
def __init__(self, name, config, *args): def __init__(self, name, parent, *args):
QtGui.QDialog.__init__(*(self,) + args) QtGui.QDialog.__init__(*(self,) + args)
self.setModal(True) self.setModal(True)
self.setWindowTitle(name) self.setWindowTitle(name)
self.config = config self.parent = parent
self.config = parent.config
self.stacked_panes = QtGui.QStackedWidget() self.stacked_panes = QtGui.QStackedWidget()
self.list_panes = PreferencesListWidget() self.list_panes = PreferencesTreeWidget("Settings")
splitter = QtGui.QSplitter() splitter = QtGui.QSplitter()
splitter.addWidget(self.list_panes) splitter.addWidget(self.list_panes)
splitter.addWidget(self.stacked_panes) splitter.addWidget(self.stacked_panes)
for section_name in config.sections(): for section_name in self.config.sections():
item = QtGui.QListWidgetItem(section_name) item = QtGui.QTreeWidgetItem(section_name)
item.setText(0, section_name)
pane = PreferencesPaneWidget(section_name) pane = PreferencesPaneWidget(section_name)
self.list_panes.addItem(item) self.list_panes.addTopLevelItem(item)
self.stacked_panes.addWidget(pane) self.stacked_panes.addWidget(pane)
for name, value in config.items(section_name): for name, value in self.config.items(section_name):
pane.addItem(name, value) pane.addItem(name, value)
self.list_panes.currentRowChanged.connect(self._pane_switch) self.list_panes.currentItemChanged.connect(self._pane_switch)
hbox = QtGui.QHBoxLayout() hbox = QtGui.QHBoxLayout()
self.dialog_buttons = QtGui.QDialogButtonBox() self.dialog_buttons = QtGui.QDialogButtonBox()
self.dialog_buttons.setStandardButtons( self.dialog_buttons.setStandardButtons(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) QtGui.QDialogButtonBox.Save | QtGui.QDialogButtonBox.Cancel)
self.dialog_buttons.rejected.connect(self.close) self.dialog_buttons.rejected.connect(self.close)
self.dialog_buttons.accepted.connect(self._save_and_close) self.dialog_buttons.accepted.connect(self._save_and_close)
@ -67,8 +69,9 @@ class PreferencesDialog(QtGui.QDialog):
self.setLayout(vbox) self.setLayout(vbox)
self.show() self.show()
def _pane_switch(self, index): def _pane_switch(self, item):
"""Switch the visible preference pane.""" """Switch the visible preference pane."""
index = self.list_panes.indexOfTopLevelItem(item)
if index >= 0: if index >= 0:
self.stacked_panes.setCurrentIndex(index) self.stacked_panes.setCurrentIndex(index)
@ -76,16 +79,23 @@ class PreferencesDialog(QtGui.QDialog):
for widget in (self.stacked_panes.widget(i) for widget in (self.stacked_panes.widget(i)
for i in range(self.stacked_panes.count())): for i in range(self.stacked_panes.count())):
for key, field in widget.fields.iteritems(): for key, field in widget.fields.iteritems():
self.config.set(widget.section_name, key, str(field.text())) if isinstance(field, QtGui.QComboBox):
text = field.itemText(field.currentIndex())
else:
text = field.text()
self.config.set(widget.section_name, key, str(text))
config.write(self.config) config.write(self.config)
self.parent.apply_preferences()
self.close() self.close()
class PreferencesListWidget(GenericListWidget): class PreferencesTreeWidget(QtGui.QTreeWidget):
"""Widget with list of preferences.""" """Widget with tree list of preferences."""
def __init__(self, *args): def __init__(self, header_label, *args):
GenericListWidget.__init__(*(self,) + args) QtGui.QTreeWidget.__init__(*(self,) + args)
self.setHeaderLabel(header_label)
self.setRootIsDecorated(False)
def switch_prev_buffer(self): def switch_prev_buffer(self):
if self.currentRow() > 0: if self.currentRow() > 0:
@ -117,10 +127,15 @@ class PreferencesPaneWidget(QtGui.QWidget):
"""Add a key-value pair.""" """Add a key-value pair."""
line = len(self.fields) line = len(self.fields)
self.grid.addWidget(QtGui.QLabel(key.capitalize()), line, 0) self.grid.addWidget(QtGui.QLabel(key.capitalize()), line, 0)
line_edit = QtGui.QLineEdit() edit = QtGui.QLineEdit()
line_edit.setFixedWidth(200) edit.setFixedWidth(200)
line_edit.insert(value) edit.insert(value)
if key == 'password': if key == 'password':
line_edit.setEchoMode(QtGui.QLineEdit.Password) edit.setEchoMode(QtGui.QLineEdit.Password)
self.grid.addWidget(line_edit, line, 1) if key == 'style':
self.fields[key] = line_edit edit = QtGui.QComboBox()
edit.addItems(QtGui.QStyleFactory.keys())
edit.setCurrentIndex(edit.findText(QtGui.qApp.style().objectName(),
QtCore.Qt.MatchFixedString))
self.grid.addWidget(edit, line, 1)
self.fields[key] = edit

View File

@ -160,12 +160,14 @@ class MainWindow(QtGui.QMainWindow):
# toolbar # toolbar
toolbar = self.addToolBar('toolBar') toolbar = self.addToolBar('toolBar')
toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
toolbar.setMovable(False);
toolbar.addActions([self.actions['connect'], toolbar.addActions([self.actions['connect'],
self.actions['disconnect'], self.actions['disconnect'],
self.actions['debug'], self.actions['debug'],
self.actions['preferences'], self.actions['preferences'],
self.actions['about'], self.actions['about'],
self.actions['quit']]) self.actions['quit']])
self.toolbar = toolbar
self.buffers[0].widget.input.setFocus() self.buffers[0].widget.input.setFocus()
@ -181,9 +183,21 @@ class MainWindow(QtGui.QMainWindow):
'ssl'), 'ssl'),
self.config.get('relay', 'password'), self.config.get('relay', 'password'),
self.config.get('relay', 'lines')) self.config.get('relay', 'lines'))
self.apply_preferences()
self.show() self.show()
def apply_preferences(self):
"""Apply non-server options from preferences."""
app = QtCore.QCoreApplication.instance()
if self.config.getboolean('look', 'toolbar'):
self.toolbar.show()
else:
self.toolbar.hide()
if self.config.get('look', 'style'):
app.setStyle(QtGui.QStyleFactory.create(
self.config.get('look', 'style')))
def _buffer_switch(self, index): def _buffer_switch(self, index):
"""Switch to a buffer.""" """Switch to a buffer."""
if index >= 0: if index >= 0:
@ -199,8 +213,7 @@ class MainWindow(QtGui.QMainWindow):
def open_preferences_dialog(self): def open_preferences_dialog(self):
"""Open a dialog with preferences.""" """Open a dialog with preferences."""
self.preferences_dialog = PreferencesDialog('Preferences', self.config, self.preferences_dialog = PreferencesDialog('Preferences', self)
self)
def save_connection(self): def save_connection(self):
"""Save connection configuration.""" """Save connection configuration."""