]>
Commit | Line | Data |
---|---|---|
7dcf23b1 SH |
1 | # -*- coding: utf-8 -*- |
2 | # | |
e836cfb0 SH |
3 | # buffer.py - management of WeeChat buffers/nicklist |
4 | # | |
da74afdb | 5 | # Copyright (C) 2011-2014 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 | # | |
22 | ||
77df9d06 | 23 | from pkg_resources import resource_filename |
7dcf23b1 SH |
24 | import qt_compat |
25 | QtCore = qt_compat.import_module('QtCore') | |
26 | QtGui = qt_compat.import_module('QtGui') | |
27 | from chat import ChatTextEdit | |
28 | from input import InputLineEdit | |
54bcac1f | 29 | import weechat.color as color |
7dcf23b1 SH |
30 | |
31 | ||
dc15599c SH |
32 | class GenericListWidget(QtGui.QListWidget): |
33 | """Generic QListWidget with dynamic size.""" | |
7dcf23b1 SH |
34 | |
35 | def __init__(self, *args): | |
67ae3204 | 36 | QtGui.QListWidget.__init__(*(self,) + args) |
dc15599c SH |
37 | self.setMaximumWidth(100) |
38 | self.setTextElideMode(QtCore.Qt.ElideNone) | |
7dcf23b1 | 39 | self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) |
7dcf23b1 SH |
40 | self.setFocusPolicy(QtCore.Qt.NoFocus) |
41 | pal = self.palette() | |
42 | pal.setColor(QtGui.QPalette.Highlight, QtGui.QColor('#ddddff')) | |
43 | pal.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor('black')) | |
44 | self.setPalette(pal) | |
45 | ||
02cba1c1 SH |
46 | def auto_resize(self): |
47 | size = self.sizeHintForColumn(0) | |
48 | if size > 0: | |
49 | size += 4 | |
50 | self.setMaximumWidth(size) | |
51 | ||
52 | def clear(self, *args): | |
53 | """Re-implement clear to set dynamic size after clear.""" | |
67ae3204 | 54 | QtGui.QListWidget.clear(*(self,) + args) |
02cba1c1 SH |
55 | self.auto_resize() |
56 | ||
dc15599c SH |
57 | def addItem(self, *args): |
58 | """Re-implement addItem to set dynamic size after add.""" | |
67ae3204 | 59 | QtGui.QListWidget.addItem(*(self,) + args) |
02cba1c1 SH |
60 | self.auto_resize() |
61 | ||
62 | def insertItem(self, *args): | |
63 | """Re-implement insertItem to set dynamic size after insert.""" | |
67ae3204 | 64 | QtGui.QListWidget.insertItem(*(self,) + args) |
02cba1c1 | 65 | self.auto_resize() |
dc15599c SH |
66 | |
67 | ||
68 | class BufferListWidget(GenericListWidget): | |
69 | """Widget with list of buffers.""" | |
70 | ||
71 | def __init__(self, *args): | |
67ae3204 | 72 | GenericListWidget.__init__(*(self,) + args) |
7dcf23b1 SH |
73 | |
74 | def switch_prev_buffer(self): | |
75 | if self.currentRow() > 0: | |
76 | self.setCurrentRow(self.currentRow() - 1) | |
77 | else: | |
78 | self.setCurrentRow(self.count() - 1) | |
79 | ||
80 | def switch_next_buffer(self): | |
81 | if self.currentRow() < self.count() - 1: | |
82 | self.setCurrentRow(self.currentRow() + 1) | |
83 | else: | |
84 | self.setCurrentRow(0) | |
85 | ||
86 | ||
87 | class BufferWidget(QtGui.QWidget): | |
77df9d06 SH |
88 | """ |
89 | Widget with (from top to bottom): | |
90 | title, chat + nicklist (optional) + prompt/input. | |
91 | """ | |
7dcf23b1 SH |
92 | |
93 | def __init__(self, display_nicklist=False): | |
94 | QtGui.QWidget.__init__(self) | |
95 | ||
96 | # title | |
97 | self.title = QtGui.QLineEdit() | |
02cba1c1 | 98 | self.title.setFocusPolicy(QtCore.Qt.NoFocus) |
7dcf23b1 SH |
99 | |
100 | # splitter with chat + nicklist | |
101 | self.chat_nicklist = QtGui.QSplitter() | |
77df9d06 SH |
102 | self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, |
103 | QtGui.QSizePolicy.Expanding) | |
3a5ec0c1 | 104 | self.chat = ChatTextEdit(debug=False) |
7dcf23b1 | 105 | self.chat_nicklist.addWidget(self.chat) |
dc15599c | 106 | self.nicklist = GenericListWidget() |
7dcf23b1 SH |
107 | if not display_nicklist: |
108 | self.nicklist.setVisible(False) | |
109 | self.chat_nicklist.addWidget(self.nicklist) | |
110 | ||
111 | # prompt + input | |
c728febd SH |
112 | self.hbox_edit = QtGui.QHBoxLayout() |
113 | self.hbox_edit.setContentsMargins(0, 0, 0, 0) | |
114 | self.hbox_edit.setSpacing(0) | |
7dcf23b1 | 115 | self.input = InputLineEdit(self.chat) |
c728febd | 116 | self.hbox_edit.addWidget(self.input) |
7dcf23b1 | 117 | prompt_input = QtGui.QWidget() |
c728febd | 118 | prompt_input.setLayout(self.hbox_edit) |
7dcf23b1 SH |
119 | |
120 | # vbox with title + chat/nicklist + prompt/input | |
121 | vbox = QtGui.QVBoxLayout() | |
122 | vbox.setContentsMargins(0, 0, 0, 0) | |
123 | vbox.setSpacing(0) | |
124 | vbox.addWidget(self.title) | |
125 | vbox.addWidget(self.chat_nicklist) | |
126 | vbox.addWidget(prompt_input) | |
127 | ||
128 | self.setLayout(vbox) | |
129 | ||
130 | def set_title(self, title): | |
131 | """Set buffer title.""" | |
132 | self.title.clear() | |
baa01601 | 133 | if title is not None: |
7dcf23b1 SH |
134 | self.title.setText(title) |
135 | ||
c728febd SH |
136 | def set_prompt(self, prompt): |
137 | """Set prompt.""" | |
138 | if self.hbox_edit.count() > 1: | |
139 | self.hbox_edit.takeAt(0) | |
baa01601 | 140 | if prompt is not None: |
c728febd SH |
141 | label = QtGui.QLabel(prompt) |
142 | label.setContentsMargins(0, 0, 5, 0) | |
143 | self.hbox_edit.insertWidget(0, label) | |
144 | ||
7dcf23b1 SH |
145 | |
146 | class Buffer(QtCore.QObject): | |
147 | """A WeeChat buffer.""" | |
148 | ||
149 | bufferInput = qt_compat.Signal(str, str) | |
150 | ||
151 | def __init__(self, data={}): | |
152 | QtCore.QObject.__init__(self) | |
153 | self.data = data | |
2a0b6adc | 154 | self.nicklist = {} |
77df9d06 SH |
155 | self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', |
156 | 0)) | |
c728febd SH |
157 | self.update_title() |
158 | self.update_prompt() | |
7dcf23b1 SH |
159 | self.widget.input.textSent.connect(self.input_text_sent) |
160 | ||
161 | def pointer(self): | |
162 | """Return pointer on buffer.""" | |
163 | return self.data.get('__path', [''])[0] | |
164 | ||
c728febd SH |
165 | def update_title(self): |
166 | """Update title.""" | |
167 | try: | |
77df9d06 SH |
168 | self.widget.set_title( |
169 | color.remove(self.data['title'].decode('utf-8'))) | |
c728febd SH |
170 | except: |
171 | self.widget.set_title(None) | |
172 | ||
173 | def update_prompt(self): | |
174 | """Update prompt.""" | |
175 | try: | |
176 | self.widget.set_prompt(self.data['local_variables']['nick']) | |
177 | except: | |
178 | self.widget.set_prompt(None) | |
179 | ||
7dcf23b1 SH |
180 | def input_text_sent(self, text): |
181 | """Called when text has to be sent to buffer.""" | |
182 | if self.data: | |
183 | self.bufferInput.emit(self.data['full_name'], text) | |
184 | ||
2a0b6adc SH |
185 | def nicklist_add_item(self, parent, group, prefix, name, visible): |
186 | """Add a group/nick in nicklist.""" | |
187 | if group: | |
77df9d06 SH |
188 | self.nicklist[name] = { |
189 | 'visible': visible, | |
190 | 'nicks': [] | |
191 | } | |
02cba1c1 | 192 | else: |
77df9d06 SH |
193 | self.nicklist[parent]['nicks'].append({'prefix': prefix, |
194 | 'name': name, | |
195 | 'visible': visible}) | |
2a0b6adc SH |
196 | |
197 | def nicklist_remove_item(self, parent, group, name): | |
198 | """Remove a group/nick from nicklist.""" | |
199 | if group: | |
200 | if name in self.nicklist: | |
201 | del self.nicklist[name] | |
202 | else: | |
203 | if parent in self.nicklist: | |
77df9d06 SH |
204 | self.nicklist[parent]['nicks'] = [ |
205 | nick for nick in self.nicklist[parent]['nicks'] | |
206 | if nick['name'] != name | |
207 | ] | |
2a0b6adc SH |
208 | |
209 | def nicklist_update_item(self, parent, group, prefix, name, visible): | |
210 | """Update a group/nick in nicklist.""" | |
211 | if group: | |
212 | if name in self.nicklist: | |
213 | self.nicklist[name]['visible'] = visible | |
214 | else: | |
215 | if parent in self.nicklist: | |
216 | for nick in self.nicklist[parent]['nicks']: | |
217 | if nick['name'] == name: | |
218 | nick['prefix'] = prefix | |
219 | nick['visible'] = visible | |
220 | break | |
221 | ||
222 | def nicklist_refresh(self): | |
223 | """Refresh nicklist.""" | |
02cba1c1 | 224 | self.widget.nicklist.clear() |
2a0b6adc | 225 | for group in sorted(self.nicklist): |
77df9d06 SH |
226 | for nick in sorted(self.nicklist[group]['nicks'], |
227 | key=lambda n: n['name']): | |
228 | prefix_color = {'': '', ' ': '', '+': 'yellow'} | |
2a0b6adc SH |
229 | color = prefix_color.get(nick['prefix'], 'green') |
230 | if color: | |
77df9d06 SH |
231 | icon = QtGui.QIcon( |
232 | resource_filename(__name__, | |
233 | 'data/icons/bullet_%s_8x8.png' % | |
234 | color)) | |
2a0b6adc SH |
235 | else: |
236 | pixmap = QtGui.QPixmap(8, 8) | |
237 | pixmap.fill() | |
238 | icon = QtGui.QIcon(pixmap) | |
239 | item = QtGui.QListWidgetItem(icon, nick['name']) | |
240 | self.widget.nicklist.addItem(item) | |
241 | self.widget.nicklist.setVisible(True) |