Redesign the topic widget
[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
33   ui.topicLineEdit->installEventFilter(this);
34 }
35
36 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
37   Q_UNUSED(previous);
38   setTopic(current.sibling(current.row(), 1).data().toString());
39 }
40
41 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
42   QItemSelectionRange changedArea(topLeft, bottomRight);
43   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
44   if(changedArea.contains(currentTopicIndex))
45     setTopic(currentTopicIndex.data().toString());
46 };
47
48 void TopicWidget::setTopic(const QString &newtopic) {
49   if(_topic == newtopic)
50     return;
51
52   _topic = newtopic;
53   ui.topicLabel->setText(newtopic);
54   ui.topicLineEdit->setText(newtopic);
55   switchPlain();
56 }
57
58 void TopicWidget::on_topicLineEdit_textEntered() {
59   QModelIndex currentIdx = currentIndex();
60   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
61     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
62     if(ui.topicLineEdit->text().isEmpty())
63       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
64     else
65       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
66   }
67   switchPlain();
68 }
69
70 void TopicWidget::on_topicEditButton_clicked() {
71   switchEditable();
72 }
73
74 void TopicWidget::switchEditable() {
75   ui.stackedWidget->setCurrentIndex(1);
76   ui.topicLineEdit->setFocus();
77   updateGeometry();
78 }
79
80 void TopicWidget::switchPlain() {
81   ui.stackedWidget->setCurrentIndex(0);
82   ui.topicLineEdit->setText(_topic);
83   updateGeometry();
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 }