2 # -*- coding: utf-8 -*-
6 # Copyright (C) 2011-2013 Sebastien 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
= { 'F': (self
.setTextColor
, self
._textcolor
),
44 'B': (self
.setTextBackgroundColor
, self
._bgcolor
) }
45 self
._setfont
= { '*': self
.setFontWeight
,
46 '_': self
.setFontUnderline
,
47 '/': self
.setFontItalic
}
48 self
._fontvalues
= { False: { '*': QtGui.QFont.Normal, '_': False, '/': False }
,
49 True: { '*': QtGui.QFont.Bold, '_': True, '/': True }
}
50 self
._color
= color
.Color(config
.color_options(), self
.debug
)
52 def display(self
, time
, prefix
, text
, forcecolor
=None):
54 d
= datetime
.datetime
.now()
56 d
= datetime
.datetime
.fromtimestamp(float(time
))
57 self
.setTextColor(QtGui
.QColor('#999999'))
58 self
.insertPlainText(d
.strftime('%H:%M '))
59 prefix
= self
._color
.convert(prefix
)
60 text
= self
._color
.convert(text
)
63 prefix
= '\x01(F%s)%s' % (forcecolor
, prefix
)
64 text
= '\x01(F%s)%s' % (forcecolor
, text
)
66 self
._display
_with
_colors
(str(prefix
).decode('utf-8') + ' ')
68 self
._display
_with
_colors
(str(text
).decode('utf-8'))
70 self
.insertPlainText('\n')
72 self
.insertPlainText('\n')
75 def _display_with_colors(self
, string
):
76 self
.setTextColor(self
._textcolor
)
77 self
.setTextBackgroundColor(self
._bgcolor
)
78 self
._reset
_attributes
()
79 items
= string
.split('\x01')
80 for i
, item
in enumerate(items
):
81 if i
> 0 and item
.startswith('('):
88 self
._set
_attribute
(code
[0], True)
91 self
._set
_attribute
(code
[0], False)
93 # reset attributes and color
95 self
._reset
_attributes
()
96 self
._setcolorcode
[action
][0](self
._setcolorcode
[action
][1])
98 # set attributes + color
99 while code
.startswith(('*', '!', '/', '_', '|', 'r')):
101 self
._reset
_attributes
()
102 elif code
[0] in self
._setfont
:
103 self
._set
_attribute
(code
[0], not self
._font
[code
[0]])
106 self
._setcolorcode
[action
][0](QtGui
.QColor(code
))
109 self
.insertPlainText(item
)
111 def _reset_attributes(self
):
113 for attr
in self
._setfont
:
114 self
._set
_attribute
(attr
, False)
116 def _set_attribute(self
, attr
, value
):
117 self
._font
[attr
] = value
118 self
._setfont
[attr
](self
._fontvalues
[self
._font
[attr
]][attr
])
120 def scroll_bottom(self
):
121 bar
= self
.verticalScrollBar()
122 bar
.setValue(bar
.maximum())