#BR127: user can now set the style of the application in the config menu (behaviour...
[quassel.git] / src / qtui / inputwidget.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 "inputwidget.h"
22
23 #include "ircuser.h"
24 #include "client.h"
25 #include "networkmodel.h"
26 #include "jumpkeyhandler.h"
27 #include "qtuisettings.h"
28
29
30 InputWidget::InputWidget(QWidget *parent)
31   : AbstractItemView(parent),
32     _networkId(0)
33 {
34   ui.setupUi(this);
35   connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
36   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
37   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
38   setFocusProxy(ui.inputEdit);
39
40   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
41   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
42   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
43
44   QtUiSettings s;
45   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
46   if(useInputLineFont) {
47     ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
48   }
49 }
50
51 InputWidget::~InputWidget() {
52 }
53
54 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
55   if(current.data(NetworkModel::BufferInfoRole) == previous.data(NetworkModel::BufferInfoRole))
56     return;
57
58   setNetwork(Client::networkModel()->networkByIndex(current));
59   updateNickSelector();
60   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
61 }
62
63 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
64   QItemSelectionRange changedArea(topLeft, bottomRight);
65   QModelIndex currentIndex = selectionModel()->currentIndex();
66   if(changedArea.contains(currentIndex)) {
67     ui.inputEdit->setEnabled(currentIndex.data(NetworkModel::ItemActiveRole).value<bool>());
68   }
69 };
70
71
72 const Network *InputWidget::currentNetwork() const {
73   return Client::network(_networkId);
74 }
75
76 BufferInfo InputWidget::currentBufferInfo() const {
77   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
78 };
79
80 void InputWidget::setNetwork(const Network *network) {
81   if(!network || _networkId == network->networkId())
82     return;
83
84   const Network *previousNet = Client::network(_networkId);
85   if(previousNet) {
86     disconnect(previousNet, 0, this, 0);
87     if(previousNet->me())
88       disconnect(previousNet->me(), 0, this, 0);
89   }
90
91   if(network) {
92     _networkId = network->networkId();
93     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
94     if(network->me()) {
95       connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
96       connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
97       connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
98       connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
99     }
100   }
101   setIdentity(network->identity());
102 }
103
104 void InputWidget::setIdentity(const IdentityId &identityId) {
105   if(_identityId == identityId)
106     return;
107
108   const Identity *previousIdentity = Client::identity(_identityId);
109   if(previousIdentity)
110     disconnect(previousIdentity, 0, this, 0);
111
112   const Identity *identity = Client::identity(identityId);
113   if(identity) {
114     _identityId = identityId;
115     connect(identity, SIGNAL(nicksSet(QStringList)),
116             this, SLOT(updateNickSelector()));
117   }
118   updateNickSelector();
119 }
120
121 void InputWidget::updateNickSelector() const {
122   ui.ownNick->clear();
123
124   const Network *net = currentNetwork();
125   if(!net)
126     return;
127
128   const Identity *identity = Client::identity(net->identity());
129   if(!identity) {
130     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
131     return;
132   }
133
134   int nickIdx;
135   QStringList nicks = identity->nicks();
136   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
137     nicks.prepend(net->myNick());
138     nickIdx = 0;
139   }
140
141   if(net->me() && nickIdx < nicks.count())
142     nicks[nickIdx] = net->myNick() + QString(" (%1)").arg(net->me()->userModes());
143       
144   ui.ownNick->addItems(nicks);
145   ui.ownNick->setCurrentIndex(nickIdx);
146 }
147
148 void InputWidget::changeNick(const QString &newNick) const {
149   const Network *net = currentNetwork();
150   if(!net || net->isMyNick(newNick))
151     return;
152   emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
153 }
154
155 void InputWidget::sendText(QString text) {
156   emit userInput(currentBufferInfo(), text);
157 }
158
159
160 // MOUSE WHEEL FILTER
161 MouseWheelFilter::MouseWheelFilter(QObject *parent)
162   : QObject(parent)
163 {
164 }
165
166 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
167   if(event->type() != QEvent::Wheel)
168     return QObject::eventFilter(obj, event);
169   else
170     return true;
171 }