]>
jfr.im git - irc/weechat/qweechat.git/blob - qweechat/chat.py
1 # -*- coding: utf-8 -*-
5 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9 # QWeeChat is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # QWeeChat is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # 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')
28 import weechat
.color
as color
31 class ChatTextEdit(QtGui
.QTextEdit
):
34 def __init__(self
, debug
, *args
):
35 QtGui
.QTextEdit
.__init
__(*(self
,) + args
)
38 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
39 self
.setFontFamily('monospace')
40 self
._textcolor
= self
.textColor()
41 self
._bgcolor
= QtGui
.QColor('#FFFFFF')
42 self
._setcolorcode
= {
43 'F': (self
.setTextColor
, self
._textcolor
),
44 'B': (self
.setTextBackgroundColor
, self
._bgcolor
)
47 '*': self
.setFontWeight
,
48 '_': self
.setFontUnderline
,
49 '/': self
.setFontItalic
53 '*': QtGui
.QFont
.Normal
,
58 '*': QtGui
.QFont
.Bold
,
63 self
._color
= color
.Color(config
.color_options(), self
.debug
)
65 def display(self
, time
, prefix
, text
, forcecolor
=None):
67 d
= datetime
.datetime
.now()
69 d
= datetime
.datetime
.fromtimestamp(float(time
))
70 self
.setTextColor(QtGui
.QColor('#999999'))
71 self
.insertPlainText(d
.strftime('%H:%M '))
72 prefix
= self
._color
.convert(prefix
)
73 text
= self
._color
.convert(text
)
76 prefix
= '\x01(F%s)%s' % (forcecolor
, prefix
)
77 text
= '\x01(F%s)%s' % (forcecolor
, text
)
79 self
._display
_with
_colors
(str(prefix
).decode('utf-8') + ' ')
81 self
._display
_with
_colors
(str(text
).decode('utf-8'))
83 self
.insertPlainText('\n')
85 self
.insertPlainText('\n')
88 def _display_with_colors(self
, string
):
89 self
.setTextColor(self
._textcolor
)
90 self
.setTextBackgroundColor(self
._bgcolor
)
91 self
._reset
_attributes
()
92 items
= string
.split('\x01')
93 for i
, item
in enumerate(items
):
94 if i
> 0 and item
.startswith('('):
101 self
._set
_attribute
(code
[0], True)
104 self
._set
_attribute
(code
[0], False)
106 # reset attributes and color
108 self
._reset
_attributes
()
109 self
._setcolorcode
[action
][0](
110 self
._setcolorcode
[action
][1])
112 # set attributes + color
113 while code
.startswith(('*', '!', '/', '_', '|',
116 self
._reset
_attributes
()
117 elif code
[0] in self
._setfont
:
120 not self
._font
[code
[0]])
123 self
._setcolorcode
[action
][0](
127 self
.insertPlainText(item
)
129 def _reset_attributes(self
):
131 for attr
in self
._setfont
:
132 self
._set
_attribute
(attr
, False)
134 def _set_attribute(self
, attr
, value
):
135 self
._font
[attr
] = value
136 self
._setfont
[attr
](self
._fontvalues
[self
._font
[attr
]][attr
])
138 def scroll_bottom(self
):
139 bar
= self
.verticalScrollBar()
140 bar
.setValue(bar
.maximum())