]> jfr.im git - irc/weechat/qweechat.git/blame - src/qweechat/buffer.py
Replace calls to "apply(f, args)" with "f(*args)"
[irc/weechat/qweechat.git] / src / qweechat / buffer.py
CommitLineData
7dcf23b1
SH
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2011 Sebastien Helleu <flashcode@flashtux.org>
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
31
32
dc15599c
SH
33class GenericListWidget(QtGui.QListWidget):
34 """Generic QListWidget with dynamic size."""
7dcf23b1
SH
35
36 def __init__(self, *args):
67ae3204 37 QtGui.QListWidget.__init__(*(self,) + args)
dc15599c
SH
38 self.setMaximumWidth(100)
39 self.setTextElideMode(QtCore.Qt.ElideNone)
7dcf23b1 40 self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
7dcf23b1
SH
41 self.setFocusPolicy(QtCore.Qt.NoFocus)
42 pal = self.palette()
43 pal.setColor(QtGui.QPalette.Highlight, QtGui.QColor('#ddddff'))
44 pal.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor('black'))
45 self.setPalette(pal)
46
02cba1c1
SH
47 def auto_resize(self):
48 size = self.sizeHintForColumn(0)
49 if size > 0:
50 size += 4
51 self.setMaximumWidth(size)
52
53 def clear(self, *args):
54 """Re-implement clear to set dynamic size after clear."""
67ae3204 55 QtGui.QListWidget.clear(*(self,) + args)
02cba1c1
SH
56 self.auto_resize()
57
dc15599c
SH
58 def addItem(self, *args):
59 """Re-implement addItem to set dynamic size after add."""
67ae3204 60 QtGui.QListWidget.addItem(*(self,) + args)
02cba1c1
SH
61 self.auto_resize()
62
63 def insertItem(self, *args):
64 """Re-implement insertItem to set dynamic size after insert."""
67ae3204 65 QtGui.QListWidget.insertItem(*(self,) + args)
02cba1c1 66 self.auto_resize()
dc15599c
SH
67
68
69class BufferListWidget(GenericListWidget):
70 """Widget with list of buffers."""
71
72 def __init__(self, *args):
67ae3204 73 GenericListWidget.__init__(*(self,) + args)
7dcf23b1
SH
74
75 def switch_prev_buffer(self):
76 if self.currentRow() > 0:
77 self.setCurrentRow(self.currentRow() - 1)
78 else:
79 self.setCurrentRow(self.count() - 1)
80
81 def switch_next_buffer(self):
82 if self.currentRow() < self.count() - 1:
83 self.setCurrentRow(self.currentRow() + 1)
84 else:
85 self.setCurrentRow(0)
86
87
88class BufferWidget(QtGui.QWidget):
89 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
90
91 def __init__(self, display_nicklist=False):
92 QtGui.QWidget.__init__(self)
93
94 # title
95 self.title = QtGui.QLineEdit()
02cba1c1 96 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
7dcf23b1
SH
97
98 # splitter with chat + nicklist
99 self.chat_nicklist = QtGui.QSplitter()
100 self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
3a5ec0c1 101 self.chat = ChatTextEdit(debug=False)
7dcf23b1 102 self.chat_nicklist.addWidget(self.chat)
dc15599c 103 self.nicklist = GenericListWidget()
7dcf23b1
SH
104 if not display_nicklist:
105 self.nicklist.setVisible(False)
106 self.chat_nicklist.addWidget(self.nicklist)
107
108 # prompt + input
c728febd
SH
109 self.hbox_edit = QtGui.QHBoxLayout()
110 self.hbox_edit.setContentsMargins(0, 0, 0, 0)
111 self.hbox_edit.setSpacing(0)
7dcf23b1 112 self.input = InputLineEdit(self.chat)
c728febd 113 self.hbox_edit.addWidget(self.input)
7dcf23b1 114 prompt_input = QtGui.QWidget()
c728febd 115 prompt_input.setLayout(self.hbox_edit)
7dcf23b1
SH
116
117 # vbox with title + chat/nicklist + prompt/input
118 vbox = QtGui.QVBoxLayout()
119 vbox.setContentsMargins(0, 0, 0, 0)
120 vbox.setSpacing(0)
121 vbox.addWidget(self.title)
122 vbox.addWidget(self.chat_nicklist)
123 vbox.addWidget(prompt_input)
124
125 self.setLayout(vbox)
126
127 def set_title(self, title):
128 """Set buffer title."""
129 self.title.clear()
130 if not title is None:
131 self.title.setText(title)
132
c728febd
SH
133 def set_prompt(self, prompt):
134 """Set prompt."""
135 if self.hbox_edit.count() > 1:
136 self.hbox_edit.takeAt(0)
137 if not prompt is None:
138 label = QtGui.QLabel(prompt)
139 label.setContentsMargins(0, 0, 5, 0)
140 self.hbox_edit.insertWidget(0, label)
141
7dcf23b1
SH
142
143class Buffer(QtCore.QObject):
144 """A WeeChat buffer."""
145
146 bufferInput = qt_compat.Signal(str, str)
147
148 def __init__(self, data={}):
149 QtCore.QObject.__init__(self)
150 self.data = data
02cba1c1 151 self.nicklist = []
7dcf23b1 152 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0))
c728febd
SH
153 self.update_title()
154 self.update_prompt()
7dcf23b1
SH
155 self.widget.input.textSent.connect(self.input_text_sent)
156
157 def pointer(self):
158 """Return pointer on buffer."""
159 return self.data.get('__path', [''])[0]
160
c728febd
SH
161 def update_title(self):
162 """Update title."""
163 try:
164 self.widget.set_title(self.data['title'])
165 except:
166 self.widget.set_title(None)
167
168 def update_prompt(self):
169 """Update prompt."""
170 try:
171 self.widget.set_prompt(self.data['local_variables']['nick'])
172 except:
173 self.widget.set_prompt(None)
174
7dcf23b1
SH
175 def input_text_sent(self, text):
176 """Called when text has to be sent to buffer."""
177 if self.data:
178 self.bufferInput.emit(self.data['full_name'], text)
179
180 def add_nick(self, prefix, nick):
181 """Add a nick to nicklist."""
02cba1c1
SH
182 prefix_color = { '': '', ' ': '', '+': 'yellow' }
183 self.nicklist.append((prefix, nick))
184 color = prefix_color.get(prefix, 'green')
185 if color:
186 icon = QtGui.QIcon('data/icons/bullet_%s_8x8.png' % color)
187 else:
188 pixmap = QtGui.QPixmap(8, 8)
189 pixmap.fill()
190 icon = QtGui.QIcon(pixmap)
191 item = QtGui.QListWidgetItem(icon, nick)
192 #item.setFont('monospace')
193 self.widget.nicklist.addItem(item)
194 self.widget.nicklist.setVisible(True)
195
196 def remove_all_nicks(self):
197 """Remove all nicks from nicklist."""
198 self.nicklist = []
199 self.widget.nicklist.clear()