From f84e68f556579cbe861f50b94b19ae83704fc17f Mon Sep 17 00:00:00 2001 From: Dirk Rettschlag Date: Fri, 26 Feb 2010 13:58:06 +0100 Subject: [PATCH 1/1] implemented per chat history Closes #168 --- src/qtui/inputwidget.cpp | 26 +++++++++++++++- src/qtui/inputwidget.h | 11 +++++++ .../settingspages/inputwidgetsettingspage.ui | 16 ++++++++++ src/uisupport/multilineedit.cpp | 30 +++++++++---------- src/uisupport/multilineedit.h | 14 +++++++-- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/qtui/inputwidget.cpp b/src/qtui/inputwidget.cpp index 4aa75923..f05311a7 100644 --- a/src/qtui/inputwidget.cpp +++ b/src/qtui/inputwidget.cpp @@ -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 ¤t, const QModelIndex &previous) { - Q_UNUSED(previous) + BufferId currentBufferId = current.data(NetworkModel::BufferIdRole).value(); + BufferId previousBufferId = previous.data(NetworkModel::BufferIdRole).value(); + + 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(); if(networkId == _networkId) return; diff --git a/src/qtui/inputwidget.h b/src/qtui/inputwidget.h index 165c312e..05916ee6 100644 --- a/src/qtui/inputwidget.h +++ b/src/qtui/inputwidget.h @@ -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 tempHistory; + qint32 idx; + QString inputLine; + }; + + QMap historyMap; }; diff --git a/src/qtui/settingspages/inputwidgetsettingspage.ui b/src/qtui/settingspages/inputwidgetsettingspage.ui index c64add4a..64048cdb 100644 --- a/src/qtui/settingspages/inputwidgetsettingspage.ui +++ b/src/qtui/settingspages/inputwidgetsettingspage.ui @@ -57,6 +57,22 @@ + + + + Enable per chat history + + + true + + + EnablePerChatHistory + + + true + + + diff --git a/src/uisupport/multilineedit.cpp b/src/uisupport/multilineedit.cpp index 02e9df2e..2440c42d 100644 --- a/src/uisupport/multilineedit.cpp +++ b/src/uisupport/multilineedit.cpp @@ -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 diff --git a/src/uisupport/multilineedit.h b/src/uisupport/multilineedit.h index f10572b8..73ea6788 100644 --- a/src/uisupport/multilineedit.h +++ b/src/uisupport/multilineedit.h @@ -70,6 +70,10 @@ public: inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; } inline QMap mircColorMap() const { return _mircColorMap; } + inline QStringList history() { return _history; } + inline QHash 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 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 tempHistory; - qint32 idx; + QStringList _history; + QHash _tempHistory; + qint32 _idx; Mode _mode; bool _singleLine; int _minHeight; -- 2.20.1