Prevent coreinfodlg from being too tall
[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 #include <QScrollBar>
35
36 BufferWidget::BufferWidget(QWidget *parent)
37   : AbstractBufferContainer(parent),
38     _chatViewSearchController(new ChatViewSearchController(this))
39 {
40   ui.setupUi(this);
41   layout()->setContentsMargins(0, 0, 0, 0);
42   layout()->setSpacing(0);
43   // ui.searchBar->hide();
44
45   _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
46   _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
47   _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
48   _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
49
50   connect(ui.searchBar->searchEditLine(), SIGNAL(textChanged(const QString &)),
51           _chatViewSearchController, SLOT(setSearchString(const QString &)));
52   connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
53           _chatViewSearchController, SLOT(setCaseSensitive(bool)));
54   connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
55           _chatViewSearchController, SLOT(setSearchSenders(bool)));
56   connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
57           _chatViewSearchController, SLOT(setSearchMsgs(bool)));
58   connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
59           _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
60   connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
61           _chatViewSearchController, SLOT(highlightPrev()));
62   connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
63           _chatViewSearchController, SLOT(highlightNext()));
64
65   connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
66           this, SLOT(scrollToHighlight(QGraphicsItem *)));
67   
68   ActionCollection *coll = QtUi::actionCollection();
69
70   Action *zoomChatview = coll->add<Action>("ZoomChatView");
71   connect(zoomChatview, SIGNAL(triggered()), SLOT(zoomIn()));
72   zoomChatview->setText(tr("Enlarge Chat View"));
73   zoomChatview->setShortcut(tr("Ctrl++"));
74
75   Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView");
76   connect(zoomOutChatview, SIGNAL(triggered()), SLOT(zoomOut()));
77   zoomOutChatview->setText(tr("Demagnify Chat View"));
78   zoomOutChatview->setShortcut(tr("Ctrl+-"));
79
80   Action *zoomNormalChatview = coll->add<Action>("ZoomNormalChatView");
81   connect(zoomNormalChatview, SIGNAL(triggered()), SLOT(zoomNormal()));
82   zoomNormalChatview->setText(tr("Normalize zoom of Chat View"));
83   zoomNormalChatview->setShortcut(tr("Ctrl+0"));
84 }
85
86 BufferWidget::~BufferWidget() {
87   delete _chatViewSearchController;
88   _chatViewSearchController = 0;
89 }
90
91 AbstractChatView *BufferWidget::createChatView(BufferId id) {
92   ChatView *chatView;
93   chatView = new ChatView(id, this);
94   _chatViews[id] = chatView;
95   ui.stackedWidget->addWidget(chatView);
96   chatView->setFocusProxy(this);
97   return chatView;
98 }
99
100 void BufferWidget::removeChatView(BufferId id) {
101   QWidget *view = _chatViews.value(id, 0);
102   if(!view) return;
103   ui.stackedWidget->removeWidget(view);
104   view->deleteLater();
105   _chatViews.take(id);
106 }
107
108 void BufferWidget::showChatView(BufferId id) {
109   if(!id.isValid()) {
110     ui.stackedWidget->setCurrentWidget(ui.page);
111   } else {
112     ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
113     Q_ASSERT(view);
114     ui.stackedWidget->setCurrentWidget(view);
115     _chatViewSearchController->setScene(view->scene());
116   }
117 }
118
119 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
120   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
121   if(view) {
122     view->centerOn(highlightItem);
123   }
124 }
125
126
127 void BufferWidget::zoomIn() {
128   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
129   if(!view) return;
130   view->zoomIn();
131 }
132
133 void BufferWidget::zoomOut() {
134   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
135   if(!view) return;
136   view->zoomOut();
137 }
138
139 void BufferWidget::zoomNormal() {
140   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
141   if(!view) return;
142   view->zoomNormal();
143 }
144
145 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
146   Q_UNUSED(watched);
147   if(event->type() != QEvent::KeyPress)
148     return false;
149
150   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
151
152   int direction = 1;
153   switch(keyEvent->key()) {
154     case Qt::Key_PageUp:
155     case Qt::Key_PageDown:
156       // static cast to access public qobject::event
157       return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
158
159     case Qt::Key_Up:
160       direction = -1;
161     case Qt::Key_Down:
162       if(keyEvent->modifiers() == Qt::ShiftModifier) {
163         QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(ui.stackedWidget->currentWidget());
164         if(!scrollArea)
165           return false;
166         int sliderPosition = scrollArea->verticalScrollBar()->value();
167         scrollArea->verticalScrollBar()->setValue(sliderPosition + (direction * 12));
168         return true;
169       }
170     default:
171       return false;
172   }
173 }