]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # buffer.py - management of WeeChat buffers/nicklist | |
5 | # | |
6 | # Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org> | |
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 | ||
24 | import qt_compat | |
25 | QtCore = qt_compat.import_module('QtCore') | |
26 | QtGui = qt_compat.import_module('QtGui') | |
27 | from chat import ChatTextEdit | |
28 | from input import InputLineEdit | |
29 | import weechat.color as color | |
30 | ||
31 | ||
32 | class GenericListWidget(QtGui.QListWidget): | |
33 | """Generic QListWidget with dynamic size.""" | |
34 | ||
35 | def __init__(self, *args): | |
36 | QtGui.QListWidget.__init__(*(self,) + args) | |
37 | self.setMaximumWidth(100) | |
38 | self.setTextElideMode(QtCore.Qt.ElideNone) | |
39 | self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) | |
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 | ||
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.""" | |
54 | QtGui.QListWidget.clear(*(self,) + args) | |
55 | self.auto_resize() | |
56 | ||
57 | def addItem(self, *args): | |
58 | """Re-implement addItem to set dynamic size after add.""" | |
59 | QtGui.QListWidget.addItem(*(self,) + args) | |
60 | self.auto_resize() | |
61 | ||
62 | def insertItem(self, *args): | |
63 | """Re-implement insertItem to set dynamic size after insert.""" | |
64 | QtGui.QListWidget.insertItem(*(self,) + args) | |
65 | self.auto_resize() | |
66 | ||
67 | ||
68 | class BufferListWidget(GenericListWidget): | |
69 | """Widget with list of buffers.""" | |
70 | ||
71 | def __init__(self, *args): | |
72 | GenericListWidget.__init__(*(self,) + args) | |
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 | ||
87 | class 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() | |
95 | self.title.setFocusPolicy(QtCore.Qt.NoFocus) | |
96 | ||
97 | # splitter with chat + nicklist | |
98 | self.chat_nicklist = QtGui.QSplitter() | |
99 | self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) | |
100 | self.chat = ChatTextEdit(debug=False) | |
101 | self.chat_nicklist.addWidget(self.chat) | |
102 | self.nicklist = GenericListWidget() | |
103 | if not display_nicklist: | |
104 | self.nicklist.setVisible(False) | |
105 | self.chat_nicklist.addWidget(self.nicklist) | |
106 | ||
107 | # prompt + input | |
108 | self.hbox_edit = QtGui.QHBoxLayout() | |
109 | self.hbox_edit.setContentsMargins(0, 0, 0, 0) | |
110 | self.hbox_edit.setSpacing(0) | |
111 | self.input = InputLineEdit(self.chat) | |
112 | self.hbox_edit.addWidget(self.input) | |
113 | prompt_input = QtGui.QWidget() | |
114 | prompt_input.setLayout(self.hbox_edit) | |
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 | ||
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 | ||
141 | ||
142 | class 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 | |
150 | self.nicklist = [] | |
151 | self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0)) | |
152 | self.update_title() | |
153 | self.update_prompt() | |
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 | ||
160 | def update_title(self): | |
161 | """Update title.""" | |
162 | try: | |
163 | self.widget.set_title(color.remove(self.data['title'].decode('utf-8'))) | |
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 | ||
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.""" | |
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() |