]> jfr.im git - irc/weechat/qweechat.git/blame - qweechat/buffer.py
Remove obsolete commented code
[irc/weechat/qweechat.git] / qweechat / buffer.py
CommitLineData
7dcf23b1
SH
1# -*- coding: utf-8 -*-
2#
e836cfb0
SH
3# buffer.py - management of WeeChat buffers/nicklist
4#
8335009d 5# Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
7dcf23b1
SH
6#
7# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
8#
9# QWeeChat is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 3 of the License, or
12# (at your option) any later version.
13#
14# QWeeChat is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
21#
f3da694d 22
1fe88536
SH
23"""Management of WeeChat buffers/nicklist."""
24
77df9d06 25from pkg_resources import resource_filename
5b2c7244 26
f3da694d
SH
27from PySide6 import QtCore, QtGui, QtWidgets
28
0ee7d2e9
AR
29from qweechat.chat import ChatTextEdit
30from qweechat.input import InputLineEdit
5b2c7244 31from qweechat.weechat import color
7dcf23b1
SH
32
33
1f27a20e 34class GenericListWidget(QtWidgets.QListWidget):
dc15599c 35 """Generic QListWidget with dynamic size."""
7dcf23b1
SH
36
37 def __init__(self, *args):
1f27a20e 38 super().__init__(*args)
dc15599c
SH
39 self.setMaximumWidth(100)
40 self.setTextElideMode(QtCore.Qt.ElideNone)
7dcf23b1 41 self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
7dcf23b1
SH
42 self.setFocusPolicy(QtCore.Qt.NoFocus)
43 pal = self.palette()
44 pal.setColor(QtGui.QPalette.Highlight, QtGui.QColor('#ddddff'))
45 pal.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor('black'))
46 self.setPalette(pal)
47
02cba1c1
SH
48 def auto_resize(self):
49 size = self.sizeHintForColumn(0)
50 if size > 0:
51 size += 4
52 self.setMaximumWidth(size)
53
54 def clear(self, *args):
55 """Re-implement clear to set dynamic size after clear."""
1f27a20e 56 QtWidgets.QListWidget.clear(*(self,) + args)
02cba1c1
SH
57 self.auto_resize()
58
dc15599c
SH
59 def addItem(self, *args):
60 """Re-implement addItem to set dynamic size after add."""
1f27a20e 61 QtWidgets.QListWidget.addItem(*(self,) + args)
02cba1c1
SH
62 self.auto_resize()
63
64 def insertItem(self, *args):
65 """Re-implement insertItem to set dynamic size after insert."""
1f27a20e 66 QtWidgets.QListWidget.insertItem(*(self,) + args)
02cba1c1 67 self.auto_resize()
dc15599c
SH
68
69
70class BufferListWidget(GenericListWidget):
71 """Widget with list of buffers."""
72
7dcf23b1
SH
73 def switch_prev_buffer(self):
74 if self.currentRow() > 0:
75 self.setCurrentRow(self.currentRow() - 1)
76 else:
77 self.setCurrentRow(self.count() - 1)
78
79 def switch_next_buffer(self):
80 if self.currentRow() < self.count() - 1:
81 self.setCurrentRow(self.currentRow() + 1)
82 else:
83 self.setCurrentRow(0)
84
85
1f27a20e 86class BufferWidget(QtWidgets.QWidget):
77df9d06
SH
87 """
88 Widget with (from top to bottom):
89 title, chat + nicklist (optional) + prompt/input.
90 """
7dcf23b1
SH
91
92 def __init__(self, display_nicklist=False):
1f27a20e 93 super().__init__()
7dcf23b1
SH
94
95 # title
1f27a20e 96 self.title = QtWidgets.QLineEdit()
02cba1c1 97 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
7dcf23b1
SH
98
99 # splitter with chat + nicklist
1f27a20e
AR
100 self.chat_nicklist = QtWidgets.QSplitter()
101 self.chat_nicklist.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
102 QtWidgets.QSizePolicy.Expanding)
3a5ec0c1 103 self.chat = ChatTextEdit(debug=False)
7dcf23b1 104 self.chat_nicklist.addWidget(self.chat)
dc15599c 105 self.nicklist = GenericListWidget()
7dcf23b1
SH
106 if not display_nicklist:
107 self.nicklist.setVisible(False)
108 self.chat_nicklist.addWidget(self.nicklist)
109
110 # prompt + input
1f27a20e 111 self.hbox_edit = QtWidgets.QHBoxLayout()
c728febd
SH
112 self.hbox_edit.setContentsMargins(0, 0, 0, 0)
113 self.hbox_edit.setSpacing(0)
7dcf23b1 114 self.input = InputLineEdit(self.chat)
c728febd 115 self.hbox_edit.addWidget(self.input)
1f27a20e 116 prompt_input = QtWidgets.QWidget()
c728febd 117 prompt_input.setLayout(self.hbox_edit)
7dcf23b1
SH
118
119 # vbox with title + chat/nicklist + prompt/input
1f27a20e 120 vbox = QtWidgets.QVBoxLayout()
7dcf23b1
SH
121 vbox.setContentsMargins(0, 0, 0, 0)
122 vbox.setSpacing(0)
123 vbox.addWidget(self.title)
124 vbox.addWidget(self.chat_nicklist)
125 vbox.addWidget(prompt_input)
126
127 self.setLayout(vbox)
128
129 def set_title(self, title):
130 """Set buffer title."""
131 self.title.clear()
baa01601 132 if title is not None:
7dcf23b1
SH
133 self.title.setText(title)
134
c728febd
SH
135 def set_prompt(self, prompt):
136 """Set prompt."""
137 if self.hbox_edit.count() > 1:
138 self.hbox_edit.takeAt(0)
baa01601 139 if prompt is not None:
1f27a20e 140 label = QtWidgets.QLabel(prompt)
c728febd
SH
141 label.setContentsMargins(0, 0, 5, 0)
142 self.hbox_edit.insertWidget(0, label)
143
7dcf23b1
SH
144
145class Buffer(QtCore.QObject):
146 """A WeeChat buffer."""
147
1f27a20e 148 bufferInput = QtCore.Signal(str, str)
7dcf23b1 149
1fe88536 150 def __init__(self, data=None):
7dcf23b1 151 QtCore.QObject.__init__(self)
1fe88536 152 self.data = data or {}
2a0b6adc 153 self.nicklist = {}
77df9d06
SH
154 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist',
155 0))
c728febd
SH
156 self.update_title()
157 self.update_prompt()
7dcf23b1
SH
158 self.widget.input.textSent.connect(self.input_text_sent)
159
160 def pointer(self):
161 """Return pointer on buffer."""
162 return self.data.get('__path', [''])[0]
163
c728febd
SH
164 def update_title(self):
165 """Update title."""
166 try:
77df9d06 167 self.widget.set_title(
eedb06f0 168 color.remove(self.data['title']))
7a3a48a5 169 except Exception: # noqa: E722
eedb06f0
AR
170 # TODO: Debug print the exception to be fixed.
171 # traceback.print_exc()
c728febd
SH
172 self.widget.set_title(None)
173
174 def update_prompt(self):
175 """Update prompt."""
176 try:
177 self.widget.set_prompt(self.data['local_variables']['nick'])
7a3a48a5 178 except Exception: # noqa: E722
c728febd
SH
179 self.widget.set_prompt(None)
180
7dcf23b1
SH
181 def input_text_sent(self, text):
182 """Called when text has to be sent to buffer."""
183 if self.data:
184 self.bufferInput.emit(self.data['full_name'], text)
185
2a0b6adc
SH
186 def nicklist_add_item(self, parent, group, prefix, name, visible):
187 """Add a group/nick in nicklist."""
188 if group:
77df9d06
SH
189 self.nicklist[name] = {
190 'visible': visible,
191 'nicks': []
192 }
02cba1c1 193 else:
890d4fa2
SH
194 self.nicklist[parent]['nicks'].append({
195 'prefix': prefix,
196 'name': name,
197 'visible': visible,
198 })
2a0b6adc
SH
199
200 def nicklist_remove_item(self, parent, group, name):
201 """Remove a group/nick from nicklist."""
202 if group:
203 if name in self.nicklist:
204 del self.nicklist[name]
205 else:
206 if parent in self.nicklist:
77df9d06
SH
207 self.nicklist[parent]['nicks'] = [
208 nick for nick in self.nicklist[parent]['nicks']
209 if nick['name'] != name
210 ]
2a0b6adc
SH
211
212 def nicklist_update_item(self, parent, group, prefix, name, visible):
213 """Update a group/nick in nicklist."""
214 if group:
215 if name in self.nicklist:
216 self.nicklist[name]['visible'] = visible
217 else:
218 if parent in self.nicklist:
219 for nick in self.nicklist[parent]['nicks']:
220 if nick['name'] == name:
221 nick['prefix'] = prefix
222 nick['visible'] = visible
223 break
224
225 def nicklist_refresh(self):
226 """Refresh nicklist."""
02cba1c1 227 self.widget.nicklist.clear()
2a0b6adc 228 for group in sorted(self.nicklist):
77df9d06
SH
229 for nick in sorted(self.nicklist[group]['nicks'],
230 key=lambda n: n['name']):
890d4fa2
SH
231 prefix_color = {
232 '': '',
233 ' ': '',
234 '+': 'yellow',
235 }
1fe88536
SH
236 col = prefix_color.get(nick['prefix'], 'green')
237 if col:
77df9d06
SH
238 icon = QtGui.QIcon(
239 resource_filename(__name__,
240 'data/icons/bullet_%s_8x8.png' %
1fe88536 241 col))
2a0b6adc
SH
242 else:
243 pixmap = QtGui.QPixmap(8, 8)
244 pixmap.fill()
245 icon = QtGui.QIcon(pixmap)
1f27a20e 246 item = QtWidgets.QListWidgetItem(icon, nick['name'])
2a0b6adc
SH
247 self.widget.nicklist.addItem(item)
248 self.widget.nicklist.setVisible(True)