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"
28 InputWidget::InputWidget(QWidget *parent)
35 connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
36 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
37 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SLOT(userInput(BufferInfo, QString)));
38 setFocusProxy(ui.inputEdit);
41 InputWidget::~InputWidget() {
44 void InputWidget::setModel(BufferModel *bufferModel) {
45 _bufferModel = bufferModel;
48 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
50 disconnect(_selectionModel, 0, this, 0);
52 _selectionModel = selectionModel;
53 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
54 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
57 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
60 validBuffer = current.isValid();
66 variant = current.data(NetworkModel::BufferInfoRole);
67 if(!variant.isValid())
70 currentBufferInfo = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
74 const Network *InputWidget::currentNetwork() const {
78 return Client::network(currentBufferInfo.networkId());
81 void InputWidget::updateNickSelector() const {
82 const Network *net = currentNetwork();
86 const Identity *identity = Client::identity(net->identity());
88 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
93 QStringList nicks = identity->nicks();
94 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
95 nicks.prepend(net->myNick());
100 ui.ownNick->addItems(nicks);
101 ui.ownNick->setCurrentIndex(nickIdx);
104 void InputWidget::changeNick(const QString &newNick) const {
105 const Network *net = currentNetwork();
106 if(!net || net->isMyNick(newNick))
108 emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
111 void InputWidget::enterPressed() {
112 QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
113 foreach(QString msg, lines) {
114 if(msg.isEmpty()) continue;
115 emit userInput(currentBufferInfo, msg);
117 ui.inputEdit->clear();