From 6d55e659fa87565462d7f3e92da594fdcf9708a2 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Mon, 20 Oct 2008 17:24:38 +0200 Subject: [PATCH] Reworking highlighting a bit * Adding an option to match nicks case-insensitive * Fix bugs in NotificationsSettingsPage * Simplify regexps * No testing (yet) --- src/client/clientsettings.cpp | 8 ++++ src/client/clientsettings.h | 7 +++- src/qtui/qtuimessageprocessor.cpp | 29 +++++++++------ src/qtui/qtuimessageprocessor.h | 7 +++- .../settingspages/highlightsettingspage.cpp | 31 ++++++++-------- .../settingspages/highlightsettingspage.ui | 37 ++++++++++++++++--- 6 files changed, 84 insertions(+), 35 deletions(-) diff --git a/src/client/clientsettings.cpp b/src/client/clientsettings.cpp index 980bb2f6..31200782 100644 --- a/src/client/clientsettings.cpp +++ b/src/client/clientsettings.cpp @@ -126,3 +126,11 @@ void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickT NotificationSettings::HighlightNickType NotificationSettings::highlightNick() { return (NotificationSettings::HighlightNickType) localValue("highlightNick", CurrentNick).toInt(); } + +void NotificationSettings::setNicksCaseSensitive(bool cs) { + setLocalValue("Highlights/NicksCaseSensitive", cs); +} + +bool NotificationSettings::nicksCaseSensitive() { + return localValue("Highlights/NicksCaseSensitive", true).toBool(); +} diff --git a/src/client/clientsettings.h b/src/client/clientsettings.h index 6c7c6db1..01353d7d 100644 --- a/src/client/clientsettings.h +++ b/src/client/clientsettings.h @@ -78,16 +78,19 @@ class NotificationSettings : public ClientSettings { }; NotificationSettings(); - + inline void setValue(const QString &key, const QVariant &data) { setLocalValue(key, data); } inline QVariant value(const QString &key, const QVariant &def = QVariant()) { return localValue(key, def); } inline void remove(const QString &key) { removeLocalKey(key); } - + void setHighlightList(const QVariantList &highlightList); QVariantList highlightList(); void setHighlightNick(HighlightNickType); HighlightNickType highlightNick(); + void setNicksCaseSensitive(bool); + bool nicksCaseSensitive(); + }; #endif diff --git a/src/qtui/qtuimessageprocessor.cpp b/src/qtui/qtuimessageprocessor.cpp index 9399d1f0..e7fa1983 100644 --- a/src/qtui/qtuimessageprocessor.cpp +++ b/src/qtui/qtuimessageprocessor.cpp @@ -36,11 +36,13 @@ QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) _msgCount(0) { NotificationSettings notificationSettings; + _nicksCaseSensitive = notificationSettings.nicksCaseSensitive(); _highlightNick = notificationSettings.highlightNick(); highlightListChanged(notificationSettings.highlightList()); + notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &))); notificationSettings.notify("highlightList", this, SLOT(highlightListChanged(const QVariant &))); notificationSettings.notify("highlightNick", this, SLOT(highlightNickChanged(const QVariant &))); - + _processTimer.setInterval(0); connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage())); } @@ -71,7 +73,7 @@ void QtUiMessageProcessor::process(QList &msgs) { Client::messageModel()->insertMessages(msgs); return; - + if(msgs.isEmpty()) return; _processQueue.append(msgs); _msgCount += msgs.count(); @@ -124,7 +126,7 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) { if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self))) return; - //NotificationSettings notificationSettings; + // TODO: Cache this (per network) const Network *net = Client::network(msg.bufferInfo().networkId()); if(net && !net->myNick().isEmpty()) { QStringList nickList; @@ -136,23 +138,24 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) { nickList = myIdentity->nicks(); } foreach(QString nickname, nickList) { - QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$"); - if(nickRegExp.exactMatch(msg.contents())) { + QRegExp nickRegExp("\\b" + QRegExp::escape(nickname) + "\\b", + _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + if(nickRegExp.indexIn(msg.contents()) >= 0) { msg.setFlags(msg.flags() | Message::Highlight); return; } } for(int i = 0; i < _highlightRules.count(); i++) { - const HighlightRule &rule = _highlightRules[i]; + const HighlightRule &rule = _highlightRules.at(i); if(!rule.isEnabled) continue; QRegExp userRegExp; if(rule.isRegExp) { - userRegExp = QRegExp(rule.name, rule.caseSensitive); + userRegExp = QRegExp(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive); } else { - userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive); + userRegExp = QRegExp("\\b" + QRegExp::escape(rule.name) + "\\b", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive); } if(userRegExp.exactMatch(msg.contents())) { msg.setFlags(msg.flags() | Message::Highlight); @@ -162,6 +165,10 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) { } } +void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) { + _nicksCaseSensitive = variant.toBool(); +} + void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) { QVariantList varList = variant.toList(); @@ -171,9 +178,9 @@ void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) { while(iter != iterEnd) { QVariantMap rule; _highlightRules << HighlightRule(rule["name"].toString(), - rule["enable"].toBool(), - rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive, - rule["regex"].toBool()); + rule["enable"].toBool(), + rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive, + rule["regex"].toBool()); iter++; } } diff --git a/src/qtui/qtuimessageprocessor.h b/src/qtui/qtuimessageprocessor.h index 57ed9bcc..de2d88c1 100644 --- a/src/qtui/qtuimessageprocessor.h +++ b/src/qtui/qtuimessageprocessor.h @@ -48,6 +48,7 @@ public slots: private slots: void processNextMessage(); + void nicksCaseSensitiveChanged(const QVariant &variant); void highlightListChanged(const QVariant &variant); void highlightNickChanged(const QVariant &variant); @@ -69,11 +70,13 @@ private: bool isEnabled; Qt::CaseSensitivity caseSensitive; bool isRegExp; - inline HighlightRule(const QString &name, bool enabled, Qt::CaseSensitivity cs, bool regExp) : name(name), isEnabled(enabled), caseSensitive(cs), isRegExp(regExp) {} + inline HighlightRule(const QString &name, bool enabled, Qt::CaseSensitivity cs, bool regExp) + : name(name), isEnabled(enabled), caseSensitive(cs), isRegExp(regExp) {} }; - + QList _highlightRules; NotificationSettings::HighlightNickType _highlightNick; + bool _nicksCaseSensitive; }; #endif diff --git a/src/qtui/settingspages/highlightsettingspage.cpp b/src/qtui/settingspages/highlightsettingspage.cpp index 9c290af4..45567d1d 100644 --- a/src/qtui/settingspages/highlightsettingspage.cpp +++ b/src/qtui/settingspages/highlightsettingspage.cpp @@ -39,9 +39,9 @@ HighlightSettingsPage::HighlightSettingsPage(QWidget *parent) ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setWhatsThis("CS: This option determines if the highlight rule should be interpreted case sensitive."); ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch); - ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents); - ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents); - ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents); + ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents); + ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents); + ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents); connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow())); connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows())); @@ -51,6 +51,7 @@ HighlightSettingsPage::HighlightSettingsPage(QWidget *parent) connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged())); connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged())); connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged())); + connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged())); connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged())); connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged())); connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *))); @@ -62,6 +63,7 @@ bool HighlightSettingsPage::hasDefaults() const { void HighlightSettingsPage::defaults() { ui.highlightCurrentNick->setChecked(true); + ui.nicksCaseSensitive->setChecked(true); emptyTable(); widgetHasChanged(); @@ -145,7 +147,7 @@ void HighlightSettingsPage::emptyTable() { } void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) { - if(item->row()+1 > highlightList.size()) + if(item->row()+1 > highlightList.size()) return; QVariantMap highlightRule = highlightList.value(item->row()).toMap(); @@ -198,6 +200,7 @@ void HighlightSettingsPage::load() { ui.highlightAllNicks->setChecked(true); break; } + ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive()); setChangedState(false); } @@ -207,14 +210,15 @@ void HighlightSettingsPage::save() { notificationSettings.setHighlightList(highlightList); NotificationSettings::HighlightNickType highlightNickType; - if(ui.highlightNoNick->isChecked()) + if(ui.highlightNoNick->isChecked()) highlightNickType = NotificationSettings::NoNick; - if(ui.highlightCurrentNick->isChecked()) + if(ui.highlightCurrentNick->isChecked()) highlightNickType = NotificationSettings::CurrentNick; - if(ui.highlightAllNicks->isChecked()) + if(ui.highlightAllNicks->isChecked()) highlightNickType = NotificationSettings::AllNicks; notificationSettings.setHighlightNick(highlightNickType); + notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked()); load(); setChangedState(false); @@ -229,20 +233,17 @@ bool HighlightSettingsPage::testHasChanged() { NotificationSettings notificationSettings; NotificationSettings::HighlightNickType highlightNickType; - if(ui.highlightNoNick->isChecked()) + if(ui.highlightNoNick->isChecked()) highlightNickType = NotificationSettings::NoNick; - if(ui.highlightCurrentNick->isChecked()) + if(ui.highlightCurrentNick->isChecked()) highlightNickType = NotificationSettings::CurrentNick; - if(ui.highlightAllNicks->isChecked()) + if(ui.highlightAllNicks->isChecked()) highlightNickType = NotificationSettings::AllNicks; if(notificationSettings.highlightNick() != highlightNickType) return true; + if(notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked()) return true; if(notificationSettings.highlightList() != highlightList) return true; - return true; + return false; } - - - - diff --git a/src/qtui/settingspages/highlightsettingspage.ui b/src/qtui/settingspages/highlightsettingspage.ui index ea210206..1929460e 100644 --- a/src/qtui/settingspages/highlightsettingspage.ui +++ b/src/qtui/settingspages/highlightsettingspage.ui @@ -16,7 +16,7 @@ - Highlight list + Custom Highlights @@ -25,7 +25,7 @@ - + @@ -68,7 +68,7 @@ Qt::Vertical - + 20 40 @@ -82,7 +82,7 @@ - Highlight nicks + Highlight Nicks @@ -109,11 +109,38 @@ + + + + Case sensitive + + + true + + + - + + + highlightNoNick + toggled(bool) + nicksCaseSensitive + setDisabled(bool) + + + 64 + 350 + + + 69 + 381 + + + + -- 2.20.1