]>
Commit | Line | Data |
---|---|---|
7dcf23b1 SH |
1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
e17d5dc0 | 4 | # Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org> |
7dcf23b1 SH |
5 | # |
6 | # This file is part of QWeeChat, a Qt remote GUI for WeeChat. | |
7 | # | |
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. | |
12 | # | |
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. | |
17 | # | |
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/>. | |
20 | # | |
21 | ||
22 | # | |
23 | # Chat area. | |
24 | # | |
25 | ||
26 | import datetime | |
27 | import qt_compat | |
28 | QtCore = qt_compat.import_module('QtCore') | |
29 | QtGui = qt_compat.import_module('QtGui') | |
3a5ec0c1 SH |
30 | import config |
31 | import weechat.color as color | |
7dcf23b1 SH |
32 | |
33 | ||
34 | class ChatTextEdit(QtGui.QTextEdit): | |
35 | """Chat area.""" | |
36 | ||
3a5ec0c1 | 37 | def __init__(self, debug, *args): |
67ae3204 | 38 | QtGui.QTextEdit.__init__(*(self,) + args) |
3a5ec0c1 | 39 | self.debug = debug |
7dcf23b1 SH |
40 | self.readOnly = True |
41 | self.setFocusPolicy(QtCore.Qt.NoFocus) | |
42 | self.setFontFamily('monospace') | |
3a5ec0c1 SH |
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) | |
7dcf23b1 | 53 | |
3a5ec0c1 | 54 | def display(self, time, prefix, text, forcecolor=None): |
7dcf23b1 SH |
55 | if time == 0: |
56 | d = datetime.datetime.now() | |
57 | else: | |
58 | d = datetime.datetime.fromtimestamp(float(time)) | |
59 | self.setTextColor(QtGui.QColor('#999999')) | |
60 | self.insertPlainText(d.strftime('%H:%M ')) | |
3a5ec0c1 SH |
61 | prefix = self._color.convert(prefix) |
62 | text = self._color.convert(text) | |
63 | if forcecolor: | |
64 | if prefix: | |
65 | prefix = '\x01(F%s)%s' % (forcecolor, prefix) | |
66 | text = '\x01(F%s)%s' % (forcecolor, text) | |
7dcf23b1 | 67 | if prefix: |
3a5ec0c1 SH |
68 | self._display_with_colors(str(prefix).decode('utf-8') + ' ') |
69 | if text: | |
70 | self._display_with_colors(str(text).decode('utf-8')) | |
71 | if text[-1:] != '\n': | |
72 | self.insertPlainText('\n') | |
73 | else: | |
7dcf23b1 | 74 | self.insertPlainText('\n') |
7dcf23b1 SH |
75 | self.scroll_bottom() |
76 | ||
3a5ec0c1 SH |
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('('): | |
84 | pos = item.find(')') | |
85 | if pos >= 2: | |
86 | action = item[1] | |
87 | code = item[2:pos] | |
88 | if action == '+': | |
89 | # set attribute | |
90 | self._set_attribute(code[0], True) | |
91 | elif action == '-': | |
92 | # remove attribute | |
93 | self._set_attribute(code[0], False) | |
94 | else: | |
95 | # reset attributes and color | |
96 | if code == 'r': | |
97 | self._reset_attributes() | |
98 | self._setcolorcode[action][0](self._setcolorcode[action][1]) | |
99 | else: | |
100 | # set attributes + color | |
101 | while code.startswith(('*', '!', '/', '_', '|', 'r')): | |
102 | if code[0] == 'r': | |
103 | self._reset_attributes() | |
104 | elif code[0] in self._setfont: | |
105 | self._set_attribute(code[0], not self._font[code[0]]) | |
106 | code = code[1:] | |
107 | if code: | |
108 | self._setcolorcode[action][0](QtGui.QColor(code)) | |
109 | item = item[pos+1:] | |
110 | if len(item) > 0: | |
111 | self.insertPlainText(item) | |
112 | ||
113 | def _reset_attributes(self): | |
114 | self._font = {} | |
115 | for attr in self._setfont: | |
116 | self._set_attribute(attr, False) | |
117 | ||
118 | def _set_attribute(self, attr, value): | |
119 | self._font[attr] = value | |
120 | self._setfont[attr](self._fontvalues[self._font[attr]][attr]) | |
121 | ||
7dcf23b1 SH |
122 | def scroll_bottom(self): |
123 | bar = self.verticalScrollBar() | |
124 | bar.setValue(bar.maximum()) |