Merge pull request #2 from sandsmark/wii
[quassel.git] / src / qtui / topicwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "topicwidget.h"
22
23 #include "client.h"
24 #include "iconloader.h"
25 #include "networkmodel.h"
26 #include "uisettings.h"
27
28 TopicWidget::TopicWidget(QWidget *parent)
29     : AbstractItemView(parent)
30 {
31     ui.setupUi(this);
32     ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
33     ui.topicLineEdit->setWordWrapEnabled(true);
34     ui.topicLineEdit->installEventFilter(this);
35
36     connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
37     connect(ui.topicLineEdit, SIGNAL(noTextEntered()), SLOT(on_topicLineEdit_textEntered()));
38
39     UiSettings s("TopicWidget");
40     s.notify("DynamicResize", this, SLOT(updateResizeMode()));
41     s.notify("ResizeOnHover", this, SLOT(updateResizeMode()));
42     updateResizeMode();
43
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()));
49
50     _mouseEntered = false;
51     _readonly = false;
52 }
53
54
55 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous)
56 {
57     Q_UNUSED(previous);
58     setTopic(current);
59 }
60
61
62 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
63 {
64     QItemSelectionRange changedArea(topLeft, bottomRight);
65     QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
66     if (changedArea.contains(currentTopicIndex))
67         setTopic(selectionModel()->currentIndex());
68 };
69
70 void TopicWidget::setUseCustomFont(const QVariant &v)
71 {
72     if (v.toBool()) {
73         UiStyleSettings fs("Fonts");
74         setCustomFont(fs.value("TopicWidget").value<QFont>());
75     }
76     else
77         setCustomFont(QFont());
78 }
79
80
81 void TopicWidget::setCustomFont(const QVariant &v)
82 {
83     UiStyleSettings fs("Fonts");
84     if (!fs.value("UseCustomTopicWidgetFont", false).toBool())
85         return;
86
87     setCustomFont(v.value<QFont>());
88 }
89
90
91 void TopicWidget::setCustomFont(const QFont &f)
92 {
93     QFont font = f;
94     if (font.family().isEmpty())
95         font = QApplication::font();
96
97     ui.topicLineEdit->setCustomFont(font);
98     ui.topicLabel->setCustomFont(font);
99 }
100
101
102 void TopicWidget::setTopic(const QModelIndex &index)
103 {
104     QString newtopic;
105     bool readonly = true;
106
107     BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
108     if (id.isValid()) {
109         QModelIndex index0 = index.sibling(index.row(), 0);
110         const Network *network = Client::network(Client::networkModel()->networkId(id));
111
112         switch (Client::networkModel()->bufferType(id)) {
113         case BufferInfo::StatusBuffer:
114             if (network) {
115                 newtopic = QString("%1 (%2) | %3 | %4")
116                            .arg(Qt::escape(network->networkName()))
117                            .arg(Qt::escape(network->currentServer()))
118                            .arg(tr("Users: %1").arg(network->ircUsers().count()))
119                            .arg(tr("Lag: %1 msecs").arg(network->latency()));
120             }
121             else {
122                 newtopic = index0.data(Qt::DisplayRole).toString();
123             }
124             break;
125
126         case BufferInfo::ChannelBuffer:
127             newtopic = index.sibling(index.row(), 1).data().toString();
128             readonly = false;
129             break;
130
131         case BufferInfo::QueryBuffer:
132         {
133             QString nickname = index0.data(Qt::DisplayRole).toString();
134             if (network) {
135                 const IrcUser *user = network->ircUser(nickname);
136                 if (user) {
137                     newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
138                                .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
139                                .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
140                                .arg(user->user())
141                                .arg(user->host());
142                 }
143                 else { // no such user
144                     newtopic = nickname;
145                 }
146             }
147             else { // no valid Network-Obj.
148                 newtopic = nickname;
149             }
150             break;
151         }
152         default:
153             newtopic = index0.data(Qt::DisplayRole).toString();
154         }
155     }
156
157     _topic = newtopic;
158     _readonly = readonly;
159
160     ui.topicEditButton->setVisible(!_readonly);
161     ui.topicLabel->setText(newtopic);
162     ui.topicLineEdit->setPlainText(newtopic);
163     switchPlain();
164 }
165
166
167 void TopicWidget::setReadOnly(const bool &readonly)
168 {
169     if (_readonly == readonly)
170         return;
171
172     _readonly = readonly;
173 }
174
175
176 void TopicWidget::updateResizeMode()
177 {
178     StyledLabel::ResizeMode mode = StyledLabel::NoResize;
179     UiSettings s("TopicWidget");
180     if (s.value("DynamicResize", true).toBool()) {
181         if (s.value("ResizeOnHover", true).toBool())
182             mode = StyledLabel::ResizeOnHover;
183         else
184             mode = StyledLabel::DynamicResize;
185     }
186
187     ui.topicLabel->setResizeMode(mode);
188 }
189
190
191 void TopicWidget::clickableActivated(const Clickable &click)
192 {
193     NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
194     click.activate(networkId, _topic);
195 }
196
197
198 void TopicWidget::on_topicLineEdit_textEntered()
199 {
200     QModelIndex currentIdx = currentIndex();
201     if (currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
202         BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
203         if (ui.topicLineEdit->text().isEmpty())
204             Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
205         else
206             Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
207     }
208     switchPlain();
209 }
210
211
212 void TopicWidget::on_topicEditButton_clicked()
213 {
214     switchEditable();
215 }
216
217
218 void TopicWidget::switchEditable()
219 {
220     ui.stackedWidget->setCurrentIndex(1);
221     ui.topicLineEdit->setFocus();
222     ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
223     updateGeometry();
224 }
225
226
227 void TopicWidget::switchPlain()
228 {
229     ui.stackedWidget->setCurrentIndex(0);
230     ui.topicLineEdit->setPlainText(_topic);
231     updateGeometry();
232     emit switchedPlain();
233 }
234
235
236 // filter for the input widget to switch back to normal mode
237 bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
238 {
239     if (event->type() == QEvent::FocusOut && !_mouseEntered) {
240         switchPlain();
241         return true;
242     }
243
244     if (event->type() == QEvent::Enter) {
245         _mouseEntered = true;
246     }
247
248     if (event->type() == QEvent::Leave) {
249         _mouseEntered = false;
250     }
251
252     if (event->type() != QEvent::KeyRelease)
253         return QObject::eventFilter(obj, event);
254
255     QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
256
257     if (keyEvent->key() == Qt::Key_Escape) {
258         switchPlain();
259         return true;
260     }
261
262     return false;
263 }