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/>.
28 QtCore
= qt_compat
.import_module('QtCore')
29 QtGui
= qt_compat
.import_module('QtGui')
31 import weechat
.color
as color
34 class ChatTextEdit(QtGui
.QTextEdit
):
37 def __init__(self
, debug
, *args
):
38 QtGui
.QTextEdit
.__init
__(*(self
,) + args
)
41 self
.setFocusPolicy(QtCore
.Qt
.NoFocus
)
42 self
.setFontFamily('monospace')
43 self
._textcolor
= self
.textColor()
44 self
._bgcolor
= QtGui
.QColor('#FFFFFF')
45 self
._setcolorcode
= { 'F': (self
.setTextColor
, self
._textcolor
),
46 'B': (self
.setTextBackgroundColor
, self
._bgcolor
) }
47 self
._setfont
= { '*': self
.setFontWeight
,
48 '_': self
.setFontUnderline
,
49 '/': self
.setFontItalic
}
50 self
._fontvalues
= { False: { '*': QtGui.QFont.Normal, '_': False, '/': False }
,
51 True: { '*': QtGui.QFont.Bold, '_': True, '/': True }
}
52 self
._color
= color
.Color(config
.color_options(), self
.debug
)
54 def display(self
, time
, prefix
, text
, forcecolor
=None):
56 d
= datetime
.datetime
.now()
58 d
= datetime
.datetime
.fromtimestamp(float(time
))
59 self
.setTextColor(QtGui
.QColor('#999999'))
60 self
.insertPlainText(d
.strftime('%H:%M '))
61 prefix
= self
._color
.convert(prefix
)
62 text
= self
._color
.convert(text
)
65 prefix
= '\x01(F%s)%s' % (forcecolor
, prefix
)
66 text
= '\x01(F%s)%s' % (forcecolor
, text
)
68 self
._display
_with
_colors
(str(prefix
).decode('utf-8') + ' ')
70 self
._display
_with
_colors
(str(text
).decode('utf-8'))
72 self
.insertPlainText('\n')
74 self
.insertPlainText('\n')
77 def _display_with_colors(self
, string
):
78 self
.setTextColor(self
._textcolor
)
79 self
.setTextBackgroundColor(self
._bgcolor
)
80 self
._reset
_attributes
()
81 items
= string
.split('\x01')
82 for i
, item
in enumerate(items
):
83 if i
> 0 and item
.startswith('('):
90 self
._set
_attribute
(code
[0], True)
93 self
._set
_attribute
(code
[0], False)
95 # reset attributes and color
97 self
._reset
_attributes
()
98 self
._setcolorcode
[action
][0](self
._setcolorcode
[action
][1])
100 # set attributes + color
101 while code
.startswith(('*', '!', '/', '_', '|', 'r')):
103 self
._reset
_attributes
()
104 elif code
[0] in self
._setfont
:
105 self
._set
_attribute
(code
[0], not self
._font
[code
[0]])
108 self
._setcolorcode
[action
][0](QtGui
.QColor(code
))
111 self
.insertPlainText(item
)
113 def _reset_attributes(self
):
115 for attr
in self
._setfont
:
116 self
._set
_attribute
(attr
, False)
118 def _set_attribute(self
, attr
, value
):
119 self
._font
[attr
] = value
120 self
._setfont
[attr
](self
._fontvalues
[self
._font
[attr
]][attr
])
122 def scroll_bottom(self
):
123 bar
= self
.verticalScrollBar()
124 bar
.setValue(bar
.maximum())