Added basic preferences dialog. (Entire config.)

This commit is contained in:
Ricky 2016-11-13 19:54:42 -08:00
parent f5e54d0169
commit d558eb9f29
2 changed files with 129 additions and 4 deletions

126
qweechat/preferences.py Normal file
View File

@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
#
# preferences.py - preferences dialog box
#
# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
#
# QWeeChat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# QWeeChat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
#
import qt_compat
import config
from buffer import GenericListWidget
QtCore = qt_compat.import_module('QtCore')
QtGui = qt_compat.import_module('QtGui')
class PreferencesDialog(QtGui.QDialog):
"""Preferences dialog."""
def __init__(self, name, config, *args):
QtGui.QDialog.__init__(*(self,) + args)
self.setModal(True)
self.setWindowTitle(name)
self.config = config
self.stacked_panes = QtGui.QStackedWidget()
self.list_panes = PreferencesListWidget()
splitter = QtGui.QSplitter()
splitter.addWidget(self.list_panes)
splitter.addWidget(self.stacked_panes)
for section_name in config.sections():
item = QtGui.QListWidgetItem(section_name)
pane = PreferencesPaneWidget(section_name)
self.list_panes.addItem(item)
self.stacked_panes.addWidget(pane)
for name, value in config.items(section_name):
pane.addItem(name, value)
self.list_panes.currentRowChanged.connect(self._pane_switch)
hbox = QtGui.QHBoxLayout()
self.dialog_buttons = QtGui.QDialogButtonBox()
self.dialog_buttons.setStandardButtons(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
self.dialog_buttons.rejected.connect(self.close)
self.dialog_buttons.accepted.connect(self._save_and_close)
hbox.addStretch(1)
hbox.addWidget(self.dialog_buttons)
hbox.addStretch(1)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(splitter)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
def _pane_switch(self, index):
"""Switch the visible preference pane."""
if index >= 0:
self.stacked_panes.setCurrentIndex(index)
def _save_and_close(self):
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()))
config.write(self.config)
self.close()
class PreferencesListWidget(GenericListWidget):
"""Widget with list of preferences."""
def __init__(self, *args):
GenericListWidget.__init__(*(self,) + args)
def switch_prev_buffer(self):
if self.currentRow() > 0:
self.setCurrentRow(self.currentRow() - 1)
else:
self.setCurrentRow(self.count() - 1)
def switch_next_buffer(self):
if self.currentRow() < self.count() - 1:
self.setCurrentRow(self.currentRow() + 1)
else:
self.setCurrentRow(0)
class PreferencesPaneWidget(QtGui.QWidget):
"""
Widget with (from top to bottom):
title, chat + nicklist (optional) + prompt/input.
"""
def __init__(self, section_name):
QtGui.QWidget.__init__(self)
self.grid = QtGui.QGridLayout()
self.section_name = section_name
self.fields = {}
self.setLayout(self.grid)
def addItem(self, key, value):
"""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)
if key == 'password':
line_edit.setEchoMode(QtGui.QLineEdit.Password)
self.grid.addWidget(line_edit, line, 1)
self.fields[key] = line_edit

View File

@ -44,6 +44,7 @@ from connection import ConnectionDialog
from buffer import BufferListWidget, Buffer from buffer import BufferListWidget, Buffer
from debug import DebugDialog from debug import DebugDialog
from about import AboutDialog from about import AboutDialog
from preferences import PreferencesDialog
from version import qweechat_version from version import qweechat_version
QtCore = qt_compat.import_module('QtCore') QtCore = qt_compat.import_module('QtCore')
@ -198,10 +199,8 @@ class MainWindow(QtGui.QMainWindow):
def open_preferences_dialog(self): def open_preferences_dialog(self):
"""Open a dialog with preferences.""" """Open a dialog with preferences."""
# TODO: implement the preferences dialog box self.preferences_dialog = PreferencesDialog('Preferences', self.config,
messages = ['Not yet implemented!', self)
'']
self.preferences_dialog = AboutDialog('Preferences', messages, self)
def save_connection(self): def save_connection(self):
"""Save connection configuration.""" """Save connection configuration."""