Add SslInfoDlg as a nice way to show information about an SSL connection
[quassel.git] / src / qtui / bufferwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 <QLayout>
22 #include <QKeyEvent>
23 #include <QMenu>
24 #include <QScrollBar>
25
26 #include "action.h"
27 #include "actioncollection.h"
28 #include "bufferwidget.h"
29 #include "chatview.h"
30 #include "chatviewsearchbar.h"
31 #include "chatviewsearchcontroller.h"
32 #include "client.h"
33 #include "iconloader.h"
34 #include "multilineedit.h"
35 #include "qtui.h"
36 #include "settings.h"
37
38
39 BufferWidget::BufferWidget(QWidget *parent)
40   : AbstractBufferContainer(parent),
41     _chatViewSearchController(new ChatViewSearchController(this))
42 {
43   ui.setupUi(this);
44   layout()->setContentsMargins(0, 0, 0, 0);
45   layout()->setSpacing(0);
46   // ui.searchBar->hide();
47
48   _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
49   _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
50   _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
51   _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
52
53   connect(ui.searchBar, SIGNAL(searchChanged(const QString &)),
54           _chatViewSearchController, SLOT(setSearchString(const QString &)));
55   connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
56           _chatViewSearchController, SLOT(setCaseSensitive(bool)));
57   connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
58           _chatViewSearchController, SLOT(setSearchSenders(bool)));
59   connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
60           _chatViewSearchController, SLOT(setSearchMsgs(bool)));
61   connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
62           _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
63   connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
64           _chatViewSearchController, SLOT(highlightPrev()));
65   connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
66           _chatViewSearchController, SLOT(highlightNext()));
67
68   connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
69           this, SLOT(scrollToHighlight(QGraphicsItem *)));
70
71   ActionCollection *coll = QtUi::actionCollection();
72
73   Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
74   zoomInChatview->setText(tr("Zoom In"));
75   zoomInChatview->setIcon(SmallIcon("zoom-in"));
76   zoomInChatview->setShortcut(QKeySequence::ZoomIn);
77
78   Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
79   zoomOutChatview->setIcon(SmallIcon("zoom-out"));
80   zoomOutChatview->setText(tr("Zoom Out"));
81   zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
82
83   Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
84   zoomOriginalChatview->setIcon(SmallIcon("zoom-original"));
85   zoomOriginalChatview->setText(tr("Actual Size"));
86   //zoomOriginalChatview->setShortcut(tr("Ctrl+0")); // used for RTS switching
87 }
88
89 BufferWidget::~BufferWidget() {
90   delete _chatViewSearchController;
91   _chatViewSearchController = 0;
92 }
93
94 AbstractChatView *BufferWidget::createChatView(BufferId id) {
95   ChatView *chatView;
96   chatView = new ChatView(id, this);
97   chatView->setBufferContainer(this);
98   _chatViews[id] = chatView;
99   ui.stackedWidget->addWidget(chatView);
100   chatView->setFocusProxy(this);
101   return chatView;
102 }
103
104 void BufferWidget::removeChatView(BufferId id) {
105   QWidget *view = _chatViews.value(id, 0);
106   if(!view) return;
107   ui.stackedWidget->removeWidget(view);
108   view->deleteLater();
109   _chatViews.take(id);
110 }
111
112 void BufferWidget::showChatView(BufferId id) {
113   if(!id.isValid()) {
114     ui.stackedWidget->setCurrentWidget(ui.page);
115   } else {
116     ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
117     Q_ASSERT(view);
118     ui.stackedWidget->setCurrentWidget(view);
119     _chatViewSearchController->setScene(view->scene());
120   }
121 }
122
123 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
124   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
125   if(view) {
126     view->centerOn(highlightItem);
127   }
128 }
129
130
131 void BufferWidget::zoomIn() {
132   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
133   if(view)
134     view->zoomIn();
135 }
136
137 void BufferWidget::zoomOut() {
138   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
139   if(view)
140     view->zoomOut();
141 }
142
143 void BufferWidget::zoomOriginal() {
144   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
145   if(view)
146     view->zoomOriginal();
147 }
148
149 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos) {
150   Q_UNUSED(pos);
151   ActionCollection *coll = QtUi::actionCollection();
152   menu->addSeparator();
153   menu->addAction(coll->action("ZoomInChatView"));
154   menu->addAction(coll->action("ZoomOutChatView"));
155   menu->addAction(coll->action("ZoomOriginalChatView"));
156 }
157
158 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
159   if(event->type() != QEvent::KeyPress)
160     return false;
161
162   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
163
164   MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
165   if(!inputLine)
166     return false;
167
168   // Intercept copy key presses
169   if(keyEvent == QKeySequence::Copy) {
170     if(inputLine->hasSelectedText())
171       return false;
172     ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
173     if(view)
174       view->scene()->selectionToClipboard();
175     return true;
176   }
177
178   // We don't want to steal cursor movement keys if the input line is in multiline mode
179   if(!inputLine->isSingleLine())
180     return false;
181
182   switch(keyEvent->key()) {
183   case Qt::Key_Up:
184   case Qt::Key_Down:
185     if(!(keyEvent->modifiers() & Qt::ShiftModifier))
186       return false;
187   case Qt::Key_PageUp:
188   case Qt::Key_PageDown:
189     // static cast to access public qobject::event
190     return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
191   default:
192     return false;
193   }
194 }