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