Add SslInfoDlg as a nice way to show information about an SSL connection
[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 }
51
52 void TopicWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
53   Q_UNUSED(previous);
54   setTopic(current.sibling(current.row(), 1).data().toString());
55 }
56
57 void TopicWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
58   QItemSelectionRange changedArea(topLeft, bottomRight);
59   QModelIndex currentTopicIndex = selectionModel()->currentIndex().sibling(selectionModel()->currentIndex().row(), 1);
60   if(changedArea.contains(currentTopicIndex))
61     setTopic(currentTopicIndex.data().toString());
62 };
63
64 void TopicWidget::setUseCustomFont(const QVariant &v) {
65   if(v.toBool()) {
66     UiStyleSettings fs("Fonts");
67     setCustomFont(fs.value("TopicWidget").value<QFont>());
68   } else
69     setCustomFont(QFont());
70 }
71
72 void TopicWidget::setCustomFont(const QVariant &v) {
73   UiStyleSettings fs("Fonts");
74   if(!fs.value("UseCustomTopicWidgetFont", false).toBool())
75     return;
76
77   setCustomFont(v.value<QFont>());
78 }
79
80 void TopicWidget::setCustomFont(const QFont &f) {
81   QFont font = f;
82   if(font.family().isEmpty())
83     font = QApplication::font();
84
85   ui.topicLineEdit->setCustomFont(font);
86   ui.topicLabel->setCustomFont(font);
87 }
88
89 void TopicWidget::setTopic(const QString &newtopic) {
90   if(_topic == newtopic)
91     return;
92
93   _topic = newtopic;
94   ui.topicLabel->setText(newtopic);
95   ui.topicLineEdit->setText(newtopic);
96   switchPlain();
97 }
98
99 void TopicWidget::updateResizeMode() {
100   StyledLabel::ResizeMode mode = StyledLabel::NoResize;
101   UiSettings s("TopicWidget");
102   if(s.value("DynamicResize", true).toBool()) {
103     if(s.value("ResizeOnHover", true).toBool())
104       mode = StyledLabel::ResizeOnHover;
105     else
106       mode = StyledLabel::DynamicResize;
107   }
108
109   ui.topicLabel->setResizeMode(mode);
110 }
111
112 void TopicWidget::clickableActivated(const Clickable &click) {
113   NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
114   click.activate(networkId, _topic);
115 }
116
117 void TopicWidget::on_topicLineEdit_textEntered() {
118   QModelIndex currentIdx = currentIndex();
119   if(currentIdx.isValid() && currentIdx.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
120     BufferInfo bufferInfo = currentIdx.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
121     if(ui.topicLineEdit->text().isEmpty())
122       Client::userInput(bufferInfo, QString("/quote TOPIC %1 :").arg(bufferInfo.bufferName()));
123     else
124       Client::userInput(bufferInfo, QString("/topic %1").arg(ui.topicLineEdit->text()));
125   }
126   switchPlain();
127 }
128
129 void TopicWidget::on_topicEditButton_clicked() {
130   switchEditable();
131 }
132
133 void TopicWidget::switchEditable() {
134   ui.stackedWidget->setCurrentIndex(1);
135   ui.topicLineEdit->setFocus();
136   updateGeometry();
137 }
138
139 void TopicWidget::switchPlain() {
140   ui.stackedWidget->setCurrentIndex(0);
141   ui.topicLineEdit->setText(_topic);
142   updateGeometry();
143 }
144
145 // filter for the input widget to switch back to normal mode
146 bool TopicWidget::eventFilter(QObject *obj, QEvent *event) {
147   if(event->type() == QEvent::FocusOut) {
148     switchPlain();
149     return true;
150   }
151
152   if(event->type() != QEvent::KeyPress)
153     return QObject::eventFilter(obj, event);
154
155   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
156
157   if(keyEvent->key() == Qt::Key_Escape) {
158     switchPlain();
159     return true;
160   }
161
162   return false;
163 }