Fix tray icon behavior for non-KDE StatusNotifier
[quassel.git] / src / qtui / topicwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
55   Q_UNUSED(previous);
56   setTopic(current);
57 }
58
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());
64 };
65
66 void TopicWidget::setUseCustomFont(const QVariant &v) {
67   if(v.toBool()) {
68     UiStyleSettings fs("Fonts");
69     setCustomFont(fs.value("TopicWidget").value<QFont>());
70   } else
71     setCustomFont(QFont());
72 }
73
74 void TopicWidget::setCustomFont(const QVariant &v) {
75   UiStyleSettings fs("Fonts");
76   if(!fs.value("UseCustomTopicWidgetFont", false).toBool())
77     return;
78
79   setCustomFont(v.value<QFont>());
80 }
81
82 void TopicWidget::setCustomFont(const QFont &f) {
83   QFont font = f;
84   if(font.family().isEmpty())
85     font = QApplication::font();
86
87   ui.topicLineEdit->setCustomFont(font);
88   ui.topicLabel->setCustomFont(font);
89 }
90
91 void TopicWidget::setTopic(const QModelIndex &index) {
92   QString newtopic;
93   bool readonly = true;
94
95   BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
96   if(id.isValid()) {
97     QModelIndex index0 = index.sibling(index.row(), 0);
98     const Network *network = Client::network(Client::networkModel()->networkId(id));
99
100     switch(Client::networkModel()->bufferType(id)) {
101     case BufferInfo::StatusBuffer:
102       if(network) {
103         newtopic = QString("%1 (%2) | %3 | %4")
104           .arg(Qt::escape(network->networkName()))
105           .arg(Qt::escape(network->currentServer()))
106           .arg(tr("Users: %1").arg(network->ircUsers().count()))
107           .arg(tr("Lag: %1 msecs").arg(network->latency()));
108       } else {
109         newtopic = index0.data(Qt::DisplayRole).toString();
110       }
111       break;
112
113     case BufferInfo::ChannelBuffer:
114       newtopic = index.sibling(index.row(), 1).data().toString();
115       readonly = false;
116       break;
117
118     case BufferInfo::QueryBuffer:
119       {
120         QString nickname = index0.data(Qt::DisplayRole).toString();
121         if(network) {
122           const IrcUser *user = network->ircUser(nickname);
123           if(user) {
124             newtopic = QString("%1%2%3 | %4@%5").arg(nickname)
125               .arg(user->userModes().isEmpty() ? QString() : QString(" (+%1)").arg(user->userModes()))
126               .arg(user->realName().isEmpty() ? QString() : QString(" | %1").arg(user->realName()))
127               .arg(user->user())
128             .arg(user->host());
129           } else { // no such user
130             newtopic = nickname;
131           }
132         } else { // no valid Network-Obj.
133           newtopic = nickname;
134         }
135         break;
136       }
137     default:
138       newtopic = index0.data(Qt::DisplayRole).toString();
139     }
140   }
141
142   _topic = newtopic;
143   _readonly = readonly;
144
145   ui.topicEditButton->setVisible(!_readonly);
146   ui.topicLabel->setText(newtopic);
147   ui.topicLineEdit->setText(newtopic);
148   switchPlain();
149 }
150
151
152 void TopicWidget::setReadOnly(const bool &readonly) {
153   if(_readonly == readonly)
154     return;
155
156   _readonly = readonly;
157 }
158
159 void TopicWidget::updateResizeMode() {
160   StyledLabel::ResizeMode mode = StyledLabel::NoResize;
161   UiSettings s("TopicWidget");
162   if(s.value("DynamicResize", true).toBool()) {
163     if(s.value("ResizeOnHover", true).toBool())
164       mode = StyledLabel::ResizeOnHover;
165     else
166       mode = StyledLabel::DynamicResize;
167   }
168
169   ui.topicLabel->setResizeMode(mode);
170 }
171
172 void TopicWidget::clickableActivated(const Clickable &click) {
173   NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
174   click.activate(networkId, _topic);
175 }
176
177 void TopicWidget::on_topicLineEdit_textEntered() {
178   QModelIndex currentIdx = currentIndex();
179   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
180     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
181     if(ui.topicLineEdit->text().isEmpty())
182       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
183     else
184       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
185   }
186   switchPlain();
187 }
188
189 void TopicWidget::on_topicEditButton_clicked() {
190   switchEditable();
191 }
192
193 void TopicWidget::switchEditable() {
194   ui.stackedWidget->setCurrentIndex(1);
195   ui.topicLineEdit->setFocus();
196   ui.topicLineEdit->moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
197   updateGeometry();
198 }
199
200 void TopicWidget::switchPlain() {
201   ui.stackedWidget->setCurrentIndex(0);
202   ui.topicLineEdit->setText(_topic);
203   updateGeometry();
204   emit switchedPlain();
205 }
206
207 // filter for the input widget to switch back to normal mode
208 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
209
210   if(event->type() == QEvent::FocusOut && !_mouseEntered) {
211     switchPlain();
212     return true;
213   }
214
215   if(event->type() == QEvent::Enter) {
216     _mouseEntered = true;
217   }
218
219   if(event->type() == QEvent::Leave) {
220     _mouseEntered = false;
221   }
222
223   if(event->type() != QEvent::KeyRelease)
224     return QObject::eventFilter(obj, event);
225
226   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
227
228   if(keyEvent->key() == Qt::Key_Escape) {
229     switchPlain();
230     return true;
231   }
232
233   return false;
234 }