ab901fd6193e17d8aa40d21f99eed0e7bb6359b0
[quassel.git] / src / qtui / topicwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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 "topicwidget.h"
22
23 #include <QDebug>
24
25 #include "client.h"
26 #include "networkmodel.h"
27
28 TopicWidget::TopicWidget(QWidget *parent)
29   : AbstractItemView(parent)
30 {
31   ui.setupUi(this);
32   ui.topicLineEdit->hide();
33   ui.topicLineEdit->installEventFilter(this);
34   ui.topicLabel->show();
35 }
36
37 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
38   Q_UNUSED(previous);
39   setTopic(current.sibling(current.row(), 1).data().toString());
40 }
41
42 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
43   QItemSelectionRange changedArea(topLeft, bottomRight);
44   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
45   if(changedArea.contains(currentTopicIndex))
46     setTopic(currentTopicIndex.data().toString());
47 };
48
49 void TopicWidget::setTopic(const QString &newtopic) {
50   if(_topic == newtopic)
51     return;
52   
53   _topic = newtopic;
54   ui.topicLabel->setText(newtopic);
55   ui.topicLineEdit->setText(newtopic);
56   switchPlain();
57 }
58
59 void TopicWidget::on_topicLineEdit_returnPressed() {
60   QModelIndex currentIdx = currentIndex();
61   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
62     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
63     Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
64   }
65   switchPlain();
66 }
67
68 void TopicWidget::on_topicEditButton_clicked() {
69   switchEditable();
70 }
71
72 void TopicWidget::switchEditable() {
73   ui.topicLabel->hide();
74   ui.topicEditButton->hide();
75   ui.topicLineEdit->show();
76   ui.topicLineEdit->setFocus();
77 }
78
79 void TopicWidget::switchPlain() {
80   ui.topicLineEdit->hide();
81   ui.topicLabel->show();
82   ui.topicEditButton->show();
83   ui.topicLineEdit->setText(_topic);
84 }
85
86 // filter for the input widget to switch back to normal mode
87 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
88   if(event->type() == QEvent::FocusOut) {
89     switchPlain();
90     return true;
91   }
92
93   if(event->type() != QEvent::KeyPress)
94     return QObject::eventFilter(obj, event);
95
96   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
97
98   if(keyEvent->key() == Qt::Key_Escape) {
99     switchPlain();
100     return true;
101   }
102   
103   return false;
104 }
105