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 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
)
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 apply(QtGui
.QListWidget
.clear
, (self
,) + args
)
58 def addItem(self
, *args
):
59 """Re-implement addItem to set dynamic size after add."""
60 apply(QtGui
.QListWidget
.addItem
, (self
,) + args
)
63 def insertItem(self
, *args
):
64 """Re-implement insertItem to set dynamic size after insert."""
65 apply(QtGui
.QListWidget
.insertItem
, (self
,) + args
)
69 class BufferListWidget(GenericListWidget
):
70 """Widget with list of buffers."""
72 def __init__(self
, *args
):
73 apply(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()
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 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)
121 # vbox with title + chat/nicklist + prompt/input
122 vbox
= QtGui
.QVBoxLayout()
123 vbox
.setContentsMargins(0, 0, 0, 0)
125 vbox
.addWidget(self
.title
)
126 vbox
.addWidget(self
.chat_nicklist
)
127 vbox
.addWidget(prompt_input
)
131 def set_title(self
, title
):
132 """Set buffer title."""
134 if not title
is None:
135 self
.title
.setText(title
)
138 class Buffer(QtCore
.QObject
):
139 """A WeeChat buffer."""
141 bufferInput
= qt_compat
.Signal(str, str)
143 def __init__(self
, data
={}):
144 QtCore
.QObject
.__init
__(self
)
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
)
153 """Return pointer on buffer."""
154 return self
.data
.get('__path', [''])[0]
156 def input_text_sent(self
, text
):
157 """Called when text has to be sent to buffer."""
159 self
.bufferInput
.emit(self
.data
['full_name'], text
)
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')
167 icon
= QtGui
.QIcon('data/icons/bullet_%s_8x8.png' % color
)
169 pixmap
= QtGui
.QPixmap(8, 8)
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)
177 def remove_all_nicks(self
):
178 """Remove all nicks from nicklist."""
180 self
.widget
.nicklist
.clear()