shift up/down scrolls up and down little bit.
[quassel.git] / src / qtui / bufferwidget.cpp
index 2deb46b..bfda813 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by The Quassel Team                             *
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  ***************************************************************************/
 
 #include "bufferwidget.h"
-#include "buffer.h"
-#include "chatline.h"
-#include "chatwidget.h"
+#include "chatview.h"
+#include "chatviewsearchbar.h"
+#include "chatviewsearchcontroller.h"
 #include "settings.h"
+#include "client.h"
 
-BufferWidget::BufferWidget(QWidget *parent) : QWidget(parent) {
-  ui.setupUi(this);
-
-  curBuf = 0;
-  //setBaseSize(QSize(600,400));
-  //setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
-  connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
-  connect(this, SIGNAL(nickListUpdated(QStringList)), ui.inputEdit, SLOT(updateNickList(QStringList)));
-
-}
+#include "action.h"
+#include "actioncollection.h"
+#include "qtui.h"
 
-void BufferWidget::init() {
+#include <QLayout>
+#include <QKeyEvent>
+#include <QScrollBar>
 
+BufferWidget::BufferWidget(QWidget *parent)
+  : AbstractBufferContainer(parent),
+    _chatViewSearchController(new ChatViewSearchController(this))
+{
+  ui.setupUi(this);
+  layout()->setContentsMargins(0, 0, 0, 0);
+  layout()->setSpacing(0);
+  // ui.searchBar->hide();
+
+  _chatViewSearchController->setCaseSensitive(ui.searchBar->caseSensitiveBox()->isChecked());
+  _chatViewSearchController->setSearchSenders(ui.searchBar->searchSendersBox()->isChecked());
+  _chatViewSearchController->setSearchMsgs(ui.searchBar->searchMsgsBox()->isChecked());
+  _chatViewSearchController->setSearchOnlyRegularMsgs(ui.searchBar->searchOnlyRegularMsgsBox()->isChecked());
+
+  connect(ui.searchBar->searchEditLine(), SIGNAL(textChanged(const QString &)),
+         _chatViewSearchController, SLOT(setSearchString(const QString &)));
+  connect(ui.searchBar->caseSensitiveBox(), SIGNAL(toggled(bool)),
+         _chatViewSearchController, SLOT(setCaseSensitive(bool)));
+  connect(ui.searchBar->searchSendersBox(), SIGNAL(toggled(bool)),
+         _chatViewSearchController, SLOT(setSearchSenders(bool)));
+  connect(ui.searchBar->searchMsgsBox(), SIGNAL(toggled(bool)),
+         _chatViewSearchController, SLOT(setSearchMsgs(bool)));
+  connect(ui.searchBar->searchOnlyRegularMsgsBox(), SIGNAL(toggled(bool)),
+         _chatViewSearchController, SLOT(setSearchOnlyRegularMsgs(bool)));
+  connect(ui.searchBar->searchUpButton(), SIGNAL(clicked()),
+         _chatViewSearchController, SLOT(highlightPrev()));
+  connect(ui.searchBar->searchDownButton(), SIGNAL(clicked()),
+         _chatViewSearchController, SLOT(highlightNext()));
+
+  connect(_chatViewSearchController, SIGNAL(newCurrentHighlight(QGraphicsItem *)),
+         this, SLOT(scrollToHighlight(QGraphicsItem *)));
+  
+  ActionCollection *coll = QtUi::actionCollection();
+
+  Action *zoomChatview = coll->add<Action>("ZoomChatView");
+  connect(zoomChatview, SIGNAL(triggered()), SLOT(zoomIn()));
+  zoomChatview->setText(tr("Enlarge Chat View"));
+  zoomChatview->setShortcut(tr("Ctrl++"));
+
+  Action *zoomOutChatview = coll->add<Action>("ZoomOutChatView");
+  connect(zoomOutChatview, SIGNAL(triggered()), SLOT(zoomOut()));
+  zoomOutChatview->setText(tr("Demagnify Chat View"));
+  zoomOutChatview->setShortcut(tr("Ctrl+-"));
+
+  Action *zoomNormalChatview = coll->add<Action>("ZoomNormalChatView");
+  connect(zoomNormalChatview, SIGNAL(triggered()), SLOT(zoomNormal()));
+  zoomNormalChatview->setText(tr("Normalize zoom of Chat View"));
+  zoomNormalChatview->setShortcut(tr("Ctrl+0"));
 }
 
 BufferWidget::~BufferWidget() {
-  foreach(BufferState *s, states.values()) {
-    delete s;
-  }
-}
-
-void BufferWidget::setBuffer(Buffer *buf) {
-  BufferState *state;
-  curBuf = buf;
-  if(states.contains(buf)) {
-    state = states[buf];
-  } else {
-    BufferState *s = new BufferState;
-    s->currentLine = Settings::guiValue(QString("BufferStates/%1/%2/currentLine").arg(buf->networkName()).arg(buf->bufferName()), -1).toInt();
-    if(buf->bufferType() == Buffer::ChannelBuffer) {
-      s->splitterState = Settings::guiValue(QString("BufferStates/%1/%2/splitter").arg(buf->networkName()).arg(buf->bufferName())).toByteArray();
-      s->splitter = new QSplitter(this);
-      s->chatWidget = new ChatWidget(s->splitter);
-      s->nickTree = new QTreeWidget(s->splitter);
-      s->nickTree->headerItem()->setHidden(true);
-      s->nickTree->setRootIsDecorated(false);
-      s->page = s->splitter;
-      s->ownNick = buf->ownNick();
-      s->topic = buf->topic();
-      updateNickList(s, buf->nickList());
-      s->splitter->restoreState(s->splitterState);
-      connect(buf, SIGNAL(nickListChanged(QVariantMap)), this, SLOT(updateNickList(QVariantMap)));
-      connect(s->nickTree, SIGNAL(itemExpanded(QTreeWidgetItem *)), this, SLOT(itemExpansionChanged(QTreeWidgetItem*)));
-      connect(s->nickTree, SIGNAL(itemCollapsed(QTreeWidgetItem *)), this, SLOT(itemExpansionChanged(QTreeWidgetItem*)));
-    } else {
-      s->splitter = 0; s->nickTree = 0;
-      s->chatWidget = new ChatWidget(this);
-      s->page = s->chatWidget;
-      s->ownNick = buf->ownNick();
-      s->topic = buf->bufferName();
-    }
-    s->opsExpanded = Settings::guiValue(QString("BufferStates/%1/%2/opsExpanded").arg(buf->networkName()).arg(buf->bufferName()), true).toBool();
-    s->voicedExpanded = Settings::guiValue(QString("BufferStates/%1/%2/voicedExpanded").arg(buf->networkName()).arg(buf->bufferName()), true).toBool();
-    s->usersExpanded = Settings::guiValue(QString("BufferStates/%1/%2/usersExpanded").arg(buf->networkName()).arg(buf->bufferName()), true).toBool();
-    states[buf] = s;
-    state = s;
-    state->chatWidget->init(networkName, bufferName);
-    QList<ChatLine *> lines;
-    QList<AbstractUiMsg *> msgs = buf->contents();
-    foreach(AbstractUiMsg *msg, msgs) {
-      lines.append(dynamic_cast<ChatLine*>(msg));
-    }
-    state->chatWidget->setContents(lines);
-    connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), state->chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
-    connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), state->chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
-    connect(buf, SIGNAL(topicSet(QString)), this, SLOT(setTopic(QString)));
-    connect(buf, SIGNAL(ownNickSet(QString)), this, SLOT(setOwnNick(QString)));
-    ui.stackedWidget->addWidget(s->page);
-  }
-  ui.stackedWidget->setCurrentWidget(state->page);
-  ui.topicEdit->setText(buf->topic());
-  chatWidget = state->chatWidget;
-  nickTree = state->nickTree;
-  splitter = state->splitter;
-  //ui.ownNick->set
-  disconnect(this, SIGNAL(userInput(QString)), 0, 0);
-  connect(this, SIGNAL(userInput(QString)), buf, SLOT(processUserInput(QString)));
-  state->chatWidget->setFocusProxy(ui.inputEdit);
-  ui.inputEdit->setFocus();
-  ui.topicEdit->setText(state->topic);
-  ui.ownNick->clear();  // TODO add nick history
-  ui.ownNick->addItem(state->ownNick);
-  updateTitle();
-}
-
-void BufferWidget::saveState() {
-  foreach(Buffer *buf, states.keys()) {
-    BufferState *s = states[buf];
-    if(s->splitter) Settings::setGuiValue(QString("BufferStates/%1/%2/splitter").arg(buf->networkName()).arg(buf->bufferName()), s->splitter->saveState());
-    Settings::setGuiValue(QString("BufferStates/%1/%2/currentLine").arg(buf->networkName()).arg(buf->bufferName()), s->currentLine);
-    Settings::setGuiValue(QString("BufferStates/%1/%2/opsExpanded").arg(buf->networkName()).arg(buf->bufferName()), s->opsExpanded);
-    Settings::setGuiValue(QString("BufferStates/%1/%2/voicedExpanded").arg(buf->networkName()).arg(buf->bufferName()), s->voicedExpanded);
-    Settings::setGuiValue(QString("BufferStates/%1/%2/usersExpanded").arg(buf->networkName()).arg(buf->bufferName()), s->usersExpanded);
-  }
+  delete _chatViewSearchController;
+  _chatViewSearchController = 0;
 }
 
-QSize BufferWidget::sizeHint() const {
-  return QSize(800,400);
+AbstractChatView *BufferWidget::createChatView(BufferId id) {
+  ChatView *chatView;
+  chatView = new ChatView(id, this);
+  _chatViews[id] = chatView;
+  ui.stackedWidget->addWidget(chatView);
+  chatView->setFocusProxy(this);
+  return chatView;
 }
 
-void BufferWidget::updateTitle() {
-  QString title = QString("%1 in %2 [%3]: %4").arg(ui.ownNick->currentText()).arg(bufferName).arg(networkName).arg(ui.topicEdit->text());
-  setWindowTitle(title);
+void BufferWidget::removeChatView(BufferId id) {
+  QWidget *view = _chatViews.value(id, 0);
+  if(!view) return;
+  ui.stackedWidget->removeWidget(view);
+  view->deleteLater();
+  _chatViews.take(id);
 }
 
-void BufferWidget::enterPressed() {
-  QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
-  foreach(QString msg, lines) {
-    if(msg.isEmpty()) continue;
-    emit userInput(msg);
+void BufferWidget::showChatView(BufferId id) {
+  if(!id.isValid()) {
+    ui.stackedWidget->setCurrentWidget(ui.page);
+  } else {
+    ChatView *view = qobject_cast<ChatView *>(_chatViews.value(id));
+    Q_ASSERT(view);
+    ui.stackedWidget->setCurrentWidget(view);
+    _chatViewSearchController->setScene(view->scene());
   }
-  ui.inputEdit->clear();
 }
 
-void BufferWidget::setActive(bool act) {
-  if(act != active) {
-    active = act;
-    //renderContents();
-    //scrollToEnd();
+void BufferWidget::scrollToHighlight(QGraphicsItem *highlightItem) {
+  ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
+  if(view) {
+    view->centerOn(highlightItem);
   }
 }
 
-void BufferWidget::resizeEvent ( QResizeEvent * event ) {
-  //qDebug() << "resizing:" << bufferName << event->size();
-  QWidget::resizeEvent(event);
 
+void BufferWidget::zoomIn() {
+  ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
+  if(!view) return;
+  view->zoomIn();
 }
 
-/*
-void BufferWidget::displayMsg(Message msg) {
-  chatWidget->appendMsg(msg);
-}
-*/
-
-void BufferWidget::setOwnNick(QString nick) {
-  Buffer *buf = qobject_cast<Buffer*>(sender());
-  Q_ASSERT(buf);
-  states[buf]->ownNick = nick;
-  if(buf == curBuf) {
-    ui.ownNick->clear();
-    ui.ownNick->addItem(nick);
-    updateTitle();
-  }
+void BufferWidget::zoomOut() {
+  ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
+  if(!view) return;
+  view->zoomOut();
 }
 
-void BufferWidget::setTopic(QString topic) {
-  Buffer *buf = qobject_cast<Buffer*>(sender());
-  Q_ASSERT(buf);
-  states[buf]->topic = topic;
-  if(buf == curBuf) {
-    ui.topicEdit->setText(topic);
-    updateTitle();
-  }
+void BufferWidget::zoomNormal() {
+  ChatView *view = qobject_cast<ChatView *>(ui.stackedWidget->currentWidget());
+  if(!view) return;
+  view->zoomNormal();
 }
 
+bool BufferWidget::eventFilter(QObject *watched, QEvent *event) {
+  Q_UNUSED(watched);
+  if(event->type() != QEvent::KeyPress)
+    return false;
 
-void BufferWidget::updateNickList(QVariantMap nicks) {
-  Buffer *buf = qobject_cast<Buffer*>(sender());
-  Q_ASSERT(buf);
-  updateNickList(states[buf], nicks);
-}
+  QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
 
-// TODO Use 005
-void BufferWidget::updateNickList(BufferState *state, QVariantMap nicks) {
-  emit nickListUpdated(nicks.keys());
-  QTreeWidget *tree = state->nickTree;
-  if(!tree) return;
-  tree->clear();
-  if(nicks.count() != 1) tree->setHeaderLabel(tr("%1 Users").arg(nicks.count()));
-  else tree->setHeaderLabel(tr("1 User"));
-  QTreeWidgetItem *ops = new QTreeWidgetItem();
-  QTreeWidgetItem *voiced = new QTreeWidgetItem();
-  QTreeWidgetItem *users = new QTreeWidgetItem();
-  // To sort case-insensitive, we have to put all nicks in a map which is sorted by (lowercase) key...
-  QMap<QString, QString> sorted;
-  foreach(QString n, nicks.keys()) { sorted[n.toLower()] = n; }
-  foreach(QString n, sorted.keys()) {
-    QString nick = sorted[n];
-    QString mode = nicks[nick].toMap()["Channels"].toMap()[bufferName].toMap()["Mode"].toString();
-    if(mode.contains('o')) { new QTreeWidgetItem(ops, QStringList(QString("@%1").arg(nick))); }
-    else if(mode.contains('v')) { new QTreeWidgetItem(voiced, QStringList(QString("+%1").arg(nick))); }
-    else new QTreeWidgetItem(users, QStringList(nick));
-  }
-  if(ops->childCount()) {
-    ops->setText(0, tr("%1 Operators").arg(ops->childCount()));
-    tree->addTopLevelItem(ops);
-    ops->setExpanded(state->opsExpanded);
-  } else delete ops;
-  if(voiced->childCount()) {
-    voiced->setText(0, tr("%1 Voiced").arg(voiced->childCount()));
-    tree->addTopLevelItem(voiced);
-    voiced->setExpanded(state->voicedExpanded);
-  } else delete voiced;
-  if(users->childCount()) {
-    users->setText(0, tr("%1 Users").arg(users->childCount()));
-    tree->addTopLevelItem(users);
-    users->setExpanded(state->usersExpanded);
-  } else delete users;
-}
+  int direction = 1;
+  switch(keyEvent->key()) {
+    case Qt::Key_PageUp:
+    case Qt::Key_PageDown:
+      // static cast to access public qobject::event
+      return static_cast<QObject*>(ui.stackedWidget->currentWidget())->event(event);
 
-// TODO Use 005 and additional user modes
-void BufferWidget::itemExpansionChanged(QTreeWidgetItem *item) {
-  if(item->child(0)->text(0).startsWith('@')) states[curBuf]->opsExpanded = item->isExpanded();
-  else if(item->child(0)->text(0).startsWith('+')) states[curBuf]->voicedExpanded = item->isExpanded();
-  else states[curBuf]->usersExpanded = item->isExpanded();
+    case Qt::Key_Up:
+      direction = -1;
+    case Qt::Key_Down:
+      if(keyEvent->modifiers() == Qt::ShiftModifier) {
+        QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(ui.stackedWidget->currentWidget());
+        if(!scrollArea)
+          return false;
+        int sliderPosition = scrollArea->verticalScrollBar()->value();
+        scrollArea->verticalScrollBar()->setValue(sliderPosition + (direction * 12));
+      }
+    default:
+      return false;
+  }
 }
-