cleanups (WiP)
[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   setContentsMargins(0,0,0,0);
36   parent->setMinimumHeight(layout()->sizeHint().height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
37 }
38
39 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
40   Q_UNUSED(previous);
41   setTopic(current.sibling(current.row(), 1).data().toString());
42 }
43
44 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
45   QItemSelectionRange changedArea(topLeft, bottomRight);
46   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
47   if(changedArea.contains(currentTopicIndex))
48     setTopic(currentTopicIndex.data().toString());
49 };
50
51 void TopicWidget::setTopic(const QString &newtopic) {
52   if(_topic == newtopic)
53     return;
54   
55   _topic = newtopic;
56   ui.topicLabel->setText(newtopic);
57   ui.topicLineEdit->setText(newtopic);
58   switchPlain();
59 }
60
61 void TopicWidget::on_topicLineEdit_returnPressed() {
62   QModelIndex currentIdx = currentIndex();
63   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
64     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
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.topicLabel->hide();
76   ui.topicEditButton->hide();
77   ui.topicLineEdit->show();
78   ui.topicLineEdit->setFocus();
79
80   setFixedHeight(layout()->sizeHint().height());
81   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
82   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
83   qobject_cast<QWidget *>(parent())->adjustSize();
84 }
85
86 void TopicWidget::switchPlain() {
87   ui.topicLineEdit->hide();
88   ui.topicLabel->show();
89   ui.topicEditButton->show();
90   ui.topicLineEdit->setText(_topic);
91   setFixedHeight(layout()->sizeHint().height());
92   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
93   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
94   qobject_cast<QWidget *>(parent())->adjustSize();
95 }
96
97 // filter for the input widget to switch back to normal mode
98 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
99   if(event->type() == QEvent::FocusOut) {
100     switchPlain();
101     return true;
102   }
103
104   if(event->type() != QEvent::KeyPress)
105     return QObject::eventFilter(obj, event);
106
107   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
108
109   if(keyEvent->key() == Qt::Key_Escape) {
110     switchPlain();
111     return true;
112   }
113   
114   return false;
115 }