Read/Write settings from/to correct subcategory and cleanup
[quassel.git] / dev-notes / obsolete / qtopia / chatwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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), AbstractChatView() {
26   setStyleSheet("background-color: rgba(255, 255, 255, 60%)");
27   setTextInteractionFlags(Qt::TextBrowserInteraction);
28 }
29
30 void ChatWidget::setContents(const QList<AbstractUiMsg *> &lines) {
31   clear();
32   QList<ChatLine *> list;
33   foreach(AbstractUiMsg *msg, lines) list << static_cast<ChatLine*>(msg);
34   appendChatLines(list);
35
36 }
37
38 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
39   ChatLine *line = static_cast<ChatLine*>(msg);
40   Q_ASSERT(line);
41   prependChatLine(line);
42 }
43
44 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
45   ChatLine *line = static_cast<ChatLine*>(msg);
46   Q_ASSERT(line);
47   appendChatLine(line);
48 }
49
50 void ChatWidget::appendChatLine(ChatLine *line) {
51   QTextCursor cursor = textCursor();
52   moveCursor(QTextCursor::End);
53   if(!document()->isEmpty()) insertPlainText("\n");
54   insertStyledText(line->styledSender());
55   insertPlainText(" ");
56   insertStyledText(line->styledContents());
57   setTextCursor(cursor);
58 }
59
60 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
61   foreach(ChatLine *line, list) {
62     appendChatLine(line);
63   }
64 }
65
66 void ChatWidget::prependChatLine(ChatLine *line) {
67   QTextCursor cursor = textCursor();
68   moveCursor(QTextCursor::Start);
69   bool flg = document()->isEmpty();
70   insertStyledText(line->styledSender());
71   insertPlainText(" ");
72   insertStyledText(line->styledContents());
73   if(!flg) insertPlainText("\n");
74   setTextCursor(cursor);
75 }
76
77 void ChatWidget::prependChatLines(QList<ChatLine *> list) {
78   foreach(ChatLine *line, list) {
79     prependChatLine(line);
80   }
81 }
82
83 void ChatWidget::insertChatLine(ChatLine *line) {
84   if(!document()->isEmpty()) insertPlainText("\n");
85   insertStyledText(line->styledSender());
86   insertPlainText(" ");
87   insertStyledText(line->styledContents());
88 }
89
90 void ChatWidget::insertStyledText(const QtopiaUiStyle::StyledText &stext) {
91   QTextCursor cursor = textCursor();
92   foreach(QTextLayout::FormatRange format, stext.formatList) {
93     cursor.setCharFormat(format.format);
94     setTextCursor(cursor);
95     insertPlainText(stext.plainText.mid(format.start, format.length));
96   }
97 }