]>
jfr.im git - irc/weechat/qweechat.git/blob - qweechat/chat.py
5b4be597b925d4ea776265a1a3a4c3ee093db76a
2 # -*- coding: utf-8 -*-
6 # Copyright (C) 2011-2014 Sébastien 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/>.
26 QtCore
= qt_compat
.import_module('QtCore')
27 QtGui
= qt_compat
.import_module('QtGui')
29 import weechat
.color
as color
32 class ChatTextEdit(QtGui
.QTextEdit
):
35 def __init__(self
, debug
, *args
):
36 QtGui
.QTextEdit
.__init
__(*(self
,) + args
)
39 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
40 self
.setFontFamily('monospace')
41 self
._textcolor
= self
.textColor()
42 self
._bgcolor
= QtGui
.QColor('#FFFFFF')
43 self
._setcolorcode
= {
44 'F': (self
.setTextColor
, self
._textcolor
),
45 'B': (self
.setTextBackgroundColor
, self
._bgcolor
)
48 '*': self
.setFontWeight
,
49 '_': self
.setFontUnderline
,
50 '/': self
.setFontItalic
54 '*': QtGui
.QFont
.Normal
,
59 '*': QtGui
.QFont
.Bold
,
64 self
._color
= color
.Color(config
.color_options(), self
.debug
)
66 def display(self
, time
, prefix
, text
, forcecolor
=None):
68 d
= datetime
.datetime
.now()
70 d
= datetime
.datetime
.fromtimestamp(float(time
))
71 self
.setTextColor(QtGui
.QColor('#999999'))
72 self
.insertPlainText(d
.strftime('%H:%M '))
73 prefix
= self
._color
.convert(prefix
)
74 text
= self
._color
.convert(text
)
77 prefix
= '\x01(F%s)%s' % (forcecolor
, prefix
)
78 text
= '\x01(F%s)%s' % (forcecolor
, text
)
80 self
._display
_with
_colors
(str(prefix
).decode('utf-8') + ' ')
82 self
._display
_with
_colors
(str(text
).decode('utf-8'))
84 self
.insertPlainText('\n')
86 self
.insertPlainText('\n')
89 def _display_with_colors(self
, string
):
90 self
.setTextColor(self
._textcolor
)
91 self
.setTextBackgroundColor(self
._bgcolor
)
92 self
._reset
_attributes
()
93 items
= string
.split('\x01')
94 for i
, item
in enumerate(items
):
95 if i
> 0 and item
.startswith('('):
102 self
._set
_attribute
(code
[0], True)
105 self
._set
_attribute
(code
[0], False)
107 # reset attributes and color
109 self
._reset
_attributes
()
110 self
._setcolorcode
[action
][0](
111 self
._setcolorcode
[action
][1])
113 # set attributes + color
114 while code
.startswith(('*', '!', '/', '_', '|',
117 self
._reset
_attributes
()
118 elif code
[0] in self
._setfont
:
121 not self
._font
[code
[0]])
124 self
._setcolorcode
[action
][0](
128 self
.insertPlainText(item
)
130 def _reset_attributes(self
):
132 for attr
in self
._setfont
:
133 self
._set
_attribute
(attr
, False)
135 def _set_attribute(self
, attr
, value
):
136 self
._font
[attr
] = value
137 self
._setfont
[attr
](self
._fontvalues
[self
._font
[attr
]][attr
])
139 def scroll_bottom(self
):
140 bar
= self
.verticalScrollBar()
141 bar
.setValue(bar
.maximum())