Fixed bugs in topic widget
[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 "client.h"
24 #include "iconloader.h"
25 #include "networkmodel.h"
26 #include "uisettings.h"
27
28 TopicWidget::TopicWidget(QWidget *parent)
29   : AbstractItemView(parent)
30 {
31   ui.setupUi(this);
32   ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
33   ui.topicLineEdit->setWordWrapEnabled(true);
34   ui.topicLineEdit->installEventFilter(this);
35
36   connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
37   connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
38
39   UiSettings s("TopicWidget");
40   s.notify("DynamicResize", this, SLOT(updateResizeMode()));
41   s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
42   updateResizeMode();
43
44   UiStyleSettings fs("Fonts");
45   fs.notify("UseCustomTopicWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
46   fs.notify("TopicWidget", this, SLOT(setCustomFont(QVariant)));
47   if(fs.value("UseCustomTopicWidgetFont", false).toBool())
48     setCustomFont(fs.value("TopicWidget", QFont()));
49
50   _mouseEntered = false;
51 }
52
53 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
54   Q_UNUSED(previous);
55   setTopic(current.sibling(current.row(), 1).data().toString());
56 }
57
58 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
59   QItemSelectionRange changedArea(topLeft, bottomRight);
60   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
61   if(changedArea.contains(currentTopicIndex))
62     setTopic(currentTopicIndex.data().toString());
63 };
64
65 void TopicWidget::setUseCustomFont(const QVariant &v) {
66   if(v.toBool()) {
67     UiStyleSettings fs("Fonts");
68     setCustomFont(fs.value("TopicWidget").value<QFont>());
69   } else
70     setCustomFont(QFont());
71 }
72
73 void TopicWidget::setCustomFont(const QVariant &v) {
74   UiStyleSettings fs("Fonts");
75   if(!fs.value("UseCustomTopicWidgetFont", false).toBool())
76     return;
77
78   setCustomFont(v.value<QFont>());
79 }
80
81 void TopicWidget::setCustomFont(const QFont &f) {
82   QFont font = f;
83   if(font.family().isEmpty())
84     font = QApplication::font();
85
86   ui.topicLineEdit->setCustomFont(font);
87   ui.topicLabel->setCustomFont(font);
88 }
89
90 void TopicWidget::setTopic(const QString &newtopic) {
91   if(_topic == newtopic)
92     return;
93
94   _topic = newtopic;
95   ui.topicLabel->setText(newtopic);
96   ui.topicLineEdit->setText(newtopic);
97   switchPlain();
98 }
99
100 void TopicWidget::updateResizeMode() {
101   StyledLabel::ResizeMode mode = StyledLabel::NoResize;
102   UiSettings s("TopicWidget");
103   if(s.value("DynamicResize", true).toBool()) {
104     if(s.value("ResizeOnHover", true).toBool())
105       mode = StyledLabel::ResizeOnHover;
106     else
107       mode = StyledLabel::DynamicResize;
108   }
109
110   ui.topicLabel->setResizeMode(mode);
111 }
112
113 void TopicWidget::clickableActivated(const Clickable &click) {
114   NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
115   click.activate(networkId, _topic);
116 }
117
118 void TopicWidget::on_topicLineEdit_textEntered() {
119   QModelIndex currentIdx = currentIndex();
120   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
121     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
122     if(ui.topicLineEdit->text().isEmpty())
123       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
124     else
125       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
126   }
127   switchPlain();
128 }
129
130 void TopicWidget::on_topicEditButton_clicked() {
131   switchEditable();
132 }
133
134 void TopicWidget::switchEditable() {
135   ui.stackedWidget->setCurrentIndex(1);
136   ui.topicLineEdit->setFocus();
137   ui.topicLineEdit->moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
138   updateGeometry();
139 }
140
141 void TopicWidget::switchPlain() {
142   ui.stackedWidget->setCurrentIndex(0);
143   ui.topicLineEdit->setText(_topic);
144   updateGeometry();
145 }
146
147 // filter for the input widget to switch back to normal mode
148 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
149   
150   if(event->type() == QEvent::FocusOut && !_mouseEntered) {
151     switchPlain();
152     return true;
153   }
154   
155   if(event->type() == QEvent::Enter) {
156     _mouseEntered = true;
157   }
158   
159   if(event->type() == QEvent::Leave) {
160     _mouseEntered = false;
161   }
162   
163   if(event->type() != QEvent::KeyRelease)
164     return QObject::eventFilter(obj, event);
165
166   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
167
168   if(keyEvent->key() == Qt::Key_Escape) {
169     switchPlain();
170     return true;
171   }
172
173   return false;
174 }