Hide some of the bufferviews by default in order to not confuse new users too much.
[quassel.git] / src / qtui / bufferwidget.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 "bufferwidget.h"
22 #include "buffer.h"
23 #include "chatline-old.h"
24 #include "chatwidget.h"
25 #include "settings.h"
26 #include "client.h"
27 #include "network.h"
28
29 BufferWidget::BufferWidget(QWidget *parent)
30   : QWidget(parent),
31     _currentBuffer(0),
32     _currentNetwork(0)
33 {
34   ui.setupUi(this);
35   ui.ownNick->clear();  // TODO add nick history
36   connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
37 }
38
39 void BufferWidget::init() {
40
41 }
42
43 BufferWidget::~BufferWidget() {
44 }
45
46 BufferId BufferWidget::currentBuffer() const {
47   return _currentBuffer;
48 }
49
50 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
51   ChatWidget *chatWidget;
52   if(_chatWidgets.contains(bufferId)) {
53      chatWidget = _chatWidgets[bufferId];
54   } else {
55     Buffer *buf = Client::buffer(bufferId);
56     if(!buf) {
57       qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
58       return;
59     }
60     chatWidget = new ChatWidget(this);
61     chatWidget->init(bufferId);
62     QList<ChatLine *> lines;
63     QList<AbstractUiMsg *> msgs = buf->contents();
64     foreach(AbstractUiMsg *msg, msgs) {
65       lines.append(dynamic_cast<ChatLine*>(msg));
66     }
67     chatWidget->setContents(lines);
68     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
69     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
70     _chatWidgets[bufferId] = chatWidget;
71     ui.stackedWidget->addWidget(chatWidget);
72   }
73   ui.stackedWidget->setCurrentWidget(chatWidget);
74   disconnect(this, SIGNAL(userInput(QString)), 0, 0);
75   connect(this, SIGNAL(userInput(QString)), Client::buffer(bufferId), SLOT(processUserInput(QString)));
76   chatWidget->setFocusProxy(ui.inputEdit);
77   ui.inputEdit->setFocus();
78
79 }
80
81 NetworkId BufferWidget::currentNetwork() const {
82   return _currentNetwork;
83 }
84
85 void BufferWidget::setCurrentNetwork(NetworkId networkId) {
86   Network *net = Client::network(networkId);
87   if(!net)
88     return;
89   _currentNetwork = networkId;
90   
91   ui.ownNick->clear();
92   ui.ownNick->addItem(net->myNick());
93 }
94
95 void BufferWidget::removeBuffer(BufferId bufferId) {
96   if(!_chatWidgets.contains(bufferId))
97     return;
98
99   ChatWidget *chatWidget = _chatWidgets.take(bufferId);
100   ui.stackedWidget->removeWidget(chatWidget);
101   chatWidget->deleteLater();
102 }
103
104 void BufferWidget::saveState() {
105 }
106
107 QSize BufferWidget::sizeHint() const {
108   return QSize(800,400);
109 }
110
111 void BufferWidget::enterPressed() {
112   QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
113   foreach(QString msg, lines) {
114     if(msg.isEmpty()) continue;
115     emit userInput(msg);
116   }
117   ui.inputEdit->clear();
118 }
119
120
121
122 /*
123 void BufferWidget::displayMsg(Message msg) {
124   chatWidget->appendMsg(msg);
125 }
126 */
127