]> jfr.im git - irc/weechat/qweechat.git/blob - src/qweechat/buffer.py
Add auto-resize of nicklist on insertItem/clear, set NoFocus on title, add bullets...
[irc/weechat/qweechat.git] / src / qweechat / buffer.py
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
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
32
33 class GenericListWidget(QtGui.QListWidget):
34 """Generic QListWidget with dynamic size."""
35
36 def __init__(self, *args):
37 apply(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)
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
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."""
55 apply(QtGui.QListWidget.clear, (self,) + args)
56 self.auto_resize()
57
58 def addItem(self, *args):
59 """Re-implement addItem to set dynamic size after add."""
60 apply(QtGui.QListWidget.addItem, (self,) + args)
61 self.auto_resize()
62
63 def insertItem(self, *args):
64 """Re-implement insertItem to set dynamic size after insert."""
65 apply(QtGui.QListWidget.insertItem, (self,) + args)
66 self.auto_resize()
67
68
69 class BufferListWidget(GenericListWidget):
70 """Widget with list of buffers."""
71
72 def __init__(self, *args):
73 apply(GenericListWidget.__init__, (self,) + args)
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
88 class 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()
96 self.title.setFocusPolicy(QtCore.Qt.NoFocus)
97
98 # splitter with chat + nicklist
99 self.chat_nicklist = QtGui.QSplitter()
100 self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
101 self.chat = ChatTextEdit()
102 self.chat_nicklist.addWidget(self.chat)
103 self.nicklist = GenericListWidget()
104 if not display_nicklist:
105 self.nicklist.setVisible(False)
106 self.chat_nicklist.addWidget(self.nicklist)
107
108 # prompt + input
109 hbox_edit = QtGui.QHBoxLayout()
110 hbox_edit.setContentsMargins(0, 0, 0, 0)
111 hbox_edit.setSpacing(0)
112 self.prompt = QtGui.QLabel('FlashCode')
113 self.prompt.setContentsMargins(0, 0, 5, 0)
114 hbox_edit.addWidget(self.prompt)
115 self.input = InputLineEdit(self.chat)
116 hbox_edit.addWidget(self.input)
117 prompt_input = QtGui.QWidget()
118 prompt_input.setLayout(hbox_edit)
119 prompt_input.setContentsMargins(0, 0, 0, 0)
120
121 # vbox with title + chat/nicklist + prompt/input
122 vbox = QtGui.QVBoxLayout()
123 vbox.setContentsMargins(0, 0, 0, 0)
124 vbox.setSpacing(0)
125 vbox.addWidget(self.title)
126 vbox.addWidget(self.chat_nicklist)
127 vbox.addWidget(prompt_input)
128
129 self.setLayout(vbox)
130
131 def set_title(self, title):
132 """Set buffer title."""
133 self.title.clear()
134 if not title is None:
135 self.title.setText(title)
136
137
138 class Buffer(QtCore.QObject):
139 """A WeeChat buffer."""
140
141 bufferInput = qt_compat.Signal(str, str)
142
143 def __init__(self, data={}):
144 QtCore.QObject.__init__(self)
145 self.data = data
146 self.nicklist = []
147 self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0))
148 if self.data and self.data['title']:
149 self.widget.set_title(self.data['title'])
150 self.widget.input.textSent.connect(self.input_text_sent)
151
152 def pointer(self):
153 """Return pointer on buffer."""
154 return self.data.get('__path', [''])[0]
155
156 def input_text_sent(self, text):
157 """Called when text has to be sent to buffer."""
158 if self.data:
159 self.bufferInput.emit(self.data['full_name'], text)
160
161 def add_nick(self, prefix, nick):
162 """Add a nick to nicklist."""
163 prefix_color = { '': '', ' ': '', '+': 'yellow' }
164 self.nicklist.append((prefix, nick))
165 color = prefix_color.get(prefix, 'green')
166 if color:
167 icon = QtGui.QIcon('data/icons/bullet_%s_8x8.png' % color)
168 else:
169 pixmap = QtGui.QPixmap(8, 8)
170 pixmap.fill()
171 icon = QtGui.QIcon(pixmap)
172 item = QtGui.QListWidgetItem(icon, nick)
173 #item.setFont('monospace')
174 self.widget.nicklist.addItem(item)
175 self.widget.nicklist.setVisible(True)
176
177 def remove_all_nicks(self):
178 """Remove all nicks from nicklist."""
179 self.nicklist = []
180 self.widget.nicklist.clear()