1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "inputwidget.h"
24 #include "networkmodel.h"
25 #include "jumpkeyhandler.h"
27 InputWidget::InputWidget(QWidget *parent)
34 connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
35 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
36 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
37 setFocusProxy(ui.inputEdit);
39 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
42 InputWidget::~InputWidget() {
45 void InputWidget::setModel(BufferModel *bufferModel) {
47 disconnect(_bufferModel, 0, this, 0);
49 _bufferModel = bufferModel;
50 connect(bufferModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
51 this, SLOT(dataChanged(QModelIndex, QModelIndex)));
54 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
56 disconnect(_selectionModel, 0, this, 0);
58 _selectionModel = selectionModel;
59 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
60 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
63 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
66 validBuffer = current.isValid();
72 variant = current.data(NetworkModel::BufferInfoRole);
73 if(!variant.isValid())
76 currentBufferInfo = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
77 setNetwork(Client::networkModel()->networkByIndex(current));
79 ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
82 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
83 QItemSelectionRange changedArea(topLeft, bottomRight);
84 QModelIndex currentIndex = Client::bufferModel()->currentIndex();
85 if(changedArea.contains(currentIndex)) {
86 ui.inputEdit->setEnabled(currentIndex.data(NetworkModel::ItemActiveRole).value<bool>());
91 const Network *InputWidget::currentNetwork() const {
95 return Client::network(_networkId);
98 void InputWidget::setNetwork(const Network *network) {
99 if(_networkId == network->networkId())
102 const Network *previousNet = Client::network(_networkId);
104 disconnect(previousNet, 0, this, 0);
107 _networkId = network->networkId();
108 connect(network, SIGNAL(myNickSet(QString)),
109 this, SLOT(updateNickSelector()));
110 connect(network, SIGNAL(identitySet(IdentityId)),
111 this, SLOT(setIdentity(IdentityId)));
113 setIdentity(network->identity());
116 void InputWidget::setIdentity(const IdentityId &identityId) {
117 if(_identityId == identityId)
120 const Identity *previousIdentity = Client::identity(_identityId);
122 disconnect(previousIdentity, 0, this, 0);
124 const Identity *identity = Client::identity(identityId);
126 _identityId = identityId;
127 connect(identity, SIGNAL(nicksSet(QStringList)),
128 this, SLOT(updateNickSelector()));
130 updateNickSelector();
133 void InputWidget::updateNickSelector() const {
136 const Network *net = currentNetwork();
140 const Identity *identity = Client::identity(net->identity());
142 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
147 QStringList nicks = identity->nicks();
148 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
149 nicks.prepend(net->myNick());
153 ui.ownNick->addItems(nicks);
154 ui.ownNick->setCurrentIndex(nickIdx);
157 void InputWidget::changeNick(const QString &newNick) const {
158 const Network *net = currentNetwork();
159 if(!net || net->isMyNick(newNick))
161 emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
164 void InputWidget::sendText(QString text) {
165 emit userInput(currentBufferInfo, text);