]> jfr.im git - irc/weechat/qweechat.git/blob - src/qweechat/buffer.py
Convert time object to integer (instead of string)
[irc/weechat/qweechat.git] / src / qweechat / buffer.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2011-2012 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
26 import qt_compat
27 QtCore = qt_compat.import_module('QtCore')
28 QtGui = qt_compat.import_module('QtGui')
29 from chat import ChatTextEdit
30 from input import InputLineEdit
31 import weechat.color as color
32
33
34 class GenericListWidget(QtGui.QListWidget):
35 """Generic QListWidget with dynamic size."""
36
37 def __init__(self, *args):
38 QtGui.QListWidget.__init__(*(self,) + args)
39 self.setMaximumWidth(100)
40 self.setTextElideMode(QtCore.Qt.ElideNone)
41 self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
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
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."""
56 QtGui.QListWidget.clear(*(self,) + args)
57 self.auto_resize()
58
59 def addItem(self, *args):
60 """Re-implement addItem to set dynamic size after add."""
61 QtGui.QListWidget.addItem(*(self,) + args)
62 self.auto_resize()
63
64 def insertItem(self, *args):
65 """Re-implement insertItem to set dynamic size after insert."""
66 QtGui.QListWidget.insertItem(*(self,) + args)
67 self.auto_resize()
68
69
70 class BufferListWidget(GenericListWidget):
71 """Widget with list of buffers."""
72
73 def __init__(self, *args):
74 GenericListWidget.__init__(*(self,) + args)
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
89 class 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()
97 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
98
99 # splitter with chat + nicklist
100 self.chat_nicklist = QtGui.QSplitter()
101 self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
102 self.chat = ChatTextEdit(debug=False)
103 self.chat_nicklist.addWidget(self.chat)
104 self.nicklist = GenericListWidget()
105 if not display_nicklist:
106 self.nicklist.setVisible(False)
107 self.chat_nicklist.addWidget(self.nicklist)
108
109 # prompt + input
110 self.hbox_edit = QtGui.QHBoxLayout()
111 self.hbox_edit.setContentsMargins(0, 0, 0, 0)
112 self.hbox_edit.setSpacing(0)
113 self.input = InputLineEdit(self.chat)
114 self.hbox_edit.addWidget(self.input)
115 prompt_input = QtGui.QWidget()
116 prompt_input.setLayout(self.hbox_edit)
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
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
143
144 class 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
152 self.nicklist = []
153 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0))
154 self.update_title()
155 self.update_prompt()
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
162 def update_title(self):
163 """Update title."""
164 try:
165 self.widget.set_title(color.remove(self.data['title']))
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
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."""
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()