1 /***************************************************************************
2 * Copyright (C) 2005-2022 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 ***************************************************************************/
21 #include "bufferwidget.h"
29 #include "actioncollection.h"
30 #include "backlogsettings.h"
33 #include "chatviewsearchbar.h"
34 #include "chatviewsearchcontroller.h"
35 #include "chatviewsettings.h"
38 #include "multilineedit.h"
42 BufferWidget::BufferWidget(QWidget* parent)
43 : AbstractBufferContainer(parent)
44 , _chatViewSearchController(new ChatViewSearchController(this))
45 , _autoMarkerLine(true)
46 , _autoMarkerLineOnLostFocus(true)
49 layout()->setContentsMargins(0, 0, 0, 0);
50 layout()->setSpacing(0);
51 // ui.searchBar->hide();
53 _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
54 _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
55 _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
56 _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
58 connect(ui.searchBar, &ChatViewSearchBar::searchChanged, _chatViewSearchController, &ChatViewSearchController::setSearchString);
59 connect(ui.searchBar->caseSensitiveBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setCaseSensitive);
60 connect(ui.searchBar->searchSendersBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setSearchSenders);
61 connect(ui.searchBar->searchMsgsBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setSearchMsgs);
62 connect(ui.searchBar->searchOnlyRegularMsgsBox(),
63 &QAbstractButton::toggled,
64 _chatViewSearchController,
65 &ChatViewSearchController::setSearchOnlyRegularMsgs);
66 connect(ui.searchBar->searchUpButton(), &QAbstractButton::clicked, _chatViewSearchController, &ChatViewSearchController::highlightPrev);
67 connect(ui.searchBar->searchDownButton(), &QAbstractButton::clicked, _chatViewSearchController, &ChatViewSearchController::highlightNext);
69 connect(ui.searchBar, &ChatViewSearchBar::hidden, this, selectOverload<>(&QWidget::setFocus));
71 connect(_chatViewSearchController, &ChatViewSearchController::newCurrentHighlight, this, &BufferWidget::scrollToHighlight);
73 ActionCollection* coll = QtUi::actionCollection();
75 {{"ZoomInChatView", new Action{icon::get("zoom-in"), tr("Zoom In"), coll, this, &BufferWidget::zoomIn, QKeySequence::ZoomIn}},
76 {"ZoomOutChatView", new Action{icon::get("zoom-out"), tr("Zoom Out"), coll, this, &BufferWidget::zoomOut, QKeySequence::ZoomOut}},
77 {"ZoomOriginalChatView", new Action{icon::get("zoom-original"), tr("Actual Size"), coll, this, &BufferWidget::zoomOriginal}},
78 {"SetMarkerLineToBottom",
79 new Action{tr("Set Marker Line"), coll, this, [this]() { setMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_R)}}});
80 coll = QtUi::actionCollection("Navigation");
81 coll->addAction("JumpToMarkerLine",
82 new Action{tr("Go to Marker Line"), coll, this, [this]() { jumpToMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_K)});
85 s.initAndNotify("AutoMarkerLine", this, &BufferWidget::setAutoMarkerLine, true);
86 s.initAndNotify("AutoMarkerLineOnLostFocus", this, &BufferWidget::setAutoMarkerLineOnLostFocus, true);
88 BacklogSettings backlogSettings;
89 backlogSettings.initAndNotify("EnsureBacklogOnBufferShow", this, &BufferWidget::setEnsureBacklogOnBufferShow, true);
92 BufferWidget::~BufferWidget()
94 delete _chatViewSearchController;
95 _chatViewSearchController = nullptr;
98 void BufferWidget::setAutoMarkerLine(const QVariant& v)
100 _autoMarkerLine = v.toBool();
103 void BufferWidget::setAutoMarkerLineOnLostFocus(const QVariant& v)
105 _autoMarkerLineOnLostFocus = v.toBool();
108 void BufferWidget::setEnsureBacklogOnBufferShow(const QVariant& v)
110 _ensureBacklogOnBufferShow = v.toBool();
113 AbstractChatView* BufferWidget::createChatView(BufferId id)
116 chatView = new ChatView(id, this);
117 chatView->setBufferContainer(this);
118 _chatViews[id] = chatView;
119 ui.stackedWidget->addWidget(chatView);
120 chatView->setFocusProxy(this);
124 void BufferWidget::removeChatView(BufferId id)
126 QWidget* view = _chatViews.value(id, 0);
129 ui.stackedWidget->removeWidget(view);
134 void BufferWidget::showChatView(BufferId id)
137 ui.stackedWidget->setCurrentWidget(ui.page);
140 auto* view = qobject_cast<ChatView*>(_chatViews.value(id));
142 ui.stackedWidget->setCurrentWidget(view);
143 _chatViewSearchController->setScene(view->scene());
144 if (_ensureBacklogOnBufferShow) {
145 // Try to ensure some messages are visible
146 view->requestBacklogForScroll();
151 void BufferWidget::scrollToHighlight(QGraphicsItem* highlightItem)
153 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
155 view->centerOn(highlightItem);
159 void BufferWidget::zoomIn()
161 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
166 void BufferWidget::zoomOut()
168 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
173 void BufferWidget::zoomOriginal()
175 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
177 view->zoomOriginal();
180 void BufferWidget::addActionsToMenu(QMenu* menu, const QPointF& pos)
183 ActionCollection* coll = QtUi::actionCollection();
184 menu->addSeparator();
185 menu->addAction(coll->action("ZoomInChatView"));
186 menu->addAction(coll->action("ZoomOutChatView"));
187 menu->addAction(coll->action("ZoomOriginalChatView"));
190 bool BufferWidget::eventFilter(QObject* watched, QEvent* event)
192 if (event->type() != QEvent::KeyPress)
195 auto* keyEvent = static_cast<QKeyEvent*>(event);
197 auto* inputLine = qobject_cast<MultiLineEdit*>(watched);
201 // Intercept copy key presses
202 if (keyEvent == QKeySequence::Copy) {
203 if (inputLine->hasSelectedText())
205 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
207 view->scene()->selectionToClipboard();
211 // We don't want to steal cursor movement keys if the input line is in multiline mode
212 if (!inputLine->isSingleLine())
215 switch (keyEvent->key()) {
218 if (!(keyEvent->modifiers() & Qt::ShiftModifier))
222 case Qt::Key_PageDown:
223 // static cast to access public qobject::event
224 return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
230 void BufferWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
232 auto* prevView = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
234 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
236 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
237 auto* curView = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
239 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
240 if (curBufferId.isValid()) {
241 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
242 if (markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
243 curView->setMarkerLineVisible(false);
245 curView->setMarkerLineVisible(true);
249 if (prevView && autoMarkerLine())
250 setMarkerLine(prevView, false);
253 void BufferWidget::setMarkerLine(ChatView* view, bool allowGoingBack)
256 view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
260 ChatLine* lastLine = view->lastVisibleChatLine();
262 QModelIndex idx = lastLine->index();
263 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
264 BufferId bufId = view->scene()->singleBufferId();
266 if (!allowGoingBack) {
267 MsgId oldMsgId = Client::markerLine(bufId);
268 if (oldMsgId.isValid() && msgId <= oldMsgId)
271 Client::setMarkerLine(bufId, msgId);
275 void BufferWidget::jumpToMarkerLine(ChatView* view, bool requestBacklog)
278 view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
282 view->jumpToMarkerLine(requestBacklog);