added zoom feature: "ctrl +" magnifies, "ctrl -" demagnifies and "ctrl 0" normalizes
[quassel.git] / src / qtui / bufferwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "bufferwidget.h"
22 #include "chatview.h"
23 #include "chatviewsearchbar.h"
24 #include "chatviewsearchcontroller.h"
25 #include "settings.h"
26 #include "client.h"
27
28 #include "action.h"
29 #include "actioncollection.h"
30 #include "qtui.h"
31
32 #include <QLayout>
33 #include <QKeyEvent>
34
35 BufferWidget::BufferWidget(QWidget *parent)
36   : AbstractBufferContainer(parent),
37     _chatViewSearchController(new ChatViewSearchController(this))
38 {
39   ui.setupUi(this);
40   layout()->setContentsMargins(0, 0, 0, 0);
41   layout()->setSpacing(0);
42   // ui.searchBar->hide();
43
44   _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
45   _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
46   _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
47   _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
48
49   connect(ui.searchBar->searchEditLine(), SIGNAL(textChanged(const QString &)),
50           _chatViewSearchController, SLOT(setSearchString(const QString &)));
51   connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
52           _chatViewSearchController, SLOT(setCaseSensitive(bool)));
53   connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
54           _chatViewSearchController, SLOT(setSearchSenders(bool)));
55   connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
56           _chatViewSearchController, SLOT(setSearchMsgs(bool)));
57   connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
58           _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
59   connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
60           _chatViewSearchController, SLOT(highlightPrev()));
61   connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
62           _chatViewSearchController, SLOT(highlightNext()));
63
64   connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
65           this, SLOT(scrollToHighlight(QGraphicsItem *)));
66   
67   ActionCollection *coll = QtUi::actionCollection();
68
69   Action *zoomChatview = coll->add<Action>("ZoomChatView");
70   connect(zoomChatview, SIGNAL(triggered()), SLOT(zoomIn()));
71   zoomChatview->setText(tr("Enlarge Chat View"));
72   zoomChatview->setShortcut(tr("Ctrl++"));
73
74   Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView");
75   connect(zoomOutChatview, SIGNAL(triggered()), SLOT(zoomOut()));
76   zoomOutChatview->setText(tr("Demagnify Chat View"));
77   zoomOutChatview->setShortcut(tr("Ctrl+-"));
78
79   Action *zoomNormalChatview = coll->add<Action>("ZoomNormalChatView");
80   connect(zoomNormalChatview, SIGNAL(triggered()), SLOT(zoomNormal()));
81   zoomNormalChatview->setText(tr("Normalize zoom of Chat View"));
82   zoomNormalChatview->setShortcut(tr("Ctrl+0"));
83 }
84
85 BufferWidget::~BufferWidget() {
86   delete _chatViewSearchController;
87   _chatViewSearchController = 0;
88 }
89
90 AbstractChatView *BufferWidget::createChatView(BufferId id) {
91   ChatView *chatView;
92   chatView = new ChatView(id, this);
93   _chatViews[id] = chatView;
94   ui.stackedWidget->addWidget(chatView);
95   chatView->setFocusProxy(this);
96   return chatView;
97 }
98
99 void BufferWidget::removeChatView(BufferId id) {
100   QWidget *view = _chatViews.value(id, 0);
101   if(!view) return;
102   ui.stackedWidget->removeWidget(view);
103   view->deleteLater();
104   _chatViews.take(id);
105 }
106
107 void BufferWidget::showChatView(BufferId id) {
108   if(!id.isValid()) {
109     ui.stackedWidget->setCurrentWidget(ui.page);
110   } else {
111     ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
112     Q_ASSERT(view);
113     ui.stackedWidget->setCurrentWidget(view);
114     _chatViewSearchController->setScene(view->scene());
115   }
116 }
117
118 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
119   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
120   if(view) {
121     view->centerOn(highlightItem);
122   }
123 }
124
125
126 void BufferWidget::zoomIn() {
127   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
128   if(!view) return;
129   view->zoomIn();
130 }
131
132 void BufferWidget::zoomOut() {
133   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
134   if(!view) return;
135   view->zoomOut();
136 }
137
138 void BufferWidget::zoomNormal() {
139   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
140   if(!view) return;
141   view->zoomNormal();
142 }
143
144 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
145   Q_UNUSED(watched);
146   if(event->type() != QEvent::KeyPress)
147     return false;
148
149   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
150   switch(keyEvent->key()) {
151     case Qt::Key_PageUp:
152     case Qt::Key_PageDown:
153       // static cast to access public qobject::event
154       return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
155     default:
156       return false;
157   }
158 }