Skip to content

Commit

Permalink
Add a filter to show only buffers with new messages by default
Browse files Browse the repository at this point in the history
Closes #156
  • Loading branch information
MartinBriza committed Sep 28, 2023
1 parent 05b6329 commit ac643ae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
29 changes: 27 additions & 2 deletions modules/Lith/Core/lith.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ void Lith::handleHotlistInitialization(const WeeChatProtocol::HData& hda) {
}
addHotlist(ptr, item);
}
emit hotlistUpdated();
}

void Lith::handleNicklistInitialization(const WeeChatProtocol::HData& hda) {
Expand Down Expand Up @@ -427,6 +428,7 @@ void Lith::handleHotlist(const WeeChatProtocol::HData& hda) {
hl->setProperty(qPrintable(key), value);
}
}
emit hotlistUpdated();
}

void Lith::_buffer_opened(const WeeChatProtocol::HData& hda) {
Expand Down Expand Up @@ -770,18 +772,41 @@ QSharedPointer<const HotListItem> Lith::getHotlist(pointer_t ptr) const {
return {};
}

ProxyBufferList::ProxyBufferList(QObject* parent, QAbstractListModel* parentModel)
ProxyBufferList::ProxyBufferList(Lith* parent, QAbstractListModel* parentModel)
: QSortFilterProxyModel(parent) {
setSourceModel(parentModel);
setFilterRole(Qt::UserRole);
connect(this, &ProxyBufferList::filterWordChanged, [this] { setFilterFixedString(filterWordGet()); });
connect(Lith::settingsGet(), &Settings::bufferListShowsOnlyBuffersWithNewMessagesChanged, this, [this]() {
showOnlyBuffersWithNewMessagesSet(Lith::settingsGet()->bufferListShowsOnlyBuffersWithNewMessagesGet());
invalidateFilter();
});
connect(parent, &Lith::hotlistUpdated, this, [this] {
if (showOnlyBuffersWithNewMessagesGet()) {
invalidateFilter();
}
});
showOnlyBuffersWithNewMessagesSet(Lith::settingsGet()->bufferListShowsOnlyBuffersWithNewMessagesGet());
}

bool ProxyBufferList::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const {
// If there are no conditions, just bail early
if (filterWordGet().isEmpty() && !showOnlyBuffersWithNewMessagesGet()) {
return true;
}

auto index = sourceModel()->index(source_row, 0, source_parent);
auto v = sourceModel()->data(index);
auto* b = qvariant_cast<Buffer*>(v);
if (b) {
return b->nameGet().toLower().contains(filterWordGet().toLower());
if (filterWordGet().isEmpty()) {
if (showOnlyBuffersWithNewMessagesGet()) {
return b->totalUnreadMessagesGet() > 0;
}
return true;
} else {
return b->nameGet().toLower().contains(filterWordGet().toLower());
}
}
return false;
}
4 changes: 3 additions & 1 deletion modules/Lith/Core/lith.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public slots:
signals:
void selectedBufferChanged();
void errorStringChanged();
void hotlistUpdated();

void pongReceived(qint64 id);

Expand Down Expand Up @@ -203,8 +204,9 @@ public slots:
class LITHCORE_EXPORT ProxyBufferList : public QSortFilterProxyModel {
Q_OBJECT
PROPERTY(QString, filterWord)
PROPERTY(bool, showOnlyBuffersWithNewMessages, false)
public:
ProxyBufferList(QObject* parent = nullptr, QAbstractListModel* parentModel = nullptr);
ProxyBufferList(Lith* parent = nullptr, QAbstractListModel* parentModel = nullptr);

bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
};
Expand Down
1 change: 1 addition & 0 deletions modules/Lith/Core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class LITHCORE_EXPORT Settings : public QObject {

SETTING(bool, showBufferListOnStartup, false)
SETTING(bool, platformBufferControlPosition, true)
SETTING(bool, bufferListShowsOnlyBuffersWithNewMessages, false)

SETTING(bool, showInternalData, false)
SETTING(bool, enableLogging, false)
Expand Down
9 changes: 9 additions & 0 deletions modules/Lith/UI/SettingsInterface.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ScrollView {
Lith.settings.baseFontFamily = fontDialog.currentFont.family
Lith.settings.showBufferListOnStartup = showBufferListOnStartupCheckbox.checked
Lith.settings.platformBufferControlPosition = platformBufferControlPositionCheckbox.checked
Lith.settings.bufferListShowsOnlyBuffersWithNewMessages = bufferListShowsOnlyBuffersWithNewMessagesCheckbox.checked
Lith.settings.enableNotifications = enableNotificationsCheckbox.checked
}
function restore() {
Expand Down Expand Up @@ -92,6 +93,7 @@ ScrollView {
fontDialog.currentFont.family = Lith.settings.baseFontFamily
showBufferListOnStartupCheckbox.checked = Lith.settings.showBufferListOnStartup
platformBufferControlPositionCheckbox.checked = Lith.settings.platformBufferControlPosition
bufferListShowsOnlyBuffersWithNewMessagesCheckbox.checked = Lith.settings.bufferListShowsOnlyBuffersWithNewMessages
enableNotificationsCheckbox.checked = Lith.settings.enableNotifications
}

Expand Down Expand Up @@ -309,6 +311,13 @@ ScrollView {
checked: Lith.settings.platformBufferControlPosition
}

Fields.Boolean {
id: bufferListShowsOnlyBuffersWithNewMessagesCheckbox
summary: qsTr("Show only buffers with new messages")
details: qsTr("When using the text filter, all buffers will be shown.")
checked: Lith.settings.bufferListShowsOnlyBuffersWithNewMessages
}

////////////////////////// URL HANDLING
Fields.Header {
text: qsTr("URL handling")
Expand Down

0 comments on commit ac643ae

Please sign in to comment.