2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 Sebastien Helleu <flashcode@flashtux.org>
6 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
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.
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.
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/>.
27 QtCore
= qt_compat
.import_module('QtCore')
28 QtGui
= qt_compat
.import_module('QtGui')
29 from chat
import ChatTextEdit
30 from input import InputLineEdit
33 class GenericListWidget(QtGui
.QListWidget
):
34 """Generic QListWidget with dynamic size."""
36 def __init__(self
, *args
):
37 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
)
43 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
44 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
47 def auto_resize(self
):
48 size
= self
.sizeHintForColumn(0)
51 self
.setMaximumWidth(size
)
53 def clear(self
, *args
):
54 """Re-implement clear to set dynamic size after clear."""
55 QtGui
.QListWidget
.clear(*(self
,) + args
)
58 def addItem(self
, *args
):
59 """Re-implement addItem to set dynamic size after add."""
60 QtGui
.QListWidget
.addItem(*(self
,) + args
)
63 def insertItem(self
, *args
):
64 """Re-implement insertItem to set dynamic size after insert."""
65 QtGui
.QListWidget
.insertItem(*(self
,) + args
)
69 class BufferListWidget(GenericListWidget
):
70 """Widget with list of buffers."""
72 def __init__(self
, *args
):
73 GenericListWidget
.__init
__(*(self
,) + args
)
75 def switch_prev_buffer(self
):
76 if self
.currentRow() > 0:
77 self
.setCurrentRow(self
.currentRow() - 1)
79 self
.setCurrentRow(self
.count() - 1)
81 def switch_next_buffer(self
):
82 if self
.currentRow() < self
.count() - 1:
83 self
.setCurrentRow(self
.currentRow() + 1)
88 class BufferWidget(QtGui
.QWidget
):
89 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
91 def __init__(self
, display_nicklist
=False):
92 QtGui
.QWidget
.__init
__(self
)
95 self
.title
= QtGui
.QLineEdit()
96 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
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(debug
=False)
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
)
109 self
.hbox_edit
= QtGui
.QHBoxLayout()
110 self
.hbox_edit
.setContentsMargins(0, 0, 0, 0)
111 self
.hbox_edit
.setSpacing(0)
112 self
.input = InputLineEdit(self
.chat
)
113 self
.hbox_edit
.addWidget(self
.input)
114 prompt_input
= QtGui
.QWidget()
115 prompt_input
.setLayout(self
.hbox_edit
)
117 # vbox with title + chat/nicklist + prompt/input
118 vbox
= QtGui
.QVBoxLayout()
119 vbox
.setContentsMargins(0, 0, 0, 0)
121 vbox
.addWidget(self
.title
)
122 vbox
.addWidget(self
.chat_nicklist
)
123 vbox
.addWidget(prompt_input
)
127 def set_title(self
, title
):
128 """Set buffer title."""
130 if not title
is None:
131 self
.title
.setText(title
)
133 def set_prompt(self
, prompt
):
135 if self
.hbox_edit
.count() > 1:
136 self
.hbox_edit
.takeAt(0)
137 if not prompt
is None:
138 label
= QtGui
.QLabel(prompt
)
139 label
.setContentsMargins(0, 0, 5, 0)
140 self
.hbox_edit
.insertWidget(0, label
)
143 class Buffer(QtCore
.QObject
):
144 """A WeeChat buffer."""
146 bufferInput
= qt_compat
.Signal(str, str)
148 def __init__(self
, data
={}):
149 QtCore
.QObject
.__init
__(self
)
152 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist', 0))
155 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
158 """Return pointer on buffer."""
159 return self
.data
.get('__path', [''])[0]
161 def update_title(self
):
164 self
.widget
.set_title(self
.data
['title'])
166 self
.widget
.set_title(None)
168 def update_prompt(self
):
171 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
173 self
.widget
.set_prompt(None)
175 def input_text_sent(self
, text
):
176 """Called when text has to be sent to buffer."""
178 self
.bufferInput
.emit(self
.data
['full_name'], text
)
180 def add_nick(self
, prefix
, nick
):
181 """Add a nick to nicklist."""
182 prefix_color
= { '': '', ' ': '', '+': 'yellow' }
183 self
.nicklist
.append((prefix
, nick
))
184 color
= prefix_color
.get(prefix
, 'green')
186 icon
= QtGui
.QIcon('data/icons/bullet_%s_8x8.png' % color
)
188 pixmap
= QtGui
.QPixmap(8, 8)
190 icon
= QtGui
.QIcon(pixmap
)
191 item
= QtGui
.QListWidgetItem(icon
, nick
)
192 #item.setFont('monospace')
193 self
.widget
.nicklist
.addItem(item
)
194 self
.widget
.nicklist
.setVisible(True)
196 def remove_all_nicks(self
):
197 """Remove all nicks from nicklist."""
199 self
.widget
.nicklist
.clear()