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