introduce fullscreen mode, fixes #803
[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 "chatline.h"
30 #include "chatview.h"
31 #include "chatviewsearchbar.h"
32 #include "chatviewsearchcontroller.h"
33 #include "chatviewsettings.h"
34 #include "client.h"
35 #include "iconloader.h"
36 #include "multilineedit.h"
37 #include "qtui.h"
38 #include "settings.h"
39
40
41 BufferWidget::BufferWidget(QWidget *parent)
42   : AbstractBufferContainer(parent),
43     _chatViewSearchController(new ChatViewSearchController(this)),
44     _autoMarkerLine(true)
45 {
46   ui.setupUi(this);
47   layout()->setContentsMargins(0, 0, 0, 0);
48   layout()->setSpacing(0);
49   // ui.searchBar->hide();
50
51   _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
52   _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
53   _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
54   _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
55
56   connect(ui.searchBar, SIGNAL(searchChanged(const QString &)),
57     _chatViewSearchController, SLOT(setSearchString(const QString &)));
58   connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
59     _chatViewSearchController, SLOT(setCaseSensitive(bool)));
60   connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
61     _chatViewSearchController, SLOT(setSearchSenders(bool)));
62   connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
63     _chatViewSearchController, SLOT(setSearchMsgs(bool)));
64   connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
65     _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
66   connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
67     _chatViewSearchController, SLOT(highlightPrev()));
68   connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
69     _chatViewSearchController, SLOT(highlightNext()));
70
71   connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
72
73   connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
74     this, SLOT(scrollToHighlight(QGraphicsItem *)));
75
76   ActionCollection *coll = QtUi::actionCollection();
77
78   Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
79   zoomInChatview->setText(tr("Zoom In"));
80   zoomInChatview->setIcon(SmallIcon("zoom-in"));
81   zoomInChatview->setShortcut(QKeySequence::ZoomIn);
82
83   Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
84   zoomOutChatview->setIcon(SmallIcon("zoom-out"));
85   zoomOutChatview->setText(tr("Zoom Out"));
86   zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
87
88   Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
89   zoomOriginalChatview->setIcon(SmallIcon("zoom-original"));
90   zoomOriginalChatview->setText(tr("Actual Size"));
91   //zoomOriginalChatview->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); // used for RTS switching
92
93   Action *setMarkerLine = coll->add<Action>("SetMarkerLineToBottom", this, SLOT(setMarkerLine()));
94   setMarkerLine->setText(tr("Set Marker Line"));
95   setMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
96
97   Action *jumpToMarkerLine = QtUi::actionCollection("Navigation")->add<Action>("JumpToMarkerLine", this, SLOT(jumpToMarkerLine()));
98   jumpToMarkerLine->setText(tr("Go to Marker Line"));
99   jumpToMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
100
101   ChatViewSettings s;
102   s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
103 }
104
105 BufferWidget::~BufferWidget() {
106   delete _chatViewSearchController;
107   _chatViewSearchController = 0;
108 }
109
110 void BufferWidget::setAutoMarkerLine(const QVariant &v) {
111   _autoMarkerLine = v.toBool();
112 }
113
114 AbstractChatView *BufferWidget::createChatView(BufferId id) {
115   ChatView *chatView;
116   chatView = new ChatView(id, this);
117   chatView->setBufferContainer(this);
118   _chatViews[id] = chatView;
119   ui.stackedWidget->addWidget(chatView);
120   chatView->setFocusProxy(this);
121   return chatView;
122 }
123
124 void BufferWidget::removeChatView(BufferId id) {
125   QWidget *view = _chatViews.value(id, 0);
126   if(!view) return;
127   ui.stackedWidget->removeWidget(view);
128   view->deleteLater();
129   _chatViews.take(id);
130 }
131
132 void BufferWidget::showChatView(BufferId id) {
133   if(!id.isValid()) {
134     ui.stackedWidget->setCurrentWidget(ui.page);
135   } else {
136     ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
137     Q_ASSERT(view);
138     ui.stackedWidget->setCurrentWidget(view);
139     _chatViewSearchController->setScene(view->scene());
140   }
141 }
142
143 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
144   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
145   if(view) {
146     view->centerOn(highlightItem);
147   }
148 }
149
150 void BufferWidget::zoomIn() {
151   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
152   if(view)
153     view->zoomIn();
154 }
155
156 void BufferWidget::zoomOut() {
157   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
158   if(view)
159     view->zoomOut();
160 }
161
162 void BufferWidget::zoomOriginal() {
163   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
164   if(view)
165     view->zoomOriginal();
166 }
167
168 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos) {
169   Q_UNUSED(pos);
170   ActionCollection *coll = QtUi::actionCollection();
171   menu->addSeparator();
172   menu->addAction(coll->action("ZoomInChatView"));
173   menu->addAction(coll->action("ZoomOutChatView"));
174   menu->addAction(coll->action("ZoomOriginalChatView"));
175 }
176
177 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
178   if(event->type() != QEvent::KeyPress)
179     return false;
180
181   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
182
183   MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
184   if(!inputLine)
185     return false;
186
187   // Intercept copy key presses
188   if(keyEvent == QKeySequence::Copy) {
189     if(inputLine->hasSelectedText())
190       return false;
191     ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
192     if(view)
193       view->scene()->selectionToClipboard();
194     return true;
195   }
196
197   // We don't want to steal cursor movement keys if the input line is in multiline mode
198   if(!inputLine->isSingleLine())
199     return false;
200
201   switch(keyEvent->key()) {
202   case Qt::Key_Up:
203   case Qt::Key_Down:
204     if(!(keyEvent->modifiers() & Qt::ShiftModifier))
205       return false;
206   case Qt::Key_PageUp:
207   case Qt::Key_PageDown:
208     // static cast to access public qobject::event
209     return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
210   default:
211     return false;
212   }
213 }
214
215 void BufferWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
216   ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
217
218   AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
219
220   // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
221   ChatView *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
222   if(curView) {
223     BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
224     if(curBufferId.isValid()) {
225       MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
226       if(markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
227         curView->setMarkerLineVisible(false);
228       else
229         curView->setMarkerLineVisible(true);
230     }
231   }
232
233   if(prevView && autoMarkerLine())
234     setMarkerLine(prevView, false);
235 }
236
237 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack) {
238   if(!view)
239     view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
240   if(!view)
241     return;
242
243   ChatLine *lastLine = view->lastVisibleChatLine();
244   if(lastLine) {
245     QModelIndex idx = lastLine->index();
246     MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
247
248     if(!allowGoingBack) {
249       BufferId bufId = view->scene()->singleBufferId();
250       MsgId oldMsgId = Client::markerLine(bufId);
251       if(oldMsgId.isValid() && msgId <= oldMsgId)
252         return;
253     }
254     view->setMarkerLine(msgId);
255   }
256 }
257
258 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog) {
259   if(!view)
260     view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
261   if(!view)
262     return;
263
264   view->jumpToMarkerLine(requestBacklog);
265 }