some minor smoothifications to the nick selector
[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     if(ui.topicLineEdit->text().isEmpty())
69       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
70     else
71       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
72   }
73   switchPlain();
74 }
75
76 void TopicWidget::on_topicEditButton_clicked() {
77   switchEditable();
78 }
79
80 void TopicWidget::switchEditable() {
81   ui.topicLabel->hide();
82   ui.topicEditButton->hide();
83   ui.topicLineEdit->show();
84   ui.topicLineEdit->setFocus();
85
86   setFixedHeight(layout()->sizeHint().height());
87   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
88   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
89   qobject_cast<QWidget *>(parent())->adjustSize();
90 }
91
92 void TopicWidget::switchPlain() {
93   ui.topicLineEdit->hide();
94   ui.topicLabel->show();
95   ui.topicEditButton->show();
96   ui.topicLineEdit->setText(_topic);
97   setFixedHeight(layout()->sizeHint().height());
98   // Update the dock widget too, else it won't resize in all styles... FIXME try to sanitize this
99   qobject_cast<QWidget *>(parent())->setMinimumHeight(height() + 2*qApp->style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin));
100   qobject_cast<QWidget *>(parent())->adjustSize();
101 }
102
103 // filter for the input widget to switch back to normal mode
104 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
105   if(event->type() == QEvent::FocusOut) {
106     switchPlain();
107     return true;
108   }
109
110   if(event->type() != QEvent::KeyPress)
111     return QObject::eventFilter(obj, event);
112
113   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
114
115   if(keyEvent->key() == Qt::Key_Escape) {
116     switchPlain();
117     return true;
118   }
119
120   return false;
121 }