Fixed a bug in the new topic widget where no repaint event was triggered if only...
[quassel.git] / src / qtui / topicwidget.cpp
index 0d9cb25..511d275 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005/06 by The Quassel Team                             *
+ *   Copyright (C) 2005/06 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
@@ -26,15 +26,52 @@ TopicWidget::TopicWidget(QWidget *parent)
   : QWidget(parent)
 {
   ui.setupUi(this);
-}
-
-QString TopicWidget::topic() const {
-  return ui.topicLineEdit->text();
+  ui.topicLineEdit->hide();
+  ui.topicLineEdit->installEventFilter(this);
+  ui.topicButton->show();
 }
 
 void TopicWidget::setTopic(const QString &newtopic) {
+  if(_topic == newtopic)
+    return;
+  
+  _topic = newtopic;
+  ui.topicButton->setAndStyleText(newtopic);
   ui.topicLineEdit->setText(newtopic);
+  switchPlain();
+}
+
+void TopicWidget::on_topicLineEdit_returnPressed() {
+  switchPlain();
+  emit topicChanged(topic());
+}
+
+void TopicWidget::on_topicButton_clicked() {
+  switchEditable();
 }
 
-TopicWidget::~TopicWidget() {
+void TopicWidget::switchEditable() {
+  ui.topicButton->hide();
+  ui.topicLineEdit->show();
+  ui.topicLineEdit->setFocus();
+}
+
+void TopicWidget::switchPlain() {
+  ui.topicLineEdit->hide();
+  ui.topicButton->show();
+}
+
+bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
+  if(event->type() != QEvent::KeyPress)
+    return QObject::eventFilter(obj, event);
+
+  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
+
+  if(keyEvent->key() == Qt::Key_Escape) {
+    switchPlain();
+    ui.topicLineEdit->setText(_topic);
+    return true;
+  }
+  
+  return false;
 }