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