]>
jfr.im git - irc/weechat/qweechat.git/blob - qweechat/buffer.py
0f4efcb13043f84a674bbc213d21d91a8a48c3da
1 # -*- coding: utf-8 -*-
3 # buffer.py - management of WeeChat buffers/nicklist
5 # Copyright (C) 2011-2016 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/>.
23 from pkg_resources
import resource_filename
25 QtCore
= qt_compat
.import_module('QtCore')
26 QtGui
= qt_compat
.import_module('QtGui')
27 from chat
import ChatTextEdit
28 from input import InputLineEdit
29 import weechat
.color
as color
32 class GenericListWidget(QtGui
.QListWidget
):
33 """Generic QListWidget with dynamic size."""
35 def __init__(self
, *args
):
36 QtGui
.QListWidget
.__init
__(*(self
,) + args
)
37 self
.setMaximumWidth(100)
38 self
.setTextElideMode(QtCore
.Qt
.ElideNone
)
39 self
.setHorizontalScrollBarPolicy(QtCore
.Qt
.ScrollBarAlwaysOff
)
40 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
42 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
43 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
46 def auto_resize(self
):
47 size
= self
.sizeHintForColumn(0)
50 self
.setMaximumWidth(size
)
52 def clear(self
, *args
):
53 """Re-implement clear to set dynamic size after clear."""
54 QtGui
.QListWidget
.clear(*(self
,) + args
)
57 def addItem(self
, *args
):
58 """Re-implement addItem to set dynamic size after add."""
59 QtGui
.QListWidget
.addItem(*(self
,) + args
)
62 def insertItem(self
, *args
):
63 """Re-implement insertItem to set dynamic size after insert."""
64 QtGui
.QListWidget
.insertItem(*(self
,) + args
)
68 class BufferListWidget(GenericListWidget
):
69 """Widget with list of buffers."""
71 def __init__(self
, *args
):
72 GenericListWidget
.__init
__(*(self
,) + args
)
74 def switch_prev_buffer(self
):
75 if self
.currentRow() > 0:
76 self
.setCurrentRow(self
.currentRow() - 1)
78 self
.setCurrentRow(self
.count() - 1)
80 def switch_next_buffer(self
):
81 if self
.currentRow() < self
.count() - 1:
82 self
.setCurrentRow(self
.currentRow() + 1)
87 class BufferWidget(QtGui
.QWidget
):
89 Widget with (from top to bottom):
90 title, chat + nicklist (optional) + prompt/input.
93 def __init__(self
, display_nicklist
=False):
94 QtGui
.QWidget
.__init
__(self
)
97 self
.title
= QtGui
.QLineEdit()
98 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
100 # splitter with chat + nicklist
101 self
.chat_nicklist
= QtGui
.QSplitter()
102 self
.chat_nicklist
.setSizePolicy(QtGui
.QSizePolicy
.Expanding
,
103 QtGui
.QSizePolicy
.Expanding
)
104 self
.chat
= ChatTextEdit(debug
=False)
105 self
.chat_nicklist
.addWidget(self
.chat
)
106 self
.nicklist
= GenericListWidget()
107 if not display_nicklist
:
108 self
.nicklist
.setVisible(False)
109 self
.chat_nicklist
.addWidget(self
.nicklist
)
112 self
.hbox_edit
= QtGui
.QHBoxLayout()
113 self
.hbox_edit
.setContentsMargins(0, 0, 0, 0)
114 self
.hbox_edit
.setSpacing(0)
115 self
.input = InputLineEdit(self
.chat
)
116 self
.hbox_edit
.addWidget(self
.input)
117 prompt_input
= QtGui
.QWidget()
118 prompt_input
.setLayout(self
.hbox_edit
)
120 # vbox with title + chat/nicklist + prompt/input
121 vbox
= QtGui
.QVBoxLayout()
122 vbox
.setContentsMargins(0, 0, 0, 0)
124 vbox
.addWidget(self
.title
)
125 vbox
.addWidget(self
.chat_nicklist
)
126 vbox
.addWidget(prompt_input
)
130 def set_title(self
, title
):
131 """Set buffer title."""
133 if title
is not None:
134 self
.title
.setText(title
)
136 def set_prompt(self
, prompt
):
138 if self
.hbox_edit
.count() > 1:
139 self
.hbox_edit
.takeAt(0)
140 if prompt
is not None:
141 label
= QtGui
.QLabel(prompt
)
142 label
.setContentsMargins(0, 0, 5, 0)
143 self
.hbox_edit
.insertWidget(0, label
)
146 class Buffer(QtCore
.QObject
):
147 """A WeeChat buffer."""
149 bufferInput
= qt_compat
.Signal(str, str)
151 def __init__(self
, data
={}):
152 QtCore
.QObject
.__init
__(self
)
155 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist',
159 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
162 """Return pointer on buffer."""
163 return self
.data
.get('__path', [''])[0]
165 def update_title(self
):
168 self
.widget
.set_title(
169 color
.remove(self
.data
['title'].decode('utf-8')))
171 self
.widget
.set_title(None)
173 def update_prompt(self
):
176 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
178 self
.widget
.set_prompt(None)
180 def input_text_sent(self
, text
):
181 """Called when text has to be sent to buffer."""
183 self
.bufferInput
.emit(self
.data
['full_name'], text
)
185 def nicklist_add_item(self
, parent
, group
, prefix
, name
, visible
):
186 """Add a group/nick in nicklist."""
188 self
.nicklist
[name
] = {
193 self
.nicklist
[parent
]['nicks'].append({
199 def nicklist_remove_item(self
, parent
, group
, name
):
200 """Remove a group/nick from nicklist."""
202 if name
in self
.nicklist
:
203 del self
.nicklist
[name
]
205 if parent
in self
.nicklist
:
206 self
.nicklist
[parent
]['nicks'] = [
207 nick
for nick
in self
.nicklist
[parent
]['nicks']
208 if nick
['name'] != name
211 def nicklist_update_item(self
, parent
, group
, prefix
, name
, visible
):
212 """Update a group/nick in nicklist."""
214 if name
in self
.nicklist
:
215 self
.nicklist
[name
]['visible'] = visible
217 if parent
in self
.nicklist
:
218 for nick
in self
.nicklist
[parent
]['nicks']:
219 if nick
['name'] == name
:
220 nick
['prefix'] = prefix
221 nick
['visible'] = visible
224 def nicklist_refresh(self
):
225 """Refresh nicklist."""
226 self
.widget
.nicklist
.clear()
227 for group
in sorted(self
.nicklist
):
228 for nick
in sorted(self
.nicklist
[group
]['nicks'],
229 key
=lambda n
: n
['name']):
235 color
= prefix_color
.get(nick
['prefix'], 'green')
238 resource_filename(__name__
,
239 'data/icons/bullet_%s_8x8.png' %
242 pixmap
= QtGui
.QPixmap(8, 8)
244 icon
= QtGui
.QIcon(pixmap
)
245 item
= QtGui
.QListWidgetItem(icon
, nick
['name'])
246 self
.widget
.nicklist
.addItem(item
)
247 self
.widget
.nicklist
.setVisible(True)