1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
27 #include "actioncollection.h"
28 #include "bufferwidget.h"
31 #include "chatviewsearchbar.h"
32 #include "chatviewsearchcontroller.h"
33 #include "chatviewsettings.h"
35 #include "iconloader.h"
36 #include "multilineedit.h"
41 # include "qmlchatview.h"
44 BufferWidget::BufferWidget(QWidget *parent)
45 : AbstractBufferContainer(parent),
46 _chatViewSearchController(new ChatViewSearchController(this)),
50 layout()->setContentsMargins(0, 0, 0, 0);
51 layout()->setSpacing(0);
52 // ui.searchBar->hide();
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());
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()));
74 connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
76 connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
77 this, SLOT(scrollToHighlight(QGraphicsItem *)));
79 ActionCollection *coll = QtUi::actionCollection();
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);
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);
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
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));
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));
105 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
108 BufferWidget::~BufferWidget() {
109 delete _chatViewSearchController;
110 _chatViewSearchController = 0;
113 void BufferWidget::setAutoMarkerLine(const QVariant &v) {
114 _autoMarkerLine = v.toBool();
117 AbstractChatView *BufferWidget::createChatView(BufferId id) {
119 QmlChatView *chatView;
120 chatView = new QmlChatView(id, this);
123 chatView = new ChatView(id, this);
125 chatView->setBufferContainer(this);
126 _chatViews[id] = chatView;
127 ui.stackedWidget->addWidget(chatView);
128 chatView->setFocusProxy(this);
132 void BufferWidget::removeChatView(BufferId id) {
133 QWidget *view = _chatViews.value(id, 0);
135 ui.stackedWidget->removeWidget(view);
140 void BufferWidget::showChatView(BufferId id) {
142 ui.stackedWidget->setCurrentWidget(ui.page);
145 QmlChatView *view = qobject_cast<QmlChatView *>(_chatViews.value(id));
147 ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
150 ui.stackedWidget->setCurrentWidget(view);
151 //_chatViewSearchController->setScene(view->scene());
155 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
156 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
158 view->centerOn(highlightItem);
162 void BufferWidget::zoomIn() {
163 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
168 void BufferWidget::zoomOut() {
169 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
174 void BufferWidget::zoomOriginal() {
175 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
177 view->zoomOriginal();
180 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &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"));
189 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
190 if(event->type() != QEvent::KeyPress)
193 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
195 MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
199 // Intercept copy key presses
200 if(keyEvent == QKeySequence::Copy) {
201 if(inputLine->hasSelectedText())
203 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
205 view->scene()->selectionToClipboard();
209 // We don't want to steal cursor movement keys if the input line is in multiline mode
210 if(!inputLine->isSingleLine())
213 switch(keyEvent->key()) {
216 if(!(keyEvent->modifiers() & Qt::ShiftModifier))
219 case Qt::Key_PageDown:
220 // static cast to access public qobject::event
221 return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
227 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
228 ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
230 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
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());
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);
241 curView->setMarkerLineVisible(true);
245 if(prevView && autoMarkerLine())
246 setMarkerLine(prevView, false);
249 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack) {
251 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
255 ChatLine *lastLine = view->lastVisibleChatLine();
257 QModelIndex idx = lastLine->index();
258 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
260 if(!allowGoingBack) {
261 BufferId bufId = view->scene()->singleBufferId();
262 MsgId oldMsgId = Client::markerLine(bufId);
263 if(oldMsgId.isValid() && msgId <= oldMsgId)
266 view->setMarkerLine(msgId);
270 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog) {
272 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
276 view->jumpToMarkerLine(requestBacklog);