]> jfr.im git - irc/weechat/qweechat.git/blame - src/qweechat/buffer.py
Add missing utf-8 decoding for buffer title
[irc/weechat/qweechat.git] / src / qweechat / buffer.py
CommitLineData
7dcf23b1
SH
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
d1b4884d 4# Copyright (C) 2011-2012 Sebastien Helleu <flashcode@flashtux.org>
7dcf23b1
SH
5#
6# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
7#
8# QWeeChat is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 3 of the License, or
11# (at your option) any later version.
12#
13# QWeeChat is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
20#
21
22#
23# Buffers.
24#
25
26import qt_compat
27QtCore = qt_compat.import_module('QtCore')
28QtGui = qt_compat.import_module('QtGui')
29from chat import ChatTextEdit
30from input import InputLineEdit
54bcac1f 31import weechat.color as color
7dcf23b1
SH
32
33
dc15599c
SH
34class GenericListWidget(QtGui.QListWidget):
35 """Generic QListWidget with dynamic size."""
7dcf23b1
SH
36
37 def __init__(self, *args):
67ae3204 38 QtGui.QListWidget.__init__(*(self,) + 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."""
67ae3204 56 QtGui.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."""
67ae3204 61 QtGui.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."""
67ae3204 66 QtGui.QListWidget.insertItem(*(self,) + args)
02cba1c1 67 self.auto_resize()
dc15599c
SH
68
69
70class BufferListWidget(GenericListWidget):
71 """Widget with list of buffers."""
72
73 def __init__(self, *args):
67ae3204 74 GenericListWidget.__init__(*(self,) + args)
7dcf23b1
SH
75
76 def switch_prev_buffer(self):
77 if self.currentRow() > 0:
78 self.setCurrentRow(self.currentRow() - 1)
79 else:
80 self.setCurrentRow(self.count() - 1)
81
82 def switch_next_buffer(self):
83 if self.currentRow() < self.count() - 1:
84 self.setCurrentRow(self.currentRow() + 1)
85 else:
86 self.setCurrentRow(0)
87
88
89class BufferWidget(QtGui.QWidget):
90 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
91
92 def __init__(self, display_nicklist=False):
93 QtGui.QWidget.__init__(self)
94
95 # title
96 self.title = QtGui.QLineEdit()
02cba1c1 97 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
7dcf23b1
SH
98
99 # splitter with chat + nicklist
100 self.chat_nicklist = QtGui.QSplitter()
101 self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
3a5ec0c1 102 self.chat = ChatTextEdit(debug=False)
7dcf23b1 103 self.chat_nicklist.addWidget(self.chat)
dc15599c 104 self.nicklist = GenericListWidget()
7dcf23b1
SH
105 if not display_nicklist:
106 self.nicklist.setVisible(False)
107 self.chat_nicklist.addWidget(self.nicklist)
108
109 # prompt + input
c728febd
SH
110 self.hbox_edit = QtGui.QHBoxLayout()
111 self.hbox_edit.setContentsMargins(0, 0, 0, 0)
112 self.hbox_edit.setSpacing(0)
7dcf23b1 113 self.input = InputLineEdit(self.chat)
c728febd 114 self.hbox_edit.addWidget(self.input)
7dcf23b1 115 prompt_input = QtGui.QWidget()
c728febd 116 prompt_input.setLayout(self.hbox_edit)
7dcf23b1
SH
117
118 # vbox with title + chat/nicklist + prompt/input
119 vbox = QtGui.QVBoxLayout()
120 vbox.setContentsMargins(0, 0, 0, 0)
121 vbox.setSpacing(0)
122 vbox.addWidget(self.title)
123 vbox.addWidget(self.chat_nicklist)
124 vbox.addWidget(prompt_input)
125
126 self.setLayout(vbox)
127
128 def set_title(self, title):
129 """Set buffer title."""
130 self.title.clear()
131 if not title is None:
132 self.title.setText(title)
133
c728febd
SH
134 def set_prompt(self, prompt):
135 """Set prompt."""
136 if self.hbox_edit.count() > 1:
137 self.hbox_edit.takeAt(0)
138 if not prompt is None:
139 label = QtGui.QLabel(prompt)
140 label.setContentsMargins(0, 0, 5, 0)
141 self.hbox_edit.insertWidget(0, label)
142
7dcf23b1
SH
143
144class Buffer(QtCore.QObject):
145 """A WeeChat buffer."""
146
147 bufferInput = qt_compat.Signal(str, str)
148
149 def __init__(self, data={}):
150 QtCore.QObject.__init__(self)
151 self.data = data
02cba1c1 152 self.nicklist = []
7dcf23b1 153 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0))
c728febd
SH
154 self.update_title()
155 self.update_prompt()
7dcf23b1
SH
156 self.widget.input.textSent.connect(self.input_text_sent)
157
158 def pointer(self):
159 """Return pointer on buffer."""
160 return self.data.get('__path', [''])[0]
161
c728febd
SH
162 def update_title(self):
163 """Update title."""
164 try:
f4780bac 165 self.widget.set_title(color.remove(self.data['title'].decode('utf-8')))
c728febd
SH
166 except:
167 self.widget.set_title(None)
168
169 def update_prompt(self):
170 """Update prompt."""
171 try:
172 self.widget.set_prompt(self.data['local_variables']['nick'])
173 except:
174 self.widget.set_prompt(None)
175
7dcf23b1
SH
176 def input_text_sent(self, text):
177 """Called when text has to be sent to buffer."""
178 if self.data:
179 self.bufferInput.emit(self.data['full_name'], text)
180
181 def add_nick(self, prefix, nick):
182 """Add a nick to nicklist."""
02cba1c1
SH
183 prefix_color = { '': '', ' ': '', '+': 'yellow' }
184 self.nicklist.append((prefix, nick))
185 color = prefix_color.get(prefix, 'green')
186 if color:
187 icon = QtGui.QIcon('data/icons/bullet_%s_8x8.png' % color)
188 else:
189 pixmap = QtGui.QPixmap(8, 8)
190 pixmap.fill()
191 icon = QtGui.QIcon(pixmap)
192 item = QtGui.QListWidgetItem(icon, nick)
193 #item.setFont('monospace')
194 self.widget.nicklist.addItem(item)
195 self.widget.nicklist.setVisible(True)
196
197 def remove_all_nicks(self):
198 """Remove all nicks from nicklist."""
199 self.nicklist = []
200 self.widget.nicklist.clear()