]> jfr.im git - irc/weechat/qweechat.git/blame - qweechat/chat.py
Remove python shebangs
[irc/weechat/qweechat.git] / qweechat / chat.py
CommitLineData
7dcf23b1
SH
1# -*- coding: utf-8 -*-
2#
e836cfb0
SH
3# chat.py - chat area
4#
da74afdb 5# Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7dcf23b1
SH
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
7dcf23b1
SH
23import datetime
24import qt_compat
25QtCore = qt_compat.import_module('QtCore')
26QtGui = qt_compat.import_module('QtGui')
3a5ec0c1
SH
27import config
28import weechat.color as color
7dcf23b1
SH
29
30
31class ChatTextEdit(QtGui.QTextEdit):
32 """Chat area."""
33
3a5ec0c1 34 def __init__(self, debug, *args):
67ae3204 35 QtGui.QTextEdit.__init__(*(self,) + args)
3a5ec0c1 36 self.debug = debug
7dcf23b1
SH
37 self.readOnly = True
38 self.setFocusPolicy(QtCore.Qt.NoFocus)
39 self.setFontFamily('monospace')
3a5ec0c1
SH
40 self._textcolor = self.textColor()
41 self._bgcolor = QtGui.QColor('#FFFFFF')
77df9d06
SH
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 }
3a5ec0c1 63 self._color = color.Color(config.color_options(), self.debug)
7dcf23b1 64
3a5ec0c1 65 def display(self, time, prefix, text, forcecolor=None):
7dcf23b1
SH
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 '))
3a5ec0c1
SH
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)
7dcf23b1 78 if prefix:
3a5ec0c1
SH
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:
7dcf23b1 85 self.insertPlainText('\n')
7dcf23b1
SH
86 self.scroll_bottom()
87
3a5ec0c1
SH
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()
77df9d06
SH
109 self._setcolorcode[action][0](
110 self._setcolorcode[action][1])
3a5ec0c1
SH
111 else:
112 # set attributes + color
77df9d06
SH
113 while code.startswith(('*', '!', '/', '_', '|',
114 'r')):
3a5ec0c1
SH
115 if code[0] == 'r':
116 self._reset_attributes()
117 elif code[0] in self._setfont:
77df9d06
SH
118 self._set_attribute(
119 code[0],
120 not self._font[code[0]])
3a5ec0c1
SH
121 code = code[1:]
122 if code:
77df9d06
SH
123 self._setcolorcode[action][0](
124 QtGui.QColor(code))
3a5ec0c1
SH
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
7dcf23b1
SH
138 def scroll_bottom(self):
139 bar = self.verticalScrollBar()
140 bar.setValue(bar.maximum())