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