1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 "iconloader.h"
25 #include "networkmodel.h"
26 #include "uisettings.h"
27 #include "graphicalui.h"
30 TopicWidget::TopicWidget(QWidget *parent)
31 : AbstractItemView(parent)
34 ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
35 ui.topicLineEdit->setLineWrapEnabled(true);
36 ui.topicLineEdit->installEventFilter(this);
38 connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
39 connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
41 UiSettings s("TopicWidget");
42 s.notify("DynamicResize", this, SLOT(updateResizeMode()));
43 s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
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()));
52 _mouseEntered = false;
57 void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
64 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
66 QItemSelectionRange changedArea(topLeft, bottomRight);
67 QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
68 if (changedArea.contains(currentTopicIndex))
69 setTopic(selectionModel()->currentIndex());
72 void TopicWidget::setUseCustomFont(const QVariant &v)
75 UiStyleSettings fs("Fonts");
76 setCustomFont(fs.value("TopicWidget").value<QFont>());
79 setCustomFont(QFont());
83 void TopicWidget::setCustomFont(const QVariant &v)
85 UiStyleSettings fs("Fonts");
86 if (!fs.value("UseCustomTopicWidgetFont", false).toBool())
89 setCustomFont(v.value<QFont>());
93 void TopicWidget::setCustomFont(const QFont &f)
96 if (font.family().isEmpty())
97 font = QApplication::font();
99 ui.topicLineEdit->setCustomFont(font);
100 ui.topicLabel->setCustomFont(font);
104 void TopicWidget::setTopic(const QModelIndex &index)
107 bool readonly = true;
109 BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
111 QModelIndex index0 = index.sibling(index.row(), 0);
112 const Network *network = Client::network(Client::networkModel()->networkId(id));
114 switch (Client::networkModel()->bufferType(id)) {
115 case BufferInfo::StatusBuffer:
117 #if QT_VERSION < 0x050000
118 newtopic = QString("%1 (%2) | %3 | %4")
119 .arg(Qt::escape(network->networkName()))
120 .arg(Qt::escape(network->currentServer()))
121 .arg(tr("Users: %1").arg(network->ircUsers().count()))
122 .arg(tr("Lag: %1 msecs").arg(network->latency()));
124 newtopic = QString("%1 (%2) | %3 | %4")
125 .arg(network->networkName().toHtmlEscaped())
126 .arg(network->currentServer().toHtmlEscaped())
127 .arg(tr("Users: %1").arg(network->ircUsers().count()))
128 .arg(tr("Lag: %1 msecs").arg(network->latency()));
132 newtopic = index0.data(Qt::DisplayRole).toString();
136 case BufferInfo::ChannelBuffer:
137 newtopic = index.sibling(index.row(), 1).data().toString();
141 case BufferInfo::QueryBuffer:
143 QString nickname = index0.data(Qt::DisplayRole).toString();
145 const IrcUser *user = network->ircUser(nickname);
147 newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
148 .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
149 .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
153 else { // no such user
157 else { // no valid Network-Obj.
163 newtopic = index0.data(Qt::DisplayRole).toString();
167 _topic = sanitizeTopic(newtopic);
168 _readonly = readonly;
170 ui.topicEditButton->setVisible(!_readonly);
171 ui.topicLabel->setText(_topic);
172 ui.topicLineEdit->setPlainText(_topic);
177 void TopicWidget::setReadOnly(const bool &readonly)
179 if (_readonly == readonly)
182 _readonly = readonly;
186 void TopicWidget::updateResizeMode()
188 StyledLabel::ResizeMode mode = StyledLabel::NoResize;
189 UiSettings s("TopicWidget");
190 if (s.value("DynamicResize", true).toBool()) {
191 if (s.value("ResizeOnHover", true).toBool())
192 mode = StyledLabel::ResizeOnHover;
194 mode = StyledLabel::DynamicResize;
197 ui.topicLabel->setResizeMode(mode);
201 void TopicWidget::clickableActivated(const Clickable &click)
203 NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
204 UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic), UiStyle::PlainMsg);
205 click.activate(networkId, sstr.plainText);
209 void TopicWidget::on_topicLineEdit_textEntered()
211 QModelIndex currentIdx = currentIndex();
212 if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
213 BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
214 if (ui.topicLineEdit->text().isEmpty())
215 Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
217 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
223 void TopicWidget::on_topicEditButton_clicked()
229 void TopicWidget::switchEditable()
231 ui.stackedWidget->setCurrentIndex(1);
232 ui.topicLineEdit->setFocus();
233 ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
238 void TopicWidget::switchPlain()
240 ui.stackedWidget->setCurrentIndex(0);
241 ui.topicLineEdit->setPlainText(_topic);
243 emit switchedPlain();
247 // filter for the input widget to switch back to normal mode
248 bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
250 if (event->type() == QEvent::FocusOut && !_mouseEntered) {
255 if (event->type() == QEvent::Enter) {
256 _mouseEntered = true;
259 if (event->type() == QEvent::Leave) {
260 _mouseEntered = false;
263 if (event->type() != QEvent::KeyRelease)
264 return QObject::eventFilter(obj, event);
266 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
268 if (keyEvent->key() == Qt::Key_Escape) {
276 QString TopicWidget::sanitizeTopic(const QString& topic)
278 // Normally, you don't have new lines in topic messages
279 // But the use of "plain text" functionnality from Qt replaces
280 // some unicode characters with a new line, which then triggers
281 // a stack overflow later
282 QString result(topic);
283 result.replace(QChar::ParagraphSeparator, " ");
284 result.replace(QChar::LineSeparator, " ");