]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # Copyright (C) 2011 Sebastien Helleu <flashcode@flashtux.org> | |
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 | # Buffers. | |
24 | # | |
25 | ||
26 | import qt_compat | |
27 | QtCore = qt_compat.import_module('QtCore') | |
28 | QtGui = qt_compat.import_module('QtGui') | |
29 | from chat import ChatTextEdit | |
30 | from input import InputLineEdit | |
31 | ||
32 | ||
33 | class BufferListWidget(QtGui.QListWidget): | |
34 | """Widget with list of buffers.""" | |
35 | ||
36 | def __init__(self, *args): | |
37 | apply(QtGui.QListWidget.__init__, (self,) + args) | |
38 | self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) | |
39 | self.setMinimumWidth(120) | |
40 | self.setMaximumWidth(200) | |
41 | # TODO: do a dynamic size for widget | |
42 | #self.setMinimumWidth(self.sizeHintForColumn(0)) | |
43 | #self.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.MinimumExpanding) | |
44 | #self.setResizeMode(QtGui.QListView.Adjust) | |
45 | self.setFocusPolicy(QtCore.Qt.NoFocus) | |
46 | pal = self.palette() | |
47 | pal.setColor(QtGui.QPalette.Highlight, QtGui.QColor('#ddddff')) | |
48 | pal.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor('black')) | |
49 | self.setPalette(pal) | |
50 | ||
51 | # TODO: do a dynamic size for widget | |
52 | def sizeHint(self): | |
53 | s = QtCore.QSize() | |
54 | s.setHeight(super(BufferListWidget,self).sizeHint().height()) | |
55 | s.setWidth(self.sizeHintForColumn(0)) | |
56 | return s | |
57 | ||
58 | def switch_prev_buffer(self): | |
59 | if self.currentRow() > 0: | |
60 | self.setCurrentRow(self.currentRow() - 1) | |
61 | else: | |
62 | self.setCurrentRow(self.count() - 1) | |
63 | ||
64 | def switch_next_buffer(self): | |
65 | if self.currentRow() < self.count() - 1: | |
66 | self.setCurrentRow(self.currentRow() + 1) | |
67 | else: | |
68 | self.setCurrentRow(0) | |
69 | ||
70 | ||
71 | class BufferWidget(QtGui.QWidget): | |
72 | """Widget with (from top to bottom): title, chat + nicklist (optional) + prompt/input.""" | |
73 | ||
74 | def __init__(self, display_nicklist=False): | |
75 | QtGui.QWidget.__init__(self) | |
76 | ||
77 | # title | |
78 | self.title = QtGui.QLineEdit() | |
79 | ||
80 | # splitter with chat + nicklist | |
81 | self.chat_nicklist = QtGui.QSplitter() | |
82 | self.chat_nicklist.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) | |
83 | self.chat = ChatTextEdit() | |
84 | self.chat_nicklist.addWidget(self.chat) | |
85 | self.nicklist = QtGui.QListWidget() | |
86 | self.nicklist.setMaximumWidth(100) | |
87 | self.nicklist.setFocusPolicy(QtCore.Qt.NoFocus) | |
88 | if not display_nicklist: | |
89 | self.nicklist.setVisible(False) | |
90 | self.chat_nicklist.addWidget(self.nicklist) | |
91 | ||
92 | # prompt + input | |
93 | hbox_edit = QtGui.QHBoxLayout() | |
94 | hbox_edit.setContentsMargins(0, 0, 0, 0) | |
95 | hbox_edit.setSpacing(0) | |
96 | self.prompt = QtGui.QLabel('FlashCode') | |
97 | self.prompt.setContentsMargins(0, 0, 5, 0) | |
98 | hbox_edit.addWidget(self.prompt) | |
99 | self.input = InputLineEdit(self.chat) | |
100 | hbox_edit.addWidget(self.input) | |
101 | prompt_input = QtGui.QWidget() | |
102 | prompt_input.setLayout(hbox_edit) | |
103 | prompt_input.setContentsMargins(0, 0, 0, 0) | |
104 | ||
105 | # vbox with title + chat/nicklist + prompt/input | |
106 | vbox = QtGui.QVBoxLayout() | |
107 | vbox.setContentsMargins(0, 0, 0, 0) | |
108 | vbox.setSpacing(0) | |
109 | vbox.addWidget(self.title) | |
110 | vbox.addWidget(self.chat_nicklist) | |
111 | vbox.addWidget(prompt_input) | |
112 | ||
113 | self.setLayout(vbox) | |
114 | ||
115 | def set_title(self, title): | |
116 | """Set buffer title.""" | |
117 | self.title.clear() | |
118 | if not title is None: | |
119 | self.title.setText(title) | |
120 | ||
121 | ||
122 | class Buffer(QtCore.QObject): | |
123 | """A WeeChat buffer.""" | |
124 | ||
125 | bufferInput = qt_compat.Signal(str, str) | |
126 | ||
127 | def __init__(self, data={}): | |
128 | QtCore.QObject.__init__(self) | |
129 | self.data = data | |
130 | self.widget = BufferWidget(display_nicklist=self.data.get('nicklist', 0)) | |
131 | if self.data and self.data['title']: | |
132 | self.widget.set_title(self.data['title']) | |
133 | self.widget.input.textSent.connect(self.input_text_sent) | |
134 | ||
135 | def pointer(self): | |
136 | """Return pointer on buffer.""" | |
137 | return self.data.get('__path', [''])[0] | |
138 | ||
139 | def input_text_sent(self, text): | |
140 | """Called when text has to be sent to buffer.""" | |
141 | if self.data: | |
142 | self.bufferInput.emit(self.data['full_name'], text) | |
143 | ||
144 | def add_nick(self, prefix, nick): | |
145 | """Add a nick to nicklist.""" | |
146 | self.widget.nicklist.addItem('%s%s' % (prefix, nick)) |