1 /***************************************************************************
2 * Copyright (C) 2005-2019 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"
32 #include "chatviewsearchbar.h"
33 #include "chatviewsearchcontroller.h"
34 #include "chatviewsettings.h"
37 #include "multilineedit.h"
41 BufferWidget::BufferWidget(QWidget* parent)
42 : AbstractBufferContainer(parent)
43 , _chatViewSearchController(new ChatViewSearchController(this))
44 , _autoMarkerLine(true)
45 , _autoMarkerLineOnLostFocus(true)
48 layout()->setContentsMargins(0, 0, 0, 0);
49 layout()->setSpacing(0);
50 // ui.searchBar->hide();
52 _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
53 _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
54 _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
55 _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
57 connect(ui.searchBar, &ChatViewSearchBar::searchChanged, _chatViewSearchController, &ChatViewSearchController::setSearchString);
58 connect(ui.searchBar->caseSensitiveBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setCaseSensitive);
59 connect(ui.searchBar->searchSendersBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setSearchSenders);
60 connect(ui.searchBar->searchMsgsBox(), &QAbstractButton::toggled, _chatViewSearchController, &ChatViewSearchController::setSearchMsgs);
61 connect(ui.searchBar->searchOnlyRegularMsgsBox(),
62 &QAbstractButton::toggled,
63 _chatViewSearchController,
64 &ChatViewSearchController::setSearchOnlyRegularMsgs);
65 connect(ui.searchBar->searchUpButton(), &QAbstractButton::clicked, _chatViewSearchController, &ChatViewSearchController::highlightPrev);
66 connect(ui.searchBar->searchDownButton(), &QAbstractButton::clicked, _chatViewSearchController, &ChatViewSearchController::highlightNext);
68 connect(ui.searchBar, &ChatViewSearchBar::hidden, this, selectOverload<>(&QWidget::setFocus));
70 connect(_chatViewSearchController, &ChatViewSearchController::newCurrentHighlight, this, &BufferWidget::scrollToHighlight);
72 ActionCollection* coll = QtUi::actionCollection();
74 {{"ZoomInChatView", new Action{icon::get("zoom-in"), tr("Zoom In"), coll, this, &BufferWidget::zoomIn, QKeySequence::ZoomIn}},
75 {"ZoomOutChatView", new Action{icon::get("zoom-out"), tr("Zoom Out"), coll, this, &BufferWidget::zoomOut, QKeySequence::ZoomOut}},
76 {"ZoomOriginalChatView", new Action{icon::get("zoom-original"), tr("Actual Size"), coll, this, &BufferWidget::zoomOriginal}},
77 {"SetMarkerLineToBottom",
78 new Action{tr("Set Marker Line"), coll, this, [this]() { setMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_R)}}});
79 coll = QtUi::actionCollection("Navigation");
80 coll->addAction("JumpToMarkerLine",
81 new Action{tr("Go to Marker Line"), coll, this, [this]() { jumpToMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_K)});
84 s.initAndNotify("AutoMarkerLine", this, &BufferWidget::setAutoMarkerLine, true);
85 s.initAndNotify("AutoMarkerLineOnLostFocus", this, &BufferWidget::setAutoMarkerLineOnLostFocus, true);
88 BufferWidget::~BufferWidget()
90 delete _chatViewSearchController;
91 _chatViewSearchController = nullptr;
94 void BufferWidget::setAutoMarkerLine(const QVariant& v)
96 _autoMarkerLine = v.toBool();
99 void BufferWidget::setAutoMarkerLineOnLostFocus(const QVariant& v)
101 _autoMarkerLineOnLostFocus = v.toBool();
104 AbstractChatView* BufferWidget::createChatView(BufferId id)
107 chatView = new ChatView(id, this);
108 chatView->setBufferContainer(this);
109 _chatViews[id] = chatView;
110 ui.stackedWidget->addWidget(chatView);
111 chatView->setFocusProxy(this);
115 void BufferWidget::removeChatView(BufferId id)
117 QWidget* view = _chatViews.value(id, 0);
120 ui.stackedWidget->removeWidget(view);
125 void BufferWidget::showChatView(BufferId id)
128 ui.stackedWidget->setCurrentWidget(ui.page);
131 auto* view = qobject_cast<ChatView*>(_chatViews.value(id));
133 ui.stackedWidget->setCurrentWidget(view);
134 _chatViewSearchController->setScene(view->scene());
138 void BufferWidget::scrollToHighlight(QGraphicsItem* highlightItem)
140 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
142 view->centerOn(highlightItem);
146 void BufferWidget::zoomIn()
148 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
153 void BufferWidget::zoomOut()
155 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
160 void BufferWidget::zoomOriginal()
162 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
164 view->zoomOriginal();
167 void BufferWidget::addActionsToMenu(QMenu* menu, const QPointF& 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"));
177 bool BufferWidget::eventFilter(QObject* watched, QEvent* event)
179 if (event->type() != QEvent::KeyPress)
182 auto* keyEvent = static_cast<QKeyEvent*>(event);
184 auto* inputLine = qobject_cast<MultiLineEdit*>(watched);
188 // Intercept copy key presses
189 if (keyEvent == QKeySequence::Copy) {
190 if (inputLine->hasSelectedText())
192 auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
194 view->scene()->selectionToClipboard();
198 // We don't want to steal cursor movement keys if the input line is in multiline mode
199 if (!inputLine->isSingleLine())
202 switch (keyEvent->key()) {
205 if (!(keyEvent->modifiers() & Qt::ShiftModifier))
209 case Qt::Key_PageDown:
210 // static cast to access public qobject::event
211 return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
217 void BufferWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
219 auto* prevView = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
221 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
223 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
224 auto* curView = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
226 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
227 if (curBufferId.isValid()) {
228 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
229 if (markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
230 curView->setMarkerLineVisible(false);
232 curView->setMarkerLineVisible(true);
236 if (prevView && autoMarkerLine())
237 setMarkerLine(prevView, false);
240 void BufferWidget::setMarkerLine(ChatView* view, bool allowGoingBack)
243 view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
247 ChatLine* lastLine = view->lastVisibleChatLine();
249 QModelIndex idx = lastLine->index();
250 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
251 BufferId bufId = view->scene()->singleBufferId();
253 if (!allowGoingBack) {
254 MsgId oldMsgId = Client::markerLine(bufId);
255 if (oldMsgId.isValid() && msgId <= oldMsgId)
258 Client::setMarkerLine(bufId, msgId);
262 void BufferWidget::jumpToMarkerLine(ChatView* view, bool requestBacklog)
265 view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
269 view->jumpToMarkerLine(requestBacklog);