]> jfr.im git - irc/weechat/qweechat.git/blob - qweechat/chat.py
Remove python shebangs
[irc/weechat/qweechat.git] / qweechat / chat.py
1 # -*- coding: utf-8 -*-
2 #
3 # chat.py - chat area
4 #
5 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
6 #
7 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
8 #
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.
13 #
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.
18 #
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/>.
21 #
22
23 import datetime
24 import qt_compat
25 QtCore = qt_compat.import_module('QtCore')
26 QtGui = qt_compat.import_module('QtGui')
27 import config
28 import weechat.color as color
29
30
31 class ChatTextEdit(QtGui.QTextEdit):
32 """Chat area."""
33
34 def __init__(self, debug, *args):
35 QtGui.QTextEdit.__init__(*(self,) + args)
36 self.debug = debug
37 self.readOnly = True
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)
45 }
46 self._setfont = {
47 '*': self.setFontWeight,
48 '_': self.setFontUnderline,
49 '/': self.setFontItalic
50 }
51 self._fontvalues = {
52 False: {
53 '*': QtGui.QFont.Normal,
54 '_': False,
55 '/': False
56 },
57 True: {
58 '*': QtGui.QFont.Bold,
59 '_': True,
60 '/': True
61 }
62 }
63 self._color = color.Color(config.color_options(), self.debug)
64
65 def display(self, time, prefix, text, forcecolor=None):
66 if time == 0:
67 d = datetime.datetime.now()
68 else:
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)
74 if forcecolor:
75 if prefix:
76 prefix = '\x01(F%s)%s' % (forcecolor, prefix)
77 text = '\x01(F%s)%s' % (forcecolor, text)
78 if prefix:
79 self._display_with_colors(str(prefix).decode('utf-8') + ' ')
80 if text:
81 self._display_with_colors(str(text).decode('utf-8'))
82 if text[-1:] != '\n':
83 self.insertPlainText('\n')
84 else:
85 self.insertPlainText('\n')
86 self.scroll_bottom()
87
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('('):
95 pos = item.find(')')
96 if pos >= 2:
97 action = item[1]
98 code = item[2:pos]
99 if action == '+':
100 # set attribute
101 self._set_attribute(code[0], True)
102 elif action == '-':
103 # remove attribute
104 self._set_attribute(code[0], False)
105 else:
106 # reset attributes and color
107 if code == 'r':
108 self._reset_attributes()
109 self._setcolorcode[action][0](
110 self._setcolorcode[action][1])
111 else:
112 # set attributes + color
113 while code.startswith(('*', '!', '/', '_', '|',
114 'r')):
115 if code[0] == 'r':
116 self._reset_attributes()
117 elif code[0] in self._setfont:
118 self._set_attribute(
119 code[0],
120 not self._font[code[0]])
121 code = code[1:]
122 if code:
123 self._setcolorcode[action][0](
124 QtGui.QColor(code))
125 item = item[pos+1:]
126 if len(item) > 0:
127 self.insertPlainText(item)
128
129 def _reset_attributes(self):
130 self._font = {}
131 for attr in self._setfont:
132 self._set_attribute(attr, False)
133
134 def _set_attribute(self, attr, value):
135 self._font[attr] = value
136 self._setfont[attr](self._fontvalues[self._font[attr]][attr])
137
138 def scroll_bottom(self):
139 bar = self.verticalScrollBar()
140 bar.setValue(bar.maximum())