From 75f017b60db474303104113f1977c7cf8bd5739e Mon Sep 17 00:00:00 2001 From: Ricky Date: Mon, 14 Nov 2016 00:29:18 -0800 Subject: [PATCH] Improved preferences, added options to change qt style and save toolbar state. --- qweechat/config.py | 2 ++ qweechat/preferences.py | 59 ++++++++++++++++++++++++++--------------- qweechat/qweechat.py | 17 ++++++++++-- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/qweechat/config.py b/qweechat/config.py index 71760e6..b695afe 100644 --- a/qweechat/config.py +++ b/qweechat/config.py @@ -36,6 +36,8 @@ CONFIG_DEFAULT_OPTIONS = (('relay.server', ''), ('relay.autoconnect', 'off'), ('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)), ('look.debug', 'off'), + ('look.style', ''), + ('look.toolbar', 'on'), ('look.statusbar', 'off')) # Default colors for WeeChat color options (option name, #rgb value) diff --git a/qweechat/preferences.py b/qweechat/preferences.py index 68d30e3..6acc1c6 100644 --- a/qweechat/preferences.py +++ b/qweechat/preferences.py @@ -20,7 +20,6 @@ import qt_compat import config -from buffer import GenericListWidget QtCore = qt_compat.import_module('QtCore') QtGui = qt_compat.import_module('QtGui') @@ -29,30 +28,33 @@ QtGui = qt_compat.import_module('QtGui') class PreferencesDialog(QtGui.QDialog): """Preferences dialog.""" - def __init__(self, name, config, *args): + def __init__(self, name, parent, *args): QtGui.QDialog.__init__(*(self,) + args) self.setModal(True) self.setWindowTitle(name) - self.config = config + self.parent = parent + self.config = parent.config self.stacked_panes = QtGui.QStackedWidget() - self.list_panes = PreferencesListWidget() + self.list_panes = PreferencesTreeWidget("Settings") + splitter = QtGui.QSplitter() splitter.addWidget(self.list_panes) splitter.addWidget(self.stacked_panes) - for section_name in config.sections(): - item = QtGui.QListWidgetItem(section_name) + for section_name in self.config.sections(): + item = QtGui.QTreeWidgetItem(section_name) + item.setText(0, section_name) pane = PreferencesPaneWidget(section_name) - self.list_panes.addItem(item) + self.list_panes.addTopLevelItem(item) 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) - self.list_panes.currentRowChanged.connect(self._pane_switch) + self.list_panes.currentItemChanged.connect(self._pane_switch) hbox = QtGui.QHBoxLayout() self.dialog_buttons = QtGui.QDialogButtonBox() 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.accepted.connect(self._save_and_close) @@ -67,8 +69,9 @@ class PreferencesDialog(QtGui.QDialog): self.setLayout(vbox) self.show() - def _pane_switch(self, index): + def _pane_switch(self, item): """Switch the visible preference pane.""" + index = self.list_panes.indexOfTopLevelItem(item) if index >= 0: self.stacked_panes.setCurrentIndex(index) @@ -76,16 +79,23 @@ class PreferencesDialog(QtGui.QDialog): for widget in (self.stacked_panes.widget(i) for i in range(self.stacked_panes.count())): 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) + self.parent.apply_preferences() self.close() -class PreferencesListWidget(GenericListWidget): - """Widget with list of preferences.""" +class PreferencesTreeWidget(QtGui.QTreeWidget): + """Widget with tree list of preferences.""" - def __init__(self, *args): - GenericListWidget.__init__(*(self,) + args) + def __init__(self, header_label, *args): + QtGui.QTreeWidget.__init__(*(self,) + args) + self.setHeaderLabel(header_label) + self.setRootIsDecorated(False) def switch_prev_buffer(self): if self.currentRow() > 0: @@ -117,10 +127,15 @@ class PreferencesPaneWidget(QtGui.QWidget): """Add a key-value pair.""" line = len(self.fields) self.grid.addWidget(QtGui.QLabel(key.capitalize()), line, 0) - line_edit = QtGui.QLineEdit() - line_edit.setFixedWidth(200) - line_edit.insert(value) + edit = QtGui.QLineEdit() + edit.setFixedWidth(200) + edit.insert(value) if key == 'password': - line_edit.setEchoMode(QtGui.QLineEdit.Password) - self.grid.addWidget(line_edit, line, 1) - self.fields[key] = line_edit + edit.setEchoMode(QtGui.QLineEdit.Password) + if key == 'style': + 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 diff --git a/qweechat/qweechat.py b/qweechat/qweechat.py index e430d35..e4f0417 100644 --- a/qweechat/qweechat.py +++ b/qweechat/qweechat.py @@ -160,12 +160,14 @@ class MainWindow(QtGui.QMainWindow): # toolbar toolbar = self.addToolBar('toolBar') toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) + toolbar.setMovable(False); toolbar.addActions([self.actions['connect'], self.actions['disconnect'], self.actions['debug'], self.actions['preferences'], self.actions['about'], self.actions['quit']]) + self.toolbar = toolbar self.buffers[0].widget.input.setFocus() @@ -181,9 +183,21 @@ class MainWindow(QtGui.QMainWindow): 'ssl'), self.config.get('relay', 'password'), self.config.get('relay', 'lines')) + self.apply_preferences() 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): """Switch to a buffer.""" if index >= 0: @@ -199,8 +213,7 @@ class MainWindow(QtGui.QMainWindow): def open_preferences_dialog(self): """Open a dialog with preferences.""" - self.preferences_dialog = PreferencesDialog('Preferences', self.config, - self) + self.preferences_dialog = PreferencesDialog('Preferences', self) def save_connection(self): """Save connection configuration."""