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 "iconloader.h"
27 #include "networkmodel.h"
29 TopicWidget::TopicWidget(QWidget *parent)
30 : AbstractItemView(parent)
33 ui.topicEditButton->setPixmap(BarIcon("edit-rename"));
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));
42 void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
44 setTopic(current.sibling(current.row(), 1).data().toString());
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());
54 void TopicWidget::setTopic(const QString &newtopic) {
55 if(_topic == newtopic)
59 ui.topicLabel->setText(newtopic);
60 ui.topicLineEdit->setText(newtopic);
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()));
71 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
76 void TopicWidget::on_topicEditButton_clicked() {
80 void TopicWidget::switchEditable() {
81 ui.topicLabel->hide();
82 ui.topicEditButton->hide();
83 ui.topicLineEdit->show();
84 ui.topicLineEdit->setFocus();
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();
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();
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) {
110 if(event->type() != QEvent::KeyPress)
111 return QObject::eventFilter(obj, event);
113 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
115 if(keyEvent->key() == Qt::Key_Escape) {