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 newtopic = QString("%1 (%2) | %3 | %4")
118 .arg(Qt::escape(network->networkName()))
119 .arg(Qt::escape(network->currentServer()))
120 .arg(tr("Users: %1").arg(network->ircUsers().count()))
121 .arg(tr("Lag: %1 msecs").arg(network->latency()));
124 newtopic = index0.data(Qt::DisplayRole).toString();
128 case BufferInfo::ChannelBuffer:
129 newtopic = index.sibling(index.row(), 1).data().toString();
133 case BufferInfo::QueryBuffer:
135 QString nickname = index0.data(Qt::DisplayRole).toString();
137 const IrcUser *user = network->ircUser(nickname);
139 newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
140 .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
141 .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
145 else { // no such user
149 else { // no valid Network-Obj.
155 newtopic = index0.data(Qt::DisplayRole).toString();
159 _topic = sanitizeTopic(newtopic);
160 _readonly = readonly;
162 ui.topicEditButton->setVisible(!_readonly);
163 ui.topicLabel->setText(_topic);
164 ui.topicLineEdit->setPlainText(_topic);
169 void TopicWidget::setReadOnly(const bool &readonly)
171 if (_readonly == readonly)
174 _readonly = readonly;
178 void TopicWidget::updateResizeMode()
180 StyledLabel::ResizeMode mode = StyledLabel::NoResize;
181 UiSettings s("TopicWidget");
182 if (s.value("DynamicResize", true).toBool()) {
183 if (s.value("ResizeOnHover", true).toBool())
184 mode = StyledLabel::ResizeOnHover;
186 mode = StyledLabel::DynamicResize;
189 ui.topicLabel->setResizeMode(mode);
193 void TopicWidget::clickableActivated(const Clickable &click)
195 NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
196 UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic), UiStyle::PlainMsg);
197 click.activate(networkId, sstr.plainText);
201 void TopicWidget::on_topicLineEdit_textEntered()
203 QModelIndex currentIdx = currentIndex();
204 if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
205 BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
206 if (ui.topicLineEdit->text().isEmpty())
207 Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
209 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
215 void TopicWidget::on_topicEditButton_clicked()
221 void TopicWidget::switchEditable()
223 ui.stackedWidget->setCurrentIndex(1);
224 ui.topicLineEdit->setFocus();
225 ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
230 void TopicWidget::switchPlain()
232 ui.stackedWidget->setCurrentIndex(0);
233 ui.topicLineEdit->setPlainText(_topic);
235 emit switchedPlain();
239 // filter for the input widget to switch back to normal mode
240 bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
242 if (event->type() == QEvent::FocusOut && !_mouseEntered) {
247 if (event->type() == QEvent::Enter) {
248 _mouseEntered = true;
251 if (event->type() == QEvent::Leave) {
252 _mouseEntered = false;
255 if (event->type() != QEvent::KeyRelease)
256 return QObject::eventFilter(obj, event);
258 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
260 if (keyEvent->key() == Qt::Key_Escape) {
268 QString TopicWidget::sanitizeTopic(const QString& topic)
270 // Normally, you don't have new lines in topic messages
271 // But the use of "plain text" functionnality from Qt replaces
272 // some unicode characters with a new line, which then triggers
273 // a stack overflow later
274 QString result(topic);
275 result.replace(QChar::ParagraphSeparator, " ");
276 result.replace(QChar::LineSeparator, " ");