2 # -*- coding: utf-8 -*-
4 # buffer.py - management of WeeChat buffers/nicklist
6 # Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org>
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
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.
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.
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/>.
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
32 class GenericListWidget(QtGui
.QListWidget
):
33 """Generic QListWidget with dynamic size."""
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
)
42 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
43 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
46 def auto_resize(self
):
47 size
= self
.sizeHintForColumn(0)
50 self
.setMaximumWidth(size
)
52 def clear(self
, *args
):
53 """Re-implement clear to set dynamic size after clear."""
54 QtGui
.QListWidget
.clear(*(self
,) + args
)
57 def addItem(self
, *args
):
58 """Re-implement addItem to set dynamic size after add."""
59 QtGui
.QListWidget
.addItem(*(self
,) + args
)
62 def insertItem(self
, *args
):
63 """Re-implement insertItem to set dynamic size after insert."""
64 QtGui
.QListWidget
.insertItem(*(self
,) + args
)
68 class BufferListWidget(GenericListWidget
):
69 """Widget with list of buffers."""
71 def __init__(self
, *args
):
72 GenericListWidget
.__init
__(*(self
,) + args
)
74 def switch_prev_buffer(self
):
75 if self
.currentRow() > 0:
76 self
.setCurrentRow(self
.currentRow() - 1)
78 self
.setCurrentRow(self
.count() - 1)
80 def switch_next_buffer(self
):
81 if self
.currentRow() < self
.count() - 1:
82 self
.setCurrentRow(self
.currentRow() + 1)
87 class BufferWidget(QtGui
.QWidget
):
88 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
90 def __init__(self
, display_nicklist
=False):
91 QtGui
.QWidget
.__init
__(self
)
94 self
.title
= QtGui
.QLineEdit()
95 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
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
)
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
)
116 # vbox with title + chat/nicklist + prompt/input
117 vbox
= QtGui
.QVBoxLayout()
118 vbox
.setContentsMargins(0, 0, 0, 0)
120 vbox
.addWidget(self
.title
)
121 vbox
.addWidget(self
.chat_nicklist
)
122 vbox
.addWidget(prompt_input
)
126 def set_title(self
, title
):
127 """Set buffer title."""
129 if not title
is None:
130 self
.title
.setText(title
)
132 def set_prompt(self
, 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
)
142 class Buffer(QtCore
.QObject
):
143 """A WeeChat buffer."""
145 bufferInput
= qt_compat
.Signal(str, str)
147 def __init__(self
, data
={}):
148 QtCore
.QObject
.__init
__(self
)
151 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist', 0))
154 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
157 """Return pointer on buffer."""
158 return self
.data
.get('__path', [''])[0]
160 def update_title(self
):
163 self
.widget
.set_title(color
.remove(self
.data
['title'].decode('utf-8')))
165 self
.widget
.set_title(None)
167 def update_prompt(self
):
170 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
172 self
.widget
.set_prompt(None)
174 def input_text_sent(self
, text
):
175 """Called when text has to be sent to buffer."""
177 self
.bufferInput
.emit(self
.data
['full_name'], text
)
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')
185 icon
= QtGui
.QIcon('data/icons/bullet_%s_8x8.png' % color
)
187 pixmap
= QtGui
.QPixmap(8, 8)
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)
195 def remove_all_nicks(self
):
196 """Remove all nicks from nicklist."""
198 self
.widget
.nicklist
.clear()