1 /***************************************************************************
2 * Copyright (C) 2005/06 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "topicwidget.h"
24 #include "iconloader.h"
25 #include "networkmodel.h"
26 #include "uisettings.h"
28 TopicWidget::TopicWidget(QWidget *parent)
29 : AbstractItemView(parent)
32 ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
33 ui.topicLineEdit->setWordWrapEnabled(true);
34 ui.topicLineEdit->installEventFilter(this);
36 connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
37 connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
39 UiSettings s("TopicWidget");
40 s.notify("DynamicResize", this, SLOT(updateResizeMode()));
41 s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
44 UiStyleSettings fs("Fonts");
45 fs.notify("UseCustomTopicWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
46 fs.notify("TopicWidget", this, SLOT(setCustomFont(QVariant)));
47 if(fs.value("UseCustomTopicWidgetFont", false).toBool())
48 setCustomFont(fs.value("TopicWidget", QFont()));
50 _mouseEntered = false;
54 void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
59 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
60 QItemSelectionRange changedArea(topLeft, bottomRight);
61 QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
62 if(changedArea.contains(currentTopicIndex))
63 setTopic(selectionModel()->currentIndex());
66 void TopicWidget::setUseCustomFont(const QVariant &v) {
68 UiStyleSettings fs("Fonts");
69 setCustomFont(fs.value("TopicWidget").value<QFont>());
71 setCustomFont(QFont());
74 void TopicWidget::setCustomFont(const QVariant &v) {
75 UiStyleSettings fs("Fonts");
76 if(!fs.value("UseCustomTopicWidgetFont", false).toBool())
79 setCustomFont(v.value<QFont>());
82 void TopicWidget::setCustomFont(const QFont &f) {
84 if(font.family().isEmpty())
85 font = QApplication::font();
87 ui.topicLineEdit->setCustomFont(font);
88 ui.topicLabel->setCustomFont(font);
91 void TopicWidget::setTopic(const QModelIndex &index) {
92 BufferId id = index.sibling(index.row(), 0).data(NetworkModel::BufferIdRole).value<BufferId>();
96 ui.topicEditButton->setVisible(false);
97 ui.topicLabel->setText(_topic);
98 ui.topicLineEdit->setText(_topic);
103 const Network *network = Client::network(Client::networkModel()->networkId(id));
106 if(Client::networkModel()->bufferType(id) == BufferInfo::StatusBuffer) {
107 newtopic = QString("%1 (%2) | %3 | %4")
108 .arg(Qt::escape(network->networkName()))
109 .arg(Qt::escape(network->currentServer()))
110 .arg(tr("Users: %1").arg(network->ircUsers().count()))
111 .arg(tr("Lag: %1 msecs").arg(network->latency()));
113 } else if(Client::networkModel()->bufferType(id) == BufferInfo::QueryBuffer) {
114 newtopic = QString("%1").arg(index.sibling(index.row(), 0).data().toString());
115 const IrcUser *user = network->ircUser(QString(index.sibling(index.row(), 0).data().toString()));
117 if(!user->userModes().isEmpty())
118 newtopic.append(QString(" (+%1)").arg(user->userModes()));
119 if(!user->realName().isEmpty())
120 newtopic.append(QString(" | %1").arg(user->realName()));
121 newtopic.append(QString(" | %1").arg(user->hostmask().remove(0, user->hostmask().indexOf("!")+1)));
124 } else if(Client::networkModel()->bufferType(id) == BufferInfo::ChannelBuffer) {
125 newtopic = index.sibling(index.row(), 1).data().toString();
133 ui.topicEditButton->setVisible(!_readonly);
136 ui.topicLabel->setText(newtopic);
137 ui.topicLineEdit->setText(newtopic);
141 void TopicWidget::setReadOnly(const bool &readonly) {
142 if(_readonly == readonly)
145 _readonly = readonly;
148 void TopicWidget::updateResizeMode() {
149 StyledLabel::ResizeMode mode = StyledLabel::NoResize;
150 UiSettings s("TopicWidget");
151 if(s.value("DynamicResize", true).toBool()) {
152 if(s.value("ResizeOnHover", true).toBool())
153 mode = StyledLabel::ResizeOnHover;
155 mode = StyledLabel::DynamicResize;
158 ui.topicLabel->setResizeMode(mode);
161 void TopicWidget::clickableActivated(const Clickable &click) {
162 NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
163 click.activate(networkId, _topic);
166 void TopicWidget::on_topicLineEdit_textEntered() {
167 QModelIndex currentIdx = currentIndex();
168 if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
169 BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
170 if(ui.topicLineEdit->text().isEmpty())
171 Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
173 Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
178 void TopicWidget::on_topicEditButton_clicked() {
182 void TopicWidget::switchEditable() {
183 ui.stackedWidget->setCurrentIndex(1);
184 ui.topicLineEdit->setFocus();
185 ui.topicLineEdit->moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
189 void TopicWidget::switchPlain() {
190 ui.stackedWidget->setCurrentIndex(0);
191 ui.topicLineEdit->setText(_topic);
193 emit switchedPlain();
196 // filter for the input widget to switch back to normal mode
197 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
199 if(event->type() == QEvent::FocusOut && !_mouseEntered) {
204 if(event->type() == QEvent::Enter) {
205 _mouseEntered = true;
208 if(event->type() == QEvent::Leave) {
209 _mouseEntered = false;
212 if(event->type() != QEvent::KeyRelease)
213 return QObject::eventFilter(obj, event);
215 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
217 if(keyEvent->key() == Qt::Key_Escape) {