2 # -*- coding: utf-8 -*-
4 # buffer.py - management of WeeChat buffers/nicklist
6 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
10 # QWeeChat is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # QWeeChat is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
24 from pkg_resources
import resource_filename
26 QtCore
= qt_compat
.import_module('QtCore')
27 QtGui
= qt_compat
.import_module('QtGui')
28 from chat
import ChatTextEdit
29 from input import InputLineEdit
30 import weechat
.color
as color
33 class GenericListWidget(QtGui
.QListWidget
):
34 """Generic QListWidget with dynamic size."""
36 def __init__(self
, *args
):
37 QtGui
.QListWidget
.__init
__(*(self
,) + args
)
38 self
.setMaximumWidth(100)
39 self
.setTextElideMode(QtCore
.Qt
.ElideNone
)
40 self
.setHorizontalScrollBarPolicy(QtCore
.Qt
.ScrollBarAlwaysOff
)
41 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
43 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
44 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
47 def auto_resize(self
):
48 size
= self
.sizeHintForColumn(0)
51 self
.setMaximumWidth(size
)
53 def clear(self
, *args
):
54 """Re-implement clear to set dynamic size after clear."""
55 QtGui
.QListWidget
.clear(*(self
,) + args
)
58 def addItem(self
, *args
):
59 """Re-implement addItem to set dynamic size after add."""
60 QtGui
.QListWidget
.addItem(*(self
,) + args
)
63 def insertItem(self
, *args
):
64 """Re-implement insertItem to set dynamic size after insert."""
65 QtGui
.QListWidget
.insertItem(*(self
,) + args
)
69 class BufferListWidget(GenericListWidget
):
70 """Widget with list of buffers."""
72 def __init__(self
, *args
):
73 GenericListWidget
.__init
__(*(self
,) + args
)
75 def switch_prev_buffer(self
):
76 if self
.currentRow() > 0:
77 self
.setCurrentRow(self
.currentRow() - 1)
79 self
.setCurrentRow(self
.count() - 1)
81 def switch_next_buffer(self
):
82 if self
.currentRow() < self
.count() - 1:
83 self
.setCurrentRow(self
.currentRow() + 1)
88 class BufferWidget(QtGui
.QWidget
):
90 Widget with (from top to bottom):
91 title, chat + nicklist (optional) + prompt/input.
94 def __init__(self
, display_nicklist
=False):
95 QtGui
.QWidget
.__init
__(self
)
98 self
.title
= QtGui
.QLineEdit()
99 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
101 # splitter with chat + nicklist
102 self
.chat_nicklist
= QtGui
.QSplitter()
103 self
.chat_nicklist
.setSizePolicy(QtGui
.QSizePolicy
.Expanding
,
104 QtGui
.QSizePolicy
.Expanding
)
105 self
.chat
= ChatTextEdit(debug
=False)
106 self
.chat_nicklist
.addWidget(self
.chat
)
107 self
.nicklist
= GenericListWidget()
108 if not display_nicklist
:
109 self
.nicklist
.setVisible(False)
110 self
.chat_nicklist
.addWidget(self
.nicklist
)
113 self
.hbox_edit
= QtGui
.QHBoxLayout()
114 self
.hbox_edit
.setContentsMargins(0, 0, 0, 0)
115 self
.hbox_edit
.setSpacing(0)
116 self
.input = InputLineEdit(self
.chat
)
117 self
.hbox_edit
.addWidget(self
.input)
118 prompt_input
= QtGui
.QWidget()
119 prompt_input
.setLayout(self
.hbox_edit
)
121 # vbox with title + chat/nicklist + prompt/input
122 vbox
= QtGui
.QVBoxLayout()
123 vbox
.setContentsMargins(0, 0, 0, 0)
125 vbox
.addWidget(self
.title
)
126 vbox
.addWidget(self
.chat_nicklist
)
127 vbox
.addWidget(prompt_input
)
131 def set_title(self
, title
):
132 """Set buffer title."""
134 if title
is not None:
135 self
.title
.setText(title
)
137 def set_prompt(self
, prompt
):
139 if self
.hbox_edit
.count() > 1:
140 self
.hbox_edit
.takeAt(0)
141 if prompt
is not None:
142 label
= QtGui
.QLabel(prompt
)
143 label
.setContentsMargins(0, 0, 5, 0)
144 self
.hbox_edit
.insertWidget(0, label
)
147 class Buffer(QtCore
.QObject
):
148 """A WeeChat buffer."""
150 bufferInput
= qt_compat
.Signal(str, str)
152 def __init__(self
, data
={}):
153 QtCore
.QObject
.__init
__(self
)
156 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist',
160 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
163 """Return pointer on buffer."""
164 return self
.data
.get('__path', [''])[0]
166 def update_title(self
):
169 self
.widget
.set_title(
170 color
.remove(self
.data
['title'].decode('utf-8')))
172 self
.widget
.set_title(None)
174 def update_prompt(self
):
177 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
179 self
.widget
.set_prompt(None)
181 def input_text_sent(self
, text
):
182 """Called when text has to be sent to buffer."""
184 self
.bufferInput
.emit(self
.data
['full_name'], text
)
186 def nicklist_add_item(self
, parent
, group
, prefix
, name
, visible
):
187 """Add a group/nick in nicklist."""
189 self
.nicklist
[name
] = {
194 self
.nicklist
[parent
]['nicks'].append({'prefix': prefix
,
198 def nicklist_remove_item(self
, parent
, group
, name
):
199 """Remove a group/nick from nicklist."""
201 if name
in self
.nicklist
:
202 del self
.nicklist
[name
]
204 if parent
in self
.nicklist
:
205 self
.nicklist
[parent
]['nicks'] = [
206 nick
for nick
in self
.nicklist
[parent
]['nicks']
207 if nick
['name'] != name
210 def nicklist_update_item(self
, parent
, group
, prefix
, name
, visible
):
211 """Update a group/nick in nicklist."""
213 if name
in self
.nicklist
:
214 self
.nicklist
[name
]['visible'] = visible
216 if parent
in self
.nicklist
:
217 for nick
in self
.nicklist
[parent
]['nicks']:
218 if nick
['name'] == name
:
219 nick
['prefix'] = prefix
220 nick
['visible'] = visible
223 def nicklist_refresh(self
):
224 """Refresh nicklist."""
225 self
.widget
.nicklist
.clear()
226 for group
in sorted(self
.nicklist
):
227 for nick
in sorted(self
.nicklist
[group
]['nicks'],
228 key
=lambda n
: n
['name']):
229 prefix_color
= {'': '', ' ': '', '+': 'yellow'}
230 color
= prefix_color
.get(nick
['prefix'], 'green')
233 resource_filename(__name__
,
234 'data/icons/bullet_%s_8x8.png' %
237 pixmap
= QtGui
.QPixmap(8, 8)
239 icon
= QtGui
.QIcon(pixmap
)
240 item
= QtGui
.QListWidgetItem(icon
, nick
['name'])
241 self
.widget
.nicklist
.addItem(item
)
242 self
.widget
.nicklist
.setVisible(True)