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, &ChatViewSearchBar::searchChanged,
57 _chatViewSearchController, &ChatViewSearchController::setSearchString);
58 connect(ui.searchBar->caseSensitiveBox(), &QAbstractButton::toggled,
59 _chatViewSearchController, &ChatViewSearchController::setCaseSensitive);
60 connect(ui.searchBar->searchSendersBox(), &QAbstractButton::toggled,
61 _chatViewSearchController, &ChatViewSearchController::setSearchSenders);
62 connect(ui.searchBar->searchMsgsBox(), &QAbstractButton::toggled,
63 _chatViewSearchController, &ChatViewSearchController::setSearchMsgs);
64 connect(ui.searchBar->searchOnlyRegularMsgsBox(), &QAbstractButton::toggled,
65 _chatViewSearchController, &ChatViewSearchController::setSearchOnlyRegularMsgs);
66 connect(ui.searchBar->searchUpButton(), &QAbstractButton::clicked,
67 _chatViewSearchController, &ChatViewSearchController::highlightPrev);
68 connect(ui.searchBar->searchDownButton(), &QAbstractButton::clicked,
69 _chatViewSearchController, &ChatViewSearchController::highlightNext);
71 connect(ui.searchBar, &ChatViewSearchBar::hidden, this, selectOverload<>(&QWidget::setFocus));
73 connect(_chatViewSearchController, &ChatViewSearchController::newCurrentHighlight,
74 this, &BufferWidget::scrollToHighlight);
76 ActionCollection *coll = QtUi::actionCollection();
78 {"ZoomInChatView", new Action{icon::get("zoom-in"), tr("Zoom In"), coll, this, &BufferWidget::zoomIn, QKeySequence::ZoomIn}},
79 {"ZoomOutChatView", new Action{icon::get("zoom-out"), tr("Zoom Out"), coll, this, &BufferWidget::zoomOut, QKeySequence::ZoomOut}},
80 {"ZoomOriginalChatView", new Action{icon::get("zoom-original"), tr("Actual Size"), coll, this, &BufferWidget::zoomOriginal}},
81 {"SetMarkerLineToBottom", new Action{tr("Set Marker Line"), coll, this, [this]() { setMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_R)}}
83 coll = QtUi::actionCollection("Navigation");
84 coll->addAction("JumpToMarkerLine", new Action{tr("Go to Marker Line"), coll, this, [this]() { jumpToMarkerLine(); }, QKeySequence(Qt::CTRL + Qt::Key_K)});
87 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
88 s.initAndNotify("AutoMarkerLineOnLostFocus", this, SLOT(setAutoMarkerLineOnLostFocus(QVariant)), true);
92 BufferWidget::~BufferWidget()
94 delete _chatViewSearchController;
95 _chatViewSearchController = nullptr;
99 void BufferWidget::setAutoMarkerLine(const QVariant &v)
101 _autoMarkerLine = v.toBool();
104 void BufferWidget::setAutoMarkerLineOnLostFocus(const QVariant &v)
106 _autoMarkerLineOnLostFocus = v.toBool();
110 AbstractChatView *BufferWidget::createChatView(BufferId id)
113 chatView = new ChatView(id, this);
114 chatView->setBufferContainer(this);
115 _chatViews[id] = chatView;
116 ui.stackedWidget->addWidget(chatView);
117 chatView->setFocusProxy(this);
122 void BufferWidget::removeChatView(BufferId id)
124 QWidget *view = _chatViews.value(id, 0);
126 ui.stackedWidget->removeWidget(view);
132 void BufferWidget::showChatView(BufferId id)
135 ui.stackedWidget->setCurrentWidget(ui.page);
138 auto *view = qobject_cast<ChatView *>(_chatViews.value(id));
140 ui.stackedWidget->setCurrentWidget(view);
141 _chatViewSearchController->setScene(view->scene());
146 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem)
148 auto *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
150 view->centerOn(highlightItem);
155 void BufferWidget::zoomIn()
157 auto *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
163 void BufferWidget::zoomOut()
165 auto *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
171 void BufferWidget::zoomOriginal()
173 auto *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
175 view->zoomOriginal();
179 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"));
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);
231 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
233 auto *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
235 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
237 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
238 auto *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
240 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
241 if (curBufferId.isValid()) {
242 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
243 if (markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
244 curView->setMarkerLineVisible(false);
246 curView->setMarkerLineVisible(true);
250 if (prevView && autoMarkerLine())
251 setMarkerLine(prevView, false);
255 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack)
258 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
262 ChatLine *lastLine = view->lastVisibleChatLine();
264 QModelIndex idx = lastLine->index();
265 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
266 BufferId bufId = view->scene()->singleBufferId();
268 if (!allowGoingBack) {
269 MsgId oldMsgId = Client::markerLine(bufId);
270 if (oldMsgId.isValid() && msgId <= oldMsgId)
273 Client::setMarkerLine(bufId, msgId);
278 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog)
281 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
285 view->jumpToMarkerLine(requestBacklog);