2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011-2012 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
31 import weechat
.color
as color
34 class GenericListWidget(QtGui
.QListWidget
):
35 """Generic QListWidget with dynamic size."""
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
)
44 pal
.setColor(QtGui
.QPalette
.Highlight
, QtGui
.QColor('#ddddff'))
45 pal
.setColor(QtGui
.QPalette
.HighlightedText
, QtGui
.QColor('black'))
48 def auto_resize(self
):
49 size
= self
.sizeHintForColumn(0)
52 self
.setMaximumWidth(size
)
54 def clear(self
, *args
):
55 """Re-implement clear to set dynamic size after clear."""
56 QtGui
.QListWidget
.clear(*(self
,) + args
)
59 def addItem(self
, *args
):
60 """Re-implement addItem to set dynamic size after add."""
61 QtGui
.QListWidget
.addItem(*(self
,) + args
)
64 def insertItem(self
, *args
):
65 """Re-implement insertItem to set dynamic size after insert."""
66 QtGui
.QListWidget
.insertItem(*(self
,) + args
)
70 class BufferListWidget(GenericListWidget
):
71 """Widget with list of buffers."""
73 def __init__(self
, *args
):
74 GenericListWidget
.__init
__(*(self
,) + args
)
76 def switch_prev_buffer(self
):
77 if self
.currentRow() > 0:
78 self
.setCurrentRow(self
.currentRow() - 1)
80 self
.setCurrentRow(self
.count() - 1)
82 def switch_next_buffer(self
):
83 if self
.currentRow() < self
.count() - 1:
84 self
.setCurrentRow(self
.currentRow() + 1)
89 class BufferWidget(QtGui
.QWidget
):
90 """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input."""
92 def __init__(self
, display_nicklist
=False):
93 QtGui
.QWidget
.__init
__(self
)
96 self
.title
= QtGui
.QLineEdit()
97 self
.title
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
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
)
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
)
118 # vbox with title + chat/nicklist + prompt/input
119 vbox
= QtGui
.QVBoxLayout()
120 vbox
.setContentsMargins(0, 0, 0, 0)
122 vbox
.addWidget(self
.title
)
123 vbox
.addWidget(self
.chat_nicklist
)
124 vbox
.addWidget(prompt_input
)
128 def set_title(self
, title
):
129 """Set buffer title."""
131 if not title
is None:
132 self
.title
.setText(title
)
134 def set_prompt(self
, 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
)
144 class Buffer(QtCore
.QObject
):
145 """A WeeChat buffer."""
147 bufferInput
= qt_compat
.Signal(str, str)
149 def __init__(self
, data
={}):
150 QtCore
.QObject
.__init
__(self
)
153 self
.widget
= BufferWidget(display_nicklist
=self
.data
.get('nicklist', 0))
156 self
.widget
.input.textSent
.connect(self
.input_text_sent
)
159 """Return pointer on buffer."""
160 return self
.data
.get('__path', [''])[0]
162 def update_title(self
):
165 self
.widget
.set_title(color
.remove(self
.data
['title']))
167 self
.widget
.set_title(None)
169 def update_prompt(self
):
172 self
.widget
.set_prompt(self
.data
['local_variables']['nick'])
174 self
.widget
.set_prompt(None)
176 def input_text_sent(self
, text
):
177 """Called when text has to be sent to buffer."""
179 self
.bufferInput
.emit(self
.data
['full_name'], text
)
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')
187 icon
= QtGui
.QIcon('data/icons/bullet_%s_8x8.png' % color
)
189 pixmap
= QtGui
.QPixmap(8, 8)
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)
197 def remove_all_nicks(self
):
198 """Remove all nicks from nicklist."""
200 self
.widget
.nicklist
.clear()