Newly created buffers are now selected automatically.
[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 "identity.h"
28 #include "network.h"
29 #include "networkmodel.h"
30
31 BufferWidget::BufferWidget(QWidget *parent)
32   : QWidget(parent),
33     _bufferModel(0),
34     _selectionModel(0),
35     _currentBuffer(0)
36 {
37   ui.setupUi(this);
38 }
39
40 BufferWidget::~BufferWidget() {
41 }
42
43 void BufferWidget::init() {
44 }
45
46 void BufferWidget::setModel(BufferModel *bufferModel) {
47   if(_bufferModel) {
48     disconnect(_bufferModel, 0, this, 0);
49   }
50   _bufferModel = bufferModel;
51   connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
52           this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
53 }
54
55 void BufferWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
56   if(_selectionModel) {
57     disconnect(_selectionModel, 0, this, 0);
58   }
59   _selectionModel = selectionModel;
60   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
61           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
62 }
63
64 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
65   Q_ASSERT(model());
66   if(!parent.isValid()) {
67     // ok this means that whole networks are about to be removed
68     // we can't determine which buffers are affect, so we hope that all nets are removed
69     // this is the most common case (for example disconnecting from the core or terminating the clint)
70     if(model()->rowCount(parent) != end - start + 1)
71       return;
72
73     ChatWidget *chatWidget;
74     QHash<BufferId, ChatWidget *>::iterator iter = _chatWidgets.begin();
75     while(iter != _chatWidgets.end()) {
76       chatWidget = *iter;
77       iter = _chatWidgets.erase(iter);
78       ui.stackedWidget->removeWidget(chatWidget);
79       chatWidget->deleteLater();
80     }
81     
82   } else {
83     // check if there are explicitly buffers removed
84     for(int i = start; i <= end; i++) {
85       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
86       if(!variant.isValid())
87         continue;
88       
89       BufferId bufferId = qVariantValue<BufferId>(variant);
90       removeBuffer(bufferId);
91     }
92   }
93 }
94
95 void BufferWidget::removeBuffer(BufferId bufferId) {
96   if(!_chatWidgets.contains(bufferId))
97     return;
98
99   if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
100   ChatWidget *chatWidget = _chatWidgets.take(bufferId);
101   ui.stackedWidget->removeWidget(chatWidget);
102   chatWidget->deleteLater();
103 }
104
105 void BufferWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
106   Q_UNUSED(previous);
107   QVariant variant;
108
109   variant = current.data(NetworkModel::BufferIdRole);
110   if(!variant.isValid())
111     return;
112   setCurrentBuffer(variant.value<BufferId>());
113 }
114
115 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
116   ChatWidget *chatWidget = 0;
117   Buffer *buf = Client::buffer(bufferId);
118   if(!buf) {
119     qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
120     return;
121   }
122   Buffer *prevBuffer = Client::buffer(currentBuffer());
123   if(prevBuffer) prevBuffer->setVisible(false);
124   if(_chatWidgets.contains(bufferId)) {
125      chatWidget = _chatWidgets[bufferId];
126   } else {
127     chatWidget = new ChatWidget(this);
128     chatWidget->init(bufferId);
129     QList<ChatLine *> lines;
130     QList<AbstractUiMsg *> msgs = buf->contents();
131     foreach(AbstractUiMsg *msg, msgs) {
132       lines.append(dynamic_cast<ChatLine*>(msg));
133     }
134     chatWidget->setContents(lines);
135     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
136     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
137     _chatWidgets[bufferId] = chatWidget;
138     ui.stackedWidget->addWidget(chatWidget);
139     chatWidget->setFocusProxy(this);
140   }
141   _currentBuffer = bufferId;
142   ui.stackedWidget->setCurrentWidget(chatWidget);
143   buf->setVisible(true);
144   setFocus();
145 }
146