Make URLs (and channel names!) in the topic widget clickable again
[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 "client.h"
24 #include "iconloader.h"
25 #include "networkmodel.h"
26
27 TopicWidget::TopicWidget(QWidget *parent)
28   : AbstractItemView(parent)
29 {
30   ui.setupUi(this);
31   ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
32   ui.topicLineEdit->setWordWrapEnabled(true);
33   ui.topicLineEdit->installEventFilter(this);
34
35   connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
36 }
37
38 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
39   Q_UNUSED(previous);
40   setTopic(current.sibling(current.row(), 1).data().toString());
41 }
42
43 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
44   QItemSelectionRange changedArea(topLeft, bottomRight);
45   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
46   if(changedArea.contains(currentTopicIndex))
47     setTopic(currentTopicIndex.data().toString());
48 };
49
50 void TopicWidget::setTopic(const QString &newtopic) {
51   if(_topic == newtopic)
52     return;
53
54   _topic = newtopic;
55   ui.topicLabel->setText(newtopic);
56   ui.topicLineEdit->setText(newtopic);
57   switchPlain();
58 }
59
60 void TopicWidget::clickableActivated(const Clickable &click) {
61   NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
62   click.activate(networkId, _topic);
63 }
64
65 void TopicWidget::on_topicLineEdit_textEntered() {
66   QModelIndex currentIdx = currentIndex();
67   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
68     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
69     if(ui.topicLineEdit->text().isEmpty())
70       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
71     else
72       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
73   }
74   switchPlain();
75 }
76
77 void TopicWidget::on_topicEditButton_clicked() {
78   switchEditable();
79 }
80
81 void TopicWidget::switchEditable() {
82   ui.stackedWidget->setCurrentIndex(1);
83   ui.topicLineEdit->setFocus();
84   updateGeometry();
85 }
86
87 void TopicWidget::switchPlain() {
88   ui.stackedWidget->setCurrentIndex(0);
89   ui.topicLineEdit->setText(_topic);
90   updateGeometry();
91 }
92
93 // filter for the input widget to switch back to normal mode
94 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
95   if(event->type() == QEvent::FocusOut) {
96     switchPlain();
97     return true;
98   }
99
100   if(event->type() != QEvent::KeyPress)
101     return QObject::eventFilter(obj, event);
102
103   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
104
105   if(keyEvent->key() == Qt::Key_Escape) {
106     switchPlain();
107     return true;
108   }
109
110   return false;
111 }