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