]> jfr.im git - irc/weechat/qweechat.git/blob - src/qweechat/chat.py
Update copyright dates
[irc/weechat/qweechat.git] / src / qweechat / chat.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # chat.py - chat area
5 #
6 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7 #
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9 #
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.
14 #
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.
19 #
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/>.
22 #
23
24 import datetime
25 import qt_compat
26 QtCore = qt_compat.import_module('QtCore')
27 QtGui = qt_compat.import_module('QtGui')
28 import config
29 import weechat.color as color
30
31
32 class ChatTextEdit(QtGui.QTextEdit):
33 """Chat area."""
34
35 def __init__(self, debug, *args):
36 QtGui.QTextEdit.__init__(*(self,) + args)
37 self.debug = debug
38 self.readOnly = True
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)
51
52 def display(self, time, prefix, text, forcecolor=None):
53 if time == 0:
54 d = datetime.datetime.now()
55 else:
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)
61 if forcecolor:
62 if prefix:
63 prefix = '\x01(F%s)%s' % (forcecolor, prefix)
64 text = '\x01(F%s)%s' % (forcecolor, text)
65 if prefix:
66 self._display_with_colors(str(prefix).decode('utf-8') + ' ')
67 if text:
68 self._display_with_colors(str(text).decode('utf-8'))
69 if text[-1:] != '\n':
70 self.insertPlainText('\n')
71 else:
72 self.insertPlainText('\n')
73 self.scroll_bottom()
74
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('('):
82 pos = item.find(')')
83 if pos >= 2:
84 action = item[1]
85 code = item[2:pos]
86 if action == '+':
87 # set attribute
88 self._set_attribute(code[0], True)
89 elif action == '-':
90 # remove attribute
91 self._set_attribute(code[0], False)
92 else:
93 # reset attributes and color
94 if code == 'r':
95 self._reset_attributes()
96 self._setcolorcode[action][0](self._setcolorcode[action][1])
97 else:
98 # set attributes + color
99 while code.startswith(('*', '!', '/', '_', '|', 'r')):
100 if code[0] == 'r':
101 self._reset_attributes()
102 elif code[0] in self._setfont:
103 self._set_attribute(code[0], not self._font[code[0]])
104 code = code[1:]
105 if code:
106 self._setcolorcode[action][0](QtGui.QColor(code))
107 item = item[pos+1:]
108 if len(item) > 0:
109 self.insertPlainText(item)
110
111 def _reset_attributes(self):
112 self._font = {}
113 for attr in self._setfont:
114 self._set_attribute(attr, False)
115
116 def _set_attribute(self, attr, value):
117 self._font[attr] = value
118 self._setfont[attr](self._fontvalues[self._font[attr]][attr])
119
120 def scroll_bottom(self):
121 bar = self.verticalScrollBar()
122 bar.setValue(bar.maximum())