827452cc987630467ab132f192afa4eb918d5a65
[quassel.git] / src / qtui / topicwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #include "graphicalui.h"
28 #include "uistyle.h"
29
30 TopicWidget::TopicWidget(QWidget *parent)
31     : AbstractItemView(parent)
32 {
33     ui.setupUi(this);
34     ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
35     ui.topicLineEdit->setWordWrapEnabled(true);
36     ui.topicLineEdit->installEventFilter(this);
37
38     connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
39     connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
40
41     UiSettings s("TopicWidget");
42     s.notify("DynamicResize", this, SLOT(updateResizeMode()));
43     s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
44     updateResizeMode();
45
46     UiStyleSettings fs("Fonts");
47     fs.notify("UseCustomTopicWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
48     fs.notify("TopicWidget", this, SLOT(setCustomFont(QVariant)));
49     if (fs.value("UseCustomTopicWidgetFont", false).toBool())
50         setCustomFont(fs.value("TopicWidget", QFont()));
51
52     _mouseEntered = false;
53     _readonly = false;
54 }
55
56
57 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous)
58 {
59     Q_UNUSED(previous);
60     setTopic(current);
61 }
62
63
64 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
65 {
66     QItemSelectionRange changedArea(topLeft, bottomRight);
67     QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
68     if (changedArea.contains(currentTopicIndex))
69         setTopic(selectionModel()->currentIndex());
70 };
71
72 void TopicWidget::setUseCustomFont(const QVariant &v)
73 {
74     if (v.toBool()) {
75         UiStyleSettings fs("Fonts");
76         setCustomFont(fs.value("TopicWidget").value<QFont>());
77     }
78     else
79         setCustomFont(QFont());
80 }
81
82
83 void TopicWidget::setCustomFont(const QVariant &v)
84 {
85     UiStyleSettings fs("Fonts");
86     if (!fs.value("UseCustomTopicWidgetFont", false).toBool())
87         return;
88
89     setCustomFont(v.value<QFont>());
90 }
91
92
93 void TopicWidget::setCustomFont(const QFont &f)
94 {
95     QFont font = f;
96     if (font.family().isEmpty())
97         font = QApplication::font();
98
99     ui.topicLineEdit->setCustomFont(font);
100     ui.topicLabel->setCustomFont(font);
101 }
102
103
104 void TopicWidget::setTopic(const QModelIndex &index)
105 {
106     QString newtopic;
107     bool readonly = true;
108
109     BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
110     if (id.isValid()) {
111         QModelIndex index0 = index.sibling(index.row(), 0);
112         const Network *network = Client::network(Client::networkModel()->networkId(id));
113
114         switch (Client::networkModel()->bufferType(id)) {
115         case BufferInfo::StatusBuffer:
116             if (network) {
117                 newtopic = QString("%1 (%2) | %3 | %4")
118                            .arg(Qt::escape(network->networkName()))
119                            .arg(Qt::escape(network->currentServer()))
120                            .arg(tr("Users: %1").arg(network->ircUsers().count()))
121                            .arg(tr("Lag: %1 msecs").arg(network->latency()));
122             }
123             else {
124                 newtopic = index0.data(Qt::DisplayRole).toString();
125             }
126             break;
127
128         case BufferInfo::ChannelBuffer:
129             newtopic = index.sibling(index.row(), 1).data().toString();
130             readonly = false;
131             break;
132
133         case BufferInfo::QueryBuffer:
134         {
135             QString nickname = index0.data(Qt::DisplayRole).toString();
136             if (network) {
137                 const IrcUser *user = network->ircUser(nickname);
138                 if (user) {
139                     newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
140                                .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
141                                .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
142                                .arg(user->user())
143                                .arg(user->host());
144                 }
145                 else { // no such user
146                     newtopic = nickname;
147                 }
148             }
149             else { // no valid Network-Obj.
150                 newtopic = nickname;
151             }
152             break;
153         }
154         default:
155             newtopic = index0.data(Qt::DisplayRole).toString();
156         }
157     }
158
159     _topic = newtopic;
160     _readonly = readonly;
161
162     ui.topicEditButton->setVisible(!_readonly);
163     ui.topicLabel->setText(newtopic);
164     ui.topicLineEdit->setPlainText(newtopic);
165     switchPlain();
166 }
167
168
169 void TopicWidget::setReadOnly(const bool &readonly)
170 {
171     if (_readonly == readonly)
172         return;
173
174     _readonly = readonly;
175 }
176
177
178 void TopicWidget::updateResizeMode()
179 {
180     StyledLabel::ResizeMode mode = StyledLabel::NoResize;
181     UiSettings s("TopicWidget");
182     if (s.value("DynamicResize", true).toBool()) {
183         if (s.value("ResizeOnHover", true).toBool())
184             mode = StyledLabel::ResizeOnHover;
185         else
186             mode = StyledLabel::DynamicResize;
187     }
188
189     ui.topicLabel->setResizeMode(mode);
190 }
191
192
193 void TopicWidget::clickableActivated(const Clickable &click)
194 {
195     NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
196     UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic), UiStyle::PlainMsg);
197     click.activate(networkId, sstr.plainText);
198 }
199
200
201 void TopicWidget::on_topicLineEdit_textEntered()
202 {
203     QModelIndex currentIdx = currentIndex();
204     if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
205         BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
206         if (ui.topicLineEdit->text().isEmpty())
207             Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
208         else
209             Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
210     }
211     switchPlain();
212 }
213
214
215 void TopicWidget::on_topicEditButton_clicked()
216 {
217     switchEditable();
218 }
219
220
221 void TopicWidget::switchEditable()
222 {
223     ui.stackedWidget->setCurrentIndex(1);
224     ui.topicLineEdit->setFocus();
225     ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
226     updateGeometry();
227 }
228
229
230 void TopicWidget::switchPlain()
231 {
232     ui.stackedWidget->setCurrentIndex(0);
233     ui.topicLineEdit->setPlainText(_topic);
234     updateGeometry();
235     emit switchedPlain();
236 }
237
238
239 // filter for the input widget to switch back to normal mode
240 bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
241 {
242     if (event->type() == QEvent::FocusOut && !_mouseEntered) {
243         switchPlain();
244         return true;
245     }
246
247     if (event->type() == QEvent::Enter) {
248         _mouseEntered = true;
249     }
250
251     if (event->type() == QEvent::Leave) {
252         _mouseEntered = false;
253     }
254
255     if (event->type() != QEvent::KeyRelease)
256         return QObject::eventFilter(obj, event);
257
258     QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
259
260     if (keyEvent->key() == Qt::Key_Escape) {
261         switchPlain();
262         return true;
263     }
264
265     return false;
266 }