1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
41 BufferWidget::BufferWidget(QWidget *parent)
42 : AbstractBufferContainer(parent),
43 _chatViewSearchController(new ChatViewSearchController(this)),
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, SIGNAL(searchChanged(const QString &)),
57 _chatViewSearchController, SLOT(setSearchString(const QString &)));
58 connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
59 _chatViewSearchController, SLOT(setCaseSensitive(bool)));
60 connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
61 _chatViewSearchController, SLOT(setSearchSenders(bool)));
62 connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
63 _chatViewSearchController, SLOT(setSearchMsgs(bool)));
64 connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
65 _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
66 connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
67 _chatViewSearchController, SLOT(highlightPrev()));
68 connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
69 _chatViewSearchController, SLOT(highlightNext()));
71 connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
73 connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
74 this, SLOT(scrollToHighlight(QGraphicsItem *)));
76 ActionCollection *coll = QtUi::actionCollection();
78 Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
79 zoomInChatview->setText(tr("Zoom In"));
80 zoomInChatview->setIcon(SmallIcon("zoom-in"));
81 zoomInChatview->setShortcut(QKeySequence::ZoomIn);
83 Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
84 zoomOutChatview->setIcon(SmallIcon("zoom-out"));
85 zoomOutChatview->setText(tr("Zoom Out"));
86 zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
88 Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
89 zoomOriginalChatview->setIcon(SmallIcon("zoom-original"));
90 zoomOriginalChatview->setText(tr("Actual Size"));
91 //zoomOriginalChatview->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); // used for RTS switching
93 Action *setMarkerLine = coll->add<Action>("SetMarkerLineToBottom", this, SLOT(setMarkerLine()));
94 setMarkerLine->setText(tr("Set Marker Line"));
95 setMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
97 Action *jumpToMarkerLine = QtUi::actionCollection("Navigation")->add<Action>("JumpToMarkerLine", this, SLOT(jumpToMarkerLine()));
98 jumpToMarkerLine->setText(tr("Go to Marker Line"));
99 jumpToMarkerLine->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
102 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
105 BufferWidget::~BufferWidget() {
106 delete _chatViewSearchController;
107 _chatViewSearchController = 0;
110 void BufferWidget::setAutoMarkerLine(const QVariant &v) {
111 _autoMarkerLine = v.toBool();
114 AbstractChatView *BufferWidget::createChatView(BufferId id) {
116 chatView = new ChatView(id, this);
117 chatView->setBufferContainer(this);
118 _chatViews[id] = chatView;
119 ui.stackedWidget->addWidget(chatView);
120 chatView->setFocusProxy(this);
124 void BufferWidget::removeChatView(BufferId id) {
125 QWidget *view = _chatViews.value(id, 0);
127 ui.stackedWidget->removeWidget(view);
132 void BufferWidget::showChatView(BufferId id) {
134 ui.stackedWidget->setCurrentWidget(ui.page);
136 ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
138 ui.stackedWidget->setCurrentWidget(view);
139 _chatViewSearchController->setScene(view->scene());
143 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
144 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
146 view->centerOn(highlightItem);
150 void BufferWidget::zoomIn() {
151 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
156 void BufferWidget::zoomOut() {
157 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
162 void BufferWidget::zoomOriginal() {
163 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
165 view->zoomOriginal();
168 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) {
178 if(event->type() != QEvent::KeyPress)
181 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
183 MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
187 // Intercept copy key presses
188 if(keyEvent == QKeySequence::Copy) {
189 if(inputLine->hasSelectedText())
191 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
193 view->scene()->selectionToClipboard();
197 // We don't want to steal cursor movement keys if the input line is in multiline mode
198 if(!inputLine->isSingleLine())
201 switch(keyEvent->key()) {
204 if(!(keyEvent->modifiers() & Qt::ShiftModifier))
207 case Qt::Key_PageDown:
208 // static cast to access public qobject::event
209 return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
215 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
216 ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
218 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
220 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
221 ChatView *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
223 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
224 if(curBufferId.isValid()) {
225 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
226 if(markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
227 curView->setMarkerLineVisible(false);
229 curView->setMarkerLineVisible(true);
233 if(prevView && autoMarkerLine())
234 setMarkerLine(prevView, false);
237 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack) {
239 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
243 ChatLine *lastLine = view->lastVisibleChatLine();
245 QModelIndex idx = lastLine->index();
246 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
248 if(!allowGoingBack) {
249 BufferId bufId = view->scene()->singleBufferId();
250 MsgId oldMsgId = Client::markerLine(bufId);
251 if(oldMsgId.isValid() && msgId <= oldMsgId)
254 view->setMarkerLine(msgId);
258 void BufferWidget::jumpToMarkerLine(ChatView *view, bool requestBacklog) {
260 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
264 view->jumpToMarkerLine(requestBacklog);