Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preferences #8

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
17 changes: 15 additions & 2 deletions qweechat/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ class Buffer(QtCore.QObject):

bufferInput = qt_compat.Signal(str, str)

def __init__(self, data={}):
def __init__(self, data={}, config=False):
QtCore.QObject.__init__(self)
self.data = data
self.config = config
self.nicklist = {}
self.widget = BufferWidget(display_nicklist=self.data.get('nicklist',
0))
Expand Down Expand Up @@ -183,6 +184,14 @@ def input_text_sent(self, text):
if self.data:
self.bufferInput.emit(self.data['full_name'], text)

def update_config(self):
"""Match visibility to configuration, faster than a nicklist refresh"""
if (self.config):
nicklist_visible = self.config.get("look", "nicklist") != "off"
topic_visible = self.config.get("look", "topic") != "off"
self.widget.nicklist.setVisible(nicklist_visible)
self.widget.title.setVisible(topic_visible)

def nicklist_add_item(self, parent, group, prefix, name, visible):
"""Add a group/nick in nicklist."""
if group:
Expand Down Expand Up @@ -245,4 +254,8 @@ def nicklist_refresh(self):
icon = QtGui.QIcon(pixmap)
item = QtGui.QListWidgetItem(icon, nick['name'])
self.widget.nicklist.addItem(item)
self.widget.nicklist.setVisible(True)
if self.config and self.config.get("look",
"nicklist") == "off":
self.widget.nicklist.setVisible(False)
else:
self.widget.nicklist.setVisible(True)
6 changes: 6 additions & 0 deletions qweechat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
('relay.autoconnect', 'off'),
('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)),
('look.debug', 'off'),
('look.style', ''),
('look.buffer_list', 'left'),
('look.nicklist', 'on'),
('look.toolbar', 'on'),
('look.menubar', 'on'),
('look.topic', 'on'),
('look.statusbar', 'off'))

# Default colors for WeeChat color options (option name, #rgb value)
Expand Down
199 changes: 199 additions & 0 deletions qweechat/preferences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# -*- coding: utf-8 -*-
#
# preferences.py - preferences dialog box
#
# Copyright (C) 2016 Ricky Brent <[email protected]>
#
# 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

QtCore = qt_compat.import_module('QtCore')
QtGui = qt_compat.import_module('QtGui')


class PreferencesDialog(QtGui.QDialog):
"""Preferences dialog."""

def __init__(self, name, parent, *args):
QtGui.QDialog.__init__(*(self,) + args)
self.setModal(True)
self.setWindowTitle(name)
self.parent = parent
self.config = parent.config
self.stacked_panes = QtGui.QStackedWidget()
self.list_panes = PreferencesTreeWidget("Settings")

splitter = QtGui.QSplitter()
splitter.addWidget(self.list_panes)
splitter.addWidget(self.stacked_panes)

# Follow same order as defaults:
section_panes = {}
for section in self.config.sections():
item = QtGui.QTreeWidgetItem(section)
item.setText(0, section)
section_panes[section] = PreferencesPaneWidget(section)
self.list_panes.addTopLevelItem(item)
self.stacked_panes.addWidget(section_panes[section])

for setting, default in config.CONFIG_DEFAULT_OPTIONS:
section, key = setting.split(".")
section_panes[section].addItem(key, self.config.get(section, key))
for key, value in self.config.items("color"):
section_panes["color"].addItem(key, value)

self.list_panes.currentItemChanged.connect(self._pane_switch)
self.list_panes.setCurrentItem(self.list_panes.topLevelItem(0))

hbox = QtGui.QHBoxLayout()
self.dialog_buttons = QtGui.QDialogButtonBox()
self.dialog_buttons.setStandardButtons(
QtGui.QDialogButtonBox.Save | 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, item):
"""Switch the visible preference pane."""
index = self.list_panes.indexOfTopLevelItem(item)
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.items():
if isinstance(field, QtGui.QComboBox):
text = field.itemText(field.currentIndex())
elif isinstance(field, QtGui.QCheckBox):
text = "on" if field.isChecked() else "off"
else:
text = field.text()
self.config.set(widget.section_name, key, str(text))
config.write(self.config)
self.parent.apply_preferences()
self.close()


class PreferencesTreeWidget(QtGui.QTreeWidget):
"""Widget with tree list of preferences."""

def __init__(self, header_label, *args):
QtGui.QTreeWidget.__init__(*(self,) + args)
self.setHeaderLabel(header_label)
self.setRootIsDecorated(False)
self.setMaximumWidth(90)
self.setTextElideMode(QtCore.Qt.ElideNone)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setFocusPolicy(QtCore.Qt.NoFocus)


class PreferencesColorEdit(QtGui.QPushButton):
"""Simple color square that changes based on the color selected."""
def __init__(self, *args):
QtGui.QPushButton.__init__(*(self,) + args)
self.color = "#000000"
self.clicked.connect(self._color_picker)
# Some of the configured colors use a astrisk prefix.
# Toggle this on right click.
self.star = False
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self._color_star)

def insert(self, color):
"""Insert the desired color for the widget."""
if color[:1] == "*":
self.star = True
color = color[1:]
self.setText("*" if self.star else "")
self.color = color
self.setStyleSheet("background-color: " + color)

def text(self):
"""Returns the hex value of the color."""
return ("*" if self.star else "") + self.color

def _color_picker(self):
color = QtGui.QColorDialog.getColor()
self.insert(color.name())

def _color_star(self):
self.star = not self.star
self.insert(self.text())


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.grid.setAlignment(QtCore.Qt.AlignTop)
self.section_name = section_name
self.fields = {}
self.setLayout(self.grid)
self.grid.setColumnStretch(2, 1)
self.grid.setSpacing(10)
self.checkboxes = ("ssl", "autoconnect", "statusbar", "topic",
"menubar", "toolbar", "nicklist", "debug")
self.comboboxes = {"style": QtGui.QStyleFactory.keys(),
"buffer_list": ["left", "right"]}

def addItem(self, key, value):
"""Add a key-value pair."""
line = len(self.fields)
name = key.capitalize().replace("_", " ")
start = 0
if self.section_name == "color":
start = 2 * (line % 2)
line = line // 2
self.grid.addWidget(QtGui.QLabel(name), line, start + 0)
if self.section_name == "color":
edit = PreferencesColorEdit()
edit.setFixedWidth(edit.sizeHint().height())
edit.insert(value)
elif key in self.comboboxes.keys():
edit = QtGui.QComboBox()
edit.addItems(self.comboboxes[key])
edit.setCurrentIndex(edit.findText(value))
edit.setFixedWidth(200)
elif key in self.checkboxes:
edit = QtGui.QCheckBox()
edit.setChecked(value == "on")
else:
edit = QtGui.QLineEdit()
edit.setFixedWidth(200)
edit.insert(value)
if key == 'password':
edit.setEchoMode(QtGui.QLineEdit.Password)
self.grid.addWidget(edit, line, start + 1)
self.fields[key] = edit
5 changes: 4 additions & 1 deletion qweechat/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

def _pyside_import_module(moduleName):
pyside = __import__('PySide', globals(), locals(), [moduleName], -1)
return getattr(pyside, moduleName)
mod = getattr(pyside, moduleName)
if moduleName == "QtGui":
mod.QWIDGETSIZE_MAX = ((1 << 24) - 1)
return mod


def _pyqt4_import_module(moduleName):
Expand Down
Loading