implemented per chat history
authorDirk Rettschlag <dirk.rettschlag@gmail.com>
Fri, 26 Feb 2010 12:58:06 +0000 (13:58 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 3 Mar 2010 01:56:31 +0000 (02:56 +0100)
Closes #168

src/qtui/inputwidget.cpp
src/qtui/inputwidget.h
src/qtui/settingspages/inputwidgetsettingspage.ui
src/uisupport/multilineedit.cpp
src/uisupport/multilineedit.h

index 4aa7592..f05311a 100644 (file)
@@ -110,6 +110,9 @@ InputWidget::InputWidget(QWidget *parent)
   s.notify("ShowStyleButtons", this, SLOT(setShowStyleButtons(QVariant)));
   setShowStyleButtons(s.value("ShowStyleButtons", true));
 
+  s.notify("EnablePerChatHistory", this, SLOT(setEnablePerChatHistory(QVariant)));
+  setEnablePerChatHistory(s.value("EnablePerChatHistory", true));
+
   s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
   setMaxLines(s.value("MaxNumLines", 5));
 
@@ -159,6 +162,10 @@ void InputWidget::setShowStyleButtons(const QVariant &v) {
   ui.showStyleButton->setVisible(v.toBool());
 }
 
+void InputWidget::setEnablePerChatHistory(const QVariant &v) {
+  _perChatHistory = v.toBool();
+}
+
 void InputWidget::setMaxLines(const QVariant &v) {
   ui.inputEdit->setMaxHeight(v.toInt());
 }
@@ -203,7 +210,24 @@ bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
 }
 
 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
-  Q_UNUSED(previous)
+  BufferId currentBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
+  BufferId previousBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
+
+  if (_perChatHistory) {
+    //backup
+    historyMap[previousBufferId].history = inputLine()->history();
+    historyMap[previousBufferId].tempHistory = inputLine()->tempHistory();
+    historyMap[previousBufferId].idx = inputLine()->idx();
+    historyMap[previousBufferId].inputLine = inputLine()->html();
+
+    //restore
+    inputLine()->setHistory(historyMap[currentBufferId].history);
+    inputLine()->setTempHistory(historyMap[currentBufferId].tempHistory);
+    inputLine()->setIdx(historyMap[currentBufferId].idx);
+    inputLine()->setHtml(historyMap[currentBufferId].inputLine);
+    inputLine()->moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
+  }
+
   NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
   if(networkId == _networkId)
     return;
index 165c312..05916ee 100644 (file)
@@ -57,6 +57,7 @@ private slots:
   void setEnableSpellCheck(const QVariant &);
   void setShowNickSelector(const QVariant &);
   void setShowStyleButtons(const QVariant &);
+  void setEnablePerChatHistory(const QVariant &);
   void setMaxLines(const QVariant &);
   void setMultiLineEnabled(const QVariant &);
   void setScrollBarsEnabled(const QVariant &);
@@ -91,6 +92,16 @@ private:
   QIcon createColorToolButtonIcon(const QIcon &icon, const QColor &color);
   QTextCharFormat getFormatOfWordOrSelection();
   void setFormatOnSelection(const QTextCharFormat &format);
+
+  bool _perChatHistory;
+  struct HistoryState {
+    QStringList history;
+    QHash<int, QString> tempHistory;
+    qint32 idx;
+    QString inputLine;
+  };
+
+  QMap<BufferId, HistoryState> historyMap;
 };
 
 
index c64add4..64048cd 100644 (file)
      </property>
     </widget>
    </item>
+   <item>
+    <widget class="QCheckBox" name="enablePerBufferHistory">
+     <property name="text">
+      <string>Enable per chat history</string>
+     </property>
+     <property name="checked">
+      <bool>true</bool>
+     </property>
+     <property name="settingsKey" stdset="0">
+      <string>EnablePerChatHistory</string>
+     </property>
+     <property name="defaultValue" stdset="0">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
    <item>
     <widget class="QCheckBox" name="showNickSelector">
      <property name="text">
index 02e9df2..2440c42 100644 (file)
@@ -37,7 +37,7 @@ MultiLineEdit::MultiLineEdit(QWidget *parent)
 #else
     QTextEdit(parent),
 #endif
-    idx(0),
+    _idx(0),
     _mode(SingleLine),
     _singleLine(true),
     _minHeight(1),
@@ -191,8 +191,8 @@ void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *) {
 void MultiLineEdit::historyMoveBack() {
   addToHistory(convertRichtextToMircCodes(), true);
 
-  if(idx > 0) {
-    idx--;
+  if(_idx > 0) {
+    _idx--;
     showHistoryEntry();
   }
 }
@@ -200,9 +200,9 @@ void MultiLineEdit::historyMoveBack() {
 void MultiLineEdit::historyMoveForward() {
   addToHistory(convertRichtextToMircCodes(), true);
 
-  if(idx < history.count()) {
-    idx++;
-    if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
+  if(_idx < _history.count()) {
+    _idx++;
+    if(_idx < _history.count() || _tempHistory.contains(_idx)) // tempHistory might have an entry for idx == history.count() + 1
       showHistoryEntry();
     else
       reset();              // equals clear() in this case
@@ -216,20 +216,20 @@ bool MultiLineEdit::addToHistory(const QString &text, bool temporary) {
   if(text.isEmpty())
     return false;
 
-  Q_ASSERT(0 <= idx && idx <= history.count());
+  Q_ASSERT(0 <= _idx && _idx <= _history.count());
 
   if(temporary) {
     // if an entry of the history is changed, we remember it and show it again at this
     // position until a line was actually sent
     // sent lines get appended to the history
-    if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
-      tempHistory[idx] = text;
+    if(_history.isEmpty() || text != _history[_idx - (int)(_idx == _history.count())]) {
+      _tempHistory[_idx] = text;
       return true;
     }
   } else {
-    if(history.isEmpty() || text != history.last()) {
-      history << text;
-      tempHistory.clear();
+    if(_history.isEmpty() || text != _history.last()) {
+      _history << text;
+      _tempHistory.clear();
       return true;
     }
   }
@@ -524,7 +524,7 @@ void MultiLineEdit::on_returnPressed(const QString & text) {
       emit textEntered(line);
     }
     reset();
-    tempHistory.clear();
+    _tempHistory.clear();
   } else {
     emit noTextEntered();
   }
@@ -584,7 +584,7 @@ void MultiLineEdit::on_documentHeightChanged(qreal) {
 
 void MultiLineEdit::reset() {
   // every time the MultiLineEdit is cleared we also reset history index
-  idx = history.count();
+  _idx = _history.count();
   clear();
   QTextBlockFormat format = textCursor().blockFormat();
   format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
@@ -594,7 +594,7 @@ void MultiLineEdit::reset() {
 
 void MultiLineEdit::showHistoryEntry() {
   // if the user changed the history, display the changed line
-  setHtml(convertMircCodesToHtml(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]));
+  setHtml(convertMircCodesToHtml(_tempHistory.contains(_idx) ? _tempHistory[_idx] : _history[_idx]));
   QTextCursor cursor = textCursor();
   QTextBlockFormat format = cursor.blockFormat();
   format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
index f10572b..73ea678 100644 (file)
@@ -70,6 +70,10 @@ public:
   inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
   inline QMap<QString, QString>  mircColorMap() const { return _mircColorMap; }
 
+  inline QStringList history() { return _history; }
+  inline QHash<int, QString> tempHistory() { return _tempHistory; }
+  inline qint32 idx() { return _idx; }
+
 public slots:
   void setMode(Mode mode);
   void setMinHeight(int numLines);
@@ -81,6 +85,10 @@ public slots:
   // Note: Enabling wrap will make isSingleLine() not work correctly, so only use this if minHeight() > 1!
   void setWordWrapEnabled(bool enable = true);
 
+  inline void setHistory(QStringList history) { _history = history; }
+  inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
+  inline void setIdx(qint32 idx) { _idx = idx; }
+
 signals:
   void textEntered(const QString &text);
   void noTextEntered();
@@ -104,9 +112,9 @@ private slots:
   bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
 
 private:
-  QStringList history;
-  QHash<int, QString> tempHistory;
-  qint32 idx;
+  QStringList _history;
+  QHash<int, QString> _tempHistory;
+  qint32 _idx;
   Mode _mode;
   bool _singleLine;
   int _minHeight;