1b125138d1df71dcc369ded1a4396a7e18487207
[quassel.git] / src / qtui / bufferwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "bufferwidget.h"
22
23 #include <QKeyEvent>
24 #include <QLayout>
25 #include <QMenu>
26 #include <QScrollBar>
27
28 #include "action.h"
29 #include "actioncollection.h"
30 #include "backlogsettings.h"
31 #include "chatline.h"
32 #include "chatview.h"
33 #include "chatviewsearchbar.h"
34 #include "chatviewsearchcontroller.h"
35 #include "chatviewsettings.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "multilineedit.h"
39 #include "qtui.h"
40 #include "settings.h"
41
42 BufferWidget::BufferWidget(QWidget* parent)
43     : AbstractBufferContainer(parent)
44     , _chatViewSearchController(new ChatViewSearchController(this))
45     , _autoMarkerLine(true)
46     , _autoMarkerLineOnLostFocus(true)
47 {
48     ui.setupUi(this);
49     layout()->setContentsMargins(0, 0, 0, 0);
50     layout()->setSpacing(0);
51     // ui.searchBar->hide();
52
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());
57
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);
68
69     connect(ui.searchBar, &ChatViewSearchBar::hidden, this, selectOverload<>(&QWidget::setFocus));
70
71     connect(_chatViewSearchController, &ChatViewSearchController::newCurrentHighlight, this, &BufferWidget::scrollToHighlight);
72
73     ActionCollection* coll = QtUi::actionCollection();
74     coll->addActions(
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)});
83
84     ChatViewSettings s;
85     s.initAndNotify("AutoMarkerLine", this, &BufferWidget::setAutoMarkerLine, true);
86     s.initAndNotify("AutoMarkerLineOnLostFocus", this, &BufferWidget::setAutoMarkerLineOnLostFocus, true);
87
88     BacklogSettings backlogSettings;
89     backlogSettings.initAndNotify("EnsureBacklogOnBufferShow", this, &BufferWidget::setEnsureBacklogOnBufferShow, true);
90 }
91
92 BufferWidget::~BufferWidget()
93 {
94     delete _chatViewSearchController;
95     _chatViewSearchController = nullptr;
96 }
97
98 void BufferWidget::setAutoMarkerLine(const QVariant& v)
99 {
100     _autoMarkerLine = v.toBool();
101 }
102
103 void BufferWidget::setAutoMarkerLineOnLostFocus(const QVariant& v)
104 {
105     _autoMarkerLineOnLostFocus = v.toBool();
106 }
107
108 void BufferWidget::setEnsureBacklogOnBufferShow(const QVariant& v)
109 {
110     _ensureBacklogOnBufferShow = v.toBool();
111 }
112
113 AbstractChatView* BufferWidget::createChatView(BufferId id)
114 {
115     ChatView* chatView;
116     chatView = new ChatView(id, this);
117     chatView->setBufferContainer(this);
118     _chatViews[id] = chatView;
119     ui.stackedWidget->addWidget(chatView);
120     chatView->setFocusProxy(this);
121     return chatView;
122 }
123
124 void BufferWidget::removeChatView(BufferId id)
125 {
126     QWidget* view = _chatViews.value(id, 0);
127     if (!view)
128         return;
129     ui.stackedWidget->removeWidget(view);
130     view->deleteLater();
131     _chatViews.take(id);
132 }
133
134 void BufferWidget::showChatView(BufferId id)
135 {
136     if (!id.isValid()) {
137         ui.stackedWidget->setCurrentWidget(ui.page);
138     }
139     else {
140         auto* view = qobject_cast<ChatView*>(_chatViews.value(id));
141         Q_ASSERT(view);
142         ui.stackedWidget->setCurrentWidget(view);
143         _chatViewSearchController->setScene(view->scene());
144         if (_ensureBacklogOnBufferShow) {
145             // Try to ensure some messages are visible
146             view->requestBacklogForScroll();
147         }
148     }
149 }
150
151 void BufferWidget::scrollToHighlight(QGraphicsItem* highlightItem)
152 {
153     auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
154     if (view) {
155         view->centerOn(highlightItem);
156     }
157 }
158
159 void BufferWidget::zoomIn()
160 {
161     auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
162     if (view)
163         view->zoomIn();
164 }
165
166 void BufferWidget::zoomOut()
167 {
168     auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
169     if (view)
170         view->zoomOut();
171 }
172
173 void BufferWidget::zoomOriginal()
174 {
175     auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
176     if (view)
177         view->zoomOriginal();
178 }
179
180 void BufferWidget::addActionsToMenu(QMenu* menu, const QPointF& pos)
181 {
182     Q_UNUSED(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"));
188 }
189
190 bool BufferWidget::eventFilter(QObject* watched, QEvent* event)
191 {
192     if (event->type() != QEvent::KeyPress)
193         return false;
194
195     auto* keyEvent = static_cast<QKeyEvent*>(event);
196
197     auto* inputLine = qobject_cast<MultiLineEdit*>(watched);
198     if (!inputLine)
199         return false;
200
201     // Intercept copy key presses
202     if (keyEvent == QKeySequence::Copy) {
203         if (inputLine->hasSelectedText())
204             return false;
205         auto* view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
206         if (view)
207             view->scene()->selectionToClipboard();
208         return true;
209     }
210
211     // We don't want to steal cursor movement keys if the input line is in multiline mode
212     if (!inputLine->isSingleLine())
213         return false;
214
215     switch (keyEvent->key()) {
216     case Qt::Key_Up:
217     case Qt::Key_Down:
218         if (!(keyEvent->modifiers() & Qt::ShiftModifier))
219             return false;
220         // fallthrough
221     case Qt::Key_PageUp:
222     case Qt::Key_PageDown:
223         // static cast to access public qobject::event
224         return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
225     default:
226         return false;
227     }
228 }
229
230 void BufferWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
231 {
232     auto* prevView = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
233
234     AbstractBufferContainer::currentChanged(current, previous);  // switch first to avoid a redraw
235
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());
238     if (curView) {
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);
244             else
245                 curView->setMarkerLineVisible(true);
246         }
247     }
248
249     if (prevView && autoMarkerLine())
250         setMarkerLine(prevView, false);
251 }
252
253 void BufferWidget::setMarkerLine(ChatView* view, bool allowGoingBack)
254 {
255     if (!view)
256         view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
257     if (!view)
258         return;
259
260     ChatLine* lastLine = view->lastVisibleChatLine();
261     if (lastLine) {
262         QModelIndex idx = lastLine->index();
263         MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
264         BufferId bufId = view->scene()->singleBufferId();
265
266         if (!allowGoingBack) {
267             MsgId oldMsgId = Client::markerLine(bufId);
268             if (oldMsgId.isValid() && msgId <= oldMsgId)
269                 return;
270         }
271         Client::setMarkerLine(bufId, msgId);
272     }
273 }
274
275 void BufferWidget::jumpToMarkerLine(ChatView* view, bool requestBacklog)
276 {
277     if (!view)
278         view = qobject_cast<ChatView*>(ui.stackedWidget->currentWidget());
279     if (!view)
280         return;
281
282     view->jumpToMarkerLine(requestBacklog);
283 }