1 /***************************************************************************
2 * Copyright (C) 2005/06 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "topicwidget.h"
26 #include "networkmodel.h"
28 TopicWidget::TopicWidget(QWidget *parent)
29 : AbstractItemView(parent)
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));
39 void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
41 setTopic(current.sibling(current.row(), 1).data().toString());
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());
51 void TopicWidget::setTopic(const QString &newtopic) {
52 if(_topic == newtopic)
56 ui.topicLabel->setText(newtopic);
57 ui.topicLineEdit->setText(newtopic);
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()));
70 void TopicWidget::on_topicEditButton_clicked() {
74 void TopicWidget::switchEditable() {
75 ui.topicLabel->hide();
76 ui.topicEditButton->hide();
77 ui.topicLineEdit->show();
78 ui.topicLineEdit->setFocus();
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();
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();
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) {
104 if(event->type() != QEvent::KeyPress)
105 return QObject::eventFilter(obj, event);
107 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
109 if(keyEvent->key() == Qt::Key_Escape) {