1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "topicwidget.h"
24 #include "graphicalui.h"
26 #include "networkmodel.h"
27 #include "uisettings.h"
31 TopicWidget::TopicWidget(QWidget* parent)
32 : AbstractItemView(parent)
35 ui.topicEditButton->setIcon(icon::get("edit-rename"));
36 ui.topicLineEdit->setLineWrapEnabled(true);
37 ui.topicLineEdit->installEventFilter(this);
39 connect(ui.topicLabel, &StyledLabel::clickableActivated, this, &TopicWidget::clickableActivated);
40 connect(ui.topicLineEdit, &MultiLineEdit::noTextEntered, this, &TopicWidget::on_topicLineEdit_textEntered);
42 UiSettings s("TopicWidget");
43 s.notify("DynamicResize", this, &TopicWidget::updateResizeMode);
44 s.notify("ResizeOnHover", this, &TopicWidget::updateResizeMode);
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()));
53 _mouseEntered = false;
57 void TopicWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
63 void TopicWidget::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
65 QItemSelectionRange changedArea(topLeft, bottomRight);
66 QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
67 if (changedArea.contains(currentTopicIndex))
68 setTopic(selectionModel()->currentIndex());
71 void TopicWidget::setUseCustomFont(const QVariant& v)
74 UiStyleSettings fs("Fonts");
75 setCustomFont(fs.value("TopicWidget").value<QFont>());
78 setCustomFont(QFont());
81 void TopicWidget::setCustomFont(const QVariant& v)
83 UiStyleSettings fs("Fonts");
84 if (!fs.value("UseCustomTopicWidgetFont", false).toBool())
87 setCustomFont(v.value<QFont>());
90 void TopicWidget::setCustomFont(const QFont& f)
93 if (font.family().isEmpty())
94 font = QApplication::font();
96 ui.topicLineEdit->setCustomFont(font);
97 ui.topicLabel->setCustomFont(font);
100 void TopicWidget::setTopic(const QModelIndex& index)
103 bool readonly = true;
105 BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
107 QModelIndex index0 = index.sibling(index.row(), 0);
108 const Network* network = Client::network(Client::networkModel()->networkId(id));
110 switch (Client::networkModel()->bufferType(id)) {
111 case BufferInfo::StatusBuffer:
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()));
120 newtopic = index0.data(Qt::DisplayRole).toString();
124 case BufferInfo::ChannelBuffer:
125 newtopic = index.sibling(index.row(), 1).data().toString();
129 case BufferInfo::QueryBuffer: {
130 QString nickname = index0.data(Qt::DisplayRole).toString();
132 const IrcUser* user = network->ircUser(nickname);
134 newtopic = QString("%1%2%3 | %4@%5")
136 .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
137 .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
141 else { // no such user
145 else { // no valid Network-Obj.
151 newtopic = index0.data(Qt::DisplayRole).toString();
155 _topic = sanitizeTopic(newtopic);
156 _readonly = readonly;
158 ui.topicEditButton->setVisible(!_readonly);
159 ui.topicLabel->setText(_topic);
160 ui.topicLineEdit->setPlainText(_topic);
164 void TopicWidget::setReadOnly(const bool& readonly)
166 if (_readonly == readonly)
169 _readonly = readonly;
172 void TopicWidget::updateResizeMode()
174 StyledLabel::ResizeMode mode = StyledLabel::NoResize;
175 UiSettings s("TopicWidget");
176 if (s.value("DynamicResize", true).toBool()) {
177 if (s.value("ResizeOnHover", true).toBool())
178 mode = StyledLabel::ResizeOnHover;
180 mode = StyledLabel::DynamicResize;
183 ui.topicLabel->setResizeMode(mode);
186 void TopicWidget::clickableActivated(const Clickable& click)
188 NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
189 UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic),
190 UiStyle::FormatType::PlainMsg);
191 click.activate(networkId, sstr.plainText);
194 void TopicWidget::on_topicLineEdit_textEntered()
196 QModelIndex currentIdx = currentIndex();
197 if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
198 BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
199 if (ui.topicLineEdit->text().isEmpty())
200 Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
202 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
207 void TopicWidget::on_topicEditButton_clicked()
212 void TopicWidget::switchEditable()
214 ui.stackedWidget->setCurrentIndex(1);
215 ui.topicLineEdit->setFocus();
216 ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
220 void TopicWidget::switchPlain()
222 ui.stackedWidget->setCurrentIndex(0);
223 ui.topicLineEdit->setPlainText(_topic);
225 emit switchedPlain();
228 // filter for the input widget to switch back to normal mode
229 bool TopicWidget::eventFilter(QObject* obj, QEvent* event)
231 if (event->type() == QEvent::FocusOut && !_mouseEntered) {
236 if (event->type() == QEvent::Enter) {
237 _mouseEntered = true;
240 if (event->type() == QEvent::Leave) {
241 _mouseEntered = false;
244 if (event->type() != QEvent::KeyRelease)
245 return QObject::eventFilter(obj, event);
247 auto* keyEvent = static_cast<QKeyEvent*>(event);
249 if (keyEvent->key() == Qt::Key_Escape) {
257 QString TopicWidget::sanitizeTopic(const QString& topic)
259 // Normally, you don't have new lines in topic messages
260 // But the use of "plain text" functionnality from Qt replaces
261 // some unicode characters with a new line, which then triggers
262 // a stack overflow later
263 QString result(topic);
264 result.replace(QChar::CarriageReturn, " ");
265 result.replace(QChar::ParagraphSeparator, " ");
266 result.replace(QChar::LineSeparator, " ");