1 /***************************************************************************
2 * Copyright (C) 2005-2013 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"
35 #include "iconloader.h"
36 #include "multilineedit.h"
40 BufferWidget::BufferWidget(QWidget *parent)
41 : AbstractBufferContainer(parent),
42 _chatViewSearchController(new ChatViewSearchController(this)),
46 layout()->setContentsMargins(0, 0, 0, 0);
47 layout()->setSpacing(0);
48 // ui.searchBar->hide();
50 _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
51 _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
52 _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
53 _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
55 connect(ui.searchBar, SIGNAL(searchChanged(const QString &)),
56 _chatViewSearchController, SLOT(setSearchString(const QString &)));
57 connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
58 _chatViewSearchController, SLOT(setCaseSensitive(bool)));
59 connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
60 _chatViewSearchController, SLOT(setSearchSenders(bool)));
61 connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
62 _chatViewSearchController, SLOT(setSearchMsgs(bool)));
63 connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
64 _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
65 connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
66 _chatViewSearchController, SLOT(highlightPrev()));
67 connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
68 _chatViewSearchController, SLOT(highlightNext()));
70 connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
72 connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
73 this, SLOT(scrollToHighlight(QGraphicsItem *)));
75 ActionCollection *coll = QtUi::actionCollection();
77 Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
78 zoomInChatview->setText(tr("Zoom In"));
79 zoomInChatview->setIcon(SmallIcon("zoom-in"));
80 zoomInChatview->setShortcut(QKeySequence::ZoomIn);
82 Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
83 zoomOutChatview->setIcon(SmallIcon("zoom-out"));
84 zoomOutChatview->setText(tr("Zoom Out"));
85 zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
87 Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
88 zoomOriginalChatview->setIcon(SmallIcon("zoom-original"));
89 zoomOriginalChatview->setText(tr("Actual Size"));
90 //zoomOriginalChatview->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); // used for RTS switching
92 Action *setMarkerLine = coll->add<Action>("SetMarkerLineToBottom", this, SLOT(setMarkerLine()));
93 setMarkerLine->setText(tr("Set Marker Line"));
94 setMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
96 Action *jumpToMarkerLine = QtUi::actionCollection("Navigation")->add<Action>("JumpToMarkerLine", this, SLOT(jumpToMarkerLine()));
97 jumpToMarkerLine->setText(tr("Go to Marker Line"));
98 jumpToMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
101 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
105 BufferWidget::~BufferWidget()
107 delete _chatViewSearchController;
108 _chatViewSearchController = 0;
112 void BufferWidget::setAutoMarkerLine(const QVariant &v)
114 _autoMarkerLine = v.toBool();
118 AbstractChatView *BufferWidget::createChatView(BufferId id)
121 chatView = new ChatView(id, this);
122 chatView->setBufferContainer(this);
123 _chatViews[id] = chatView;
124 ui.stackedWidget->addWidget(chatView);
125 chatView->setFocusProxy(this);
130 void BufferWidget::removeChatView(BufferId id)
132 QWidget *view = _chatViews.value(id, 0);
134 ui.stackedWidget->removeWidget(view);
140 void BufferWidget::showChatView(BufferId id)
143 ui.stackedWidget->setCurrentWidget(ui.page);
146 ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
148 ui.stackedWidget->setCurrentWidget(view);
149 _chatViewSearchController->setScene(view->scene());
154 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem)
156 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
158 view->centerOn(highlightItem);
163 void BufferWidget::zoomIn()
165 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
171 void BufferWidget::zoomOut()
173 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
179 void BufferWidget::zoomOriginal()
181 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
183 view->zoomOriginal();
187 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos)
190 ActionCollection *coll = QtUi::actionCollection();
191 menu->addSeparator();
192 menu->addAction(coll->action("ZoomInChatView"));
193 menu->addAction(coll->action("ZoomOutChatView"));
194 menu->addAction(coll->action("ZoomOriginalChatView"));
198 bool BufferWidget::eventFilter(QObject *watched, QEvent *event)
200 if (event->type() != QEvent::KeyPress)
203 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
205 MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
209 // Intercept copy key presses
210 if (keyEvent == QKeySequence::Copy) {
211 if (inputLine->hasSelectedText())
213 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
215 view->scene()->selectionToClipboard();
219 // We don't want to steal cursor movement keys if the input line is in multiline mode
220 if (!inputLine->isSingleLine())
223 switch (keyEvent->key()) {
226 if (!(keyEvent->modifiers() & Qt::ShiftModifier))
229 case Qt::Key_PageDown:
230 // static cast to access public qobject::event
231 return static_cast<QObject *>(ui.stackedWidget->currentWidget())->event(event);
238 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
240 ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
242 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
244 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
245 ChatView *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
247 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
248 if (curBufferId.isValid()) {
249 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
250 if (markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
251 curView->setMarkerLineVisible(false);
253 curView->setMarkerLineVisible(true);
257 if (prevView && autoMarkerLine())
258 setMarkerLine(prevView, false);
262 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack)
265 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
269 ChatLine *lastLine = view->lastVisibleChatLine();
271 QModelIndex idx = lastLine->index();
272 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
274 if (!allowGoingBack) {
275 BufferId bufId = view->scene()->singleBufferId();
276 MsgId oldMsgId = Client::markerLine(bufId);
277 if (oldMsgId.isValid() && msgId <= oldMsgId)
280 view->setMarkerLine(msgId);
285 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog)
288 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
292 view->jumpToMarkerLine(requestBacklog);