as a result of semi boredom and since it bugged me for quite a while:
[quassel.git] / src / qtopia / chatwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QDebug>
22 #include <QtGui>
23 #include "chatwidget.h"
24
25 ChatWidget::ChatWidget(QWidget *parent) : QTextEdit(parent) {
26   setStyleSheet("background-color: rgba(255, 255, 255, 60%)");
27   setTextInteractionFlags(Qt::TextBrowserInteraction);
28 }
29
30 void ChatWidget::setContents(QList<ChatLine *> lines) {
31   clear();
32   appendChatLines(lines);
33
34 }
35
36 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
37   ChatLine *line = static_cast<ChatLine*>(msg);
38   Q_ASSERT(line);
39   prependChatLine(line);
40 }
41
42 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
43   ChatLine *line = static_cast<ChatLine*>(msg);
44   Q_ASSERT(line);
45   appendChatLine(line);
46 }
47
48 void ChatWidget::appendChatLine(ChatLine *line) {
49   QTextCursor cursor = textCursor();
50   moveCursor(QTextCursor::End);
51   if(!document()->isEmpty()) insertPlainText("\n");
52   insertStyledText(line->styledSender());
53   insertPlainText(" ");
54   insertStyledText(line->styledText());
55   setTextCursor(cursor);
56 }
57
58 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
59   foreach(ChatLine *line, list) {
60     appendChatLine(line);
61   }
62 }
63
64 void ChatWidget::prependChatLine(ChatLine *line) {
65   QTextCursor cursor = textCursor();
66   moveCursor(QTextCursor::Start);
67   bool flg = document()->isEmpty();
68   insertStyledText(line->styledSender());
69   insertPlainText(" ");
70   insertStyledText(line->styledText());
71   if(!flg) insertPlainText("\n");
72   setTextCursor(cursor);
73 }
74
75 void ChatWidget::prependChatLines(QList<ChatLine *> list) {
76   foreach(ChatLine *line, list) {
77     prependChatLine(line);
78   }
79 }
80
81 void ChatWidget::insertChatLine(ChatLine *line) {
82   if(!document()->isEmpty()) insertPlainText("\n");
83   insertStyledText(line->styledSender());
84   insertPlainText(" ");
85   insertStyledText(line->styledText());
86 }
87
88 void ChatWidget::insertStyledText(const QtopiaUiStyle::StyledText &stext) {
89   QTextCursor cursor = textCursor();
90   foreach(QTextLayout::FormatRange format, stext.formats) {
91     cursor.setCharFormat(format.format);
92     setTextCursor(cursor);
93     insertPlainText(stext.text.mid(format.start, format.length));
94   }
95 }