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));
98 s.initAndNotify("AutoMarkerLine", this, SLOT(setAutoMarkerLine(QVariant)), true);
101 BufferWidget::~BufferWidget() {
102 delete _chatViewSearchController;
103 _chatViewSearchController = 0;
106 void BufferWidget::setAutoMarkerLine(const QVariant &v) {
107 _autoMarkerLine = v.toBool();
110 AbstractChatView *BufferWidget::createChatView(BufferId id) {
112 chatView = new ChatView(id, this);
113 chatView->setBufferContainer(this);
114 _chatViews[id] = chatView;
115 ui.stackedWidget->addWidget(chatView);
116 chatView->setFocusProxy(this);
120 void BufferWidget::removeChatView(BufferId id) {
121 QWidget *view = _chatViews.value(id, 0);
123 ui.stackedWidget->removeWidget(view);
128 void BufferWidget::showChatView(BufferId id) {
130 ui.stackedWidget->setCurrentWidget(ui.page);
132 ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
134 ui.stackedWidget->setCurrentWidget(view);
135 _chatViewSearchController->setScene(view->scene());
139 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
140 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
142 view->centerOn(highlightItem);
146 void BufferWidget::zoomIn() {
147 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
152 void BufferWidget::zoomOut() {
153 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
158 void BufferWidget::zoomOriginal() {
159 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
161 view->zoomOriginal();
164 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos) {
166 ActionCollection *coll = QtUi::actionCollection();
167 menu->addSeparator();
168 menu->addAction(coll->action("ZoomInChatView"));
169 menu->addAction(coll->action("ZoomOutChatView"));
170 menu->addAction(coll->action("ZoomOriginalChatView"));
173 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
174 if(event->type() != QEvent::KeyPress)
177 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
179 MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
183 // Intercept copy key presses
184 if(keyEvent == QKeySequence::Copy) {
185 if(inputLine->hasSelectedText())
187 ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
189 view->scene()->selectionToClipboard();
193 // We don't want to steal cursor movement keys if the input line is in multiline mode
194 if(!inputLine->isSingleLine())
197 switch(keyEvent->key()) {
200 if(!(keyEvent->modifiers() & Qt::ShiftModifier))
203 case Qt::Key_PageDown:
204 // static cast to access public qobject::event
205 return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
211 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
212 ChatView *prevView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
214 AbstractBufferContainer::currentChanged(current, previous); // switch first to avoid a redraw
216 // we need to hide the marker line if it's already/still at the bottom of the view (and not scrolled up)
217 ChatView *curView = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
219 BufferId curBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
220 if(curBufferId.isValid()) {
221 MsgId markerMsgId = Client::networkModel()->markerLineMsgId(curBufferId);
222 if(markerMsgId == curView->lastMsgId() && markerMsgId == curView->lastVisibleMsgId())
223 curView->setMarkerLineVisible(false);
225 curView->setMarkerLineVisible(true);
229 if(prevView && autoMarkerLine())
230 setMarkerLine(prevView, false);
233 void BufferWidget::setMarkerLine(ChatView *view, bool allowGoingBack) {
235 view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
239 ChatLine *lastLine = view->lastVisibleChatLine();
241 if(!allowGoingBack) {
242 QModelIndex idx = lastLine->index();
243 BufferId bufId = view->scene()->singleBufferId();
244 MsgId msgId = idx.data(MessageModel::MsgIdRole).value<MsgId>();
245 MsgId oldMsgId = Client::markerLine(bufId);
246 if(oldMsgId.isValid() && msgId <= oldMsgId)
249 view->setMarkedLine(lastLine);