Make keyboard shortcuts independent from translations
[quassel.git] / src / qtui / bufferwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 <QLayout>
22 #include <QKeyEvent>
23 #include <QMenu>
24 #include <QScrollBar>
25
26 #include "action.h"
27 #include "actioncollection.h"
28 #include "bufferwidget.h"
29 #include "chatview.h"
30 #include "chatviewsearchbar.h"
31 #include "chatviewsearchcontroller.h"
32 #include "client.h"
33 #include "iconloader.h"
34 #include "multilineedit.h"
35 #include "qtui.h"
36 #include "settings.h"
37
38
39 BufferWidget::BufferWidget(QWidget *parent)
40   : AbstractBufferContainer(parent),
41     _chatViewSearchController(new ChatViewSearchController(this))
42 {
43   ui.setupUi(this);
44   layout()->setContentsMargins(0, 0, 0, 0);
45   layout()->setSpacing(0);
46   // ui.searchBar->hide();
47
48   _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
49   _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
50   _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
51   _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
52
53   connect(ui.searchBar, SIGNAL(searchChanged(const QString &)),
54     _chatViewSearchController, SLOT(setSearchString(const QString &)));
55   connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
56     _chatViewSearchController, SLOT(setCaseSensitive(bool)));
57   connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
58     _chatViewSearchController, SLOT(setSearchSenders(bool)));
59   connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
60     _chatViewSearchController, SLOT(setSearchMsgs(bool)));
61   connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
62     _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
63   connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
64     _chatViewSearchController, SLOT(highlightPrev()));
65   connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
66     _chatViewSearchController, SLOT(highlightNext()));
67
68   connect(ui.searchBar, SIGNAL(hidden()), this, SLOT(setFocus()));
69
70   connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
71     this, SLOT(scrollToHighlight(QGraphicsItem *)));
72
73   ActionCollection *coll = QtUi::actionCollection();
74
75   Action *zoomInChatview = coll->add<Action>("ZoomInChatView", this, SLOT(zoomIn()));
76   zoomInChatview->setText(tr("Zoom In"));
77   zoomInChatview->setIcon(SmallIcon("zoom-in"));
78   zoomInChatview->setShortcut(QKeySequence::ZoomIn);
79
80   Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView", this, SLOT(zoomOut()));
81   zoomOutChatview->setIcon(SmallIcon("zoom-out"));
82   zoomOutChatview->setText(tr("Zoom Out"));
83   zoomOutChatview->setShortcut(QKeySequence::ZoomOut);
84
85   Action *zoomOriginalChatview = coll->add<Action>("ZoomOriginalChatView", this, SLOT(zoomOriginal()));
86   zoomOriginalChatview->setIcon(SmallIcon("zoom-original"));
87   zoomOriginalChatview->setText(tr("Actual Size"));
88   //zoomOriginalChatview->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); // used for RTS switching
89 }
90
91 BufferWidget::~BufferWidget() {
92   delete _chatViewSearchController;
93   _chatViewSearchController = 0;
94 }
95
96 AbstractChatView *BufferWidget::createChatView(BufferId id) {
97   ChatView *chatView;
98   chatView = new ChatView(id, this);
99   chatView->setBufferContainer(this);
100   _chatViews[id] = chatView;
101   ui.stackedWidget->addWidget(chatView);
102   chatView->setFocusProxy(this);
103   return chatView;
104 }
105
106 void BufferWidget::removeChatView(BufferId id) {
107   QWidget *view = _chatViews.value(id, 0);
108   if(!view) return;
109   ui.stackedWidget->removeWidget(view);
110   view->deleteLater();
111   _chatViews.take(id);
112 }
113
114 void BufferWidget::showChatView(BufferId id) {
115   if(!id.isValid()) {
116     ui.stackedWidget->setCurrentWidget(ui.page);
117   } else {
118     ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
119     Q_ASSERT(view);
120     ui.stackedWidget->setCurrentWidget(view);
121     _chatViewSearchController->setScene(view->scene());
122   }
123 }
124
125 void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
126   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
127   if(view) {
128     view->centerOn(highlightItem);
129   }
130 }
131
132
133 void BufferWidget::zoomIn() {
134   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
135   if(view)
136     view->zoomIn();
137 }
138
139 void BufferWidget::zoomOut() {
140   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
141   if(view)
142     view->zoomOut();
143 }
144
145 void BufferWidget::zoomOriginal() {
146   ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
147   if(view)
148     view->zoomOriginal();
149 }
150
151 void BufferWidget::addActionsToMenu(QMenu *menu, const QPointF &pos) {
152   Q_UNUSED(pos);
153   ActionCollection *coll = QtUi::actionCollection();
154   menu->addSeparator();
155   menu->addAction(coll->action("ZoomInChatView"));
156   menu->addAction(coll->action("ZoomOutChatView"));
157   menu->addAction(coll->action("ZoomOriginalChatView"));
158 }
159
160 bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
161   if(event->type() != QEvent::KeyPress)
162     return false;
163
164   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
165
166   MultiLineEdit *inputLine = qobject_cast<MultiLineEdit *>(watched);
167   if(!inputLine)
168     return false;
169
170   // Intercept copy key presses
171   if(keyEvent == QKeySequence::Copy) {
172     if(inputLine->hasSelectedText())
173       return false;
174     ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
175     if(view)
176       view->scene()->selectionToClipboard();
177     return true;
178   }
179
180   // We don't want to steal cursor movement keys if the input line is in multiline mode
181   if(!inputLine->isSingleLine())
182     return false;
183
184   switch(keyEvent->key()) {
185   case Qt::Key_Up:
186   case Qt::Key_Down:
187     if(!(keyEvent->modifiers() & Qt::ShiftModifier))
188       return false;
189   case Qt::Key_PageUp:
190   case Qt::Key_PageDown:
191     // static cast to access public qobject::event
192     return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
193   default:
194     return false;
195   }
196 }