1 /***************************************************************************
2 * Copyright (C) 2005-2015 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"
26 #include "networkmodel.h"
27 #include "uisettings.h"
28 #include "graphicalui.h"
31 TopicWidget::TopicWidget(QWidget *parent)
32 : AbstractItemView(parent)
35 ui.topicEditButton->setIcon(QIcon::fromTheme("edit-rename"));
36 ui.topicLineEdit->setLineWrapEnabled(true);
37 ui.topicLineEdit->installEventFilter(this);
39 connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
40 connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
42 UiSettings s("TopicWidget");
43 s.notify("DynamicResize", this, SLOT(updateResizeMode()));
44 s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
47 UiStyleSettings fs("Fonts");
48 fs.notify("UseCustomTopicWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
49 fs.notify("TopicWidget", this, SLOT(setCustomFont(QVariant)));
50 if (fs.value("UseCustomTopicWidgetFont", false).toBool())
51 setCustomFont(fs.value("TopicWidget", QFont()));
53 _mouseEntered = false;
58 void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
65 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
67 QItemSelectionRange changedArea(topLeft, bottomRight);
68 QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
69 if (changedArea.contains(currentTopicIndex))
70 setTopic(selectionModel()->currentIndex());
73 void TopicWidget::setUseCustomFont(const QVariant &v)
76 UiStyleSettings fs("Fonts");
77 setCustomFont(fs.value("TopicWidget").value<QFont>());
80 setCustomFont(QFont());
84 void TopicWidget::setCustomFont(const QVariant &v)
86 UiStyleSettings fs("Fonts");
87 if (!fs.value("UseCustomTopicWidgetFont", false).toBool())
90 setCustomFont(v.value<QFont>());
94 void TopicWidget::setCustomFont(const QFont &f)
97 if (font.family().isEmpty())
98 font = QApplication::font();
100 ui.topicLineEdit->setCustomFont(font);
101 ui.topicLabel->setCustomFont(font);
105 void TopicWidget::setTopic(const QModelIndex &index)
108 bool readonly = true;
110 BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
112 QModelIndex index0 = index.sibling(index.row(), 0);
113 const Network *network = Client::network(Client::networkModel()->networkId(id));
115 switch (Client::networkModel()->bufferType(id)) {
116 case BufferInfo::StatusBuffer:
118 #if QT_VERSION < 0x050000
119 newtopic = QString("%1 (%2) | %3 | %4")
120 .arg(Qt::escape(network->networkName()))
121 .arg(Qt::escape(network->currentServer()))
122 .arg(tr("Users: %1").arg(network->ircUsers().count()))
123 .arg(tr("Lag: %1 msecs").arg(network->latency()));
125 newtopic = QString("%1 (%2) | %3 | %4")
126 .arg(network->networkName().toHtmlEscaped())
127 .arg(network->currentServer().toHtmlEscaped())
128 .arg(tr("Users: %1").arg(network->ircUsers().count()))
129 .arg(tr("Lag: %1 msecs").arg(network->latency()));
133 newtopic = index0.data(Qt::DisplayRole).toString();
137 case BufferInfo::ChannelBuffer:
138 newtopic = index.sibling(index.row(), 1).data().toString();
142 case BufferInfo::QueryBuffer:
144 QString nickname = index0.data(Qt::DisplayRole).toString();
146 const IrcUser *user = network->ircUser(nickname);
148 newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
149 .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
150 .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
154 else { // no such user
158 else { // no valid Network-Obj.
164 newtopic = index0.data(Qt::DisplayRole).toString();
168 _topic = sanitizeTopic(newtopic);
169 _readonly = readonly;
171 ui.topicEditButton->setVisible(!_readonly);
172 ui.topicLabel->setText(_topic);
173 ui.topicLineEdit->setPlainText(_topic);
178 void TopicWidget::setReadOnly(const bool &readonly)
180 if (_readonly == readonly)
183 _readonly = readonly;
187 void TopicWidget::updateResizeMode()
189 StyledLabel::ResizeMode mode = StyledLabel::NoResize;
190 UiSettings s("TopicWidget");
191 if (s.value("DynamicResize", true).toBool()) {
192 if (s.value("ResizeOnHover", true).toBool())
193 mode = StyledLabel::ResizeOnHover;
195 mode = StyledLabel::DynamicResize;
198 ui.topicLabel->setResizeMode(mode);
202 void TopicWidget::clickableActivated(const Clickable &click)
204 NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
205 UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic), UiStyle::PlainMsg);
206 click.activate(networkId, sstr.plainText);
210 void TopicWidget::on_topicLineEdit_textEntered()
212 QModelIndex currentIdx = currentIndex();
213 if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
214 BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
215 if (ui.topicLineEdit->text().isEmpty())
216 Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
218 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
224 void TopicWidget::on_topicEditButton_clicked()
230 void TopicWidget::switchEditable()
232 ui.stackedWidget->setCurrentIndex(1);
233 ui.topicLineEdit->setFocus();
234 ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
239 void TopicWidget::switchPlain()
241 ui.stackedWidget->setCurrentIndex(0);
242 ui.topicLineEdit->setPlainText(_topic);
244 emit switchedPlain();
248 // filter for the input widget to switch back to normal mode
249 bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
251 if (event->type() == QEvent::FocusOut && !_mouseEntered) {
256 if (event->type() == QEvent::Enter) {
257 _mouseEntered = true;
260 if (event->type() == QEvent::Leave) {
261 _mouseEntered = false;
264 if (event->type() != QEvent::KeyRelease)
265 return QObject::eventFilter(obj, event);
267 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
269 if (keyEvent->key() == Qt::Key_Escape) {
277 QString TopicWidget::sanitizeTopic(const QString& topic)
279 // Normally, you don't have new lines in topic messages
280 // But the use of "plain text" functionnality from Qt replaces
281 // some unicode characters with a new line, which then triggers
282 // a stack overflow later
283 QString result(topic);
284 result.replace(QChar::ParagraphSeparator, " ");
285 result.replace(QChar::LineSeparator, " ");