]> jfr.im git - irc/weechat/qweechat.git/blame - src/qweechat/buffer.py
Use OrderedDict (if python >= 2.7) to display ordered dicts in objects (debug window...
[irc/weechat/qweechat.git] / src / qweechat / buffer.py
CommitLineData
7dcf23b1
SH
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
e836cfb0
SH
4# buffer.py - management of WeeChat buffers/nicklist
5#
e17d5dc0 6# Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org>
7dcf23b1
SH
7#
8# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9#
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.
14#
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.
19#
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/>.
22#
23
7dcf23b1
SH
24import qt_compat
25QtCore = qt_compat.import_module('QtCore')
26QtGui = qt_compat.import_module('QtGui')
27from chat import ChatTextEdit
28from input import InputLineEdit
54bcac1f 29import weechat.color as color
7dcf23b1
SH
30
31
dc15599c
SH
32class 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
68class 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
87class BufferWidget(QtGui.QWidget):
88 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
89
90 def __init__(self, display_nicklist=False):
91 QtGui.QWidget.__init__(self)
92
93 # title
94 self.title = QtGui.QLineEdit()
02cba1c1 95 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
7dcf23b1
SH
96
97 # splitter with chat + nicklist
98 self.chat_nicklist = QtGui.QSplitter()
99 self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
3a5ec0c1 100 self.chat = ChatTextEdit(debug=False)
7dcf23b1 101 self.chat_nicklist.addWidget(self.chat)
dc15599c 102 self.nicklist = GenericListWidget()
7dcf23b1
SH
103 if not display_nicklist:
104 self.nicklist.setVisible(False)
105 self.chat_nicklist.addWidget(self.nicklist)
106
107 # prompt + input
c728febd
SH
108 self.hbox_edit = QtGui.QHBoxLayout()
109 self.hbox_edit.setContentsMargins(0, 0, 0, 0)
110 self.hbox_edit.setSpacing(0)
7dcf23b1 111 self.input = InputLineEdit(self.chat)
c728febd 112 self.hbox_edit.addWidget(self.input)
7dcf23b1 113 prompt_input = QtGui.QWidget()
c728febd 114 prompt_input.setLayout(self.hbox_edit)
7dcf23b1
SH
115
116 # vbox with title + chat/nicklist + prompt/input
117 vbox = QtGui.QVBoxLayout()
118 vbox.setContentsMargins(0, 0, 0, 0)
119 vbox.setSpacing(0)
120 vbox.addWidget(self.title)
121 vbox.addWidget(self.chat_nicklist)
122 vbox.addWidget(prompt_input)
123
124 self.setLayout(vbox)
125
126 def set_title(self, title):
127 """Set buffer title."""
128 self.title.clear()
129 if not title is None:
130 self.title.setText(title)
131
c728febd
SH
132 def set_prompt(self, prompt):
133 """Set prompt."""
134 if self.hbox_edit.count() > 1:
135 self.hbox_edit.takeAt(0)
136 if not prompt is None:
137 label = QtGui.QLabel(prompt)
138 label.setContentsMargins(0, 0, 5, 0)
139 self.hbox_edit.insertWidget(0, label)
140
7dcf23b1
SH
141
142class Buffer(QtCore.QObject):
143 """A WeeChat buffer."""
144
145 bufferInput = qt_compat.Signal(str, str)
146
147 def __init__(self, data={}):
148 QtCore.QObject.__init__(self)
149 self.data = data
02cba1c1 150 self.nicklist = []
7dcf23b1 151 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0))
c728febd
SH
152 self.update_title()
153 self.update_prompt()
7dcf23b1
SH
154 self.widget.input.textSent.connect(self.input_text_sent)
155
156 def pointer(self):
157 """Return pointer on buffer."""
158 return self.data.get('__path', [''])[0]
159
c728febd
SH
160 def update_title(self):
161 """Update title."""
162 try:
f4780bac 163 self.widget.set_title(color.remove(self.data['title'].decode('utf-8')))
c728febd
SH
164 except:
165 self.widget.set_title(None)
166
167 def update_prompt(self):
168 """Update prompt."""
169 try:
170 self.widget.set_prompt(self.data['local_variables']['nick'])
171 except:
172 self.widget.set_prompt(None)
173
7dcf23b1
SH
174 def input_text_sent(self, text):
175 """Called when text has to be sent to buffer."""
176 if self.data:
177 self.bufferInput.emit(self.data['full_name'], text)
178
179 def add_nick(self, prefix, nick):
180 """Add a nick to nicklist."""
02cba1c1
SH
181 prefix_color = { '': '', ' ': '', '+': 'yellow' }
182 self.nicklist.append((prefix, nick))
183 color = prefix_color.get(prefix, 'green')
184 if color:
185 icon = QtGui.QIcon('data/icons/bullet_%s_8x8.png' % color)
186 else:
187 pixmap = QtGui.QPixmap(8, 8)
188 pixmap.fill()
189 icon = QtGui.QIcon(pixmap)
190 item = QtGui.QListWidgetItem(icon, nick)
191 #item.setFont('monospace')
192 self.widget.nicklist.addItem(item)
193 self.widget.nicklist.setVisible(True)
194
195 def remove_all_nicks(self):
196 """Remove all nicks from nicklist."""
197 self.nicklist = []
198 self.widget.nicklist.clear()