fixing a typo in an include and properly making qwebkit optional
[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 "iconloader.h"
27 #include "networkmodel.h"
28
29 TopicWidget::TopicWidget(QWidget *parent)
30   : AbstractItemView(parent)
31 {
32   ui.setupUi(this);
33   ui.topicEditButton->setPixmap(BarIcon("edit-rename"));
34
35   ui.topicLineEdit->hide();
36   ui.topicLineEdit->installEventFilter(this);
37   ui.topicLabel->show();
38   setContentsMargins(0,0,0,0);
39   parent->setMinimumHeight(layout()->sizeHint().height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
40 }
41
42 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
43   Q_UNUSED(previous);
44   setTopic(current.sibling(current.row(), 1).data().toString());
45 }
46
47 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
48   QItemSelectionRange changedArea(topLeft, bottomRight);
49   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
50   if(changedArea.contains(currentTopicIndex))
51     setTopic(currentTopicIndex.data().toString());
52 };
53
54 void TopicWidget::setTopic(const QString &newtopic) {
55   if(_topic == newtopic)
56     return;
57
58   _topic = newtopic;
59   ui.topicLabel->setText(newtopic);
60   ui.topicLineEdit->setText(newtopic);
61   switchPlain();
62 }
63
64 void TopicWidget::on_topicLineEdit_returnPressed() {
65   QModelIndex currentIdx = currentIndex();
66   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
67     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
68     Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
69   }
70   switchPlain();
71 }
72
73 void TopicWidget::on_topicEditButton_clicked() {
74   switchEditable();
75 }
76
77 void TopicWidget::switchEditable() {
78   ui.topicLabel->hide();
79   ui.topicEditButton->hide();
80   ui.topicLineEdit->show();
81   ui.topicLineEdit->setFocus();
82
83   setFixedHeight(layout()->sizeHint().height());
84   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
85   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
86   qobject_cast<QWidget *>(parent())->adjustSize();
87 }
88
89 void TopicWidget::switchPlain() {
90   ui.topicLineEdit->hide();
91   ui.topicLabel->show();
92   ui.topicEditButton->show();
93   ui.topicLineEdit->setText(_topic);
94   setFixedHeight(layout()->sizeHint().height());
95   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
96   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
97   qobject_cast<QWidget *>(parent())->adjustSize();
98 }
99
100 // filter for the input widget to switch back to normal mode
101 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
102   if(event->type() == QEvent::FocusOut) {
103     switchPlain();
104     return true;
105   }
106
107   if(event->type() != QEvent::KeyPress)
108     return QObject::eventFilter(obj, event);
109
110   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
111
112   if(keyEvent->key() == Qt::Key_Escape) {
113     switchPlain();
114     return true;
115   }
116
117   return false;
118 }