1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
27 #include "actioncollection.h"
28 #include "bufferwidget.h"
31 #include "chatviewsearchbar.h"
32 #include "chatviewsearchcontroller.h"
33 #include "chatviewsettings.h"
36 #include "multilineedit.h"
40 BufferWidget::BufferWidget(QWidget *parent)
41 : AbstractBufferContainer(parent),
42 _chatViewSearchController(new ChatViewSearchController(this)),
43 _autoMarkerLine(true),
44 _autoMarkerLineOnLostFocus(true)
47 layout()->setContentsMargins(0, 0, 0, 0);
48 layout()->setSpacing(0);
49 // ui.searchBar->hide();
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());
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()));
71 connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
73 connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
74 this, SLOT(scrollToHighlight(QGraphicsItem *)));
76 ActionCollection *coll = QtUi::actionCollection();
78 Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
79 zoomInChatview->setText(tr("Zoom In"));
80 zoomInChatview->setIcon(icon::get("zoom-in"));
81 zoomInChatview->setShortcut(QKeySequence::ZoomIn);
83 Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
84 zoomOutChatview->setIcon(icon::get("zoom-out"));
85 zoomOutChatview->setText(tr("Zoom Out"));
86 zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
88 Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
89 zoomOriginalChatview->setIcon(icon::get("zoom-original"));
90 zoomOriginalChatview->setText(tr("Actual Size"));
91 //zoomOriginalChatview->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); // used for RTS switching
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));
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));
102 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
103 s.initAndNotify("AutoMarkerLineOnLostFocus", this, SLOT(setAutoMarkerLineOnLostFocus(QVariant)), true);
107 BufferWidget::~BufferWidget()
109 delete _chatViewSearchController;
110 _chatViewSearchController = 0;
114 void BufferWidget::setAutoMarkerLine(const QVariant &v)
116 _autoMarkerLine = v.toBool();
119 void BufferWidget::setAutoMarkerLineOnLostFocus(const QVariant &v)
121 _autoMarkerLineOnLostFocus = v.toBool();
125 AbstractChatView *BufferWidget::createChatView(BufferId id)
128 chatView = new ChatView(id, this);
129 chatView->setBufferContainer(this);
130 _chatViews[id] = chatView;
131 ui.stackedWidget->addWidget(chatView);
132 chatView->setFocusProxy(this);
137 void BufferWidget::removeChatView(BufferId id)
139 QWidget *view = _chatViews.value(id, 0);
141 ui.stackedWidget->removeWidget(view);
147 void BufferWidget::showChatView(BufferId id)
150 ui.stackedWidget->setCurrentWidget(ui.page);
153 ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
155 ui.stackedWidget->setCurrentWidget(view);
156 _chatViewSearchController->setScene(view->scene());
161 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem)
163 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
165 view->centerOn(highlightItem);
170 void BufferWidget::zoomIn()
172 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
178 void BufferWidget::zoomOut()
180 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
186 void BufferWidget::zoomOriginal()
188 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
190 view->zoomOriginal();
194 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos)
197 ActionCollection *coll = QtUi::actionCollection();
198 menu->addSeparator();
199 menu->addAction(coll->action("ZoomInChatView"));
200 menu->addAction(coll->action("ZoomOutChatView"));
201 menu->addAction(coll->action("ZoomOriginalChatView"));
205 bool BufferWidget::eventFilter(QObject *watched, QEvent *event)
207 if (event->type() != QEvent::KeyPress)
210 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
212 MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
216 // Intercept copy key presses
217 if (keyEvent == QKeySequence::Copy) {
218 if (inputLine->hasSelectedText())
220 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
222 view->scene()->selectionToClipboard();
226 // We don't want to steal cursor movement keys if the input line is in multiline mode
227 if (!inputLine->isSingleLine())
230 switch (keyEvent->key()) {
233 if (!(keyEvent->modifiers() & Qt::ShiftModifier))
237 case Qt::Key_PageDown:
238 // static cast to access public qobject::event
239 return static_cast<QObject *>(ui.stackedWidget->currentWidget())->event(event);
246 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
248 ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
250 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
252 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
253 ChatView *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
255 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
256 if (curBufferId.isValid()) {
257 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
258 if (markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
259 curView->setMarkerLineVisible(false);
261 curView->setMarkerLineVisible(true);
265 if (prevView && autoMarkerLine())
266 setMarkerLine(prevView, false);
270 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack)
273 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
277 ChatLine *lastLine = view->lastVisibleChatLine();
279 QModelIndex idx = lastLine->index();
280 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
281 BufferId bufId = view->scene()->singleBufferId();
283 if (!allowGoingBack) {
284 MsgId oldMsgId = Client::markerLine(bufId);
285 if (oldMsgId.isValid() && msgId <= oldMsgId)
288 Client::setMarkerLine(bufId, msgId);
293 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog)
296 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
300 view->jumpToMarkerLine(requestBacklog);