]>
jfr.im git - irc/weechat/qweechat.git/blob - qweechat/buffer.py
1 # -*- coding: utf-8 -*-
3 # buffer.py - management of WeeChat buffers/nicklist
5 # Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
7 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
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.
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.
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/>.
24 from pkg_resources
import resource_filename
25 from qweechat
.chat
import ChatTextEdit
26 from qweechat
.input import InputLineEdit
27 import qweechat
.weechat
.color
as color
29 from PySide6
import QtCore
30 from PySide6
import QtWidgets
31 from PySide6
import QtGui
34 class GenericListWidget(QtWidgets
.QListWidget
):
35 """Generic QListWidget with dynamic size."""
37 def __init__(self
, *args
):
38 super().__init
__(*args
)
39 self
.setMaximumWidth(100)
40 self
.setTextElideMode(QtCore
.Qt
.ElideNone
)
41 self
.setHorizontalScrollBarPolicy(QtCore
.Qt
.ScrollBarAlwaysOff
)
42 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
44 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
45 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
48 def auto_resize(self
):
49 size
= self
.sizeHintForColumn(0)
52 self
.setMaximumWidth(size
)
54 def clear(self
, *args
):
55 """Re-implement clear to set dynamic size after clear."""
56 QtWidgets
.QListWidget
.clear(*(self
,) + args
)
59 def addItem(self
, *args
):
60 """Re-implement addItem to set dynamic size after add."""
61 QtWidgets
.QListWidget
.addItem(*(self
,) + args
)
64 def insertItem(self
, *args
):
65 """Re-implement insertItem to set dynamic size after insert."""
66 QtWidgets
.QListWidget
.insertItem(*(self
,) + args
)
70 class BufferListWidget(GenericListWidget
):
71 """Widget with list of buffers."""
73 def __init__(self
, *args
):
74 super().__init
__(*args
)
76 def switch_prev_buffer(self
):
77 if self
.currentRow() > 0:
78 self
.setCurrentRow(self
.currentRow() - 1)
80 self
.setCurrentRow(self
.count() - 1)
82 def switch_next_buffer(self
):
83 if self
.currentRow() < self
.count() - 1:
84 self
.setCurrentRow(self
.currentRow() + 1)
89 class BufferWidget(QtWidgets
.QWidget
):
91 Widget with (from top to bottom):
92 title, chat + nicklist (optional) + prompt/input.
95 def __init__(self
, display_nicklist
=False):
99 self
.title
= QtWidgets
.QLineEdit()
100 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
102 # splitter with chat + nicklist
103 self
.chat_nicklist
= QtWidgets
.QSplitter()
104 self
.chat_nicklist
.setSizePolicy(QtWidgets
.QSizePolicy
.Expanding
,
105 QtWidgets
.QSizePolicy
.Expanding
)
106 self
.chat
= ChatTextEdit(debug
=False)
107 self
.chat_nicklist
.addWidget(self
.chat
)
108 self
.nicklist
= GenericListWidget()
109 if not display_nicklist
:
110 self
.nicklist
.setVisible(False)
111 self
.chat_nicklist
.addWidget(self
.nicklist
)
114 self
.hbox_edit
= QtWidgets
.QHBoxLayout()
115 self
.hbox_edit
.setContentsMargins(0, 0, 0, 0)
116 self
.hbox_edit
.setSpacing(0)
117 self
.input = InputLineEdit(self
.chat
)
118 self
.hbox_edit
.addWidget(self
.input)
119 prompt_input
= QtWidgets
.QWidget()
120 prompt_input
.setLayout(self
.hbox_edit
)
122 # vbox with title + chat/nicklist + prompt/input
123 vbox
= QtWidgets
.QVBoxLayout()
124 vbox
.setContentsMargins(0, 0, 0, 0)
126 vbox
.addWidget(self
.title
)
127 vbox
.addWidget(self
.chat_nicklist
)
128 vbox
.addWidget(prompt_input
)
132 def set_title(self
, title
):
133 """Set buffer title."""
135 if title
is not None:
136 self
.title
.setText(title
)
138 def set_prompt(self
, prompt
):
140 if self
.hbox_edit
.count() > 1:
141 self
.hbox_edit
.takeAt(0)
142 if prompt
is not None:
143 label
= QtWidgets
.QLabel(prompt
)
144 label
.setContentsMargins(0, 0, 5, 0)
145 self
.hbox_edit
.insertWidget(0, label
)
148 class Buffer(QtCore
.QObject
):
149 """A WeeChat buffer."""
151 bufferInput
= QtCore
.Signal(str, str)
153 def __init__(self
, data
={}):
154 QtCore
.QObject
.__init
__(self
)
157 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist',
161 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
164 """Return pointer on buffer."""
165 return self
.data
.get('__path', [''])[0]
167 def update_title(self
):
170 self
.widget
.set_title(
171 color
.remove(self
.data
['title']))
172 except Exception as e
: # noqa: E722
173 # TODO: Debug print the exception to be fixed.
174 # traceback.print_exc()
175 self
.widget
.set_title(None)
177 def update_prompt(self
):
180 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
181 except Exception as e
: # noqa: E722
182 self
.widget
.set_prompt(None)
184 def input_text_sent(self
, text
):
185 """Called when text has to be sent to buffer."""
187 self
.bufferInput
.emit(self
.data
['full_name'], text
)
189 def nicklist_add_item(self
, parent
, group
, prefix
, name
, visible
):
190 """Add a group/nick in nicklist."""
192 self
.nicklist
[name
] = {
197 self
.nicklist
[parent
]['nicks'].append({
203 def nicklist_remove_item(self
, parent
, group
, name
):
204 """Remove a group/nick from nicklist."""
206 if name
in self
.nicklist
:
207 del self
.nicklist
[name
]
209 if parent
in self
.nicklist
:
210 self
.nicklist
[parent
]['nicks'] = [
211 nick
for nick
in self
.nicklist
[parent
]['nicks']
212 if nick
['name'] != name
215 def nicklist_update_item(self
, parent
, group
, prefix
, name
, visible
):
216 """Update a group/nick in nicklist."""
218 if name
in self
.nicklist
:
219 self
.nicklist
[name
]['visible'] = visible
221 if parent
in self
.nicklist
:
222 for nick
in self
.nicklist
[parent
]['nicks']:
223 if nick
['name'] == name
:
224 nick
['prefix'] = prefix
225 nick
['visible'] = visible
228 def nicklist_refresh(self
):
229 """Refresh nicklist."""
230 self
.widget
.nicklist
.clear()
231 for group
in sorted(self
.nicklist
):
232 for nick
in sorted(self
.nicklist
[group
]['nicks'],
233 key
=lambda n
: n
['name']):
239 color
= prefix_color
.get(nick
['prefix'], 'green')
242 resource_filename(__name__
,
243 'data/icons/bullet_%s_8x8.png' %
246 pixmap
= QtGui
.QPixmap(8, 8)
248 icon
= QtGui
.QIcon(pixmap
)
249 item
= QtWidgets
.QListWidgetItem(icon
, nick
['name'])
250 self
.widget
.nicklist
.addItem(item
)
251 self
.widget
.nicklist
.setVisible(True)