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